2018-11-14 14:41:28 +00:00
|
|
|
import codecs, enum, re, threading
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-11-08 11:11:38 +00:00
|
|
|
RE_ISUPPORT_ESCAPE = re.compile(r"\\x(\d\d)", re.I)
|
2018-08-30 10:12:48 +00:00
|
|
|
RE_MODES = re.compile(r"[-+]\w+")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-04 06:35:26 +00:00
|
|
|
CAPABILITIES = {"multi-prefix", "chghost", "invite-notify", "account-tag",
|
|
|
|
"account-notify", "extended-join", "away-notify", "userhost-in-names",
|
2018-11-05 15:15:08 +00:00
|
|
|
"draft/message-tags-0.2", "draft/message-tags-0.3", "server-time",
|
|
|
|
"cap-notify", "batch", "draft/labeled-response", "draft/rename"}
|
2018-09-03 10:47:11 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
class Direction(enum.Enum):
|
|
|
|
SEND = 0
|
|
|
|
RECV = 1
|
|
|
|
|
2018-10-01 12:47:45 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-30 17:52:23 +00:00
|
|
|
def _handle(self, server, line):
|
2018-11-14 14:41:28 +00:00
|
|
|
hooks = self.events.on("raw.received").on(line.command).get_hooks()
|
2018-10-02 16:47:45 +00:00
|
|
|
default_events = []
|
2018-08-29 11:20:25 +00:00
|
|
|
for hook in hooks:
|
2018-10-02 16:47:45 +00:00
|
|
|
default_events.append(hook.kwargs.get("default_event", False))
|
|
|
|
default_event = any(default_events)
|
|
|
|
|
2018-11-04 15:35:52 +00:00
|
|
|
kwargs = {"args": line.args, "tags": line.tags, "server": server,
|
2018-11-14 14:41:28 +00:00
|
|
|
"prefix": line.prefix, "has_arbitrary": line.has_arbitrary,
|
|
|
|
"direction": Direction.RECV}
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
self.events.on("raw.received").on(line.command).call_unsafe(**kwargs)
|
2018-08-29 11:43:54 +00:00
|
|
|
if default_event or not hooks:
|
2018-10-03 12:22:37 +00:00
|
|
|
if line.command.isdigit():
|
2018-10-03 10:31:51 +00:00
|
|
|
self.events.on("received.numeric").on(line.command).call(
|
|
|
|
**kwargs)
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-10-03 10:31:51 +00:00
|
|
|
self.events.on("received").on(line.command).call(**kwargs)
|
2018-10-30 17:52:23 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received")
|
2018-10-03 10:31:51 +00:00
|
|
|
def handle_raw(self, event):
|
2018-10-30 17:52:23 +00:00
|
|
|
line = utils.irc.parse_line(event["line"])
|
2018-10-03 10:31:51 +00:00
|
|
|
if "batch" in line.tags and line.tags["batch"] in event[
|
|
|
|
"server"].batches:
|
|
|
|
server.batches[tag["batch"]].append(line)
|
|
|
|
else:
|
2018-10-30 17:52:23 +00:00
|
|
|
self._handle(event["server"], line)
|
2018-10-03 10:31:51 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("preprocess.send")
|
2018-10-03 10:31:51 +00:00
|
|
|
def handle_send(self, event):
|
2018-10-30 17:52:23 +00:00
|
|
|
line = utils.irc.parse_line(event["line"])
|
2018-11-14 14:41:28 +00:00
|
|
|
self.events.on("raw.send").on(line.command).call_unsafe(
|
|
|
|
args=line.args, tags=line.tags, server=event["server"],
|
|
|
|
direction=Direction.SEND)
|
|
|
|
|
|
|
|
def _event(self, event, event_name: str, **kwargs: dict):
|
|
|
|
direction = event["direction"]
|
|
|
|
if direction == Direction.RECV:
|
|
|
|
root_event = self.events.on("received")
|
|
|
|
elif direction == Direction.SEND:
|
|
|
|
root_event = self.events.on("send")
|
|
|
|
root_event.on(event_name).call(**kwargs)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-08-29 11:54:47 +00:00
|
|
|
# ping from the server
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.ping")
|
2018-08-29 11:20:25 +00:00
|
|
|
def ping(self, event):
|
2018-11-04 15:35:52 +00:00
|
|
|
event["server"].send_pong(event["args"].get(0))
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-08-29 11:54:47 +00:00
|
|
|
# first numeric line the server sends
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.001", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_001(self, event):
|
2018-11-19 10:12:52 +00:00
|
|
|
event["server"].set_write_throttling(True)
|
2018-09-10 08:31:24 +00:00
|
|
|
event["server"].name = event["prefix"].nickname
|
2018-08-29 11:20:25 +00:00
|
|
|
event["server"].set_own_nickname(event["args"][0])
|
|
|
|
event["server"].send_whois(event["server"].nickname)
|
|
|
|
|
2018-08-29 11:54:47 +00:00
|
|
|
# server telling us what it supports
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.005")
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_005(self, event):
|
2018-11-08 11:11:38 +00:00
|
|
|
isupport_list = event["args"][1:]
|
|
|
|
if event["has_arbitrary"]:
|
|
|
|
isupport_list = isupport_list[:-1]
|
|
|
|
|
|
|
|
isupport = {}
|
|
|
|
for i, item in enumerate(isupport_list):
|
|
|
|
key, sep, value = item.partition("=")
|
|
|
|
if value:
|
|
|
|
for match in RE_ISUPPORT_ESCAPE.finditer(value):
|
|
|
|
char = codecs.decode(match.group(1), "hex").decode("ascii")
|
|
|
|
value.replace(match.group(0), char)
|
|
|
|
|
|
|
|
if sep:
|
|
|
|
isupport[key] = value
|
|
|
|
else:
|
|
|
|
isupport[key] = None
|
2018-11-08 12:34:30 +00:00
|
|
|
event["server"].isupport.update(isupport)
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2018-11-08 14:30:50 +00:00
|
|
|
if "NAMESX" in isupport and not "multi-prefix" in event[
|
|
|
|
"server"].capabilities:
|
2018-08-29 11:20:25 +00:00
|
|
|
event["server"].send("PROTOCTL NAMESX")
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2018-11-08 11:11:38 +00:00
|
|
|
if "PREFIX" in isupport:
|
|
|
|
modes, symbols = isupport["PREFIX"][1:].split(")", 1)
|
2018-10-03 15:44:00 +00:00
|
|
|
event["server"].prefix_symbols.clear()
|
|
|
|
event["server"].prefix_modes.clear()
|
|
|
|
for symbol, mode in zip(symbols, modes):
|
|
|
|
event["server"].prefix_symbols[symbol] = mode
|
|
|
|
event["server"].prefix_modes[mode] = symbol
|
2018-09-11 07:52:12 +00:00
|
|
|
|
2018-11-08 11:11:38 +00:00
|
|
|
if "CHANMODES" in isupport:
|
2018-12-06 08:26:07 +00:00
|
|
|
modes = isupport["CHANMODES"].split(",", 3)
|
|
|
|
event["server"].channel_list_modes = list(modes[0])
|
|
|
|
event["server"].channel_paramatered_modes = list(modes[1])
|
|
|
|
event["server"].channel_modes = list(modes[3])
|
2018-11-08 11:11:38 +00:00
|
|
|
if "CHANTYPES" in isupport:
|
|
|
|
event["server"].channel_types = list(isupport["CHANTYPES"])
|
|
|
|
if "CASEMAPPING" in isupport:
|
|
|
|
event["server"].case_mapping = isupport["CASEMAPPING"]
|
2018-09-11 07:52:12 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "numeric.005", isupport=isupport,
|
|
|
|
server=event["server"])
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# whois respose (nickname, username, realname, hostname)
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.311", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_311(self, event):
|
|
|
|
nickname = event["args"][1]
|
|
|
|
if event["server"].is_own_nickname(nickname):
|
|
|
|
target = event["server"]
|
|
|
|
else:
|
|
|
|
target = event["server"].get_user(nickname)
|
|
|
|
target.username = event["args"][2]
|
|
|
|
target.hostname = event["args"][3]
|
2018-11-04 15:35:52 +00:00
|
|
|
target.realname = event["args"][4]
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# on-join channel topic line
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.332")
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_332(self, event):
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][1])
|
2018-11-04 15:35:52 +00:00
|
|
|
topic = event["args"].get(2)
|
|
|
|
channel.set_topic(topic)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "numeric.332", channel=channel,
|
2018-11-04 15:35:52 +00:00
|
|
|
server=event["server"], topic=topic)
|
2018-08-29 13:57:40 +00:00
|
|
|
|
|
|
|
# channel topic changed
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.topic")
|
2018-08-29 13:57:40 +00:00
|
|
|
def topic(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][0])
|
2018-11-04 15:35:52 +00:00
|
|
|
topic = event["args"].get(1)
|
|
|
|
channel.set_topic(topic)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "topic", channel=channel, server=event["server"],
|
|
|
|
topic=topic, user=user)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# on-join channel topic set by/at
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.333")
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_333(self, event):
|
2018-11-11 12:50:12 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][1])
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
topic_setter_hostmask = event["args"][2]
|
2018-10-03 12:22:37 +00:00
|
|
|
topic_setter = utils.irc.seperate_hostmask(topic_setter_hostmask)
|
2018-08-29 11:20:25 +00:00
|
|
|
topic_time = int(event["args"][3]) if event["args"][3].isdigit(
|
|
|
|
) else None
|
|
|
|
|
2018-09-10 08:31:24 +00:00
|
|
|
channel.set_topic_setter(topic_setter.nickname, topic_setter.username,
|
|
|
|
topic_setter.hostname)
|
2018-08-29 11:20:25 +00:00
|
|
|
channel.set_topic_time(topic_time)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "numeric.333", channel=channel,
|
2018-09-10 08:31:24 +00:00
|
|
|
setter=topic_setter.nickname, set_at=topic_time,
|
|
|
|
server=event["server"])
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-09-04 06:11:02 +00:00
|
|
|
# /names response, also on-join user list
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.353", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_353(self, event):
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][2])
|
2018-11-04 15:35:52 +00:00
|
|
|
nicknames = event["args"].get(3).split()
|
2018-08-29 11:20:25 +00:00
|
|
|
for nickname in nicknames:
|
2018-09-04 06:11:02 +00:00
|
|
|
modes = set([])
|
|
|
|
|
2018-10-03 15:44:00 +00:00
|
|
|
while nickname[0] in event["server"].prefix_symbols:
|
|
|
|
modes.add(event["server"].prefix_symbols[nickname[0]])
|
2018-09-04 06:11:02 +00:00
|
|
|
nickname = nickname[1:]
|
|
|
|
|
2018-09-04 07:04:49 +00:00
|
|
|
if "userhost-in-names" in event["server"].capabilities:
|
2018-10-03 12:22:37 +00:00
|
|
|
hostmask = utils.irc.seperate_hostmask(nickname)
|
2018-09-14 09:43:52 +00:00
|
|
|
nickname = hostmask.nickname
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(hostmask.nickname)
|
|
|
|
user.username = hostmask.username
|
|
|
|
user.hostname = hostmask.hostname
|
2018-09-04 07:12:45 +00:00
|
|
|
else:
|
|
|
|
user = event["server"].get_user(nickname)
|
2018-09-04 06:11:02 +00:00
|
|
|
user.join_channel(channel)
|
|
|
|
channel.add_user(user)
|
|
|
|
|
|
|
|
for mode in modes:
|
|
|
|
channel.add_mode(mode, nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# on-join user list has finished
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.366", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_366(self, event):
|
2018-09-13 09:25:33 +00:00
|
|
|
event["server"].send_whox(event["args"][1], "n", "ahnrtu", "111")
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.375")
|
2018-11-13 14:59:47 +00:00
|
|
|
def motd_start(self, event):
|
|
|
|
event["server"].motd_lines.clear()
|
2018-11-14 08:22:21 +00:00
|
|
|
event["server"].motd_lines.append(event["args"][1])
|
2018-11-13 14:59:47 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.372")
|
2018-11-13 14:59:47 +00:00
|
|
|
def motd_line(self, event):
|
|
|
|
event["server"].motd_lines.append(event["args"][1])
|
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
# on user joining channel
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.join")
|
2018-08-29 11:20:25 +00:00
|
|
|
def join(self, event):
|
2018-09-03 11:37:07 +00:00
|
|
|
account = None
|
|
|
|
realname = None
|
2018-11-11 13:03:54 +00:00
|
|
|
channel_name = event["args"][0]
|
2018-11-11 12:34:22 +00:00
|
|
|
|
2018-09-03 11:37:07 +00:00
|
|
|
if len(event["args"]) == 2:
|
2018-10-15 13:29:16 +00:00
|
|
|
if not event["args"][1] == "*":
|
2018-09-03 11:37:07 +00:00
|
|
|
account = event["args"][1]
|
2018-11-04 15:35:52 +00:00
|
|
|
realname = event["args"][2]
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-09-10 08:31:24 +00:00
|
|
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
2018-11-11 13:03:54 +00:00
|
|
|
channel = event["server"].channels.get(channel_name)
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
|
|
|
if not user.username and not user.hostname:
|
|
|
|
user.username = event["prefix"].username
|
|
|
|
user.hostname = event["prefix"].hostname
|
2018-09-04 08:18:14 +00:00
|
|
|
|
|
|
|
if account:
|
|
|
|
user.identified_account = account
|
2018-09-05 11:04:34 +00:00
|
|
|
user.identified_account_id = event["server"].get_user(
|
2018-09-05 12:34:21 +00:00
|
|
|
account).get_id()
|
2018-09-04 08:18:14 +00:00
|
|
|
if realname:
|
|
|
|
user.realname = realname
|
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
channel.add_user(user)
|
|
|
|
user.join_channel(channel)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "join", channel=channel, user=user,
|
|
|
|
server=event["server"], account=account, realname=realname)
|
2018-08-29 11:20:25 +00:00
|
|
|
else:
|
2018-11-11 13:03:54 +00:00
|
|
|
channel = event["server"].channels.add(channel_name)
|
2018-08-29 11:20:25 +00:00
|
|
|
if channel.name in event["server"].attempted_join:
|
|
|
|
del event["server"].attempted_join[channel.name]
|
2018-11-27 15:43:32 +00:00
|
|
|
self.events.on("self.join").call(channel=channel,
|
2018-09-03 11:37:07 +00:00
|
|
|
server=event["server"], account=account, realname=realname)
|
2018-08-29 11:20:25 +00:00
|
|
|
channel.send_mode()
|
|
|
|
|
|
|
|
# on user parting channel
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.part")
|
2018-08-29 11:20:25 +00:00
|
|
|
def part(self, event):
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][0])
|
2018-11-04 15:35:52 +00:00
|
|
|
reason = event["args"].get(1)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-09-10 08:31:24 +00:00
|
|
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "part", channel=channel, reason=reason,
|
|
|
|
user=user, server=event["server"])
|
2018-08-29 11:20:25 +00:00
|
|
|
channel.remove_user(user)
|
|
|
|
user.part_channel(channel)
|
|
|
|
if not len(user.channels):
|
|
|
|
event["server"].remove_user(user)
|
|
|
|
else:
|
2018-11-27 15:43:32 +00:00
|
|
|
self.events.on("self.part").call(channel=channel, reason=reason,
|
2018-11-14 14:41:28 +00:00
|
|
|
server=event["server"])
|
2018-11-11 12:34:22 +00:00
|
|
|
event["server"].channels.remove(channel)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# unknown command sent by us, oops!
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.421", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_421(self, event):
|
2018-12-02 09:46:57 +00:00
|
|
|
self.bot.log.warn("We sent an unknown command: %s", [event["args"][1]])
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# a user has disconnected!
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.quit")
|
2018-08-29 11:20:25 +00:00
|
|
|
def quit(self, event):
|
2018-11-05 10:03:31 +00:00
|
|
|
reason = event["args"].get(0)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-09-10 08:31:24 +00:00
|
|
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
event["server"].remove_user(user)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "quit", reason=reason, user=user,
|
|
|
|
server=event["server"])
|
2018-08-29 11:20:25 +00:00
|
|
|
else:
|
|
|
|
event["server"].disconnect()
|
|
|
|
|
|
|
|
# the server is telling us about its capabilities!
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.cap")
|
2018-08-29 11:20:25 +00:00
|
|
|
def cap(self, event):
|
2018-11-05 11:56:28 +00:00
|
|
|
capabilities = utils.parse.keyvalue(event["args"][2])
|
2018-09-07 14:51:41 +00:00
|
|
|
subcommand = event["args"][1].lower()
|
|
|
|
is_multiline = len(event["args"]) > 2 and event["args"][2] == "*"
|
2018-09-03 10:14:27 +00:00
|
|
|
|
|
|
|
if subcommand == "ls":
|
2018-11-09 10:43:05 +00:00
|
|
|
event["server"].cap_started = True
|
2018-09-07 14:51:41 +00:00
|
|
|
event["server"].server_capabilities.update(capabilities)
|
|
|
|
if not is_multiline:
|
|
|
|
matched_capabilities = set(event["server"
|
|
|
|
].server_capabilities.keys()) & CAPABILITIES
|
|
|
|
if matched_capabilities:
|
|
|
|
event["server"].queue_capabilities(matched_capabilities)
|
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "cap.ls",
|
2018-09-07 14:51:41 +00:00
|
|
|
capabilities=event["server"].server_capabilities,
|
|
|
|
server=event["server"])
|
|
|
|
|
|
|
|
if event["server"].has_capability_queue():
|
|
|
|
event["server"].send_capability_queue()
|
|
|
|
else:
|
|
|
|
event["server"].send_capability_end()
|
2018-09-07 15:13:39 +00:00
|
|
|
elif subcommand == "new":
|
|
|
|
event["server"].capabilities.update(set(capabilities.keys()))
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "cap.new", server=event["server"],
|
2018-09-07 15:13:39 +00:00
|
|
|
capabilities=capabilities)
|
|
|
|
elif subcommand == "del":
|
|
|
|
event["server"].capabilities.difference_update(set(
|
|
|
|
capabilities.keys()))
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "cap.del", server=event["server"],
|
2018-09-07 15:13:39 +00:00
|
|
|
capabilities=capabilities)
|
2018-09-03 21:17:01 +00:00
|
|
|
elif subcommand == "ack":
|
2018-09-07 14:51:41 +00:00
|
|
|
event["server"].capabilities.update(capabilities)
|
|
|
|
if not is_multiline:
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "cap.ack",
|
2018-09-07 14:51:41 +00:00
|
|
|
capabilities=event["server"].capabilities,
|
|
|
|
server=event["server"])
|
|
|
|
|
2018-11-09 10:43:05 +00:00
|
|
|
if event["server"].cap_started and not event["server"
|
|
|
|
].waiting_for_capabilities():
|
|
|
|
event["server"].cap_started = False
|
|
|
|
event["server"].send_capability_end()
|
2018-09-07 14:51:41 +00:00
|
|
|
elif subcommand == "nack":
|
2018-09-03 21:17:01 +00:00
|
|
|
event["server"].send_capability_end()
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# the server is asking for authentication
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.authenticate")
|
2018-08-29 11:20:25 +00:00
|
|
|
def authenticate(self, event):
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "authenticate", message=event["args"][0],
|
|
|
|
server=event["server"])
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# someone has changed their nickname
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.nick")
|
2018-08-29 11:20:25 +00:00
|
|
|
def nick(self, event):
|
2018-11-04 15:35:52 +00:00
|
|
|
new_nickname = event["args"].get(0)
|
2018-09-10 08:31:24 +00:00
|
|
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
old_nickname = user.nickname
|
|
|
|
user.set_nickname(new_nickname)
|
|
|
|
event["server"].change_user_nickname(old_nickname, new_nickname)
|
2018-09-04 09:41:16 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "nick", new_nickname=new_nickname,
|
2018-09-04 09:41:16 +00:00
|
|
|
old_nickname=old_nickname, user=user, server=event["server"])
|
2018-08-29 11:20:25 +00:00
|
|
|
else:
|
|
|
|
old_nickname = event["server"].nickname
|
|
|
|
event["server"].set_own_nickname(new_nickname)
|
2018-09-04 09:41:16 +00:00
|
|
|
|
2018-11-27 15:43:32 +00:00
|
|
|
self.events.on("self.nick").call(server=event["server"],
|
2018-09-04 09:41:16 +00:00
|
|
|
new_nickname=new_nickname, old_nickname=old_nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# something's mode has changed
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.mode")
|
2018-08-29 11:20:25 +00:00
|
|
|
def mode(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
target = event["args"][0]
|
|
|
|
is_channel = target[0] in event["server"].channel_types
|
|
|
|
if is_channel:
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(target)
|
2018-08-29 11:20:25 +00:00
|
|
|
remove = False
|
|
|
|
args = event["args"][2:]
|
2018-09-02 21:40:41 +00:00
|
|
|
_args = args[:]
|
2018-08-30 10:12:48 +00:00
|
|
|
modes = RE_MODES.findall(event["args"][1])
|
|
|
|
for chunk in modes:
|
|
|
|
remove = chunk[0] == "-"
|
|
|
|
for mode in chunk[1:]:
|
|
|
|
if mode in event["server"].channel_modes:
|
|
|
|
channel.change_mode(remove, mode)
|
2018-10-03 15:44:00 +00:00
|
|
|
elif mode in event["server"].prefix_modes and len(args):
|
2018-08-30 10:12:48 +00:00
|
|
|
channel.change_mode(remove, mode, args.pop(0))
|
2018-12-06 08:26:07 +00:00
|
|
|
elif (mode in event["server"].channel_list_modes or
|
|
|
|
mode in event["server"].channel_paramatered_modes):
|
|
|
|
args.pop(0)
|
|
|
|
elif not remove:
|
2018-08-29 11:20:25 +00:00
|
|
|
args.pop(0)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "mode.channel", modes=modes, mode_args=_args,
|
|
|
|
channel=channel, server=event["server"], user=user)
|
2018-08-29 11:20:25 +00:00
|
|
|
elif event["server"].is_own_nickname(target):
|
2018-11-04 15:35:52 +00:00
|
|
|
modes = RE_MODES.findall(event["args"][1])
|
2018-08-30 10:12:48 +00:00
|
|
|
for chunk in modes:
|
|
|
|
remove = chunk[0] == "-"
|
2018-08-30 10:15:01 +00:00
|
|
|
for mode in chunk[1:]:
|
2018-08-30 10:12:48 +00:00
|
|
|
event["server"].change_own_mode(remove, mode)
|
2018-11-27 15:43:32 +00:00
|
|
|
self.events.on("self.mode").call(modes=modes,
|
|
|
|
server=event["server"])
|
2018-11-27 13:16:43 +00:00
|
|
|
event["server"].send_who(event["server"].nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-09-03 10:47:11 +00:00
|
|
|
# someone (maybe me!) has been invited somewhere
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.invite")
|
2018-08-29 11:20:25 +00:00
|
|
|
def invite(self, event):
|
2018-11-12 22:37:01 +00:00
|
|
|
target_channel = event["args"][1]
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-09-03 11:16:27 +00:00
|
|
|
target_user = event["server"].get_user(event["args"][0])
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "invite", user=user, target_channel=target_channel,
|
|
|
|
server=event["server"], target_user=target_user)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
# we've received/sent a message
|
|
|
|
@utils.hook("raw.received.privmsg")
|
|
|
|
@utils.hook("raw.send.privmsg")
|
2018-08-29 11:20:25 +00:00
|
|
|
def privmsg(self, event):
|
2018-11-14 14:41:28 +00:00
|
|
|
user = None
|
|
|
|
user_nickname = None
|
|
|
|
if "prefix" in event:
|
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
|
|
|
user_nickname = user.nickname
|
|
|
|
|
2018-11-04 15:35:52 +00:00
|
|
|
message = event["args"][1]
|
2018-08-29 11:20:25 +00:00
|
|
|
message_split = message.split(" ")
|
|
|
|
target = event["args"][0]
|
2018-11-14 21:28:27 +00:00
|
|
|
|
|
|
|
channel = None
|
|
|
|
if target[0] in event["server"].channel_types:
|
|
|
|
channel = event["server"].channels.get(event["args"][0])
|
|
|
|
|
|
|
|
action = False
|
|
|
|
ctcp_message = utils.irc.parse_ctcp(message)
|
|
|
|
if ctcp_message:
|
|
|
|
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)
|
2018-09-03 21:17:34 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
if user and "account" in event["tags"]:
|
2018-09-21 09:35:18 +00:00
|
|
|
user.identified_account = event["tags"]["account"]
|
|
|
|
user.identified_account_id = event["server"].get_user(
|
|
|
|
event["tags"]["account"]).get_id()
|
|
|
|
|
2018-09-03 21:17:34 +00:00
|
|
|
kwargs = {"message": message, "message_split": message_split,
|
|
|
|
"server": event["server"], "tags": event["tags"],
|
|
|
|
"action": action}
|
|
|
|
|
2018-11-14 22:07:01 +00:00
|
|
|
if channel:
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "message.channel", user=user, channel=channel,
|
|
|
|
**kwargs)
|
|
|
|
channel.buffer.add_message(user_nickname, message, action,
|
|
|
|
event["tags"], user==None)
|
2018-08-29 11:20:25 +00:00
|
|
|
elif event["server"].is_own_nickname(target):
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "message.private", user=user, **kwargs)
|
2018-11-14 21:28:27 +00:00
|
|
|
user.buffer.add_message(user.nickname, message, action,
|
|
|
|
event["tags"], False)
|
|
|
|
elif not "prefix" in event:
|
|
|
|
# a message we've sent to a user
|
|
|
|
user = event["server"].get_user(target)
|
|
|
|
user.buffer.add_message(None, message, action, event["tags"], True)
|
|
|
|
self._event(event, "message.private", user=user, **kwargs)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-11-14 21:28:27 +00:00
|
|
|
# we've received/sent a notice
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.notice")
|
2018-11-14 21:28:27 +00:00
|
|
|
@utils.hook("raw.send.notice")
|
2018-08-29 11:20:25 +00:00
|
|
|
def notice(self, event):
|
2018-11-04 15:35:52 +00:00
|
|
|
message = event["args"][1]
|
2018-08-29 11:20:25 +00:00
|
|
|
message_split = message.split(" ")
|
|
|
|
target = event["args"][0]
|
2018-09-06 10:41:32 +00:00
|
|
|
|
2018-11-14 22:14:12 +00:00
|
|
|
if "prefix" in event and (not event["prefix"] or
|
2018-11-14 21:28:27 +00:00
|
|
|
event["prefix"].hostmask == event["server"].name or
|
|
|
|
target == "*" or
|
|
|
|
(not event["prefix"].hostname and not event["server"].name)):
|
2018-09-10 08:31:24 +00:00
|
|
|
event["server"].name = event["prefix"].hostmask
|
2018-09-06 10:41:32 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "server-notice", message=message,
|
|
|
|
message_split=message_split, server=event["server"])
|
2018-11-14 22:18:54 +00:00
|
|
|
else:
|
2018-11-14 21:28:27 +00:00
|
|
|
user = None
|
|
|
|
user_nickname = None
|
|
|
|
if "prefix" in event:
|
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
|
|
|
|
|
|
|
kwargs = {"message": message, "message_split": message_split,
|
|
|
|
"server": event["server"], "tags": event["tags"]}
|
2018-09-06 10:41:32 +00:00
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
if target[0] in event["server"].channel_types:
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(target)
|
2018-11-14 21:28:27 +00:00
|
|
|
self._event(event, "notice.channel", user=user, channel=channel,
|
|
|
|
**kwargs)
|
2018-11-14 22:07:01 +00:00
|
|
|
channel.buffer.add_notice(user_nickname, message, event["tags"],
|
|
|
|
user==None)
|
2018-08-29 11:20:25 +00:00
|
|
|
elif event["server"].is_own_nickname(target):
|
2018-11-14 21:28:27 +00:00
|
|
|
self._event(event, "notice.private", user=user, **kwargs)
|
2018-11-14 22:07:01 +00:00
|
|
|
user.buffer.add_notice(user.nickname, message, event["tags"],
|
|
|
|
False)
|
2018-11-14 22:18:54 +00:00
|
|
|
elif not "prefix" in event:
|
|
|
|
# a notice we've sent to a user
|
|
|
|
user = event["server"].get_user(target)
|
|
|
|
user.buffer.add_message(None, message, event["tags"], True)
|
|
|
|
self._event(event, "notice.private", user=user, **kwargs)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-09-03 20:47:59 +00:00
|
|
|
# IRCv3 TAGMSG, used to send tags without any other information
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.tagmsg")
|
2018-09-03 20:47:59 +00:00
|
|
|
def tagmsg(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-09-03 20:47:59 +00:00
|
|
|
target = event["args"][0]
|
|
|
|
|
|
|
|
if target[0] in event["server"].channel_types:
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(target)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "tagmsg.channel", channel=channel, user=user,
|
|
|
|
tags=event["tags"], server=event["server"])
|
2018-09-03 20:47:59 +00:00
|
|
|
elif event["server"].is_own_nickname(target):
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "tagmsg.private", user=user, tags=event["tags"],
|
|
|
|
server=event["server"])
|
2018-09-03 20:47:59 +00:00
|
|
|
|
2018-09-03 20:58:26 +00:00
|
|
|
# IRCv3 AWAY, used to notify us that a client we can see has changed /away
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.away")
|
2018-09-03 20:58:26 +00:00
|
|
|
def away(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-11-04 15:35:52 +00:00
|
|
|
message = event["args"].get(0)
|
2018-09-03 20:58:26 +00:00
|
|
|
if message:
|
2018-09-04 09:15:20 +00:00
|
|
|
user.away = True
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "away.on", user=user, server=event["server"],
|
|
|
|
message=message)
|
2018-09-03 20:58:26 +00:00
|
|
|
else:
|
2018-09-04 09:15:20 +00:00
|
|
|
user.away = False
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "away.off", user=user, server=event["server"])
|
2018-09-03 20:58:26 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.batch")
|
2018-09-10 07:57:10 +00:00
|
|
|
def batch(self, event):
|
|
|
|
identifier = event["args"][0]
|
|
|
|
modifier, identifier = identifier[0], identifier[1:]
|
|
|
|
if modifier == "+":
|
|
|
|
event["server"].batches[identifier] = []
|
|
|
|
else:
|
|
|
|
lines = event["server"].batches[identifier]
|
|
|
|
del event["server"].batches[identifier]
|
|
|
|
for line in lines:
|
2018-10-03 10:32:20 +00:00
|
|
|
self._handle(line)
|
2018-09-10 07:57:10 +00:00
|
|
|
|
2018-09-03 20:58:26 +00:00
|
|
|
# IRCv3 CHGHOST, a user's username and/or hostname has changed
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.chghost")
|
2018-09-03 10:47:11 +00:00
|
|
|
def chghost(self, event):
|
|
|
|
username = event["args"][0]
|
|
|
|
hostname = event["args"][1]
|
2018-09-10 08:10:43 +00:00
|
|
|
|
2018-09-10 08:31:24 +00:00
|
|
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
2018-09-10 08:10:43 +00:00
|
|
|
target = event["server"].get_user("nickanme")
|
|
|
|
else:
|
|
|
|
target = event["server"]
|
|
|
|
target.username = username
|
|
|
|
target.hostname = hostname
|
2018-09-03 10:47:11 +00:00
|
|
|
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.account")
|
2018-09-03 11:31:00 +00:00
|
|
|
def account(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-09-03 11:31:00 +00:00
|
|
|
|
|
|
|
if not event["args"][0] == "*":
|
2018-09-05 07:30:13 +00:00
|
|
|
user.identified_account = event["args"][0]
|
2018-09-05 11:04:34 +00:00
|
|
|
user.identified_account_id = event["server"].get_user(
|
2018-09-05 12:34:21 +00:00
|
|
|
event["args"][0]).get_id()
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "account.login", user=user,
|
2018-09-05 07:30:13 +00:00
|
|
|
server=event["server"], account=event["args"][0])
|
2018-09-03 11:31:00 +00:00
|
|
|
else:
|
2018-09-04 08:18:14 +00:00
|
|
|
user.identified_account = None
|
2018-09-05 11:04:34 +00:00
|
|
|
user.identified_account_id = None
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "account.logout", user=user,
|
2018-09-03 11:31:00 +00:00
|
|
|
server=event["server"])
|
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
# response to a WHO command for user information
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.352", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_352(self, event):
|
2018-11-27 12:14:03 +00:00
|
|
|
nickname = event["args"][5]
|
|
|
|
if not event["server"].is_own_nickname(nickname):
|
|
|
|
target = event["server"].get_user(nickname)
|
|
|
|
else:
|
|
|
|
target = event["server"]
|
|
|
|
target.username = event["args"][2]
|
|
|
|
target.hostname = event["args"][3]
|
|
|
|
|
2018-09-05 10:58:10 +00:00
|
|
|
# response to a WHOX command for user information, including account name
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.354", default_event=True)
|
2018-09-05 10:58:10 +00:00
|
|
|
def handle_354(self, event):
|
2018-09-10 07:57:41 +00:00
|
|
|
if event["args"][1] == "111":
|
2018-09-05 10:58:10 +00:00
|
|
|
nickname = event["args"][4]
|
2018-11-27 12:14:03 +00:00
|
|
|
|
|
|
|
if not event["server"].is_own_nickname(nickname):
|
|
|
|
target = event["server"].get_user(nickname)
|
|
|
|
|
|
|
|
account = event["args"][5]
|
|
|
|
if not account == "0":
|
|
|
|
target.identified_account = account
|
|
|
|
else:
|
|
|
|
target.identified_account = None
|
|
|
|
else:
|
|
|
|
target = event["server"]
|
|
|
|
|
|
|
|
target.username = event["args"][2]
|
|
|
|
target.hostname = event["args"][3]
|
|
|
|
target.realname = event["args"][6]
|
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# response to an empty mode command
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.324", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_324(self, event):
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][1])
|
2018-08-29 11:20:25 +00:00
|
|
|
modes = event["args"][2]
|
|
|
|
if modes[0] == "+" and modes[1:]:
|
|
|
|
for mode in modes[1:]:
|
|
|
|
if mode in event["server"].channel_modes:
|
|
|
|
channel.add_mode(mode)
|
|
|
|
|
|
|
|
# channel creation unix timestamp
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.329", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_329(self, event):
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][1])
|
2018-08-29 11:20:25 +00:00
|
|
|
channel.creation_timestamp = int(event["args"][2])
|
|
|
|
|
|
|
|
# nickname already in use
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.433", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_433(self, event):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# we need a registered nickname for this channel
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.477", default_event=True)
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_477(self, event):
|
2019-01-24 17:12:50 +00:00
|
|
|
channel_name = event["server"].irc_lower(event["args"][1])
|
2018-12-19 21:43:10 +00:00
|
|
|
if channel_name in event["server"].channels:
|
2018-09-28 15:51:36 +00:00
|
|
|
key = event["server"].attempted_join[channel_name]
|
|
|
|
self.timers.add("rejoin", 5, channel_name=channe_name, key=key,
|
2018-08-29 11:20:25 +00:00
|
|
|
server_id=event["server"].id)
|
|
|
|
|
|
|
|
# someone's been kicked from a channel
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.kick")
|
2018-08-29 11:20:25 +00:00
|
|
|
def kick(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
target = event["args"][1]
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(event["args"][0])
|
2018-11-04 15:35:52 +00:00
|
|
|
reason = event["args"].get(2)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
if not event["server"].is_own_nickname(target):
|
|
|
|
target_user = event["server"].get_user(target)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "kick", channel=channel, reason=reason,
|
|
|
|
target_user=target_user, user=user, server=event["server"])
|
2018-08-29 11:20:25 +00:00
|
|
|
else:
|
2018-11-27 15:43:32 +00:00
|
|
|
self.events.on("self.kick").call(channel=channel, reason=reason,
|
2018-11-14 14:41:28 +00:00
|
|
|
user=user, server=event["server"])
|
2018-11-04 14:48:55 +00:00
|
|
|
|
|
|
|
# a channel has been renamed
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("raw.received.rename")
|
2018-11-04 14:48:55 +00:00
|
|
|
def rename(self, event):
|
|
|
|
old_name = event["args"][0]
|
|
|
|
new_name = event["args"][1]
|
2018-11-11 12:34:22 +00:00
|
|
|
channel = event["server"].channels.get(old_name)
|
2018-11-04 14:48:55 +00:00
|
|
|
|
2018-11-11 12:34:22 +00:00
|
|
|
event["server"].channels.rename(old_name, new_name)
|
2018-11-14 14:41:28 +00:00
|
|
|
self._event(event, "rename", channel=channel, old_name=old_name,
|
|
|
|
new_name=new_name, reason=event["args"].get(2),
|
|
|
|
server=event["server"])
|