From d610282c9e2ca9a55d1d79c36b9dfb732b07f810 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 3 Jun 2019 11:03:12 +0100 Subject: [PATCH] Consolidate PRIVMSG, NOTICE and TAGMSG handlers in to 1 function --- modules/ctcp.py | 8 +- modules/line_handler/__init__.py | 24 ++--- modules/line_handler/message.py | 180 ++++++++++--------------------- 3 files changed, 71 insertions(+), 141 deletions(-) diff --git a/modules/ctcp.py b/modules/ctcp.py index 6c973356..58f8d7e5 100644 --- a/modules/ctcp.py +++ b/modules/ctcp.py @@ -8,23 +8,23 @@ from src import IRCBot, ModuleManager, utils "help": "Set whether I respond to CTCPs on this server", "validate": utils.bool_or_none, "example": "on"}) class Module(ModuleManager.BaseModule): - @utils.hook("received.ctcp.version.private") + @utils.hook("received.ctcp.request.version") def ctcp_version(self, event): default = "BitBot %s (%s)" % (IRCBot.VERSION, IRCBot.SOURCE) event["user"].send_ctcp_response("VERSION", self.bot.config.get("ctcp-version", default)) - @utils.hook("received.ctcp.source.private") + @utils.hook("received.ctcp.request.source") def ctcp_source(self, event): event["user"].send_ctcp_response("SOURCE", self.bot.config.get("ctcp-source", IRCBot.SOURCE)) - @utils.hook("received.ctcp.ping.private") + @utils.hook("received.ctcp.request.ping") def ctcp_ping(self, event): event["user"].send_ctcp_response("PING", event["message"]) - @utils.hook("received.ctcp.time.private") + @utils.hook("received.ctcp.request.time") def ctcp_time(self, event): event["user"].send_ctcp_response("TIME", datetime.datetime.now().strftime("%c")) diff --git a/modules/line_handler/__init__.py b/modules/line_handler/__init__.py index 73057b55..aee4e05a 100644 --- a/modules/line_handler/__init__.py +++ b/modules/line_handler/__init__.py @@ -10,8 +10,9 @@ class Module(ModuleManager.BaseModule): default_events.append(hook.kwargs.get("default_event", False)) default_event = any(default_events) - kwargs = {"args": line.args, "tags": line.tags, "server": server, - "prefix": line.prefix, "direction": utils.Direction.Recv} + kwargs = {"command": line.command, "args": line.args, "tags": line.tags, + "server": server, "prefix": line.prefix, + "direction": utils.Direction.Recv} self.events.on("raw.received").on(line.command).call_unsafe(**kwargs) if default_event or not hooks: @@ -29,8 +30,9 @@ class Module(ModuleManager.BaseModule): @utils.hook("raw.send") def handle_send(self, event): self.events.on("raw.send").on(event["line"].command).call_unsafe( - args=event["line"].args, tags=event["line"].tags, - server=event["server"], direction=utils.Direction.Send) + command=event["line"].command, args=event["line"].args, + tags=event["line"].tags, server=event["server"], + direction=utils.Direction.Send) # ping from the server @utils.hook("raw.received.ping") @@ -153,22 +155,14 @@ class Module(ModuleManager.BaseModule): def invite(self, event): core.invite(self.events, event) - # we've received/sent a message + # we've received/sent a PRIVMSG, NOTICE or TAGMSG @utils.hook("raw.received.privmsg") @utils.hook("raw.send.privmsg") - def privmsg(self, event): - message.privmsg(self.events, event) - - # we've received/sent a notice @utils.hook("raw.received.notice") @utils.hook("raw.send.notice") - def notice(self, event): - message.notice(self.events, event) - - # IRCv3 TAGMSG, used to send tags without any other information @utils.hook("raw.received.tagmsg") - def tagmsg(self, event): - message.tagmsg(self.events, event) + def message(self, event): + message.message(self.events, event) # IRCv3 AWAY, used to notify us that a client we can see has changed /away @utils.hook("raw.received.away") diff --git a/modules/line_handler/message.py b/modules/line_handler/message.py index 4fb507ca..369a72dc 100644 --- a/modules/line_handler/message.py +++ b/modules/line_handler/message.py @@ -12,152 +12,88 @@ def _from_self(server, direction, prefix): else: return False -def privmsg(events, event): +def message(events, event): from_self = _from_self(event["server"], event["direction"], event.get("prefix", None)) if from_self == None: return - user = None - if "prefix" in event and not from_self: - user = event["server"].get_user(event["prefix"].nickname) - - message = event["args"][1] target_str = event["args"][0] + message = None + if len(event["args"]) > 1: + message = event["args"][1] + + if not from_self and ( + not event["prefix"] or + not event["server"].name or + event["prefix"].hostmask == event["server"].name or + target_str == "*"): + if event["prefix"]: + event["server"].name = event["prefix"].hostmask + + events.on("received.server-notice").call(message=message, + message_split=message.split(" "), server=event["server"]) + return + + if from_self: + user = event["server"].get_user(event["server"].nickname) + else: + user = event["server"].get_user(event["prefix"].nickname) + # strip prefix_symbols from the start of target, for when people use # e.g. 'PRIVMSG +#channel :hi' which would send a message to only # voiced-or-above users target = target_str.lstrip("".join(event["server"].prefix_symbols.keys())) - channel = None + is_channel = False + if target[0] in event["server"].channel_types: + is_channel = True if not target in event["server"].channels: return - channel = event["server"].channels.get(target) + target_obj = event["server"].channels.get(target) + else: + target_obj = event["server"].get_user(target) + + kwargs = {"server": event["server"], "target": target_obj, + "target_str": target_str, "user": user, "tags": event["tags"]} action = False - event_type = "message" ctcp_message = utils.irc.parse_ctcp(message) + if ctcp_message: - message = ctcp_message.message - if ctcp_message.command == "ACTION": - action = True - message = ctcp_message.message + if not ctcp_message.command == "ACTION" or not event["command" + ] == "PRIVMSG": + if event["command"] == "PRIVMSG": + direction = "request" + else: + direction = "response" + events.on("received.ctcp").on(direction).on(ctcp_message.command + ).call(message=ctcp_message.message, **kwargs) + return else: - event_type = "ctcp.%s" % ctcp_message.command + message = ctcp_message.message + action = True - if user and "account" in event["tags"]: - user.identified_account = event["tags"]["account"] - user.identified_account_id = event["server"].get_user( - event["tags"]["account"]).get_id() + if not message == None: + kwargs["message"] = message + kwargs["message_split"] = message.split(" ") + kwargs["action"] = action - kwargs = {"message": message, "message_split": message.split(" "), - "server": event["server"], "tags": event["tags"], - "action": action, "target_str": target_str} + event_type = event["command"].lower() + if event["command"] == "PRIVMSG": + event_type = "message" direction = "send" if from_self else "received" - context = "channel" if channel else "private" + context = "channel" if is_channel else "private" hook = events.on(direction).on(event_type).on(context) - user_nickname = None - if user: - user_nickname = None if from_self else user.nickname - - if channel: - hook.call(user=user, channel=channel, **kwargs) - channel.buffer.add_message(user_nickname, message, action, - event["tags"], user==None) - elif from_self: - # a message we've sent to a user - user = event["server"].get_user(target) - hook.call(user=user, **kwargs) - user.buffer.add_message(user_nickname, message, action, - event["tags"], True) - elif event["server"].is_own_nickname(target): - hook.call(user=user, **kwargs) - user.buffer.add_message(user_nickname, message, action, - event["tags"], False) - -def notice(events, event): - from_self = _from_self(event["server"], event["direction"], - event.get("prefix", None)) - if from_self == None: - return - - message = event["args"][1] - target = event["args"][0] - - if "prefix" in event and ( - not event["prefix"] or - not event["server"].name or - event["prefix"].hostmask == event["server"].name or - target == "*"): - if event["prefix"]: - event["server"].name = event["prefix"].hostmask - - events.on("received.server-notice").call(message=message, - message_split=message.split(" "), server=event["server"]) + if is_channel: + hook.call(channel=target_obj, **kwargs) + target_obj.buffer.add_message(user.nickname, message, action, + event["tags"], from_self) else: - user = None - if "prefix" in event and not from_self: - user = event["server"].get_user(event["prefix"].nickname) - - channel = None - if target[0] in event["server"].channel_types: - channel = event["server"].channels.get(target) - - direction = "send" if from_self else "received" - context = "channel" if channel else "private" - hook = events.on(direction).on("notice").on(context) - - user_nickname = None - if user: - user_nickname = None if from_self else user.nickname - - kwargs = {"message": message, "message_split": message.split(" "), - "server": event["server"], "tags": event["tags"]} - - if channel: - hook.call(user=user, channel=channel, **kwargs) - channel.buffer.add_notice(user_nickname, message, event["tags"], - user==None) - elif from_self: - # a notice we've sent to a user - user = event["server"].get_user(target) - hook.call(user=user, **kwargs) - user.buffer.add_notice(user_nickname, message, event["tags"], - True) - elif event["server"].is_own_nickname(target): - hook.call(user=user, **kwargs) - user.buffer.add_notice(user_nickname, message, event["tags"], - False) - -def tagmsg(events, event): - from_self = _from_self(event["server"], event["direction"], - event.get("prefix", None)) - if from_self == None: - return - - user = None - if "prefix" in event and not from_self: - user = event["server"].get_user(event["prefix"].nickname) - - target = event["args"][0] - channel = None - if target[0] in event["server"].channel_types: - channel = event["server"].channels.get(target) - - direction = "send" if from_self else "received" - context = "channel" if channel else "private" - hook = events.on(direction).on("tagmsg").on(context) - - kwargs = {"server": event["server"], "tags": event["tags"]} - - if channel: - hook.call(user=user, channel=channel, **kwargs) - elif event["server"].is_own_nickname(target): - hook.call(user=user, **kwargs) - elif from_self: - user = event["server"].get_user(target) - hook.call(user=user, **kwargs) + hook.call(**kwargs) + target_obj.buffer.add_message(user.nickname, message, action, + event["tags"], True)