diff src/plugins/plugin_misc_account.py @ 2181:968b0d13bcc7

plugin account, tools: some cleaning account + email and password tools: - plugin misc account has been reordered and cleaned a bit. - new xmpp_domain setting is used to get the domain name of main XMPP server - password generation method has been moved to tools.utils - email sending method has been moved to tools.email
author Goffi <goffi@goffi.org>
date Sun, 12 Mar 2017 19:32:59 +0100
parents 09cfec4d8d19
children 8be4f5769bf7
line wrap: on
line diff
--- a/src/plugins/plugin_misc_account.py	Thu Mar 09 23:11:42 2017 +0100
+++ b/src/plugins/plugin_misc_account.py	Sun Mar 12 19:32:59 2017 +0100
@@ -29,10 +29,7 @@
 from twisted.internet import defer
 from twisted.python.failure import Failure
 from twisted.words.protocols.jabber import jid
-from twisted.mail.smtp import sendmail
-from email.mime.text import MIMEText
-
-import random
+from sat.tools import email as sat_email
 
 # FIXME: this plugin code is old and need a cleaning
 # TODO: account deletion/password change need testing
@@ -67,10 +64,32 @@
                 "email_admins_list": [],
                 "admin_email": "",
                 "new_account_server": "localhost",
-                "new_account_domain": "example.net",
+                "new_account_domain": "",  # use xmpp_domain if not found
                 "reserved_list": ['libervia'] # profiles which can't be used
                }
 
+WELCOME_MSG = D_(u"""Welcome to Libervia, the web interface of Salut à Toi.
+
+Your account on {domain} has been successfully created.
+This is a demonstration version to show you the current status of the project.
+It is still under development, please keep it in mind!
+
+Here is your connection information:
+
+Login on {domain}: {profile}
+Jabber ID (JID): {jid}
+Your password has been chosen by yourself during registration.
+
+In the beginning, you have nobody to talk to. To find some contacts, you may use the users' directory:
+    - make yourself visible in "Service / Directory subscription".
+    - search for people with "Contacts" / Search directory".
+
+Any feedback welcome. Thank you!
+
+Salut à Toi association
+https://www.salut-a-toi.org
+""")
+
 
 class MiscAccount(object):
     """Account plugin: create a SàT + XMPP account, used by Libervia"""
@@ -121,7 +140,7 @@
         value.add(admin_email)
         self.host.memory.config.set(section, param_name, ",".join(value))
 
-    def getConfig(self, name):
+    def getConfig(self, name, section=CONFIG_SECTION):
         if name.startswith("email_"):
             # XXX: email_ parameters were first in [plugin account] section
             #      but as it make more sense to have them in common with other plugins,
@@ -133,17 +152,11 @@
             else:
                 return value
 
-        return self.host.memory.getConfig(CONFIG_SECTION, name, default_conf[name])
-
-    def generatePassword(self):
-        """Generate a password with random characters.
-
-        @return unicode
-        """
-        random.seed()
-        #charset = [chr(i) for i in range(0x21,0x7F)] #XXX: this charset seems to have some issues with openfire
-        charset = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)]
-        return ''.join([random.choice(charset) for i in range(15)])
+        if section == CONFIG_SECTION:
+            default = default_conf[name]
+        else:
+            default = None
+        return self.host.memory.getConfig(section, name, default)
 
     def _registerAccount(self, email, password, profile):
         return self.registerAccount(email, password, None, profile)
@@ -199,7 +212,7 @@
             d = defer.succeed(None)
             jid_ = jid.JID(jid_s)
         else:
-            jid_s = profile + u"@" + self.getConfig('new_account_domain')
+            jid_s = profile + u"@" + self.getNewAccountDomain()
             jid_ = jid.JID(jid_s)
             d = self.host.plugins['XEP-0077'].registerNewAccount(jid_, password)
 
@@ -218,30 +231,16 @@
         d.addErrback(removeProfile)
         return d
 
+    def _sendEmailEb(self, failure_, email):
+        # TODO: return error code to user
+        log.error(_(u"Failed to send account creation confirmation to {email}: {msg}").format(
+            email = email,
+            msg = failure_))
+
     def sendEmails(self, email, profile):
         # time to send the email
 
