aboutsummaryrefslogtreecommitdiff
path: root/src/IRCBuffer.py
blob: 3e8f5aa9e1ea877eab759f21ceabfb0bdb840c4b (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
import collections, dataclasses, re, typing
from src import IRCBot, IRCServer, utils

MAX_LINES = 64

@dataclasses.dataclass
class BufferLine(object):
    sender: str
    message: str
    action: bool
    tags: dict
    from_self: bool
    method: str
    notes: typing.Dict[str, str] = dataclasses.field(
        default_factory=dict)

class BufferLineMatch(object):
    def __init__(self, line: BufferLine, match: str):
        self.line = line
        self.match = match

class Buffer(object):
    def __init__(self, bot: "IRCBot.Bot", server: "IRCServer.Server"):
        self.bot = bot
        self.server = server
        self._lines = collections.deque(maxlen=MAX_LINES
            ) # type: typing.Deque[BufferLine]

    def add(self, line: BufferLine):
        self._lines.appendleft(line)

    def get(self, index: int=0, **kwargs) -> typing.Optional[BufferLine]:
        from_self = kwargs.get("from_self", True)
        for line in self._lines:
            if line.from_self and not from_self:
                continue
            return line
        return None
    def get_all(self, for_user: typing.Optional[str]=None):
        if not for_user == None:
            for line in self._lines:
                if self.server.irc_lower(line.sender) == for_user:
                    yield line
        else:
            for line in self._lines:
                yield line
    def find(self, pattern: typing.Union[str, typing.Pattern[str]], **kwargs
            ) -> typing.Optional[BufferLineMatch]:
        from_self = kwargs.get("from_self", True)
        for_user = kwargs.get("for_user", "")
        for_user = self.server.irc_lower(for_user) if for_user else None
        not_pattern = kwargs.get("not_pattern", None)
        for line in self._lines:
            if line.from_self and not from_self:
                continue
            else:
                match = re.search(pattern, line.message)
                if match:
                    if not_pattern and re.search(not_pattern, line.message):
                        continue
                    if for_user and not self.server.irc_lower(line.sender
                            ) == for_user:
                        continue
                    return BufferLineMatch(line, match.group(0))
        return None

    def find_from(self, nickname: str) -> typing.Optional[BufferLine]:
        lines = self.find_many_from(nickname, 1)
        if lines:
            return lines[0]
        else:
            return None
    def find_many_from(self, nickname: str, max: int
            ) -> typing.List[BufferLine]:
        nickname_lower = self.server.irc_lower(nickname)
        found_lines = []
        for line in self._lines:
            if (not line.from_self
                    and self.server.irc_lower(line.sender) == nickname_lower):
                found_lines.append(line)
                if len(found_lines) == max:
                    break
        return found_lines