2016-03-31 12:20:03 +00:00
|
|
|
import re, threading
|
2016-03-29 11:56:58 +00:00
|
|
|
import Utils
|
|
|
|
|
|
|
|
RE_PREFIXES = re.compile(r"\bPREFIX=\((\w+)\)(\W+)(?:\b|$)")
|
|
|
|
RE_CHANMODES = re.compile(
|
|
|
|
r"\bCHANMODES=(\w*),(\w*),(\w*),(\w*)(?:\b|$)")
|
|
|
|
RE_CHANTYPES = re.compile(r"\bCHANTYPES=(\W+)(?:\b|$)")
|
2018-09-11 07:52:12 +00:00
|
|
|
RE_CASEMAPPING = re.compile(r"\bCASEMAPPING=(\S+)")
|
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-09-10 07:57:10 +00:00
|
|
|
"draft/message-tags-0.2", "server-time", "cap-notify",
|
|
|
|
"batch", "draft/labeled-response"}
|
2018-09-03 10:47:11 +00:00
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
class LineHandler(object):
|
2018-08-31 11:55:52 +00:00
|
|
|
def __init__(self, bot, events):
|
2018-08-29 11:20:25 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events = events
|
2018-09-04 09:41:16 +00:00
|
|
|
events.on("raw.PING").hook(self.ping)
|
|
|
|
|
|
|
|
events.on("raw.001").hook(self.handle_001, default_event=True)
|
|
|
|
events.on("raw.005").hook(self.handle_005)
|
|
|
|
events.on("raw.311").hook(self.handle_311, default_event=True)
|
|
|
|
events.on("raw.332").hook(self.handle_332)
|
|
|
|
events.on("raw.333").hook(self.handle_333)
|
|
|
|
events.on("raw.353").hook(self.handle_353, default_event=True)
|
|
|
|
events.on("raw.366").hook(self.handle_366, default_event=True)
|
|
|
|
events.on("raw.421").hook(self.handle_421, default_event=True)
|
|
|
|
events.on("raw.352").hook(self.handle_352, default_event=True)
|
2018-09-05 10:58:10 +00:00
|
|
|
events.on("raw.354").hook(self.handle_354, default_event=True)
|
2018-09-04 09:41:16 +00:00
|
|
|
events.on("raw.324").hook(self.handle_324, default_event=True)
|
|
|
|
events.on("raw.329").hook(self.handle_329, default_event=True)
|
|
|
|
events.on("raw.433").hook(self.handle_433, default_event=True)
|
|
|
|
events.on("raw.477").hook(self.handle_477, default_event=True)
|
|
|
|
|
|
|
|
events.on("raw.JOIN").hook(self.join)
|
|
|
|
events.on("raw.PART").hook(self.part)
|
|
|
|
events.on("raw.QUIT").hook(self.quit)
|
|
|
|
events.on("raw.NICK").hook(self.nick)
|
|
|
|
events.on("raw.MODE").hook(self.mode)
|
|
|
|
events.on("raw.KICK").hook(self.kick)
|
|
|
|
events.on("raw.INVITE").hook(self.invite)
|
|
|
|
events.on("raw.TOPIC").hook(self.topic)
|
|
|
|
events.on("raw.PRIVMSG").hook(self.privmsg)
|
|
|
|
events.on("raw.NOTICE").hook(self.notice)
|
2018-09-10 07:57:10 +00:00
|
|
|
|
|
|
|
events.on("raw.CAP").hook(self.cap)
|
|
|
|
events.on("raw.AUTHENTICATE").hook(self.authenticate)
|
2018-09-04 09:41:16 +00:00
|
|
|
events.on("raw.CHGHOST").hook(self.chghost)
|
|
|
|
events.on("raw.ACCOUNT").hook(self.account)
|
2018-09-09 22:23:16 +00:00
|
|
|
events.on("raw.TAGMSG").hook(self.tagmsg)
|
2018-09-04 09:41:16 +00:00
|
|
|
events.on("raw.AWAY").hook(self.away)
|
2018-09-10 07:57:10 +00:00
|
|
|
events.on("raw.BATCH").hook(self.batch)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
def handle(self, server, line):
|
|
|
|
original_line = line
|
2018-09-03 10:14:27 +00:00
|
|
|
tags = {}
|
2018-08-29 11:20:25 +00:00
|
|
|
prefix = None
|
|
|
|
command = None
|
2018-09-03 10:14:27 +00:00
|
|
|
|
|
|
|
if line[0] == "@":
|
|
|
|
tags_prefix, line = line[1:].split(" ", 1)
|
|
|
|
for tag in tags_prefix.split(";"):
|
2018-09-03 11:42:50 +00:00
|
|
|
if tag:
|
|
|
|
tag_split = tag.split("=", 1)
|
2018-09-03 19:04:58 +00:00
|
|
|
tags[tag_split[0]] = "".join(tag_split[1:])
|
2018-09-10 07:57:10 +00:00
|
|
|
if "batch" in tags and tags["batch"] in server.batches:
|
|
|
|
server.batches[tag["batch"]].append(line)
|
|
|
|
return
|
2018-09-03 10:14:27 +00:00
|
|
|
|
2018-09-06 10:40:43 +00:00
|
|
|
arbitrary = None
|
|
|
|
if " :" in line:
|
|
|
|
line, arbitrary = line.split(" :", 1)
|
|
|
|
if line.endswith(" "):
|
|
|
|
line = line[:-1]
|
2018-08-29 11:20:25 +00:00
|
|
|
if line[0] == ":":
|
2018-09-04 06:20:27 +00:00
|
|
|
prefix, command = line[1:].split(" ", 1)
|
2018-09-10 08:31:24 +00:00
|
|
|
prefix = Utils.seperate_hostmask(prefix)
|
2018-09-04 06:20:27 +00:00
|
|
|
if " " in command:
|
|
|
|
command, line = command.split(" ", 1)
|
2017-07-16 20:18:58 +00:00
|
|
|
else:
|
2018-09-06 10:40:43 +00:00
|
|
|
command = line
|
|
|
|
if " " in line:
|
|
|
|
command, line = line.split(" ", 1)
|
2018-08-29 11:20:25 +00:00
|
|
|
args = line.split(" ")
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
hooks = self.events.on("raw").on(command).get_hooks()
|
2018-08-29 11:20:25 +00:00
|
|
|
default_event = False
|
|
|
|
for hook in hooks:
|
|
|
|
if hook.kwargs.get("default_event", False):
|
|
|
|
default_event = True
|
|
|
|
break
|
2018-09-10 07:56:21 +00:00
|
|
|
last = arbitrary or args[-1]
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
#server, prefix, command, args, arbitrary
|
2018-09-10 07:56:21 +00:00
|
|
|
self.events.on("raw").on(command).call(server=server, last=last,
|
2018-09-03 10:14:27 +00:00
|
|
|
prefix=prefix, args=args, arbitrary=arbitrary, tags=tags)
|
2018-08-29 11:43:54 +00:00
|
|
|
if default_event or not hooks:
|
2018-08-29 11:20:25 +00:00
|
|
|
if command.isdigit():
|
2018-09-10 07:56:21 +00:00
|
|
|
self.events.on("received.numeric").on(command).call(
|
|
|
|
line=original_line, server=server, tags=tags, last=last,
|
2018-08-30 10:13:05 +00:00
|
|
|
line_split=original_line.split(" "), number=command)
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events.on("received").on(command).call(
|
2018-08-30 10:13:05 +00:00
|
|
|
line=original_line, line_split=original_line.split(" "),
|
2018-09-10 07:56:21 +00:00
|
|
|
command=command, server=server, tags=tags, last=last)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-08-29 11:54:47 +00:00
|
|
|
# ping from the server
|
2018-08-29 11:20:25 +00:00
|
|
|
def ping(self, event):
|
2018-09-10 07:56:21 +00:00
|
|
|
event["server"].send_pong(event["last"])
|
2018-08-29 11:20:25 +00:00
|
|
|
|
2018-08-29 11:54:47 +00:00
|
|
|
# first numeric line the server sends
|
2018-08-29 11:20:25 +00:00
|
|
|
def handle_001(self, event):
|
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-08-29 11:20:25 +00:00
|
|
|
def handle_005(self, event):
|
|
|
|
isupport_line = " ".join(event["args"][1:])
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
if "NAMESX" in isupport_line:
|
|
|
|
event["server"].send("PROTOCTL NAMESX")
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
match = re.search(RE_PREFIXES, isupport_line)
|
|
|
|
if match:
|
|
|
|
event["server"].mode_prefixes.clear()
|
|
|
|
modes = match.group(1)
|
|
|
|
prefixes = match.group(2)
|
|
|
|
for i, prefix in enumerate(prefixes):
|
|
|
|
if i < len(modes):
|
|
|
|
event["server"].mode_prefixes[prefix] = modes[i]
|
|
|
|
match = re.search(RE_CHANMODES, isupport_line)
|
|
|
|
if match:
|
|
|
|
event["server"].channel_modes = list(match.group(4))
|
|
|
|
match = re.search(RE_CHANTYPES, isupport_line)
|
|
|
|
if match:
|
|
|
|
event["server"].channel_types = list(match.group(1))
|
2018-09-11 07:52:12 +00:00
|
|
|
|
|
|
|
match = re.search(RE_CASEMAPPING, isupport_line)
|
|
|
|
if match:
|
|
|
|
event["server"].case_mapping = match.group(1)
|
|
|
|
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.numeric.005").call(
|
2018-08-29 11:20:25 +00:00
|
|
|
isupport=isupport_line, server=event["server"])
|
|
|
|
|
|
|
|
# whois respose (nickname, username, realname, hostname)
|
|
|
|
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]
|
|
|
|
target.realname = event["arbitrary"]
|
|
|
|
|
|
|
|
# on-join channel topic line
|
|
|
|
def handle_332(self, event):
|
2018-08-29 13:57:40 +00:00
|
|
|
channel = event["server"].get_channel(event["args"][1])
|
|
|
|
|
|
|
|
channel.set_topic(event["arbitrary"])
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.numeric.332").call(channel=channel,
|
|
|
|
server=event["server"], topic=event["arbitrary"])
|
2018-08-29 13:57:40 +00:00
|
|
|
|
|
|
|
# channel topic changed
|
|
|
|
def topic(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-08-29 13:57:40 +00:00
|
|
|
channel = event["server"].get_channel(event["args"][0])
|
|
|
|
channel.set_topic(event["arbitrary"])
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.topic").call(channel=channel,
|
2018-08-29 13:57:40 +00:00
|
|
|
server=event["server"], topic=event["arbitrary"], user=user)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# on-join channel topic set by/at
|
|
|
|
def handle_333(self, event):
|
|
|
|
channel = event["server"].get_channel(event["args"][1])
|
|
|
|
|
|
|
|
topic_setter_hostmask = event["args"][2]
|
2018-09-10 08:31:24 +00:00
|
|
|
topic_setter = Utils.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-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.numeric.333").call(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-08-29 11:20:25 +00:00
|
|
|
def handle_353(self, event):
|
|
|
|
channel = event["server"].get_channel(event["args"][2])
|
|
|
|
nicknames = event["arbitrary"].split()
|
|
|
|
for nickname in nicknames:
|
2018-09-04 06:11:02 +00:00
|
|
|
modes = set([])
|
|
|
|
|
|
|
|
while nickname[0] in event["server"].mode_prefixes:
|
|
|
|
modes.add(event["server"].mode_prefixes[nickname[0]])
|
|
|
|
nickname = nickname[1:]
|
|
|
|
|
2018-09-04 07:04:49 +00:00
|
|
|
if "userhost-in-names" in event["server"].capabilities:
|
2018-09-10 08:31:24 +00:00
|
|
|
hostmask = Utils.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
|
|
|
|
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
|
|
|
|
|
|
|
# on user joining channel
|
|
|
|
def join(self, event):
|
2018-09-03 11:37:07 +00:00
|
|
|
account = None
|
|
|
|
realname = None
|
|
|
|
if len(event["args"]) == 2:
|
|
|
|
channel = event["server"].get_channel(event["args"][0])
|
|
|
|
if not event["args"] == "*":
|
|
|
|
account = event["args"][1]
|
|
|
|
realname = event["arbitrary"]
|
|
|
|
else:
|
2018-09-10 07:56:21 +00:00
|
|
|
channel = event["server"].get_channel(event["last"])
|
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)
|
|
|
|
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-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.join").call(channel=channel,
|
2018-09-03 11:37:07 +00:00
|
|
|
user=user, server=event["server"], account=account,
|
|
|
|
realname=realname)
|
2018-08-29 11:20:25 +00:00
|
|
|
else:
|
|
|
|
if channel.name in event["server"].attempted_join:
|
|
|
|
del event["server"].attempted_join[channel.name]
|
2018-09-04 09:41:16 +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
|
|
|
|
def part(self, event):
|
|
|
|
channel = event["server"].get_channel(event["args"][0])
|
|
|
|
reason = event["arbitrary"] or ""
|
|
|
|
|
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-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.part").call(channel=channel,
|
2018-08-29 11:20:25 +00:00
|
|
|
reason=reason, user=user, server=event["server"])
|
|
|
|
channel.remove_user(user)
|
|
|
|
user.part_channel(channel)
|
|
|
|
if not len(user.channels):
|
|
|
|
event["server"].remove_user(user)
|
|
|
|
else:
|
|
|
|
event["server"].remove_channel(channel)
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("self.part").call(channel=channel,
|
2018-08-29 11:20:25 +00:00
|
|
|
reason=reason, server=event["server"])
|
|
|
|
|
|
|
|
# unknown command sent by us, oops!
|
|
|
|
def handle_421(self, event):
|
|
|
|
print("warning: unknown command '%s'." % event["args"][1])
|
|
|
|
|
|
|
|
# a user has disconnected!
|
|
|
|
def quit(self, event):
|
|
|
|
reason = event["arbitrary"] or ""
|
|
|
|
|
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-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.quit").call(reason=reason,
|
2018-08-29 11:20:25 +00:00
|
|
|
user=user, server=event["server"])
|
|
|
|
else:
|
|
|
|
event["server"].disconnect()
|
|
|
|
|
|
|
|
# the server is telling us about its capabilities!
|
|
|
|
def cap(self, event):
|
2018-09-07 14:51:41 +00:00
|
|
|
capabilities_list = (event["arbitrary"] or "").split(" ")
|
|
|
|
capabilities = {}
|
|
|
|
for capability in capabilities_list:
|
|
|
|
argument = None
|
|
|
|
if "=" in capability:
|
|
|
|
capability, argument = capability.split("=", 1)
|
|
|
|
capabilities[capability] = argument
|
2018-09-03 10:14:27 +00:00
|
|
|
|
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-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)
|
|
|
|
|
|
|
|
self.events.on("received.cap.ls").call(
|
|
|
|
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()))
|
|
|
|
self.events.on("received.cap.new").call(server=event["server"],
|
|
|
|
capabilities=capabilities)
|
|
|
|
elif subcommand == "del":
|
|
|
|
event["server"].capabilities.difference_update(set(
|
|
|
|
capabilities.keys()))
|
|
|
|
self.events.on("received.cap.del").call(server=event["server"],
|
|
|
|
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:
|
|
|
|
self.events.on("received.cap.ack").call(
|
|
|
|
capabilities=event["server"].capabilities,
|
|
|
|
server=event["server"])
|
|
|
|
|
|
|
|
if not event["server"].waiting_for_capabilities():
|
|
|
|
event["server"].send_capability_end()
|
|
|
|
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
|
|
|
|
def authenticate(self, event):
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.authenticate").call(
|
2018-08-29 11:20:25 +00:00
|
|
|
message=event["args"][0], server=event["server"])
|
|
|
|
|
|
|
|
# someone has changed their nickname
|
|
|
|
def nick(self, event):
|
|
|
|
new_nickname = event["arbitrary"]
|
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
|
|
|
|
|
|
|
self.events.on("received.nick").call(new_nickname=new_nickname,
|
|
|
|
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
|
|
|
|
|
|
|
self.events.on("self.nick").call(server=event["server"],
|
|
|
|
new_nickname=new_nickname, old_nickname=old_nickname)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# something's mode has changed
|
|
|
|
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:
|
|
|
|
channel = event["server"].get_channel(target)
|
|
|
|
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)
|
|
|
|
elif mode in event["server"].mode_prefixes.values(
|
2018-08-29 11:20:25 +00:00
|
|
|
) and len(args):
|
2018-08-30 10:12:48 +00:00
|
|
|
channel.change_mode(remove, mode, args.pop(0))
|
|
|
|
else:
|
2018-08-29 11:20:25 +00:00
|
|
|
args.pop(0)
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.mode.channel").call(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-09-10 07:56:21 +00:00
|
|
|
modes = RE_MODES.findall(event["last"])
|
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-09-04 09:41:16 +00:00
|
|
|
self.events.on("self.mode").call(modes=modes,
|
2018-08-29 11:20:25 +00:00
|
|
|
server=event["server"])
|
|
|
|
|
2018-09-03 10:47:11 +00:00
|
|
|
# someone (maybe me!) has been invited somewhere
|
2018-08-29 11:20:25 +00:00
|
|
|
def invite(self, event):
|
2018-09-10 07:56:21 +00:00
|
|
|
target_channel = event["last"]
|
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-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.invite").call(user=user,
|
|
|
|
target_channel=target_channel, server=event["server"],
|
|
|
|
target_user=target_user)
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# we've received a message
|
|
|
|
def privmsg(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
|
|
|
message = event["arbitrary"] or ""
|
|
|
|
message_split = message.split(" ")
|
|
|
|
target = event["args"][0]
|
2018-09-05 14:02:38 +00:00
|
|
|
action = message.startswith("\x01ACTION ")
|
2018-08-29 11:20:25 +00:00
|
|
|
if action:
|
2018-09-05 14:02:38 +00:00
|
|
|
message = message.replace("\x01ACTION ", "", 1)
|
|
|
|
if message.endswith("\x01"):
|
|
|
|
message = message[:-1]
|
2018-09-03 21:17:34 +00:00
|
|
|
|
|
|
|
kwargs = {"message": message, "message_split": message_split,
|
|
|
|
"server": event["server"], "tags": event["tags"],
|
|
|
|
"action": action}
|
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
if target[0] in event["server"].channel_types:
|
|
|
|
channel = event["server"].get_channel(event["args"][0])
|
2018-09-03 21:32:48 +00:00
|
|
|
self.events.on("received.message.channel").call(
|
|
|
|
user=user, channel=channel, **kwargs)
|
|
|
|
channel.buffer.add_line(user.nickname, message, action)
|
2018-08-29 11:20:25 +00:00
|
|
|
elif event["server"].is_own_nickname(target):
|
2018-09-03 21:17:34 +00:00
|
|
|
self.events.on("received.message.private").call(
|
|
|
|
user=user, **kwargs)
|
2018-08-29 11:20:25 +00:00
|
|
|
user.buffer.add_line(user.nickname, message, action)
|
|
|
|
|
|
|
|
# we've received a notice
|
|
|
|
def notice(self, event):
|
|
|
|
message = event["arbitrary"] or ""
|
|
|
|
message_split = message.split(" ")
|
|
|
|
target = event["args"][0]
|
2018-09-06 10:41:32 +00:00
|
|
|
|
2018-09-10 08:31:24 +00:00
|
|
|
if not event["prefix"] or event["prefix"].hostmask == event["server"
|
|
|
|
].name or target == "*":
|
|
|
|
event["server"].name = event["prefix"].hostmask
|
2018-09-06 10:41:32 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events.on("received.server-notice").call(
|
2018-07-22 20:46:22 +00:00
|
|
|
message=message, message_split=message_split,
|
2018-08-29 11:20:25 +00:00
|
|
|
server=event["server"])
|
|
|
|
else:
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-09-06 10:41:32 +00:00
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
if target[0] in event["server"].channel_types:
|
|
|
|
channel = event["server"].get_channel(target)
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events.on("received.notice.channel").call(
|
2018-09-03 20:48:14 +00:00
|
|
|
message=message, message_split=message_split, user=user,
|
|
|
|
server=event["server"], channel=channel,
|
|
|
|
tags=event["tags"])
|
2018-08-29 11:20:25 +00:00
|
|
|
elif event["server"].is_own_nickname(target):
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events.on("received.notice.private").call(
|
2018-09-03 20:48:14 +00:00
|
|
|
message=message, message_split=message_split, user=user,
|
|
|
|
server=event["server"], tags=event["tags"])
|
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
|
|
|
|
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:
|
|
|
|
channel = event["server"].get_channel(target)
|
|
|
|
self.events.on("received.tagmsg.channel").call(channel=channel,
|
2018-09-03 20:58:14 +00:00
|
|
|
user=user, tags=event["tags"], server=event["server"])
|
2018-09-03 20:47:59 +00:00
|
|
|
elif event["server"].is_own_nickname(target):
|
|
|
|
self.events.on("received.tagmsg.private").call(
|
2018-09-03 20:58:14 +00:00
|
|
|
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
|
|
|
|
def away(self, event):
|
2018-09-10 08:31:24 +00:00
|
|
|
user = event["server"].get_user(event["prefix"].nickname)
|
2018-09-03 20:58:26 +00:00
|
|
|
message = event["arbitrary"]
|
|
|
|
if message:
|
2018-09-04 09:15:20 +00:00
|
|
|
user.away = True
|
2018-09-03 20:58:26 +00:00
|
|
|
self.events.on("received.away.on").call(user=user,
|
|
|
|
server=event["server"], message=message)
|
|
|
|
else:
|
2018-09-04 09:15:20 +00:00
|
|
|
user.away = False
|
2018-09-03 20:58:26 +00:00
|
|
|
self.events.on("received.away.off").call(user=user,
|
|
|
|
server=event["server"])
|
|
|
|
|
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:
|
|
|
|
self.handle(event["server"], line)
|
|
|
|
|
2018-09-03 20:58:26 +00:00
|
|
|
# IRCv3 CHGHOST, a user's username and/or hostname has changed
|
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-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-09-03 11:31:00 +00:00
|
|
|
self.events.on("received.account.login").call(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-09-03 11:31:00 +00:00
|
|
|
self.events.on("received.account.logout").call(user=user,
|
|
|
|
server=event["server"])
|
|
|
|
|
2018-08-29 11:20:25 +00:00
|
|
|
# response to a WHO command for user information
|
|
|
|
def handle_352(self, event):
|
|
|
|
user = event["server"].get_user(event["args"][5])
|
|
|
|
user.username = event["args"][2]
|
|
|
|
user.hostname = event["args"][3]
|
2018-09-05 10:58:10 +00:00
|
|
|
# response to a WHOX command for user information, including account name
|
|
|
|
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
|
|
|
username = event["args"][2]
|
|
|
|
hostname = event["args"][3]
|
|
|
|
nickname = event["args"][4]
|
|
|
|
account = event["args"][5]
|
2018-09-10 07:56:21 +00:00
|
|
|
realname = event["last"]
|
2018-09-05 10:58:10 +00:00
|
|
|
|
|
|
|
user = event["server"].get_user(nickname)
|
|
|
|
user.username = username
|
|
|
|
user.hostname = hostname
|
|
|
|
user.realname = realname
|
|
|
|
if not account == "0":
|
|
|
|
user.identified_account = account
|
2018-08-29 11:20:25 +00:00
|
|
|
|
|
|
|
# response to an empty mode command
|
|
|
|
def handle_324(self, event):
|
|
|
|
channel = event["server"].get_channel(event["args"][1])
|
|
|
|
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
|
|
|
|
def handle_329(self, event):
|
|
|
|
channel = event["server"].get_channel(event["args"][1])
|
|
|
|
channel.creation_timestamp = int(event["args"][2])
|
|
|
|
|
|
|
|
# nickname already in use
|
|
|
|
def handle_433(self, event):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# we need a registered nickname for this channel
|
|
|
|
def handle_477(self, event):
|
2018-09-11 09:15:16 +00:00
|
|
|
channel_name = Utils.irc_lower(event["server"], event["args"][1])
|
2018-09-11 07:52:12 +00:00
|
|
|
if channel_name in event["server"].attempted_join:
|
2018-08-29 11:20:25 +00:00
|
|
|
self.bot.add_timer("rejoin", 5,
|
|
|
|
channel_name=event["args"][1],
|
2018-09-11 07:52:12 +00:00
|
|
|
key=event["server"].attempted_join[channel_name],
|
2018-08-29 11:20:25 +00:00
|
|
|
server_id=event["server"].id)
|
|
|
|
|
|
|
|
# someone's been kicked from a channel
|
|
|
|
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]
|
|
|
|
channel = event["server"].get_channel(event["args"][0])
|
|
|
|
reason = event["arbitrary"] or ""
|
|
|
|
|
|
|
|
if not event["server"].is_own_nickname(target):
|
|
|
|
target_user = event["server"].get_user(target)
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("received.kick").call(channel=channel,
|
2018-08-29 11:20:25 +00:00
|
|
|
reason=reason, target_user=target_user, user=user,
|
|
|
|
server=event["server"])
|
|
|
|
else:
|
2018-09-04 09:41:16 +00:00
|
|
|
self.events.on("self.kick").call(channel=channel,
|
2018-08-29 11:20:25 +00:00
|
|
|
reason=reason, user=user, server=event["server"])
|