diff src/plugins/plugin_xep_0047.py @ 1578:7fef6cdf5953

plugin XEP-0047: some cleaning
author Goffi <goffi@goffi.org>
date Wed, 11 Nov 2015 18:21:20 +0100
parents 6a8dd91476f0
children d46aae87c03a
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0047.py	Wed Nov 11 18:19:49 2015 +0100
+++ b/src/plugins/plugin_xep_0047.py	Wed Nov 11 18:21:20 2015 +0100
@@ -22,7 +22,6 @@
 log = getLogger(__name__)
 from sat.core.constants import Const as C
 from sat.core import exceptions
-from twisted.words.protocols.jabber import client as jabber_client
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber import xmlstream
 from twisted.words.protocols.jabber import error
@@ -295,45 +294,48 @@
         assert block_size <= 65535
         session_data["block_size"] = block_size
 
-        iq_elt = jabber_client.IQ(client.xmlstream, 'set') # FIXME: use client.IQ here
-        iq_elt['from'] = client.jid.full()
+        iq_elt = client.IQ()
         iq_elt['to'] = to_jid.full()
-        open_elt = iq_elt.addElement('open', NS_IBB)
+        open_elt = iq_elt.addElement((NS_IBB, 'open'))
         open_elt['block-size'] = str(block_size)
         open_elt['sid'] = sid
         open_elt['stanza'] = 'iq' # TODO: manage <message> stanza ?
-        iq_elt.addCallback(self._IQDataStream, session_data, client)
-        iq_elt.send()
+        args = [session_data, client]
+        d = iq_elt.send()
+        d.addCallbacks(self._IQDataStreamCb, self._IQDataStreamEb, args, None, args)
         return session_data[DEFER_KEY]
 
-    def _IQDataStream(self, session_data, client, iq_elt):
+    def _IQDataStreamCb(self, iq_elt, session_data, client):
         """Called during the whole data streaming
 
+        @param iq_elt(domish.Element): iq result
         @param session_data(dict): data of this streaming session
         @param client: %(doc_client)s
-        @param iq_elt(domish.Element): iq result
         """
-        if iq_elt['type'] == 'error':
-            log.warning(_(u"IBB transfer failed: {}").format(iq_elt))
-            self.terminateStream(session_data, client, "IQ_ERROR")
-            return
-
         session_data["timer"].reset(TIMEOUT)
 
         buffer_ = session_data["file_obj"].read(session_data["block_size"])
         if buffer_:
-            next_iq_elt = jabber_client.IQ(client.xmlstream, 'set') # FIXME: use client.IQ here
+            next_iq_elt = client.IQ()
             next_iq_elt['to'] = session_data["to"].full()
-            data_elt = next_iq_elt.addElement('data', NS_IBB)
+            data_elt = next_iq_elt.addElement((NS_IBB, 'data'))
             seq = session_data['seq'] = (session_data['seq'] + 1) % 65535
             data_elt['seq'] = unicode(seq)
             data_elt['sid'] = session_data['id']
             data_elt.addContent(base64.b64encode(buffer_))
-            next_iq_elt.addCallback(self._IQDataStream, session_data, client)
-            next_iq_elt.send()
+            args = [session_data, client]
+            d = next_iq_elt.send()
+            d.addCallbacks(self._IQDataStreamCb, self._IQDataStreamEb, args, None, args)
         else:
             self.terminateStream(session_data, client)
 
+    def _IQDataStreamEb(self, failure, session_data, client):
+        if failure.check(error.StanzaError):
+            log.warning(u"IBB transfer failed: {}".format(failure.condition))
+        else:
+            log.error(u"IBB transfer failed: {}".format(failure.condition))
+        self.terminateStream(session_data, client, "IQ_ERROR")
+
     def terminateStream(self, session_data, client, failure_reason=None):
         """Terminate the stream session
 
@@ -341,9 +343,9 @@
         @param client: %(doc_client)s
         @param failure_reason(unicode, None): reason of the failure, or None if steam was successful
         """
-        iq_elt = jabber_client.IQ(client.xmlstream, 'set')
+        iq_elt = client.IQ()
         iq_elt['to'] = session_data["to"].full()
-        close_elt = iq_elt.addElement('close', NS_IBB)
+        close_elt = iq_elt.addElement((NS_IBB, 'close'))
         close_elt['sid'] = session_data['id']
         iq_elt.send()
         self._killSession(session_data['id'], client, failure_reason)