Skip to content

Instantly share code, notes, and snippets.

@kaecy
Last active December 12, 2021 12:58
Show Gist options
  • Save kaecy/9072476040b42f5c15a5478bdad6a911 to your computer and use it in GitHub Desktop.
Save kaecy/9072476040b42f5c15a5478bdad6a911 to your computer and use it in GitHub Desktop.
import curses, birc
# Primitive input method for curses.
def read(i):
buffer = ""
i.erase()
i.addstr("> ")
while True:
key = i.getch()
yx = i.getyx()
if key == 8:
if yx != (0, 2):
if yx[1] == 0:
i.move(0, curses.COLS - 1)
i.delch()
buffer = buffer[:-1]
else:
i.move(yx[0], yx[1] - 1)
i.delch()
buffer = buffer[:-1]
elif key != 10:
if yx != (1, curses.COLS - 1):
i.addch(key)
buffer = buffer + chr(key)
elif key == 10:
return buffer + "\n"
def changeChannels(target):
global activeChan
activeChan = target
s.erase()
s.addstr("Ch: " + target)
s.refresh()
o.erase()
for msg in irc.channel[target].messages:
o.addstr(msg.origin.name + ": " + msg.content + "\n")
o.refresh()
def processCommands(command):
spIndex = command.find(" ")
if spIndex > -1:
command, params = command.split(" ")
target = params
if command == "/j":
if target not in irc.channel.keys():
irc.join(params)
changeChannels(target)
if command == "/s":
if target in irc.channel.keys():
changeChannels(target)
def log(msg):
if msg.type == "001":
o.addstr("Connected\n")
if msg.type == "join" or msg.type == "part" and msg.content[0] == activeChan:
o.addstr(msg.origin.name + " " + msg.type + "ed " + msg.content[0] + "\n")
if msg.type == "privmsg" and msg.target == activeChan:
o.addstr(msg.origin.name + ": " + msg.content + "\n")
if msg.type == "ping":
o.addstr("PING\n")
o.refresh()
i.refresh()
def rawLog(msg):
o.addstr(msg)
o.refresh()
i.refresh()
# IRC lib init.
activeChan = "#testing"
irc = birc.IRCMessageLoop("irc.libera.chat", 6665)
irc.nick("christine")
irc.join(activeChan)
irc.log = log
# Curses lib init.
stdscr = curses.initscr()
curses.noecho()
s = stdscr.subwin(1, curses.COLS, 0, 0)
s.addstr("Ch: " + activeChan)
s.refresh()
o = stdscr.subwin(curses.LINES - 3, curses.COLS, 1, 0)
o.scrollok(True)
i = stdscr.subwin(2, curses.COLS, curses.LINES - 2, 0)
# Start the IRC message loop off.
irc.start()
while True:
input = read(i)
if input.startswith("/"):
# Process commands.
processCommands(input.strip())
else:
# Send message.
irc.msg(activeChan, input.strip())
rawLog("%s: " % irc.name + input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment