comparison sat/plugins/plugin_misc_register_account.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_misc_register_account.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SàT plugin for registering a new XMPP account
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 from sat.core.i18n import _, D_
22 from sat.core.log import getLogger
23 log = getLogger(__name__)
24 from sat.core.constants import Const as C
25 from twisted.words.protocols.jabber import jid
26 from sat.memory.memory import Sessions
27 from sat.tools import xml_tools
28 from sat.tools.xml_tools import SAT_FORM_PREFIX, SAT_PARAM_SEPARATOR
29
30
31 PLUGIN_INFO = {
32 C.PI_NAME: "Register Account Plugin",
33 C.PI_IMPORT_NAME: "REGISTER-ACCOUNT",
34 C.PI_TYPE: "MISC",
35 C.PI_PROTOCOLS: [],
36 C.PI_DEPENDENCIES: ["XEP-0077"],
37 C.PI_RECOMMENDATIONS: [],
38 C.PI_MAIN: "RegisterAccount",
39 C.PI_HANDLER: "no",
40 C.PI_DESCRIPTION: _(u"""Register XMPP account""")
41 }
42
43
44 class RegisterAccount(object):
45 # FIXME: this plugin is messy and difficult to read, it needs to be cleaned up and documented
46
47 def __init__(self, host):
48 log.info(_(u"Plugin Register Account initialization"))
49 self.host = host
50 self._sessions = Sessions()
51 host.registerCallback(self.registerNewAccountCB, with_data=True, force_id="registerNewAccount")
52 self.__register_account_id = host.registerCallback(self._registerConfirmation, with_data=True)
53
54 def registerNewAccountCB(self, data, profile):
55 """Called when the user click on the "New account" button."""
56 session_data = {}
57
58 # FIXME: following loop is overcomplicated, hard to read
59 # FIXME: while used with parameters, hashed password is used and overwrite clear one
60 for param in (u'JabberID', u'Password', C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
61 try:
62 session_data[param] = data[SAT_FORM_PREFIX + u"Connection" + SAT_PARAM_SEPARATOR + param]
63 except KeyError:
64 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
65 session_data[param] = ''
66
67 for param in (u'JabberID', u'Password'):
68 if not session_data[param]:
69 form_ui = xml_tools.XMLUI(u"popup", title=D_(u"Missing values"))
70 form_ui.addText(D_(u"No user JID or password given: can't register new account."))
71 return {u'xmlui': form_ui.toXml()}
72
73 session_data['user'], host, resource = jid.parse(session_data['JabberID'])
74 session_data['server'] = session_data[C.FORCE_SERVER_PARAM] or host
75 session_id, dummy = self._sessions.newSession(session_data, profile=profile)
76 form_ui = xml_tools.XMLUI("form", title=D_("Register new account"), submit_id=self.__register_account_id, session_id=session_id)
77 form_ui.addText(D_(u"Do you want to register a new XMPP account {jid}?").format(
78 jid = session_data['JabberID']))
79 return {'xmlui': form_ui.toXml()}
80
81 def _registerConfirmation(self, data, profile):
82 """Save the related parameters and proceed the registration."""
83 session_data = self._sessions.profileGet(data['session_id'], profile)
84
85 self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile)
86 self.host.memory.setParam("Password", session_data["Password"], "Connection", profile_key=profile)
87 self.host.memory.setParam(C.FORCE_SERVER_PARAM, session_data[C.FORCE_SERVER_PARAM], "Connection", profile_key=profile)
88 self.host.memory.setParam(C.FORCE_PORT_PARAM, session_data[C.FORCE_PORT_PARAM], "Connection", profile_key=profile)
89
90 d = self._registerNewAccount(jid.JID(session_data['JabberID']), session_data["Password"], None, session_data['server'])
91 del self._sessions[data['session_id']]
92 return d
93
94 def _registerNewAccount(self, client, jid_, password, email, server):
95 # FIXME: port is not set here
96 def registeredCb(dummy):
97 xmlui = xml_tools.XMLUI(u"popup", title=D_(u"Confirmation"))
98 xmlui.addText(D_("Registration successful."))
99 return ({'xmlui': xmlui.toXml()})
100
101 def registeredEb(failure):
102 xmlui = xml_tools.XMLUI("popup", title=D_("Failure"))
103 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage())
104 try:
105 if failure.value.condition == 'conflict':
106 xmlui.addText(D_("Username already exists, please choose an other one."))
107 except AttributeError:
108 pass
109 return ({'xmlui': xmlui.toXml()})
110
111 registered_d = self.host.plugins['XEP-0077'].registerNewAccount(client, jid_, password, email=email, host=server, port=C.XMPP_C2S_PORT)
112 registered_d.addCallbacks(registeredCb, registeredEb)
113 return registered_d