Move parsing IRC lines to src/Utils.py, added base support for parsing outgoing

messages
This commit is contained in:
jesopo 2018-10-03 11:31:51 +01:00
parent 92477e0fba
commit 2b349105aa
2 changed files with 58 additions and 35 deletions

View file

@ -14,50 +14,39 @@ CAPABILITIES = {"multi-prefix", "chghost", "invite-notify", "account-tag",
"batch", "draft/labeled-response"} "batch", "draft/labeled-response"}
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@Utils.hook("raw") def _handle(self, line):
def handle(self, event): hooks = self.events.on("raw").on(line.command).get_hooks()
line = original_line = event["line"]
tags = {}
prefix = None
command = None
if line[0] == "@":
tags_prefix, line = line[1:].split(" ", 1)
for tag in filter(None, tags_prefix.split(";")):
tag, _, value = tag.partition("=")
tags[tag] = value
if "batch" in tags and tags["batch"] in event["server"].batches:
server.batches[tag["batch"]].append(line)
return
line, _, arbitrary = line.partition(" :")
arbitrary = arbitrary or None
if line[0] == ":":
prefix, line = line[1:].split(" ", 1)
prefix = Utils.seperate_hostmask(prefix)
command, _, line = line.partition(" ")
args = line.split(" ")
last = arbitrary or args[-1]
hooks = self.events.on("raw").on(command).get_hooks()
default_events = [] default_events = []
for hook in hooks: for hook in hooks:
default_events.append(hook.kwargs.get("default_event", False)) default_events.append(hook.kwargs.get("default_event", False))
default_event = any(default_events) default_event = any(default_events)
kwargs = {"last": last, "args": args, "arbitrary": arbitrary, kwargs = {"args": line.args, "arbitrary": line.arbitrary,
"tags": tags, "last": last, "server": event["server"], "tags": line.tags, "last": line.last,
"prefix": prefix} "server": line.server, "prefix": line.prefix}
self.events.on("raw").on(command).call(**kwargs) self.events.on("raw").on(line.command).call(**kwargs)
if default_event or not hooks: if default_event or not hooks:
if command.isdigit(): if command.isdigit():
self.events.on("received.numeric").on(command).call(**kwargs) self.events.on("received.numeric").on(line.command).call(
**kwargs)
else: else:
self.events.on("received").on(command).call(**kwargs) self.events.on("received").on(line.command).call(**kwargs)
@Utils.hook("raw")
def handle_raw(self, event):
line = Utils.parse_line(event["server"], event["line"])
if "batch" in line.tags and line.tags["batch"] in event[
"server"].batches:
server.batches[tag["batch"]].append(line)
else:
self._handle(line)
@Utils.hook("preprocess.send")
def handle_send(self, event):
line = Utils.parse_line(event["server"], event["line"])
self.events.on("send").on(line.command).call(
args=line.args, arbitrary=line.arbitrary, tags=line.tags,
last=line.last, server=line.server)
# ping from the server # ping from the server
@Utils.hook("raw.ping") @Utils.hook("raw.ping")

View file

@ -342,3 +342,37 @@ def parse_docstring(s):
description += " " description += " "
description += line description += line
return Docstring(description, items) return Docstring(description, items)
class IRCLine(object):
def __init__(self, tags, prefix, command, args, arbitrary, last, server):
self.tags = tags
self.prefix = prefix
self.command = command
self.args = args
self.arbitrary = arbitrary
self.last = last
self.server = server
def parse_line(server, line):
tags = {}
prefix = None
command = None
if line[0] == "@":
tags_prefix, line = line[1:].split(" ", 1)
for tag in filter(None, tags_prefix.split(";")):
tag, _, value = tag.partition("=")
tags[tag] = value
line, _, arbitrary = line.partition(" :")
arbitrary = arbitrary or None
if line[0] == ":":
prefix, line = line[1:].split(" ", 1)
prefix = seperate_hostmask(prefix)
command, _, line = line.partition(" ")
args = line.split(" ")
last = arbitrary or args[-1]
return IRCLine(tags, prefix, command, args, arbitrary, last, server)