From 1ce49693dd21192095ef2f91c12febea957bb94a Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Fri, 28 Jun 2019 09:29:53 +0200 Subject: [PATCH] afew is too complex after all --- config/afew/config | 31 ---------- config/afew/only_contacts.py | 33 ---------- .../spam_filter.py => notmuch-hooks/blacklist | 17 +----- notmuch-hooks/post-new | 61 ++++++++++++++++++- 4 files changed, 60 insertions(+), 82 deletions(-) delete mode 100644 config/afew/config delete mode 100644 config/afew/only_contacts.py rename config/afew/spam_filter.py => notmuch-hooks/blacklist (86%) diff --git a/config/afew/config b/config/afew/config deleted file mode 100644 index 665f7a9..0000000 --- a/config/afew/config +++ /dev/null @@ -1,31 +0,0 @@ -[FolderNameFilter] -folder_blacklist = Inbox Sent Drafts Trash -maildir_separator = / - -[SpamFilter] -[KnownSpammers] - -[TagOrContact.1] -query = tag:posteo - -[Filter.1] -message = Tag messages to my Zeus account -query = to:felixvdj@zeus.ugent.be -tags = +zeus - -[Filter.2] -message = The Dodona alias -query = to:dodona@ugent.be -tags = +dodona - -[Filter.3] -message = The Unipept alias -query = to:unipept@ugent.be -tags = +unipept - -[Filter.4] -query = tag:dodona and subject:'Onderwijsinstelling aangemaakt voor .*' -tags = -new - -[ListMailsFilter] -[InboxFilter] diff --git a/config/afew/only_contacts.py b/config/afew/only_contacts.py deleted file mode 100644 index ca08dad..0000000 --- a/config/afew/only_contacts.py +++ /dev/null @@ -1,33 +0,0 @@ -from afew.filters.BaseFilter import Filter -from afew.FilterRegistry import register_filter - -from khard.config import Config - -import re - -@register_filter -class TagOrContact(Filter): - message = 'Allow only tagged messages from non-contacts' - user = None - domain = None - - def __init__(self, database, **kwargs): - super(TagOrContact, self).__init__(database, **kwargs) - self._extract_tag_re = re.compile(f'[^+]+(\+(?P[^@]+))?@[^@]+') - self._bare_email_re = re.compile(r'[^<]*<(?P[^@<>]+@[^@<>]+)>') - self._contactbook = Config().abook - - def handle_message(self, message): - recipient = message.get_header('Delivered-To') - match = self._extract_tag_re.fullmatch(recipient) - if match is None: return # wrong email - - if match.group('tag'): - self.add_tags(message, match['tag']) - else: - sender = message.get_header('From') - if '<' in sender: sender = self._bare_email_re.fullmatch(sender)['email'] - if self._contactbook.search(sender): - self.add_tags(message, 'contact') - else: - self.add_tags(message, 'killed') diff --git a/config/afew/spam_filter.py b/notmuch-hooks/blacklist similarity index 86% rename from config/afew/spam_filter.py rename to notmuch-hooks/blacklist index 8421699..2ca6c58 100644 --- a/config/afew/spam_filter.py +++ b/notmuch-hooks/blacklist @@ -1,7 +1,4 @@ -from afew.filters.BaseFilter import Filter -from afew.FilterRegistry import register_filter - -spammers = """ +bio-congress.com golbalpublication.online all.bg vodokanal.poltava.ua @@ -155,15 +152,3 @@ oap-journals.com frontiermeetings.com submitresearch.online editor.factor21@daum.net -""" - -@register_filter -class KnownSpammers(Filter): - - def __init__(self, database, **kwargs): - super(KnownSpammers, self).__init__(database, **kwargs) - self.query = ' or '.join(f'from:{s}' for s in spammers.split('\n') if s) - - def handle_message(self, message): - self.add_tags(message, 'spam') - self.remove_tags(message, 'new') diff --git a/notmuch-hooks/post-new b/notmuch-hooks/post-new index d614d7b..e1a67f4 100755 --- a/notmuch-hooks/post-new +++ b/notmuch-hooks/post-new @@ -1,4 +1,61 @@ #!/bin/sh -afew --tag --new -notmuch search tag:unread | cut -d' ' --complement -f1 | xargs -0 --no-run-if-empty notify-send +addtag() { notmuch tag +"$1" -- "tag:new and ($2) and not tag:$1"; } +archive() { notmuch tag -new -unread -- "$@" and tag:new; } +spam() { notmuch tag -new -unread +spam -- "$@" and '(' tag:new or tag:unread or not tag:spam ')'; } + +# Tag mails by their origin (top-level maildir) +for maildir in /data/mail/*; do + origin="$(basename "$maildir")" + addtag "$origin" "folder:/$origin/" +done + +# Spam +spam "subject:/^\[SPAM\]/" +# TODO: Known spammers + +# Some aliases +addtag zeus "to:felixvdj@zeus.ugent.be" +addtag dodona "to:dodona@ugent.be" +addtag unipept "to:unipept@ugent.be" + +# More complex tagging +for mid in $(notmuch search --output=messages tag:new); do + + tag=false + + # +-tags become actual tags + for tag in $(notmuch show --format=raw "$mid" | sed -n '/^\tfor/s/.*<[^>]*+\([^@]*\)@.*>.*/\1/p' | sort | uniq); do + addtag "$tag" "$mid" + tag=true + done + + # messages from contacts get a contact tag + for sender in $(notmuch address --output=sender --output=address "$mid"); do + if khard list "$sender" > /dev/null 2>&1; then + addtag contact "$mid" + tag=true + fi + done + + # mails to posteo must have a tag + if ! "$tag"; then + addtag killed "$mid and tag:posteo" + fi + + # list ids + for list in $(notmuch show --format=raw "$mid" | sed -n '/^List-Id:/,/^\w/p' | sed '$d' | tr '\n' ' ' | sed 's/[^<]*