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
"""
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

View file

@ -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)

View file

@ -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):

View file

@ -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()

View file

@ -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):

View file

@ -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:]