2018-08-29 11:55:20 +00:00
|
|
|
import re
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import utils
|
2018-08-29 11:55:20 +00:00
|
|
|
|
|
|
|
class BufferLine(object):
|
2018-10-02 20:40:34 +00:00
|
|
|
def __init__(self, sender, message, action, tags, from_self, method):
|
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-09-11 09:15:16 +00:00
|
|
|
def __init__(self, bot, server):
|
|
|
|
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
|
|
|
|
|
|
|
def _add_message(self, sender, message, action, tags, from_self, method):
|
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-02 20:40:34 +00:00
|
|
|
def add_message(self, sender, message, action, tags, from_self=False):
|
|
|
|
self._add_message(sender, message, action, tags, from_self, "PRIVMSG")
|
|
|
|
def add_notice(self, sender, message, tags, from_self=False):
|
|
|
|
self._add_message(sender, message, False, tags, from_self, "NOTICE")
|
|
|
|
|
2018-08-29 11:55:20 +00:00
|
|
|
def get(self, index=0, **kwargs):
|
|
|
|
from_self = kwargs.get("from_self", True)
|
|
|
|
for line in self.lines:
|
|
|
|
if line.from_self and not from_self:
|
|
|
|
continue
|
|
|
|
return line
|
|
|
|
def find(self, pattern, **kwargs):
|
|
|
|
from_self = kwargs.get("from_self", True)
|
|
|
|
for_user = kwargs.get("for_user", "")
|
2018-10-03 12:22:37 +00:00
|
|
|
for_user = utils.irc.lower(self.server, 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-03 12:22:37 +00:00
|
|
|
if for_user and not utils.irc.lower(self.server, line.sender
|
2018-09-11 09:15:16 +00:00
|
|
|
) == for_user:
|
2018-08-29 11:55:20 +00:00
|
|
|
continue
|
|
|
|
return line
|
|
|
|
def skip_next(self):
|
|
|
|
self._skip_next = True
|