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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# Copyright (C) 1998,1999,2000 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 RFC 1894 (i.e. DSN) bounce formats."""
import string
import multifile
import mimetools
from Mailman.pythonlib.StringIO import StringIO
def parseaddr(val):
try:
atype, addr = string.split(val, ';')
except ValueError:
# Bogus format for Original-Recipient: or Final-Recipient: fields
return None
if string.lower(string.strip(atype)) <> 'rfc822':
return None
addr = string.strip(addr)
if not addr:
return None
# strip off <>
if addr[0] == '<' and addr[-1] == '>':
addr = addr[1:-1]
return addr
def process(msg):
if msg.gettype() <> 'multipart/report' or \
msg.getparam('report-type') <> 'delivery-status':
# then
return None
boundary = msg.getparam('boundary')
msg.fp.seek(0)
mfile = multifile.MultiFile(msg.fp)
mfile.push(boundary)
# find the subpart with message/delivery-status information
while 1:
try:
more = mfile.next()
except multifile.Error:
# the message *looked* like a DSN, but it really wasn't :(
return None
if not more:
# we didn't find it
return None
try:
s = StringIO(mfile.read())
except multifile.Error:
# It is a mis-formated or incomplete message
return None
msg2 = mimetools.Message(s)
if msg2.gettype() == 'message/delivery-status':
# hmm, could there be more than one DSN per message?
break
# now parse out the per-recipient fields, which are separated by blank
# lines. the first block is actually the per-notification fields, but
# those can be safely ignored
#
# we try to dig out the Original-Recipient (which is optional) and
# Final-Recipient (which is mandatory, but may not exactly match an addr
# on our list). Some MTA's also use X-Actual-Recipeint as a synonym for
# Original-Recipeint, but some apparently use that for other purposes :(
#
# Also grok out Action so we can do something with that too.
blocks = []
headers = {}
while 1:
line = msg2.fp.readline()
if not line:
break
line = string.strip(line)
if not line:
# a new recipient block
blocks.append(headers)
headers = {}
try:
hdr, val = string.split(line, ':', 1)
except ValueError:
continue
headers[string.lower(hdr)] = string.strip(val)
# now go through all the blocks, finding the recip address that is being
# reported.
addrs = []
for headers in blocks:
if string.lower(headers.get('action', '')) <> 'failed':
# ignore this block
continue
# preference order is original-recipient, final-recipient
## val = headers.get('original-recipient',
## headers.get('x-actual-recipient',
## headers.get('final-recipient')))
val = headers.get('original-recipient',
headers.get('final-recipient'))
if val:
addrs.append(parseaddr(val))
return filter(None, addrs) or None
|