1
|
1 from twisted.protocols.jabber import component |
|
2 from twisted.application import service |
8
|
3 from twisted.python import components |
1
|
4 import backend |
|
5 import pubsub |
|
6 import xmpp_error |
|
7 |
|
8 import sys |
|
9 |
8
|
10 NS_DISCO = 'http://jabber.org/protocol/disco' |
|
11 NS_DISCO_INFO = NS_DISCO + '#info' |
|
12 NS_DISCO_ITEMS = NS_DISCO + '#items' |
|
13 NS_VERSION = 'jabber:iq:version' |
|
14 |
1
|
15 IQ_GET = '/iq[@type="get"]' |
|
16 IQ_SET = '/iq[@type="set"]' |
8
|
17 VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]' |
|
18 DISCO_INFO = IQ_GET + '/query[@xmlns="' + NS_DISCO_INFO + '"]' |
|
19 DISCO_ITEMS = IQ_GET + '/query[@xmlns="' + NS_DISCO_ITEMS + '"]' |
1
|
20 |
|
21 class IdavollService(component.Service): |
|
22 |
|
23 def componentConnected(self, xmlstream): |
|
24 self.xmlstream = xmlstream |
|
25 xmlstream.addObserver(VERSION, self.onVersion, 1) |
8
|
26 xmlstream.addObserver(DISCO_INFO, self.onDiscoInfo, 1) |
|
27 xmlstream.addObserver(DISCO_ITEMS, self.onDiscoItems, 1) |
1
|
28 xmlstream.addObserver(IQ_GET, self.iqFallback, -1) |
|
29 xmlstream.addObserver(IQ_SET, self.iqFallback, -1) |
8
|
30 |
|
31 def getFeatures(self, node): |
|
32 if not node: |
|
33 return [NS_DISCO_INFO, NS_DISCO_ITEMS, NS_VERSION] |
1
|
34 |
|
35 def onVersion(self, iq): |
|
36 print "version?" |
|
37 iq.swapAttributeValues("to", "from") |
|
38 iq["type"] = "result" |
|
39 name = iq.addElement("name", None, 'Idavoll') |
|
40 version = iq.addElement("version", None, '0.1') |
|
41 self.send(iq) |
|
42 iq.handled = True |
|
43 |
8
|
44 def onDiscoInfo(self, iq): |
|
45 identities = [] |
|
46 features = [] |
|
47 node = iq.query.getAttribute("node") |
|
48 |
|
49 for c in self.parent: |
|
50 if components.implements(c, component.IService): |
|
51 if hasattr(c, "getIdentities"): |
|
52 identities.extend(c.getIdentities(node)) |
|
53 if hasattr(c, "getFeatures"): |
|
54 features.extend(c.getFeatures(node)) |
|
55 |
|
56 if not features and not identities and not node: |
|
57 xmpp_error.error_from_iq(iq, 'item-not-found') |
|
58 else: |
|
59 iq.swapAttributeValues("to", "from") |
|
60 iq["type"] = "result" |
|
61 for identity in identities: |
|
62 iq.query.addElement("identity", None) |
|
63 iq.query.identity.attributes = identity |
|
64 print features |
|
65 for feature in features: |
|
66 f = iq.query.addElement("feature", None) |
|
67 f["var"] = feature |
|
68 |
|
69 self.send(iq) |
|
70 iq.handled = True |
|
71 |
|
72 def onDiscoItems(self, iq): |
|
73 iq.swapAttributeValues("to", "from") |
|
74 iq["type"] = "result" |
|
75 iq.query.children = [] |
|
76 self.send(iq) |
|
77 |
1
|
78 def iqFallback(self, iq): |
|
79 if iq.handled == True: |
|
80 return |
|
81 |
|
82 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented')) |
|
83 |
|
84 def makeService(config): |
|
85 serviceCollection = service.MultiService() |
|
86 |
|
87 pss = backend.MemoryBackendService() |
|
88 |
|
89 # set up Jabber Component |
|
90 c = component.buildServiceManager(config["jid"], config["secret"], |
|
91 ("tcp:%s:%s" % (config["rhost"], config["rport"]))) |
|
92 |
|
93 s = component.IService(pss) |
|
94 s.jid = config["jid"] |
|
95 s.setServiceParent(c) |
|
96 |
|
97 s = IdavollService() |
|
98 s.setServiceParent(c) |
|
99 |
|
100 c.setServiceParent(serviceCollection) |
|
101 |
|
102 # other stuff |
|
103 |
|
104 return c |