summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2020-07-02 18:07:52 +0200
committerMatěj Cepl <mcepl@cepl.eu>2020-07-02 18:12:26 +0200
commit8658b46267349eecf52b756228e02656850fcba1 (patch)
treefd50f507a303c75ce4a844e20c3e7a8e00842086
parent08ec81019506399690caab1642563f5ea842ba02 (diff)
downloadimap-folder-training-8658b46267349eecf52b756228e02656850fcba1.tar.gz
Collect capabilities and don't EXPUNGE on servers, which don't support it.
-rwxr-xr-xcheck_bogofilter.py20
1 files 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<uid>\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()