Change raw.send and raw.receive commands to just pass around ParsedLines

This commit is contained in:
jesopo 2019-06-22 12:17:45 +01:00
parent 8671cb3708
commit 7174e83f14
7 changed files with 127 additions and 129 deletions

View file

@ -6,8 +6,8 @@ class Module(ModuleManager.BaseModule):
def send_message(self, event):
our_hostmask = utils.irc.seperate_hostmask(event["server"].hostmask())
echo = IRCLine.ParsedLine(event["command"], event["args"],
source=our_hostmask, tags=event["tags"])
echo = IRCLine.ParsedLine(event["line"].command, event["line"].args,
source=our_hostmask, tags=event["line"].tags)
echo.id = event["line"].id
self.events.on("raw.received").call(line=echo, server=event["server"])

View file

@ -8,14 +8,12 @@ class Module(ModuleManager.BaseModule):
default_events = []
for hook in hooks:
default_events.append(hook.kwargs.get("default_event", False))
default_event = any(default_events)
kwargs = {"command": line.command, "args": line.args, "tags": line.tags,
"server": server, "source": line.source, "line": line,
kwargs = {"server": server, "line": line,
"direction": utils.Direction.Recv}
self.events.on("raw.received").on(line.command).call_unsafe(**kwargs)
if default_event or not hooks:
if any(default_events) or not hooks:
self.events.on("received").on(line.command).call(**kwargs)
@utils.hook("raw.received")
@ -30,9 +28,8 @@ class Module(ModuleManager.BaseModule):
@utils.hook("raw.send")
def handle_send(self, event):
self.events.on("raw.send").on(event["line"].command).call_unsafe(
command=event["line"].command, args=event["line"].args,
tags=event["line"].tags, server=event["server"],
direction=utils.Direction.Send, line=event["line"])
server=event["server"], direction=utils.Direction.Send,
line=event["line"])
# ping from the server
@utils.hook("raw.received.ping")
@ -42,16 +39,16 @@ class Module(ModuleManager.BaseModule):
@utils.hook("raw.received.error")
def error(self, event):
self.log.error("ERROR received from %s: %s",
[str(event["server"]), event["args"][0]])
[str(event["server"]), event["line"].args[0]])
@utils.hook("raw.received.fail")
def fail(self, event):
command = event["args"][0]
error_code = event["args"][1]
context = event["args"][2:-1]
description = event["args"][-1]
command = event["line"].args[0]
error_code = event["line"].args[1]
context = event["line"].args[2:-1]
description = event["line"].args[-1]
self.log.warn("FAIL (%s %s) received: %s",
[command, error_code, description])
self.log.warn("FAIL (%s %s) received on %s: %s",
[command, error_code, str(event["server"]), description])
self.events.on("received.fail").on(command).call(error_code=error_code,
context=context, description=description, server=event["server"])
@ -122,7 +119,8 @@ class Module(ModuleManager.BaseModule):
# unknown command sent by us, oops!
@utils.hook("raw.received.421", default_event=True)
def handle_421(self, event):
self.bot.log.warn("We sent an unknown command: %s", [event["args"][1]])
self.bot.log.warn("We sent an unknown command to %s: %s",
[str(event["server"]), event["line"].args[1]])
# a user has disconnected!
@utils.hook("raw.received.quit")
@ -173,15 +171,15 @@ class Module(ModuleManager.BaseModule):
@utils.hook("raw.received.batch")
def batch(self, event):
identifier = event["args"][0]
identifier = event["line"].args[0]
modifier, identifier = identifier[0], identifier[1:]
if modifier == "+":
batch_type = event["args"][1]
args = event["args"][2:]
batch_type = event["line"].args[1]
args = event["line"].args[2:]
batch = utils.irc.IRCBatch(identifier, batch_type, args,
event["tags"])
event["line"].tags)
event["server"].batches[identifier] = batch
self.events.on("received.batch.start").call(batch=batch,
@ -227,7 +225,6 @@ class Module(ModuleManager.BaseModule):
def handle_354(self, event):
core.handle_354(event)
# response to an empty mode command
@utils.hook("raw.received.324", default_event=True)
def handle_324(self, event):
@ -246,7 +243,7 @@ class Module(ModuleManager.BaseModule):
# we need a registered nickname for this channel
@utils.hook("raw.received.477", default_event=True)
def handle_477(self, event):
channel_name = event["server"].irc_lower(event["args"][1])
channel_name = event["server"].irc_lower(event["line"].args[1])
if channel_name in event["server"].channels:
key = event["server"].attempted_join[channel_name]
self.timers.add("rejoin", 5, channel_name=channe_name, key=key,

View file

@ -1,25 +1,25 @@
from src import utils
def handle_332(events, event):
channel = event["server"].channels.get(event["args"][1])
topic = event["args"].get(2)
channel = event["server"].channels.get(event["line"].args[1])
topic = event["line"].args.get(2)
channel.set_topic(topic)
events.on("received.332").call(channel=channel, server=event["server"],
topic=topic)
def topic(events, event):
user = event["server"].get_user(event["source"].nickname)
channel = event["server"].channels.get(event["args"][0])
topic = event["args"].get(1)
user = event["server"].get_user(event["line"].source.nickname)
channel = event["server"].channels.get(event["line"].args[0])
topic = event["line"].args.get(1)
channel.set_topic(topic)
events.on("received.topic").call(channel=channel, server=event["server"],
topic=topic, user=user)
def handle_333(events, event):
channel = event["server"].channels.get(event["args"][1])
channel = event["server"].channels.get(event["line"].args[1])
topic_setter = utils.irc.seperate_hostmask(event["args"][2])
topic_time = int(event["args"][3]) if event["args"][3].isdigit() else None
topic_setter = utils.irc.seperate_hostmask(event["line"].args[2])
topic_time = int(event["line"].args[3])
channel.set_topic_setter(topic_setter.nickname, topic_setter.username,
topic_setter.hostname)
@ -28,8 +28,8 @@ def handle_333(events, event):
setter=topic_setter.nickname, set_at=topic_time, server=event["server"])
def handle_353(event):
channel = event["server"].channels.get(event["args"][2])
nicknames = event["args"].get(3).split(" ")
channel = event["server"].channels.get(event["line"].args[2])
nicknames = event["line"].args.get(3).split(" ")
# there can sometimes be a dangling space at the end of a 353
if nicknames and not nicknames[-1]:
@ -57,29 +57,29 @@ def handle_353(event):
channel.add_mode(mode, nickname)
def handle_366(event):
event["server"].send_whox(event["args"][1], "n", "ahnrtu", "111")
event["server"].send_whox(event["line"].args[1], "n", "ahnrtu", "111")
def join(events, event):
account = None
realname = None
channel_name = event["args"][0]
channel_name = event["line"].args[0]
if len(event["args"]) == 3:
if not event["args"][1] == "*":
account = event["args"][1]
realname = event["args"][2]
if len(event["line"].args) == 3:
if not event["line"].args[1] == "*":
account = event["line"].args[1]
realname = event["line"].args[2]
user = event["server"].get_user(event["source"].nickname)
user = event["server"].get_user(event["line"].source.nickname)
user.username = event["source"].username
user.hostname = event["source"].hostname
user.username = event["line"].source.username
user.hostname = event["line"].source.hostname
if account:
user.identified_account = account
user.identified_account_id = event["server"].get_user(account).get_id()
if realname:
user.realname = realname
is_self = event["server"].is_own_nickname(event["source"].nickname)
is_self = event["server"].is_own_nickname(event["line"].source.nickname)
if is_self:
channel = event["server"].channels.add(channel_name)
else:
@ -100,16 +100,16 @@ def join(events, event):
server=event["server"], account=account, realname=realname)
def part(events, event):
channel = event["server"].channels.get(event["args"][0])
user = event["server"].get_user(event["source"].nickname)
reason = event["args"].get(1)
channel = event["server"].channels.get(event["line"].args[0])
user = event["server"].get_user(event["line"].source.nickname)
reason = event["line"].args.get(1)
channel.remove_user(user)
user.part_channel(channel)
if not len(user.channels):
event["server"].remove_user(user)
if not event["server"].is_own_nickname(event["source"].nickname):
if not event["server"].is_own_nickname(event["line"].source.nickname):
events.on("received.part").call(channel=channel, reason=reason,
user=user, server=event["server"])
else:
@ -118,28 +118,28 @@ def part(events, event):
server=event["server"])
def handle_324(event):
if event["args"][1] in event["server"].channels:
channel = event["server"].channels.get(event["args"][1])
modes = event["args"][2]
args = event["args"][3:]
if event["line"].args[1] in event["server"].channels:
channel = event["server"].channels.get(event["line"].args[1])
modes = event["line"].args[2]
args = event["line"].args[3:]
channel.parse_modes(modes, args[:])
def handle_329(event):
channel = event["server"].channels.get(event["args"][1])
channel.creation_timestamp = int(event["args"][2])
channel = event["server"].channels.get(event["line"].args[1])
channel.creation_timestamp = int(event["line"].args[2])
def handle_477(timers, event):
channel_name = event["server"].irc_lower(event["args"][1])
channel_name = event["server"].irc_lower(event["line"].args[1])
if channel_name in event["server"].channels:
key = event["server"].attempted_join[channel_name]
timers.add("rejoin", 5, channel_name=channe_name, key=key,
server_id=event["server"].id)
def kick(events, event):
user = event["server"].get_user(event["source"].nickname)
target = event["args"][1]
channel = event["server"].channels.get(event["args"][0])
reason = event["args"].get(2)
user = event["server"].get_user(event["line"].source.nickname)
target = event["line"].args[1]
channel = event["server"].channels.get(event["line"].args[0])
reason = event["line"].args.get(2)
target_user = event["server"].get_user(target)
if not event["server"].is_own_nickname(target):
@ -156,10 +156,11 @@ def kick(events, event):
event["server"].remove_user(target_user)
def rename(events, event):
old_name = event["args"][0]
new_name = event["args"][1]
old_name = event["line"].args[0]
new_name = event["args"].args[1]
channel = event["server"].channels.get(old_name)
event["server"].channels.rename(old_name, new_name)
events.on("received.rename").call(channel=channel, old_name=old_name,
new_name=new_name, reason=event["args"].get(2), server=event["server"])
new_name=new_name, reason=event["line"].args.get(2),
server=event["server"])

