nixos-config/packages/sieves.nix

86 lines
2.7 KiB
Nix
Raw Permalink Normal View History

2022-06-12 15:39:15 +00:00
{
lib,
writeText,
writeScript,
zsh,
coreutils-full,
rspamd,
...
} @ pkgs: let
2022-01-20 16:02:42 +00:00
sievec = import ./sievec.nix pkgs;
rspamd_host = "localhost";
2022-01-20 16:02:42 +00:00
sa-learn-ham-script = writeScript "sa-learn-ham" ''
#!${zsh}/bin/zsh
tmpfile=$(${coreutils-full}/bin/mktemp /tmp/spam.XXXXXX)
${coreutils-full}/bin/cat > $tmpfile
password=$(${coreutils-full}/bin/cat /run/secrets/services/dovecot/rspamd_password)
${coreutils-full}/bin/cat $tmpfile | ${rspamd}/bin/rspamc -P $password -h ${rspamd_host} learn_ham
${coreutils-full}/bin/cat $tmpfile | ${rspamd}/bin/rspamc -P $password -h ${rspamd_host} -w 5 -f 11 fuzzy_del
${coreutils-full}/bin/rm $tmpfile
'';
sa-learn-spam-script = writeScript "sa-learn-spam" ''
#!${zsh}/bin/zsh
tmpfile=$(${coreutils-full}/bin/mktemp /tmp/spam.XXXXXX)
${coreutils-full}/bin/cat > $tmpfile
password=$(${coreutils-full}/bin/cat /run/secrets/services/dovecot/rspamd_password)
${coreutils-full}/bin/cat $tmpfile | ${rspamd}/bin/rspamc -P $password -h ${rspamd_host} learn_spam
${coreutils-full}/bin/cat $tmpfile | ${rspamd}/bin/rspamc -P $password -h ${rspamd_host} -w 5 -f 11 fuzzy_add
${coreutils-full}/bin/rm $tmpfile
'';
removeStore = location: lib.removePrefix "/nix/store/" "${location}";
2022-06-12 15:39:15 +00:00
in {
2022-01-20 16:02:42 +00:00
default = sievec {
name = "default";
src = writeText "default.sieve" ''
require "fileinto";
if header :contains "X-Spam" "YES" {
fileinto "Junk";
}
'';
2022-06-12 15:39:15 +00:00
exts = ["fileinto"];
2022-01-20 16:02:42 +00:00
};
report-ham = sievec {
name = "report-ham";
src = writeText "report-ham.sieve" ''
require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
if environment :matches "imap.mailbox" "*" {
set "mailbox" "$${1}";
}
2022-01-20 16:02:42 +00:00
if string "$${mailbox}" [ "Trash", "train_ham", "train_prob", "train_spam" ] {
stop;
}
pipe :copy "${removeStore sa-learn-ham-script}";
'';
2022-06-12 15:39:15 +00:00
exts = ["vnd.dovecot.pipe" "copy" "imapsieve" "environment" "variables"];
2022-01-20 16:02:42 +00:00
};
report-spam = sievec {
name = "report-spam";
src = writeText "report-spam.sieve" ''
require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "imap4flags"];
if environment :is "imap.cause" "COPY" {
pipe :copy "${removeStore sa-learn-spam-script}";
}
# Catch replied or forwarded spam
elsif anyof (allof (hasflag "\\Answered",
environment :contains "imap.changedflags" "\\Answered"),
allof (hasflag "$Forwarded",
environment :contains "imap.changedflags" "$Forwarded")) {
pipe :copy "${removeStore sa-learn-spam-script}";
}
'';
2022-06-12 15:39:15 +00:00
exts = ["vnd.dovecot.pipe" "copy" "imapsieve" "environment" "imap4flags"];
2022-01-20 16:02:42 +00:00
};
}