From 76e5801d87c02cb258719adbc105e6998760474b Mon Sep 17 00:00:00 2001 From: Rouven Seifert Date: Sun, 3 Sep 2023 22:21:16 +0200 Subject: [PATCH 1/4] mail: enable spam training through imap-sieve --- modules/mail.nix | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/modules/mail.nix b/modules/mail.nix index 5c218cc..3b2c371 100644 --- a/modules/mail.nix +++ b/modules/mail.nix @@ -38,6 +38,22 @@ 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"; + }; + }; services = { postfix = { @@ -143,7 +159,7 @@ in mailPlugins = { perProtocol = { imap = { - enable = [ ]; + enable = [ "imap_sieve" ]; }; lmtp = { enable = [ "sieve" ]; @@ -202,6 +218,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:/var/lib/dovecot/imap_sieve/report-spam.sieve + + # Ham: From Spam folder to elsewhere + imapsieve_mailbox2_name = * + imapsieve_mailbox2_from = Spam + imapsieve_mailbox2_causes = COPY + imapsieve_mailbox1_before = file:/var/lib/dovecot/imap_sieve/report-ham.sieve + } ''; }; opendkim = { -- 2.44.2 From a0f78133f604c1f96421e38dc0d8c6b1c547868f Mon Sep 17 00:00:00 2001 From: Rouven Seifert Date: Sun, 3 Sep 2023 22:53:06 +0200 Subject: [PATCH 2/4] mail: automatically subscribe to all important mailboxes and expunge spam --- modules/mail.nix | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/mail.nix b/modules/mail.nix index 3b2c371..9a3560e 100644 --- a/modules/mail.nix +++ b/modules/mail.nix @@ -168,19 +168,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"; }; }; -- 2.44.2 From 11b090353571035445215ec466d85667051a4cfd Mon Sep 17 00:00:00 2001 From: Rouven Seifert Date: Mon, 4 Sep 2023 13:10:01 +0200 Subject: [PATCH 3/4] mail: put spam sieve scripts into /etc --- modules/{mail.nix => mail/default.nix} | 16 ++++++++++++++-- modules/mail/report-ham.sieve | 15 +++++++++++++++ modules/mail/report-spam.sieve | 7 +++++++ 3 files changed, 36 insertions(+), 2 deletions(-) rename modules/{mail.nix => mail/default.nix} (95%) create mode 100755 modules/mail/report-ham.sieve create mode 100755 modules/mail/report-spam.sieve diff --git a/modules/mail.nix b/modules/mail/default.nix similarity index 95% rename from modules/mail.nix rename to modules/mail/default.nix index 9a3560e..9713b99 100644 --- a/modules/mail.nix +++ b/modules/mail/default.nix @@ -53,6 +53,18 @@ in ''; 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 = { @@ -229,13 +241,13 @@ in # 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:/var/lib/dovecot/imap_sieve/report-spam.sieve + 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_mailbox1_before = file:/var/lib/dovecot/imap_sieve/report-ham.sieve + imapsieve_mailbox2_before = file:/etc/dovecot/sieve/report-ham.sieve } ''; }; 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 -- 2.44.2 From 664f87444d9a19fa4749e7cb0580f2171e30c70a Mon Sep 17 00:00:00 2001 From: Rouven Seifert Date: Mon, 4 Sep 2023 13:14:05 +0200 Subject: [PATCH 4/4] fix flake.nix --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 -- 2.44.2