comparison sat/plugins/plugin_xep_0060.py @ 2702:6555e9835ff8

plugin XEP-0060: use new parseExtra methods from MAM and RSM
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 10:34:42 +0100
parents 1bf7e89fded0
children bb6adaa580ee
comparison
equal deleted inserted replaced
2701:2ea2369ae7de 2702:6555e9835ff8
30 from wokkel import disco 30 from wokkel import disco
31 from wokkel import data_form 31 from wokkel import data_form
32 from zope.interface import implements 32 from zope.interface import implements
33 from collections import namedtuple 33 from collections import namedtuple
34 import urllib 34 import urllib
35 import datetime
36 from dateutil import tz
37 35
38 # XXX: sat_tmp.wokkel.pubsub is actually use instead of wokkel version 36 # XXX: sat_tmp.wokkel.pubsub is actually use instead of wokkel version
39 # mam and rsm come from sat_tmp.wokkel too 37 # mam and rsm come from sat_tmp.wokkel too
40 from wokkel import pubsub 38 from wokkel import pubsub
41 from wokkel import rsm 39 from wokkel import rsm
42 from wokkel import mam 40 from wokkel import mam
43 41
44 42
45 PLUGIN_INFO = { 43 PLUGIN_INFO = {
46 C.PI_NAME: "Publish-Subscribe", 44 C.PI_NAME: u"Publish-Subscribe",
47 C.PI_IMPORT_NAME: "XEP-0060", 45 C.PI_IMPORT_NAME: u"XEP-0060",
48 C.PI_TYPE: "XEP", 46 C.PI_TYPE: u"XEP",
49 C.PI_PROTOCOLS: ["XEP-0060"], 47 C.PI_PROTOCOLS: [u"XEP-0060"],
50 C.PI_DEPENDENCIES: [], 48 C.PI_DEPENDENCIES: [],
51 C.PI_RECOMMENDATIONS: ["XEP-0313"], 49 C.PI_RECOMMENDATIONS: [u"XEP-0059", u"XEP-0313"],
52 C.PI_MAIN: "XEP_0060", 50 C.PI_MAIN: u"XEP_0060",
53 C.PI_HANDLER: "yes", 51 C.PI_HANDLER: u"yes",
54 C.PI_DESCRIPTION: _("""Implementation of PubSub Protocol"""), 52 C.PI_DESCRIPTION: _(u"""Implementation of PubSub Protocol"""),
55 } 53 }
56 54
57 UNSPECIFIED = "unspecified error" 55 UNSPECIFIED = "unspecified error"
58 MAM_FILTER = "mam_filter_"
59 56
60 57
61 Extra = namedtuple("Extra", ("rsm_request", "extra")) 58 Extra = namedtuple("Extra", ("rsm_request", "extra"))
62 # rsm_request is the rsm.RSMRequest build with rsm_ prefixed keys, or None 59 # rsm_request is the rsm.RSMRequest build with rsm_ prefixed keys, or None
63 # extra is a potentially empty dict 60 # extra is a potentially empty dict
83 ID_SINGLETON = "current" 80 ID_SINGLETON = "current"
84 81
85 def __init__(self, host): 82 def __init__(self, host):
86 log.info(_(u"PubSub plugin initialization")) 83 log.info(_(u"PubSub plugin initialization"))
87 self.host = host 84 self.host = host
85 self._rsm = host.plugins.get("XEP-0059")
88 self._mam = host.plugins.get("XEP-0313") 86 self._mam = host.plugins.get("XEP-0313")
89 self._node_cb = {} # dictionnary of callbacks for node (key: node, value: list of callbacks) 87 self._node_cb = {} # dictionnary of callbacks for node (key: node, value: list of callbacks)
90 self.rt_sessions = sat_defer.RTDeferredSessions() 88 self.rt_sessions = sat_defer.RTDeferredSessions()
91 host.bridge.addMethod( 89 host.bridge.addMethod(
92 "psNodeCreate", 90 "psNodeCreate",
322 if extra is None: 320 if extra is None:
323 rsm_request = None 321 rsm_request = None
324 extra = {} 322 extra = {}
325 else: 323 else:
326 # rsm 324 # rsm
327 rsm_args = {} 325 if self._rsm is None:
328 for arg in ("max", "after", "before", "index"): 326 rsm_request = None
329 try:
330 argname = "max_" if arg == "max" else arg
331 rsm_args[argname] = extra.pop("rsm_{}".format(arg))
332 except KeyError:
333 continue
334
335 if rsm_args:
336 rsm_request = rsm.RSMRequest(**rsm_args)
337 else: 327 else:
338 rsm_request = None 328 rsm_request = self._rsm.parseExtra(extra)
339 329
340 # mam 330 # mam
341 mam_args = {} 331 if self._mam is None:
342 for arg in ("start", "end"): 332 mam_request = None
343 try: 333 else:
344 mam_args[arg] = datetime.datetime.fromtimestamp( 334 mam_request = self._mam.parseExtra(extra, with_rsm=False)
345 int(extra.pop("{}{}".format(MAM_FILTER, arg))), tz.tzutc() 335
346 ) 336 if mam_request is not None:
347 except (TypeError, ValueError): 337 assert u"mam" not in extra
348 log.warning(u"Bad value for {} filter".format(arg)) 338 extra[u"mam"] = mam_request
349 except KeyError:
350 continue
351
352 try:
353 mam_args["with_jid"] = jid.JID(extra.pop("{}jid".format(MAM_FILTER)))
354 except (jid.InvalidFormat):
355 log.warning(u"Bad value for jid filter")
356 except KeyError:
357 pass
358
359 for name, value in extra.iteritems():
360 if name.startswith(MAM_FILTER):
361 var = name[len(MAM_FILTER) :]
362 extra_fields = mam_args.setdefault("extra_fields", [])
363 extra_fields.append(data_form.Field(var=var, value=value))
364
365 if mam_args:
366 assert "mam" not in extra
367 extra["mam"] = mam.MAMRequest(mam.buildForm(**mam_args))
368 return Extra(rsm_request, extra) 339 return Extra(rsm_request, extra)
369 340
370 def addManagedNode(self, node, **kwargs): 341 def addManagedNode(self, node, **kwargs):
371 """Add a handler for a node 342 """Add a handler for a node
372 343