Refactor how CTCPs are sent through events

This commit is contained in:
jesopo 2019-02-12 16:49:57 +00:00
parent b4092a14ca
commit 938495fc3a
2 changed files with 24 additions and 33 deletions

View file

@ -1,32 +1,28 @@
import datetime import datetime
from src import ModuleManager, utils from src import ModuleManager, utils
VERSION_DEFAULT = "BitBot (https://git.io/bitbot)"
SOURCE_DEFAULT = "https://git.io/bitbot"
@utils.export("serverset", {"setting": "ctcp-responses", @utils.export("serverset", {"setting": "ctcp-responses",
"help": "Set whether I respond to CTCPs on this server", "help": "Set whether I respond to CTCPs on this server",
"validate": utils.bool_or_none}) "validate": utils.bool_or_none})
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@utils.hook("received.message.private") @utils.hook("received.ctcp.version.private")
def private_message(self, event): def ctcp_version(self, event):
if event["message"][0] == "\x01" and event["message"][-1] == "\x01": event["user"].send_ctcp_response("VERSION",
if event["server"].get_setting("ctcp-responses", True): self.bot.config.get("ctcp-version", VERSION_DEFAULT))
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(" ")
ctcp_response = None @utils.hook("received.ctcp.source.private")
if ctcp_command == "VERSION": def ctcp_source(self, event):
ctcp_response = self.bot.config.get("ctcp-version", event["user"].send_ctcp_response("SOURCE",
"BitBot (https://github.com/jesopo/bitbot)") self.bot.config.get("ctcp-source", SOURCE_DEFAULT))
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")
if ctcp_response: @utils.hook("received.ctcp.ping.private")
event["user"].send_ctcp_response(ctcp_command, def ctcp_ping(self, event):
ctcp_response) 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"))

View file

@ -444,7 +444,6 @@ class Module(ModuleManager.BaseModule):
user = event["server"].get_user(event["prefix"].nickname) user = event["server"].get_user(event["prefix"].nickname)
message = event["args"][1] message = event["args"][1]
message_split = message.split(" ")
target = event["args"][0] target = event["args"][0]
# strip prefix_symbols from the start of target, for when people use # 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) channel = event["server"].channels.get(target)
action = False action = False
event_type = "message"
ctcp_message = utils.irc.parse_ctcp(message) ctcp_message = utils.irc.parse_ctcp(message)
if ctcp_message: if ctcp_message:
message = ctcp_message.message
event_type = "ctcp.%s" % ctcp_message.command
if ctcp_message.command == "ACTION": if ctcp_message.command == "ACTION":
action = True action = True
message = ctcp_message.message 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"]: if user and "account" in event["tags"]:
user.identified_account = event["tags"]["account"] user.identified_account = event["tags"]["account"]
user.identified_account_id = event["server"].get_user( user.identified_account_id = event["server"].get_user(
event["tags"]["account"]).get_id() 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"], "server": event["server"], "tags": event["tags"],
"action": action} "action": action}
direction = "send" if from_self else "received" direction = "send" if from_self else "received"
context = "channel" if channel else "private" 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: if channel:
hook.call(user=user, channel=channel, statusmsg=statusmsg, **kwargs) hook.call(user=user, channel=channel, statusmsg=statusmsg, **kwargs)