import irctokens import time 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())