email: Implement spam sorting with sieve

- Sieve script detects spam headers used by Rspamd and SpamAssassin
- Autosubscribe to Archive, Drafts, Junk, Sent, Trash
- Autoexpunge Trash and Junk folders
- INBOX.Junk -> Junk, INBOX.Trash -> Trash, Sent Messages -> Sent
This commit is contained in:
fliu 2021-07-29 03:46:42 +00:00 committed by Sunil Mohan Adapa
parent 41f0461ac7
commit c31a896e81
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
5 changed files with 83 additions and 0 deletions

View File

@ -1,7 +1,9 @@
"""Configures spam filters and the virus scanner"""
# SPDX-License-Identifier: AGPL-3.0-or-later
import glob
import logging
import subprocess
from plinth import actions
@ -50,3 +52,21 @@ def fix_filter(diagnosis):
def action_set_filter():
with postconf.mutex.lock_all():
fix_filter(check_filter())
_compile_sieve()
def _compile_sieve():
sieve_list = glob.glob('/etc/dovecot/freedombox-sieve-after/*.sieve')
for sieve_file in sieve_list:
_run_sievec(sieve_file)
def _run_sievec(sieve_file):
logger.info('Compiling sieve script %s', sieve_file)
args = ['sievec', '--', sieve_file]
completed = subprocess.run(args, capture_output=True)
if completed.returncode != 0:
logger.critical('Subprocess returned %d', completed.returncode)
logger.critical('Stdout: %r', completed.stdout)
logger.critical('Stderr: %r', completed.stderr)
raise OSError('Sieve compilation failed: ' + sieve_file)

View File

@ -0,0 +1,6 @@
# Direct edits to this file will be lost!
# Manage your settings on Plinth <https://localhost/plinth/apps/email_server>
protocol lmtp {
mail_plugins = $mail_plugins sieve
}

View File

@ -0,0 +1,43 @@
# Direct edits to this file will be lost!
# Manage your settings on Plinth <https://localhost/plinth/apps/email_server>
namespace inbox {
mailbox Archive {
auto = subscribe
special_use = \Archive
}
mailbox Drafts {
auto = subscribe
special_use = \Drafts
}
mailbox Junk {
auto = subscribe
autoexpunge = 30d
special_use = \Junk
}
mailbox Trash {
auto = subscribe
autoexpunge = 30d
special_use = \Trash
}
mailbox INBOX.Junk {
auto = no
autoexpunge = 30d
special_use = \Junk
}
mailbox INBOX.Trash {
auto = no
autoexpunge = 30d
special_use = \Trash
}
mailbox Sent {
auto = subscribe
special_use = \Sent
}
mailbox "Sent Messages" {
auto = no
special_use = \Sent
}
}

View File

@ -0,0 +1,6 @@
# Direct edits to this file will be lost!
# Manage your settings on Plinth <https://localhost/plinth/apps/email_server>
plugin {
sieve_after = /etc/dovecot/freedombox-sieve-after
}

View File

@ -0,0 +1,8 @@
require ["fileinto", "mailbox"];
if anyof(header :contains "X-Spam" "Yes",
header :contains "X-Spam-Status" "Yes",
header :matches "X-Spam-Flag" "YES") {
fileinto :create "Junk";
stop;
}