Guest User

Gmail Thunderbird Set Replied Flag

a guest
Apr 24th, 2011
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. '''
  4. Restores the "Replied" flag in e-mails downloaded from Gmail via IMAP.
  5. Primarily useful when migrating from Gmail to another IMAP e-mail service.
  6. This may cause loss of your locally stored messages. Use at your own risk.
  7.  
  8. See: http://www.emaildiscussions.com/showpost.php?p=521340&postcount=19
  9. See: http://www.eyrich-net.org/mozilla/X-Mozilla-Status.html?en
  10.  
  11. Copyright (c) 2011, Kijin Sung <[email protected]>
  12. License: BSD License <http://www.opensource.org/licenses/bsd-license.php>
  13. '''
  14.  
  15. # Names of local folders where sent and received messages are stored.
  16.  
  17. SENT_FILE = 'Sent'
  18. RECEIVED_FILE = 'Received'
  19.  
  20. # Collect replied-to message IDs from the Sent folder.
  21.  
  22. msgids = set()
  23. f = open(SENT_FILE, 'r')
  24. for line in f:
  25.     if line.lower().startswith('in-reply-to: '):
  26.         msgid = line[13:].strip()
  27.         msgids.add(msgid)
  28. f.close()
  29.  
  30. # Mark the corresponding Received messages as replied.
  31.  
  32. f = open(RECEIVED_FILE, 'r+')
  33. while True:
  34.     flag_position = f.tell() + 18  # Remember where the flags are.
  35.     line = f.readline()
  36.     if line == '':
  37.         break
  38.     if line.startswith('X-Mozilla-Status: '):
  39.         flags = line[18:].strip()
  40.         msgid_found = False
  41.         while True:
  42.             line2 = f.readline()
  43.             if line == '' or line == '\n':  # Don't go past the end of headers.
  44.                 break
  45.             if line2.lower().startswith('message-id: '):
  46.                 msgid = line2[12:].strip()
  47.                 if msgid != '' and msgid in msgids:
  48.                     msgid_found = True
  49.                 break
  50.         if msgid_found:
  51.             last_line_position = f.tell()
  52.             modified_flags = flags[0:3] + str(int(flags[3]) | 2)
  53.             f.seek(flag_position)
  54.             f.write(modified_flags)
  55.             f.seek(last_line_position)
  56.             print msgid + ' ' + flags + ' => ' + modified_flags
  57. f.close()
  58. print 'Done!\n'
Advertisement
Add Comment
Please, Sign In to add comment