comparison src/plugins/plugin_misc_account.py @ 1907:0b748ad46ede

plugin misc account: separate profile creation and sending emails, so we can wait and send the emails at the right time (when we know that no error occured)
author souliane <souliane@mailoo.org>
date Fri, 11 Mar 2016 12:31:33 +0100
parents faa8427dd032
children 72837638f076
comparison
equal deleted inserted replaced
1906:58f25b274536 1907:0b748ad46ede
208 208
209 def _registerAccount(self, email, password, profile): 209 def _registerAccount(self, email, password, profile):
210 return self.registerAccount(email, password, None, profile) 210 return self.registerAccount(email, password, None, profile)
211 211
212 def registerAccount(self, email, password, jid_s, profile): 212 def registerAccount(self, email, password, jid_s, profile):
213 """Register a new profile and its associated XMPP account. 213 """Register a new profile, its associated XMPP account, send the confirmation emails.
214 214
215 @param email (unicode): where to send to confirmation email to 215 @param email (unicode): where to send to confirmation email to
216 @param password (unicode): password chosen by the user 216 @param password (unicode): password chosen by the user
217 while be used for profile *and* XMPP account 217 while be used for profile *and* XMPP account
218 @param jid_s (unicode): JID to re-use or to register: 218 @param jid_s (unicode): JID to re-use or to register:
219 - non empty value: bind this JID to the new sat profile 219 - non empty value: bind this JID to the new sat profile
220 - None or "": register a new JID on the local XMPP server 220 - None or "": register a new JID on the local XMPP server
221 @param profile 221 @param profile
222 @return Deferred 222 @return Deferred
223 """ 223 """
224 d = self.createProfile(password, jid_s, profile)
225 d.addCallback(lambda dummy: self.sendEmails(email, jid_s, password, profile))
226 return d
227
228 def createProfile(self, password, jid_s, profile):
229 """Register a new profile and its associated XMPP account.
230
231 @param password (unicode): password chosen by the user
232 while be used for profile *and* XMPP account
233 @param jid_s (unicode): JID to re-use or to register:
234 - non empty value: bind this JID to the new sat profile
235 - None or "": register a new JID on the local XMPP server
236 @param profile
237 @return Deferred
238 """
224 if not password or not profile: 239 if not password or not profile:
225 raise exceptions.DataError 240 raise exceptions.DataError
226 241
227 if profile.lower() in self.getConfig('reserved_list'): 242 if profile.lower() in self.getConfig('reserved_list'):
228 return defer.fail(Failure(exceptions.ConflictError)) 243 return defer.fail(Failure(exceptions.ConflictError))
229 244
230 d = self.host.memory.asyncCreateProfile(profile, password) 245 d = self.host.memory.asyncCreateProfile(profile, password)
231 d.addCallback(lambda dummy: self.profileRegistered(email, password, jid_s, profile)) 246 d.addCallback(lambda dummy: self.profileCreated(password, jid_s, profile))
232 return d 247 return d
233 248
234 def profileRegistered(self, email, password, jid_s, profile): 249 def profileCreated(self, password, jid_s, profile):
235 """Create the XMPP account, set the profile connection parameters and send the confirmation email. 250 """Create the XMPP account and set the profile connection parameters.
236 251
237 @param email (unicode): where to send to confirmation email to
238 @param password (unicode): password chosen by the user 252 @param password (unicode): password chosen by the user
239 @param jid_s (unicode): JID to re-use or to register: 253 @param jid_s (unicode): JID to re-use or to register:
240 - non empty value: bind this JID to the new sat profile 254 - non empty value: bind this JID to the new sat profile
241 - None or empty: register a new JID on the local XMPP server 255 - None or empty: register a new JID on the local XMPP server
242 @param profile 256 @param profile
246 if jid_s: 260 if jid_s:
247 d = defer.succeed(None) 261 d = defer.succeed(None)
248 else: 262 else:
249 d = ProsodyRegisterProtocol.prosodyctl(self, 'adduser', password, profile) 263 d = ProsodyRegisterProtocol.prosodyctl(self, 'adduser', password, profile)
250 jid_s = "%s@%s" % (profile, self.getConfig('new_account_domain')) 264 jid_s = "%s@%s" % (profile, self.getConfig('new_account_domain'))
251
252 d.addCallback(lambda dummy: self.sendEmails(email, jid_s, password, profile))
253 265
254 def setParams(dummy): 266 def setParams(dummy):
255 self.host.memory.setParam("JabberID", jid_s, "Connection", profile_key=profile) 267 self.host.memory.setParam("JabberID", jid_s, "Connection", profile_key=profile)
256 d = self.host.memory.setParam("Password", password, "Connection", profile_key=profile) 268 d = self.host.memory.setParam("Password", password, "Connection", profile_key=profile)
257 return d 269 return d
600 except exceptions.ProfileUnknownError: 612 except exceptions.ProfileUnknownError:
601 pass 613 pass
602 else: 614 else:
603 raise exceptions.ConflictError 615 raise exceptions.ConflictError
604 616
605 d = self.registerAccount(None, password, jid_s, jid_s) 617 d = self.createProfile(password, jid_s, jid_s)
606 d.addCallback(lambda dummy: self.host.memory.getProfileName(jid_s)) # checks if the profile has been successfuly created 618 d.addCallback(lambda dummy: self.host.memory.getProfileName(jid_s)) # checks if the profile has been successfuly created
607 d.addCallback(self.host.asyncConnect, password, 0) 619 d.addCallback(self.host.asyncConnect, password, 0)
608 620
609 def removeProfile(failure): # profile has been successfully created but the XMPP credentials are wrong! 621 def removeProfile(failure): # profile has been successfully created but the XMPP credentials are wrong!
610 log.debug("Removing previously auto-created profile: %s" % failure.getErrorMessage()) 622 log.debug("Removing previously auto-created profile: %s" % failure.getErrorMessage())
611 self.host.memory.asyncDeleteProfile(jid_s) 623 self.host.memory.asyncDeleteProfile(jid_s)
612 raise failure 624 raise failure
613 625
614 d.addErrback(removeProfile) 626 # FIXME: we don't catch the case where the JID host is not an XMPP server, and the user
615 return d 627 # has to wait until the DBUS timeout ; as a consequence, emails are sent to the admins
628 # and the profile is not deleted. When the host exists, removeProfile is well called.
629 d.addCallbacks(lambda dummy: self.sendEmails(None, jid_s, password, jid_s), removeProfile)
630 return d