From 963dd4ff120447515bac5f366ba763ec8d8e3805 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Fri, 12 Feb 2021 13:40:31 +0000
Subject: [PATCH] Make the list of mailing list domains configurable

---
 feedbackloop/main.py  |  3 ++-
 feedbackloop/parse.py | 31 +++++++++++++++++++------------
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/feedbackloop/main.py b/feedbackloop/main.py
index 522b8ce..1e1dca2 100644
--- a/feedbackloop/main.py
+++ b/feedbackloop/main.py
@@ -32,7 +32,8 @@ def ingest(unseen, limit):
                 app.config['IMAP_SERVER'],
                 app.config['IMAP_USERNAME'],
                 app.config['IMAP_PASSWORD'],
-                app.config.get('IMAP_REMOVE_MESSAGES', True)) as scanner:
+                app.config.get('IMAP_REMOVE_MESSAGES', True),
+                app.config.get('LIST_DOMAINS')) as scanner:
             for entry in scanner.scan(unseen, limit):
                 db.session.add(entry)
         db.session.commit()
diff --git a/feedbackloop/parse.py b/feedbackloop/parse.py
index d28d358..3ebe6cb 100644
--- a/feedbackloop/parse.py
+++ b/feedbackloop/parse.py
@@ -32,14 +32,6 @@ def _normalize_addr(addr):
     return addr, False
 
 
-def _list_name_from_id(list_id):
-    """Parse a (mailman) List-Id header and return the list address."""
-    m = re.search(r'<(.+)\.(autistici\.org|inventati\.org|boum\.org|naga\.it)>', list_id)
-    if m:
-        return f'{m[1]}@{m[2]}'
-    return f'list-id:{list_id}'
-
-
 def _find_authenticated_sender(hdr_list):
     for name, value in hdr_list:
         if name != 'Received':
@@ -55,7 +47,6 @@ def _hdr(hdr_list, name):
             return v
 
 
-
 class MailScanner():
     """Read messages from a remote IMAP folder and remove them.
 
@@ -63,13 +54,20 @@ class MailScanner():
     if the inner block executes successfully.
     """
 
-    def __init__(self, server, username, password, remove=True):
+    def __init__(self, server, username, password, remove=True,
+                 list_domains=None):
         self._server = server
         self._username = username
         self._password = password
         self._remove = remove
         self._to_delete = []
         self._conn = None
+        if list_domains:
+            self._list_id_rx = re.compile(
+                r'<(.+)\.(%s)>' % (
+                    '|'.join(map(re.escape, list_domains))))
+        else:
+            self._list_id_rx = None
 
     def __enter__(self):
         self._conn = imaplib.IMAP4_SSL(self._server)
@@ -95,6 +93,13 @@ class MailScanner():
         except:
             pass
 
+    def _list_name_from_id(self, list_id):
+        if self._list_id_rx:
+            m = self._list_id_rx.search(list_id)
+            if m:
+                return f'{m[1]}@{m[2]}'
+        return None
+
     def _scan_mailbox(self, unseen=False, limit=None):
         i = 0
         res, data = self._conn.uid('search', None, '(UNSEEN)' if unseen else 'ALL')
@@ -156,8 +161,10 @@ class MailScanner():
                 if hdrs:
                     list_id = _hdr(hdrs, 'List-Id')
                     if list_id:
-                        original_from = _list_name_from_id(list_id)
-                        is_list = True
+                        list_name = self._list_name_from_id(list_id)
+                        if list_name:
+                            original_from = list_name
+                            is_list = True
 
                 if not original_from:
                     print('unable to extract original sender')
-- 
GitLab