2019-11-27 12:07:15 +00:00
|
|
|
import collections, dataclasses, re, typing
|
2018-10-30 17:49:35 +00:00
|
|
|
from src import IRCBot, IRCServer, utils
|
2018-08-29 11:55:20 +00:00
|
|
|
|
2019-06-07 12:31:51 +00:00
|
|
|
MAX_LINES = 64
|
|
|
|
|
2019-11-27 12:07:15 +00:00
|
|
|
@dataclasses.dataclass
|
2018-08-29 11:55:20 +00:00
|
|
|
class BufferLine(object):
|
2020-01-17 14:19:30 +00:00
|
|
|
id: str
|
2019-11-27 12:07:15 +00:00
|
|
|
sender: str
|
|
|
|
message: str
|
|
|
|
action: bool
|
|
|
|
tags: dict
|
|
|
|
from_self: bool
|
|
|
|
method: str
|
2019-11-27 12:59:36 +00:00
|
|
|
notes: typing.Dict[str, str] = dataclasses.field(
|
|
|
|
default_factory=dict)
|
2020-01-17 14:19:46 +00:00
|
|
|
deleted: bool=False
|
2018-08-29 11:55:20 +00:00
|
|
|
|
2019-08-13 12:48:03 +00:00
|
|
|
class BufferLineMatch(object):
|
|
|
|
def __init__(self, line: BufferLine, match: str):
|
|
|
|
self.line = line
|
|
|
|
self.match = match
|
|
|
|
|
2018-08-29 11:55:20 +00:00
|
|
|
class Buffer(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, bot: "IRCBot.Bot", server: "IRCServer.Server"):
|
2018-09-11 09:15:16 +00:00
|
|
|
self.bot = bot
|
|
|
|
self.server = server
|
2019-06-07 12:34:30 +00:00
|
|
|
self._lines = collections.deque(maxlen=MAX_LINES
|
|
|
|
) # type: typing.Deque[BufferLine]
|
2018-10-02 20:40:34 +00:00
|
|
|
|
2019-11-30 20:25:52 +00:00
|
|
|
def add(self, line: BufferLine):
|
2019-11-27 12:06:57 +00:00
|
|
|
self._lines.appendleft(line)
|
2018-10-02 20:40:34 +00:00
|
|
|
|
2020-01-17 14:19:46 +00:00
|
|
|
def get(self, index: int=0, from_self=True, deleted=False
|
|
|
|
) -> typing.Optional[BufferLine]:
|
2019-06-07 12:31:51 +00:00
|
|
|
for line in self._lines:
|
2018-08-29 11:55:20 +00:00
|
|
|
if line.from_self and not from_self:
|
|
|
|
continue
|
2020-01-17 14:19:46 +00:00
|
|
|
if line.deleted and not deleted:
|
|
|
|
continue
|
2018-08-29 11:55:20 +00:00
|
|
|
return line
|
2018-10-31 15:12:46 +00:00
|
|
|
return None
|
2019-11-27 12:59:50 +00:00
|
|
|
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
|
2020-01-17 14:19:30 +00:00
|
|
|
|
2020-01-17 14:27:37 +00:00
|
|
|
def find(self, pattern: typing.Union[str, typing.Pattern[str]],
|
|
|
|
not_pattern: typing.Union[str, typing.Pattern[str]],
|
|
|
|
from_self=True, for_user: str=None, deleted=False
|
2019-08-13 12:48:03 +00:00
|
|
|
) -> typing.Optional[BufferLineMatch]:
|
2020-01-17 14:27:37 +00:00
|
|
|
if for_user:
|
|
|
|
for_user = self.server.irc_lower(for_user)
|
|
|
|
|
2019-06-07 12:31:51 +00:00
|
|
|
for line in self._lines:
|
2018-08-29 11:55:20 +00:00
|
|
|
if line.from_self and not from_self:
|
|
|
|
continue
|
2019-08-13 12:48:03 +00:00
|
|
|
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
|
2020-01-17 14:27:37 +00:00
|
|
|
if line.deleted and not deleted:
|
|
|
|
continue
|
2019-08-15 12:44:57 +00:00
|
|
|
return BufferLineMatch(line, match.group(0))
|
2018-10-31 15:12:46 +00:00
|
|
|
return None
|
2019-01-26 15:46:20 +00:00
|
|
|
|
2020-01-17 14:19:30 +00:00
|
|
|
def find_id(self, id: str) -> typing.Optional[BufferLine]:
|
|
|
|
for line in self._lines:
|
|
|
|
if line.id == id:
|
|
|
|
return line
|
|
|
|
return None
|
|
|
|
|
2019-01-26 15:46:20 +00:00
|
|
|
def find_from(self, nickname: str) -> typing.Optional[BufferLine]:
|
2019-10-25 22:32:00 +00:00
|
|
|
lines = self.find_many_from(nickname, 1)
|
|
|
|
if lines:
|
|
|
|
return lines[0]
|
|
|
|
else:
|
|
|
|
return None
|
2019-09-26 12:44:21 +00:00
|
|
|
def find_many_from(self, nickname: str, max: int
|
|
|
|
) -> typing.List[BufferLine]:
|
2019-01-26 15:46:20 +00:00
|
|
|
nickname_lower = self.server.irc_lower(nickname)
|
2019-09-26 12:44:21 +00:00
|
|
|
found_lines = []
|
2019-06-07 12:31:51 +00:00
|
|
|
for line in self._lines:
|
2019-01-26 15:58:57 +00:00
|
|
|
if (not line.from_self
|
|
|
|
and self.server.irc_lower(line.sender) == nickname_lower):
|
2019-09-26 12:44:21 +00:00
|
|
|
found_lines.append(line)
|
|
|
|
if len(found_lines) == max:
|
|
|
|
break
|
|
|
|
return found_lines
|