blob: c065eb2adb3e2d9672f4a1c4616811f5ea758d82 [file] [log] [blame]
Sjoerd Mullender89686241995-10-19 09:35:31 +00001"""mailerdaemon - classes to parse mailer-daemon messages"""
2
3import string
4import rfc822
Guido van Rossum8e7a54f1996-07-21 02:50:30 +00005import calendar
Sjoerd Mullender89686241995-10-19 09:35:31 +00006import regex
7import os
8import sys
9
10Unparseable = 'mailerdaemon.Unparseable'
11
12class ErrorMessage(rfc822.Message):
13 def __init__(self, fp):
Guido van Rossumed5b3d81998-03-24 05:30:29 +000014 rfc822.Message.__init__(self, fp)
Sjoerd Mullender89686241995-10-19 09:35:31 +000015
16 def is_warning(self):
Guido van Rossumed5b3d81998-03-24 05:30:29 +000017 sub = self.getheader('Subject')
18 if not sub:
19 return 0
20 sub = string.lower(sub)
21 if sub[:12] == 'waiting mail': return 1
22 if string.find(sub, 'warning') >= 0: return 1
23 self.sub = sub
24 return 0
Sjoerd Mullender89686241995-10-19 09:35:31 +000025
26 def get_errors(self):
Guido van Rossumed5b3d81998-03-24 05:30:29 +000027 for p in EMPARSERS:
28 self.rewindbody()
29 try:
30 return p(self.fp, self.sub)
31 except Unparseable:
32 pass
33 raise Unparseable
Sjoerd Mullender89686241995-10-19 09:35:31 +000034
35sendmail_pattern = regex.compile('[0-9][0-9][0-9] ')
Guido van Rossum8e7a54f1996-07-21 02:50:30 +000036def emparse_sendmail(fp, sub):
Sjoerd Mullender89686241995-10-19 09:35:31 +000037 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +000038 line = fp.readline()
39 if not line:
40 raise Unparseable
41 line = line[:-1]
Sjoerd Mullender89686241995-10-19 09:35:31 +000042
Guido van Rossumed5b3d81998-03-24 05:30:29 +000043 # Check that we're not in the returned message yet
44 if string.lower(line)[:5] == 'from:':
45 raise Unparseable
46 line = string.split(line)
47 if len(line) > 3 and \
48 ((line[0] == '-----' and line[1] == 'Transcript') or
49 (line[0] == '---' and line[1] == 'The' and
50 line[2] == 'transcript') or
51 (line[0] == 'While' and
52 line[1] == 'talking' and
53 line[2] == 'to')):
54 # Yes, found it!
55 break
Sjoerd Mullender89686241995-10-19 09:35:31 +000056
57 errors = []
58 found_a_line = 0
59 warnings = 0
60 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +000061 line = fp.readline()
62 if not line:
63 break
64 line = line[:-1]
65 if not line:
66 continue
67 if sendmail_pattern.match(line) == 4:
68 # Yes, an error/warning line. Ignore 4, remember 5, stop on rest
69 if line[0] == '5':
70 errors.append(line)
71 elif line[0] == '4':
72 warnings = 1
73 else:
74 raise Unparseable
75 line = string.split(line)
76 if line and line[0][:3] == '---':
77 break
78 found_a_line = 1
Guido van Rossum8e7a54f1996-07-21 02:50:30 +000079 # special case for CWI sendmail
80 if len(line) > 1 and line[1] == 'Undelivered':
Guido van Rossumed5b3d81998-03-24 05:30:29 +000081 while 1:
82 line = fp.readline()
83 if not line:
84 break
85 line = string.strip(line)
86 if not line:
87 break
88 errors.append(line + ': ' + sub)
Sjoerd Mullender89686241995-10-19 09:35:31 +000089 # Empty transcripts are ok, others without an error are not.
90 if found_a_line and not (errors or warnings):
Guido van Rossumed5b3d81998-03-24 05:30:29 +000091 raise Unparseable
Sjoerd Mullender89686241995-10-19 09:35:31 +000092 return errors
93
Guido van Rossum8e7a54f1996-07-21 02:50:30 +000094def emparse_cts(fp, sub):
Sjoerd Mullender89686241995-10-19 09:35:31 +000095 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +000096 line = fp.readline()
97 if not line:
98 raise Unparseable
99 line = line[:-1]
Sjoerd Mullender89686241995-10-19 09:35:31 +0000100
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000101 # Check that we're not in the returned message yet
102 if string.lower(line)[:5] == 'from:':
103 raise Unparseable
104 line = string.split(line)
105 if len(line) > 3 and line[0][:2] == '|-' and line[1] == 'Failed' \
106 and line[2] == 'addresses':
107 # Yes, found it!
108 break
Sjoerd Mullender89686241995-10-19 09:35:31 +0000109
110 errors = []
111 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000112 line = fp.readline()
113 if not line:
114 break
115 line = line[:-1]
116 if not line:
117 continue
118 if line[:2] == '|-':
119 break
120 errors.append(line)
Sjoerd Mullender89686241995-10-19 09:35:31 +0000121 return errors
122
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000123def emparse_aol(fp, sub):
Sjoerd Mullender89686241995-10-19 09:35:31 +0000124 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000125 line = fp.readline()
126 if not line:
127 raise Unparseable
128 line = line[:-1]
129 if line:
130 break
Sjoerd Mullender89686241995-10-19 09:35:31 +0000131 exp = 'The mail you sent could not be delivered to:'
132 if line[:len(exp)] != exp:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000133 raise Unparseable
Sjoerd Mullender89686241995-10-19 09:35:31 +0000134 errors = []
135 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000136 line = fp.readline()
137 if sendmail_pattern.match(line) == 4:
138 # Yes, an error/warning line. Ignore 4, remember 5, stop on rest
139 if line[0] == '5':
140 errors.append(line)
141 elif line[0] != '4':
142 raise Unparseable
143 elif line == '\n':
144 break
145 else:
146 raise Unparseable
Sjoerd Mullender89686241995-10-19 09:35:31 +0000147 return errors
Jack Jansen4ec940a1995-10-30 10:10:19 +0000148
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000149def emparse_compuserve(fp, sub):
Jack Jansen4ec940a1995-10-30 10:10:19 +0000150 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000151 line = fp.readline()
152 if not line:
153 raise Unparseable
154 line = line[:-1]
155 if line:
156 break
Jack Jansen4ec940a1995-10-30 10:10:19 +0000157 exp = 'Your message could not be delivered for the following reason:'
158 if line[:len(exp)] != exp:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000159 raise Unparseable
Jack Jansen4ec940a1995-10-30 10:10:19 +0000160 errors = []
161 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000162 line = fp.readline()
163 if not line: break
164 if line[:3] == '---': break
165 line = line[:-1]
166 if not line: continue
167 if line == 'Please resend your message at a later time.':
168 continue
169 line = 'Compuserve: ' + line
170 errors.append(line)
Jack Jansen4ec940a1995-10-30 10:10:19 +0000171 return errors
Sjoerd Mullender89686241995-10-19 09:35:31 +0000172
Jack Jansen81299f11995-10-30 10:23:10 +0000173prov_pattern = regex.compile('.* | \(.*\)')
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000174def emparse_providence(fp, sub):
Jack Jansen81299f11995-10-30 10:23:10 +0000175 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000176 line = fp.readline()
177 if not line:
178 raise Unparseable
179 line = line[:-1]
Sjoerd Mullender89686241995-10-19 09:35:31 +0000180
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000181 # Check that we're not in the returned message yet
182 if string.lower(line)[:5] == 'from:':
183 raise Unparseable
184 exp = 'The following errors occurred'
185 if line[:len(exp)] == exp:
186 break
Jack Jansen81299f11995-10-30 10:23:10 +0000187
188 errors = []
189 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000190 line = fp.readline()
191 if not line:
192 break
193 line = line[:-1]
194 if not line:
195 continue
196 if line[:4] == '----':
197 break
198 if prov_pattern.match(line) > 0:
199 errors.append(prov_pattern.group(1))
Jack Jansen81299f11995-10-30 10:23:10 +0000200
201 if not errors:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000202 raise Unparseable
Jack Jansen81299f11995-10-30 10:23:10 +0000203 return errors
204
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000205def emparse_x400(fp, sub):
206 exp = 'This report relates to your message:'
207 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000208 line = fp.readline()
209 if not line:
210 raise Unparseable
211 line = line[:-1]
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000212
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000213 # Check that we're not in the returned message yet
214 if string.lower(line)[:5] == 'from:':
215 raise Unparseable
216 if line[:len(exp)] == exp:
217 break
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000218
219 errors = []
220 exp = 'Your message was not delivered to'
221 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000222 line = fp.readline()
223 if not line:
224 break
225 line = line[:-1]
226 if not line:
227 continue
228 if line[:len(exp)] == exp:
229 error = string.strip(line[len(exp):])
230 sep = ': '
231 while 1:
232 line = fp.readline()
233 if not line:
234 break
235 line = line[:-1]
236 if not line:
237 break
238 if line[0] == ' ' and line[-1] != ':':
239 error = error + sep + string.strip(line)
240 sep = '; '
241 errors.append(error)
242 return errors
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000243 raise Unparseable
244
245def emparse_passau(fp, sub):
246 exp = 'Unable to deliver message because'
247 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000248 line = fp.readline()
249 if not line:
250 raise Unparseable
251 if string.lower(line)[:5] == 'from:':
252 raise Unparseable
253 if line[:len(exp)] == exp:
254 break
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000255
256 errors = []
257 exp = 'Returned Text follows'
258 while 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000259 line = fp.readline()
260 if not line:
261 raise Unparseable
262 line = line[:-1]
263 # Check that we're not in the returned message yet
264 if string.lower(line)[:5] == 'from:':
265 raise Unparseable
266 if not line:
267 continue
268 if line[:len(exp)] == exp:
269 return errors
270 errors.append(string.strip(line))
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000271
Jack Jansen81299f11995-10-30 10:23:10 +0000272EMPARSERS = [emparse_sendmail, emparse_aol, emparse_cts, emparse_compuserve,
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000273 emparse_providence, emparse_x400, emparse_passau]
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000274
275def sort_numeric(a, b):
276 a = string.atoi(a)
277 b = string.atoi(b)
278 if a < b: return -1
279 elif a > b: return 1
280 else: return 0
Sjoerd Mullender89686241995-10-19 09:35:31 +0000281
282def parsedir(dir, modify):
283 os.chdir(dir)
Sjoerd Mullender89686241995-10-19 09:35:31 +0000284 pat = regex.compile('^[0-9]*$')
285 errordict = {}
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000286 errorfirst = {}
Sjoerd Mullender89686241995-10-19 09:35:31 +0000287 errorlast = {}
288 nok = nwarn = nbad = 0
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000289
290 # find all numeric file names and sort them
291 files = filter(lambda fn, pat=pat: pat.match(fn) > 0, os.listdir('.'))
292 files.sort(sort_numeric)
Sjoerd Mullender89686241995-10-19 09:35:31 +0000293
294 for fn in files:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000295 # Lets try to parse the file.
296 fp = open(fn)
297 m = ErrorMessage(fp)
298 sender = m.getaddr('From')
299 print '%s\t%-40s\t'%(fn, sender[1]),
Sjoerd Mullender89686241995-10-19 09:35:31 +0000300
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000301 if m.is_warning():
302 print 'warning only'
303 nwarn = nwarn + 1
304 if modify:
305 os.unlink(fn)
306 continue
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000307
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000308 try:
309 errors = m.get_errors()
310 except Unparseable:
311 print '** Not parseable'
312 nbad = nbad + 1
313 continue
314 print len(errors), 'errors'
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000315
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000316 # Remember them
317 for e in errors:
318 try:
319 mm, dd = m.getdate('date')[1:1+2]
320 date = '%s %02d' % (calendar.month_abbr[mm], dd)
321 except:
322 date = '??????'
323 if not errordict.has_key(e):
324 errordict[e] = 1
325 errorfirst[e] = '%s (%s)' % (fn, date)
326 else:
327 errordict[e] = errordict[e] + 1
328 errorlast[e] = '%s (%s)' % (fn, date)
Guido van Rossum8e7a54f1996-07-21 02:50:30 +0000329
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000330 nok = nok + 1
331 if modify:
332 os.unlink(fn)
Sjoerd Mullender89686241995-10-19 09:35:31 +0000333
334 print '--------------'
335 print nok, 'files parsed,',nwarn,'files warning-only,',
336 print nbad,'files unparseable'
337 print '--------------'
338 for e in errordict.keys():
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000339 print errordict[e], errorfirst[e], '-', errorlast[e], '\t', e
Sjoerd Mullender89686241995-10-19 09:35:31 +0000340
341def main():
342 modify = 0
343 if len(sys.argv) > 1 and sys.argv[1] == '-d':
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000344 modify = 1
345 del sys.argv[1]
Sjoerd Mullender89686241995-10-19 09:35:31 +0000346 if len(sys.argv) > 1:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000347 for folder in sys.argv[1:]:
348 parsedir(folder, modify)
Sjoerd Mullender89686241995-10-19 09:35:31 +0000349 else:
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000350 parsedir('/ufs/jack/Mail/errorsinbox', modify)
Sjoerd Mullender89686241995-10-19 09:35:31 +0000351
352if __name__ == '__main__' or sys.argv[0] == __name__:
353 main()