From 938495fc3a3a7ac8c48363b997935358dcdb61c1 Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 12 Feb 2019 16:49:57 +0000 Subject: [PATCH] Refactor how CTCPs are sent through events --- modules/ctcp.py | 42 +++++++++++++++++++---------------------- modules/line_handler.py | 15 +++++---------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/modules/ctcp.py b/modules/ctcp.py index f165b03d..5ccf85c7 100644 --- a/modules/ctcp.py +++ b/modules/ctcp.py @@ -1,32 +1,28 @@ import datetime from src import ModuleManager, utils +VERSION_DEFAULT = "BitBot (https://git.io/bitbot)" +SOURCE_DEFAULT = "https://git.io/bitbot" + @utils.export("serverset", {"setting": "ctcp-responses", "help": "Set whether I respond to CTCPs on this server", "validate": utils.bool_or_none}) class Module(ModuleManager.BaseModule): - @utils.hook("received.message.private") - def private_message(self, event): - if event["message"][0] == "\x01" and event["message"][-1] == "\x01": - if event["server"].get_setting("ctcp-responses", True): - ctcp_command = event["message_split"][0][1:].upper() - if ctcp_command.endswith("\x01"): - ctcp_command = ctcp_command[:-1] - ctcp_args = " ".join(event["message_split"][1:])[:-1] - ctcp_args_split = ctcp_args.split(" ") + @utils.hook("received.ctcp.version.private") + def ctcp_version(self, event): + event["user"].send_ctcp_response("VERSION", + self.bot.config.get("ctcp-version", VERSION_DEFAULT)) - ctcp_response = None - if ctcp_command == "VERSION": - ctcp_response = self.bot.config.get("ctcp-version", - "BitBot (https://github.com/jesopo/bitbot)") - elif ctcp_command == "SOURCE": - ctcp_response = self.bot.config.get("ctcp-source", - "https://github.com/jesopo/bitbot") - elif ctcp_command == "PING": - ctcp_response = " ".join(ctcp_args_split) - elif ctcp_command == "TIME": - ctcp_response = datetime.datetime.now().strftime("%c") + @utils.hook("received.ctcp.source.private") + def ctcp_source(self, event): + event["user"].send_ctcp_response("SOURCE", + self.bot.config.get("ctcp-source", SOURCE_DEFAULT)) - if ctcp_response: - event["user"].send_ctcp_response(ctcp_command, - ctcp_response) + @utils.hook("received.ctcp.ping.private") + def ctcp_ping(self, event): + event["user"].send_ctcp_response("PING", event["message"]) + + @utils.hook("received.ctcp.time.private") + def ctcp_time(self, event): + event["user"].send_ctcp_response("TIME", + datetme.datetime.now().strftime("%c")) diff --git a/modules/line_handler.py b/modules/line_handler.py index f7d0ae50..4706e367 100644 --- a/modules/line_handler.py +++ b/modules/line_handler.py @@ -444,7 +444,6 @@ class Module(ModuleManager.BaseModule): user = event["server"].get_user(event["prefix"].nickname) message = event["args"][1] - message_split = message.split(" ") target = event["args"][0] # strip prefix_symbols from the start of target, for when people use @@ -460,31 +459,27 @@ class Module(ModuleManager.BaseModule): channel = event["server"].channels.get(target) action = False + event_type = "message" ctcp_message = utils.irc.parse_ctcp(message) if ctcp_message: + message = ctcp_message.message + event_type = "ctcp.%s" % ctcp_message.command if ctcp_message.command == "ACTION": action = True message = ctcp_message.message - else: - if channel: - self._event(event, "ctcp.channel", channel=channel, - ctcp=ctcp_message) - else: - self._event(event, "ctcp.private", user=user, - ctcp=ctcp_message) 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() - kwargs = {"message": message, "message_split": message_split, + kwargs = {"message": message, "message_split": message.split(), "server": event["server"], "tags": event["tags"], "action": action} direction = "send" if from_self else "received" context = "channel" if channel else "private" - hook = self.events.on(direction).on("message").on(context) + hook = self.events.on(direction).on(event_type).on(context) if channel: hook.call(user=user, channel=channel, statusmsg=statusmsg, **kwargs)