Esempio n. 1
0
def user_or_none(author):
    """Try to match email part of VCS committer string with a local user - or return None"""
    from kallithea.model.db import User
    email = author_email(author)
    if email:
        return User.get_by_email(email, cache=True) # cache will only use sql_cache_short
    return None
Esempio n. 2
0
def user_or_none(author):
    """Try to match email part of VCS committer string with a local user - or return None"""
    from kallithea.model.db import User
    email = author_email(author)
    if email:
        return User.get_by_email(
            email, cache=True)  # cache will only use sql_cache_short
    return None
Esempio n. 3
0
def user_attr_or_none(author, show_attr):
    """Try to match email part of VCS committer string with a local user and return show_attr
    - or return None if user not found"""
    email = author_email(author)
    if email:
        from kallithea.model.db import User
        user = User.get_by_email(email)
        if user is not None:
            return getattr(user, show_attr)
    return None
Esempio n. 4
0
    def get_user_email(self, config_file=None):
        """
        Returns user's email from global configuration file.

        :param config_file: A path to file which should be used to retrieve
          configuration from (might also be a list of file paths)
        """
        username = self.get_config_value('ui', 'username')
        if username:
            return author_email(username)
        return None
Esempio n. 5
0
    def get_user_email(self, config_file=None):
        """
        Returns user's email from global configuration file.

        :param config_file: A path to file which should be used to retrieve
          configuration from (might also be a list of file paths)
        """
        username = self.get_config_value('ui', 'username')
        if username:
            return author_email(username)
        return None
Esempio n. 6
0
def email_or_none(author):
    """Try to match email part of VCS committer string with a local user.
    Return primary email of user, email part of the specified author name, or None."""
    if not author:
        return None
    email = user_attr_or_none(author, 'email')
    if email is not None:
        return email # always use user's main email address - not necessarily the one used to find user

    # extract email from the commit string
    email = author_email(author)
    if email:
        return email

    # No valid email, not a valid user in the system, none!
    return None
Esempio n. 7
0
def email_or_none(author):
    """Try to match email part of VCS committer string with a local user.
    Return primary email of user, email part of the specified author name, or None."""
    if not author:
        return None
    user = user_or_none(author)
    if user is not None:
        return user.email # always use main email address - not necessarily the one used to find user

    # extract email from the commit string
    email = author_email(author)
    if email:
        return email

    # No valid email, not a valid user in the system, none!
    return None
Esempio n. 8
0
    def author_email(self):
        """
        Returns Author email address for given commit
        """

        return author_email(self.author)
Esempio n. 9
0
    def test_author_email(self):

        for test_str, result in self.TEST_AUTHORS:
            self.assertEqual(result[1], author_email(test_str))
Esempio n. 10
0
    def test_author_email(self):

        for test_str, result in self.TEST_AUTHORS:
            self.assertEqual(result[1], author_email(test_str))
Esempio n. 11
0
 def test_author_email(self):
     for test_str, result in self.TEST_AUTHORS:
         assert result[1] == author_email(test_str)
Esempio n. 12
0
def send_email(recipients,
               subject,
               body='',
               html_body='',
               headers=None,
               from_name=None):
    """
    Sends an email with defined parameters from the .ini files.

    :param recipients: list of recipients, if this is None, the defined email
        address from field 'email_to' and all admins is used instead
    :param subject: subject of the mail
    :param body: body of the mail
    :param html_body: html version of body
    :param headers: dictionary of prepopulated e-mail headers
    :param from_name: full name to be used as sender of this mail - often a
    .full_name_or_username value
    """
    assert isinstance(recipients, list), recipients
    if headers is None:
        headers = {}
    else:
        # do not modify the original headers object passed by the caller
        headers = headers.copy()

    email_config = config
    email_prefix = email_config.get('email_prefix', '')
    if email_prefix:
        subject = "%s %s" % (email_prefix, subject)

    if not recipients:
        # if recipients are not defined we send to email_config + all admins
        recipients = [
            u.email for u in User.query().filter(User.admin == True).all()
        ]
        if email_config.get('email_to') is not None:
            recipients += email_config.get('email_to').split(',')

        # If there are still no recipients, there are no admins and no address
        # configured in email_to, so return.
        if not recipients:
            log.error("No recipients specified and no fallback available.")
            return False

        log.warning("No recipients specified for '%s' - sending to admins %s",
                    subject, ' '.join(recipients))

    # SMTP sender
    envelope_from = email_config.get('app_email_from', 'Kallithea')
    # 'From' header
    if from_name is not None:
        # set From header based on from_name but with a generic e-mail address
        # In case app_email_from is in "Some Name <e-mail>" format, we first
        # extract the e-mail address.
        envelope_addr = author_email(envelope_from)
        headers['From'] = '"%s" <%s>' % (email.utils.quote(
            '%s (no-reply)' % from_name), envelope_addr)

    user = email_config.get('smtp_username')
    passwd = email_config.get('smtp_password')
    mail_server = email_config.get('smtp_server')
    mail_port = email_config.get('smtp_port')
    tls = str2bool(email_config.get('smtp_use_tls'))
    ssl = str2bool(email_config.get('smtp_use_ssl'))
    debug = str2bool(email_config.get('debug'))
    smtp_auth = email_config.get('smtp_auth')

    logmsg = ("Mail details:\n"
              "recipients: %s\n"
              "headers: %s\n"
              "subject: %s\n"
              "body:\n%s\n"
              "html:\n%s\n" %
              (' '.join(recipients), headers, subject, body, html_body))

    if mail_server:
        log.debug("Sending e-mail. " + logmsg)
    else:
        log.error("SMTP mail server not configured - cannot send e-mail.")
        log.warning(logmsg)
        return False

    try:
        m = SmtpMailer(envelope_from,
                       user,
                       passwd,
                       mail_server,
                       smtp_auth,
                       mail_port,
                       ssl,
                       tls,
                       debug=debug)
        m.send(recipients, subject, body, html_body, headers=headers)
    except:
        log.error('Mail sending failed')
        log.error(traceback.format_exc())
        return False
    return True
