comparison src/plugins/plugin_misc_register_account.py @ 1037:6e975c6b0faf

core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin: - the bridge method has been removed, now a plugin takes care of it with XMLUI callback system - TODO: xmpp.RegisteringAuthenticator still needs to be fixed
author souliane <souliane@mailoo.org>
date Fri, 16 May 2014 00:58:20 +0200
parents
children 85c110c0be86
comparison
equal deleted inserted replaced
1036:35048cafb18d 1037:6e975c6b0faf
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SàT plugin for registering a new XMPP account
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013, 2014 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, xmlstream
26 from sat.core import xmpp
27 from sat.memory.memory import Sessions
28 from twisted.internet import reactor, defer
29 from sat.tools import xml_tools
30 from sat.tools.xml_tools import SAT_FORM_PREFIX, SAT_PARAM_SEPARATOR
31
32
33 PLUGIN_INFO = {
34 "name": "Register Account Plugin",
35 "import_name": "REGISTER-ACCOUNT",
36 "type": "MISC",
37 "protocols": [],
38 "dependencies": [],
39 "recommendations": [],
40 "main": "RegisterAccount",
41 "handler": "no",
42 "description": _(u"""Register XMPP account""")
43 }
44
45
46 class RegisterAccount(object):
47
48 def __init__(self, host):
49 log.info(_(u"Plugin Register Account initialization"))
50 self.host = host
51 self._sessions = Sessions()
52 host.registerCallback(self.registerNewAccountCB, with_data=True, force_id="registerNewAccount")
53 self.__register_account_id = host.registerCallback(self._registerConfirmation, with_data=True)
54
55 def registerNewAccountCB(self, data, profile):
56 """Called when the use click on the "New account" button."""
57 session_data = {}
58 for param in ('JabberID', 'Password', 'Port', 'Server'):
59 try:
60 session_data[param] = data["%s%s%s%s" % (SAT_FORM_PREFIX, "Connection", SAT_PARAM_SEPARATOR, param)]
61 except KeyError:
62 if param == 'Port':
63 session_data[param] = 5222
64
65 for param in ('JabberID', 'Password', 'Server'):
66 if not session_data[param]:
67 form_ui = xml_tools.XMLUI("popup", title=D_("Missing values"))
68 form_ui.addText(D_("No user, password or server given: can't register new account."))
69 return {'xmlui': form_ui.toXml()}
70
71 user = jid.parse(session_data['JabberID'])[0]
72 session_id, dummy = self._sessions.newSession(session_data, profile)
73 form_ui = xml_tools.XMLUI("form", title=D_("Register new account"), submit_id=self.__register_account_id, session_id=session_id)
74 form_ui.addText(D_("Do you want to register a new XMPP account [%(user)s] on server %(server)s ?") % {'user': user, 'server': session_data['Server']})
75 return {'xmlui': form_ui.toXml()}
76
77 def _registerConfirmation(self, data, profile):
78 """Save the related parameters and proceed the registration."""
79 session_data = self._sessions.profileGet(data['session_id'], profile)
80
81 self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile)
82 self.host.memory.setParam("Password", session_data["Password"], "Connection", profile_key=profile)
83 self.host.memory.setParam("Server", session_data["Server"], "Connection", profile_key=profile)
84 self.host.memory.setParam("Port", session_data["Port"], "Connection", profile_key=profile)
85
86 user = jid.parse(session_data['JabberID'])[0]
87 return self._registerNewAccount(user, session_data["Password"], None, session_data["Server"], profile_key=profile)
88
89 def _registerNewAccount(self, user, password, email, host, port=5222, profile_key=C.PROF_KEY_NONE):
90 """Connect to a server and create a new account using in-band registration.
91 @param user: login of the account
92 @param password: password of the account
93 @param email: email of the account
94 @param host: host of the server to register to
95 @param port: port of the server to register to
96 @param profile_key: %(doc_profile_key)s
97 """
98 profile = self.host.memory.getProfileName(profile_key)
99
100 d = defer.Deferred()
101 serverRegistrer = xmlstream.XmlStreamFactory(xmpp.RegisteringAuthenticator(self, host, user, password, email, d, profile))
102 connector = reactor.connectTCP(host, port, serverRegistrer)
103 serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect()
104
105 def cb(dummy):
106 xmlui = xml_tools.XMLUI("popup", title=D_("Confirmation"))
107 xmlui.addText(D_("Registration successful."))
108 return ({'xmlui': xmlui.toXml()})
109
110 def eb(failure):
111 xmlui = xml_tools.XMLUI("popup", title=D_("Failure"))
112 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage())
113 try:
114 if failure.value.condition == 'conflict':
115 xmlui.addText(D_("Username already exists, please choose an other one."))
116 except AttributeError:
117 pass
118 return ({'xmlui': xmlui.toXml()})
119
120 d.addCallbacks(cb, eb)
121 return d