diff --git a/flake.nix b/flake.nix index 39ad4f9..87a9710 100755 --- a/flake.nix +++ b/flake.nix @@ -31,7 +31,7 @@ ./modules/sops.nix ./modules/kpp.nix ./modules/ldap - ./modules/mail.nix + ./modules/mail ./modules/mailman.nix ./modules/nginx.nix ./modules/hydra.nix diff --git a/modules/mail.nix b/modules/mail/default.nix similarity index 83% rename from modules/mail.nix rename to modules/mail/default.nix index 5c218cc..9713b99 100644 --- a/modules/mail.nix +++ b/modules/mail/default.nix @@ -38,6 +38,34 @@ in 4190 # sieve ]; 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 = { postfix = { @@ -143,7 +171,7 @@ in mailPlugins = { perProtocol = { imap = { - enable = [ ]; + enable = [ "imap_sieve" ]; }; lmtp = { enable = [ "sieve" ]; @@ -152,19 +180,20 @@ in }; mailboxes = { Spam = { - auto = "create"; + auto = "subscribe"; specialUse = "Junk"; + autoexpunge = "60d"; }; Sent = { - auto = "create"; + auto = "subscribe"; specialUse = "Sent"; }; Drafts = { - auto = "create"; + auto = "subscribe"; specialUse = "Drafts"; }; Trash = { - auto = "create"; + auto = "subscribe"; specialUse = "Trash"; }; }; @@ -202,6 +231,24 @@ in } 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 = { diff --git a/modules/mail/report-ham.sieve b/modules/mail/report-ham.sieve new file mode 100755 index 0000000..a9d30cf --- /dev/null +++ b/modules/mail/report-ham.sieve @@ -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}" ]; diff --git a/modules/mail/report-spam.sieve b/modules/mail/report-spam.sieve new file mode 100755 index 0000000..4024b7a --- /dev/null +++ b/modules/mail/report-spam.sieve @@ -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}" ]; \ No newline at end of file