Esempio n. 13
0
    def author_email(self):
        """
        Returns Author email address for given commit
        """

        return author_email(self.author)
Esempio n. 14
0
def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
    """
    Sends an email with defined parameters from the .ini files.

    :param recipients: list of recipients, if this is None, the defined email
        address from field 'email_to' and all admins is used instead
    :param subject: subject of the mail
    :param body: body of the mail
    :param html_body: html version of body
    :param headers: dictionary of prepopulated e-mail headers
    :param author: User object of the author of this mail, if known and relevant
    """
    log = get_logger(send_email)
    assert isinstance(recipients, list), recipients
    if headers is None:
        headers = {}
    else:
        # do not modify the original headers object passed by the caller
        headers = headers.copy()

    email_config = config
    email_prefix = email_config.get('email_prefix', '')
    if email_prefix:
        subject = "%s %s" % (email_prefix, subject)

    if not recipients:
        # if recipients are not defined we send to email_config + all admins
        recipients = [u.email for u in User.query()
                      .filter(User.admin == True).all()]
        if email_config.get('email_to') is not None:
            recipients += [email_config.get('email_to')]

        # If there are still no recipients, there are no admins and no address
        # configured in email_to, so return.
        if not recipients:
            log.error("No recipients specified and no fallback available.")
            return False

        log.warning("No recipients specified for '%s' - sending to admins %s", subject, ' '.join(recipients))

    # SMTP sender
    envelope_from = email_config.get('app_email_from', 'Kallithea')
    # 'From' header
    if author is not None:
        # set From header based on author but with a generic e-mail address
        # In case app_email_from is in "Some Name <e-mail>" format, we first
        # extract the e-mail address.
        envelope_addr = author_email(envelope_from)
        headers['From'] = '"%s" <%s>' % (
            rfc822.quote('%s (no-reply)' % author.full_name_or_username),
            envelope_addr)

    user = email_config.get('smtp_username')
    passwd = email_config.get('smtp_password')
    mail_server = email_config.get('smtp_server')
    mail_port = email_config.get('smtp_port')
    tls = str2bool(email_config.get('smtp_use_tls'))
    ssl = str2bool(email_config.get('smtp_use_ssl'))
    debug = str2bool(email_config.get('debug'))
    smtp_auth = email_config.get('smtp_auth')

    logmsg = ("Mail details:\n"
              "recipients: %s\n"
              "headers: %s\n"
              "subject: %s\n"
              "body:\n%s\n"
              "html:\n%s\n"
              % (' '.join(recipients), headers, subject, body, html_body))

    if mail_server:
        log.debug("Sending e-mail. " + logmsg)
    else:
        log.error("SMTP mail server not configured - cannot send e-mail.")
        log.warning(logmsg)
        return False

    try:
        m = SmtpMailer(envelope_from, user, passwd, mail_server, smtp_auth,
                       mail_port, ssl, tls, debug=debug)
        m.send(recipients, subject, body, html_body, headers=headers)
    except:
        log.error('Mail sending failed')
        log.error(traceback.format_exc())
        return False
    return True