changeset 73:5d7a924ebddb

Reworked disco support. Actually return disco items.
author Ralph Meijer <ralphm@ik.nu>
date Sun, 07 Nov 2004 14:21:09 +0000
parents a914cad421c5
children 0e642e5b4fb2
files idavoll/idavoll.py idavoll/pubsub.py
diffstat 2 files changed, 76 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/idavoll/idavoll.py	Sun Nov 07 14:19:46 2004 +0000
+++ b/idavoll/idavoll.py	Sun Nov 07 14:21:09 2004 +0000
@@ -5,19 +5,17 @@
 import backend
 import pubsub
 import xmpp_error
+import disco
 
 import sys
 
-NS_DISCO = 'http://jabber.org/protocol/disco'
-NS_DISCO_INFO = NS_DISCO + '#info'
-NS_DISCO_ITEMS = NS_DISCO + '#items'
 NS_VERSION = 'jabber:iq:version'
 
 IQ_GET = '/iq[@type="get"]'
 IQ_SET = '/iq[@type="set"]'
 VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]'
-DISCO_INFO = IQ_GET + '/query[@xmlns="' + NS_DISCO_INFO + '"]'
-DISCO_ITEMS = IQ_GET + '/query[@xmlns="' + NS_DISCO_ITEMS + '"]'
+DISCO_INFO = IQ_GET + '/query[@xmlns="' + disco.NS_INFO + '"]'
+DISCO_ITEMS = IQ_GET + '/query[@xmlns="' + disco.NS_ITEMS + '"]'
 
 class IdavollService(component.Service):
 
@@ -29,13 +27,14 @@
         xmlstream.addObserver(IQ_GET, self.iqFallback, -1)
         xmlstream.addObserver(IQ_SET, self.iqFallback, -1)
 
-    def getFeatures(self, node):
-        features = []
+    def get_disco_info(self, node):
+        info = []
 
         if not node:
-            features.extend([NS_DISCO_ITEMS, NS_VERSION])
+            info.append(disco.Feature(disco.NS_ITEMS))
+            info.append(disco.Feature(NS_VERSION))
 
-        return defer.succeed(features)
+        return defer.succeed(info)
     
     def onVersion(self, iq):
         iq.swapAttributeValues("to", "from")
@@ -46,65 +45,63 @@
         iq.handled = True
 
     def onDiscoInfo(self, iq):
-        identities_deferreds = []
-        features_deferreds = []
+        dl = []
         node = iq.query.getAttribute("node")
 
         for c in self.parent:
             if components.implements(c, component.IService):
-                if hasattr(c, "getIdentities"):
-                    identities_deferreds.append(c.getIdentities(node))
-                if hasattr(c, "getFeatures"):
-                    features_deferreds.append(c.getFeatures(node))
-        print identities_deferreds
-        print features_deferreds
-        d1 = defer.DeferredList(identities_deferreds, fireOnOneErrback=1)
-        d2 = defer.DeferredList(features_deferreds, fireOnOneErrback=1)
-        d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
+                if hasattr(c, "get_disco_info"):
+                    dl.append(c.get_disco_info(node))
+        d = defer.DeferredList(dl, fireOnOneErrback=1)
         d.addCallback(self._disco_info_results, iq, node)
-        d.addErrback(self._disco_info_error, iq)
-        d.addCallback(self.q)
+        d.addErrback(self._error, iq)
         d.addCallback(self.send)
 
         iq.handled = True
 
-    def q(self, result):
-        print result
-        return result
+    def _disco_info_results(self, results, iq, node):
+        info = []
+        for i in results:
+            info.extend(i[1])
 
-    def _disco_info_results(self, results, iq, node):
-        identities = []
-        for i in results[0][1]:
-            identities.extend(i[1])
-
-        features = []
-        for f in results[1][1]:
-            features.extend(f[1])
-
-        if node and not features and not identities:
+        if node and not info:
             return xmpp_error.error_from_iq(iq, 'item-not-found')
         else:
             iq.swapAttributeValues("to", "from")
             iq["type"] = "result"
-            for identity in identities:
-                i = iq.query.addElement("identity")
-                i.attributes = identity
-            print features
-            for feature in features:
-                f = iq.query.addElement("feature")
-                f["var"] = feature
-
+            iq.query.children = info
 
         return iq
 
-    def _disco_info_error(self, results, iq):
+    def _error(self, results, iq):
         return xmpp_error.error_from_iq(iq, 'internal-error')
 
     def onDiscoItems(self, iq):
