labels: Use BatchType to match, pass lines not line, batch ID isn't a label,

fire event on label response
This commit is contained in:
jesopo 2019-06-04 17:53:40 +01:00
parent 2470c1ec03
commit 2d46fe0cbf
2 changed files with 16 additions and 9 deletions

View file

@ -31,20 +31,22 @@ class Module(ModuleManager.BaseModule):
label = str(uuid.uuid4())
event["line"].tags[tag_key] = label
event["server"]._label_cache[label] = event["line"]
event["server"]._label_cache[label] = [event["line"],
event["events"]]
@utils.hook("raw.received")
def raw_recv(self, event):
if not event["line"].command == "BATCH":
label = TAG.get_value(event["line"].tags)
if not label == None:
self._recv(event["server"], label, event["line"])
self._recv(event["server"], label, [event["line"]])
@utils.hook("received.batch.end")
def batch_end(self, event):
if TAG.match(event["batch"].type):
self._recv(event["server"], event["batch"].identifier, None)
if BATCH.match(event["batch"].type):
label = TAG.get_value(event["batch"].tags)
self._recv(event["server"], label, event["batch"].get_lines())
def _recv(self, server, label, line):
cached_line = server._label_cache.pop(label)
# do something with the line!
def _recv(self, server, label, lines):
cached_line, cached_events = server._label_cache.pop(label)
cached_events.on("labeled-response").call(lines=lines)

View file

@ -275,11 +275,16 @@ class Capability(object):
self._caps = set([name, draft_name])
self._on_ack_callbacks = [
] # type: typing.List[typing.Callable[[], None]]
def available(self, capabilities: typing.Iterable[str]
) -> typing.Optional[str]:
match = list(set(capabilities)&self._caps)
return match[0] if match else None
def match(self, capability: str) -> typing.Optional[str]:
cap = list(set([capability])&self._caps)
return cap[0] if cap else None
def copy(self):
return Capability(*self._caps)
@ -297,8 +302,8 @@ class MessageTag(object):
def get_value(self, tags: typing.Dict[str, str]) -> typing.Optional[str]:
key = list(set(tags.keys())&self._names)
return tags[key[0]] if key else None
def match(self, s: str) -> typing.Optional[str]:
key = list(set([s])&self._names)
def match(self, tag: str) -> typing.Optional[str]:
key = list(set([tag])&self._names)
return key[0] if key else None
class BatchType(object):