diff options
Diffstat (limited to 'imap-tool')
-rwxr-xr-x | imap-tool | 60 |
1 files changed, 60 insertions, 0 deletions
@@ -17,12 +17,14 @@ class imap_action(Enum): MAILBOX_UNSUBSCRIBE = 6 MAILBOX_STATS = 7 MAILBOX_RENAME = 8 + MAILBOX_ARCHIVE = 9 class imap_tool(): host = "localhost" port = 143 user = "user" password = "password" + archive_batch = 50 ssl = False action = imap_action.MAILBOX_LIST @@ -43,6 +45,7 @@ class imap_tool(): " mailbox-subscribe [mailbox]\n" \ " mailbox-unsubscribe [mailbox]\n" \ " mailbox-rename [mailbox] [mailbox]\n" \ + " mailbox-archive [mailbox] [year]\n" \ " mailbox-stats" def error_decode(self, data): @@ -128,6 +131,59 @@ class imap_tool(): raise Exception() print("Renamed mailbox " + self.mailbox[0] + " to " + self.mailbox[1]) + elif self.action == imap_action.MAILBOX_ARCHIVE: + mailbox_archive = "archives/" + self.mailbox + "/" + str(self.archive_year) + year_start = self.archive_year + year_stop = self.archive_year + 1 + + ret, data = imap.create(mailbox_archive) + if ret != "OK": + if not self.error_decode(data).startswith("[ALREADYEXISTS]"): + print("Error creating mailbox " + mailbox_archive + ": " + self.error_decode(data)) + raise Exception() + else: + print("Created mailbox " + mailbox_archive) + + ret, date = imap.select(self.mailbox) + if ret != "OK": + print("Error selecting mailbox " + self.mailbox + ": " + self.error_decode(data)) + raise Exception() + + ret, data = imap.uid("SEARCH", None, "(SINCE \"01-Jan-" + str(year_start) + "\" BEFORE \"01-Jan-" + str(year_stop) + "\")") + if ret != "OK": + print("Error searching mailbox " + self.mailbox + ": " + self.error_decode(data)) + raise Exception() + + uids_search = data[0].decode("ascii").split() + uids_search_count = len(uids_search) + + print("Retrieved " + str(uids_search_count) + " messages from mailbox " + self.mailbox + " for year " + str(self.archive_year)) + + index = 0 + uids = [] + + while index < uids_search_count: + uid = uids_search[index] + index += 1 + + uids.append(uid) + + if len(uids) == self.archive_batch or index == uids_search_count: + ret, data = imap.uid("COPY", ",".join(uids), mailbox_archive) + if ret != "OK": + print("Error copying " + str(len(uids)) + " messages to mailbox " + mailbox_archive) + raise Exception() + + ret, data = imap.uid("STORE", ",".join(uids), "+FLAGS", "\\Deleted") + if ret != "OK": + print("Error deleting " + str(len(uids)) + " messages from mailbox " + self.mailbox) + raise Exception() + + print("Archived " + str(len(uids)) + " messages to mailbox " + mailbox_archive) + + uids = [] + + imap.expunge() except Exception: imap.logout() return 1 @@ -178,6 +234,10 @@ class imap_tool(): elif action_option == "mailbox-rename": self.action = imap_action.MAILBOX_RENAME self.mailbox = [ arguments[1], arguments[2] ] + elif action_option == "mailbox-archive": + self.action = imap_action.MAILBOX_ARCHIVE + self.mailbox = arguments[1] + self.archive_year = int(arguments[2]) else: print("Invalid action specified") return 1 |