2019-11-30 20:25:52 +00:00
|
|
|
from src import IRCBuffer, utils
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-21 09:47:21 +00:00
|
|
|
def _from_self(server, source):
|
|
|
|
if source:
|
|
|
|
return server.is_own_nickname(source.nickname)
|
2019-02-22 11:23:36 +00:00
|
|
|
else:
|
2019-06-21 09:47:21 +00:00
|
|
|
return False
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
def message(events, event):
|
2019-06-22 11:17:45 +00:00
|
|
|
from_self = _from_self(event["server"], event["line"].source)
|
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-06-22 11:17:45 +00:00
|
|
|
target_str = event["line"].args[0]
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-06-03 10:03:12 +00:00
|
|
|
message = None
|
2019-06-22 11:17:45 +00:00
|
|
|
if len(event["line"].args) > 1:
|
|
|
|
message = event["line"].args[1]
|
2019-06-03 10:03:12 +00:00
|
|
|
|
|
|
|
if not from_self and (
|
2019-06-22 11:17:45 +00:00
|
|
|
not event["line"].source or
|
2019-06-03 10:03:12 +00:00
|
|
|
not event["server"].name or
|
2019-06-22 11:17:45 +00:00
|
|
|
event["line"].source.hostmask == event["server"].name or
|
2019-06-03 10:03:12 +00:00
|
|
|
target_str == "*"):
|
2019-06-22 11:17:45 +00:00
|
|
|
if event["line"].source:
|
|
|
|
event["server"].name = event["line"].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-09-27 11:54:20 +00:00
|
|
|
user = event["server"].get_user(event["line"].source.nickname,
|
|
|
|
username=event["line"].source.username,
|
|
|
|
hostname=event["line"].source.hostname)
|
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-09-20 13:52:41 +00:00
|
|
|
is_channel = event["server"].is_channel(target)
|
2019-06-03 10:03:12 +00:00
|
|
|
|
2019-09-20 13:52:41 +00:00
|
|
|
if is_channel:
|
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-22 11:17:45 +00:00
|
|
|
"target_str": target_str, "user": user, "tags": event["line"].tags,
|
2019-06-21 19:20:28 +00:00
|
|
|
"is_channel": is_channel, "from_self": from_self, "line": event["line"]}
|
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:
|
2019-06-22 11:17:45 +00:00
|
|
|
if (not ctcp_message.command == "ACTION" or not
|
|
|
|
event["line"].command == "PRIVMSG"):
|
|
|
|
if event["line"].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-22 11:17:45 +00:00
|
|
|
event_type = event["line"].command.lower()
|
|
|
|
if event_type == "privmsg":
|
2019-06-03 10:03:12 +00:00
|
|
|
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-11-30 20:25:52 +00:00
|
|
|
buffer_line = None
|
|
|
|
if message:
|
|
|
|
buffer_line = IRCBuffer.BufferLine(user.nickname, message, action,
|
|
|
|
event["line"].tags, from_self, event["line"].command)
|
|
|
|
|
|
|
|
buffer_obj = target_obj
|
2019-06-03 10:03:12 +00:00
|
|
|
if is_channel:
|
2019-11-27 12:06:57 +00:00
|
|
|
hook.call(channel=target_obj, buffer_line=buffer_line, **kwargs)
|
2019-02-22 11:23:36 +00:00
|
|
|
else:
|
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-11-27 12:06:57 +00:00
|
|
|
hook.call(buffer_line=buffer_line, **kwargs)
|
2019-11-30 20:25:52 +00:00
|
|
|
|
|
|
|
if buffer_line:
|
|
|
|
buffer_obj.buffer.add(buffer_line)
|