split by year received
This commit is contained in:
parent
8d3ccc573a
commit
79df8cbaa3
1 changed files with 78 additions and 89 deletions
|
@ -64,19 +64,6 @@ 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}\"", '*')
|
||||
|
@ -112,81 +99,68 @@ class BackupImap:
|
|||
os.mkdir(os.path.dirname(mailbox_path))
|
||||
if not os.path.exists(mailbox_path):
|
||||
os.mkdir(mailbox_path)
|
||||
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)
|
||||
)
|
||||
msg_counter = 1
|
||||
for message_uid in all_msgs_uids:
|
||||
current = time.gmtime()
|
||||
for year in range(current.tm_year-10, current.tm_year+1):
|
||||
search_response, search_data = self.imap.search('UTF-8', 'UNDELETED', f'BEFORE={year}')
|
||||
if search_response == 'OK':
|
||||
all_msgs_uids = search_data[0].split()
|
||||
self._log.debug(
|
||||
"Processing message %s (%s/%s)",
|
||||
message_uid.decode(),
|
||||
msg_counter,
|
||||
"Found %s messages",
|
||||
len(all_msgs_uids)
|
||||
)
|
||||
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':
|
||||
self._log.error(
|
||||
"Error marking as unseen the message '%s'. %s",
|
||||
message_uid,
|
||||
unseen_data
|
||||
)
|
||||
sys.exit(6)
|
||||
subject = f'__no_subject__{message_uid}'
|
||||
data = fetch_data[0][1]
|
||||
subjects = self._get_mail_header('Subject', data)
|
||||
if len(subjects) > 0:
|
||||
subject = subjects[-1].replace(
|
||||
os.path.sep,
|
||||
'_'
|
||||
).replace(
|
||||
'\r',
|
||||
'_'
|
||||
).replace(
|
||||
'\n',
|
||||
'_'
|
||||
).replace(
|
||||
':',
|
||||
'_'
|
||||
)
|
||||
message_path = os.path.join(
|
||||
mailbox_path,
|
||||
subject
|
||||
msg_counter = 1
|
||||
for message_uid in all_msgs_uids:
|
||||
self._log.debug(
|
||||
"Processing message %s (%s/%s)",
|
||||
message_uid.decode(),
|
||||
msg_counter,
|
||||
len(all_msgs_uids)
|
||||
)
|
||||
original_subject = subject
|
||||
message_id = self._get_mail_header('Message-ID', data)[-1]
|
||||
if not self._backedup_message(message_id, mailbox):
|
||||
counter = 1
|
||||
while os.path.exists(message_path):
|
||||
subject = f"{original_subject}_{counter}"
|
||||
message_path = os.path.join(
|
||||
mailbox_path,
|
||||
subject
|
||||
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':
|
||||
self._log.error(
|
||||
"Error marking as unseen the message '%s'. %s",
|
||||
message_uid,
|
||||
unseen_data
|
||||
)
|
||||
counter += 1
|
||||
try:
|
||||
with open(message_path, 'wb') as file_pointer:
|
||||
file_pointer.write(data)
|
||||
self.data['backedup_messages'].append(
|
||||
{
|
||||
"message_id": message_id,
|
||||
"mailbox": mailbox
|
||||
}
|
||||
sys.exit(6)
|
||||
subject = f'__no_subject__{message_uid}'
|
||||
data = fetch_data[0][1]
|
||||
subjects = self._get_mail_header('Subject', data)
|
||||
if len(subjects) > 0:
|
||||
subject = subjects[-1].replace(
|
||||
os.path.sep,
|
||||
'_'
|
||||
).replace(
|
||||
'\r',
|
||||
'_'
|
||||
).replace(
|
||||
'\n',
|
||||
'_'
|
||||
).replace(
|
||||
':',
|
||||
'_'
|
||||
)
|
||||
self._save_cached_data(self.data)
|
||||
except OSError as error:
|
||||
if error.errno == 36: # File name too long
|
||||
message_path = os.path.join(
|
||||
mailbox_path,
|
||||
subject
|
||||
)
|
||||
original_subject = subject
|
||||
message_id = self._get_mail_header('Message-ID', data)[-1]
|
||||
if not self._backedup_message(message_id, mailbox):
|
||||
counter = 1
|
||||
while os.path.exists(message_path):
|
||||
subject = f"{original_subject}_{counter}"
|
||||
message_path = os.path.join(
|
||||
mailbox_path,
|
||||
f"message_uid_{message_uid.decode()}"
|
||||
subject
|
||||
)
|
||||
counter += 1
|
||||
try:
|
||||
with open(message_path, 'wb') as file_pointer:
|
||||
file_pointer.write(data)
|
||||
self.data['backedup_messages'].append(
|
||||
|
@ -196,16 +170,31 @@ class BackupImap:
|
|||
}
|
||||
)
|
||||
self._save_cached_data(self.data)
|
||||
else:
|
||||
self._log.error(
|
||||
"Error writing email '%s'. %s",
|
||||
message_path,
|
||||
error
|
||||
)
|
||||
else:
|
||||
self._log.debug(
|
||||
"Skipping already backed up message"
|
||||
)
|
||||
except OSError as error:
|
||||
if error.errno == 36: # File name too long
|
||||
message_path = os.path.join(
|
||||
mailbox_path,
|
||||
f"message_uid_{message_uid.decode()}"
|
||||
)
|
||||
with open(message_path, 'wb') as file_pointer:
|
||||
file_pointer.write(data)
|
||||
self.data['backedup_messages'].append(
|
||||
{
|
||||
"message_id": message_id,
|
||||
"mailbox": mailbox
|
||||
}
|
||||
)
|
||||
self._save_cached_data(self.data)
|
||||
else:
|
||||
self._log.error(
|
||||
"Error writing email '%s'. %s",
|
||||
message_path,
|
||||
error
|
||||
)
|
||||
else:
|
||||
self._log.debug(
|
||||
"Skipping already backed up message"
|
||||
)
|
||||
|
||||
def _backedup_message(self, message_id, mailbox):
|
||||
for message in self.data['backedup_messages']:
|
||||
|
|
Loading…
Reference in a new issue