<commit_before>from operator import and_

from django.conf import settings
from django.core.mail.backends.smtp import EmailBackend as SMTPBackend
from django.template.loader import render_to_string


class HijackBackend(SMTPBackend):
    """
    This backend intercepts outgoing messages drops them to a single email
    address.
    """

    def send_messages(self, email_messages):
        admins = getattr(settings, 'ADMINS', ())
        sever_email = getattr(settings, 'SERVER_EMAIL', 'root@localhost')
        bandit_email = getattr(settings, 'BANDIT_EMAIL', sever_email)
        approved_emails = set([sever_email, bandit_email, ] + [email for name, email in admins])
        for message in email_messages:
            all_approved = reduce(and_, map(lambda e: e in approved_emails, message.to))
            if not all_approved:            
                old_to = message.to
                context = {'previous_recipients': old_to}
                header = render_to_string("bandit/hijacked-email-header.txt", context)
                message.body = header + message.body
                message.to = [bandit_email, ]
        return super(HijackBackend, self).send_messages(email_messages)<commit_msg>convert HijackBackend into a Mixin so it can be used with other EmailBackends<commit_after>from operator import and_

from django.conf import settings
from django.core.mail.backends.smtp import EmailBackend as SMTPBackend
from django.template.loader import render_to_string


class HijackBackendMixin(object):
    """
    This backend mixin intercepts outgoing messages drops them to a single
    email address.
    """

    def send_messages(self, email_messages):
        admins = getattr(settings, 'ADMINS', ())
        server_email = getattr(settings, 'SERVER_EMAIL', 'root@localhost')
        bandit_email = getattr(settings, 'BANDIT_EMAIL', server_email)
        approved_emails = set([server_email, bandit_email, ] + [email for name, email in admins])
        for message in email_messages:
            all_approved = reduce(and_, map(lambda e: e in approved_emails, message.to))
            if not all_approved:            
                old_to = message.to
                context = {'previous_recipients': old_to}
                header = render_to_string("bandit/hijacked-email-header.txt", context)
                message.body = header + message.body
                message.to = [bandit_email, ]
        return super(HijackBackendMixin, self).send_messages(email_messages)


class HijackBackend(HijackBackendMixin, SMTPBackend):
    """
    This backend intercepts outgoing messages drops them to a single email
    address.
    """
    pass
{'fraction_non_alphanumeric': 0.08286464191976003, 'fraction_numerical': 0.0, 'mean_word_length': 2.976154992548435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '66063', 'n_tokens_mistral': 759, 'n_tokens_neox': 744, 'n_words': 200}