~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/runners/bounce.py

  • Committer: klm
  • Date: 1998-01-07 21:21:35 UTC
  • Revision ID: vcs-imports@canonical.com-19980107212135-sv0y521ps0xye37r
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2001-2012 by the Free Software Foundation, Inc.
2
 
#
3
 
# This file is part of GNU Mailman.
4
 
#
5
 
# GNU Mailman is free software: you can redistribute it and/or modify it under
6
 
# the terms of the GNU General Public License as published by the Free
7
 
# Software Foundation, either version 3 of the License, or (at your option)
8
 
# any later version.
9
 
#
10
 
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11
 
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
 
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13
 
# more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License along with
16
 
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
"""Bounce runner."""
19
 
 
20
 
import logging
21
 
 
22
 
from flufl.bounce import all_failures, scan_message
23
 
from zope.component import getUtility
24
 
 
25
 
from mailman.app.bounces import ProbeVERP, StandardVERP, maybe_forward
26
 
from mailman.core.runner import Runner
27
 
from mailman.interfaces.bounce import BounceContext, IBounceProcessor
28
 
 
29
 
 
30
 
COMMASPACE = ', '
31
 
 
32
 
log = logging.getLogger('mailman.bounce')
33
 
elog = logging.getLogger('mailman.error')
34
 
 
35
 
 
36
 
 
37
 
class BounceRunner(Runner):
38
 
    """The bounce runner."""
39
 
 
40
 
    def __init__(self, name, slice=None):
41
 
        super(BounceRunner, self).__init__(name, slice)
42
 
        self._processor = getUtility(IBounceProcessor)
43
 
 
44
 
    def _dispose(self, mlist, msg, msgdata):
45
 
        # List isn't doing bounce processing?
46
 
        if not mlist.process_bounces:
47
 
            return False
48
 
        # Try VERP detection first, since it's quick and easy
49
 
        context = BounceContext.normal
50
 
        addresses = StandardVERP().get_verp(mlist, msg)
51
 
        if len(addresses) > 0:
52
 
            # Scan the message to see if it contained permanent or temporary
53
 
            # failures.  We'll ignore temporary failures, but even if there
54
 
            # are no permanent failures, we'll assume VERP bounces are
55
 
            # permanent.
56
 
            temporary, permanent = all_failures(msg)
57
 
            if len(temporary) > 0:
58
 
                # This was a temporary failure, so just ignore it.
59
 
                return False
60
 
        else:
61
 
            # See if this was a probe message.
62
 
            addresses = ProbeVERP().get_verp(mlist, msg)
63
 
            if len(addresses) > 0:
64
 
                context = BounceContext.probe
65
 
            else:
66
 
                # That didn't give us anything useful, so try the old fashion
67
 
                # bounce matching modules.  This returns only the permanently
68
 
                # failing addresses.  Since Mailman currently doesn't score
69
 
                # temporary failures, if we get no permanent failures, we're
70
 
                # done.s
71
 
                addresses = scan_message(msg)
72
 
        # If that still didn't return us any useful addresses, then send it on
73
 
        # or discard it.  The addresses will come back from flufl.bounce as
74
 
        # bytes/8-bit strings, but we must store them as unicodes in the
75
 
        # database.  Assume utf-8 encoding, but be cautious.
76
 
        if len(addresses) > 0:
77
 
            for address in addresses:
78
 
                if isinstance(address, bytes):
79
 
                    try:
80
 
                        address = address.decode('utf-8')
81
 
                    except UnicodeError:
82
 
                        log.exception('Ignoring non-UTF-8 encoded '
83
 
                                      'address: {0}'.format(address))
84
 
                        continue
85
 
                self._processor.register(mlist, address, msg, context)
86
 
        else:
87
 
            log.info('Bounce message w/no discernable addresses: %s',
88
 
                     msg.get('message-id', 'n/a'))
89
 
            maybe_forward(mlist, msg)
90
 
        # Dequeue this message.
91
 
        return False