diff idavoll/idavoll.py @ 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 cdf17c4d3bf9
children 1f7b8b6b8ce9
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: