Пример #1
0
    def operation():

        try:
            (two_weeks, three_days, gone) = yield check_expiration_date()

            messages = dict({})

            for username, sincepoch in two_weeks.iteritems():
                messages.update({ username : untranslated_template % "expire in two weeks" })

            for username, sincepoch in three_days.iteritems():
                messages.update({ username : untranslated_template % "expire in three days" })

            for username, sincepoch in gone.iteritems():
                messages.update({ username : untranslated_template % "it's already expired" })

            for recipient, message in messages.iteritems():

                mail_building = ["Date: %s" % rfc822_date(),
                                 "From: \"%s\" <%s>" %
                                 ( GLSetting.memory_copy.notif_source_name,
                                   GLSetting.memory_copy.notif_source_email ),
                                 "To: %s" % recipient,
                                 "Subject: Your PGP key expiration date is coming",
                                 "Content-Type: text/plain; charset=ISO-8859-1",
                                 "Content-Transfer-Encoding: 8bit", None,
                                 message]

                mail_content = collapse_mail_content(mail_building)

                if not mail_content:
                    log.err("Unable to format (and then notify!) PGP key incoming expiration for %s" % recipient)
                    log.debug(mail_building)
                    return

                sendmail(GLSetting.memory_copy.notif_username,
                         GLSetting.memory_copy.notif_password,
                         GLSetting.memory_copy.notif_username,
                         [ recipient ],
                         mail_content,
                         GLSetting.memory_copy.notif_server,
                         GLSetting.memory_copy.notif_port,
                         GLSetting.memory_copy.notif_security)

        except Exception as excep:
            log.err("Error in PGP key expiration check: %s (failure ignored)" % excep)
            return
Пример #2
0
    def do_notify(self, event):

        # check if exists the conf
        if not self.validate_admin_opt(event.notification_settings):
            log.info('invalid configuration for admin email!')
            return None

        # At the moment the language used is a system language, not
        # Receiver preferences language ?
        if event.type == u'tip':
            body = self.format_template(
                event.notification_settings['tip_template'], event)
            title = self.format_template(
                event.notification_settings['tip_mail_title'], event)
        elif event.type == u'comment':
            body = self.format_template(
                event.notification_settings['comment_template'], event)
            title = self.format_template(
                event.notification_settings['comment_mail_title'], event)
        elif event.type == u'file':
            body = self.format_template(
                event.notification_settings['file_template'], event)
            title = self.format_template(
                event.notification_settings['file_mail_title'], event)
        else:
            raise NotImplementedError("At the moment, only Tip expected")

        # If the receiver has encryption enabled (for notification), encrypt the mail body
        if event.receiver_info['gpg_key_status'] == Receiver._gpg_types[1] and \
           event.receiver_info['gpg_enable_notification']:

            try:
                gpob = GLBGPG(event.receiver_info)

                if not gpob.validate_key(event.receiver_info['gpg_key_armor']):
                    log.err("unable to validated GPG key for receiver %s" %
                            event.receiver_info['username'])
                    return None

                body = gpob.encrypt_message(body)
                gpob.destroy_environment()

            except Exception as excep:
                log.err("Error in GPG interface object (for %s: %s)! (notification+encryption)" %
                        (event.receiver_info['username'], str(excep) ))
                return None

        receiver_mail = event.receiver_info['notification_fields']['mail_address']

        # Compose the email having the system+subject+recipient data
        mail_building = ["Date: %s" % rfc822_date(),
                         "From: \"%s\" <%s>" % (
                             GLSetting.memory_copy.notif_source_name,
                             GLSetting.memory_copy.notif_source_email ),
                         "To: %s" % receiver_mail,
                         "Subject: %s" % title,
                         "Content-Type: text/plain; charset=ISO-8859-1",
                         "Content-Transfer-Encoding: 8bit",
                         None,
                         body]

        # XXX here can be catch the subject (may change if encrypted or whatever)

        # appending 'None' it's used to mean "\n" without being escaped by collapse_mail_content

        message = collapse_mail_content(mail_building)

        if not message:
            log.err("Unable to format (and then notify!) email for %s" % receiver_mail)
            log.debug(mail_building)
            return None

        self.finished = self.mail_flush(event.notification_settings['source_email'],
                                        [ receiver_mail ], message, event)

        return self.finished