|
@@ -0,0 +1,40 @@
|
|
|
+#!/usr/bin/env python
|
|
|
+import mailbox
|
|
|
+from datetime import datetime
|
|
|
+import json
|
|
|
+import re
|
|
|
+
|
|
|
+# Helper to get plain-text payload from a given email
|
|
|
+def get_message_body(msg):
|
|
|
+ for part in msg.walk():
|
|
|
+ if part.get_content_type() == 'text/plain':
|
|
|
+ return part.get_payload(decode=True)
|
|
|
+ return None
|
|
|
+
|
|
|
+# Open a maildir inbox, and run provided parsers
|
|
|
+def parse_maildir_inbox(inbox,parsers):
|
|
|
+
|
|
|
+ mail = mailbox.Maildir(inbox,None)
|
|
|
+
|
|
|
+ messages = {}
|
|
|
+
|
|
|
+ for msgid in mail.keys():
|
|
|
+
|
|
|
+ subject = mail[msgid]['subject']
|
|
|
+ to_addr = mail[msgid]['to']
|
|
|
+ from_addr = mail[msgid]['from']
|
|
|
+ cur_time = datetime.now().strftime("%b %d %H:%M:%S")
|
|
|
+
|
|
|
+ for regex,action in parsers:
|
|
|
+
|
|
|
+ msg_details = {'subject': subject, 'to': to_addr, 'from': from_addr}
|
|
|
+
|
|
|
+ m = re.match(regex,subject)
|
|
|
+ if m:
|
|
|
+ parse_success = action(mail[msgid])
|
|
|
+
|
|
|
+ if parse_success:
|
|
|
+ print "%s: Parse successful: %s: %s" % (cur_time, action.__name__, json.dumps(msg_details))
|
|
|
+ mail.remove(msgid)
|
|
|
+ else:
|
|
|
+ print "%s: No parser match: %s" % (cur_time, json.dumps(msg_details))
|