diff --git a/src/context.py b/src/context.py index 9052aee..7ef0b63 100644 --- a/src/context.py +++ b/src/context.py @@ -1,3 +1,4 @@ +from collections import defaultdict from operator import attrgetter from src.logger import debuglog @@ -39,6 +40,8 @@ def context_types(*types): class IRCContext: """Base class for channels and users.""" + _messages = defaultdict(list) + def __init__(self, name, client): self.name = name self.client = client @@ -54,6 +57,27 @@ class IRCContext: return "NOTICE" return "PRIVMSG" + def queue_message(self, message): + if self.is_fake: + self.send(message) # Don't actually queue it + return + + self._messages[message].append(self) + + @classmethod + def send_messages(cls, *, notice=False, privmsg=False): + for message, targets in cls._messages.items(): + send_types = defaultdict(list) + for target in targets: + send_types[target.get_send_type(is_notice=notice, is_privmsg=privmsg)].append(target) + for send_type, targets in send_types.items(): + max_targets = Features["TARGMAX"][send_type] + while targets: + using, targets = targets[:max_targets], targets[max_targets:] + cls._send([message], "", " ", targets[0].client, send_type, ",".join([t.nick for t in using])) + + cls._messages.clear() + @classmethod def get_context_type(cls, *, max_types=1): context_type = [] diff --git a/src/users.py b/src/users.py index 545f2ce..1624f12 100644 --- a/src/users.py +++ b/src/users.py @@ -1,4 +1,3 @@ -from collections import defaultdict from weakref import WeakSet import fnmatch import re @@ -169,8 +168,6 @@ class User(IRCContext): is_user = True - _messages = defaultdict(list) - def __new__(cls, cli, nick, ident, host, realname, account): self = super().__new__(cls) super(User, self).__init__(nick, cli) @@ -408,23 +405,6 @@ class User(IRCContext): return amount - def queue_message(self, message): - self._messages[message].append(self) - - @classmethod - def send_messages(cls, *, notice=False, privmsg=False): - for message, targets in cls._messages.items(): - send_types = defaultdict(list) - for target in targets: - send_types[target.get_send_type(is_notice=notice, is_privmsg=privmsg)].append(target) - for send_type, targets in send_types.items(): - max_targets = Features["TARGMAX"][send_type] - while targets: - using, targets = targets[:max_targets], targets[max_targets:] - cls._send([message], "", " ", targets[0].client, send_type, ",".join([t.nick for t in using])) - - cls._messages.clear() - @property def nick(self): # name should be the same as nick (for length calculation) return self.name @@ -508,9 +488,6 @@ class FakeUser(User): def __hash__(self): return hash(self.nick) - def queue_message(self, message): - self.send(message) # don't actually queue it - @property def nick(self): return self.name