comparison libervia/backend/plugins/plugin_misc_register_account.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_misc_register_account.py@524856bd7b19
children
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SàT plugin for registering a new XMPP account
5 # Copyright (C) 2009-2021 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 libervia.backend.core.i18n import _, D_
22 from libervia.backend.core.log import getLogger
23
24 log = getLogger(__name__)
25 from libervia.backend.core.constants import Const as C
26 from twisted.words.protocols.jabber import jid
27 from libervia.backend.memory.memory import Sessions
28 from libervia.backend.tools import xml_tools
29 from libervia.backend.tools.xml_tools import SAT_FORM_PREFIX, SAT_PARAM_SEPARATOR
30
31
32 PLUGIN_INFO = {
33 C.PI_NAME: "Register Account Plugin",
34 C.PI_IMPORT_NAME: "REGISTER-ACCOUNT",
35 C.PI_TYPE: "MISC",
36 C.PI_PROTOCOLS: [],
37 C.PI_DEPENDENCIES: ["XEP-0077"],
38 C.PI_RECOMMENDATIONS: [],
39 C.PI_MAIN: "RegisterAccount",
40 C.PI_HANDLER: "no",
41 C.PI_DESCRIPTION: _("""Register XMPP account"""),
42 }
43
44
45 class RegisterAccount(object):
46 # FIXME: this plugin is messy and difficult to read, it needs to be cleaned up and documented
47
48 def __init__(self, host):
49 log.info(_("Plugin Register Account initialization"))
50 self.host = host
51 self._sessions = Sessions()
52 host.register_callback(
53 self.register_new_account_cb, with_data=True, force_id="register_new_account"
54 )
55 self.__register_account_id = host.register_callback(
56 self._register_confirmation, with_data=True
57 )
58
59 def register_new_account_cb(self, data, profile):
60 """Called when the user click on the "New account" button."""
61 session_data = {}
62
63 # FIXME: following loop is overcomplicated, hard to read
64 # FIXME: while used with parameters, hashed password is used and overwrite clear one
65 for param in ("JabberID", "Password", C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
66 try:
67 session_data[param] = data[
68 SAT_FORM_PREFIX + "Connection" + SAT_PARAM_SEPARATOR + param
69 ]
70 except KeyError:
71 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
72 session_data[param] = ""
73
74 for param in ("JabberID", "Password"):
75 if not session_data[param]:
76 form_ui = xml_tools.XMLUI("popup", title=D_("Missing values"))
77 form_ui.addText(
78 D_("No user JID or password given: can't register new account.")
79 )
80 return {"xmlui": form_ui.toXml()}
81
82 session_data["user"], host, resource = jid.parse(session_data["JabberID"])
83 session_data["server"] = session_data[C.FORCE_SERVER_PARAM] or host
84 session_id, __ = self._sessions.new_session(session_data, profile=profile)
85 form_ui = xml_tools.XMLUI(
86 "form",
87 title=D_("Register new account"),
88 submit_id=self.__register_account_id,
89 session_id=session_id,
90 )
91 form_ui.addText(
92 D_("Do you want to register a new XMPP account {jid}?").format(
93 jid=session_data["JabberID"]
94 )
95 )
96 return {"xmlui": form_ui.toXml()}
97
98 def _register_confirmation(self, data, profile):
99 """Save the related parameters and proceed the registration."""
100 session_data = self._sessions.profile_get(data["session_id"], profile)
101
102 self.host.memory.param_set(
103 "JabberID", session_data["JabberID"], "Connection", profile_key=profile
104 )
105 self.host.memory.param_set(
106 "Password", session_data["Password"], "Connection", profile_key=profile
107 )
108 self.host.memory.param_set(
109 C.FORCE_SERVER_PARAM,
110 session_data[C.FORCE_SERVER_PARAM],
111 "Connection",
112 profile_key=profile,
113 )
114 self.host.memory.param_set(
115 C.FORCE_PORT_PARAM,
116 session_data[C.FORCE_PORT_PARAM],
117 "Connection",
118 profile_key=profile,
119 )
120
121 d = self._register_new_account(
122 jid.JID(session_data["JabberID"]),
123 session_data["Password"],
124 None,
125 session_data["server"],
126 )
127 del self._sessions[data["session_id"]]
128 return d
129
130 def _register_new_account(self, client, jid_, password, email, server):
131 #  FIXME: port is not set here
132 def registered_cb(__):
133 xmlui = xml_tools.XMLUI("popup", title=D_("Confirmation"))
134 xmlui.addText(D_("Registration successful."))
135 return {"xmlui": xmlui.toXml()}
136
137 def registered_eb(failure):
138 xmlui = xml_tools.XMLUI("popup", title=D_("Failure"))
139 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage())
140 try:
141 if failure.value.condition == "conflict":
142 xmlui.addText(
143 D_("Username already exists, please choose an other one.")
144 )
145 except AttributeError:
146 pass
147 return {"xmlui": xmlui.toXml()}
148
149 registered_d = self.host.plugins["XEP-0077"].register_new_account(
150 client, jid_, password, email=email, host=server, port=C.XMPP_C2S_PORT
151 )
152 registered_d.addCallbacks(registered_cb, registered_eb)
153 return registered_d