summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/mailerdaemon.py
blob: c065eb2adb3e2d9672f4a1c4616811f5ea758d82 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""mailerdaemon - classes to parse mailer-daemon messages"""

import string
import rfc822
import calendar
import regex
import os
import sys

Unparseable = 'mailerdaemon.Unparseable'

class ErrorMessage(rfc822.Message):
    def __init__(self, fp):
        rfc822.Message.__init__(self, fp)

    def is_warning(self):
        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

    def get_errors(self):
        for p in EMPARSERS:
            self.rewindbody()
            try:
                return p(self.fp, self.sub)
            except Unparseable:
                pass
        raise Unparseable

sendmail_pattern = regex.compile('[0-9][0-9][0-9] ')
def emparse_sendmail(fp, sub):
    while 1:
        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
        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

    errors = []
    found_a_line = 0
    warnings = 0
    while 1:
        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
    # special case for CWI sendmail
    if len(line) > 1 and line[1] == 'Undelivered':
        while 1:
            line = fp.readline()
            if not line:
                break
            line = string.strip(line)
            if not line:
                break
            errors.append(line + ': ' + sub)
    # Empty transcripts are ok, others without an error are not.
    if found_a_line and not (errors or warnings):
        raise Unparseable
    return errors
    
def emparse_cts(fp, sub):
    while 1:
        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
        line = string.split(line)
        if len(line) > 3 and line[0][:2] == '|-' and line[1] == 'Failed' \
           and line[2] == 'addresses':
            # Yes, found it!
            break

    errors = []
    while 1:
        line = fp.readline()
        if not line:
            break
        line = line[:-1]
        if not line:
            continue
        if line[:2] == '|-':
            break
        errors.append(line)
    return errors

def emparse_aol(fp, sub):
    while 1:
        line = fp.readline()
        if not line:
            raise Unparseable
        line = line[:-1]
        if line:
            break
    exp = 'The mail you sent could not be delivered to:'
    if line[:len(exp)] != exp:
        raise Unparseable
    errors = []
    while 1:
        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
    return errors
    
def emparse_compuserve(fp, sub):
    while 1:
        line = fp.readline()
        if not line:
            raise Unparseable
        line = line[:-1]
        if line:
            break
    exp = 'Your message could not be delivered for the following reason:'
    if line[:len(exp)] != exp:
        raise Unparseable
    errors = []
    while 1:
        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)
    return errors

prov_pattern = regex.compile('.* | \(.*\)')
def emparse_providence(fp, sub):
    while 1:
        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
        exp = 'The following errors occurred'
        if line[:len(exp)] == exp:
            break

    errors = []
    while 1:
        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))

    if not errors:
        raise Unparseable
    return errors

def emparse_x400(fp, sub):
    exp = 'This report relates to your message:'
    while 1:
        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 line[:len(exp)] == exp:
            break

    errors = []
    exp = 'Your message was not delivered to'
    while 1:
        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
    raise Unparseable

def emparse_passau(fp, sub):
    exp = 'Unable to deliver message because'
    while 1:
        line = fp.readline()
        if not line:
            raise Unparseable
        if string.lower(line)[:5] == 'from:':
            raise Unparseable
        if line[:len(exp)] == exp:
            break

    errors = []
    exp = 'Returned Text follows'
    while 1:
        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))

EMPARSERS = [emparse_sendmail, emparse_aol, emparse_cts, emparse_compuserve,
             emparse_providence, emparse_x400, emparse_passau]

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

def parsedir(dir, modify):
    os.chdir(dir)
    pat = regex.compile('^[0-9]*$')
    errordict = {}
    errorfirst = {}
    errorlast = {}
    nok = nwarn = nbad = 0

    # find all numeric file names and sort them
    files = filter(lambda fn, pat=pat: pat.match(fn) > 0, os.listdir('.'))
    files.sort(sort_numeric)
    
    for fn in files:
        # Lets try to parse the file.
        fp = open(fn)
        m = ErrorMessage(fp)
        sender = m.getaddr('From')
        print '%s\t%-40s\t'%(fn, sender[1]),

        if m.is_warning():
            print 'warning only'
            nwarn = nwarn + 1
            if modify:
                os.unlink(fn)
            continue

        try:
            errors = m.get_errors()
        except Unparseable:
            print '** Not parseable'
            nbad = nbad + 1
            continue
        print len(errors), 'errors'

        # 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)

        nok = nok + 1
        if modify:
            os.unlink(fn)

    print '--------------'
    print nok, 'files parsed,',nwarn,'files warning-only,',
    print nbad,'files unparseable'
    print '--------------'
    for e in errordict.keys():
        print errordict[e], errorfirst[e], '-', errorlast[e], '\t', e

def main():
    modify = 0
    if len(sys.argv) > 1 and sys.argv[1] == '-d':
        modify = 1
        del sys.argv[1]
    if len(sys.argv) > 1:
        for folder in sys.argv[1:]:
            parsedir(folder, modify)
    else:
        parsedir('/ufs/jack/Mail/errorsinbox', modify)

if __name__ == '__main__' or sys.argv[0] == __name__:
    main()