diff --git a/modules/admin.py b/modules/admin.py index 765758ca..3128afb8 100644 --- a/modules/admin.py +++ b/modules/admin.py @@ -45,7 +45,7 @@ class Module(ModuleManager.BaseModule): :permission: reconnect """ line = event["server"].send_quit("Reconnecting") - line.on_send(lambda: self.bot.reconnect( + line.events.on("send").hook(lambda e: self.bot.reconnect( event["server"].id, event["server"].connection_params)) @utils.hook("received.command.connect", min_args=1) @@ -97,9 +97,9 @@ class Module(ModuleManager.BaseModule): reason = event["args"] or "" for server in self.bot.servers.values(): line = server.send_quit(reason) - line.on_send(self._shutdown_hook(server)) + line.events.on("send").hook(self._shutdown_hook(server)) def _shutdown_hook(self, server): - def shutdown(): + def shutdown(e): server.disconnect() self.bot.disconnect(server) return shutdown diff --git a/modules/signals.py b/modules/signals.py index 8e31d03e..0be15692 100644 --- a/modules/signals.py +++ b/modules/signals.py @@ -26,7 +26,7 @@ class Module(ModuleManager.BaseModule): line = utils.irc.protocol.quit("Shutting down") sent_line = server.send(line, immediate=True) - sent_line.on_send(self._make_hook(server)) + sent_line.events.on("send").hook(self._make_hook(server)) server.send_enabled = False written = True @@ -35,7 +35,7 @@ class Module(ModuleManager.BaseModule): sys.exit() def _make_hook(self, server): - return lambda: self.bot.disconnect(server) + return lambda e: self.bot.disconnect(server) def SIGUSR1(self, signum, frame): self.bot.trigger(self._reload_config) diff --git a/src/EventManager.py b/src/EventManager.py index 18006058..473386c2 100644 --- a/src/EventManager.py +++ b/src/EventManager.py @@ -57,6 +57,9 @@ class EventHook(object): self._stored_events = [] # type: typing.List[typing.Dict] self._context_hooks = {} # type: typing.Dict[str, typing.List[EventCallback]] + def new_root(self) -> "EventHook": + return EventHook(self.log) + def _make_event(self, kwargs: dict) -> Event: return Event(self._get_path(), **kwargs) def make_event(self, **kwargs): diff --git a/src/IRCLine.py b/src/IRCLine.py index 53f328b8..2a52bf23 100644 --- a/src/IRCLine.py +++ b/src/IRCLine.py @@ -85,13 +85,13 @@ class ParsedLine(object): return " ".join(pieces).split("\n")[0].strip("\r") class SentLine(IRCObject.Object): - def __init__(self, send_time: datetime.datetime, hostmask: str, - line: ParsedLine): + def __init__(self, events: "EventManager.EventHook", + send_time: datetime.datetime, hostmask: str, line: ParsedLine): + self.events = events self.send_time = send_time self._hostmask = hostmask self.parsed_line = line - self._on_send: typing.List[typing.Callable[[], None]] = [] self.truncate_marker: typing.Optional[str] = None def __repr__(self) -> str: @@ -135,9 +135,3 @@ class SentLine(IRCObject.Object): return self._for_wire().decode("utf8") def truncated(self) -> str: return self._encode_truncate()[1] - - def on_send(self, func: typing.Callable[[], None]): - self._on_send.append(func) - def sent(self): - for func in self._on_send[:]: - func() diff --git a/src/IRCServer.py b/src/IRCServer.py index 29a705c0..a8bd12ea 100644 --- a/src/IRCServer.py +++ b/src/IRCServer.py @@ -246,14 +246,16 @@ class Server(IRCObject.Object): if not self.send_enabled: return None + line_events = self.events.new_root() + self.events.on("preprocess.send").on(line_parsed.command - ).call_unsafe(server=self, line=line_parsed) + ).call_unsafe(server=self, line=line_parsed, events=line_events) self.events.on("preprocess.send").call_unsafe(server=self, - line=line_parsed) + line=line_parsed, events=line_events) line = line_parsed.format() - line_obj = IRCLine.SentLine(datetime.datetime.utcnow(), self.hostmask(), - line_parsed) + line_obj = IRCLine.SentLine(line_events, datetime.datetime.utcnow(), + self.hostmask(), line_parsed) self.socket.send(line_obj, immediate=immediate) return line_obj def send_raw(self, line: str): diff --git a/src/IRCSocket.py b/src/IRCSocket.py index 4853a23f..3b3395e8 100644 --- a/src/IRCSocket.py +++ b/src/IRCSocket.py @@ -149,7 +149,7 @@ class Socket(IRCObject.Object): sent_lines = [] # type: typing.List[IRCLine.SentLine] for i in range(sent_lines_count): sent_line = self._buffered_lines.pop(0) - sent_line.sent() + sent_line.events.on("send").call() sent_lines.append(sent_line) self._write_buffer = self._write_buffer[bytes_written_i:]