comparison sat/stdui/ui_profile_manager.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/stdui/ui_profile_manager.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT standard user interface for managing contacts
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.constants import Const as C
23 from sat.core.log import getLogger
24 log = getLogger(__name__)
25 from sat.core import exceptions
26 from sat.tools import xml_tools
27 from sat.memory.memory import ProfileSessions
28 from twisted.words.protocols.jabber import jid
29
30
31 class ProfileManager(object):
32 """Manage profiles."""
33
34 def __init__(self, host):
35 self.host = host
36 self.profile_ciphers = {}
37 self._sessions = ProfileSessions()
38 host.registerCallback(self._authenticateProfile, force_id=C.AUTHENTICATE_PROFILE_ID, with_data=True)
39 host.registerCallback(self._changeXMPPPassword, force_id=C.CHANGE_XMPP_PASSWD_ID, with_data=True)
40 self.__new_xmpp_passwd_id = host.registerCallback(self._changeXMPPPasswordCb, with_data=True)
41
42 def _startSessionEb(self, fail, first, profile):
43 """Errback method for startSession during profile authentication
44
45 @param first(bool): if True, this is the first try and we have tryied empty password
46 in this case we ask for a password to the user.
47 @param profile(unicode, None): %(doc_profile)s
48 must only be used if first is True
49 """
50 if first:
51 # first call, we ask for the password
52 form_ui = xml_tools.XMLUI("form", title=D_('Profile password for {}').format(profile), submit_id='')
53 form_ui.addPassword('profile_password', value='')
54 d = xml_tools.deferredUI(self.host, form_ui, chained=True)
55 d.addCallback(self._authenticateProfile, profile)
56 return {'xmlui': form_ui.toXml()}
57
58 assert profile is None
59
60 if fail.check(exceptions.PasswordError):
61 dialog = xml_tools.XMLUI('popup', title=D_('Connection error'))
62 dialog.addText(D_("The provided profile password doesn't match."))
63 else:
64 log.error(u"Unexpected exceptions: {}".format(fail))
65 dialog = xml_tools.XMLUI('popup', title=D_('Internal error'))
66 dialog.addText(D_(u"Internal error: {}".format(fail)))
67 return {'xmlui': dialog.toXml(), 'validated': C.BOOL_FALSE}
68
69 def _authenticateProfile(self, data, profile):
70 if C.bool(data.get('cancelled', 'false')):
71 return {}
72 if self.host.memory.isSessionStarted(profile):
73 return {'validated': C.BOOL_TRUE}
74 try:
75 password = data[xml_tools.formEscape('profile_password')]
76 except KeyError:
77 # first request, we try empty password
78 password = ''
79 first = True
80 eb_profile = profile
81 else:
82 first = False
83 eb_profile = None
84 d = self.host.memory.startSession(password, profile)
85 d.addCallback(lambda dummy: {'validated': C.BOOL_TRUE})
86 d.addErrback(self._startSessionEb, first, eb_profile)
87 return d
88
89 def _changeXMPPPassword(self, data, profile):
90 session_data = self._sessions.profileGetUnique(profile)
91 if not session_data:
92 server = self.host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection", profile_key=profile)
93 if not server:
94 server = jid.parse(self.host.memory.getParamA('JabberID', "Connection", profile_key=profile))[1]
95 session_id, session_data = self._sessions.newSession({'count': 0, 'server': server}, profile=profile)
96 if session_data['count'] > 2: # 3 attempts with a new password after the initial try
97 self._sessions.profileDelUnique(profile)
98 _dialog = xml_tools.XMLUI('popup', title=D_('Connection error'))
99 _dialog.addText(D_("Can't connect to %s. Please check your connection details.") % session_data['server'])
100 return {'xmlui': _dialog.toXml()}
101 session_data['count'] += 1
102 counter = ' (%d)' % session_data['count'] if session_data['count'] > 1 else ''
103 title = D_('XMPP password for %(profile)s%(counter)s') % {'profile': profile, 'counter': counter}
104 form_ui = xml_tools.XMLUI("form", title=title, submit_id=self.__new_xmpp_passwd_id)
105 form_ui.addText(D_("Can't connect to %s. Please check your connection details or try with another password.") % session_data['server'])
106 form_ui.addPassword('xmpp_password', value='')
107 return {'xmlui': form_ui.toXml()}
108
109 def _changeXMPPPasswordCb(self, data, profile):
110 xmpp_password = data[xml_tools.formEscape('xmpp_password')]
111 d = self.host.memory.setParam("Password", xmpp_password, "Connection", profile_key=profile)
112 d.addCallback(lambda dummy: self.host.connect(profile))
113 d.addCallback(lambda dummy: {})
114 d.addErrback(lambda dummy: self._changeXMPPPassword({}, profile))
115 return d