diff src/plugins/plugin_xep_0060.py @ 1219:16484ebb695b

plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
author souliane <souliane@mailoo.org>
date Mon, 22 Sep 2014 22:25:44 +0200
parents 318eab3f93f8
children f584fbda4773
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0060.py	Mon Sep 22 20:50:20 2014 +0200
+++ b/src/plugins/plugin_xep_0060.py	Mon Sep 22 22:25:44 2014 +0200
@@ -23,6 +23,7 @@
 log = getLogger(__name__)
 from sat.memory.memory import Sessions
 
+from wokkel.compat import IQ
 from wokkel import disco, pubsub
 from wokkel.pubsub import PubSubRequest, NS_PUBSUB
 from zope.interface import implements
@@ -35,6 +36,7 @@
     "type": "XEP",
     "protocols": ["XEP-0060"],
     "dependencies": [],
+    "recommendations": ["XEP-0059"],
     "main": "XEP_0060",
     "handler": "yes",
     "description": _("""Implementation of PubSub Protocol""")
@@ -232,7 +234,7 @@
 
     # FIXME: we have to temporary override this method here just
     # to set the attributes itemIdentifiers which is not used
-    # in pubsub.PubSubClient.items
+    # in pubsub.PubSubClient.items + use the XEP-0059
     def items(self, service, nodeIdentifier, maxItems=None, itemIdentifiers=None,
               subscriptionIdentifier=None, sender=None):
         """
@@ -255,7 +257,8 @@
             the results to the specific subscription.
         @type subscriptionIdentifier: C{unicode}
         """
-        request = PubSubRequest('items')
+        # TODO: add method attributes for RSM: before, after, index
+        request = PubSubRequest('items', self.host, {'limit': maxItems} if maxItems else {})
         request.recipient = service
         request.nodeIdentifier = nodeIdentifier
         if maxItems:
@@ -269,6 +272,7 @@
             for element in iq.pubsub.items.elements():
                 if element.uri == NS_PUBSUB and element.name == 'item':
                     items.append(element)
+            # TODO: return (items, self.host.plugins['XEP-0059'].extractMetadata(iq)) ??
             return items
 
         d = request.send(self.xmlstream)
@@ -331,3 +335,64 @@
 
     def getDiscoItems(self, requestor, service, nodeIdentifier=''):
         return self.host.getDiscoItems(service, nodeIdentifier, self.parent.profile)
+
+
+class PubSubRequest(pubsub.PubSubRequest):
+
+    def __init__(self, verb=None, host=None, page_attrs=None):
+        """
+        @param verb (str): the type of pubsub request
+        @param host (SAT): the SAT instance
+        @param page_attrs (dict): options for RSM paging:
+            - limit (int): the maximum number of items in the page
+            - index (int): the starting index of the requested page
+            - after (str, int): the element immediately preceding the page
+            - before (str, int): the element immediately following the page
+        """
+        self.verb = verb
+        self.host = host
+        self.page_attrs = page_attrs
+
+    # FIXME: the redefinition of this wokkel method is the easiest way I found
+    # to handle RSM. We should find a proper solution, maybe just add in wokkel an
+    # empty method postProcessMessage, call it before sending and overwrite it here
+    # instead of overwriting the whole send method.
+    def send(self, xs):
+        """
+        Send this request to its recipient.
+
+        This renders all of the relevant parameters for this specific
+        requests into an L{IQ}, and invoke its C{send} method.
+        This returns a deferred that fires upon reception of a response. See
+        L{IQ} for details.
+
+        @param xs: The XML stream to send the request on.
+        @type xs: L{twisted.words.protocols.jabber.xmlstream.XmlStream}
+        @rtype: L{defer.Deferred}.
+        """
+
+        try:
+            (self.stanzaType,
+             childURI,
+             childName) = self._verbRequestMap[self.verb]
+        except KeyError:
+            raise NotImplementedError()
+
+        iq = IQ(xs, self.stanzaType)
+        iq.addElement((childURI, 'pubsub'))
+        verbElement = iq.pubsub.addElement(childName)
+
+        if self.sender:
+            iq['from'] = self.sender.full()
+        if self.recipient:
+            iq['to'] = self.recipient.full()
+
+        for parameter in self._parameters[self.verb]:
+            getattr(self, '_render_%s' % parameter)(verbElement)
+
+        # This lines have been added for RSM
+        if self.host and 'XEP-0059' in self.host.plugins and self.page_attrs:
+            self.page_attrs['stanza'] = iq
+            self.host.plugins['XEP-0059'].requestPage(**self.page_attrs)
+
+        return iq.send()