diff options
Diffstat (limited to 'util.py')
-rw-r--r-- | util.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/util.py b/util.py new file mode 100644 index 0000000..8538ef3 --- /dev/null +++ b/util.py @@ -0,0 +1,95 @@ +import irctokens +import time +from fnmatch import fnmatchcase + + +class Util: + def __init__(self, config, sock): + self.sock = sock + self.config = config + self.target = "" + self.dict = { + "\x00": "\\x00", + "\x01": "\\x01", + "\x02": "\\x02", + "\x03": "\\x03", + "\x04": "\\x04", + "\x05": "\\x05", + "\x06": "\\x06", + "\x07": "\\x07", + "\x08": "\\x08", + "\x09": "\\x09", + "\x0a": "\\x0a", + "\x0b": "\\x0b", + "\x0c": "\\x0c", + "\x0d": "\\x0d", + "\x0e": "\\x0e", + "\x0f": "\\x0f", + "\x10": "\\x10", + "\x11": "\\x11", + "\x12": "\\x12", + "\x13": "\\x13", + "\x14": "\\x14", + "\x15": "\\x15", + "\x16": "\\x16", + "\x17": "\\x17", + "\x18": "\\x18", + "\x19": "\\x19", + "\x1a": "\\x1a", + "\x1b": "\\x1b", + "\x1c": "\\x1c", + "\x1d": "\\x1d", + "\x1e": "\\x1e", + "\x1f": "\\x1f", + "\x7f": "\\x7f", + } + + def send(self, raw: str): + stri = raw + for k, v in self.dict.items(): + stri = stri.replace(k, v) + print(f"{time.strftime('%H:%M:%S')} > {stri}") + self.sock.sendall(f"{raw}\r\n".encode("utf8")) + + def quit(self, msg=None): + if msg != None: + self.send("QUIT :" + msg) + else: + self.send("QUIT") + + def nick(self, nick=None): + if nick == None: + self.send("NICK " + self.config.self.nick) + else: + self.send("NICK " + nick) + + def _m(self, msg: str, t=None): + if t == None: + t = self.target + msg = str(msg).partition("\n")[0] + if len(msg) >= 460: + msg = msg[:460] + self.mesg("message too long!") + return t, msg + + def mesg(self, msg: str, t=None): + t, msg = self._m(msg, t) + self.send(irctokens.build("PRIVMSG", [t, str(msg)]).format()) + + def action(self, msg: str, t=None): + t, msg = self._m(msg, t) + self.send( + irctokens.build("PRIVMSG", [t, "\x01ACTION " + str(msg) + "\x01"]).format() + ) + + def notice(self, msg: str, t=None): + t, msg = self._m(msg, t) + self.send(irctokens.build("NOTICE", [t, str(msg)]).format()) + + def maskmatch(self, string: str, hostmask: str): + """DOES NOT HANDLE CASEMAPPING (yet), just dumb case-sensitive match, only ? and * are special""" + print("string is", string, "and hostmask is", hostmask) + pat = "[[]".join( + [x.replace("]", "[]]") for x in hostmask.split("[")] + ) # escape all [ and ] into [[] and []] + return fnmatchcase(string, pat) |