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