Give SentLine (and preproc.send events) a new event object, to track events

related to a line after it is sent
This commit is contained in:
jesopo 2019-06-04 17:51:20 +01:00
parent 825dce5292
commit 8dbae6a5e9
6 changed files with 18 additions and 19 deletions

View file

@ -45,7 +45,7 @@ class Module(ModuleManager.BaseModule):
:permission: reconnect :permission: reconnect
""" """
line = event["server"].send_quit("Reconnecting") 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)) event["server"].id, event["server"].connection_params))
@utils.hook("received.command.connect", min_args=1) @utils.hook("received.command.connect", min_args=1)
@ -97,9 +97,9 @@ class Module(ModuleManager.BaseModule):
reason = event["args"] or "" reason = event["args"] or ""
for server in self.bot.servers.values(): for server in self.bot.servers.values():
line = server.send_quit(reason) 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_hook(self, server):
def shutdown(): def shutdown(e):
server.disconnect() server.disconnect()
self.bot.disconnect(server) self.bot.disconnect(server)
return shutdown return shutdown

View file

@ -26,7 +26,7 @@ class Module(ModuleManager.BaseModule):
line = utils.irc.protocol.quit("Shutting down") line = utils.irc.protocol.quit("Shutting down")
sent_line = server.send(line, immediate=True) 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 server.send_enabled = False
written = True written = True
@ -35,7 +35,7 @@ class Module(ModuleManager.BaseModule):
sys.exit() sys.exit()
def _make_hook(self, server): def _make_hook(self, server):
return lambda: self.bot.disconnect(server) return lambda e: self.bot.disconnect(server)
def SIGUSR1(self, signum, frame): def SIGUSR1(self, signum, frame):
self.bot.trigger(self._reload_config) self.bot.trigger(self._reload_config)

View file

@ -57,6 +57,9 @@ class EventHook(object):
self._stored_events = [] # type: typing.List[typing.Dict] self._stored_events = [] # type: typing.List[typing.Dict]
self._context_hooks = {} # type: typing.Dict[str, typing.List[EventCallback]] 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: def _make_event(self, kwargs: dict) -> Event:
return Event(self._get_path(), **kwargs) return Event(self._get_path(), **kwargs)
def make_event(self, **kwargs): def make_event(self, **kwargs):

View file

@ -85,13 +85,13 @@ class ParsedLine(object):
return " ".join(pieces).split("\n")[0].strip("\r") return " ".join(pieces).split("\n")[0].strip("\r")
class SentLine(IRCObject.Object): class SentLine(IRCObject.Object):
def __init__(self, send_time: datetime.datetime, hostmask: str, def __init__(self, events: "EventManager.EventHook",
line: ParsedLine): send_time: datetime.datetime, hostmask: str, line: ParsedLine):
self.events = events
self.send_time = send_time self.send_time = send_time
self._hostmask = hostmask self._hostmask = hostmask
self.parsed_line = line self.parsed_line = line
self._on_send: typing.List[typing.Callable[[], None]] = []
self.truncate_marker: typing.Optional[str] = None self.truncate_marker: typing.Optional[str] = None
def __repr__(self) -> str: def __repr__(self) -> str:
@ -135,9 +135,3 @@ class SentLine(IRCObject.Object):
return self._for_wire().decode("utf8") return self._for_wire().decode("utf8")
def truncated(self) -> str: def truncated(self) -> str:
return self._encode_truncate()[1] 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()

View file

@ -246,14 +246,16 @@ class Server(IRCObject.Object):
if not self.send_enabled: if not self.send_enabled:
return None return None
line_events = self.events.new_root()
self.events.on("preprocess.send").on(line_parsed.command 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, self.events.on("preprocess.send").call_unsafe(server=self,
line=line_parsed) line=line_parsed, events=line_events)
line = line_parsed.format() line = line_parsed.format()
line_obj = IRCLine.SentLine(datetime.datetime.utcnow(), self.hostmask(), line_obj = IRCLine.SentLine(line_events, datetime.datetime.utcnow(),
line_parsed) self.hostmask(), line_parsed)
self.socket.send(line_obj, immediate=immediate) self.socket.send(line_obj, immediate=immediate)
return line_obj return line_obj
def send_raw(self, line: str): def send_raw(self, line: str):

View file

@ -149,7 +149,7 @@ class Socket(IRCObject.Object):
sent_lines = [] # type: typing.List[IRCLine.SentLine] sent_lines = [] # type: typing.List[IRCLine.SentLine]
for i in range(sent_lines_count): for i in range(sent_lines_count):
sent_line = self._buffered_lines.pop(0) sent_line = self._buffered_lines.pop(0)
sent_line.sent() sent_line.events.on("send").call()
sent_lines.append(sent_line) sent_lines.append(sent_line)
self._write_buffer = self._write_buffer[bytes_written_i:] self._write_buffer = self._write_buffer[bytes_written_i:]