Consolidate PRIVMSG, NOTICE and TAGMSG handlers in to 1 function
This commit is contained in:
parent
0cacd202dd
commit
d610282c9e
3 changed files with 71 additions and 141 deletions
|
@ -8,23 +8,23 @@ from src import IRCBot, ModuleManager, utils
|
||||||
"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, "example": "on"})
|
"validate": utils.bool_or_none, "example": "on"})
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
@utils.hook("received.ctcp.version.private")
|
@utils.hook("received.ctcp.request.version")
|
||||||
def ctcp_version(self, event):
|
def ctcp_version(self, event):
|
||||||
default = "BitBot %s (%s)" % (IRCBot.VERSION, IRCBot.SOURCE)
|
default = "BitBot %s (%s)" % (IRCBot.VERSION, IRCBot.SOURCE)
|
||||||
|
|
||||||
event["user"].send_ctcp_response("VERSION",
|
event["user"].send_ctcp_response("VERSION",
|
||||||
self.bot.config.get("ctcp-version", default))
|
self.bot.config.get("ctcp-version", default))
|
||||||
|
|
||||||
@utils.hook("received.ctcp.source.private")
|
@utils.hook("received.ctcp.request.source")
|
||||||
def ctcp_source(self, event):
|
def ctcp_source(self, event):
|
||||||
event["user"].send_ctcp_response("SOURCE",
|
event["user"].send_ctcp_response("SOURCE",
|
||||||
self.bot.config.get("ctcp-source", IRCBot.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):
|
def ctcp_ping(self, event):
|
||||||
event["user"].send_ctcp_response("PING", event["message"])
|
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):
|
def ctcp_time(self, event):
|
||||||
event["user"].send_ctcp_response("TIME",
|
event["user"].send_ctcp_response("TIME",
|
||||||
datetime.datetime.now().strftime("%c"))
|
datetime.datetime.now().strftime("%c"))
|
||||||
|
|
|
@ -10,8 +10,9 @@ class Module(ModuleManager.BaseModule):
|
||||||
default_events.append(hook.kwargs.get("default_event", False))
|
default_events.append(hook.kwargs.get("default_event", False))
|
||||||
default_event = any(default_events)
|
default_event = any(default_events)
|
||||||
|
|
||||||
kwargs = {"args": line.args, "tags": line.tags, "server": server,
|
kwargs = {"command": line.command, "args": line.args, "tags": line.tags,
|
||||||
"prefix": line.prefix, "direction": utils.Direction.Recv}
|
"server": server, "prefix": line.prefix,
|
||||||
|
"direction": utils.Direction.Recv}
|
||||||
|
|
||||||
self.events.on("raw.received").on(line.command).call_unsafe(**kwargs)
|
self.events.on("raw.received").on(line.command).call_unsafe(**kwargs)
|
||||||
if default_event or not hooks:
|
if default_event or not hooks:
|
||||||
|
@ -29,8 +30,9 @@ class Module(ModuleManager.BaseModule):
|
||||||
@utils.hook("raw.send")
|
@utils.hook("raw.send")
|
||||||
def handle_send(self, event):
|
def handle_send(self, event):
|
||||||
self.events.on("raw.send").on(event["line"].command).call_unsafe(
|
self.events.on("raw.send").on(event["line"].command).call_unsafe(
|
||||||
args=event["line"].args, tags=event["line"].tags,
|
command=event["line"].command, args=event["line"].args,
|
||||||
server=event["server"], direction=utils.Direction.Send)
|
tags=event["line"].tags, server=event["server"],
|
||||||
|
direction=utils.Direction.Send)
|
||||||
|
|
||||||
# ping from the server
|
# ping from the server
|
||||||
@utils.hook("raw.received.ping")
|
@utils.hook("raw.received.ping")
|
||||||
|
@ -153,22 +155,14 @@ class Module(ModuleManager.BaseModule):
|
||||||
def invite(self, event):
|
def invite(self, event):
|
||||||
core.invite(self.events, 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.received.privmsg")
|
||||||
@utils.hook("raw.send.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.received.notice")
|
||||||
@utils.hook("raw.send.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")
|
@utils.hook("raw.received.tagmsg")
|
||||||
def tagmsg(self, event):
|
def message(self, event):
|
||||||
message.tagmsg(self.events, event)
|
message.message(self.events, event)
|
||||||
|
|
||||||
# IRCv3 AWAY, used to notify us that a client we can see has changed /away
|
# IRCv3 AWAY, used to notify us that a client we can see has changed /away
|
||||||
@utils.hook("raw.received.away")
|
@utils.hook("raw.received.away")
|
||||||
|
|
|
@ -12,152 +12,88 @@ def _from_self(server, direction, prefix):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def privmsg(events, event):
|
def message(events, event):
|
||||||
from_self = _from_self(event["server"], event["direction"],
|
from_self = _from_self(event["server"], event["direction"],
|
||||||
event.get("prefix", None))
|
event.get("prefix", None))
|
||||||
if from_self == None:
|
if from_self == None:
|
||||||
return
|
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]
|
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
|
# strip prefix_symbols from the start of target, for when people use
|
||||||
# e.g. 'PRIVMSG +#channel :hi' which would send a message to only
|
# e.g. 'PRIVMSG +#channel :hi' which would send a message to only
|
||||||
# voiced-or-above users
|
# voiced-or-above users
|
||||||
target = target_str.lstrip("".join(event["server"].prefix_symbols.keys()))
|
target = target_str.lstrip("".join(event["server"].prefix_symbols.keys()))
|
||||||
|
|
||||||
channel = None
|
is_channel = False
|
||||||
|
|
||||||
if target[0] in event["server"].channel_types:
|
if target[0] in event["server"].channel_types:
|
||||||
|
is_channel = True
|
||||||
if not target in event["server"].channels:
|
if not target in event["server"].channels:
|
||||||
return
|
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
|
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
|
if not ctcp_message.command == "ACTION" or not event["command"
|
||||||
if ctcp_message.command == "ACTION":
|
] == "PRIVMSG":
|
||||||
action = True
|
if event["command"] == "PRIVMSG":
|
||||||
message = ctcp_message.message
|
direction = "request"
|
||||||
else:
|
else:
|
||||||
event_type = "ctcp.%s" % ctcp_message.command
|
direction = "response"
|
||||||
|
events.on("received.ctcp").on(direction).on(ctcp_message.command
|
||||||
|
).call(message=ctcp_message.message, **kwargs)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
message = ctcp_message.message
|
||||||
|
action = True
|
||||||
|
|
||||||
if user and "account" in event["tags"]:
|
if not message == None:
|
||||||
user.identified_account = event["tags"]["account"]
|
kwargs["message"] = message
|
||||||
user.identified_account_id = event["server"].get_user(
|
kwargs["message_split"] = message.split(" ")
|
||||||
event["tags"]["account"]).get_id()
|
kwargs["action"] = action
|
||||||
|
|
||||||
kwargs = {"message": message, "message_split": message.split(" "),
|
event_type = event["command"].lower()
|
||||||
"server": event["server"], "tags": event["tags"],
|
if event["command"] == "PRIVMSG":
|
||||||
"action": action, "target_str": target_str}
|
event_type = "message"
|
||||||
|
|
||||||
direction = "send" if from_self else "received"
|
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)
|
hook = events.on(direction).on(event_type).on(context)
|
||||||
|
|
||||||
user_nickname = None
|
if is_channel:
|
||||||
if user:
|
hook.call(channel=target_obj, **kwargs)
|
||||||
user_nickname = None if from_self else user.nickname
|
target_obj.buffer.add_message(user.nickname, message, action,
|
||||||
|
event["tags"], from_self)
|
||||||
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"])
|
|
||||||
else:
|
else:
|
||||||
user = None
|
hook.call(**kwargs)
|
||||||
if "prefix" in event and not from_self:
|
target_obj.buffer.add_message(user.nickname, message, action,
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
event["tags"], True)
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
Loading…
Reference in a new issue