changeset 15:218ec9984fa5

wokkel integration part III + memory saved again - disco handler (plugins handled)
author Goffi <goffi@goffi.org>
date Sat, 31 Oct 2009 00:18:35 +0100
parents a62d7d453f22
children 0a024d5e0cd0
files plugins/plugin_xep_0065.py plugins/plugin_xep_0096.py sat.tac
diffstat 3 files changed, 86 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/plugin_xep_0065.py	Fri Oct 30 20:05:25 2009 +0100
+++ b/plugins/plugin_xep_0065.py	Sat Oct 31 00:18:35 2009 +0100
@@ -63,6 +63,20 @@
 from binascii import hexlify
 import hashlib, pdb
 
+from zope.interface import implements
+
+try:
+    from twisted.words.protocols.xmlstream import XMPPHandler
+except ImportError:
+    from wokkel.subprotocols import XMPPHandler
+
+from wokkel import disco, iwokkel
+
+IQ_SET = '/iq[@type="set"]'
+NS_BS = 'http://jabber.org/protocol/bytestreams'
+BS_REQUEST = IQ_SET + '/query[@xmlns="' + NS_BS + '"]'
+
+
 
 PLUGIN_INFO = {
 "name": "XEP 0065 Plugin",
@@ -434,7 +448,9 @@
         debug ("Socks 5 client connection lost (reason: %s)", reason)
 
 
-class XEP_0065():
+class XEP_0065(XMPPHandler):
+    implements(iwokkel.IDisco)
+    
     def __init__(self, host):
         info("Plugin XEP_0065 initialization")
         self.host = host
@@ -442,10 +458,20 @@
         self.server_factory = Socks5ServerFactory()
         self.server_factory.protocol.host = self.host #needed for progress CB
         self.client_factory = Socks5ClientFactory()
-        host.add_IQ_cb("http://jabber.org/protocol/bytestreams", self.getFile)
         port = int(self.host.memory.getParamV("Port", "File Transfert"))
         info("Launching Socks5 Stream server on port %d", port)
         reactor.listenTCP(port, self.server_factory)
+    
+    def connectionInitialized(self):
+        self.xmlstream.addObserver(BS_REQUEST, self.getFile)
+
+
+    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
+        return [disco.DiscoFeature(NS_BS)]
+
+    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
+        return []
+
 
     def setData(self, data, id):
         self.data = data
@@ -459,10 +485,11 @@
         self.server_factory.protocol.filesize = size
         self.server_factory.protocol.transfert_id = id
 
-    def getFile(self, stanza):
+    def getFile(self, iq):
         """Get file using byte stream"""
-        SI_elem = stanza.firstChildElement()
-        IQ_id = stanza['id']
+        iq.handled = True
+        SI_elem = iq.firstChildElement()
+        IQ_id = iq['id']
         for element in SI_elem.elements():
             if element.name == "streamhost":
                 info ("Stream proposed: host=[%s] port=[%s]", element['host'], element['port'])
--- a/plugins/plugin_xep_0096.py	Fri Oct 30 20:05:25 2009 +0100
+++ b/plugins/plugin_xep_0096.py	Sat Oct 31 00:18:35 2009 +0100
@@ -27,6 +27,19 @@
 from twisted.internet import reactor #FIXME best way ???
 import pdb
 
+from zope.interface import implements
+
+try:
+    from twisted.words.protocols.xmlstream import XMPPHandler
+except ImportError:
+    from wokkel.subprotocols import XMPPHandler
+
+from wokkel import disco, iwokkel
+
+IQ_SET = '/iq[@type="set"]'
+NS_SI = 'http://jabber.org/protocol/si'
+SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]'
+
 PLUGIN_INFO = {
 "name": "XEP 0096 Plugin",
 "import_name": "XEP_0096",
@@ -36,16 +49,28 @@
 "description": """Implementation of SI File Transfert"""
 }
 
-class XEP_0096:
+class XEP_0096(XMPPHandler):
+    implements(iwokkel.IDisco)
+    
     def __init__(self, host):
         info("Plugin XEP_0096 initialization")
         self.host = host
         self._waiting_for_approval = {}
-        host.add_IQ_cb("http://jabber.org/protocol/si", self.xep_96)
         host.bridge.addMethod("sendFile", ".communication", in_sign='ss', out_sign='s', method=self.sendFile)
     
+    def connectionInitialized(self):
+        self.xmlstream.addObserver(SI_REQUEST, self.xep_96)
+
+
+    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
+        return [disco.DiscoFeature(NS_SI)]
+
+    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
+        return []
+
     def xep_96(self, IQ):
         info ("XEP-0096 management")
