comparison src/plugins/plugin_misc_account.py @ 1726:2ebe66a96d05

plugin misc_account: auto add some contacts at profile creation
author souliane <souliane@mailoo.org>
date Mon, 07 Dec 2015 20:35:21 +0100
parents c1be6363bfab
children 68e498b3367e
comparison
equal deleted inserted replaced
1725:c1be6363bfab 1726:2ebe66a96d05
25 from sat.memory.memory import Sessions 25 from sat.memory.memory import Sessions
26 from sat.memory.crypto import PasswordHasher 26 from sat.memory.crypto import PasswordHasher
27 from sat.core.constants import Const as C 27 from sat.core.constants import Const as C
28 28
29 from twisted.internet import reactor, defer, protocol 29 from twisted.internet import reactor, defer, protocol
30 from twisted.words.protocols.jabber import jid
30 from twisted.python.procutils import which 31 from twisted.python.procutils import which
31 from twisted.python.failure import Failure 32 from twisted.python.failure import Failure
32 from twisted.mail.smtp import sendmail 33 from twisted.mail.smtp import sendmail
33 from os.path import join, dirname 34 from os.path import join, dirname
34 from email.mime.text import MIMEText 35 from email.mime.text import MIMEText
59 "admin_email": "admin@example.net", 60 "admin_email": "admin@example.net",
60 "new_account_server": "localhost", 61 "new_account_server": "localhost",
61 "new_account_domain": "example.net", 62 "new_account_domain": "example.net",
62 "prosody_path": None, # prosody path (where prosodyctl will be executed from), or None to automaticaly find it 63 "prosody_path": None, # prosody path (where prosodyctl will be executed from), or None to automaticaly find it
63 "prosodyctl": "prosodyctl", 64 "prosodyctl": "prosodyctl",
64 "reserved_list": ['libervia'] # profiles which can't be used 65 "reserved_list": ['libervia'], # profiles which can't be used
66 "auto_add_contacts": ['salut-a-toi@libervia.org'],
65 } 67 }
66 68
67 69
68 class PasswordsMatchingError(Exception): 70 class PasswordsMatchingError(Exception):
69 pass 71 pass
166 #charset = [chr(i) for i in range(0x21,0x7F)] #XXX: this charset seems to have some issues with openfire 168 #charset = [chr(i) for i in range(0x21,0x7F)] #XXX: this charset seems to have some issues with openfire
167 charset = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)] 169 charset = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)]
168 return ''.join([random.choice(charset) for i in range(15)]) 170 return ''.join([random.choice(charset) for i in range(15)])
169 171
170 def _registerAccount(self, email, password, profile): 172 def _registerAccount(self, email, password, profile):
171 return self.registerAccount(email, password, None, profile) 173 d = self.registerAccount(email, password, None, profile)
174 d.addCallback(lambda dummy: self.autoAddContacts(password, profile))
175 return d
172 176
173 def registerAccount(self, email, password, jid_s, profile): 177 def registerAccount(self, email, password, jid_s, profile):
174 """Register a new profile and its associated XMPP account. 178 """Register a new profile and its associated XMPP account.
175 179
176 @param email (unicode): where to send to confirmation email to 180 @param email (unicode): where to send to confirmation email to
224 d.addCallback(lambda dummy: self.host.memory.startSession(password, profile)) 228 d.addCallback(lambda dummy: self.host.memory.startSession(password, profile))
225 d.addCallback(setParams) 229 d.addCallback(setParams)
226 d.addCallback(lambda dummy: self.host.memory.stopSession(profile)) 230 d.addCallback(lambda dummy: self.host.memory.stopSession(profile))
227 d.addErrback(removeProfile) 231 d.addErrback(removeProfile)
228 return d 232 return d
233
234 def autoAddContacts(self, password, profile):
235 """Auto-add some roster contacts after the profile creation.
236
237 @param password (unicode): XMPP account password
238 """
239 contacts = self.getConfig('auto_add_contacts')
240 if not contacts:
241 return
242
243 def addContact(was_connected, contacts):
244 for contact in contacts:
245 d = self.host.addContact(contact, profile)
246 if not was_connected:
247 d.addCallback(lambda dummy: self.host.disconnect(profile))
248
249 try:
250 contacts_jid = [jid.JID(contact) for contact in contacts]
251 except (RuntimeError, jid.InvalidFormat, AttributeError):
252 log.error("Invalid JIDs list: %s" % ", ".join(contacts))
253 else:
254 d = self.host.asyncConnect(profile, password, max_retries=0)
255 d.addCallback(addContact, contacts_jid)
229 256
230 def sendEmails(self, email, jid_s, password, profile): 257 def sendEmails(self, email, jid_s, password, profile):
231 # time to send the email 258 # time to send the email
232 259
233 _email_host = self.getConfig('email_server') 260 _email_host = self.getConfig('email_server')
546 def removeProfile(failure): # profile has been successfully created but the XMPP credentials are wrong! 573 def removeProfile(failure): # profile has been successfully created but the XMPP credentials are wrong!
547 log.debug("Removing previously auto-created profile: %s" % failure.getErrorMessage()) 574 log.debug("Removing previously auto-created profile: %s" % failure.getErrorMessage())
548 self.host.memory.asyncDeleteProfile(jid_s) 575 self.host.memory.asyncDeleteProfile(jid_s)
549 raise failure 576 raise failure
550 577
578 d.addCallback(self.autoAddContacts(password, jid_s))
551 d.addErrback(removeProfile) 579 d.addErrback(removeProfile)
552 return d 580 return d