Pass around an IRCPrefix object instead of "nickname!username@hostname", fix a
bug in TAGMSG handling that was trying to get a user object from a channel, fix a bug in ACCOUNT handling that was literally using "nickname" as a user's nickname, instead of the parsed nickname.
This commit is contained in:
parent
35ae1a57c7
commit
7530bb7cbd
2 changed files with 42 additions and 64 deletions
|
@ -75,6 +75,7 @@ class LineHandler(object):
|
||||||
line = line[:-1]
|
line = line[:-1]
|
||||||
if line[0] == ":":
|
if line[0] == ":":
|
||||||
prefix, command = line[1:].split(" ", 1)
|
prefix, command = line[1:].split(" ", 1)
|
||||||
|
prefix = Utils.seperate_hostmask(prefix)
|
||||||
if " " in command:
|
if " " in command:
|
||||||
command, line = command.split(" ", 1)
|
command, line = command.split(" ", 1)
|
||||||
else:
|
else:
|
||||||
|
@ -110,7 +111,7 @@ class LineHandler(object):
|
||||||
|
|
||||||
# first numeric line the server sends
|
# first numeric line the server sends
|
||||||
def handle_001(self, event):
|
def handle_001(self, event):
|
||||||
event["server"].name = Utils.remove_colon(event["prefix"])
|
event["server"].name = event["prefix"].nickname
|
||||||
event["server"].set_own_nickname(event["args"][0])
|
event["server"].set_own_nickname(event["args"][0])
|
||||||
event["server"].send_whois(event["server"].nickname)
|
event["server"].send_whois(event["server"].nickname)
|
||||||
|
|
||||||
|
@ -159,11 +160,8 @@ class LineHandler(object):
|
||||||
|
|
||||||
# channel topic changed
|
# channel topic changed
|
||||||
def topic(self, event):
|
def topic(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["prefix"])
|
|
||||||
user = event["server"].get_user(nickname)
|
|
||||||
channel = event["server"].get_channel(event["args"][0])
|
channel = event["server"].get_channel(event["args"][0])
|
||||||
|
|
||||||
channel.set_topic(event["arbitrary"])
|
channel.set_topic(event["arbitrary"])
|
||||||
self.events.on("received.topic").call(channel=channel,
|
self.events.on("received.topic").call(channel=channel,
|
||||||
server=event["server"], topic=event["arbitrary"], user=user)
|
server=event["server"], topic=event["arbitrary"], user=user)
|
||||||
|
@ -173,15 +171,16 @@ class LineHandler(object):
|
||||||
channel = event["server"].get_channel(event["args"][1])
|
channel = event["server"].get_channel(event["args"][1])
|
||||||
|
|
||||||
topic_setter_hostmask = event["args"][2]
|
topic_setter_hostmask = event["args"][2]
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
topic_setter = Utils.seperate_hostmask(topic_setter_hostmask)
|
||||||
topic_setter_hostmask)
|
|
||||||
topic_time = int(event["args"][3]) if event["args"][3].isdigit(
|
topic_time = int(event["args"][3]) if event["args"][3].isdigit(
|
||||||
) else None
|
) else None
|
||||||
|
|
||||||
channel.set_topic_setter(nickname, username, hostname)
|
channel.set_topic_setter(topic_setter.nickname, topic_setter.username,
|
||||||
|
topic_setter.hostname)
|
||||||
channel.set_topic_time(topic_time)
|
channel.set_topic_time(topic_time)
|
||||||
self.events.on("received.numeric.333").call(channel=channel,
|
self.events.on("received.numeric.333").call(channel=channel,
|
||||||
setter=nickname, set_at=topic_time, server=event["server"])
|
setter=topic_setter.nickname, set_at=topic_time,
|
||||||
|
server=event["server"])
|
||||||
|
|
||||||
# /names response, also on-join user list
|
# /names response, also on-join user list
|
||||||
def handle_353(self, event):
|
def handle_353(self, event):
|
||||||
|
@ -195,11 +194,10 @@ class LineHandler(object):
|
||||||
nickname = nickname[1:]
|
nickname = nickname[1:]
|
||||||
|
|
||||||
if "userhost-in-names" in event["server"].capabilities:
|
if "userhost-in-names" in event["server"].capabilities:
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
hostmask = Utils.seperate_hostmask(nickname)
|
||||||
nickname)
|
user = event["server"].get_user(hostmask.nickname)
|
||||||
user = event["server"].get_user(nickname)
|
user.username = hostmask.username
|
||||||
user.username = username
|
user.hostname = hostmask.hostname
|
||||||
user.hostname = hostname
|
|
||||||
else:
|
else:
|
||||||
user = event["server"].get_user(nickname)
|
user = event["server"].get_user(nickname)
|
||||||
user.join_channel(channel)
|
user.join_channel(channel)
|
||||||
|
@ -214,8 +212,6 @@ class LineHandler(object):
|
||||||
|
|
||||||
# on user joining channel
|
# on user joining channel
|
||||||
def join(self, event):
|
def join(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
|
||||||
event["prefix"])
|
|
||||||
account = None
|
account = None
|
||||||
realname = None
|
realname = None
|
||||||
if len(event["args"]) == 2:
|
if len(event["args"]) == 2:
|
||||||
|
@ -226,11 +222,11 @@ class LineHandler(object):
|
||||||
else:
|
else:
|
||||||
channel = event["server"].get_channel(event["last"])
|
channel = event["server"].get_channel(event["last"])
|
||||||
|
|
||||||
if not event["server"].is_own_nickname(nickname):
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
||||||
user = event["server"].get_user(nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
if not event["server"].has_user(nickname):
|
if not user.username and not user.hostname:
|
||||||
user.username = username
|
user.username = event["prefix"].username
|
||||||
user.hostname = hostname
|
user.hostname = event["prefix"].hostname
|
||||||
|
|
||||||
if account:
|
if account:
|
||||||
user.identified_account = account
|
user.identified_account = account
|
||||||
|
@ -253,13 +249,11 @@ class LineHandler(object):
|
||||||
|
|
||||||
# on user parting channel
|
# on user parting channel
|
||||||
def part(self, event):
|
def part(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
|
||||||
event["prefix"])
|
|
||||||
channel = event["server"].get_channel(event["args"][0])
|
channel = event["server"].get_channel(event["args"][0])
|
||||||
reason = event["arbitrary"] or ""
|
reason = event["arbitrary"] or ""
|
||||||
|
|
||||||
if not event["server"].is_own_nickname(nickname):
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
||||||
user = event["server"].get_user(nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
self.events.on("received.part").call(channel=channel,
|
self.events.on("received.part").call(channel=channel,
|
||||||
reason=reason, user=user, server=event["server"])
|
reason=reason, user=user, server=event["server"])
|
||||||
channel.remove_user(user)
|
channel.remove_user(user)
|
||||||
|
@ -277,12 +271,10 @@ class LineHandler(object):
|
||||||
|
|
||||||
# a user has disconnected!
|
# a user has disconnected!
|
||||||
def quit(self, event):
|
def quit(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
|
||||||
event["prefix"])
|
|
||||||
reason = event["arbitrary"] or ""
|
reason = event["arbitrary"] or ""
|
||||||
|
|
||||||
if not event["server"].is_own_nickname(nickname):
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
||||||
user = event["server"].get_user(nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["server"].remove_user(user)
|
event["server"].remove_user(user)
|
||||||
self.events.on("received.quit").call(reason=reason,
|
self.events.on("received.quit").call(reason=reason,
|
||||||
user=user, server=event["server"])
|
user=user, server=event["server"])
|
||||||
|
@ -346,11 +338,9 @@ class LineHandler(object):
|
||||||
|
|
||||||
# someone has changed their nickname
|
# someone has changed their nickname
|
||||||
def nick(self, event):
|
def nick(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
|
||||||
event["prefix"])
|
|
||||||
new_nickname = event["arbitrary"]
|
new_nickname = event["arbitrary"]
|
||||||
if not event["server"].is_own_nickname(nickname):
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
||||||
user = event["server"].get_user(nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
old_nickname = user.nickname
|
old_nickname = user.nickname
|
||||||
user.set_nickname(new_nickname)
|
user.set_nickname(new_nickname)
|
||||||
event["server"].change_user_nickname(old_nickname, new_nickname)
|
event["server"].change_user_nickname(old_nickname, new_nickname)
|
||||||
|
@ -366,9 +356,7 @@ class LineHandler(object):
|
||||||
|
|
||||||
# something's mode has changed
|
# something's mode has changed
|
||||||
def mode(self, event):
|
def mode(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["prefix"])
|
|
||||||
user = event["server"].get_user(nickname)
|
|
||||||
target = event["args"][0]
|
target = event["args"][0]
|
||||||
is_channel = target[0] in event["server"].channel_types
|
is_channel = target[0] in event["server"].channel_types
|
||||||
if is_channel:
|
if is_channel:
|
||||||
|
@ -401,10 +389,8 @@ class LineHandler(object):
|
||||||
|
|
||||||
# someone (maybe me!) has been invited somewhere
|
# someone (maybe me!) has been invited somewhere
|
||||||
def invite(self, event):
|
def invite(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
|
||||||
event["prefix"])
|
|
||||||
target_channel = event["last"]
|
target_channel = event["last"]
|
||||||
user = event["server"].get_user(nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
target_user = event["server"].get_user(event["args"][0])
|
target_user = event["server"].get_user(event["args"][0])
|
||||||
self.events.on("received.invite").call(user=user,
|
self.events.on("received.invite").call(user=user,
|
||||||
target_channel=target_channel, server=event["server"],
|
target_channel=target_channel, server=event["server"],
|
||||||
|
@ -412,9 +398,7 @@ class LineHandler(object):
|
||||||
|
|
||||||
# we've received a message
|
# we've received a message
|
||||||
def privmsg(self, event):
|
def privmsg(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["prefix"])
|
|
||||||
user = event["server"].get_user(nickname)
|
|
||||||
message = event["arbitrary"] or ""
|
message = event["arbitrary"] or ""
|
||||||
message_split = message.split(" ")
|
message_split = message.split(" ")
|
||||||
target = event["args"][0]
|
target = event["args"][0]
|
||||||
|
@ -443,18 +427,16 @@ class LineHandler(object):
|
||||||
message = event["arbitrary"] or ""
|
message = event["arbitrary"] or ""
|
||||||
message_split = message.split(" ")
|
message_split = message.split(" ")
|
||||||
target = event["args"][0]
|
target = event["args"][0]
|
||||||
sender = Utils.remove_colon(event["prefix"] or "")
|
|
||||||
|
|
||||||
if sender == event["server"].name or target == "*" or not event[
|
if not event["prefix"] or event["prefix"].hostmask == event["server"
|
||||||
"prefix"]:
|
].name or target == "*":
|
||||||
event["server"].name = Utils.remove_colon(event["prefix"])
|
event["server"].name = event["prefix"].hostmask
|
||||||
|
|
||||||
self.events.on("received.server-notice").call(
|
self.events.on("received.server-notice").call(
|
||||||
message=message, message_split=message_split,
|
message=message, message_split=message_split,
|
||||||
server=event["server"])
|
server=event["server"])
|
||||||
else:
|
else:
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(sender)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
user = event["server"].get_user(nickname)
|
|
||||||
|
|
||||||
if target[0] in event["server"].channel_types:
|
if target[0] in event["server"].channel_types:
|
||||||
channel = event["server"].get_channel(target)
|
channel = event["server"].get_channel(target)
|
||||||
|
@ -469,9 +451,7 @@ class LineHandler(object):
|
||||||
|
|
||||||
# IRCv3 TAGMSG, used to send tags without any other information
|
# IRCv3 TAGMSG, used to send tags without any other information
|
||||||
def tagmsg(self, event):
|
def tagmsg(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["prefix"])
|
|
||||||
user = event["channel"].get_user(nickname)
|
|
||||||
target = event["args"][0]
|
target = event["args"][0]
|
||||||
|
|
||||||
if target[0] in event["server"].channel_types:
|
if target[0] in event["server"].channel_types:
|
||||||
|
@ -484,9 +464,7 @@ class LineHandler(object):
|
||||||
|
|
||||||
# IRCv3 AWAY, used to notify us that a client we can see has changed /away
|
# IRCv3 AWAY, used to notify us that a client we can see has changed /away
|
||||||
def away(self, event):
|
def away(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["prefix"])
|
|
||||||
user = event["server"].get_user(nickname)
|
|
||||||
message = event["arbitrary"]
|
message = event["arbitrary"]
|
||||||
if message:
|
if message:
|
||||||
user.away = True
|
user.away = True
|
||||||
|
@ -510,12 +488,10 @@ class LineHandler(object):
|
||||||
|
|
||||||
# IRCv3 CHGHOST, a user's username and/or hostname has changed
|
# IRCv3 CHGHOST, a user's username and/or hostname has changed
|
||||||
def chghost(self, event):
|
def chghost(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
|
||||||
event["prefix"])
|
|
||||||
username = event["args"][0]
|
username = event["args"][0]
|
||||||
hostname = event["args"][1]
|
hostname = event["args"][1]
|
||||||
|
|
||||||
if not event["server"].is_own_nickname(nickname):
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
||||||
target = event["server"].get_user("nickanme")
|
target = event["server"].get_user("nickanme")
|
||||||
else:
|
else:
|
||||||
target = event["server"]
|
target = event["server"]
|
||||||
|
@ -523,9 +499,7 @@ class LineHandler(object):
|
||||||
target.hostname = hostname
|
target.hostname = hostname
|
||||||
|
|
||||||
def account(self, event):
|
def account(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["prefix"])
|
|
||||||
user = event["server"].get_user("nickname")
|
|
||||||
|
|
||||||
if not event["args"][0] == "*":
|
if not event["args"][0] == "*":
|
||||||
user.identified_account = event["args"][0]
|
user.identified_account = event["args"][0]
|
||||||
|
@ -588,9 +562,7 @@ class LineHandler(object):
|
||||||
|
|
||||||
# someone's been kicked from a channel
|
# someone's been kicked from a channel
|
||||||
def kick(self, event):
|
def kick(self, event):
|
||||||
nickname, username, hostname = Utils.seperate_hostmask(
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
event["prefix"])
|
|
||||||
user = event["server"].get_user(nickname)
|
|
||||||
target = event["args"][1]
|
target = event["args"][1]
|
||||||
channel = event["server"].get_channel(event["args"][0])
|
channel = event["server"].get_channel(event["args"][0])
|
||||||
reason = event["arbitrary"] or ""
|
reason = event["arbitrary"] or ""
|
||||||
|
|
8
Utils.py
8
Utils.py
|
@ -13,6 +13,12 @@ def remove_colon(s):
|
||||||
def arbitrary(s, n):
|
def arbitrary(s, n):
|
||||||
return remove_colon(" ".join(s[n:]))
|
return remove_colon(" ".join(s[n:]))
|
||||||
|
|
||||||
|
class IRCHostmask(object):
|
||||||
|
def __init__(self, nickname, username, hostname, hostmask):
|
||||||
|
self.nickname = nickname
|
||||||
|
self.username = username
|
||||||
|
self.hostname = hostname
|
||||||
|
self.hostmask = hostmask
|
||||||
def seperate_hostmask(hostmask):
|
def seperate_hostmask(hostmask):
|
||||||
hostmask = remove_colon(hostmask)
|
hostmask = remove_colon(hostmask)
|
||||||
first_delim = hostmask.find("!")
|
first_delim = hostmask.find("!")
|
||||||
|
@ -21,7 +27,7 @@ def seperate_hostmask(hostmask):
|
||||||
if first_delim > -1 and second_delim > first_delim:
|
if first_delim > -1 and second_delim > first_delim:
|
||||||
nickname, username = hostmask.split("!", 1)
|
nickname, username = hostmask.split("!", 1)
|
||||||
username, hostname = username.split("@", 1)
|
username, hostname = username.split("@", 1)
|
||||||
return nickname, username, hostname
|
return IRCHostmask(nickname, username, hostname, hostmask)
|
||||||
|
|
||||||
def get_url(url, **kwargs):
|
def get_url(url, **kwargs):
|
||||||
if not urllib.parse.urlparse(url).scheme:
|
if not urllib.parse.urlparse(url).scheme:
|
||||||
|
|
Loading…
Reference in a new issue