mailsystem: dovecot: Autolearn ham/spam when moving mails
This commit is contained in:
parent
9149f03384
commit
aacf9a9b8c
3 changed files with 76 additions and 2 deletions
|
|
@ -92,6 +92,14 @@ with (import ./common.nix {inherit config;}); let
|
||||||
chgrp "${cfg.vmailGroupName}" ${cfg.mailDirectory}
|
chgrp "${cfg.vmailGroupName}" ${cfg.mailDirectory}
|
||||||
chmod 02770 ${cfg.mailDirectory}
|
chmod 02770 ${cfg.mailDirectory}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
junkMailboxes = builtins.attrNames (lib.filterAttrs (n: v: v ? "specialUse" && v.specialUse == "Junk") dovecot2Cfg.mailboxes);
|
||||||
|
junkMailboxNumber = builtins.length junkMailboxes;
|
||||||
|
# The assertion guarantees that there is exactly one Junk mailbox.
|
||||||
|
junkMailboxName =
|
||||||
|
if junkMailboxNumber == 1
|
||||||
|
then builtins.elemAt junkMailboxes 0
|
||||||
|
else "";
|
||||||
in {
|
in {
|
||||||
options.mailsystem.dovecot.dhparamSize = lib.mkOption {
|
options.mailsystem.dovecot.dhparamSize = lib.mkOption {
|
||||||
type = lib.types.int;
|
type = lib.types.int;
|
||||||
|
|
@ -101,12 +109,18 @@ in {
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
assertions =
|
assertions =
|
||||||
lib.mapAttrsToList (user: value: [
|
[
|
||||||
{
|
{
|
||||||
|
assertion = junkMailboxNumber == 1;
|
||||||
|
message = "mailnix requires exactly one dovecot mailbox with the 'special use' flag to 'Junk' (${builtins.toString junkMailboxNumber} have been found)";
|
||||||
|
}
|
||||||
|
]
|
||||||
|
++ lib.mapAttrsToList (
|
||||||
|
user: value: {
|
||||||
assertion = value.hashedPasswordFile != null;
|
assertion = value.hashedPasswordFile != null;
|
||||||
message = "A file containing the hashed password for user ${user} needs to be set.";
|
message = "A file containing the hashed password for user ${user} needs to be set.";
|
||||||
}
|
}
|
||||||
])
|
)
|
||||||
cfg.accounts;
|
cfg.accounts;
|
||||||
|
|
||||||
services.dovecot2 = {
|
services.dovecot2 = {
|
||||||
|
|
@ -136,6 +150,44 @@ in {
|
||||||
sieve = "file:~/sieve;active=~/.dovecot.sieve";
|
sieve = "file:~/sieve;active=~/.dovecot.sieve";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sieve = {
|
||||||
|
extensions = [
|
||||||
|
"fileinto"
|
||||||
|
"mailbox"
|
||||||
|
];
|
||||||
|
|
||||||
|
scripts.after = builtins.toFile "spam.sieve" ''
|
||||||
|
require "fileinto";
|
||||||
|
require "mailbox";
|
||||||
|
|
||||||
|
if header :is "X-Spam" "Yes" {
|
||||||
|
fileinto :create "${junkMailboxName}";
|
||||||
|
stop;
|
||||||
|
}
|
||||||
|
'';
|
||||||
|
|
||||||
|
pipeBins = map lib.getExe [
|
||||||
|
(pkgs.writeShellScriptBin "learn-ham.sh"
|
||||||
|
"exec ${pkgs.rspamd}/bin/rspamc -h ${rspamdControllerSocket} learn_ham")
|
||||||
|
(pkgs.writeShellScriptBin "learn-spam.sh"
|
||||||
|
"exec ${pkgs.rspamd}/bin/rspamc -h ${rspamdControllerSocket} learn_spam")
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
imapsieve.mailbox = [
|
||||||
|
{
|
||||||
|
name = junkMailboxName;
|
||||||
|
causes = ["COPY" "APPEND"];
|
||||||
|
before = ./dovecot/report-spam.sieve;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "*";
|
||||||
|
from = junkMailboxName;
|
||||||
|
causes = ["COPY"];
|
||||||
|
before = ./dovecot/report-ham.sieve;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
# TODO: move configuration to default.nix?
|
# TODO: move configuration to default.nix?
|
||||||
mailboxes = {
|
mailboxes = {
|
||||||
Drafts = {
|
Drafts = {
|
||||||
|
|
|
||||||
15
mailsystem/dovecot/report-ham.sieve
Normal file
15
mailsystem/dovecot/report-ham.sieve
Normal 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 "learn-ham.sh" [ "${username}" ];
|
||||||
7
mailsystem/dovecot/report-spam.sieve
Normal file
7
mailsystem/dovecot/report-spam.sieve
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
|
||||||
|
|
||||||
|
if environment :matches "imap.user" "*" {
|
||||||
|
set "username" "${1}";
|
||||||
|
}
|
||||||
|
|
||||||
|
pipe :copy "learn-spam.sh" [ "${username}" ];
|
||||||
Loading…
Add table
Add a link
Reference in a new issue