2019-02-22 11:23:36 +00:00
|
|
|
import codecs, re
|
|
|
|
|
|
|
|
RE_ISUPPORT_ESCAPE = re.compile(r"\\x(\d\d)", re.I)
|
|
|
|
RE_MODES = re.compile(r"[-+]\w+")
|
|
|
|
|
|
|
|
def ping(event):
|
|
|
|
event["server"].send_pong(event["args"][0])
|
|
|
|
|
|
|
|
def handle_001(event):
|
2019-05-31 14:49:34 +00:00
|
|
|
event["server"].socket.enable_write_throttle()
|
2019-06-03 12:30:39 +00:00
|
|
|
event["server"].name = event["source"].hostmask
|
2019-02-22 11:23:36 +00:00
|
|
|
event["server"].set_own_nickname(event["args"][0])
|
|
|
|
event["server"].send_whois(event["server"].nickname)
|
2019-06-06 10:16:47 +00:00
|
|
|
event["server"].send_mode(event["server"].nickname)
|
2019-05-29 15:11:47 +00:00
|
|
|
event["server"].connected = True
|
2019-02-22 11:23:36 +00:00
|
|
|
|
|
|
|
def handle_005(events, event):
|
|
|
|
isupport_list = event["args"][1:-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
|
|
|
|
event["server"].isupport.update(isupport)
|
|
|
|
|
2019-05-20 15:33:32 +00:00
|
|
|
if "NAMESX" in isupport and not event["server"].has_capability_str(
|
2019-03-03 22:38:34 +00:00
|
|
|
"multi-prefix"):
|
2019-02-22 11:23:36 +00:00
|
|
|
event["server"].send("PROTOCTL NAMESX")
|
|
|
|
|
|
|
|
if "PREFIX" in isupport:
|
|
|
|
modes, symbols = isupport["PREFIX"][1:].split(")", 1)
|
|
|
|
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
|
|
|
|
|
|
|
|
if "CHANMODES" in isupport:
|
|
|
|
modes = isupport["CHANMODES"].split(",", 3)
|
|
|
|
event["server"].channel_list_modes = list(modes[0])
|
|
|
|
event["server"].channel_paramatered_modes = list(modes[1])
|
|
|
|
event["server"].channel_setting_modes = list(modes[2])
|
|
|
|
event["server"].channel_modes = list(modes[3])
|
|
|
|
if "CHANTYPES" in isupport:
|
|
|
|
event["server"].channel_types = list(isupport["CHANTYPES"])
|
|
|
|
if "CASEMAPPING" in isupport:
|
|
|
|
event["server"].case_mapping = isupport["CASEMAPPING"]
|
|
|
|
|
|
|
|
events.on("received.005").call(isupport=isupport,
|
|
|
|
server=event["server"])
|
|
|
|
|
2019-05-15 16:12:21 +00:00
|
|
|
def handle_004(event):
|
|
|
|
event["server"].version = event["args"][2]
|
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
def motd_start(event):
|
|
|
|
event["server"].motd_lines.clear()
|
|
|
|
def motd_line(event):
|
|
|
|
event["server"].motd_lines.append(event["args"][1])
|
|
|
|
|
2019-06-06 10:16:47 +00:00
|
|
|
def _own_modes(server, modes):
|
|
|
|
mode_chunks = RE_MODES.findall(modes)
|
|
|
|
for chunk in mode_chunks:
|
|
|
|
remove = chunk[0] == "-"
|
|
|
|
for mode in chunk[1:]:
|
|
|
|
server.change_own_mode(remove, mode)
|
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
def mode(events, event):
|
2019-06-03 12:30:39 +00:00
|
|
|
user = event["server"].get_user(event["source"].nickname)
|
2019-02-22 11:23:36 +00:00
|
|
|
target = event["args"][0]
|
|
|
|
is_channel = target[0] in event["server"].channel_types
|
|
|
|
if is_channel:
|
|
|
|
channel = event["server"].channels.get(target)
|
|
|
|
args = event["args"][2:]
|
|
|
|
_args = args[:]
|
|
|
|
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"].prefix_modes and len(args):
|
|
|
|
channel.change_mode(remove, mode, args.pop(0))
|
|
|
|
elif (mode in event["server"].channel_list_modes or
|
|
|
|
mode in event["server"].channel_paramatered_modes):
|
|
|
|
args.pop(0)
|
|
|
|
elif not remove:
|
|
|
|
args.pop(0)
|
|
|
|
events.on("received.mode.channel").call(modes=modes, mode_args=_args,
|
|
|
|
channel=channel, server=event["server"], user=user)
|
|
|
|
elif event["server"].is_own_nickname(target):
|
2019-06-06 10:22:45 +00:00
|
|
|
modes = event["args"][1]
|
|
|
|
_own_modes(event["server"], modes)
|
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
events.on("self.mode").call(modes=modes, server=event["server"])
|
|
|
|
event["server"].send_who(event["server"].nickname)
|
|
|
|
|
2019-06-06 10:16:47 +00:00
|
|
|
def handle_221(event):
|
|
|
|
_own_modes(event["server"], event["args"][1])
|
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
def invite(events, event):
|
|
|
|
target_channel = event["args"][1]
|
2019-06-03 12:30:39 +00:00
|
|
|
user = event["server"].get_user(event["source"].nickname)
|
2019-02-22 11:23:36 +00:00
|
|
|
target_user = event["server"].get_user(event["args"][0])
|
|
|
|
events.on("received.invite").call(user=user, target_channel=target_channel,
|
|
|
|
server=event["server"], target_user=target_user)
|
|
|
|
|
|
|
|
def handle_352(event):
|
|
|
|
nickname = event["args"][5]
|
2019-03-05 11:20:32 +00:00
|
|
|
username = event["args"][2]
|
|
|
|
hostname = event["args"][3]
|
|
|
|
|
|
|
|
if event["server"].is_own_nickname(nickname):
|
|
|
|
event["server"].username = username
|
|
|
|
event["server"].hostname = hostname
|
|
|
|
|
|
|
|
target = event["server"].get_user(nickname)
|
|
|
|
target.username = username
|
|
|
|
target.hostname = hostname
|
2019-02-22 11:23:36 +00:00
|
|
|
|
|
|
|
def handle_354(event):
|
|
|
|
if event["args"][1] == "111":
|
|
|
|
nickname = event["args"][4]
|
2019-03-05 11:20:32 +00:00
|
|
|
username = event["args"][2]
|
|
|
|
hostname = event["args"][3]
|
|
|
|
realname = event["args"][6]
|
|
|
|
account = event["args"][5]
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-03-05 11:20:32 +00:00
|
|
|
if event["server"].is_own_nickname(nickname):
|
|
|
|
event["server"].username = username
|
|
|
|
event["server"].hostname = hostname
|
|
|
|
event["server"].realname = realname
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2019-03-05 11:20:32 +00:00
|
|
|
target = event["server"].get_user(nickname)
|
|
|
|
target.username = username
|
|
|
|
target.hostname = hostname
|
|
|
|
target.realname = realname
|
|
|
|
if not account == "0":
|
|
|
|
target.identified_account = account
|
2019-02-22 11:23:36 +00:00
|
|
|
else:
|
2019-03-05 11:20:32 +00:00
|
|
|
target.identified_account = None
|
2019-02-22 11:23:36 +00:00
|
|
|
|
|
|
|
def handle_433(event):
|
|
|
|
new_nick = "%s|" % event["server"].connection_params.nickname
|
|
|
|
event["server"].send_nick(new_nick)
|