2018-10-30 14:58:48 +00:00
|
|
|
import re, typing
|
2018-10-30 17:49:35 +00:00
|
|
|
from src import IRCBot, IRCServer, utils
|
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
|
2018-08-29 11:55:20 +00:00
|
|
|
self.lines = []
|
|
|
|
self.max_lines = 64
|
|
|
|
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)
|
2018-08-29 11:55:20 +00:00
|
|
|
self.lines.insert(0, line)
|
|
|
|
if len(self.lines) > self.max_lines:
|
|
|
|
self.lines.pop()
|
|
|
|
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)
|
|
|
|
for line in self.lines:
|
|
|
|
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", "")
|
2018-10-30 14:58:48 +00:00
|
|
|
for_user = utils.irc.lower(self.server.case_mapping, for_user
|
2018-09-11 09:15:16 +00:00
|
|
|
) if for_user else None
|
2018-08-29 11:55:20 +00:00
|
|
|
not_pattern = kwargs.get("not_pattern", None)
|
|
|
|
for line in self.lines:
|
|
|
|
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
|
2018-10-30 14:58:48 +00:00
|
|
|
if for_user and not utils.irc.lower(self.server.case_mapping,
|
|
|
|
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
|
2018-08-29 11:55:20 +00:00
|
|
|
def skip_next(self):
|
|
|
|
self._skip_next = True
|