diff sat/plugins/plugin_xep_0077.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 8018cf9aa55b
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0077.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0077.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 xep-0077
@@ -51,7 +51,7 @@
     namespace = 'jabber:client'
 
     def __init__(self, jid_, password, email=None, check_certificate=True):
-        log.debug(_(u"Registration asked for {jid}").format(jid=jid_))
+        log.debug(_("Registration asked for {jid}").format(jid=jid_))
         xmlstream.ConnectAuthenticator.__init__(self, jid_.host)
         self.jid = jid_
         self.password = password
@@ -70,18 +70,18 @@
         xs.initializers.append(tls_init)
 
     def register(self, xmlstream):
-        log.debug(_(u"Stream started with {server}, now registering"
+        log.debug(_("Stream started with {server}, now registering"
                     .format(server=self.jid.host)))
         iq = XEP_0077.buildRegisterIQ(self.xmlstream, self.jid, self.password, self.email)
         d = iq.send(self.jid.host).addCallbacks(self.registrationCb, self.registrationEb)
         d.chainDeferred(self.registered)
 
     def registrationCb(self, answer):
-        log.debug(_(u"Registration answer: {}").format(answer.toXml()))
+        log.debug(_("Registration answer: {}").format(answer.toXml()))
         self.xmlstream.sendFooter()
 
     def registrationEb(self, failure_):
-        log.info(_("Registration failure: {}").format(unicode(failure_.value)))
+        log.info(_("Registration failure: {}").format(str(failure_.value)))
         self.xmlstream.sendFooter()
         raise failure_
 
@@ -97,7 +97,7 @@
 
     def _disconnected(self, reason):
         if not self.authenticator.registered.called:
-            err = jabber_error.StreamError(u"Server unexpectedly closed the connection")
+            err = jabber_error.StreamError("Server unexpectedly closed the connection")
             try:
                 if reason.value.args[0][0][2] == "certificate verify failed":
                     err = exceptions.InvalidCertificate()
@@ -116,7 +116,7 @@
             in_sign="ss",
             out_sign="",
             method=self._inBandRegister,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "inBandAccountNew",
@@ -124,7 +124,7 @@
             in_sign="ssssi",
             out_sign="",
             method=self._registerNewAccount,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "inBandUnregister",
@@ -132,7 +132,7 @@
             in_sign="ss",
             out_sign="",
             method=self._unregister,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "inBandPasswordChange",
@@ -140,7 +140,7 @@
             in_sign="ss",
             out_sign="",
             method=self._changePassword,
-            async=True,
+            async_=True,
         )
 
     @staticmethod
@@ -160,12 +160,12 @@
     def _regCb(self, answer, client, post_treat_cb):
         """Called after the first get IQ"""
         try:
-            query_elt = answer.elements(NS_REG, "query").next()
+            query_elt = next(answer.elements(NS_REG, "query"))
         except StopIteration:
             raise exceptions.DataError("Can't find expected query element")
 
         try:
-            x_elem = query_elt.elements(data_form.NS_X_DATA, "x").next()
+            x_elem = next(query_elt.elements(data_form.NS_X_DATA, "x"))
         except StopIteration:
             # XXX: it seems we have an old service which doesn't manage data forms
             log.warning(_("Can't find data form"))
@@ -194,17 +194,17 @@
 
     def _regEb(self, failure, client):
         """Called when something is wrong with registration"""
-        log.info(_("Registration failure: %s") % unicode(failure.value))
+        log.info(_("Registration failure: %s") % str(failure.value))
         raise failure
 
     def _regSuccess(self, answer, client, post_treat_cb):
-        log.debug(_(u"registration answer: %s") % answer.toXml())
+        log.debug(_("registration answer: %s") % answer.toXml())
         if post_treat_cb is not None:
             post_treat_cb(jid.JID(answer["from"]), client.profile)
         return {}
 
     def _regFailure(self, failure, client):
-        log.info(_(u"Registration failure: %s") % unicode(failure.value))
+        log.info(_("Registration failure: %s") % str(failure.value))
         if failure.value.condition == "conflict":
             raise exceptions.ConflictError(
                 _("Username already exists, please choose an other one")
@@ -221,8 +221,8 @@
         """
         # FIXME: this post_treat_cb arguments seems wrong, check it
         client = self.host.getClient(profile_key)
-        log.debug(_(u"Asking registration for {}").format(to_jid.full()))
-        reg_request = client.IQ(u"get")
+        log.debug(_("Asking registration for {}").format(to_jid.full()))
+        reg_request = client.IQ("get")
         reg_request["from"] = client.jid.full()
         reg_request["to"] = to_jid.full()
         reg_request.addElement("query", NS_REG)
@@ -245,7 +245,7 @@
         return self.registerNewAccount(jid.JID(jid_), password, **kwargs)
 
     def registerNewAccount(
-        self, jid_, password, email=None, host=u"127.0.0.1", port=C.XMPP_C2S_PORT
+        self, jid_, password, email=None, host="127.0.0.1", port=C.XMPP_C2S_PORT
     ):
         """register a new account on a XMPP server
 
@@ -255,7 +255,7 @@
         @param host(unicode): host of the server to register to
         @param port(int): port of the server to register to
         """
-        check_certificate = host != u"127.0.0.1"
+        check_certificate = host != "127.0.0.1"
         authenticator = RegisteringAuthenticator(
             jid_, password, email, check_certificate=check_certificate)
         registered_d = authenticator.registered
@@ -290,6 +290,6 @@
         """
         iq_elt = client.IQ()
         iq_elt["to"] = to_jid.full()
-        query_elt = iq_elt.addElement((NS_REG, u"query"))
-        query_elt.addElement(u"remove")
+        query_elt = iq_elt.addElement((NS_REG, "query"))
+        query_elt.addElement("remove")
         return iq_elt.send()