From 8d3ccc573a9d29b7528ff9814c698c269d49db28 Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Fri, 28 Mar 2025 10:11:42 +0200 Subject: [PATCH] Do search one year at a time --- backup_imap/backup_imap.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/backup_imap/backup_imap.py b/backup_imap/backup_imap.py index a91b685..d717a09 100644 --- a/backup_imap/backup_imap.py +++ b/backup_imap/backup_imap.py @@ -64,6 +64,19 @@ class BackupImap: ) self._process_mailbox(self.config['mailbox']) + def _better_imap_search(self, charset='UTF-8', criterion='UNDELETED'): + current = time.gmtime() + result = { + "result": b"NO", + "data": b"" + } + for year in range(current.tm_year-10, current.tm_year+1): + search_response, search_data = self.imap.search(charset, criterion, f'BEFORE={year}') + if search_response == 'OK': + result['result'] = 'OK' + result[search_data] += result[search_data] + return result + def _process_mailbox(self, mailbox): self._log.debug("Searching for all mailboxes in mailbox '%s'...", mailbox) list_response, list_data = self.imap.list(f"\"{mailbox}\"", '*') @@ -99,22 +112,9 @@ class BackupImap: os.mkdir(os.path.dirname(mailbox_path)) if not os.path.exists(mailbox_path): os.mkdir(mailbox_path) - search_succeeded = False - while not search_succeeded: - try: - search_response, search_data = self.imap.search('UTF-8', 'UNDELETED') - search_succeeded = True - except imaplib.IMAP4.error as error: - error_str = f"{error.__str__}" - if 'got more than' in error_str: - imaplib._MAXLINE += 10000 - self._log.debug( - "Error searching mailbox, increasing maxline to %s. %s", - imaplib._MAXLINE, - error - ) - if search_response == 'OK': - all_msgs_uids = search_data[0].split() + search_result = self._better_imap_search('UTF-8', 'UNDELETED') + if search_result['result'] == 'OK': + all_msgs_uids = search_result['data'][0].split() self._log.debug( "Found %s messages", len(all_msgs_uids) @@ -129,6 +129,7 @@ class BackupImap: ) msg_counter += 1 fetch_response, fetch_data = self.imap.fetch(message_uid, "(RFC822)") + self._log.debug(fetch_data) if fetch_response == 'OK': store_result, unseen_data = self.imap.store(message_uid, '-FLAGS', '\\Seen') if store_result != 'OK':