From 671fc5b8f1078a4e8a702de7168eb2a319aba990 Mon Sep 17 00:00:00 2001 From: bobobo1618 Date: Sun, 23 Jul 2023 17:03:09 +0200 Subject: [PATCH] Add a 'KeepRejects' option that disables auto-cleanup (#49) Add a 'KeepRejects' option that disables auto cleanup of the rejects mailbox. --- config/config.go | 1 + config/doc.go | 4 ++++ smtpserver/server.go | 7 ++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index a36f968..766aaa3 100644 --- a/config/config.go +++ b/config/config.go @@ -300,6 +300,7 @@ type Account struct { Period time.Duration `sconf-doc:"How long unique values are accepted after generating, e.g. 12h."` // todo: have a reasonable default for this? } `sconf:"optional" sconf-doc:"If configured, messages classified as weakly spam are rejected with instructions to retry delivery, but this time with a signed token added to the subject. During the next delivery attempt, the signed token will bypass the spam filter. Messages with a clear spam signal, such as a known bad reputation, are rejected/delayed without a signed token."` RejectsMailbox string `sconf:"optional" sconf-doc:"Mail that looks like spam will be rejected, but a copy can be stored temporarily in a mailbox, e.g. Rejects. If mail isn't coming in when you expect, you can look there. The mail still isn't accepted, so the remote mail server may retry (hopefully, if legitimate), or give up (hopefully, if indeed a spammer). Messages are automatically removed from this mailbox, so do not set it to a mailbox that has messages you want to keep."` + KeepRejects bool `sconf:"optional" sconf-doc:"Don't automatically delete mail in the RejectsMailbox listed above. This can be useful, e.g. for future spam training."` AutomaticJunkFlags struct { Enabled bool `sconf-doc:"If enabled, flags will be set automatically if they match a regular expression below. When two of the three mailbox regular expressions are set, the remaining one will match all unmatched messages. Messages are matched in the order specified and the search stops on the first match. Mailboxes are lowercased before matching."` JunkMailboxRegexp string `sconf:"optional" sconf-doc:"Example: ^(junk|spam)."` diff --git a/config/doc.go b/config/doc.go index 120c5f1..5035752 100644 --- a/config/doc.go +++ b/config/doc.go @@ -692,6 +692,10 @@ describe-static" and "mox config describe-domains": # mailbox that has messages you want to keep. (optional) RejectsMailbox: + # If you'd like to retain the rejects described above instead of automatically + # cleaning them (e.g. to train spam filters), you can set this to true. (optional) + KeepRejects: + # Automatically set $Junk and $NotJunk flags based on mailbox messages are # delivered/moved/copied to. Email clients typically have too limited # functionality to conveniently set these flags, especially $NonJunk, but they can diff --git a/smtpserver/server.go b/smtpserver/server.go index 6ca0a84..32b1977 100644 --- a/smtpserver/server.go +++ b/smtpserver/server.go @@ -2418,7 +2418,12 @@ func (c *conn) deliver(ctx context.Context, recvHdrFor func(string) string, msgW m.MessageID = messageid m.MessageHash = messagehash acc.WithWLock(func() { - if hasSpace, err := acc.TidyRejectsMailbox(c.log, conf.RejectsMailbox); err != nil { + hasSpace := true + var err error + if !conf.KeepRejects { + hasSpace, err = acc.TidyRejectsMailbox(c.log, conf.RejectsMailbox) + } + if err != nil { log.Errorx("tidying rejects mailbox", err) } else if hasSpace { if err := acc.DeliverMailbox(log, conf.RejectsMailbox, m, dataFile, false); err != nil {