2019-05-30 14:34:39 +00:00
|
|
|
import uuid
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
CAP = utils.irc.Capability(None, "draft/labeled-response-0.2")
|
|
|
|
TAG = utils.irc.MessageTag(None, "draft/label")
|
2019-06-04 16:51:55 +00:00
|
|
|
BATCH = utils.irc.BatchType(None, "draft/labeled-response")
|
2019-05-30 14:34:39 +00:00
|
|
|
|
|
|
|
CAP_TO_TAG = {
|
|
|
|
"draft/labeled-response-0.2": "draft/label"
|
|
|
|
}
|
|
|
|
|
2019-06-05 11:11:14 +00:00
|
|
|
class WaitingForLabel(object):
|
|
|
|
def __init__(self, line, events):
|
|
|
|
self.line = line
|
|
|
|
self.events = events
|
|
|
|
self.labels_since = 0
|
|
|
|
|
2019-06-20 15:52:23 +00:00
|
|
|
@utils.export("cap", CAP)
|
2019-05-30 14:34:39 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
@utils.hook("new.server")
|
|
|
|
def new_server(self, event):
|
|
|
|
event["server"]._label_cache = {}
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2019-06-05 11:11:14 +00:00
|
|
|
event["server"]._label_cache[label] = WaitingForLabel(event["line"],
|
|
|
|
event["events"])
|
2019-05-30 14:34:39 +00:00
|
|
|
|
|
|
|
@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:
|
2019-06-04 16:53:40 +00:00
|
|
|
self._recv(event["server"], label, [event["line"]])
|
2019-05-30 14:34:39 +00:00
|
|
|
|
|
|
|
@utils.hook("received.batch.end")
|
|
|
|
def batch_end(self, event):
|
2019-06-04 16:53:40 +00:00
|
|
|
if BATCH.match(event["batch"].type):
|
|
|
|
label = TAG.get_value(event["batch"].tags)
|
|
|
|
self._recv(event["server"], label, event["batch"].get_lines())
|
2019-05-30 14:34:39 +00:00
|
|
|
|
2019-06-04 16:53:40 +00:00
|
|
|
def _recv(self, server, label, lines):
|
2019-06-12 14:36:59 +00:00
|
|
|
if not label in server._label_cache:
|
|
|
|
self.log.warn("unknown label received: %s", [label])
|
|
|
|
return
|
|
|
|
|
2019-06-05 11:11:14 +00:00
|
|
|
cached = server._label_cache.pop(label)
|
|
|
|
cached.events.on("labeled-response").call(lines=lines)
|
|
|
|
|
|
|
|
for label, other_cached in server._label_cache.items():
|
|
|
|
other_cached.labels_since += 1
|
|
|
|
if other_cached.labels_since == 10:
|
|
|
|
self.log.warn("%d labels seen while waiting for response to %s",
|
|
|
|
[other_cached.labels_since, label])
|