diff sat/plugins/plugin_xep_0300.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 003b8b4b56a7
children fee60f17ebac
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0300.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0300.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 Hash functions (XEP-0300)
@@ -27,7 +27,7 @@
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
 from twisted.internet import threads
 from twisted.internet import defer
-from zope.interface import implements
+from zope.interface import implementer
 from wokkel import disco, iwokkel
 from collections import OrderedDict
 import hashlib
@@ -46,7 +46,7 @@
 }
 
 NS_HASHES = "urn:xmpp:hashes:2"
-NS_HASHES_FUNCTIONS = u"urn:xmpp:hash-function-text-names:{}"
+NS_HASHES_FUNCTIONS = "urn:xmpp:hash-function-text-names:{}"
 BUFFER_SIZE = 2 ** 12
 ALGO_DEFAULT = "sha-256"
 
@@ -55,10 +55,10 @@
     # TODO: add blake after moving to Python 3
     ALGOS = OrderedDict(
         (
-            (u"md5", hashlib.md5),
-            (u"sha-1", hashlib.sha1),
-            (u"sha-256", hashlib.sha256),
-            (u"sha-512", hashlib.sha512),
+            ("md5", hashlib.md5),
+            ("sha-1", hashlib.sha1),
+            ("sha-256", hashlib.sha256),
+            ("sha-512", hashlib.sha512),
         )
     )
 
@@ -98,7 +98,7 @@
             )
             if has_feature:
                 log.debug(
-                    u"Best hashing algorithm found for {jid}: {algo}".format(
+                    "Best hashing algorithm found for {jid}: {algo}".format(
                         jid=to_jid.full(), algo=algo
                     )
                 )
@@ -156,7 +156,7 @@
             hash_used_elt = next(parent.elements(NS_HASHES, "hash-used"))
         except StopIteration:
             raise exceptions.NotFound
-        algo = hash_used_elt[u"algo"]
+        algo = hash_used_elt["algo"]
         if not algo:
             raise exceptions.DataError
         return algo
@@ -172,7 +172,8 @@
         assert algo
         hash_elt = domish.Element((NS_HASHES, "hash"))
         if hash_ is not None:
-            hash_elt.addContent(base64.b64encode(hash_))
+            b64_hash = base64.b64encode(hash_.encode('utf-8')).decode('utf-8')
+            hash_elt.addContent(b64_hash)
         hash_elt["algo"] = algo
         return hash_elt
 
@@ -181,12 +182,12 @@
 
         if multiple elements are found, the strongest managed one is returned
         @param (domish.Element): parent of <hash/> element
-        @return (tuple[unicode, str]): (algo, hash) tuple
+        @return (tuple[str, bytes]): (algo, hash) tuple
             both values can be None if <hash/> is empty
         @raise exceptions.NotFound: the element is not present
         @raise exceptions.DataError: the element is invalid
         """
-        algos = XEP_0300.ALGOS.keys()
+        algos = list(XEP_0300.ALGOS.keys())
         hash_elt = None
         best_algo = None
         best_value = None
@@ -195,13 +196,13 @@
             try:
                 idx = algos.index(algo)
             except ValueError:
-                log.warning(u"Proposed {} algorithm is not managed".format(algo))
+                log.warning("Proposed {} algorithm is not managed".format(algo))
                 algo = None
                 continue
 
             if best_algo is None or algos.index(best_algo) < idx:
                 best_algo = algo
-                best_value = base64.b64decode(unicode(hash_elt))
+                best_value = base64.b64decode(str(hash_elt))
 
         if not hash_elt:
             raise exceptions.NotFound
@@ -210,8 +211,8 @@
         return best_algo, best_value
 
 
+@implementer(iwokkel.IDisco)
 class XEP_0300_handler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
         hash_functions_names = [