+        dl = []
+        node = iq.query.getAttribute("node")
+
+        for c in self.parent:
+            if components.implements(c, component.IService):
+                if hasattr(c, "get_disco_items"):
+                    dl.append(c.get_disco_items(node))
+        d = defer.DeferredList(dl, fireOnOneErrback=1)
+        d.addCallback(self._disco_items_result, iq, node)
+        d.addErrback(self._error, iq)
+        d.addCallback(self.send)
+        
+        iq.handled = True
+    
+    def _disco_items_result(self, results, iq, node):
+        items = []
+
+        for i in results:
+            items.extend(i[1])
+
         iq.swapAttributeValues("to", "from")
         iq["type"] = "result"
-        iq.query.children = []
-        self.send(iq)
+        iq.query.children = items
+
+        return iq
     
     def iqFallback(self, iq):
         if iq.handled == True:
--- a/idavoll/pubsub.py	Sun Nov 07 14:19:46 2004 +0000
+++ b/idavoll/pubsub.py	Sun Nov 07 14:21:09 2004 +0000
@@ -5,6 +5,7 @@
 
 import backend
 import xmpp_error
+import disco
 
 NS_COMPONENT = 'jabber:component:accept'
 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
@@ -113,31 +114,37 @@
 
 class ComponentServiceFromService(Service):
 
-    def getIdentities(self, node):
-        if node:
-            d = self.backend.get_node_type(node)
-            d.addCallback(lambda x: [{'category': 'pubsub', 'type': x}])
-            d.addErrback(lambda x: [])
-            return d
-        else:
-            return defer.succeed({'category': 'pubsub',
-                                  'type': 'generic',
-                                  'name': 'Generic Pubsub Service'})
-
-    def getFeatures(self, node):
-        features = []
+    def get_disco_info(self, node):
+        info = []
 
         if not node:
+            info.append(disco.Identity('pubsub', 'generic',
+                                       'Generic Pubsub Service'))
+
             if self.backend.supports_publisher_affiliation():
-                features.append(NS_PUBSUB + "#publisher-affiliation")
+                info.append(disco.Feature(NS_PUBSUB + "#publisher-affiliation"))
 
             if self.backend.supports_outcast_affiliation():
-                features.append(NS_PUBSUB + "#outcast-affiliation")
+                info.append(disco.Feature(NS_PUBSUB + "#outcast-affiliation"))
 
             if self.backend.supports_persistent_items():
-                features.append(NS_PUBSUB + "#persistent-items")
+                info.append(disco.Feature(NS_PUBSUB + "#persistent-items"))
+
+            return defer.succeed(info)
+        else:
+            d = self.backend.get_node_type(node)
+            d.addCallback(lambda x: [disco.Identity('pubsub', x)])
+            d.addErrback(lambda _: [])
+            return d
 
-        return defer.succeed(features)
+    def get_disco_items(self, node):
+        if node:
+            return defer.succeed([])
+        
+        d = self.backend.get_nodes()
+        d.addCallback(lambda nodes: [disco.Item(self.parent.jabberId, node)
+                                    for node in nodes])
+        return d
 
 components.registerAdapter(ComponentServiceFromService, backend.IBackendService, component.IService)
 
@@ -200,13 +207,13 @@
         xmlstream.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet)
         xmlstream.addObserver(PUBSUB_OPTIONS_SET, self.onOptionsSet)
     
-    def getFeatures(self, node):
-        features = []
+    def get_disco_info(self, node):
+        info = []
 
         if not node:
-            features.append(NS_PUBSUB + "#subscribe")
+            info.append(disco.Feature(NS_PUBSUB + '#subscribe'))
 
-        return defer.succeed(features)
+        return defer.succeed(info)
 
     def onSubscribe(self, iq):
         self.handler_wrapper(self._onSubscribe, iq)
@@ -269,16 +276,16 @@
         xmlstream.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet)
         xmlstream.addObserver(PUBSUB_CONFIGURE_SET, self.onConfigureSet)
 
-    def getFeatures(self, node):
-        features = []
+    def get_disco_info(self, node):
+        info = []
 
         if not node:
-            features.append(NS_PUBSUB + "#create-nodes")
+            info.append(disco.Feature(NS_PUBSUB + "#create-nodes"))
 
             if self.backend.supports_instant_nodes():
-                features.append(NS_PUBSUB + "#instant-nodes")
+                info.append(disco.Feature(NS_PUBSUB + "#instant-nodes"))
 
-        return defer.succeed(features)
+        return defer.succeed(info)
 
     def onCreate(self, iq):
         self.handler_wrapper(self._onCreate, iq)