comparison sat/plugins/plugin_misc_register_account.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 378188abe941
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
18 # You should have received a copy of the GNU Affero General Public License 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/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 from sat.core.i18n import _, D_ 21 from sat.core.i18n import _, D_
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23
23 log = getLogger(__name__) 24 log = getLogger(__name__)
24 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
25 from twisted.words.protocols.jabber import jid 26 from twisted.words.protocols.jabber import jid
26 from sat.memory.memory import Sessions 27 from sat.memory.memory import Sessions
27 from sat.tools import xml_tools 28 from sat.tools import xml_tools
35 C.PI_PROTOCOLS: [], 36 C.PI_PROTOCOLS: [],
36 C.PI_DEPENDENCIES: ["XEP-0077"], 37 C.PI_DEPENDENCIES: ["XEP-0077"],
37 C.PI_RECOMMENDATIONS: [], 38 C.PI_RECOMMENDATIONS: [],
38 C.PI_MAIN: "RegisterAccount", 39 C.PI_MAIN: "RegisterAccount",
39 C.PI_HANDLER: "no", 40 C.PI_HANDLER: "no",
40 C.PI_DESCRIPTION: _(u"""Register XMPP account""") 41 C.PI_DESCRIPTION: _(u"""Register XMPP account"""),
41 } 42 }
42 43
43 44
44 class RegisterAccount(object): 45 class RegisterAccount(object):
45 # FIXME: this plugin is messy and difficult to read, it needs to be cleaned up and documented 46 # FIXME: this plugin is messy and difficult to read, it needs to be cleaned up and documented
46 47
47 def __init__(self, host): 48 def __init__(self, host):
48 log.info(_(u"Plugin Register Account initialization")) 49 log.info(_(u"Plugin Register Account initialization"))
49 self.host = host 50 self.host = host
50 self._sessions = Sessions() 51 self._sessions = Sessions()
51 host.registerCallback(self.registerNewAccountCB, with_data=True, force_id="registerNewAccount") 52 host.registerCallback(
52 self.__register_account_id = host.registerCallback(self._registerConfirmation, with_data=True) 53 self.registerNewAccountCB, with_data=True, force_id="registerNewAccount"
54 )
55 self.__register_account_id = host.registerCallback(
56 self._registerConfirmation, with_data=True
57 )
53 58
54 def registerNewAccountCB(self, data, profile): 59 def registerNewAccountCB(self, data, profile):
55 """Called when the user click on the "New account" button.""" 60 """Called when the user click on the "New account" button."""
56 session_data = {} 61 session_data = {}
57 62
58 # FIXME: following loop is overcomplicated, hard to read 63 # FIXME: following loop is overcomplicated, hard to read
59 # FIXME: while used with parameters, hashed password is used and overwrite clear one 64 # 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): 65 for param in (u"JabberID", u"Password", C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
61 try: 66 try:
62 session_data[param] = data[SAT_FORM_PREFIX + u"Connection" + SAT_PARAM_SEPARATOR + param] 67 session_data[param] = data[
68 SAT_FORM_PREFIX + u"Connection" + SAT_PARAM_SEPARATOR + param
69 ]
63 except KeyError: 70 except KeyError:
64 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM): 71 if param in (C.FORCE_PORT_PARAM, C.FORCE_SERVER_PARAM):
65 session_data[param] = '' 72 session_data[param] = ""
66 73
67 for param in (u'JabberID', u'Password'): 74 for param in (u"JabberID", u"Password"):
68 if not session_data[param]: 75 if not session_data[param]:
69 form_ui = xml_tools.XMLUI(u"popup", title=D_(u"Missing values")) 76 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.")) 77 form_ui.addText(
71 return {u'xmlui': form_ui.toXml()} 78 D_(u"No user JID or password given: can't register new account.")
79 )
80 return {u"xmlui": form_ui.toXml()}
72 81
73 session_data['user'], host, resource = jid.parse(session_data['JabberID']) 82 session_data["user"], host, resource = jid.parse(session_data["JabberID"])
74 session_data['server'] = session_data[C.FORCE_SERVER_PARAM] or host 83 session_data["server"] = session_data[C.FORCE_SERVER_PARAM] or host
75 session_id, dummy = self._sessions.newSession(session_data, profile=profile) 84 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) 85 form_ui = xml_tools.XMLUI(
77 form_ui.addText(D_(u"Do you want to register a new XMPP account {jid}?").format( 86 "form",
78 jid = session_data['JabberID'])) 87 title=D_("Register new account"),
79 return {'xmlui': form_ui.toXml()} 88 submit_id=self.__register_account_id,
89 session_id=session_id,
90 )
91 form_ui.addText(
92 D_(u"Do you want to register a new XMPP account {jid}?").format(
93 jid=session_data["JabberID"]
94 )
95 )
96 return {"xmlui": form_ui.toXml()}
80 97
81 def _registerConfirmation(self, data, profile): 98 def _registerConfirmation(self, data, profile):
82 """Save the related parameters and proceed the registration.""" 99 """Save the related parameters and proceed the registration."""
83 session_data = self._sessions.profileGet(data['session_id'], profile) 100 session_data = self._sessions.profileGet(data["session_id"], profile)
84 101
85 self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile) 102 self.host.memory.setParam(
86 self.host.memory.setParam("Password", session_data["Password"], "Connection", profile_key=profile) 103 "JabberID", session_data["JabberID"], "Connection", profile_key=profile
87 self.host.memory.setParam(C.FORCE_SERVER_PARAM, session_data[C.FORCE_SERVER_PARAM], "Connection", profile_key=profile) 104 )
88 self.host.memory.setParam(C.FORCE_PORT_PARAM, session_data[C.FORCE_PORT_PARAM], "Connection", profile_key=profile) 105 self.host.memory.setParam(
106 "Password", session_data["Password"], "Connection", profile_key=profile
107 )
108 self.host.memory.setParam(
109 C.FORCE_SERVER_PARAM,
110 session_data[C.FORCE_SERVER_PARAM],
111 "Connection",
112 profile_key=profile,
113 )
114 self.host.memory.setParam(
115 C.FORCE_PORT_PARAM,
116 session_data[C.FORCE_PORT_PARAM],
117 "Connection",
118 profile_key=profile,
119 )
89 120
90 d = self._registerNewAccount(jid.JID(session_data['JabberID']), session_data["Password"], None, session_data['server']) 121 d = self._registerNewAccount(
91 del self._sessions[data['session_id']] 122 jid.JID(session_data["JabberID"]),
123 session_data["Password"],
124 None,
125 session_data["server"],
126 )
127 del self._sessions[data["session_id"]]
92 return d 128 return d
93 129
94 def _registerNewAccount(self, client, jid_, password, email, server): 130 def _registerNewAccount(self, client, jid_, password, email, server):
95 # FIXME: port is not set here 131 #  FIXME: port is not set here
96 def registeredCb(dummy): 132 def registeredCb(dummy):
97 xmlui = xml_tools.XMLUI(u"popup", title=D_(u"Confirmation")) 133 xmlui = xml_tools.XMLUI(u"popup", title=D_(u"Confirmation"))
98 xmlui.addText(D_("Registration successful.")) 134 xmlui.addText(D_("Registration successful."))
99 return ({'xmlui': xmlui.toXml()}) 135 return {"xmlui": xmlui.toXml()}
100 136
101 def registeredEb(failure): 137 def registeredEb(failure):
102 xmlui = xml_tools.XMLUI("popup", title=D_("Failure")) 138 xmlui = xml_tools.XMLUI("popup", title=D_("Failure"))
103 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage()) 139 xmlui.addText(D_("Registration failed: %s") % failure.getErrorMessage())
104 try: 140 try:
105 if failure.value.condition == 'conflict': 141 if failure.value.condition == "conflict":
106 xmlui.addText(D_("Username already exists, please choose an other one.")) 142 xmlui.addText(
143 D_("Username already exists, please choose an other one.")
144 )
107 except AttributeError: 145 except AttributeError:
108 pass 146 pass
109 return ({'xmlui': xmlui.toXml()}) 147 return {"xmlui": xmlui.toXml()}
110 148
111 registered_d = self.host.plugins['XEP-0077'].registerNewAccount(client, jid_, password, email=email, host=server, port=C.XMPP_C2S_PORT) 149 registered_d = self.host.plugins["XEP-0077"].registerNewAccount(
150 client, jid_, password, email=email, host=server, port=C.XMPP_C2S_PORT
151 )
112 registered_d.addCallbacks(registeredCb, registeredEb) 152 registered_d.addCallbacks(registeredCb, registeredEb)
113 return registered_d 153 return registered_d