Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 1 | """mailerdaemon - classes to parse mailer-daemon messages""" |
| 2 | |
| 3 | import string |
| 4 | import rfc822 |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 5 | import calendar |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 6 | import regex |
| 7 | import os |
| 8 | import sys |
| 9 | |
| 10 | Unparseable = 'mailerdaemon.Unparseable' |
| 11 | |
| 12 | class ErrorMessage(rfc822.Message): |
| 13 | def __init__(self, fp): |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 14 | rfc822.Message.__init__(self, fp) |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 15 | |
| 16 | def is_warning(self): |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 17 | 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 25 | |
| 26 | def get_errors(self): |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 27 | 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 34 | |
| 35 | sendmail_pattern = regex.compile('[0-9][0-9][0-9] ') |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 36 | def emparse_sendmail(fp, sub): |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 37 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 38 | line = fp.readline() |
| 39 | if not line: |
| 40 | raise Unparseable |
| 41 | line = line[:-1] |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 43 | # 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 56 | |
| 57 | errors = [] |
| 58 | found_a_line = 0 |
| 59 | warnings = 0 |
| 60 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 61 | 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 Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 79 | # special case for CWI sendmail |
| 80 | if len(line) > 1 and line[1] == 'Undelivered': |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 81 | 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 89 | # Empty transcripts are ok, others without an error are not. |
| 90 | if found_a_line and not (errors or warnings): |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 91 | raise Unparseable |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 92 | return errors |
| 93 | |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 94 | def emparse_cts(fp, sub): |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 95 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 96 | line = fp.readline() |
| 97 | if not line: |
| 98 | raise Unparseable |
| 99 | line = line[:-1] |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 101 | # 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 109 | |
| 110 | errors = [] |
| 111 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 112 | 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 121 | return errors |
| 122 | |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 123 | def emparse_aol(fp, sub): |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 124 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 125 | line = fp.readline() |
| 126 | if not line: |
| 127 | raise Unparseable |
| 128 | line = line[:-1] |
| 129 | if line: |
| 130 | break |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 131 | exp = 'The mail you sent could not be delivered to:' |
| 132 | if line[:len(exp)] != exp: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 133 | raise Unparseable |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 134 | errors = [] |
| 135 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 136 | 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 147 | return errors |
Jack Jansen | 4ec940a | 1995-10-30 10:10:19 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 149 | def emparse_compuserve(fp, sub): |
Jack Jansen | 4ec940a | 1995-10-30 10:10:19 +0000 | [diff] [blame] | 150 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 151 | line = fp.readline() |
| 152 | if not line: |
| 153 | raise Unparseable |
| 154 | line = line[:-1] |
| 155 | if line: |
| 156 | break |
Jack Jansen | 4ec940a | 1995-10-30 10:10:19 +0000 | [diff] [blame] | 157 | exp = 'Your message could not be delivered for the following reason:' |
| 158 | if line[:len(exp)] != exp: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 159 | raise Unparseable |
Jack Jansen | 4ec940a | 1995-10-30 10:10:19 +0000 | [diff] [blame] | 160 | errors = [] |
| 161 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 162 | 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 Jansen | 4ec940a | 1995-10-30 10:10:19 +0000 | [diff] [blame] | 171 | return errors |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 172 | |
Jack Jansen | 81299f1 | 1995-10-30 10:23:10 +0000 | [diff] [blame] | 173 | prov_pattern = regex.compile('.* | \(.*\)') |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 174 | def emparse_providence(fp, sub): |
Jack Jansen | 81299f1 | 1995-10-30 10:23:10 +0000 | [diff] [blame] | 175 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 176 | line = fp.readline() |
| 177 | if not line: |
| 178 | raise Unparseable |
| 179 | line = line[:-1] |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 181 | # 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 Jansen | 81299f1 | 1995-10-30 10:23:10 +0000 | [diff] [blame] | 187 | |
| 188 | errors = [] |
| 189 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 190 | 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 Jansen | 81299f1 | 1995-10-30 10:23:10 +0000 | [diff] [blame] | 200 | |
| 201 | if not errors: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 202 | raise Unparseable |
Jack Jansen | 81299f1 | 1995-10-30 10:23:10 +0000 | [diff] [blame] | 203 | return errors |
| 204 | |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 205 | def emparse_x400(fp, sub): |
| 206 | exp = 'This report relates to your message:' |
| 207 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 208 | line = fp.readline() |
| 209 | if not line: |
| 210 | raise Unparseable |
| 211 | line = line[:-1] |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 213 | # 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 Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 218 | |
| 219 | errors = [] |
| 220 | exp = 'Your message was not delivered to' |
| 221 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 222 | 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 Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 243 | raise Unparseable |
| 244 | |
| 245 | def emparse_passau(fp, sub): |
| 246 | exp = 'Unable to deliver message because' |
| 247 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 248 | 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 Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 255 | |
| 256 | errors = [] |
| 257 | exp = 'Returned Text follows' |
| 258 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 259 | 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 Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 271 | |
Jack Jansen | 81299f1 | 1995-10-30 10:23:10 +0000 | [diff] [blame] | 272 | EMPARSERS = [emparse_sendmail, emparse_aol, emparse_cts, emparse_compuserve, |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 273 | emparse_providence, emparse_x400, emparse_passau] |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 274 | |
| 275 | def 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 281 | |
| 282 | def parsedir(dir, modify): |
| 283 | os.chdir(dir) |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 284 | pat = regex.compile('^[0-9]*$') |
| 285 | errordict = {} |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 286 | errorfirst = {} |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 287 | errorlast = {} |
| 288 | nok = nwarn = nbad = 0 |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 289 | |
| 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 293 | |
| 294 | for fn in files: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 295 | # 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 Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 301 | if m.is_warning(): |
| 302 | print 'warning only' |
| 303 | nwarn = nwarn + 1 |
| 304 | if modify: |
| 305 | os.unlink(fn) |
| 306 | continue |
Guido van Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 308 | 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 Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 316 | # 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 Rossum | 8e7a54f | 1996-07-21 02:50:30 +0000 | [diff] [blame] | 329 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 330 | nok = nok + 1 |
| 331 | if modify: |
| 332 | os.unlink(fn) |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 333 | |
| 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 Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 339 | print errordict[e], errorfirst[e], '-', errorlast[e], '\t', e |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 340 | |
| 341 | def main(): |
| 342 | modify = 0 |
| 343 | if len(sys.argv) > 1 and sys.argv[1] == '-d': |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 344 | modify = 1 |
| 345 | del sys.argv[1] |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 346 | if len(sys.argv) > 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 347 | for folder in sys.argv[1:]: |
| 348 | parsedir(folder, modify) |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 349 | else: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 350 | parsedir('/ufs/jack/Mail/errorsinbox', modify) |
Sjoerd Mullender | 8968624 | 1995-10-19 09:35:31 +0000 | [diff] [blame] | 351 | |
| 352 | if __name__ == '__main__' or sys.argv[0] == __name__: |
| 353 | main() |