+        IQ.handled=True
         SI_elem = IQ.firstChildElement()
         debug(SI_elem.toXml())
         filename = ""
--- a/sat.tac	Fri Oct 30 20:05:25 2009 +0100
+++ b/sat.tac	Sat Oct 31 00:18:35 2009 +0100
@@ -44,6 +44,11 @@
 from tools.memory import Memory
 from glob import glob
 
+try:
+    from twisted.words.protocols.xmlstream import XMPPHandler
+except ImportError:
+    from wokkel.subprotocols import XMPPHandler
+
 
 ### logging configuration FIXME: put this elsewhere ###
 logging.basicConfig(level=logging.DEBUG,
@@ -174,6 +179,16 @@
     def __init__(self, host):
         disco.DiscoClientProtocol.__init__(self)
 
+class SatFallbackHandler(generic.FallbackHandler):
+    def __init__(self, host):
+        generic.FallbackHandler.__init__(self)
+
+    def iqFallback(self, iq):
+        #pdb.set_trace()
+        print "iqFallback: xml = [%s], handled=%s" % (iq.toXml(), "True" if iq.handled else "False")
+        generic.FallbackHandler.iqFallback(self, iq)
+    
+
 class SAT(service.Service):
     
     def __init__(self):
@@ -181,7 +196,6 @@
         self.memory=Memory()
         self.server_features=[]  #XXX: temp dic, need to be transfered into self.memory in the future
 
-        self._iq_cb_map = {}  #callback called when ns is found on IQ
         self._waiting_conf = {}  #callback called when a confirmation is received
         self._progress_cb_map = {}  #callback called when a progress is requested (key = progress id)
         self.plugins = {}
@@ -240,12 +254,17 @@
         self.presence = SatPresenceProtocol(self)
         self.presence.setHandlerParent(self.xmppclient)
 
-        self.fallBack = generic.FallbackHandler()
+        self.fallBack = SatFallbackHandler(self)
         self.fallBack.setHandlerParent(self.xmppclient)
 
         self.versionHandler = generic.VersionHandler(unicode(client_name), client_version)
         self.versionHandler.setHandlerParent(self.xmppclient)
 
+        debug ("setting plugins parents")
+        for plugin in self.plugins.iteritems():
+            if isinstance(plugin[1], XMPPHandler):
+                plugin[1].setHandlerParent(self.xmppclient)
+        
         self.xmppclient.startService()
     
     def disconnect(self):
@@ -260,6 +279,7 @@
         self.connect()
     
     def stopService(self):
+        self.memory.save()
         info("Salut aussi à Rantanplan")
 
     def run(self):
@@ -277,21 +297,16 @@
         self.xmlstream = self.xmppclient.xmlstream
         self.me = self.xmppclient.jid #in case of the ressource has changed
         
+        self.disco = SatDiscoProtocol(self)
+        self.disco.setHandlerParent(self.xmppclient)
+        self.discoHandler = disco.DiscoHandler()
+        self.discoHandler.setHandlerParent(self.xmppclient)
+
         self.roster.requestRoster()
         
         self.presence.available()
         
-        #FIXME:tmp
-        self.xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb)
-        ###FIXME: tmp disco ###
-        self.disco = SatDiscoProtocol(self)
-        self.disco.setHandlerParent(self.xmppclient)
         self.disco.requestInfo(jid.JID(self.memory.getParamV("Server", "Connection"))).addCallback(self.serverDisco)
-        #we now send our presence status
-
-        # add a callback for the messages
-
-        #reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile")
         
 
     def sendMessage(self,to,msg,type='chat'):
@@ -363,25 +378,6 @@
 
     ## callbacks ##
 
-    def add_IQ_cb(self, ns, cb):
-        """Add an IQ callback on namespace ns"""
-        debug ("Registered callback for namespace %s", ns)
-        self._iq_cb_map[ns]=cb
-
-    def iqCb(self, stanza):
-        info ("iqCb")
-        debug ("="*20)
-        debug ("DEBUG:\n")
-        debug (stanza.toXml().encode('utf-8'))
-        debug ("="*20)
-        #FIXME: temporary ugly code
-        uri = stanza.firstChildElement().uri
-        if self._iq_cb_map.has_key(uri):
-            self._iq_cb_map[uri](stanza)
-        #TODO: manage errors stanza
-
-
-
     def serverDisco(self, disco):
         """xep-0030 Discovery Protocol."""
         for feature in disco.features:
@@ -439,7 +435,3 @@
 application = service.Application('SàT')
 service = SAT()
 service.setServiceParent(application)
-
-
-#app.memory.save()  #FIXME: not the best place 
-#debug("Good Bye")