changeset 8:688992a789e4

Add disco support
author Ralph Meijer <ralphm@ik.nu>
date Thu, 01 Jul 2004 15:48:06 +0000
parents a8cfb31dc50c
children 52bd563b7a5d
files idavoll/idavoll.py
diffstat 1 files changed, 49 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/idavoll/idavoll.py	Sun Jun 27 15:08:24 2004 +0000
+++ b/idavoll/idavoll.py	Thu Jul 01 15:48:06 2004 +0000
@@ -1,22 +1,36 @@
 from twisted.protocols.jabber import component
 from twisted.application import service
+from twisted.python import components
 import backend
 import pubsub
 import xmpp_error
 
 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="jabber:iq:version"]'
+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 + '"]'
 
 class IdavollService(component.Service):
 
 	def componentConnected(self, xmlstream):
 		self.xmlstream = xmlstream
 		xmlstream.addObserver(VERSION, self.onVersion, 1)
+		xmlstream.addObserver(DISCO_INFO, self.onDiscoInfo, 1)
+		xmlstream.addObserver(DISCO_ITEMS, self.onDiscoItems, 1)
 		xmlstream.addObserver(IQ_GET, self.iqFallback, -1)
 		xmlstream.addObserver(IQ_SET, self.iqFallback, -1)
+
+	def getFeatures(self, node):
+		if not node:
+			return [NS_DISCO_INFO, NS_DISCO_ITEMS, NS_VERSION]
 	
 	def onVersion(self, iq):
 		print "version?"
@@ -27,6 +41,40 @@
 		self.send(iq)
 		iq.handled = True
 
+	def onDiscoInfo(self, iq):
+		identities = []
+		features = []
+		node = iq.query.getAttribute("node")
+
+		for c in self.parent:
+			if components.implements(c, component.IService):
+				if hasattr(c, "getIdentities"):
+					identities.extend(c.getIdentities(node))
+				if hasattr(c, "getFeatures"):
+					features.extend(c.getFeatures(node))
+
+		if not features and not identities and not node:
+			xmpp_error.error_from_iq(iq, 'item-not-found')
+		else:
+			iq.swapAttributeValues("to", "from")
+			iq["type"] = "result"
+			for identity in identities:
+				iq.query.addElement("identity", None)
+				iq.query.identity.attributes = identity
+			print features
+			for feature in features:
+				f = iq.query.addElement("feature", None)
+				f["var"] = feature
+
+		self.send(iq)
+		iq.handled = True
+
+	def onDiscoItems(self, iq):
+		iq.swapAttributeValues("to", "from")
+		iq["type"] = "result"
+		iq.query.children = []
+		self.send(iq)
+	
 	def iqFallback(self, iq):
 		if iq.handled == True:
 			return