#!/usr/bin/python import fileinput import os import re import string import sys import time do_it_the_gnu_way = 1 if do_it_the_gnu_way: # Include the time zone received_re = re.compile( '; ((..., )?.?. ... (..)?.. ..:..(:..)?(.*)?)' ) else: # Require a more strict format than is good, and leave off the TZ received_re = re.compile( '; (..., .?. ... .... ..:..:..)' ) for mfile in ( sys.argv[1:] ): print 'Fixing mtime on %s' % mfile received_line = '' for line in fileinput.input( mfile ): if( len(line) == 0 and len(received_line) > 0 ): # An empty line is the beginning of the message # If we get here, we didn't set the mtime print ' no "Received:" headers were found, mtime not changed' fileinput.nextfile() elif( len(received_line) > 0 and string.find( string.whitespace, line[0] ) == -1 ): # We found the beginning of a Received: header on a previous # line, and this line begins with non-whitespace characters, # indicating that we have the complete Received: header. # do the Magic! received_re_m = received_re.search( received_line ) if( received_re_m == None ): print ' bad "Received:" line in %s' % fileinput.filename() # Perhaps we can try the next Received: header?? received_line = '' else: if do_it_the_gnu_way: print ' setting time to %s' % received_re_m.group(1) os.system( 'touch -d "%s" "%s"' % ( received_re_m.group(1), fileinput.filename() ) ) else: # Do it the wrong, but portable, way print ' setting time. to %s' % received_re_m.group(1) newmtime = time.mktime( time.strptime( received_re_m.group(1), '%a, %d %b %Y %H:%M:%S ' ) ) newtime_t = ( newmtime, newmtime ) os.utime( fileinput.filename(), newtime_t ) fileinput.nextfile() elif( line[0:9] == 'Received:' ): received_line = line elif( len(received_line) > 0 and string.find( string.whitespace, line[0] ) != -1 ): # The previous line was a received header, and this is a # continuation received_line = received_line + line