2019-02-22 11:23:36 +00:00
|
|
|
from src import utils
|
|
|
|
|
2019-06-03 12:30:39 +00:00
|
|
|
def _from_self(server, direction, source):
|
2019-03-10 13:14:15 +00:00
|
|
|
if direction == utils.Direction.Send:
|
2019-05-20 15:33:32 +00:00
|
|
|
if server.has_capability_str("echo-message"):
|
2019-02-22 11:23:36 +00:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
else:
|
2019-06-03 12:30:39 +00:00
|
|
|
if source:
|
|
|
|
return server.is_own_nickname(source.nickname)
|
2019-02-22 11:23:36 +00:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
def message(events, event):
|
2019-02-22 11:23:36 +00:00
|
|
|
from_self = _from_self(event["server"], event["direction"],
|
2019-06-03 12:30:39 +00:00
|
|
|
event.get("source", None))
|
2019-02-22 11:23:36 +00:00
|
|
|
if from_self == None:
|
|
|
|
return
|
|
|
|
|
2019-06-03 13:22:19 +00:00
|
|
|
direction = "send" if from_self else "received"
|
|
|
|
|
2019-05-21 22:09:32 +00:00
|
|
|
target_str = event["args"][0]
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
message = None
|
|
|
|
if len(event["args"]) > 1:
|
|
|
|
message = event["args"][1]
|
|
|
|
|
|
|
|
if not from_self and (
|
2019-06-03 12:30:39 +00:00
|
|
|
not event["source"] or
|
2019-06-03 10:03:12 +00:00
|
|
|
not event["server"].name or
|
2019-06-03 12:30:39 +00:00
|
|
|
event["source"].hostmask == event["server"].name or
|
2019-06-03 10:03:12 +00:00
|
|
|
target_str == "*"):
|
2019-06-03 12:30:39 +00:00
|
|
|
if event["source"]:
|
|
|
|
event["server"].name = event["source"].hostmask
|
2019-06-03 10:03:12 +00:00
|
|
|
|
|
|
|
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:
|
2019-06-03 12:30:39 +00:00
|
|
|
user = event["server"].get_user(event["source"].nickname)
|
2019-06-03 10:03:12 +00:00
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
# 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
|
2019-06-08 09:59:14 +00:00
|
|
|
target = target_str.lstrip("".join(event["server"].statusmsg))
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
is_channel = False
|
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
if target[0] in event["server"].channel_types:
|
2019-06-03 10:03:12 +00:00
|
|
|
is_channel = True
|
2019-02-22 11:23:36 +00:00
|
|
|
if not target in event["server"].channels:
|
|
|
|
return
|
2019-06-03 10:03:12 +00:00
|
|
|
target_obj = event["server"].channels.get(target)
|
|
|
|
else:
|
|
|
|
target_obj = event["server"].get_user(target)
|
|
|
|
|
|
|
|
kwargs = {"server": event["server"], "target": target_obj,
|
2019-06-03 12:51:30 +00:00
|
|
|
"target_str": target_str, "user": user, "tags": event["tags"],
|
|
|
|
"is_channel": is_channel}
|
2019-02-22 11:23:36 +00:00
|
|
|
|
|
|
|
action = False
|
2019-06-03 10:03:12 +00:00
|
|
|
|
2019-06-03 10:10:01 +00:00
|
|
|
if message:
|
|
|
|
ctcp_message = utils.irc.parse_ctcp(message)
|
|
|
|
|
|
|
|
if ctcp_message:
|
|
|
|
if not ctcp_message.command == "ACTION" or not event["command"
|
|
|
|
] == "PRIVMSG":
|
|
|
|
if event["command"] == "PRIVMSG":
|
2019-06-03 13:22:19 +00:00
|
|
|
ctcp_action = "request"
|
2019-06-03 10:10:01 +00:00
|
|
|
else:
|
2019-06-03 13:22:19 +00:00
|
|
|
ctcp_action = "response"
|
|
|
|
events.on(direction).on("ctcp").on(ctcp_action).call(
|
2019-06-03 12:51:30 +00:00
|
|
|
message=ctcp_message.message, **kwargs)
|
2019-06-03 13:22:19 +00:00
|
|
|
events.on(direction).on("ctcp").on(ctcp_action).on(
|
|
|
|
ctcp_message.command).call(message=ctcp_message.message,
|
|
|
|
**kwargs)
|
2019-06-03 10:10:01 +00:00
|
|
|
return
|
2019-06-03 10:03:12 +00:00
|
|
|
else:
|
2019-06-03 10:10:01 +00:00
|
|
|
message = ctcp_message.message
|
|
|
|
action = True
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
if not message == None:
|
|
|
|
kwargs["message"] = message
|
|
|
|
kwargs["message_split"] = message.split(" ")
|
|
|
|
kwargs["action"] = action
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
event_type = event["command"].lower()
|
|
|
|
if event["command"] == "PRIVMSG":
|
|
|
|
event_type = "message"
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
context = "channel" if is_channel else "private"
|
2019-02-22 11:23:36 +00:00
|
|
|
hook = events.on(direction).on(event_type).on(context)
|
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
if is_channel:
|
|
|
|
hook.call(channel=target_obj, **kwargs)
|
2019-06-08 17:48:42 +00:00
|
|
|
if message:
|
|
|
|
target_obj.buffer.add_message(user.nickname, message, action,
|
|
|
|
event["tags"], from_self)
|
2019-02-22 11:23:36 +00:00
|
|
|
else:
|
2019-06-03 10:03:12 +00:00
|
|
|
hook.call(**kwargs)
|
2019-06-03 10:35:31 +00:00
|
|
|
|
|
|
|
buffer_obj = target_obj
|
2019-06-03 10:37:23 +00:00
|
|
|
if not from_self:
|
2019-06-03 10:35:31 +00:00
|
|
|
buffer_obj = user
|
|
|
|
|
2019-06-08 17:48:42 +00:00
|
|
|
if message:
|
|
|
|
buffer_obj.buffer.add_message(user.nickname, message, action,
|
|
|
|
event["tags"], from_self)
|