diff src/plugins/plugin_misc_register_account.py @ 2172:545a1261ac3b

core, plugin XEP-0077: in-band registration fix and move: in-band was partially in core for historical reason, it has been moved to XEP-0077, and fixed. It is still incomplete, but should work for basic accounts creation.
author Goffi <goffi@goffi.org>
date Wed, 08 Mar 2017 20:59:31 +0100
parents 33c8c4973743
children 75002ac33801
line wrap: on
line diff
--- a/src/plugins/plugin_misc_register_account.py	Wed Mar 08 20:44:28 2017 +0100
+++ b/src/plugins/plugin_misc_register_account.py	Wed Mar 08 20:59:31 2017 +0100
@@ -22,10 +22,8 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.core.constants import Const as C
-from twisted.words.protocols.jabber import jid, xmlstream
-from sat.core import xmpp
+from twisted.words.protocols.jabber import jid
 from sat.memory.memory import Sessions
-from twisted.internet import reactor, defer
 from sat.tools import xml_tools
 from sat.tools.xml_tools import SAT_FORM_PREFIX, SAT_PARAM_SEPARATOR
 
@@ -35,7 +33,7 @@
     C.PI_IMPORT_NAME: "REGISTER-ACCOUNT",
     C.PI_TYPE: "MISC",
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: [],
+    C.PI_DEPENDENCIES: ["XEP-0077"],
     C.PI_RECOMMENDATIONS: [],
     C.PI_MAIN: "RegisterAccount",
     C.PI_HANDLER: "no",
@@ -44,6 +42,7 @@
 
 
 class RegisterAccount(object):
+    # FIXME: this plugin is messy and difficult to read, it needs to be cleaned up and documented
 
     def __init__(self, host):
         log.info(_(u"Plugin Register Account initialization"))
@@ -53,30 +52,35 @@
         self.__register_account_id = host.registerCallback(self._registerConfirmation, with_data=True)
 
     def registerNewAccountCB(self, data, profile):
-        """Called when the use click on the "New account" button."""
+        """Called when the user click on the "New account" button."""
         session_data = {}
-        for param in ('JabberID', 'Password', C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
+
+        # FIXME: following loop is overcomplicated, hard to read
+        # FIXME: while used with parameters, hashed password is used and overwrite clear one
+        for param in (u'JabberID', u'Password', C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
             try:
-                session_data[param] = data["%s%s%s%s" % (SAT_FORM_PREFIX, "Connection", SAT_PARAM_SEPARATOR, param)]
+                session_data[param] = data[SAT_FORM_PREFIX + u"Connection" + SAT_PARAM_SEPARATOR + param]
             except KeyError:
                 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
                     session_data[param] = ''
 
-        for param in ('JabberID', 'Password'):
+        for param in (u'JabberID', u'Password'):
             if not session_data[param]:
-                form_ui = xml_tools.XMLUI("popup", title=D_("Missing values"))
-                form_ui.addText(D_("No user JID or password given: can't register new account."))
-                return  {'xmlui': form_ui.toXml()}
+                form_ui = xml_tools.XMLUI(u"popup", title=D_(u"Missing values"))
+                form_ui.addText(D_(u"No user JID or password given: can't register new account."))
+                return  {u'xmlui': form_ui.toXml()}
 
         session_data['user'], host, resource = jid.parse(session_data['JabberID'])
         session_data['server'] = session_data[C.FORCE_SERVER_PARAM] or host
         session_id, dummy = self._sessions.newSession(session_data, profile=profile)
         form_ui = xml_tools.XMLUI("form", title=D_("Register new account"), submit_id=self.__register_account_id, session_id=session_id)
-        form_ui.addText(D_("Do you want to register a new XMPP account [%(user)s] on server %(server)s ?") % {'user': session_data['user'], 'server': session_data['server']})
+        form_ui.addText(D_(u"Do you want to register a new XMPP account {jid}?").format(
+            jid = session_data['JabberID']))
         return  {'xmlui': form_ui.toXml()}
 
     def _registerConfirmation(self, data, profile):
         """Save the related parameters and proceed the registration."""
+        client = self.host.getClient(profile)
         session_data = self._sessions.profileGet(data['session_id'], profile)
 
         self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile)
@@ -84,32 +88,18 @@
         self.host.memory.setParam(C.FORCE_SERVER_PARAM, session_data[C.FORCE_SERVER_PARAM], "Connection", profile_key=profile)
         self.host.memory.setParam(C.FORCE_PORT_PARAM, session_data[C.FORCE_PORT_PARAM], "Connection", profile_key=profile)
 
-        d = self._registerNewAccount(session_data['user'], session_data["Password"], None, session_data['server'], profile_key=profile)
+        d = self._registerNewAccount(client, jid.JID(session_data['JabberID']), session_data["Password"], None, session_data['server'])
         del self._sessions[data['session_id']]
         return d
 
-    def _registerNewAccount(self, user, password, email, host, port=C.XMPP_C2S_PORT, profile_key=C.PROF_KEY_NONE):
-        """Connect to a server and create a new account using in-band registration.
-        @param user: login of the account
-        @param password: password of the account
-        @param email: email of the account
-        @param host: host of the server to register to
-        @param port: port of the server to register to
-        @param profile_key: %(doc_profile_key)s
-        """
-        profile = self.host.memory.getProfileName(profile_key)
-
-        d = defer.Deferred()
-        serverRegistrer = xmlstream.XmlStreamFactory(xmpp.RegisteringAuthenticator(self, host, user, password, email, d, profile))
-        connector = reactor.connectTCP(host, port or C.XMPP_C2S_PORT, serverRegistrer)
-        serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect()
-
-        def cb(dummy):
-            xmlui = xml_tools.XMLUI("popup", title=D_("Confirmation"))
+    def _registerNewAccount(self, client, jid_, password, email, server):
+        # FIXME: port is not set here
+        def registeredCb(dummy):
+            xmlui = xml_tools.XMLUI(u"popup", title=D_(u"Confirmation"))
             xmlui.addText(D_("Registration successful."))
             return ({'xmlui': xmlui.toXml()})
 
-        def eb(failure):
+        def registeredEb(failure):
             xmlui = xml_tools.XMLUI("popup", title=D_("Failure"))
             xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage())
             try:
@@ -119,5 +109,6 @@
                 pass
             return ({'xmlui': xmlui.toXml()})
 
-        d.addCallbacks(cb, eb)
-        return d
+        registered_d = self.host.plugins['XEP-0077'].registerNewAccount(client, jid_, password, email=email, host=server, port=C.XMPP_C2S_PORT)
+        registered_d.addCallbacks(registeredCb, registeredEb)
+        return registered_d