2019-06-07 12:31:51 +00:00
|
|
|
import collections, 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
|
|
|
|
|
2018-08-29 11:55:20 +00:00
|
|
|
class BufferLine(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, sender: str, message: str, action: bool, tags: dict,
|
|
|
|
from_self: bool, method: str):
|
2018-08-29 11:55:20 +00:00
|
|
|
self.sender = sender
|
|
|
|
self.message = message
|
|
|
|
self.action = action
|
2018-10-01 16:26:31 +00:00
|
|
|
self.tags = tags
|
2018-08-29 11:55:20 +00:00
|
|
|
self.from_self = from_self
|
2018-10-02 20:40:34 +00:00
|
|
|
self.method = method
|
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-08-29 11:55:20 +00:00
|
|
|
self._skip_next = False
|
2018-10-02 20:40:34 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def _add_message(self, sender: str, message: str, action: bool, tags: dict,
|
|
|
|
from_self: bool, method: str):
|
2018-08-29 11:55:20 +00:00
|
|
|
if not self._skip_next:
|
2018-10-02 20:40:34 +00:00
|
|
|
line = BufferLine(sender, message, action, tags, from_self, method)
|
2019-06-07 12:31:51 +00:00
|
|
|
self._lines.appendleft(line)
|
2018-08-29 11:55:20 +00:00
|
|
|
self._skip_next = False
|
2018-10-30 14:58:48 +00:00
|
|
|
def add_message(self, sender: str, message: str, action: bool, tags: dict,
|
|
|
|
from_self: bool=False):
|
2018-10-02 20:40:34 +00:00
|
|
|
self._add_message(sender, message, action, tags, from_self, "PRIVMSG")
|
2018-10-30 14:58:48 +00:00
|
|
|
def add_notice(self, sender: str, message: str, tags: dict,
|
|
|
|
from_self: bool=False):
|
2018-10-02 20:40:34 +00:00
|
|
|
self._add_message(sender, message, False, tags, from_self, "NOTICE")
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get(self, index: int=0, **kwargs) -> typing.Optional[BufferLine]:
|
2018-08-29 11:55:20 +00:00
|
|
|
from_self = kwargs.get("from_self", True)
|
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
|
|
|
|
return line
|
2018-10-31 15:12:46 +00:00
|
|
|
return None
|
2018-10-30 14:58:48 +00:00
|
|
|
def find(self, pattern: typing.Union[str, typing.Pattern[str]], **kwargs
|
|
|
|
) -> typing.Optional[BufferLine]:
|
2018-08-29 11:55:20 +00:00
|
|
|
from_self = kwargs.get("from_self", True)
|
|
|
|
for_user = kwargs.get("for_user", "")
|
2019-01-24 17:12:50 +00:00
|
|
|
for_user = self.server.irc_lower(for_user) if for_user else None
|
2018-08-29 11:55:20 +00:00
|
|
|
not_pattern = kwargs.get("not_pattern", None)
|
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
|
|
|
|
elif re.search(pattern, line.message):
|
|
|
|
if not_pattern and re.search(not_pattern, line.message):
|
|
|
|
continue
|
2019-01-24 17:12:50 +00:00
|
|
|
if for_user and not self.server.irc_lower(line.sender
|
|
|
|
) == for_user:
|
2018-08-29 11:55:20 +00:00
|
|
|
continue
|
|
|
|
return line
|
2018-10-31 15:12:46 +00:00
|
|
|
return None
|
2019-01-26 15:46:20 +00:00
|
|
|
|
|
|
|
def find_from(self, nickname: str) -> typing.Optional[BufferLine]:
|
|
|
|
nickname_lower = self.server.irc_lower(nickname)
|
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-01-26 15:46:20 +00:00
|
|
|
return line
|
|
|
|
return None
|
|
|
|
|
2018-08-29 11:55:20 +00:00
|
|
|
def skip_next(self):
|
|
|
|
self._skip_next = True
|