2d46fe0cbf
fire event on label response
52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
import uuid
|
|
from src import ModuleManager, utils
|
|
|
|
CAP = utils.irc.Capability(None, "draft/labeled-response-0.2")
|
|
TAG = utils.irc.MessageTag(None, "draft/label")
|
|
BATCH = utils.irc.BatchType(None, "draft/labeled-response")
|
|
|
|
CAP_TO_TAG = {
|
|
"draft/labeled-response-0.2": "draft/label"
|
|
}
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
@utils.hook("new.server")
|
|
def new_server(self, event):
|
|
event["server"]._label_cache = {}
|
|
|
|
@utils.hook("received.cap.ls")
|
|
@utils.hook("received.cap.new")
|
|
def on_cap(self, event):
|
|
if CAP.available(event["capabilities"]):
|
|
return CAP.copy()
|
|
|
|
@utils.hook("preprocess.send")
|
|
def raw_send(self, event):
|
|
available_cap = event["server"].available_capability(CAP)
|
|
|
|
if available_cap:
|
|
label = TAG.get_value(event["line"].tags)
|
|
if label == None:
|
|
tag_key = CAP_TO_TAG[available_cap]
|
|
label = str(uuid.uuid4())
|
|
event["line"].tags[tag_key] = label
|
|
|
|
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"]])
|
|
|
|
@utils.hook("received.batch.end")
|
|
def batch_end(self, event):
|
|
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, lines):
|
|
cached_line, cached_events = server._label_cache.pop(label)
|
|
cached_events.on("labeled-response").call(lines=lines)
|