+++ /dev/null
-"""mailerdaemon - classes to parse mailer-daemon messages"""\r
-\r
-import rfc822\r
-import calendar\r
-import re\r
-import os\r
-import sys\r
-\r
-Unparseable = 'mailerdaemon.Unparseable'\r
-\r
-class ErrorMessage(rfc822.Message):\r
- def __init__(self, fp):\r
- rfc822.Message.__init__(self, fp)\r
- self.sub = ''\r
-\r
- def is_warning(self):\r
- sub = self.getheader('Subject')\r
- if not sub:\r
- return 0\r
- sub = sub.lower()\r
- if sub.startswith('waiting mail'): return 1\r
- if 'warning' in sub: return 1\r
- self.sub = sub\r
- return 0\r
-\r
- def get_errors(self):\r
- for p in EMPARSERS:\r
- self.rewindbody()\r
- try:\r
- return p(self.fp, self.sub)\r
- except Unparseable:\r
- pass\r
- raise Unparseable\r
-\r
-# List of re's or tuples of re's.\r
-# If a re, it should contain at least a group (?P<email>...) which\r
-# should refer to the email address. The re can also contain a group\r
-# (?P<reason>...) which should refer to the reason (error message).\r
-# If no reason is present, the emparse_list_reason list is used to\r
-# find a reason.\r
-# If a tuple, the tuple should contain 2 re's. The first re finds a\r
-# location, the second re is repeated one or more times to find\r
-# multiple email addresses. The second re is matched (not searched)\r
-# where the previous match ended.\r
-# The re's are compiled using the re module.\r
-emparse_list_list = [\r
- 'error: (?P<reason>unresolvable): (?P<email>.+)',\r
- ('----- The following addresses had permanent fatal errors -----\n',\r
- '(?P<email>[^ \n].*)\n( .*\n)?'),\r
- 'remote execution.*\n.*rmail (?P<email>.+)',\r
- ('The following recipients did not receive your message:\n\n',\r
- ' +(?P<email>.*)\n(The following recipients did not receive your message:\n\n)?'),\r
- '------- Failure Reasons --------\n\n(?P<reason>.*)\n(?P<email>.*)',\r
- '^<(?P<email>.*)>:\n(?P<reason>.*)',\r
- '^(?P<reason>User mailbox exceeds allowed size): (?P<email>.+)',\r
- '^5\\d{2} <(?P<email>[^\n>]+)>\\.\\.\\. (?P<reason>.+)',\r
- '^Original-Recipient: rfc822;(?P<email>.*)',\r
- '^did not reach the following recipient\\(s\\):\n\n(?P<email>.*) on .*\n +(?P<reason>.*)',\r
- '^ <(?P<email>[^\n>]+)> \\.\\.\\. (?P<reason>.*)',\r
- '^Report on your message to: (?P<email>.*)\nReason: (?P<reason>.*)',\r
- '^Your message was not delivered to +(?P<email>.*)\n +for the following reason:\n +(?P<reason>.*)',\r
- '^ was not +(?P<email>[^ \n].*?) *\n.*\n.*\n.*\n because:.*\n +(?P<reason>[^ \n].*?) *\n',\r
- ]\r
-# compile the re's in the list and store them in-place.\r
-for i in range(len(emparse_list_list)):\r
- x = emparse_list_list[i]\r
- if type(x) is type(''):\r
- x = re.compile(x, re.MULTILINE)\r
- else:\r
- xl = []\r
- for x in x:\r
- xl.append(re.compile(x, re.MULTILINE))\r
- x = tuple(xl)\r
- del xl\r
- emparse_list_list[i] = x\r
- del x\r
-del i\r
-\r
-# list of re's used to find reasons (error messages).\r
-# if a string, "<>" is replaced by a copy of the email address.\r
-# The expressions are searched for in order. After the first match,\r
-# no more expressions are searched for. So, order is important.\r
-emparse_list_reason = [\r
- r'^5\d{2} <>\.\.\. (?P<reason>.*)',\r
- '<>\.\.\. (?P<reason>.*)',\r
- re.compile(r'^<<< 5\d{2} (?P<reason>.*)', re.MULTILINE),\r
- re.compile('===== stderr was =====\nrmail: (?P<reason>.*)'),\r
- re.compile('^Diagnostic-Code: (?P<reason>.*)', re.MULTILINE),\r
- ]\r
-emparse_list_from = re.compile('^From:', re.IGNORECASE|re.MULTILINE)\r
-def emparse_list(fp, sub):\r
- data = fp.read()\r
- res = emparse_list_from.search(data)\r
- if res is None:\r
- from_index = len(data)\r
- else:\r
- from_index = res.start(0)\r
- errors = []\r
- emails = []\r
- reason = None\r
- for regexp in emparse_list_list:\r
- if type(regexp) is type(()):\r
- res = regexp[0].search(data, 0, from_index)\r
- if res is not None:\r
- try:\r
- reason = res.group('reason')\r
- except IndexError:\r
- pass\r
- while 1:\r
- res = regexp[1].match(data, res.end(0), from_index)\r
- if res is None:\r
- break\r
- emails.append(res.group('email'))\r
- break\r
- else:\r
- res = regexp.search(data, 0, from_index)\r
- if res is not None:\r
- emails.append(res.group('email'))\r
- try:\r
- reason = res.group('reason')\r
- except IndexError:\r
- pass\r
- break\r
- if not emails:\r
- raise Unparseable\r
- if not reason:\r
- reason = sub\r
- if reason[:15] == 'returned mail: ':\r
- reason = reason[15:]\r
- for regexp in emparse_list_reason:\r
- if type(regexp) is type(''):\r
- for i in range(len(emails)-1,-1,-1):\r
- email = emails[i]\r
- exp = re.compile(re.escape(email).join(regexp.split('<>')), re.MULTILINE)\r
- res = exp.search(data)\r
- if res is not None:\r
- errors.append(' '.join((email.strip()+': '+res.group('reason')).split()))\r
- del emails[i]\r
- continue\r
- res = regexp.search(data)\r
- if res is not None:\r
- reason = res.group('reason')\r
- break\r
- for email in emails:\r
- errors.append(' '.join((email.strip()+': '+reason).split()))\r
- return errors\r
-\r
-EMPARSERS = [emparse_list, ]\r
-\r
-def sort_numeric(a, b):\r
- a = int(a)\r
- b = int(b)\r
- if a < b: return -1\r
- elif a > b: return 1\r
- else: return 0\r
-\r
-def parsedir(dir, modify):\r
- os.chdir(dir)\r
- pat = re.compile('^[0-9]*$')\r
- errordict = {}\r
- errorfirst = {}\r
- errorlast = {}\r
- nok = nwarn = nbad = 0\r
-\r
- # find all numeric file names and sort them\r
- files = filter(lambda fn, pat=pat: pat.match(fn) is not None, os.listdir('.'))\r
- files.sort(sort_numeric)\r
-\r
- for fn in files:\r
- # Lets try to parse the file.\r
- fp = open(fn)\r
- m = ErrorMessage(fp)\r
- sender = m.getaddr('From')\r
- print '%s\t%-40s\t'%(fn, sender[1]),\r
-\r
- if m.is_warning():\r
- fp.close()\r
- print 'warning only'\r
- nwarn = nwarn + 1\r
- if modify:\r
- os.rename(fn, ','+fn)\r
-## os.unlink(fn)\r
- continue\r
-\r
- try:\r
- errors = m.get_errors()\r
- except Unparseable:\r
- print '** Not parseable'\r
- nbad = nbad + 1\r
- fp.close()\r
- continue\r
- print len(errors), 'errors'\r
-\r
- # Remember them\r
- for e in errors:\r
- try:\r
- mm, dd = m.getdate('date')[1:1+2]\r
- date = '%s %02d' % (calendar.month_abbr[mm], dd)\r
- except:\r
- date = '??????'\r
- if not errordict.has_key(e):\r
- errordict[e] = 1\r
- errorfirst[e] = '%s (%s)' % (fn, date)\r
- else:\r
- errordict[e] = errordict[e] + 1\r
- errorlast[e] = '%s (%s)' % (fn, date)\r
-\r
- fp.close()\r
- nok = nok + 1\r
- if modify:\r
- os.rename(fn, ','+fn)\r
-## os.unlink(fn)\r
-\r
- print '--------------'\r
- print nok, 'files parsed,',nwarn,'files warning-only,',\r
- print nbad,'files unparseable'\r
- print '--------------'\r
- list = []\r
- for e in errordict.keys():\r
- list.append((errordict[e], errorfirst[e], errorlast[e], e))\r
- list.sort()\r
- for num, first, last, e in list:\r
- print '%d %s - %s\t%s' % (num, first, last, e)\r
-\r
-def main():\r
- modify = 0\r
- if len(sys.argv) > 1 and sys.argv[1] == '-d':\r
- modify = 1\r
- del sys.argv[1]\r
- if len(sys.argv) > 1:\r
- for folder in sys.argv[1:]:\r
- parsedir(folder, modify)\r
- else:\r
- parsedir('/ufs/jack/Mail/errorsinbox', modify)\r
-\r
-if __name__ == '__main__' or sys.argv[0] == __name__:\r
- main()\r