#!/usr/bin/env python """ This example shows how to restore a backup created by backup_mailbox.py" """ from ProcImap.ImapMailbox import ImapMailbox from ProcImap.ImapMessage import ImapMessage from ProcImap.Utils.MailboxFactory import MailboxFactory from mailbox import mbox import sys # usage: restore_mailbox.py backupmbox imapmailbox mailboxes = MailboxFactory('/home/goerz/.procimap/mailboxes.cfg') server = mailboxes.get_server('Gmail') mailbox = ImapMailbox((server, sys.argv[2])) backupsource = mbox(sys.argv[1], factory=ImapMessage) for message in backupsource: if message.has_key("X-ProcImap-Imapflags"): message.flags_from_string(message["X-ProcImap-Imapflags"]) del message["X-ProcImap-Imapflags"] if message.has_key("X-ProcImap-ImapInternalDate"): message. internaldate_from_string(message["X-ProcImap-ImapInternalDate"]) del message["X-ProcImap-ImapInternalDate"] mailbox.add(message) mailbox.close() backupsource.close() sys.exit(0)
for uid in encrypted: print(" Decrypting UID %s" % uid) message = mailbox[uid] labels = [mailbox_name] # all the mailboxes the mail appears (for gmail) if options.backup is not None: print(" Backing up the original (encrypted) message") backupbox.add(message) print(" Deleting the original (encrypted) message") if is_gmail_box(mailbox): labels = cache.get_labels("%s.%s" % (mailbox_name, uid)) delete(mailbox, uid) else: mailbox.discard(uid) print(" Piping message through decryption program") try: message = pipe_message(message, decryptprogram) except: # the decryption program sometimes stalls. This try-except block # allows to use Ctrl+C to get out of processing this specific mail pass print(" Done") for labelbox_name in labels: # this can be done more efficiently once we are able to find the UID # of a message that was just uploaded to the mailbox labelbox = ImapMailbox((mailbox_server.clone(), labelbox_name)) print(" Putting decrypted text into mailbox %s" % labelbox_name) labelbox.add(message) labelbox.close() mailbox.close() sys.exit(0)
#!/usr/bin/env python """ This example shows how to create a backup of an IMAP mailbox into an mbox folders. The IMAP attributes are stored in each message in special header fields" """ from ProcImap.ImapMailbox import ImapMailbox from ProcImap.Utils.MailboxFactory import MailboxFactory from mailbox import mbox import sys # usage: backup_mailbox.py imapmailbox backupmbox mailboxes = MailboxFactory('/home/goerz/.procimap/mailboxes.cfg') server = mailboxes.get_server('Gmail') mailbox = ImapMailbox((server, sys.argv[1])) backuptarget = mbox(sys.argv[2]) backuptarget.lock() for message in mailbox: message.add_header("X-ProcImap-Imapflags", message.flagstring()) message.add_header("X-ProcImap-ImapInternalDate", message.internaldatestring()) backuptarget.add(message) mailbox.close() backuptarget.close() sys.exit(0)
fromboxname = sys.argv[1] frombox = mailbox.mbox(fromboxname) frombox.lock() tobox.lock() i = 1 print "Processing mbox file %s with %s messages" % (fromboxname, len(frombox)) for message in frombox: print "%s" % i if message['Message-Id'] is None: print " WARNING: message has no message-id (mesage ID will be added)" message.add_header("Message-Id", make_msgid('katamon.mbox2imap') ) if True: try: tobox.add(message) except ImapNotOkError: print " ERROR: Transaction failed for message %s" % i toserver.reconnect() tobox = ImapMailbox((toserver, toboxname)) time.sleep(5) time.sleep(1) number_in_tobox = len(tobox.get_all_uids()) #time.sleep(1) print " Added Message, %s in mailbox" % number_in_tobox i = i + 1 frombox.close() tobox.close() sys.exit(0)
tobox = ImapMailbox((toserver, toboxname)) fromboxname = sys.argv[1] frombox = mailbox.mbox(fromboxname) frombox.lock() tobox.lock() i = 1 print "Processing mbox file %s with %s messages" % (fromboxname, len(frombox)) for message in frombox: print "%s" % i if message['Message-Id'] is None: print " WARNING: message has no message-id (mesage ID will be added)" message.add_header("Message-Id", make_msgid('katamon.mbox2imap')) if True: try: tobox.add(message) except ImapNotOkError: print " ERROR: Transaction failed for message %s" % i toserver.reconnect() tobox = ImapMailbox((toserver, toboxname)) time.sleep(5) time.sleep(1) number_in_tobox = len(tobox.get_all_uids()) #time.sleep(1) print " Added Message, %s in mailbox" % number_in_tobox i = i + 1 frombox.close() tobox.close() sys.exit(0)
# delete missing uids label = '' mailbox = None uids = [] for labeluid in labeluids: record_label = re.split("\.\d+$", labeluid)[0] if (label != record_label): label = record_label if mailbox is not None: mailbox.close() mailbox = ImapMailbox((server, label)) uids = mailbox.get_all_uids() uid = int(re.split("^.*\.", labeluid)[1]) if uid not in uids: del record[labeluid] if options.verbose: print "Deleted %s from record" % labeluid if mailbox is not None: mailbox.close() labeluids = record.keys() labeluids.sort() # write out result record_file = open(options.record, 'w') for labeluid in labeluids: print >>record_file, "%s : %s" % (labeluid, record[labeluid]) # delete those files that are not in filenames filenames = set(record.values()) for filename in glob('*.eml'): if filename not in filenames: os.remove(filename) if options.verbose: print "Removed file %s" % filename