Pass IRCChannel and IRCUser objects on formatted events where applicable

This commit is contained in:
jesopo 2019-03-06 13:36:18 +00:00
parent 5fb9eb5131
commit 9920721bc5

View file

@ -2,9 +2,10 @@ import datetime
from src import EventManager, ModuleManager, utils from src import EventManager, ModuleManager, utils
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _event(self, type, server, line, context): def _event(self, type, server, line, context, channel=None, user=None):
self.events.on("formatted").on(type).call(server=server, self.events.on("formatted").on(type).call(server=server,
context=context, line=utils.irc.parse_format(line)) context=context, line=utils.irc.parse_format(line), raw_line=line,
channel=channel, user=user)
def _mode_symbols(self, user, channel, server): def _mode_symbols(self, user, channel, server):
modes = channel.get_user_status(user) modes = channel.get_user_status(user)
@ -39,7 +40,7 @@ class Module(ModuleManager.BaseModule):
line = self._privmsg(event, event["channel"], user, nickname) line = self._privmsg(event, event["channel"], user, nickname)
self._event("message.channel", event["server"], line, self._event("message.channel", event["server"], line,
event["channel"].name) event["channel"].name, channel=event["channel"], user=user)
def _on_notice(self, event, sender, target): def _on_notice(self, event, sender, target):
return "(notice->%s) <%s> %s" % (target, sender, event["message"]) return "(notice->%s) <%s> %s" % (target, sender, event["message"])
@ -65,35 +66,41 @@ class Module(ModuleManager.BaseModule):
self._private_notice(event, event["server"].nickname, self._private_notice(event, event["server"].nickname,
event["user"].nickname) event["user"].nickname)
def _on_join(self, event, nickname): def _on_join(self, event, user):
line = "%s joined %s" % (nickname, event["channel"].name) line = "%s joined %s" % (user.nickname, event["channel"].name)
self._event("join", event["server"], line, event["channel"].name) self._event("join", event["server"], line, event["channel"].name,
channel=event["channel"], user=user)
@utils.hook("received.join") @utils.hook("received.join")
def join(self, event): def join(self, event):
self._on_join(event, event["user"].nickname) self._on_join(event, event["user"])
@utils.hook("self.join") @utils.hook("self.join")
def self_join(self, event): def self_join(self, event):
self._on_join(event, event["server"].nickname) self._on_join(event, event["server"].get_user(event["server"].nickname))
def _on_part(self, event, nickname): def _on_part(self, event, user):
reason = "" reason = ""
if event["reason"]: if event["reason"]:
reason = " (%s)" % event["reason"] reason = " (%s)" % event["reason"]
line = "%s left %s%s" % (nickname, event["channel"].name, reason) line = "%s left %s%s" % (user.nickname, event["channel"].name, reason)
self._event("part", event["server"], line, event["channel"].name) self._event("part", event["server"], line, event["channel"].name,
channel=event["channel"], user=user)
@utils.hook("received.part") @utils.hook("received.part")
def part(self, event): def part(self, event):
self._on_part(event, event["user"].nickname) self._on_part(event, event["user"])
@utils.hook("self.part") @utils.hook("self.part")
def self_part(self, event): def self_part(self, event):
self._on_part(event, event["server"].nickname) self._on_part(event, event["server"].get_user(event["server"].nickname))
@utils.hook("received.nick") def _on_nick(self, event, user):
@utils.hook("self.nick")
def on_nick(self, event):
line = "%s changed nickname to %s" % ( line = "%s changed nickname to %s" % (
event["old_nickname"], event["new_nickname"]) event["old_nickname"], event["new_nickname"])
self._event("nick", event["server"], line, None) self._event("nick", event["server"], line, None, user=user)
@utils.hook("received.nick")
def nick(self, event):
self._on_nick(event, event["user"])
@utils.hook("self.nick")
def self_nick(self, event):
self._on_nick(event, event["server"].get_user(event["server"].nickname))
@utils.hook("received.server-notice", priority=EventManager.PRIORITY_HIGH) @utils.hook("received.server-notice", priority=EventManager.PRIORITY_HIGH)
def server_notice(self, event): def server_notice(self, event):
@ -116,11 +123,12 @@ class Module(ModuleManager.BaseModule):
line = "%s set mode %s%s" % ( line = "%s set mode %s%s" % (
event["user"].nickname, "".join(event["modes"]), args) event["user"].nickname, "".join(event["modes"]), args)
self._event("mode.channel", event["server"], line, self._event("mode.channel", event["server"], line,
event["channel"].name) event["channel"].name, channel=event["channel"], user=event["user"])
def _on_topic(self, event, setter, action, topic): def _on_topic(self, event, setter, action, topic):
line = "topic %s by %s: %s" % (action, setter, topic) line = "topic %s by %s: %s" % (action, setter, topic)
self._event("topic", event["server"], line, event["channel"].name) self._event("topic", event["server"], line, event["channel"].name,
channel=event["channel"], user=event.get("user", None))
@utils.hook("received.topic") @utils.hook("received.topic")
def on_topic(self, event): def on_topic(self, event):
self._on_topic(event, event["user"].nickname, "changed", self._on_topic(event, event["user"].nickname, "changed",
@ -142,7 +150,8 @@ class Module(ModuleManager.BaseModule):
reason = "(%s)" % event["reason"] reason = "(%s)" % event["reason"]
line = "%s kicked %s from %s%s" % ( line = "%s kicked %s from %s%s" % (
event["user"].nickname, nickname, event["channel"].name, reason) event["user"].nickname, nickname, event["channel"].name, reason)
self._event("kick", event["server"], line, event["channel"].name) self._event("kick", event["server"], line, event["channel"].name,
channel=event["channel"], user=event.get("user", None))
@utils.hook("received.kick") @utils.hook("received.kick")
def kick(self, event): def kick(self, event):
self._on_kick(event, event["target_user"].nickname) self._on_kick(event, event["target_user"].nickname)
@ -150,18 +159,19 @@ class Module(ModuleManager.BaseModule):
def self_kick(self, event): def self_kick(self, event):
self._on_kick(event, event["server"].nickname) self._on_kick(event, event["server"].nickname)
def _quit(self, event, nickname, reason): def _quit(self, event, user, reason):
reason = "" reason = ""
if reason: if reason:
reason = " (%s)" % reason reason = " (%s)" % reason
line = "%s quit%s" % (nickname, reason) line = "%s quit%s" % (user.nickname, reason)
self._event("quit", event["server"], line, None) self._event("quit", event["server"], line, None)
@utils.hook("received.quit") @utils.hook("received.quit")
def on_quit(self, event): def on_quit(self, event):
self._quit(event, event["user"].nickname, event["reason"]) self._quit(event, event["user"], event["reason"])
@utils.hook("send.quit") @utils.hook("send.quit")
def send_quit(self, event): def send_quit(self, event):
self._quit(event, event["server"].nickname, event["reason"]) self._quit(event, event["server"].get_user(event["server"].nickname),
event["reason"])
@utils.hook("received.rename") @utils.hook("received.rename")
def rename(self, event): def rename(self, event):