bot6/util.py

96 lines
2.8 KiB
Python
Raw Permalink Normal View History

2021-09-16 17:35:06 +00:00
import irctokens
2021-10-07 19:36:09 +00:00
import time
from fnmatch import fnmatchcase
2021-10-04 12:48:10 +00:00
2021-09-16 17:35:06 +00:00
class Util:
2021-10-04 12:48:10 +00:00
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",
2021-10-04 16:32:47 +00:00
"\x7f": "\\x7f",
}
2021-10-04 12:48:10 +00:00
def send(self, raw: str):
stri = raw
for k, v in self.dict.items():
stri = stri.replace(k, v)
2021-10-07 19:36:09 +00:00
print(f"{time.strftime('%H:%M:%S')} > {stri}")
2021-09-16 17:35:06 +00:00
self.sock.sendall(f"{raw}\r\n".encode("utf8"))
2021-10-04 12:48:10 +00:00
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
2021-10-04 12:48:10 +00:00
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)
2021-10-04 12:48:10 +00:00
self.send(irctokens.build("PRIVMSG", [t, str(msg)]).format())
2021-10-07 21:22:53 +00:00
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())
2022-10-22 18:13:58 +00:00
def maskmatch(self, string: str, hostmask: str):
"""DOES NOT HANDLE CASEMAPPING (yet), just dumb case-sensitive match, only ? and * are special"""
2022-10-22 18:13:58 +00:00
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)