diff sat/plugins/plugin_xep_0260.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 69e4716d6268
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0260.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0260.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SAT plugin for Jingle (XEP-0260)
@@ -24,7 +24,7 @@
 log = getLogger(__name__)
 from sat.core import exceptions
 from wokkel import disco, iwokkel
-from zope.interface import implements
+from zope.interface import implementer
 from twisted.words.xish import domish
 from twisted.words.protocols.jabber import jid
 from twisted.internet import defer
@@ -122,15 +122,15 @@
             transport_elt["mode"] = "tcp"  # XXX: we only manage tcp for now
 
         for candidate in candidates:
-            log.debug(u"Adding candidate: {}".format(candidate))
+            log.debug("Adding candidate: {}".format(candidate))
             candidate_elt = transport_elt.addElement("candidate", NS_JINGLE_S5B)
             if candidate.id is None:
-                candidate.id = unicode(uuid.uuid4())
+                candidate.id = str(uuid.uuid4())
             candidate_elt["cid"] = candidate.id
             candidate_elt["host"] = candidate.host
             candidate_elt["jid"] = candidate.jid.full()
-            candidate_elt["port"] = unicode(candidate.port)
-            candidate_elt["priority"] = unicode(candidate.priority)
+            candidate_elt["port"] = str(candidate.port)
+            candidate_elt["priority"] = str(candidate.priority)
             candidate_elt["type"] = candidate.type
         return transport_elt
 
@@ -138,12 +138,12 @@
     def jingleSessionInit(self, client, session, content_name):
         content_data = session["contents"][content_name]
         transport_data = content_data["transport_data"]
-        sid = transport_data["sid"] = unicode(uuid.uuid4())
+        sid = transport_data["sid"] = str(uuid.uuid4())
         session_hash = transport_data["session_hash"] = self._s5b.getSessionHash(
-            session[u"local_jid"], session["peer_jid"], sid
+            session["local_jid"], session["peer_jid"], sid
         )
         transport_data["peer_session_hash"] = self._s5b.getSessionHash(
-            session["peer_jid"], session[u"local_jid"], sid
+            session["peer_jid"], session["local_jid"], sid
         )  # requester and target are inversed for peer candidates
         transport_data["stream_d"] = self._s5b.registerHash(client, session_hash, None)
         candidates = transport_data["candidates"] = yield self._s5b.getCandidates(
@@ -181,7 +181,7 @@
         transport_elt.addElement("proxy-error")
         iq_elt.send()
         log.warning(
-            u"Can't activate proxy, we need to fallback to IBB: {reason}".format(
+            "Can't activate proxy, we need to fallback to IBB: {reason}".format(
                 reason=stanza_error.value.condition
             )
         )
@@ -211,10 +211,10 @@
             client, self._j.A_TRANSPORT_INFO, session, content_name
         )
         if candidate is None:
-            log.warning(u"Can't connect to any peer candidate")
+            log.warning("Can't connect to any peer candidate")
             candidate_elt = transport_elt.addElement("candidate-error")
         else:
-            log.info(u"Found best peer candidate: {}".format(unicode(candidate)))
+            log.info("Found best peer candidate: {}".format(str(candidate)))
             candidate_elt = transport_elt.addElement("candidate-used")
             candidate_elt["cid"] = candidate.id
         iq_elt.send()  # TODO: check result stanza
@@ -248,9 +248,9 @@
             if best_candidate.priority == peer_best_candidate.priority:
                 # same priority, we choose initiator one according to XEP-0260 §2.4 #4
                 log.debug(
-                    u"Candidates have same priority, we select the one choosed by initiator"
+                    "Candidates have same priority, we select the one choosed by initiator"
                 )
-                if session["initiator"] == session[u"local_jid"]:
+                if session["initiator"] == session["local_jid"]:
                     choosed_candidate = best_candidate
                 else:
                     choosed_candidate = peer_best_candidate
@@ -260,7 +260,7 @@
                 )
 
         if choosed_candidate is None:
-            log.warning(u"Socks5 negociation failed, we need to fallback to IBB")
+            log.warning("Socks5 negociation failed, we need to fallback to IBB")
             self.doFallback(session, content_name, client)
         else:
             if choosed_candidate == peer_best_candidate:
