Mail: enable spam training through imap-sieve #61

Merged
rouven0 merged 4 commits from imap-sieve into main 2023-09-06 12:29:24 +02:00
4 changed files with 75 additions and 6 deletions

View file

@ -31,7 +31,7 @@
./modules/sops.nix ./modules/sops.nix
./modules/kpp.nix ./modules/kpp.nix
./modules/ldap ./modules/ldap
./modules/mail.nix ./modules/mail
./modules/mailman.nix ./modules/mailman.nix
./modules/nginx.nix ./modules/nginx.nix
./modules/hydra.nix ./modules/hydra.nix

View file

@ -38,6 +38,34 @@ in
4190 # sieve 4190 # sieve
]; ];
users.users.postfix.extraGroups = [ "opendkim" ]; users.users.postfix.extraGroups = [ "opendkim" ];
environment.etc = {
"dovecot/sieve-pipe/sa-learn-spam.sh" = {
text = ''
#!/bin/sh
${pkgs.rspamd}/bin/rspamc learn_spam
'';
mode = "0555";
};
"dovecot/sieve-pipe/sa-learn-ham.sh" = {
text = ''
#!/bin/sh
${pkgs.rspamd}/bin/rspamc learn_ham
'';
mode = "0555";
};
"dovecot/sieve/report-spam.sieve" = {
source = ./report-spam.sieve;
user = "dovecot2";
group = "dovecot2";
mode = "0544";
};
"dovecot/sieve/report-ham.sieve" = {
source = ./report-ham.sieve;
user = "dovecot2";
group = "dovecot2";
mode = "0544";
};
};
services = { services = {
postfix = { postfix = {
@ -143,7 +171,7 @@ in
mailPlugins = { mailPlugins = {
perProtocol = { perProtocol = {
imap = { imap = {
enable = [ ]; enable = [ "imap_sieve" ];
}; };
lmtp = { lmtp = {
enable = [ "sieve" ]; enable = [ "sieve" ];
@ -152,19 +180,20 @@ in
}; };
mailboxes = { mailboxes = {
Spam = { Spam = {
auto = "create"; auto = "subscribe";
specialUse = "Junk"; specialUse = "Junk";
autoexpunge = "60d";
}; };
Sent = { Sent = {
auto = "create"; auto = "subscribe";
specialUse = "Sent"; specialUse = "Sent";
}; };
Drafts = { Drafts = {
auto = "create"; auto = "subscribe";
specialUse = "Drafts"; specialUse = "Drafts";
}; };
Trash = { Trash = {
auto = "create"; auto = "subscribe";
specialUse = "Trash"; specialUse = "Trash";
}; };
}; };
@ -202,6 +231,24 @@ in
} }
client_limit = 1 client_limit = 1
} }
plugin {
sieve_plugins = sieve_imapsieve sieve_extprograms
sieve_global_extensions = +vnd.dovecot.pipe
sieve_pipe_bin_dir = /etc/dovecot/sieve-pipe
# Spam: From elsewhere to Spam folder or flag changed in Spam folder
imapsieve_mailbox1_name = Spam
imapsieve_mailbox1_causes = COPY APPEND FLAG
imapsieve_mailbox1_before = file:/etc/dovecot/sieve/report-spam.sieve
# Ham: From Spam folder to elsewhere
imapsieve_mailbox2_name = *
imapsieve_mailbox2_from = Spam
imapsieve_mailbox2_causes = COPY
imapsieve_mailbox2_before = file:/etc/dovecot/sieve/report-ham.sieve
}
''; '';
}; };
opendkim = { opendkim = {

15
modules/mail/report-ham.sieve Executable file
View file

@ -0,0 +1,15 @@
require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
if environment :matches "imap.mailbox" "*" {
set "mailbox" "${1}";
}
if string "${mailbox}" "Trash" {
stop;
}
if environment :matches "imap.user" "*" {
set "username" "${1}";
}
pipe :copy "sa-learn-ham.sh" [ "${username}" ];

7
modules/mail/report-spam.sieve Executable file
View file

@ -0,0 +1,7 @@
require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
if environment :matches "imap.user" "*" {
set "username" "${1}";
}
pipe :copy "sa-learn-spam.sh" [ "${username}" ];