diff options
author | Matt Arnold | 2022-02-20 18:33:55 -0500 |
---|---|---|
committer | Matt Arnold | 2022-02-20 18:33:55 -0500 |
commit | 13381bc12504eb0c2afcf193de6754666213739c (patch) | |
tree | bce8e9941a9a357f2a83a3989f5e859be85e4f78 /IRC.py | |
parent | 6938bd4862d8a934044b27277eab1773c0805819 (diff) |
Major rewrite to use a proper irc tokenizer json for configuration, and other such QOL improvements
Diffstat (limited to 'IRC.py')
-rw-r--r-- | IRC.py | 77 |
1 files changed, 56 insertions, 21 deletions
diff --git a/IRC.py b/IRC.py index 018756b..a67e65d 100644 --- a/IRC.py +++ b/IRC.py @@ -2,9 +2,16 @@ # Copyright (C) 2022 Matt Arnold import socket import sys +import irctokens import time class IRCBadMessage(Exception): pass +class IRCError(Exception): + pass + +def printred(s): + t = f"\033[1;31m {s} \033[0m\n" + print(t) def parsemsg(s): """Breaks a message from an IRC server into its prefix, command, and arguments. @@ -24,48 +31,76 @@ def parsemsg(s): command = args.pop(0) return prefix, command, args + LINEEND = '\r\n' class IRCBot: irc = None - def __init__(self, sock): + def __init__(self, sock, config=None): self.irc = sock + self.connected = False + self.config = config + + def send_cmd(self, line): + """Send an IRC Command, takes an IRC command string without the CRLF + Returns encoded msg on success raises IRCError on failure """ + if not self.connected: + raise IRCError("Not Connected") + encmsg = bytes(line.format() + LINEEND, 'UTF-8' ) + expected = len(encmsg) + if self.irc.send(encmsg) == expected: + return str(encmsg) + else: + raise IRCError("Unexpected Send Length") + def on_welcome(self, *args, **kwargs): + authmsg = irctokens.build("NICKSERV", ['IDENTIFY', self.config['nickpass']]) + self.send_cmd(authmsg) + joinmsg = irctokens.build("JOIN", [self.config['channel']]) + self.send_cmd(joinmsg) + + def send_privmsg(self, dst, msg): - self.irc.send(bytes("PRIVMSG " + dst + " :" + msg + LINEEND, "UTF-8")) + msg = irctokens.build("PRIVMSG",[dst, msg]) + self.send_cmd(msg) def send_quit(self, quitmsg): - msg = f'QUIT :{quitmsg}' + LINEEND + msg = irctokens.build("QUIT", [quitmsg]) print(msg) - self.irc.send(msg.encode('utf-8')) + self.send_cmd(msg) - def send_action(self, action_msg): + def send_action(self, action_msg, dst): pass - def connect(self, server, port, channel, botnick, botpass, botnickpass): + def connect(self, server, port, channel, botnick, botnickpass): + if self.config is None: + self.config = {} + self.config["hostname"] = server + self.config["port"] = port + self.config["nick"] = botnick + self.config["channel"] = channel + self.config["nickpass"] = botnickpass print("Connecting to: " + server) - self.irc.connect((server, port)) - - # Perform user authentication - self.irc.send(bytes("USER " + botnick + " " + botnick + - " " + botnick + " :python" + LINEEND, "UTF-8")) - self.irc.send(bytes("NICK " + botnick + LINEEND, "UTF-8")) - self.irc.send(bytes("NICKSERV IDENTIFY " + - botnickpass + " " + LINEEND, "UTF-8")) - time.sleep(5) - - # join the channel - self.irc.send(bytes("JOIN " + channel + LINEEND, "UTF-8")) + self.irc.connect((self.config["hostname"], self.config["port"])) + self.connected = True + # Perform user registration + usermsg = irctokens.build("USER", [botnick, "0","*", "RabbitEars Bot"]).format() + print(usermsg) + self.send_cmd(usermsg) + nickmsg = irctokens.build("NICK", [botnick]) + self.send_cmd(nickmsg) def get_response(self): - time.sleep(1) # Get the response resp = self.irc.recv(4096).decode("UTF-8") msg = parsemsg(resp) - - if msg[1] == 'PING': + nwmsg = irctokens.tokenise(resp) + + if nwmsg.command == "001": + self.on_welcome(nwmsg.params) + if nwmsg.command == 'PING': print('Sending pong') self.irc.send( bytes('PONG ' + LINEEND, "UTF-8")) |