Show also all matches and only expunge if there was any match

This commit is contained in:
Antonio J. Delgado 2024-09-06 12:20:53 +03:00
parent de444649a7
commit ebbe85dd3d

View file

@ -54,6 +54,7 @@ class ImapFilter:
)
def _process_filters(self):
matches = 0
for mailbox in self.config['mailboxes']:
self._log.debug(
"Processing mailbox '%s'...",
@ -73,7 +74,7 @@ class ImapFilter:
mailbox['mailbox']
)
msg_count = 0
self.matches = 0
mailbox_matches = 0
for message_id in all_msgs_uids:
msg_count += 1
self._log.debug(
@ -99,22 +100,28 @@ class ImapFilter:
"Error fetching message. %s",
error
)
if self.matches > 0:
if mailbox_matches > 0:
self._log.info(
"A total of %s matches for this mailbox",
self.matches
"A total of %s matches for this mailbox,",
mailbox_matches
)
else:
self._log.info(
"No matches for this mailbox"
)
try:
self.imap.expunge()
except imaplib.IMAP4.abort as error:
self._log.error(
"Error expunging connection. %s",
error
)
matches += mailbox_matches
self._log.info(
"A total of %s matches for all mailboxes.",
matches
)
if matches > 0:
try:
self.imap.expunge()
except imaplib.IMAP4.abort as error:
self._log.error(
"Error expunging connection. %s",
error
)
return True
def _process_message(self, message_id, data, mfilter):
@ -151,7 +158,7 @@ class ImapFilter:
field_data,
mfilter['name']
)
self.matches += 1
mailbox_matches += 1
if self.config['dummy']:
self._log.info('Doing nothing (dummy run)')
else:
@ -191,7 +198,7 @@ class ImapFilter:
"Creating mailbox '%s'...",
mailbox
)
typ, data = self.imap.create(mailbox.encode('utf-8', 'replace'))
typ, data = self.imap.create(mailbox)
if typ != 'OK':
self._log.error(
'Error creating mailbox %s, server replied: %s',