"""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):
if msg.is_multipart():
for part in msg.get_payload():
flatten(part, leaves)
else:
leaves.append(msg)
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())
state = 0
while 1:
line = body.readline()
if not line:
break
line = line.rstrip()
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'))
return addrs
def process(msg):
if msg.get_content_type() not in ('multipart/mixed', 'multipart/report'):
return None
leaves = []
flatten(msg, leaves)
for subpart in leaves:
if subpart.get_content_type() == 'text/plain' and \
subpart.get('content-description', '').lower() == 'notification':
return findaddr(subpart)
return None