1995-10-19 09:35:31 +00:00
|
|
|
"""mailerdaemon - classes to parse mailer-daemon messages"""
|
|
|
|
|
|
|
|
import string
|
|
|
|
import rfc822
|
1996-07-21 02:50:30 +00:00
|
|
|
import calendar
|
1995-10-19 09:35:31 +00:00
|
|
|
import regex
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
Unparseable = 'mailerdaemon.Unparseable'
|
|
|
|
|
|
|
|
class ErrorMessage(rfc822.Message):
|
|
|
|
def __init__(self, fp):
|
1998-03-24 05:30:29 +00:00
|
|
|
rfc822.Message.__init__(self, fp)
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
def is_warning(self):
|
1998-03-24 05:30:29 +00:00
|
|
|
sub = self.getheader('Subject')
|
|
|
|
if not sub:
|
|
|
|
return 0
|
|
|
|
sub = string.lower(sub)
|
|
|
|
if sub[:12] == 'waiting mail': return 1
|
|
|
|
if string.find(sub, 'warning') >= 0: return 1
|
|
|
|
self.sub = sub
|
|
|
|
return 0
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
def get_errors(self):
|
1998-03-24 05:30:29 +00:00
|
|
|
for p in EMPARSERS:
|
|
|
|
self.rewindbody()
|
|
|
|
try:
|
|
|
|
return p(self.fp, self.sub)
|
|
|
|
except Unparseable:
|
|
|
|
pass
|
|
|
|
raise Unparseable
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
sendmail_pattern = regex.compile('[0-9][0-9][0-9] ')
|
1996-07-21 02:50:30 +00:00
|
|
|
def emparse_sendmail(fp, sub):
|
1995-10-19 09:35:31 +00:00
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
line = line[:-1]
|
1995-10-19 09:35:31 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
# Check that we're not in the returned message yet
|
|
|
|
if string.lower(line)[:5] == 'from:':
|
|
|
|
raise Unparseable
|
|
|
|
line = string.split(line)
|
|
|
|
if len(line) > 3 and \
|
|
|
|
((line[0] == '-----' and line[1] == 'Transcript') or
|
|
|
|
(line[0] == '---' and line[1] == 'The' and
|
|
|
|
line[2] == 'transcript') or
|
|
|
|
(line[0] == 'While' and
|
|
|
|
line[1] == 'talking' and
|
|
|
|
line[2] == 'to')):
|
|
|
|
# Yes, found it!
|
|
|
|
break
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
errors = []
|
|
|
|
found_a_line = 0
|
|
|
|
warnings = 0
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = line[:-1]
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if sendmail_pattern.match(line) == 4:
|
|
|
|
# Yes, an error/warning line. Ignore 4, remember 5, stop on rest
|
|
|
|
if line[0] == '5':
|
|
|
|
errors.append(line)
|
|
|
|
elif line[0] == '4':
|
|
|
|
warnings = 1
|
|
|
|
else:
|
|
|
|
raise Unparseable
|
|
|
|
line = string.split(line)
|
|
|
|
if line and line[0][:3] == '---':
|
|
|
|
break
|
|
|
|
found_a_line = 1
|
1996-07-21 02:50:30 +00:00
|
|
|
# special case for CWI sendmail
|
|
|
|
if len(line) > 1 and line[1] == 'Undelivered':
|
1998-03-24 05:30:29 +00:00
|
|
|
while 1:
|
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = string.strip(line)
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
errors.append(line + ': ' + sub)
|
1995-10-19 09:35:31 +00:00
|
|
|
# Empty transcripts are ok, others without an error are not.
|
|
|
|
if found_a_line and not (errors or warnings):
|
1998-03-24 05:30:29 +00:00
|
|
|
raise Unparseable
|
1995-10-19 09:35:31 +00:00
|
|
|
return errors
|
|
|
|
|
1996-07-21 02:50:30 +00:00
|
|
|
def emparse_cts(fp, sub):
|
1995-10-19 09:35:31 +00:00
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
line = line[:-1]
|
1995-10-19 09:35:31 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
# Check that we're not in the returned message yet
|
|
|
|
if string.lower(line)[:5] == 'from:':
|
|
|
|
raise Unparseable
|
|
|
|
line = string.split(line)
|
|
|
|
if len(line) > 3 and line[0][:2] == '|-' and line[1] == 'Failed' \
|
|
|
|
and line[2] == 'addresses':
|
|
|
|
# Yes, found it!
|
|
|
|
break
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
errors = []
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = line[:-1]
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line[:2] == '|-':
|
|
|
|
break
|
|
|
|
errors.append(line)
|
1995-10-19 09:35:31 +00:00
|
|
|
return errors
|
|
|
|
|
1996-07-21 02:50:30 +00:00
|
|
|
def emparse_aol(fp, sub):
|
1995-10-19 09:35:31 +00:00
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
line = line[:-1]
|
|
|
|
if line:
|
|
|
|
break
|
1995-10-19 09:35:31 +00:00
|
|
|
exp = 'The mail you sent could not be delivered to:'
|
|
|
|
if line[:len(exp)] != exp:
|
1998-03-24 05:30:29 +00:00
|
|
|
raise Unparseable
|
1995-10-19 09:35:31 +00:00
|
|
|
errors = []
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if sendmail_pattern.match(line) == 4:
|
|
|
|
# Yes, an error/warning line. Ignore 4, remember 5, stop on rest
|
|
|
|
if line[0] == '5':
|
|
|
|
errors.append(line)
|
|
|
|
elif line[0] != '4':
|
|
|
|
raise Unparseable
|
|
|
|
elif line == '\n':
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise Unparseable
|
1995-10-19 09:35:31 +00:00
|
|
|
return errors
|
1995-10-30 10:10:19 +00:00
|
|
|
|
1996-07-21 02:50:30 +00:00
|
|
|
def emparse_compuserve(fp, sub):
|
1995-10-30 10:10:19 +00:00
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
line = line[:-1]
|
|
|
|
if line:
|
|
|
|
break
|
1995-10-30 10:10:19 +00:00
|
|
|
exp = 'Your message could not be delivered for the following reason:'
|
|
|
|
if line[:len(exp)] != exp:
|
1998-03-24 05:30:29 +00:00
|
|
|
raise Unparseable
|
1995-10-30 10:10:19 +00:00
|
|
|
errors = []
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line: break
|
|
|
|
if line[:3] == '---': break
|
|
|
|
line = line[:-1]
|
|
|
|
if not line: continue
|
|
|
|
if line == 'Please resend your message at a later time.':
|
|
|
|
continue
|
|
|
|
line = 'Compuserve: ' + line
|
|
|
|
errors.append(line)
|
1995-10-30 10:10:19 +00:00
|
|
|
return errors
|
1995-10-19 09:35:31 +00:00
|
|
|
|
1995-10-30 10:23:10 +00:00
|
|
|
prov_pattern = regex.compile('.* | \(.*\)')
|
1996-07-21 02:50:30 +00:00
|
|
|
def emparse_providence(fp, sub):
|
1995-10-30 10:23:10 +00:00
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
line = line[:-1]
|
1995-10-30 10:23:10 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
# Check that we're not in the returned message yet
|
|
|
|
if string.lower(line)[:5] == 'from:':
|
|
|
|
raise Unparseable
|
|
|
|
exp = 'The following errors occurred'
|
|
|
|
if line[:len(exp)] == exp:
|
|
|
|
break
|
1995-10-30 10:23:10 +00:00
|
|
|
|
|
|
|
errors = []
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = line[:-1]
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line[:4] == '----':
|
|
|
|
break
|
|
|
|
if prov_pattern.match(line) > 0:
|
|
|
|
errors.append(prov_pattern.group(1))
|
1995-10-30 10:23:10 +00:00
|
|
|
|
|
|
|
if not errors:
|
1998-03-24 05:30:29 +00:00
|
|
|
raise Unparseable
|
1995-10-30 10:23:10 +00:00
|
|
|
return errors
|
1995-10-19 09:35:31 +00:00
|
|
|
|
1996-07-21 02:50:30 +00:00
|
|
|
def emparse_x400(fp, sub):
|
|
|
|
exp = 'This report relates to your message:'
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
line = line[:-1]
|
1996-07-21 02:50:30 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
# Check that we're not in the returned message yet
|
|
|
|
if string.lower(line)[:5] == 'from:':
|
|
|
|
raise Unparseable
|
|
|
|
if line[:len(exp)] == exp:
|
|
|
|
break
|
1996-07-21 02:50:30 +00:00
|
|
|
|
|
|
|
errors = []
|
|
|
|
exp = 'Your message was not delivered to'
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = line[:-1]
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line[:len(exp)] == exp:
|
|
|
|
error = string.strip(line[len(exp):])
|
|
|
|
sep = ': '
|
|
|
|
while 1:
|
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = line[:-1]
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
if line[0] == ' ' and line[-1] != ':':
|
|
|
|
error = error + sep + string.strip(line)
|
|
|
|
sep = '; '
|
|
|
|
errors.append(error)
|
|
|
|
return errors
|
1996-07-21 02:50:30 +00:00
|
|
|
raise Unparseable
|
|
|
|
|
|
|
|
def emparse_passau(fp, sub):
|
|
|
|
exp = 'Unable to deliver message because'
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
if string.lower(line)[:5] == 'from:':
|
|
|
|
raise Unparseable
|
|
|
|
if line[:len(exp)] == exp:
|
|
|
|
break
|
1996-07-21 02:50:30 +00:00
|
|
|
|
|
|
|
errors = []
|
|
|
|
exp = 'Returned Text follows'
|
|
|
|
while 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise Unparseable
|
|
|
|
line = line[:-1]
|
|
|
|
# Check that we're not in the returned message yet
|
|
|
|
if string.lower(line)[:5] == 'from:':
|
|
|
|
raise Unparseable
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line[:len(exp)] == exp:
|
|
|
|
return errors
|
|
|
|
errors.append(string.strip(line))
|
1996-07-21 02:50:30 +00:00
|
|
|
|
1995-10-30 10:23:10 +00:00
|
|
|
EMPARSERS = [emparse_sendmail, emparse_aol, emparse_cts, emparse_compuserve,
|
1998-03-24 05:30:29 +00:00
|
|
|
emparse_providence, emparse_x400, emparse_passau]
|
1996-07-21 02:50:30 +00:00
|
|
|
|
|
|
|
def sort_numeric(a, b):
|
|
|
|
a = string.atoi(a)
|
|
|
|
b = string.atoi(b)
|
|
|
|
if a < b: return -1
|
|
|
|
elif a > b: return 1
|
|
|
|
else: return 0
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
def parsedir(dir, modify):
|
|
|
|
os.chdir(dir)
|
|
|
|
pat = regex.compile('^[0-9]*$')
|
|
|
|
errordict = {}
|
1996-07-21 02:50:30 +00:00
|
|
|
errorfirst = {}
|
1995-10-19 09:35:31 +00:00
|
|
|
errorlast = {}
|
|
|
|
nok = nwarn = nbad = 0
|
1996-07-21 02:50:30 +00:00
|
|
|
|
|
|
|
# find all numeric file names and sort them
|
|
|
|
files = filter(lambda fn, pat=pat: pat.match(fn) > 0, os.listdir('.'))
|
|
|
|
files.sort(sort_numeric)
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
for fn in files:
|
1998-03-24 05:30:29 +00:00
|
|
|
# Lets try to parse the file.
|
|
|
|
fp = open(fn)
|
|
|
|
m = ErrorMessage(fp)
|
|
|
|
sender = m.getaddr('From')
|
|
|
|
print '%s\t%-40s\t'%(fn, sender[1]),
|
1995-10-19 09:35:31 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
if m.is_warning():
|
|
|
|
print 'warning only'
|
|
|
|
nwarn = nwarn + 1
|
|
|
|
if modify:
|
|
|
|
os.unlink(fn)
|
|
|
|
continue
|
1996-07-21 02:50:30 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
try:
|
|
|
|
errors = m.get_errors()
|
|
|
|
except Unparseable:
|
|
|
|
print '** Not parseable'
|
|
|
|
nbad = nbad + 1
|
|
|
|
continue
|
|
|
|
print len(errors), 'errors'
|
1996-07-21 02:50:30 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
# Remember them
|
|
|
|
for e in errors:
|
|
|
|
try:
|
|
|
|
mm, dd = m.getdate('date')[1:1+2]
|
|
|
|
date = '%s %02d' % (calendar.month_abbr[mm], dd)
|
|
|
|
except:
|
|
|
|
date = '??????'
|
|
|
|
if not errordict.has_key(e):
|
|
|
|
errordict[e] = 1
|
|
|
|
errorfirst[e] = '%s (%s)' % (fn, date)
|
|
|
|
else:
|
|
|
|
errordict[e] = errordict[e] + 1
|
|
|
|
errorlast[e] = '%s (%s)' % (fn, date)
|
1996-07-21 02:50:30 +00:00
|
|
|
|
1998-03-24 05:30:29 +00:00
|
|
|
nok = nok + 1
|
|
|
|
if modify:
|
|
|
|
os.unlink(fn)
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
print '--------------'
|
|
|
|
print nok, 'files parsed,',nwarn,'files warning-only,',
|
|
|
|
print nbad,'files unparseable'
|
|
|
|
print '--------------'
|
|
|
|
for e in errordict.keys():
|
1998-03-24 05:30:29 +00:00
|
|
|
print errordict[e], errorfirst[e], '-', errorlast[e], '\t', e
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
modify = 0
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == '-d':
|
1998-03-24 05:30:29 +00:00
|
|
|
modify = 1
|
|
|
|
del sys.argv[1]
|
1995-10-19 09:35:31 +00:00
|
|
|
if len(sys.argv) > 1:
|
1998-03-24 05:30:29 +00:00
|
|
|
for folder in sys.argv[1:]:
|
|
|
|
parsedir(folder, modify)
|
1995-10-19 09:35:31 +00:00
|
|
|
else:
|
1998-03-24 05:30:29 +00:00
|
|
|
parsedir('/ufs/jack/Mail/errorsinbox', modify)
|
1995-10-19 09:35:31 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__' or sys.argv[0] == __name__:
|
|
|
|
main()
|