aboutsummaryrefslogtreecommitdiff
path: root/src/IRCLine.py
blob: 69c83006accba9539a9d1ff897680b0b82202752 (about) (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import datetime, typing
from src import IRCObject

# this should be 510 (RFC1459, 512 with \r\n) but a server BitBot uses is broken
LINE_CUTOFF = 470

class IRCArgs(object):
    def __init__(self, args: typing.List[str]):
        self._args = args

    def get(self, index: int) -> typing.Optional[str]:
        if len(self._args) > index:
            return self._args[index]
        return None

    def __repr__(self):
        return "IRCArgs(%s)" % self._args
    def __len__(self) -> int:
        return len(self._args)
    def __getitem__(self, index: int) -> str:
        return self._args[index]
    def __setitem__(self, index: int, value: str):
        self._args[index] = value

class Hostmask(object):
    def __init__(self, nickname: str, username: str, hostname: str,
            hostmask: str):
        self.nickname = nickname
        self.username = username
        self.hostname = hostname
        self.hostmask = hostmask
    def __repr__(self):
        return "Hostmask(%s)" % self.__str__()
    def __str__(self):
        return self.hostmask

class ParsedLine(object):
    def __init__(self, command: str, args: typing.List[str],
            prefix: Hostmask=None,
            tags: typing.Dict[str, str]={}):
        self.command = command
        self._args = args
        self.args = IRCArgs(args)
        self.prefix = prefix
        self.tags = {} if tags == None else tags

    def _tag_str(self, tags: typing.Dict[str, str]) -> str:
        tag_str = ""
        for tag, value in tags.items():
            if tag_str:
                tag_str += ","
            tag_str += tag
            if value:
                tag_str += "=%s" % value
        if tag_str:
            tag_str = "@%s" % tag_str
        return tag_str

    def format(self) -> str:
        s = ""
        if self.tags:
            s += "%s " % self._tag_str(self.tags)

        if self.prefix:
            s += "%s " % self.prefix

        s += self.command.upper()

        if self.args:
            if len(self._args) > 1:
                s += " %s" % " ".join(self._args[:-1])

            s += " "
            if " " in self._args[-1] or self._args[-1][0] == ":":
                s += ":%s" % self._args[-1]
            else:
                s += self._args[-1]

        return s.split("\n")[0].strip("\r")

class SentLine(IRCObject.Object):
    def __init__(self, send_time: datetime.datetime, hostmask: str,
            line: ParsedLine):
        self.send_time = send_time
        self._hostmask = hostmask
        self.parsed_line = line

        self._on_send: typing.List[typing.Callable[[], None]] = []
        self.truncate_marker: typing.Optional[str] = None

    def __repr__(self) -> str:
        return "IRCLine.SentLine(%s)" % self.__str__()
    def __str__(self) -> str:
        return self.decoded_data()

    def _char_limit(self) -> int:
        return LINE_CUTOFF-len(":%s " % self._hostmask)

    def _encode_truncate(self) -> typing.Tuple[bytes, str]:
        line = self.parsed_line.format()
        byte_max = self._char_limit()
        encoded = b""
        truncated = ""
        truncate_marker = b""
        if not self.truncate_marker == None:
            truncate_marker = typing.cast(str, self.truncate_marker
                ).encode("utf8")

        for i, character in enumerate(line):
            encoded_character = character.encode("utf8")
            new_len = len(encoded + encoded_character)
            if truncate_marker and (byte_max-new_len) < len(truncate_marker):
                encoded += truncate_marker
                truncated = line[i:]
                break
            elif new_len > byte_max:
                truncated = line[i:]
                break
            else:
                encoded += encoded_character
        return (encoded, truncated)

    def _data(self) -> bytes:
        return self._encode_truncate()[0]
    def data(self) -> bytes:
        return b"%s\r\n" % self._data()
    def decoded_data(self) -> str:
        return self._data().decode("utf8")
    def truncated(self) -> str:
        return self._encode_truncate()[1]

    def on_send(self, func: typing.Callable[[], None]):
        self._on_send.append(func)
    def sent(self):
        for func in self._on_send[:]:
            func()