document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python
  2. # timestamp.py
  3. # Author: Zachary Cutlip
  4. #         twitter: @zcutlip
  5. # Purpose: A program to fix sorting of mail messages that have been POPed or
  6. #          IMAPed in the wrong order. Compares time stamp sent and timestamp
  7. #          received on an RFC822-formatted email message, and renames the
  8. #          message file using the most recent timestamp that is no more than
  9. #          24 hours after the date sent.  Updates the file\'s atime/mtime with
  10. #          the timestamp, as well. Does not modify the headers or contents of
  11. #          the message.
  12.  
  13. import sys
  14. import os
  15. import email
  16. import email.utils
  17. import re
  18. regex=re.compile("([0-9]+)(\\..*$)")
  19.  
  20. msg_filename=sys.argv[1]
  21. print msg_filename
  22. msg=email.message_from_file(open(msg_filename,"r"))
  23.  
  24. datesent=email.utils.mktime_tz(email.utils.parsedate_tz(msg.get("Date")))
  25.  
  26. #print datesent
  27. date_rx=0
  28. for rx_hdr in msg.get_all("Received"):
  29.     rx_date_str=rx_hdr.split(";")[-1]
  30.     #print rx_date_str
  31.     tmp_rx=email.utils.mktime_tz(email.utils.parsedate_tz(rx_date_str))
  32.     if(tmp_rx - datesent) > (24*60*60):
  33.         continue
  34.  
  35.     date_rx=tmp_rx
  36.     break
  37.  
  38. #print date_rx
  39.  
  40.  
  41. #received header missing?
  42. if(date_rx < 1):
  43.     date_rx = datesent
  44.  
  45. m=regex.match(msg_filename)
  46.  
  47. if len(m.groups()) == 2:
  48.     newfilename=str(int(date_rx))+m.groups()[1]
  49.     print newfilename
  50.     if os.path.isfile(newfilename):
  51.         print "File exists!: "+newfilename
  52.     else:    
  53.         os.rename(msg_filename,newfilename)
  54.         os.utime(newfilename,(date_rx,date_rx))
');