From 8658b46267349eecf52b756228e02656850fcba1 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Thu, 2 Jul 2020 18:07:52 +0200 Subject: Collect capabilities and don't EXPUNGE on servers, which don't support it. --- check_bogofilter.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/check_bogofilter.py b/check_bogofilter.py index ed95651..c36cf44 100755 --- a/check_bogofilter.py +++ b/check_bogofilter.py @@ -4,6 +4,7 @@ import logging import os.path import re import subprocess +from collections import namedtuple from ConfigParser import ConfigParser logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', @@ -14,6 +15,10 @@ log = logging.getLogger('check_bogofilter') pattern_uid = re.compile(r'\d+ \(UID (?P\d+)\)') +# Store capabilities present in the current server +# Add additional ones as needed +AvailableCapabilities = namedtuple('Capas', ['MOVE', 'UIDPLUS']) + def parse_uid(data): match = pattern_uid.match(data) @@ -26,8 +31,8 @@ def move_messages(ids, target): ids_str = ','.join(ids) client.uid('COPY', ids_str, target) client.uid('STORE', ids_str, '+FLAGS', r'(\Deleted)') - # requires UIDPLUS extension in CAPABILITIES - client.uid('EXPUNGE', ids_str) + if client._features_available.UIDPLUS: + client.uid('EXPUNGE', ids_str) def process_folder(proc_fld_name, target_fld, unsure_fld, spam_fld=None, @@ -92,7 +97,7 @@ def process_folder(proc_fld_name, target_fld, unsure_fld, spam_fld=None, client.uid('STORE', ','.join(ham_msgs), '-FLAGS', r'(\Seen)') if spam_msgs: if spam_fld is not None: - move_messages(spam_msgs, spam_fld) + move_messages(spam_msgs, spam_fld) else: client.uid('STORE', ','.join(spam_msgs), '+FLAGS.SILENT', r'(\Deleted \Seen)') @@ -102,6 +107,7 @@ def process_folder(proc_fld_name, target_fld, unsure_fld, spam_fld=None, return proc_msg_count + processedCounter = 0 config = ConfigParser() config.read(os.path.expanduser("~/.bogofilter-imap-train-rc")) @@ -110,8 +116,16 @@ login = config.get("imap-training", "login") password = config.get("imap-training", "password") server = config.get("imap-training", "server") client = imaplib.IMAP4_SSL(server) +client._features_available = AvailableCapabilities(False, False) client.login(login, password) +ok, dat = client.capability() +if ok != 'OK': + raise client.error(dat[-1]) +capas = dat[0].decode() +client._features_available = AvailableCapabilities._make( + ['MOVE' in capas, 'UIDPLUS' in capas]) + processedCounter += process_folder('INBOX', '_suspects', '_unsure', '_spam') client.logout() -- cgit