Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- '''
- Restores the "Replied" flag in e-mails downloaded from Gmail via IMAP.
- Primarily useful when migrating from Gmail to another IMAP e-mail service.
- This may cause loss of your locally stored messages. Use at your own risk.
- See: http://www.emaildiscussions.com/showpost.php?p=521340&postcount=19
- See: http://www.eyrich-net.org/mozilla/X-Mozilla-Status.html?en
- Copyright (c) 2011, Kijin Sung <[email protected]>
- License: BSD License <http://www.opensource.org/licenses/bsd-license.php>
- '''
- # Names of local folders where sent and received messages are stored.
- SENT_FILE = 'Sent'
- RECEIVED_FILE = 'Received'
- # Collect replied-to message IDs from the Sent folder.
- msgids = set()
- f = open(SENT_FILE, 'r')
- for line in f:
- if line.lower().startswith('in-reply-to: '):
- msgid = line[13:].strip()
- msgids.add(msgid)
- f.close()
- # Mark the corresponding Received messages as replied.
- f = open(RECEIVED_FILE, 'r+')
- while True:
- flag_position = f.tell() + 18 # Remember where the flags are.
- line = f.readline()
- if line == '':
- break
- if line.startswith('X-Mozilla-Status: '):
- flags = line[18:].strip()
- msgid_found = False
- while True:
- line2 = f.readline()
- if line == '' or line == '\n': # Don't go past the end of headers.
- break
- if line2.lower().startswith('message-id: '):
- msgid = line2[12:].strip()
- if msgid != '' and msgid in msgids:
- msgid_found = True
- break
- if msgid_found:
- last_line_position = f.tell()
- modified_flags = flags[0:3] + str(int(flags[3]) | 2)
- f.seek(flag_position)
- f.write(modified_flags)
- f.seek(last_line_position)
- print msgid + ' ' + flags + ' => ' + modified_flags
- f.close()
- print 'Done!\n'
Advertisement
Add Comment
Please, Sign In to add comment