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

View file

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