annotate src/plugins/plugin_misc_register_account.py @ 2450:124f5db998f2

jp (merge-request): first draft: Introduce a new "merge-request" command to handle merge request creation/sending.
author Goffi <goffi@goffi.org>
date Thu, 30 Nov 2017 20:52:33 +0100
parents 8b37a62336c3
children 0046283a285d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
3
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SàT plugin for registering a new XMPP account
2414
8b37a62336c3 misc: date update (yes it's a bit late :p )
Goffi <goffi@goffi.org>
parents: 2175
diff changeset
5 # Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
7
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
12
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
17
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
20
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
21 from sat.core.i18n import _, D_
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from sat.core.log import getLogger
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
23 log = getLogger(__name__)
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
24 from sat.core.constants import Const as C
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
25 from twisted.words.protocols.jabber import jid
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
26 from sat.memory.memory import Sessions
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
27 from sat.tools import xml_tools
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
28 from sat.tools.xml_tools import SAT_FORM_PREFIX, SAT_PARAM_SEPARATOR
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
29
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
30
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
31 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
32 C.PI_NAME: "Register Account Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
33 C.PI_IMPORT_NAME: "REGISTER-ACCOUNT",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
34 C.PI_TYPE: "MISC",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
35 C.PI_PROTOCOLS: [],
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
36 C.PI_DEPENDENCIES: ["XEP-0077"],
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
37 C.PI_RECOMMENDATIONS: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
38 C.PI_MAIN: "RegisterAccount",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
39 C.PI_HANDLER: "no",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
40 C.PI_DESCRIPTION: _(u"""Register XMPP account""")
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
41 }
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
42
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
43
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
44 class RegisterAccount(object):
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
45 # FIXME: this plugin is messy and difficult to read, it needs to be cleaned up and documented
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
46
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
47 def __init__(self, host):
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
48 log.info(_(u"Plugin Register Account initialization"))
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
49 self.host = host
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
50 self._sessions = Sessions()
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
51 host.registerCallback(self.registerNewAccountCB, with_data=True, force_id="registerNewAccount")
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
52 self.__register_account_id = host.registerCallback(self._registerConfirmation, with_data=True)
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
53
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
54 def registerNewAccountCB(self, data, profile):
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
55 """Called when the user click on the "New account" button."""
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
56 session_data = {}
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
57
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
58 # FIXME: following loop is overcomplicated, hard to read
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
59 # FIXME: while used with parameters, hashed password is used and overwrite clear one
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
60 for param in (u'JabberID', u'Password', C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
61 try:
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
62 session_data[param] = data[SAT_FORM_PREFIX + u"Connection" + SAT_PARAM_SEPARATOR + param]
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
63 except KeyError:
1089
8e0072754413 core, plugins, stdui, frontends: fixes handling of "Force server" and "Force port" parameters:
souliane <souliane@mailoo.org>
parents: 1088
diff changeset
64 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
8e0072754413 core, plugins, stdui, frontends: fixes handling of "Force server" and "Force port" parameters:
souliane <souliane@mailoo.org>
parents: 1088
diff changeset
65 session_data[param] = ''
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
66
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
67 for param in (u'JabberID', u'Password'):
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
68 if not session_data[param]:
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
69 form_ui = xml_tools.XMLUI(u"popup", title=D_(u"Missing values"))
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
70 form_ui.addText(D_(u"No user JID or password given: can't register new account."))
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
71 return {u'xmlui': form_ui.toXml()}
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
72
1089
8e0072754413 core, plugins, stdui, frontends: fixes handling of "Force server" and "Force port" parameters:
souliane <souliane@mailoo.org>
parents: 1088
diff changeset
73 session_data['user'], host, resource = jid.parse(session_data['JabberID'])
8e0072754413 core, plugins, stdui, frontends: fixes handling of "Force server" and "Force port" parameters:
souliane <souliane@mailoo.org>
parents: 1088
diff changeset
74 session_data['server'] = session_data[C.FORCE_SERVER_PARAM] or host
1212
628e320eab1f memory: Sessions.newSession can be called with a forced session ID
souliane <souliane@mailoo.org>
parents: 1089
diff changeset
75 session_id, dummy = self._sessions.newSession(session_data, profile=profile)
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
76 form_ui = xml_tools.XMLUI("form", title=D_("Register new account"), submit_id=self.__register_account_id, session_id=session_id)
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
77 form_ui.addText(D_(u"Do you want to register a new XMPP account {jid}?").format(
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
78 jid = session_data['JabberID']))
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
79 return {'xmlui': form_ui.toXml()}
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
80
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
81 def _registerConfirmation(self, data, profile):
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
82 """Save the related parameters and proceed the registration."""
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
83 session_data = self._sessions.profileGet(data['session_id'], profile)
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
84
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
85 self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile)
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
86 self.host.memory.setParam("Password", session_data["Password"], "Connection", profile_key=profile)
1088
b29452cab50b core, memory, plugins, stdui, frontends: look for DNS SRV records when server is not specified:
souliane <souliane@mailoo.org>
parents: 1044
diff changeset
87 self.host.memory.setParam(C.FORCE_SERVER_PARAM, session_data[C.FORCE_SERVER_PARAM], "Connection", profile_key=profile)
b29452cab50b core, memory, plugins, stdui, frontends: look for DNS SRV records when server is not specified:
souliane <souliane@mailoo.org>
parents: 1044
diff changeset
88 self.host.memory.setParam(C.FORCE_PORT_PARAM, session_data[C.FORCE_PORT_PARAM], "Connection", profile_key=profile)
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
89
2175
75002ac33801 plugin XEP-0077: added changePassword and removeRegistration methods
Goffi <goffi@goffi.org>
parents: 2172
diff changeset
90 d = self._registerNewAccount(jid.JID(session_data['JabberID']), session_data["Password"], None, session_data['server'])
1044
85c110c0be86 plugins misc_account, misc_register_account: manually delete the sessions when not needed anymore
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
91 del self._sessions[data['session_id']]
85c110c0be86 plugins misc_account, misc_register_account: manually delete the sessions when not needed anymore
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
92 return d
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
93
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
94 def _registerNewAccount(self, client, jid_, password, email, server):
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
95 # FIXME: port is not set here
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
96 def registeredCb(dummy):
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
97 xmlui = xml_tools.XMLUI(u"popup", title=D_(u"Confirmation"))
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
98 xmlui.addText(D_("Registration successful."))
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
99 return ({'xmlui': xmlui.toXml()})
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
100
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
101 def registeredEb(failure):
1037
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
102 xmlui = xml_tools.XMLUI("popup", title=D_("Failure"))
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
103 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage())
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
104 try:
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
105 if failure.value.condition == 'conflict':
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
106 xmlui.addText(D_("Username already exists, please choose an other one."))
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
107 except AttributeError:
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
108 pass
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
109 return ({'xmlui': xmlui.toXml()})
6e975c6b0faf core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:
souliane <souliane@mailoo.org>
parents:
diff changeset
110
2172
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
111 registered_d = self.host.plugins['XEP-0077'].registerNewAccount(client, jid_, password, email=email, host=server, port=C.XMPP_C2S_PORT)
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
112 registered_d.addCallbacks(registeredCb, registeredEb)
545a1261ac3b core, plugin XEP-0077: in-band registration fix and move:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
113 return registered_d