From 9ab8ac9083a7bab26cc26c3ff6d4d518f68269e1 Mon Sep 17 00:00:00 2001 From: souliane Date: Mon, 7 Dec 2015 20:35:21 +0100 Subject: [PATCH] plugin misc_account: auto add some contacts at profile creation --- src/plugins/plugin_misc_account.py | 32 ++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/plugins/plugin_misc_account.py b/src/plugins/plugin_misc_account.py index 9ab7a99a..3d518274 100755 --- a/src/plugins/plugin_misc_account.py +++ b/src/plugins/plugin_misc_account.py @@ -27,6 +27,7 @@ from sat.memory.crypto import PasswordHasher from sat.core.constants import Const as C from twisted.internet import reactor, defer, protocol +from twisted.words.protocols.jabber import jid from twisted.python.procutils import which from twisted.python.failure import Failure from twisted.mail.smtp import sendmail @@ -61,7 +62,8 @@ default_conf = {"email_from": "NOREPLY@example.net", "new_account_domain": "example.net", "prosody_path": None, # prosody path (where prosodyctl will be executed from), or None to automaticaly find it "prosodyctl": "prosodyctl", - "reserved_list": ['libervia'] # profiles which can't be used + "reserved_list": ['libervia'], # profiles which can't be used + "auto_add_contacts": ['salut-a-toi@libervia.org'], } @@ -168,7 +170,9 @@ class MiscAccount(object): return ''.join([random.choice(charset) for i in range(15)]) def _registerAccount(self, email, password, profile): - return self.registerAccount(email, password, None, profile) + d = self.registerAccount(email, password, None, profile) + d.addCallback(lambda dummy: self.autoAddContacts(password, profile)) + return d def registerAccount(self, email, password, jid_s, profile): """Register a new profile and its associated XMPP account. @@ -227,6 +231,29 @@ class MiscAccount(object): d.addErrback(removeProfile) return d + def autoAddContacts(self, password, profile): + """Auto-add some roster contacts after the profile creation. + + @param password (unicode): XMPP account password + """ + contacts = self.getConfig('auto_add_contacts') + if not contacts: + return + + def addContact(was_connected, contacts): + for contact in contacts: + d = self.host.addContact(contact, profile) + if not was_connected: + d.addCallback(lambda dummy: self.host.disconnect(profile)) + + try: + contacts_jid = [jid.JID(contact) for contact in contacts] + except (RuntimeError, jid.InvalidFormat, AttributeError): + log.error("Invalid JIDs list: %s" % ", ".join(contacts)) + else: + d = self.host.asyncConnect(profile, password, max_retries=0) + d.addCallback(addContact, contacts_jid) + def sendEmails(self, email, jid_s, password, profile): # time to send the email @@ -548,5 +575,6 @@ http://www.salut-a-toi.org self.host.memory.asyncDeleteProfile(jid_s) raise failure + d.addCallback(self.autoAddContacts(password, jid_s)) d.addErrback(removeProfile) return d