comparison idavoll/idavoll.py @ 8:688992a789e4

Add disco support
author Ralph Meijer <ralphm@ik.nu>
date Thu, 01 Jul 2004 15:48:06 +0000
parents 4cc41776b7d7
children d599da9179ab
comparison
equal deleted inserted replaced
7:a8cfb31dc50c 8:688992a789e4
1 from twisted.protocols.jabber import component 1 from twisted.protocols.jabber import component
2 from twisted.application import service 2 from twisted.application import service
3 from twisted.python import components
3 import backend 4 import backend
4 import pubsub 5 import pubsub
5 import xmpp_error 6 import xmpp_error
6 7
7 import sys 8 import sys
8 9
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
9 IQ_GET = '/iq[@type="get"]' 15 IQ_GET = '/iq[@type="get"]'
10 IQ_SET = '/iq[@type="set"]' 16 IQ_SET = '/iq[@type="set"]'
11 VERSION = IQ_GET + '/query[@xmlns="jabber:iq:version"]' 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 + '"]'
12 20
13 class IdavollService(component.Service): 21 class IdavollService(component.Service):
14 22
15 def componentConnected(self, xmlstream): 23 def componentConnected(self, xmlstream):
16 self.xmlstream = xmlstream 24 self.xmlstream = xmlstream
17 xmlstream.addObserver(VERSION, self.onVersion, 1) 25 xmlstream.addObserver(VERSION, self.onVersion, 1)
26 xmlstream.addObserver(DISCO_INFO, self.onDiscoInfo, 1)
27 xmlstream.addObserver(DISCO_ITEMS, self.onDiscoItems, 1)
18 xmlstream.addObserver(IQ_GET, self.iqFallback, -1) 28 xmlstream.addObserver(IQ_GET, self.iqFallback, -1)
19 xmlstream.addObserver(IQ_SET, self.iqFallback, -1) 29 xmlstream.addObserver(IQ_SET, self.iqFallback, -1)
30
31 def getFeatures(self, node):
32 if not node:
33 return [NS_DISCO_INFO, NS_DISCO_ITEMS, NS_VERSION]
20 34
21 def onVersion(self, iq): 35 def onVersion(self, iq):
22 print "version?" 36 print "version?"
23 iq.swapAttributeValues("to", "from") 37 iq.swapAttributeValues("to", "from")
24 iq["type"] = "result" 38 iq["type"] = "result"
25 name = iq.addElement("name", None, 'Idavoll') 39 name = iq.addElement("name", None, 'Idavoll')
26 version = iq.addElement("version", None, '0.1') 40 version = iq.addElement("version", None, '0.1')
27 self.send(iq) 41 self.send(iq)
28 iq.handled = True 42 iq.handled = True
29 43
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
30 def iqFallback(self, iq): 78 def iqFallback(self, iq):
31 if iq.handled == True: 79 if iq.handled == True:
32 return 80 return
33 81
34 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented')) 82 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented'))