diff sat/plugins/plugin_xep_0047.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_0047.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0047.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 managing gateways (xep-0047)
@@ -32,7 +32,7 @@
 
 from wokkel import disco, iwokkel
 
-from zope.interface import implements
+from zope.interface import implementer
 
 import base64
 
@@ -84,7 +84,7 @@
         @param client: %(doc_client)s
         """
         log.info(
-            u"In-Band Bytestream: TimeOut reached for id {sid} [{profile}]".format(
+            "In-Band Bytestream: TimeOut reached for id {sid} [{profile}]".format(
                 sid=sid, profile=client.profile
             )
         )
@@ -101,7 +101,7 @@
         try:
             session = client.xep_0047_current_stream[sid]
         except KeyError:
-            log.warning(u"kill id called on a non existant id")
+            log.warning("kill id called on a non existant id")
             return
 
         try:
@@ -141,7 +141,7 @@
         @return (dict): session data
         """
         if sid in client.xep_0047_current_stream:
-            raise exceptions.ConflictError(u"A session with this id already exists !")
+            raise exceptions.ConflictError("A session with this id already exists !")
         session_data = client.xep_0047_current_stream[sid] = {
             "id": sid,
             DEFER_KEY: defer.Deferred(),
@@ -159,16 +159,16 @@
 
         @param iq_elt(domish.Element): the whole <iq> stanza
         """
-        log.debug(_(u"IBB stream opening"))
+        log.debug(_("IBB stream opening"))
         iq_elt.handled = True
-        open_elt = iq_elt.elements(NS_IBB, "open").next()
+        open_elt = next(iq_elt.elements(NS_IBB, "open"))
         block_size = open_elt.getAttribute("block-size")
         sid = open_elt.getAttribute("sid")
         stanza = open_elt.getAttribute("stanza", "iq")
         if not sid or not block_size or int(block_size) > 65535:
             return self._sendError("not-acceptable", sid or None, iq_elt, client)
         if not sid in client.xep_0047_current_stream:
-            log.warning(_(u"Ignoring unexpected IBB transfer: %s" % sid))
+            log.warning(_("Ignoring unexpected IBB transfer: %s" % sid))
             return self._sendError("not-acceptable", sid or None, iq_elt, client)
         session_data = client.xep_0047_current_stream[sid]
         if session_data["to"] != jid.JID(iq_elt["from"]):
@@ -204,7 +204,7 @@
         """
         iq_elt.handled = True
         log.debug(_("IBB stream closing"))
-        close_elt = iq_elt.elements(NS_IBB, "close").next()
+        close_elt = next(iq_elt.elements(NS_IBB, "close"))
         # XXX: this observer is only triggered on valid sid, so we don't need to check it
         sid = close_elt["sid"]
 
@@ -219,13 +219,13 @@
         @param element(domish.Element): <iq> or <message> stanza
         """
         element.handled = True
-        data_elt = element.elements(NS_IBB, "data").next()
+        data_elt = next(element.elements(NS_IBB, "data"))
         sid = data_elt["sid"]
 
         try:
             session_data = client.xep_0047_current_stream[sid]
         except KeyError:
-            log.warning(_(u"Received data for an unknown session id"))
+            log.warning(_("Received data for an unknown session id"))
             return self._sendError("item-not-found", None, element, client)
 
         from_jid = session_data["to"]
@@ -234,7 +234,7 @@
         if from_jid.full() != element["from"]:
             log.warning(
                 _(
-                    u"sended jid inconsistency (man in the middle attack attempt ?)\ninitial={initial}\ngiven={given}"
+                    "sended jid inconsistency (man in the middle attack attempt ?)\ninitial={initial}\ngiven={given}"
                 ).format(initial=from_jid, given=element["from"])
             )
             if element.name == "iq":
@@ -243,7 +243,7 @@
 
         session_data["seq"] = (session_data["seq"] + 1) % 65535
         if int(data_elt.getAttribute("seq", -1)) != session_data["seq"]:
-            log.warning(_(u"Sequence error"))
+            log.warning(_("Sequence error"))
             if element.name == "iq":
                 reason = "not-acceptable"
                 self._sendError(reason, sid, element, client)
@@ -258,7 +258,7 @@
             stream_object.write(base64.b64decode(str(data_elt)))
         except TypeError:
             # The base64 data is invalid
-            log.warning(_(u"Invalid base64 data"))
+            log.warning(_("Invalid base64 data"))
             if element.name == "iq":
                 self._sendError("not-acceptable", sid, element, client)
             self.terminateStream(session_data, client, reason)
@@ -279,7 +279,7 @@
         """
         iq_elt = error.StanzaError(error_condition).toResponse(iq_elt)
         log.warning(
-            u"Error while managing in-band bytestream session, cancelling: {}".format(
+            "Error while managing in-band bytestream session, cancelling: {}".format(
                 error_condition
             )
         )
@@ -334,7 +334,7 @@
             next_iq_elt["to"] = session_data["to"].full()
             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["seq"] = str(seq)
             data_elt["sid"] = session_data["id"]
             data_elt.addContent(base64.b64encode(buffer_))
             args = [session_data, client]
@@ -345,9 +345,9 @@
 
     def _IQDataStreamEb(self, failure, session_data, client):
         if failure.check(error.StanzaError):
-            log.warning(u"IBB transfer failed: {}".format(failure.value))
+            log.warning("IBB transfer failed: {}".format(failure.value))
         else:
-            log.error(u"IBB transfer failed: {}".format(failure.value))
+            log.error("IBB transfer failed: {}".format(failure.value))
         self.terminateStream(session_data, client, "IQ_ERROR")
 
     def terminateStream(self, session_data, client, failure_reason=None):
@@ -366,8 +366,8 @@
         self._killSession(session_data["id"], client, failure_reason)
 
 
+@implementer(iwokkel.IDisco)
 class XEP_0047_handler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def __init__(self, parent):
         self.plugin_parent = parent