diff plugins/plugin_xep_0065.py @ 64:d46f849664aa

SàT: multi-profile, plugins updated - core: 2 new convenient methods: getJidNStream and getClient - new param in plugin info: "handler" to know if there is a handler to plug on profiles clients - plugins with handler now use an other class which is returned to profile client with the new method "getHandler" and pluged when connecting
author Goffi <goffi@goffi.org>
date Sat, 30 Jan 2010 16:17:33 +1100
parents 9764e027ecc0
children 86f1f7f6d332
line wrap: on
line diff
--- a/plugins/plugin_xep_0065.py	Fri Jan 29 14:17:15 2010 +1100
+++ b/plugins/plugin_xep_0065.py	Sat Jan 30 16:17:33 2010 +1100
@@ -85,6 +85,7 @@
 "type": "XEP",
 "protocols": ["XEP-0065"],
 "main": "XEP_0065",
+"handler": "yes",
 "description": """Implementation of SOCKS5 Bytestreams"""
 }
 
@@ -312,7 +313,7 @@
             debug("Saving file in %s.", self.data["dest_path"])
             self.dest_file = open(self.data["dest_path"], 'w')
             self.state = STATE_TARGET_READY
-            self.activateCB(self.target_jid, self.initiator_jid, self.sid, self.IQ_id)
+            self.activateCB(self.target_jid, self.initiator_jid, self.sid, self.IQ_id, self.xmlstream)
 
 
         except struct.error, why:
@@ -450,8 +451,7 @@
         debug ("Socks 5 client connection lost (reason: %s)", reason)
 
 
-class XEP_0065(XMPPHandler):
-    implements(iwokkel.IDisco)
+class XEP_0065():
     
     params = """
     <params>
@@ -480,20 +480,12 @@
         info("Launching Socks5 Stream server on port %d", port)
         reactor.listenTCP(port, self.server_factory)
     
+    def getHandler(self):
+        return XEP_0065_handler(self)  
+   
     def getExternalIP(self):
         """Return IP visible from outside, by asking to a website"""
         return getPage("http://www.goffi.org/sat_tools/get_ip.php")
-    
-    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
@@ -507,8 +499,10 @@
         self.server_factory.protocol.filesize = size
         self.server_factory.protocol.transfert_id = id
 
-    def getFile(self, iq):
+    def getFile(self, iq, profile_key='@DEFAULT@'):
         """Get file using byte stream"""
+        client = self.host.getClient(profile_key)
+        assert(client)
         iq.handled = True
         SI_elem = iq.firstChildElement()
         IQ_id = iq['id']
@@ -518,17 +512,18 @@
                 factory = self.client_factory
                 self.server_factory.protocol.mode = "target"
                 factory.protocol.host = self.host #needed for progress CB
+                factory.protocol.xmlstream = client.xmlstream
                 factory.protocol.data = self.data
                 factory.protocol.transfert_id = self.transfert_id
                 factory.protocol.filesize = self.data["size"]
                 factory.protocol.sid = SI_elem['sid']
                 factory.protocol.initiator_jid = element['jid']
-                factory.protocol.target_jid = self.host.me.full()
+                factory.protocol.target_jid = client.jid.full()
                 factory.protocol.IQ_id = IQ_id
                 factory.protocol.activateCB = self.activateStream
                 reactor.connectTCP(element['host'], int(element['port']), factory)
                 
-    def activateStream(self, from_jid, to_jid, sid, IQ_id):
+    def activateStream(self, from_jid, to_jid, sid, IQ_id, xmlstream):
         debug("activating stream")
         result = domish.Element(('', 'iq'))
         result['type'] = 'result'
@@ -539,5 +534,21 @@
         query['sid'] = sid
         streamhost = query.addElement('streamhost-used')
         streamhost['jid'] = to_jid  #FIXME: use real streamhost
-        self.host.xmlstream.send(result)
+        xmlstream.send(result)
 
+class XEP_0065_handler(XMPPHandler):
+    implements(iwokkel.IDisco)
+    
+    def __init__(self, plugin_parent):
+        self.plugin_parent = plugin_parent
+        self.host = plugin_parent.host
+    
+    def connectionInitialized(self):
+        self.xmlstream.addObserver(BS_REQUEST, self.plugin_parent.getFile)
+
+
+    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
+        return [disco.DiscoFeature(NS_BS)]
+
+    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
+        return []