mirror of
https://github.com/mjl-/mox.git
synced 2025-01-14 01:06:27 +03:00
bf04fb8a1a
before, we used heuristics to decide when to train/untrain a message as junk or nonjunk: the message had to be seen, be in certain mailboxes. then if a message was marked as junk, it was junk. and otherwise it was nonjunk. this wasn't good enough: you may want to keep some messages around as neither junk or nonjunk. and that wasn't possible. ideally, we would just look at the imap $Junk and $NotJunk flags. the problem is that mail clients don't set these flags, or don't make it easy. thunderbird can set the flags based on its own bayesian filter. it has a shortcut for marking Junk and moving it to the junk folder (good), but the counterpart of notjunk only marks a message as notjunk without showing in the UI that it was marked as notjunk. there is also no "move and mark as notjunk" mechanism. e.g. "archive" does not mark a message as notjunk. ios mail and mutt don't appear to have any way to see or change the $Junk and $NotJunk flags. what email clients do have is the ability to move messages to other mailboxes/folders. so mox now has a mechanism that allows you to configure mailboxes that automatically set $Junk or $NotJunk (or clear both) when a message is moved/copied/delivered to that folder. e.g. a mailbox called junk or spam or rejects marks its messags as junk. inbox, postmaster, dmarc, tlsrpt, neutral* mark their messages as neither junk or notjunk. other folders mark their messages as notjunk. e.g. list/*, archive. this functionality is optional, but enabled with the quickstart and for new accounts. also, mox now keeps track of the previous training of a message and will only untrain/train if needed. before, there probably have been duplicate or missing (un)trainings. this also includes a new subcommand "retrain" to recreate the junkfilter for an account. you should run it after updating to this version. and you should probably also modify your account config to include the AutomaticJunkFlags.
155 lines
4 KiB
Go
155 lines
4 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/mjl-/bstore"
|
|
|
|
"github.com/mjl-/mox/config"
|
|
"github.com/mjl-/mox/junk"
|
|
"github.com/mjl-/mox/mlog"
|
|
"github.com/mjl-/mox/mox-"
|
|
)
|
|
|
|
// ErrNoJunkFilter indicates user did not configure/enable a junk filter.
|
|
var ErrNoJunkFilter = errors.New("junkfilter: not configured")
|
|
|
|
// OpenJunkFilter returns an opened junk filter for the account.
|
|
// If the account does not have a junk filter enabled, ErrNotConfigured is returned.
|
|
// Do not forget to save the filter after modifying, and to always close the filter when done.
|
|
// An empty filter is initialized on first access of the filter.
|
|
func (a *Account) OpenJunkFilter(log *mlog.Log) (*junk.Filter, *config.JunkFilter, error) {
|
|
conf, ok := mox.Conf.Account(a.Name)
|
|
if !ok {
|
|
return nil, nil, ErrAccountUnknown
|
|
}
|
|
jf := conf.JunkFilter
|
|
if jf == nil {
|
|
return nil, jf, ErrNoJunkFilter
|
|
}
|
|
|
|
basePath := mox.DataDirPath("accounts")
|
|
dbPath := filepath.Join(basePath, a.Name, "junkfilter.db")
|
|
bloomPath := filepath.Join(basePath, a.Name, "junkfilter.bloom")
|
|
|
|
if _, xerr := os.Stat(dbPath); xerr != nil && os.IsNotExist(xerr) {
|
|
f, err := junk.NewFilter(log, jf.Params, dbPath, bloomPath)
|
|
return f, jf, err
|
|
}
|
|
f, err := junk.OpenFilter(log, jf.Params, dbPath, bloomPath, false)
|
|
return f, jf, err
|
|
}
|
|
|
|
// RetrainMessages (un)trains messages, if relevant given their flags. Updates
|
|
// m.TrainedJunk after retraining.
|
|
func (a *Account) RetrainMessages(log *mlog.Log, tx *bstore.Tx, msgs []Message, absentOK bool) (rerr error) {
|
|
if len(msgs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var jf *junk.Filter
|
|
|
|
for i := range msgs {
|
|
if !msgs[i].NeedsTraining() {
|
|
continue
|
|
}
|
|
|
|
// Lazy open the junk filter.
|
|
if jf == nil {
|
|
var err error
|
|
jf, _, err = a.OpenJunkFilter(log)
|
|
if err != nil && errors.Is(err, ErrNoJunkFilter) {
|
|
// No junk filter configured. Nothing more to do.
|
|
return nil
|
|
}
|
|
defer func() {
|
|
if jf != nil {
|
|
err := jf.Close()
|
|
if rerr == nil {
|
|
rerr = err
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
if err := a.RetrainMessage(log, tx, jf, &msgs[i], absentOK); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// RetrainMessage untrains and/or trains a message, if relevant given m.TrainedJunk
|
|
// and m.Junk/m.Notjunk. Updates m.TrainedJunk after retraining.
|
|
func (a *Account) RetrainMessage(log *mlog.Log, tx *bstore.Tx, jf *junk.Filter, m *Message, absentOK bool) error {
|
|
untrain := m.TrainedJunk != nil
|
|
untrainJunk := untrain && *m.TrainedJunk
|
|
train := m.Junk || m.Notjunk && !(m.Junk && m.Notjunk)
|
|
trainJunk := m.Junk
|
|
|
|
if !untrain && !train || (untrain && train && untrainJunk == trainJunk) {
|
|
return nil
|
|
}
|
|
|
|
log.Info("updating junk filter", mlog.Field("untrain", untrain), mlog.Field("untrainJunk", untrainJunk), mlog.Field("train", train), mlog.Field("trainJunk", trainJunk))
|
|
|
|
mr := a.MessageReader(*m)
|
|
defer mr.Close()
|
|
|
|
p, err := m.LoadPart(mr)
|
|
if err != nil {
|
|
log.Errorx("loading part for message", err)
|
|
return nil
|
|
}
|
|
|
|
words, err := jf.ParseMessage(p)
|
|
if err != nil {
|
|
log.Errorx("parsing message for updating junk filter", err, mlog.Field("parse", ""))
|
|
return nil
|
|
}
|
|
|
|
if untrain {
|
|
err := jf.Untrain(!untrainJunk, words)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m.TrainedJunk = nil
|
|
}
|
|
if train {
|
|
err := jf.Train(!trainJunk, words)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m.TrainedJunk = &trainJunk
|
|
}
|
|
if err := tx.Update(m); err != nil && (!absentOK || err != bstore.ErrAbsent) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TrainMessage trains the junk filter based on the current m.Junk/m.Notjunk flags,
|
|
// disregarding m.TrainedJunk and not updating that field.
|
|
func (a *Account) TrainMessage(log *mlog.Log, jf *junk.Filter, m Message) (bool, error) {
|
|
if !m.Junk && !m.Notjunk || (m.Junk && m.Notjunk) {
|
|
return false, nil
|
|
}
|
|
|
|
mr := a.MessageReader(m)
|
|
defer mr.Close()
|
|
|
|
p, err := m.LoadPart(mr)
|
|
if err != nil {
|
|
log.Errorx("loading part for message", err)
|
|
return false, nil
|
|
}
|
|
|
|
words, err := jf.ParseMessage(p)
|
|
if err != nil {
|
|
log.Errorx("parsing message for updating junk filter", err, mlog.Field("parse", ""))
|
|
return false, nil
|
|
}
|
|
|
|
return true, jf.Train(m.Notjunk, words)
|
|
}
|