-        email_host = self.getConfig('email_server')
-        email_from = self.getConfig("email_from")
-        email_sender_domain = self.getConfig("email_sender_domain") or None
-        email_port = int(self.getConfig("email_port"))
-        email_username = self.getConfig("email_username") or None
-        email_password = self.getConfig("email_password") or None
-        email_starttls = C.bool(self.getConfig("email_starttls"))
-        email_auth = C.bool(self.getConfig("email_auth"))
-        domain = self.getConfig('new_account_domain')
-
-        def sendEmail(recipients, msg):
-            return sendmail(email_host.encode("utf-8"),
-                            email_from.encode("utf-8"),
-                            [email.encode("utf-8") for email in recipients],
-                            msg.as_string(),
-                            senderDomainName=email_sender_domain.encode("utf-8") if email_sender_domain else None,
-                            port=email_port,
-                            username=email_username.encode("utf-8") if email_username else None,
-                            password=email_password.encode("utf-8") if email_password else None,
-                            requireAuthentication=email_starttls,
-                            requireTransportSecurity=email_auth)
+        domain = self.getNewAccountDomain()
 
         # email to the administrators
         admins_emails = self.getConfig('email_admins_list')
@@ -249,59 +248,39 @@
             log.warning(u"No known admin email, we can't send email to administrator(s).\nPlease fill email_admins_list parameter")
             d_admin = defer.fail(exceptions.DataError("no admin email"))
         else:
-            body = (u"""New account created: %(profile)s [%(email)s]""" % {'profile': profile, 'email': email}).encode('utf-8')
-            msg = MIMEText(body, 'plain', 'UTF-8')
-            msg['Subject'] = _('New Libervia account created')
-            msg['From'] = email_from
-            msg['To'] = ", ".join(admins_emails)
+            subject = _(u'New Libervia account created')
+            body = (u"""New account created: {profile} [{email}]""".format(
+                profile = profile,
+                # there is no email when an existing XMPP account is used
+                email = email or u"<no email>"))
+            d_admin = sat_email.sendEmail(self.host, admins_emails, subject, body)
 
-            d_admin = sendEmail(admins_emails, msg)
-        admins_emails_txt = u', '.join([u"<{}>".format(addr) for addr in admins_emails])
+        admins_emails_txt = u', '.join([u'<' + addr + u'>' for addr in admins_emails])
         d_admin.addCallbacks(lambda dummy: log.debug(u"Account creation notification sent to admin(s) {}".format(admins_emails_txt)),
                              lambda dummy: log.error(u"Failed to send account creation notification to admin {}".format(admins_emails_txt)))
         if not email:
+            # TODO: if use register with an existing account, an XMPP message should be sent
             return d_admin
 
-        jid_s = self.host.memory.getParamA("JabberID", "Connection", profile_key=profile)
-        body = (_(u"""Welcome to Libervia, the web interface of Salut à Toi.
-
-Your account on {domain} has been successfully created. This is a demonstration version to show you the current status of the project. It is still under development, please keep it in mind!
-
-Here is your connection information:
-
-Login on {domain}: {profile}
-Jabber ID (JID): {jid}
-Your password has been chosen by yourself during registration.
-
-In the beginning, you have nobody to talk to. To find some contacts, you may use the users' directory:
-    - make yourself visible in "Service / Directory subscription".
-    - search for people with "Contacts" / Search directory".
-
-Any feedback welcome. Thank you!
-
-Salut à Toi association
-http://www.salut-a-toi.org
-""").format(profile=profile, jid=jid_s, domain=domain)).encode('utf-8')
-        msg = MIMEText(body, 'plain', 'UTF-8')
-        msg['Subject'] = _(u'Libervia account created')
-        msg['From'] = email_from
-        msg['To'] = email
-
-        def email_ko(dummy):
-            # TODO: return error code to user
-            log.error(u"Failed to send account creation confirmation to <%s>" % email)
+        jid_s = self.host.memory.getParamA(u"JabberID", u"Connection", profile_key=profile)
+        subject = _(u'Your Libervia account has been created')
+        body = (_(WELCOME_MSG).format(profile=profile, jid=jid_s, domain=domain))
 
         # XXX: this will not fail when the email address doesn't exist
         # FIXME: check email reception to validate email given by the user
         # FIXME: delete the profile if the email could not been sent?
-        d_user = sendEmail([email], msg)
-        d_user.addCallbacks(lambda dummy: log.debug(u"Account creation confirmation sent to <%s>" % email),
-                            email_ko)
+        d_user = sat_email.sendEmail(self.host, [email], subject, body)
+        d_user.addCallbacks(lambda dummy: log.debug(u"Account creation confirmation sent to <{}>".format(email)),
+                            self._sendEmailEb)
         return defer.DeferredList([d_user, d_admin])
 
     def getNewAccountDomain(self):
-        """@return: the domain that will be set to new account"""
-        return self.getConfig('new_account_domain')
+        """get the domain that will be set to new account"""
+
+        domain = self.getConfig('new_account_domain') or self.getConfig('xmpp_domain', None)
+        if not domain:
+            raise ValueError(_(u"xmpp_domain need to be set in sat.conf"))
+        return domain
 
     def _getAccountDialogUI(self, profile):
         """Get the main dialog to manage your account