View file

@ -4,18 +4,18 @@ 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])
event["server"].send_pong(event["line"].args[0])
def handle_001(event):
event["server"].socket.enable_write_throttle()
event["server"].name = event["source"].hostmask
event["server"].set_own_nickname(event["args"][0])
event["server"].name = event["line"].source.hostmask
event["server"].set_own_nickname(event["line"].args[0])
event["server"].send_whois(event["server"].nickname)
event["server"].send_mode(event["server"].nickname)
event["server"].connected = True
def handle_005(events, event):
isupport_list = event["args"][1:-1]
isupport_list = event["line"].args[1:-1]
isupport = {}
for i, item in enumerate(isupport_list):
@ -60,12 +60,12 @@ def handle_005(events, event):
server=event["server"])
def handle_004(event):
event["server"].version = event["args"][2]
event["server"].version = event["line"].args[2]
def motd_start(event):
event["server"].motd_lines.clear()
def motd_line(event):
event["server"].motd_lines.append(event["args"][1])
event["server"].motd_lines.append(event["line"].args[1])
def _own_modes(server, modes):
mode_chunks = RE_MODES.findall(modes)
@ -75,39 +75,39 @@ def _own_modes(server, modes):
server.change_own_mode(remove, mode)
def mode(events, event):
user = event["server"].get_user(event["source"].nickname)
target = event["args"][0]
user = event["server"].get_user(event["line"].source.nickname)
target = event["line"].args[0]
is_channel = target[0] in event["server"].channel_types
if is_channel:
channel = event["server"].channels.get(target)
modes = event["args"][1]
args = event["args"][2:]
modes = event["line"].args[1]
args = event["line"].args[2:]
channel.parse_modes(modes, args[:])
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):
modes = event["args"][1]
modes = event["line"].args[1]
_own_modes(event["server"], modes)
events.on("self.mode").call(modes=modes, server=event["server"])
event["server"].send_who(event["server"].nickname)
def handle_221(event):
_own_modes(event["server"], event["args"][1])
_own_modes(event["server"], event["line"].args[1])
def invite(events, event):
target_channel = event["args"][1]
user = event["server"].get_user(event["source"].nickname)
target_user = event["server"].get_user(event["args"][0])
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])
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]
username = event["args"][2]
hostname = event["args"][3]
nickname = event["line"].args[5]
username = event["line"].args[2]
hostname = event["line"].args[3]
if event["server"].is_own_nickname(nickname):
event["server"].username = username
@ -118,12 +118,12 @@ def handle_352(event):
target.hostname = hostname
def handle_354(event):
if event["args"][1] == "111":
nickname = event["args"][4]
username = event["args"][2]
hostname = event["args"][3]
realname = event["args"][6]
account = event["args"][5]
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]
if event["server"].is_own_nickname(nickname):
event["server"].username = username

