[mhonarc] parse received headers, add comments, handle main



commit 91fac517443e2cfbda85309d6f7d2b2b381940c2
Author: Olav Vitters <olav vitters nl>
Date:   Tue Feb 12 12:02:00 2013 +0100

    parse received headers, add comments, handle main

 archive.py |   41 +++++++++++++++++++++++++++++++++++++----
 1 files changed, 37 insertions(+), 4 deletions(-)
---
diff --git a/archive.py b/archive.py
index d230c8d..0acde8f 100755
--- a/archive.py
+++ b/archive.py
@@ -8,6 +8,8 @@ import argparse
 import time
 import dateutil.parser
 import re
+import datetime
+import email.utils
 
 
 class Archiver:
@@ -83,12 +85,24 @@ class Archiver:
 
 
     def handle_message(self, msg):
-        
         received_texts = msg.get_all('Received')
 
         received_time = None
 
-        # XXX - parse received headers
+        # Determine received time
+        for text in received_texts:
+            if ';' not in text: continue
+            time = email.utils.parsedate_tz(text.rpartition(';')[2])
+            if time is None:
+                continue
+            try:
+                time = email.utils.mktime_tz(time)
+            except ValueError:
+                time = None
+
+            if time is not None:
+                received_time = datetime.datetime.fromtimestamp(time)
+                break
 
         if received_time is not None:
             if self.start_time and received_time < self.start_time:
@@ -97,8 +111,11 @@ class Archiver:
             if self.end_time and received_time > self.end_time:
                 return False
 
+            # Archive emails per month
             path = received_time.strftime("%Y-%B")
 
+            # mhonarc can archive multiple emails at once, so only run mhonarc
+            # once output path changes
             if self.olddir is not None and self.olddir != path:
                 # XXX wtf
                 self.output(False)
@@ -107,6 +124,11 @@ class Archiver:
             self.last_time = received_time
             self.last_parseable_time = received_time_text
 
+    def process_fd(self, fd):
+        # XXX - implement
+        pass
+
+
     @classmethod
     def make_index(listname, private=False):
         path = os.path.join(self.PRIVATE_ARCHIVE_DIR, listname)
@@ -126,7 +148,7 @@ class Archiver:
             else:
                 dirs[a_dir] = a_dir
 
-            with open(os.path.join(path, 'index.html', 'w') as fp:
+            with open(os.path.join(path, 'index.html', 'w')) as fp:
                 for a_dir in sorted(dirs, key=dirs.get):
                     # XXX - write header 
                     # XXX - determine mbox type
@@ -147,7 +169,6 @@ def mkdate(datestr):
     #return time.strptime(datestr, '%Y-%m-%d')
     return dateutil.parser.parse(datestr)
 
-
 def main():
     description = """Archive an email or an mbox per month using mhonarc"""
     epilog="""Report bugs to https://bugzilla.gnome.org/enter_bug.cgi?product=sysadmin""";
@@ -167,9 +188,21 @@ def main():
 
 
     options = parser.parse_args()
+
+    old_mask = os.umask(0022)
+
     import pprint
     pprint.pprint(options)
+    sys.exit(2)
+
+    if options.makeindex:
+        Archiver.make_index(options.listname, options.private)
+        sys.exit(0)
 
+    archiver = Archiver(options.listname, private=options.private, debug=options.debug,
+                        start_time=options.start_time, end_time=options.end_time
+    )
+    archiver.process_fd(options.infile)
 
 if __name__ == "__main__":
     main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]