aboutsummaryrefslogtreecommitdiffstats
path: root/Mailman/Bouncers/Postfix.py
blob: 148766457f92b18deee6e4f35011780d5e9923d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright (C) 1998-2003 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""Parse bounce messages generated by Postfix.

This also matches something called `Keftamail' which looks just like Postfix
bounces with the word Postfix scratched out and the word `Keftamail' written
in in crayon.

It also matches something claiming to be `The BNS Postfix program', and
`SMTP_Gateway'.  Everybody's gotta be different, huh?
"""

import re
from cStringIO import StringIO



def flatten(msg, leaves):
    # give us all the leaf (non-multipart) subparts
    if msg.is_multipart():
        for part in msg.get_payload():
            flatten(part, leaves)
    else:
        leaves.append(msg)



# are these heuristics correct or guaranteed?
pcre = re.compile(r'[ \t]*the\s*(bns)?\s*(postfix|keftamail|smtp_gateway)',
                  re.IGNORECASE)
rcre = re.compile(r'failure reason:$', re.IGNORECASE)
acre = re.compile(r'<(?P<addr>[^>]*)>:')

def findaddr(msg):
    addrs = []
    body = StringIO(msg.get_payload())
    # simple state machine
    #     0 == nothing found
    #     1 == salutation found
    state = 0
    while 1:
        line = body.readline()
        if not line:
            break
        # preserve leading whitespace
        line = line.rstrip()
        # yes use match to match at beginning of string
        if state == 0 and (pcre.match(line) or rcre.match(line)):
            state = 1
        elif state == 1 and line:
            mo = acre.search(line)
            if mo:
                addrs.append(mo.group('addr'))
            # probably a continuation line
    return addrs



def process(msg):
    if msg.get_type() not in ('multipart/mixed', 'multipart/report'):
        return None
    # We're looking for the plain/text subpart with a Content-Description: of
    # `notification'.
    leaves = []
    flatten(msg, leaves)
    for subpart in leaves:
        if subpart.get_type() == 'text/plain' and \
           subpart.get('content-description', '').lower() == 'notification':
            # then...
            return findaddr(subpart)
    return None