Skip to content

Instantly share code, notes, and snippets.

@markc
Last active June 19, 2018 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markc/eeeb66ce30ea805af62631656cf86c4d to your computer and use it in GitHub Desktop.
Save markc/eeeb66ce30ea805af62631656cf86c4d to your computer and use it in GitHub Desktop.
How to use SpamProbe via sieve rules in Dovecot
This is a brief outline of how to take advantage of using only sieve scripts to manage spam via
`SpamProbe` and `Dovecot` IMAP services. First we assume there is a directory available for
global sieve scripts...
[[ ! -d /usr/lib/dovecot/sieve ]] && mkdir -p /usr/lib/dovecot/sieve
then we add these rules to a Dovecot config file. I'll assume `/etc/dovecot/dovecot.conf` for simplicity...
plugin {
imapsieve_mailbox1_before = file:/usr/lib/dovecot/sieve/retrain-as-spam.sieve
imapsieve_mailbox1_causes = COPY
imapsieve_mailbox1_name = Spam
imapsieve_mailbox2_before = file:/usr/lib/dovecot/sieve/retrain-as-good.sieve
imapsieve_mailbox2_causes = COPY
imapsieve_mailbox2_from = Junk
imapsieve_mailbox2_name = *
sieve = file:~/sieve;active=~/.dovecot.sieve
sieve_after = /usr/lib/dovecot/sieve/spamprobe.sieve
sieve_dir = ~/sieve
sieve_execute_bin_dir = /usr/lib/dovecot/sieve
sieve_extensions = +vnd.dovecot.debug +editheader
sieve_global = /usr/lib/dovecot/sieve/
sieve_global_extensions = +vnd.dovecot.execute
sieve_plugins = sieve_imapsieve sieve_extprograms
}
Next we create 3 sieve scripts and a symlink to the `/usr/bin/spamprobe` binary...
cd /usr/lib/dovecot/sieve
cat << EOS > retrain-as-good.sieve
require ["vnd.dovecot.execute", "variables", "fileinto", "environment", "vnd.dovecot.debug"];
if address :localpart :matches "delivered-to" "*" { set "localpart" "${1}"; }
if address :domain :matches "delivered-to" "*" { set "domain" "${1}"; }
execute :pipe :output "RESULT" "spamprobe" ["-d", "/home/u/${domain}/home/${localpart}/.spamprobe", "good"];
EOS
cat << EOS > retrain-as-spam.sieve
require ["vnd.dovecot.execute", "copy", "variables", "fileinto", "environment", "vnd.dovecot.debug"];
if address :localpart :matches "delivered-to" "*" { set "localpart" "${1}"; }
if address :domain :matches "delivered-to" "*" { set "domain" "${1}"; }
execute :pipe :output "RESULT" "spamprobe" ["-d", "/home/u/${domain}/home/${localpart}/.spamprobe", "spam"];
EOS
cat << EOS > spamprobe.sieve
require [ "variables", "fileinto", "envelope", "editheader", "vnd.dovecot.execute" ];
if envelope :localpart :matches "to" "*" { set "localpart" "${1}"; }
if envelope :domain :matches "to" "*" { set "domain" "${1}"; }
execute :pipe :output "SCORE" "spamprobe" ["-c", "-d", "/home/u/${domain}/home/${localpart}/.spamprobe", "receive"];
addheader :last "X-Spam" "${SCORE}";
if header :matches "X-Spam" "SPAM*" { fileinto "Spam"; }
EOS
sievec retrain-as-good.sieve
sievec retrain-as-spam.sieve
sievec spamprobe.sieve
ln -s /usr/bin/spamprobe
systemctl restart dovecot
Then to set up a user we `su - someuser` and change dir to their virtual home dir where their
`Maildir` exists and "seed" a `.spamprobe` directory. If you do not have a ham/spam corpus handy
then you can use this one...
wget https://renta.net/public/_etc_spamprobe.tgz
tar xf _etc_spamprobe.tgz
mv spamprobe .spamprobe
chown UID:GID -R .spamprobe # where UID:GID is YOUR user and group ID
spamprobe -d .spamprobe counts # to check all is well
And that is mostly it. Any incoming spam should end up in the `Spam` folder and any incorrectly
tagged messages (spam messages in your Inbox or good messages in your Spam folder) can simply
be dragged to or from the `Spam` folder to retrain the `SpamProbe` filtering database.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment