diff sat/plugins/plugin_xep_0077.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 6c89cf856d28
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0077.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat/plugins/plugin_xep_0077.py	Wed Jun 27 20:14:46 2018 +0200
@@ -21,6 +21,7 @@
 from sat.core.constants import Const as C
 from sat.core import exceptions
 from sat.core.log import getLogger
+
 log = getLogger(__name__)
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber import xmlstream
@@ -29,7 +30,7 @@
 
 from wokkel import data_form
 
-NS_REG = 'jabber:iq:register'
+NS_REG = "jabber:iq:register"
 
 PLUGIN_INFO = {
     C.PI_NAME: "XEP 0077 Plugin",
@@ -38,11 +39,12 @@
     C.PI_PROTOCOLS: ["XEP-0077"],
     C.PI_DEPENDENCIES: [],
     C.PI_MAIN: "XEP_0077",
-    C.PI_DESCRIPTION: _("""Implementation of in-band registration""")
+    C.PI_DESCRIPTION: _("""Implementation of in-band registration"""),
 }
 
 # FIXME: this implementation is incomplete
 
+
 class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
     # FIXME: request IQ is not send to check available fields, while XEP recommand to use it
     # FIXME: doesn't handle data form or oob
@@ -53,8 +55,7 @@
         self.password = password
         self.email = email
         self.registered = defer.Deferred()
-        log.debug(_(u"Registration asked for {jid}").format(
-            jid = jid_))
+        log.debug(_(u"Registration asked for {jid}").format(jid=jid_))
 
     def connectionMade(self):
         log.debug(_(u"Connection made with {server}".format(server=self.jid.host)))
@@ -77,57 +78,78 @@
 
 
 class XEP_0077(object):
-
     def __init__(self, host):
         log.info(_("Plugin XEP_0077 initialization"))
         self.host = host
-        host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='',
-                              method=self._inBandRegister,
-                              async=True)
-        host.bridge.addMethod("inBandAccountNew", ".plugin", in_sign='ssssi', out_sign='',
-                              method=self._registerNewAccount,
-                              async=True)
-        host.bridge.addMethod("inBandUnregister", ".plugin", in_sign='ss', out_sign='',
-                              method=self._unregister,
-                              async=True)
-        host.bridge.addMethod("inBandPasswordChange", ".plugin", in_sign='ss', out_sign='',
-                              method=self._changePassword,
-                              async=True)
+        host.bridge.addMethod(
+            "inBandRegister",
+            ".plugin",
+            in_sign="ss",
+            out_sign="",
+            method=self._inBandRegister,
+            async=True,
+        )
+        host.bridge.addMethod(
+            "inBandAccountNew",
+            ".plugin",
+            in_sign="ssssi",
+            out_sign="",
+            method=self._registerNewAccount,
+            async=True,
+        )
+        host.bridge.addMethod(
+            "inBandUnregister",
+            ".plugin",
+            in_sign="ss",
+            out_sign="",
+            method=self._unregister,
+            async=True,
+        )
+        host.bridge.addMethod(
+            "inBandPasswordChange",
+            ".plugin",
+            in_sign="ss",
+            out_sign="",
+            method=self._changePassword,
+            async=True,
+        )
 
     @staticmethod
     def buildRegisterIQ(xmlstream_, jid_, password, email=None):
-        iq_elt = xmlstream.IQ(xmlstream_, 'set')
+        iq_elt = xmlstream.IQ(xmlstream_, "set")
         iq_elt["to"] = jid_.host
-        query_elt = iq_elt.addElement(('jabber:iq:register', 'query'))
-        username_elt = query_elt.addElement('username')
+        query_elt = iq_elt.addElement(("jabber:iq:register", "query"))
+        username_elt = query_elt.addElement("username")
         username_elt.addContent(jid_.user)
-        password_elt = query_elt.addElement('password')
+        password_elt = query_elt.addElement("password")
         password_elt.addContent(password)
         if email is not None:
-            email_elt = query_elt.addElement('email')
+            email_elt = query_elt.addElement("email")
             email_elt.addContent(email)
         return iq_elt
 
     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 = answer.elements(NS_REG, "query").next()
         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 = query_elt.elements(data_form.NS_X_DATA, "x").next()
         except StopIteration:
             # XXX: it seems we have an old service which doesn't manage data forms
             log.warning(_("Can't find data form"))
-            raise exceptions.DataError(_("This gateway can't be managed by SàT, sorry :("))
+            raise exceptions.DataError(
+                _("This gateway can't be managed by SàT, sorry :(")
+            )
 
         def submitForm(data, profile):
             form_elt = xml_tools.XMLUIResultToElt(data)
 
             iq_elt = client.IQ()
-            iq_elt['id'] = answer['id']
-            iq_elt['to'] = answer['from']
+            iq_elt["id"] = answer["id"]
+            iq_elt["to"] = answer["from"]
             query_elt = iq_elt.addElement("query", NS_REG)
             query_elt.addChild(form_elt)
             d = iq_elt.send()
@@ -136,7 +158,9 @@
             return d
 
         form = data_form.Form.fromElement(x_elem)
-        submit_reg_id = self.host.registerCallback(submitForm, with_data=True, one_shot=True)
+        submit_reg_id = self.host.registerCallback(
+            submitForm, with_data=True, one_shot=True
+        )
         return xml_tools.dataForm2XMLUI(form, submit_reg_id)
 
     def _regEb(self, failure, client):
@@ -147,13 +171,15 @@
     def _regSuccess(self, answer, client, post_treat_cb):
         log.debug(_(u"registration answer: %s") % answer.toXml())
         if post_treat_cb is not None:
-            post_treat_cb(jid.JID(answer['from']), client.profile)
+            post_treat_cb(jid.JID(answer["from"]), client.profile)
         return {}
 
     def _regFailure(self, failure, client):
         log.info(_(u"Registration failure: %s") % unicode(failure.value))
-        if failure.value.condition == 'conflict':
-            raise exceptions.ConflictError( _("Username already exists, please choose an other one"))
+        if failure.value.condition == "conflict":
+            raise exceptions.ConflictError(
+                _("Username already exists, please choose an other one")
+            )
         raise failure
 
     def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE):
@@ -167,24 +193,31 @@
         # 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')
+        reg_request = client.IQ(u"get")
         reg_request["from"] = client.jid.full()
         reg_request["to"] = to_jid.full()
-        reg_request.addElement('query', NS_REG)
-        d = reg_request.send(to_jid.full()).addCallbacks(self._regCb, self._regEb, callbackArgs=[client, post_treat_cb], errbackArgs=[client])
+        reg_request.addElement("query", NS_REG)
+        d = reg_request.send(to_jid.full()).addCallbacks(
+            self._regCb,
+            self._regEb,
+            callbackArgs=[client, post_treat_cb],
+            errbackArgs=[client],
+        )
         return d
 
     def _registerNewAccount(self, jid_, password, email, host, port):
         kwargs = {}
         if email:
-            kwargs['email'] = email
+            kwargs["email"] = email
         if host:
-            kwargs['host'] = host
+            kwargs["host"] = host
         if port:
-            kwargs['port'] = port
+            kwargs["port"] = port
         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):
+    def registerNewAccount(
+        self, jid_, password, email=None, host=u"127.0.0.1", port=C.XMPP_C2S_PORT
+    ):
         """register a new account on a XMPP server
 
         @param jid_(jid.JID): request jid to register
@@ -207,7 +240,11 @@
     def changePassword(self, client, new_password):
         iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password)
         d = iq_elt.send(client.jid.host)
-        d.addCallback(lambda dummy: self.host.memory.setParam("Password", new_password, "Connection", profile_key=client.profile))
+        d.addCallback(
+            lambda dummy: self.host.memory.setParam(
+                "Password", new_password, "Connection", profile_key=client.profile
+            )
+        )
         return d
 
     def _unregister(self, to_jid_s, profile_key):
@@ -222,7 +259,7 @@
         @param to_jid(jid.JID): jid of the service or server
         """
         iq_elt = client.IQ()
-        iq_elt['to'] = to_jid.full()
-        query_elt = iq_elt.addElement((NS_REG, u'query'))
-        query_elt.addElement(u'remove')
+        iq_elt["to"] = to_jid.full()
+        query_elt = iq_elt.addElement((NS_REG, u"query"))
+        query_elt.addElement(u"remove")
         return iq_elt.send()