Mercurial > libervia-pubsub
comparison 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 |
comparison
equal
deleted
inserted
replaced
72:a914cad421c5 | 73:5d7a924ebddb |
---|---|
3 from twisted.python import components | 3 from twisted.python import components |
4 from twisted.internet import defer | 4 from twisted.internet import defer |
5 import backend | 5 import backend |
6 import pubsub | 6 import pubsub |
7 import xmpp_error | 7 import xmpp_error |
8 import disco | |
8 | 9 |
9 import sys | 10 import sys |
10 | 11 |
11 NS_DISCO = 'http://jabber.org/protocol/disco' | |
12 NS_DISCO_INFO = NS_DISCO + '#info' | |
13 NS_DISCO_ITEMS = NS_DISCO + '#items' | |
14 NS_VERSION = 'jabber:iq:version' | 12 NS_VERSION = 'jabber:iq:version' |
15 | 13 |
16 IQ_GET = '/iq[@type="get"]' | 14 IQ_GET = '/iq[@type="get"]' |
17 IQ_SET = '/iq[@type="set"]' | 15 IQ_SET = '/iq[@type="set"]' |
18 VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]' | 16 VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]' |
19 DISCO_INFO = IQ_GET + '/query[@xmlns="' + NS_DISCO_INFO + '"]' | 17 DISCO_INFO = IQ_GET + '/query[@xmlns="' + disco.NS_INFO + '"]' |
20 DISCO_ITEMS = IQ_GET + '/query[@xmlns="' + NS_DISCO_ITEMS + '"]' | 18 DISCO_ITEMS = IQ_GET + '/query[@xmlns="' + disco.NS_ITEMS + '"]' |
21 | 19 |
22 class IdavollService(component.Service): | 20 class IdavollService(component.Service): |
23 | 21 |
24 def componentConnected(self, xmlstream): | 22 def componentConnected(self, xmlstream): |
25 self.xmlstream = xmlstream | 23 self.xmlstream = xmlstream |
27 xmlstream.addObserver(DISCO_INFO, self.onDiscoInfo, 1) | 25 xmlstream.addObserver(DISCO_INFO, self.onDiscoInfo, 1) |
28 xmlstream.addObserver(DISCO_ITEMS, self.onDiscoItems, 1) | 26 xmlstream.addObserver(DISCO_ITEMS, self.onDiscoItems, 1) |
29 xmlstream.addObserver(IQ_GET, self.iqFallback, -1) | 27 xmlstream.addObserver(IQ_GET, self.iqFallback, -1) |
30 xmlstream.addObserver(IQ_SET, self.iqFallback, -1) | 28 xmlstream.addObserver(IQ_SET, self.iqFallback, -1) |
31 | 29 |
32 def getFeatures(self, node): | 30 def get_disco_info(self, node): |
33 features = [] | 31 info = [] |
34 | 32 |
35 if not node: | 33 if not node: |
36 features.extend([NS_DISCO_ITEMS, NS_VERSION]) | 34 info.append(disco.Feature(disco.NS_ITEMS)) |
35 info.append(disco.Feature(NS_VERSION)) | |
37 | 36 |
38 return defer.succeed(features) | 37 return defer.succeed(info) |
39 | 38 |
40 def onVersion(self, iq): | 39 def onVersion(self, iq): |
41 iq.swapAttributeValues("to", "from") | 40 iq.swapAttributeValues("to", "from") |
42 iq["type"] = "result" | 41 iq["type"] = "result" |
43 name = iq.addElement("name", None, 'Idavoll') | 42 name = iq.addElement("name", None, 'Idavoll') |
44 version = iq.addElement("version", None, '0.1') | 43 version = iq.addElement("version", None, '0.1') |
45 self.send(iq) | 44 self.send(iq) |
46 iq.handled = True | 45 iq.handled = True |
47 | 46 |
48 def onDiscoInfo(self, iq): | 47 def onDiscoInfo(self, iq): |
49 identities_deferreds = [] | 48 dl = [] |
50 features_deferreds = [] | |
51 node = iq.query.getAttribute("node") | 49 node = iq.query.getAttribute("node") |
52 | 50 |
53 for c in self.parent: | 51 for c in self.parent: |
54 if components.implements(c, component.IService): | 52 if components.implements(c, component.IService): |
55 if hasattr(c, "getIdentities"): | 53 if hasattr(c, "get_disco_info"): |
56 identities_deferreds.append(c.getIdentities(node)) | 54 dl.append(c.get_disco_info(node)) |
57 if hasattr(c, "getFeatures"): | 55 d = defer.DeferredList(dl, fireOnOneErrback=1) |
58 features_deferreds.append(c.getFeatures(node)) | |
59 print identities_deferreds | |
60 print features_deferreds | |
61 d1 = defer.DeferredList(identities_deferreds, fireOnOneErrback=1) | |
62 d2 = defer.DeferredList(features_deferreds, fireOnOneErrback=1) | |
63 d = defer.DeferredList([d1, d2], fireOnOneErrback=1) | |
64 d.addCallback(self._disco_info_results, iq, node) | 56 d.addCallback(self._disco_info_results, iq, node) |
65 d.addErrback(self._disco_info_error, iq) | 57 d.addErrback(self._error, iq) |
66 d.addCallback(self.q) | |
67 d.addCallback(self.send) | 58 d.addCallback(self.send) |
68 | 59 |
69 iq.handled = True | 60 iq.handled = True |
70 | 61 |
71 def q(self, result): | 62 def _disco_info_results(self, results, iq, node): |
72 print result | 63 info = [] |
73 return result | 64 for i in results: |
65 info.extend(i[1]) | |
74 | 66 |
75 def _disco_info_results(self, results, iq, node): | 67 if node and not info: |
76 identities = [] | |
77 for i in results[0][1]: | |
78 identities.extend(i[1]) | |
79 | |
80 features = [] | |
81 for f in results[1][1]: | |
82 features.extend(f[1]) | |
83 | |
84 if node and not features and not identities: | |
85 return xmpp_error.error_from_iq(iq, 'item-not-found') | 68 return xmpp_error.error_from_iq(iq, 'item-not-found') |
86 else: | 69 else: |
87 iq.swapAttributeValues("to", "from") | 70 iq.swapAttributeValues("to", "from") |
88 iq["type"] = "result" | 71 iq["type"] = "result" |
89 for identity in identities: | 72 iq.query.children = info |
90 i = iq.query.addElement("identity") | |
91 i.attributes = identity | |
92 print features | |
93 for feature in features: | |
94 f = iq.query.addElement("feature") | |
95 f["var"] = feature | |
96 | |
97 | 73 |
98 return iq | 74 return iq |
99 | 75 |
100 def _disco_info_error(self, results, iq): | 76 def _error(self, results, iq): |
101 return xmpp_error.error_from_iq(iq, 'internal-error') | 77 return xmpp_error.error_from_iq(iq, 'internal-error') |
102 | 78 |
103 def onDiscoItems(self, iq): | 79 def onDiscoItems(self, iq): |
80 dl = [] | |
81 node = iq.query.getAttribute("node") | |
82 | |
83 for c in self.parent: | |
84 if components.implements(c, component.IService): | |
85 if hasattr(c, "get_disco_items"): | |
86 dl.append(c.get_disco_items(node)) | |
87 d = defer.DeferredList(dl, fireOnOneErrback=1) | |
88 d.addCallback(self._disco_items_result, iq, node) | |
89 d.addErrback(self._error, iq) | |
90 d.addCallback(self.send) | |
91 | |
92 iq.handled = True | |
93 | |
94 def _disco_items_result(self, results, iq, node): | |
95 items = [] | |
96 | |
97 for i in results: | |
98 items.extend(i[1]) | |
99 | |
104 iq.swapAttributeValues("to", "from") | 100 iq.swapAttributeValues("to", "from") |
105 iq["type"] = "result" | 101 iq["type"] = "result" |
106 iq.query.children = [] | 102 iq.query.children = items |
107 self.send(iq) | 103 |
104 return iq | |
108 | 105 |
109 def iqFallback(self, iq): | 106 def iqFallback(self, iq): |
110 if iq.handled == True: | 107 if iq.handled == True: |
111 return | 108 return |
112 | 109 |