View file

@ -72,9 +72,9 @@ def _cap_match(server, caps):
return matched_caps
def cap(exports, events, event):
capabilities = utils.parse.keyvalue(event["args"][-1])
subcommand = event["args"][1].upper()
is_multiline = len(event["args"]) > 3 and event["args"][2] == "*"
capabilities = utils.parse.keyvalue(event["line"].args[-1])
subcommand = event["line"].args[1].upper()
is_multiline = len(event["line"].args) > 3 and event["line"].args[2] == "*"
if subcommand == "DEL":
for capability in capabilities.keys():
@ -127,5 +127,5 @@ def cap(exports, events, event):
event["server"].send_capability_end()
def authenticate(events, event):
events.on("received.authenticate").call(message=event["args"][0],
events.on("received.authenticate").call(message=event["line"].args[0],
server=event["server"])

View file

@ -7,25 +7,25 @@ def _from_self(server, source):
return False
def message(events, event):
from_self = _from_self(event["server"], event.get("source", None))
from_self = _from_self(event["server"], event["line"].source)
if from_self == None:
return
direction = "send" if from_self else "received"
target_str = event["args"][0]
target_str = event["line"].args[0]
message = None
if len(event["args"]) > 1:
message = event["args"][1]
if len(event["line"].args) > 1:
message = event["line"].args[1]
if not from_self and (
not event["source"] or
not event["line"].source or
not event["server"].name or
event["source"].hostmask == event["server"].name or
event["line"].source.hostmask == event["server"].name or
target_str == "*"):
if event["source"]:
event["server"].name = event["source"].hostmask
if event["line"].source:
event["server"].name = event["line"].source.hostmask
events.on("received.server-notice").call(message=message,
message_split=message.split(" "), server=event["server"])
@ -34,7 +34,7 @@ def message(events, event):
if from_self:
user = event["server"].get_user(event["server"].nickname)
else:
user = event["server"].get_user(event["source"].nickname)
user = event["server"].get_user(event["line"].source.nickname)
# strip prefix_symbols from the start of target, for when people use
# e.g. 'PRIVMSG +#channel :hi' which would send a message to only
@ -52,7 +52,7 @@ def message(events, event):
target_obj = event["server"].get_user(target)
kwargs = {"server": event["server"], "target": target_obj,
"target_str": target_str, "user": user, "tags": event["tags"],
"target_str": target_str, "user": user, "tags": event["line"].tags,
"is_channel": is_channel, "from_self": from_self, "line": event["line"]}
action = False
@ -61,9 +61,9 @@ def message(events, event):
ctcp_message = utils.irc.parse_ctcp(message)
if ctcp_message:
if not ctcp_message.command == "ACTION" or not event["command"
] == "PRIVMSG":
if event["command"] == "PRIVMSG":
if (not ctcp_message.command == "ACTION" or not
event["line"].command == "PRIVMSG"):
if event["line"].command == "PRIVMSG":
ctcp_action = "request"
else:
ctcp_action = "response"
@ -82,8 +82,8 @@ def message(events, event):
kwargs["message_split"] = message.split(" ")
kwargs["action"] = action
event_type = event["command"].lower()
if event["command"] == "PRIVMSG":
event_type = event["line"].command.lower()
if event_type == "privmsg":
event_type = "message"
context = "channel" if is_channel else "private"
@ -93,7 +93,7 @@ def message(events, event):
hook.call(channel=target_obj, **kwargs)
if message:
target_obj.buffer.add_message(user.nickname, message, action,
event["tags"], from_self)
event["line"].tags, from_self)
else:
hook.call(**kwargs)
@ -103,4 +103,4 @@ def message(events, event):
if message:
buffer_obj.buffer.add_message(user.nickname, message, action,
event["tags"], from_self)
event["line"].tags, from_self)

View file

@ -1,10 +1,10 @@
from src import utils
def handle_311(event):
nickname = event["args"][1]
username = event["args"][2]
hostname = event["args"][3]
realname = event["args"][4]
nickname = event["line"].args[1]
username = event["line"].args[2]
hostname = event["line"].args[3]
realname = event["line"].args[4]
if event["server"].is_own_nickname(nickname):
event["server"].username = username
@ -19,13 +19,13 @@ def handle_311(event):
def quit(events, event):
nickname = None
if event["direction"] == utils.Direction.Recv:
nickname = event["source"].nickname
reason = event["args"].get(0)
nickname = event["line"].source.nickname
reason = event["line"].args.get(0)
if event["direction"] == utils.Direction.Recv:
nickname = event["source"].nickname
nickname = event["line"].source.nickname
if (not event["server"].is_own_nickname(nickname) and
not event["source"].hostmask == "*"):
not event["line"].source.hostmask == "*"):
user = event["server"].get_user(nickname)
events.on("received.quit").call(reason=reason, user=user,
server=event["server"])
@ -36,11 +36,11 @@ def quit(events, event):
events.on("send.quit").call(reason=reason, server=event["server"])
def nick(events, event):
new_nickname = event["args"].get(0)
user = event["server"].get_user(event["source"].nickname)
new_nickname = event["line"].args.get(0)
user = event["server"].get_user(event["line"].source.nickname)
old_nickname = user.nickname
if not event["server"].is_own_nickname(event["source"].nickname):
if not event["server"].is_own_nickname(event["line"].source.nickname):
events.on("received.nick").call(new_nickname=new_nickname,
old_nickname=old_nickname, user=user, server=event["server"])
else:
@ -52,8 +52,8 @@ def nick(events, event):
event["server"].change_user_nickname(old_nickname, new_nickname)
def away(events, event):
user = event["server"].get_user(event["source"].nickname)
message = event["args"].get(0)
user = event["server"].get_user(event["line"].source.nickname)
message = event["line"].args.get(0)
if message:
user.away = True
user.away_message = message
@ -65,9 +65,9 @@ def away(events, event):
events.on("received.away.off").call(user=user, server=event["server"])
def chghost(event):
nickname = event["source"].nickname
username = event["args"][0]
hostname = event["args"][1]
nickname = event["line"].source.nickname
username = event["line"].args[0]
hostname = event["line"].args[1]
if event["server"].is_own_nickname(nickname):
event["server"].username = username
@ -78,8 +78,8 @@ def chghost(event):
target.hostname = hostname
def setname(event):
nickname = event["source"].nickname
realname = event["args"][0]
nickname = event["line"].source.nickname
realname = event["line"].args[0]
user = event["server"].get_user(nickname)
user.realname = realname
@ -88,14 +88,14 @@ def setname(event):
event["server"].realname = realname
def account(events, event):
user = event["server"].get_user(event["source"].nickname)
user = event["server"].get_user(event["line"].source.nickname)
if not event["args"][0] == "*":
user.identified_account = event["args"][0]
if not event["line"].args[0] == "*":
user.identified_account = event["line"].args[0]
user.identified_account_id = event["server"].get_user(
event["args"][0]).get_id()
event["line"].args[0]).get_id()
events.on("received.account.login").call(user=user,
server=event["server"], account=event["args"][0])
server=event["server"], account=event["line"].args[0])
else:
user.identified_account = None
user.identified_account_id = None