#! /usr/bin/env python """ maildir-dates [maildir1 [maildir2 [...]]] sets file timestams according to "Date" header inside maildir messages. Handy for IMAP servers which use file times for sorting. --drt@un.bewaff.net -- http://c0re.jp/ """ import rfc822 import os, sys def redate(dirname): # check for new mail newdir = os.path.join(dirname, 'new') boxes = [os.path.join(newdir, f) for f in os.listdir(newdir) if f[0] != '.'] # Now check for current mail in this maildir curdir = os.path.join(dirname, 'cur') boxes += [os.path.join(curdir, f) for f in os.listdir(curdir) if f[0] != '.'] for fn in boxes: try: fp = open(fn) msg = rfc822.Message(fp) t= rfc822.mktime_tz(rfc822.parsedate_tz(msg['Date'])) os.utime(fn, (t, t)) except: print "Problem with %r" % fn raise if len(sys.argv) == 1: print __doc__ else: for x in sys.argv[1:]: redate(x)