@@ -276,8 +276,8 @@
                 our_candidate = False
 
             log.info(
-                u"Socks5 negociation successful, {who} candidate will be used: {candidate}".format(
-                    who=u"our" if our_candidate else u"other peer",
+                "Socks5 negociation successful, {who} candidate will be used: {candidate}".format(
+                    who="our" if our_candidate else "other peer",
                     candidate=choosed_candidate,
                 )
             )
@@ -321,10 +321,10 @@
         Will try to fallback to IBB
         """
         try:
-            reason = unicode(fail.value)
+            reason = str(fail.value)
         except AttributeError:
-            reason = unicode(fail)
-        log.warning(u"Cant start transfert, we'll try fallback method: {}".format(reason))
+            reason = str(fail)
+        log.warning("Cant start transfert, we'll try fallback method: {}".format(reason))
         self.doFallback(session, content_name, client)
 
     def _candidateInfo(
@@ -347,25 +347,25 @@
             try:
                 cid = candidate_elt.attributes["cid"]
             except KeyError:
-                log.warning(u"No cid found in <candidate-used>")
+                log.warning("No cid found in <candidate-used>")
                 raise exceptions.DataError
             try:
-                candidate = (
+                candidate = next((
                     c for c in transport_data["candidates"] if c.id == cid
-                ).next()
+                ))
             except StopIteration:
-                log.warning(u"Given cid doesn't correspond to any known candidate !")
+                log.warning("Given cid doesn't correspond to any known candidate !")
                 raise exceptions.DataError  # TODO: send an error to other peer, and use better exception
             except KeyError:
                 # a transport-info can also be intentionaly sent too early by other peer
                 # but there is little probability
                 log.error(
-                    u'"candidates" key doesn\'t exists in transport_data, it should at this point'
+                    '"candidates" key doesn\'t exists in transport_data, it should at this point'
                 )
                 raise exceptions.InternalError
             # at this point we have the candidate choosed by other peer
             transport_data["peer_best_candidate"] = candidate
-            log.info(u"Other peer best candidate: {}".format(candidate))
+            log.info("Other peer best candidate: {}".format(candidate))
 
         del transport_data["candidates"]
         self._checkCandidates(session, content_name, transport_data, client)
@@ -385,7 +385,7 @@
         try:
             activation_d = transport_data.pop("activation_d")
         except KeyError:
-            log.warning(u"Received unexpected transport-info for proxy activation")
+            log.warning("Received unexpected transport-info for proxy activation")
 
         if proxy_elt.name == "activated":
             activation_d.callback(None)
@@ -467,7 +467,7 @@
             ):
                 for name in names:
                     try:
-                        candidate_elt = transport_elt.elements(NS_JINGLE_S5B, name).next()
+                        candidate_elt = next(transport_elt.elements(NS_JINGLE_S5B, name))
                     except StopIteration:
                         continue
                     else:
@@ -478,20 +478,20 @@
 
             if candidate_elt is None:
                 log.warning(
-                    u"Unexpected transport element: {}".format(transport_elt.toXml())
+                    "Unexpected transport element: {}".format(transport_elt.toXml())
                 )
         elif action == self._j.A_DESTROY:
             # the transport is replaced (fallback ?), We need mainly to kill XEP-0065 session.
             # note that sid argument is not necessary for sessions created by this plugin
             self._s5b.killSession(None, transport_data["session_hash"], None, client)
         else:
-            log.warning(u"FIXME: unmanaged action {}".format(action))
+            log.warning("FIXME: unmanaged action {}".format(action))
 
         defer.returnValue(transport_elt)
 
     def jingleTerminate(self, client, action, session, content_name, reason_elt):
         if reason_elt.decline:
-            log.debug(u"Session declined, deleting S5B session")
+            log.debug("Session declined, deleting S5B session")
             # we just need to clean the S5B session if it is declined
             content_data = session["contents"][content_name]
             transport_data = content_data["transport_data"]
@@ -504,7 +504,7 @@
          """
         if not feature_checked:
             log.warning(
-                u"Other peer can't manage jingle IBB, be have to terminate the session"
+                "Other peer can't manage jingle IBB, be have to terminate the session"
             )
             self._j.terminate(client, self._j.REASON_CONNECTIVITY_ERROR, session)
         else:
@@ -524,7 +524,7 @@
             return
         if self._jingle_ibb is None:
             log.warning(
-                u"Jingle IBB (XEP-0261) plugin is not available, we have to close the session"
+                "Jingle IBB (XEP-0261) plugin is not available, we have to close the session"
             )
             self._j.terminate(client, self._j.REASON_CONNECTIVITY_ERROR, session)
         else:
@@ -535,8 +535,8 @@
         return d
 
 
+@implementer(iwokkel.IDisco)
 class XEP_0260_handler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
         return [disco.DiscoFeature(NS_JINGLE_S5B)]