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):
|
2019-06-22 11:17:45 +00:00
|
|
|
event["server"].send_pong(event["line"].args[0])
|
2019-02-22 11:23:36 +00:00
|
|
|
|
|
|
|
def handle_001(event):
|
2019-05-31 14:49:34 +00:00
|
|
|
event["server"].socket.enable_write_throttle()
|
2019-06-22 11:17:45 +00:00
|
|
|
event["server"].name = event["line"].source.hostmask
|
|
|
|
event["server"].set_own_nickname(event["line"].args[0])
|
2019-02-22 11:23:36 +00:00
|
|
|
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):
|
2019-06-22 11:17:45 +00:00
|
|
|
isupport_list = event["line"].args[1:-1]
|
2019-02-22 11:23:36 +00:00
|
|
|
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"]
|
2019-06-08 09:59:14 +00:00
|
|
|
if "STATUSMSG" in isupport:
|
|
|
|
event["server"].statusmsg = list(isupport["STATUSMSG"])
|
2019-02-22 11:23:36 +00:00
|
|
|
|
|
|
|
events.on("received.005").call(isupport=isupport,
|
|
|
|
server=event["server"])
|
|
|
|
|
2019-05-15 16:12:21 +00:00
|
|
|
def handle_004(event):
|
2019-06-22 11:17:45 +00:00
|
|
|
event["server"].version = event["line"].args[2]
|
2019-05-15 16:12:21 +00:00
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
def motd_start(event):
|
|
|
|
event["server"].motd_lines.clear()
|
|
|
|
def motd_line(event):
|
2019-06-22 11:17:45 +00:00
|
|
|
event["server"].motd_lines.append(event["line"].args[1])
|
2019-02-22 11:23:36 +00:00
|
|
|
|
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-22 11:17:45 +00:00
|
|
|
user = event["server"].get_user(event["line"].source.nickname)
|
|
|
|
target = event["line"].args[0]
|
2019-02-22 11:23:36 +00:00
|
|
|
is_channel = target[0] in event["server"].channel_types
|
|
|
|
if is_channel:
|
|
|
|
channel = event["server"].channels.get(target)
|
2019-06-22 11:17:45 +00:00
|
|
|
modes = event["line"].args[1]
|
|
|
|
args = event["line"].args[2:]
|
2019-06-13 10:53:47 +00:00
|
|
|
|
|
|
|
channel.parse_modes(modes, args[:])
|
|
|
|
|
|
|
|
events.on("received.mode.channel").call(modes=modes, mode_args=args,
|
2019-02-22 11:23:36 +00:00
|
|
|
channel=channel, server=event["server"], user=user)
|
|
|
|
elif event["server"].is_own_nickname(target):
|
2019-06-22 11:17:45 +00:00
|
|
|
modes = event["line"].args[1]
|
2019-06-06 10:22:45 +00:00
|
|
|
_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):
|
2019-06-22 11:17:45 +00:00
|
|
|
_own_modes(event["server"], event["line"].args[1])
|
2019-06-06 10:16:47 +00:00
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
def invite(events, event):
|
2019-06-22 11:17:45 +00:00
|
|
|
target_channel = event["line"].args[1]
|
|
|
|
user = event["server"].get_user(event["line"].source.nickname)
|
|
|
|
target_user = event["server"].get_user(event["line"].args[0])
|
2019-02-22 11:23:36 +00:00
|
|
|
events.on("received.invite").call(user=user, target_channel=target_channel,
|
|
|
|
server=event["server"], target_user=target_user)
|
|
|
|
|
|
|
|
def handle_352(event):
|
2019-06-22 11:17:45 +00:00
|
|
|
nickname = event["line"].args[5]
|
|
|
|
username = event["line"].args[2]
|
|
|
|
hostname = event["line"].args[3]
|
2019-03-05 11:20:32 +00:00
|
|
|
|
|
|
|
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):
|
2019-06-22 11:17:45 +00:00
|
|
|
if event["line"].args[1] == "111":
|
|
|
|
nickname = event["line"].args[4]
|
|
|
|
username = event["line"].args[2]
|
|
|
|
hostname = event["line"].args[3]
|
|
|
|
realname = event["line"].args[6]
|
|
|
|
account = event["line"].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)
|