changeset 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 be54e1c3394c
children 087eec4c6c07
files src/plugins/plugin_misc_account.py src/tools/email.py src/tools/utils.py
diffstat 3 files changed, 137 insertions(+), 79 deletions(-) [+]
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tools/email.py	Sun Mar 12 19:32:59 2017 +0100
@@ -0,0 +1,67 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+
+# SàT: a jabber client
+# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""email sending facilities"""
+
+from __future__ import absolute_import
+from sat.core.constants import Const as C
+from sat.core.log import getLogger
+log = getLogger(__name__)
+from twisted.mail import smtp
+from email.mime.text import MIMEText
+
+
+
+def sendEmail(host, to_emails, subject=u'', body=u'', from_email=None):
+    """send an email using SàT configuration
+
+    @param to_emails(list[unicode]): list of recipients
+    @param subject(unicode): subject of the message
+    @param body(unicode): body of the message
+    @param from_email(unicode): address of the sender
+    @return (D): same as smtp.sendmail
+    """
+    email_host = host.memory.getConfig(None, u'email_server') or u'localhost'
+    email_from = host.memory.getConfig(None, u'email_from')
+    if email_from is None:
+        # we suppose that email domain and XMPP domain are identical
+        domain = host.memory.getConfig(None, u'xmpp_domain', u'example.net')
+        email_from = u'no_reply@' + domain
+    email_sender_domain = host.memory.getConfig(None, u'email_sender_domain')
+    email_port = int(host.memory.getConfig(None, u'email_port', 25))
+    email_username = host.memory.getConfig(None, u'email_username')
+    email_password = host.memory.getConfig(None, u'email_password')
+    email_auth = C.bool(host.memory.getConfig(None, 'email_auth', False))
+    email_starttls = C.bool(host.memory.getConfig(None, 'email_starttls', False))
+
+    msg = MIMEText(body, 'plain', 'UTF-8')
+    msg[u'Subject'] = subject
+    msg[u'From'] = email_from
+    msg[u'To'] = u", ".join(to_emails)
+
+    return smtp.sendmail(email_host.encode("utf-8"),
+                         email_from.encode("utf-8"),
+                         [email.encode("utf-8") for email in to_emails],
+                         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_auth,
+                         requireTransportSecurity = email_starttls)
--- a/src/tools/utils.py	Thu Mar 09 23:11:42 2017 +0100
+++ b/src/tools/utils.py	Sun Mar 12 19:32:59 2017 +0100
@@ -26,6 +26,7 @@
 import datetime
 import time
 import sys
+import random
 
 
 def clean_ustr(ustr):
@@ -54,6 +55,17 @@
     template = u"{}T{}".format(template_date, template_time) if with_time else template_date
     return datetime.datetime.utcfromtimestamp(time.time() if timestamp is None else timestamp).strftime(template)
 
+def generatePassword(vocabulary=None, size=20):
+    """Generate a password with random characters.
+
+    @param vocabulary(iterable): characters to use to create password
+    @param size(int): number of characters in the password to generate
+    @return (unicode): generated password
+    """
+    random.seed()
+    if vocabulary is None:
+        vocabulary = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)]
+    return u''.join([random.choice(vocabulary) for i in range(15)])
 
 def getRepositoryData(module, as_string=True, is_path=False, save_dir_path=None):
     """Retrieve info on current mecurial repository