changeset 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 (2015-12-07)
parents c1be6363bfab
children 68e498b3367e
files src/plugins/plugin_misc_account.py
diffstat 1 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_account.py	Mon Dec 07 19:56:12 2015 +0100
+++ b/src/plugins/plugin_misc_account.py	Mon Dec 07 20:35:21 2015 +0100
@@ -27,6 +27,7 @@
 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 @@
                 "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 @@
         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 @@
         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 @@
             self.host.memory.asyncDeleteProfile(jid_s)
             raise failure
 
+        d.addCallback(self.autoAddContacts(password, jid_s))
         d.addErrback(removeProfile)
         return d