mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 18:24:35 +03:00
Refactor how messages are added to mailboxes
DeliverMessage() is now MessageAdd(), and it takes a Mailbox object that it modifies but doesn't write to the database (the caller must do it, and plenty of times can do it more efficiently by doing it once for multiple messages). The new AddOpts let the caller influence how many checks and how much of the work MessageAdd() does. The zero-value AddOpts enable all checks and all the work, but callers can take responsibility of some of the checks/work if it can do it more efficiently itself. This simplifies the code in most places, and makes it more efficient. The checks to update per-mailbox keywords is a bit simpler too now. We are also more careful to close the junk filter without saving it in case of errors. Still part of more upcoming changes.
This commit is contained in:
@ -3413,15 +3413,45 @@ func (c *conn) deliver(ctx context.Context, recvHdrFor func(string) string, msgW
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorx("tidying rejects mailbox", err)
|
||||
} else if hasSpace {
|
||||
if err := a.d.acc.DeliverMailbox(log, conf.RejectsMailbox, a.d.m, dataFile); err != nil {
|
||||
log.Errorx("delivering spammy mail to rejects mailbox", err)
|
||||
} else {
|
||||
log.Info("delivered spammy mail to rejects mailbox")
|
||||
}
|
||||
} else {
|
||||
} else if !hasSpace {
|
||||
log.Info("not storing spammy mail to full rejects mailbox")
|
||||
return
|
||||
}
|
||||
|
||||
var changes []store.Change
|
||||
var stored bool
|
||||
err = a.d.acc.DB.Write(context.TODO(), func(tx *bstore.Tx) error {
|
||||
mbrej, err := a.d.acc.MailboxFind(tx, conf.RejectsMailbox)
|
||||
if err != nil {
|
||||
return fmt.Errorf("finding rejects mailbox: %v", err)
|
||||
}
|
||||
if mbrej == nil {
|
||||
nmb, chl, _, _, err := a.d.acc.MailboxCreate(tx, conf.RejectsMailbox, store.SpecialUse{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating rejects mailbox: %v", err)
|
||||
}
|
||||
changes = append(changes, chl...)
|
||||
|
||||
mbrej = &nmb
|
||||
}
|
||||
a.d.m.MailboxID = mbrej.ID
|
||||
if err := a.d.acc.MessageAdd(log, tx, mbrej, a.d.m, dataFile, store.AddOpts{}); err != nil {
|
||||
return fmt.Errorf("delivering spammy mail to rejects mailbox: %v", err)
|
||||
}
|
||||
if err := tx.Update(mbrej); err != nil {
|
||||
return fmt.Errorf("updating rejects mailbox: %v", err)
|
||||
}
|
||||
changes = append(changes, a.d.m.ChangeAddUID(), mbrej.ChangeCounts())
|
||||
stored = true
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorx("delivering to rejects mailbox", err)
|
||||
return
|
||||
} else if stored {
|
||||
log.Info("stored spammy mail in rejects mailbox")
|
||||
}
|
||||
store.BroadcastChanges(a.d.acc, changes)
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user