summary refs log tree commit diff
path: root/util.py
blob: 8538ef3e988e609616690dd655a5c588b7fc1818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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)