<commit_before>import logging

from django.conf import settings
from slimta.policy import RelayPolicy

from munch.core.mail.utils import UniqueEmailAddressParser

log = logging.getLogger(__name__)

return_path_parser = UniqueEmailAddressParser(
    domain=lambda: settings.RETURNPATH_DOMAIN, prefix='return-')


class RewriteReturnPath(RelayPolicy):
    """ Rewrite existing ReturnPath based on some conditions """
    def apply(self, envelope):
        # If there is X-HTTP-Return-Path
        if settings.TRANSACTIONAL['X_HTTP_DSN_RETURN_PATH_HEADER'] in \
                envelope.headers:
            # Rewrite it
            envelope.sender = return_path_parser.new(
                envelope.headers[settings.TRANSACTIONAL[
                    'X_MESSAGE_ID_HEADER']])
        # If there is no X-HTTP-Return-Path
        else:
            log.info(
                '[{}] Keeping original Return-Path ({}) because there is no '
                '"{}" (then DSN will be sent to original sender).'.format(
                    envelope.headers[settings.TRANSACTIONAL[
                        'X_MESSAGE_ID_HEADER']],
                    envelope.sender,
                    settings.TRANSACTIONAL['X_HTTP_DSN_RETURN_PATH_HEADER']))
<commit_msg>rewrite return-path when X-SMTP-Return-Path header is present<commit_after>import logging

from django.conf import settings
from slimta.policy import RelayPolicy

from munch.core.mail.utils import UniqueEmailAddressParser

log = logging.getLogger(__name__)

return_path_parser = UniqueEmailAddressParser(
    domain=lambda: settings.RETURNPATH_DOMAIN, prefix='return-')


class RewriteReturnPath(RelayPolicy):
    """ Rewrite existing ReturnPath based on some conditions """
    def apply(self, envelope):
        # If there is X-HTTP-Return-Path
        if settings.TRANSACTIONAL['X_HTTP_DSN_RETURN_PATH_HEADER'] in \
                envelope.headers:
            # Rewrite it
            envelope.sender = return_path_parser.new(
                envelope.headers[settings.TRANSACTIONAL[
                    'X_MESSAGE_ID_HEADER']])
        elif settings.TRANSACTIONAL['X_SMTP_DSN_RETURN_PATH_HEADER'] in \
                envelope.headers:
            # Rewrite it
            envelope.sender = return_path_parser.new(
                envelope.headers[settings.TRANSACTIONAL[
                    'X_MESSAGE_ID_HEADER']])
        # If there is no X-HTTP-Return-Path or X-SMTP-Return-Path
        else:
            log.info(
                '[{}] Keeping original Return-Path ({}) because there are no '
                '"{}" or "{}" headers (then DSN will be sent to original sender).'.format(
                    envelope.headers[settings.TRANSACTIONAL[
                        'X_MESSAGE_ID_HEADER']],
                    envelope.sender,
                    settings.TRANSACTIONAL['X_HTTP_DSN_RETURN_PATH_HEADER'],
                    settings.TRANSACTIONAL['X_SMTP_DSN_RETURN_PATH_HEADER']))

{'fraction_non_alphanumeric': 0.08738524311458687, 'fraction_numerical': 0.0, 'mean_word_length': 2.3318233295583237, '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': '6885016', 'n_tokens_mistral': 816, 'n_tokens_neox': 794, 'n_words': 167}