mox/smtpserver/rejects.go
Mechiel Lukkien 7f1b7198a8
add condstore & qresync imap extensions
for conditional storing and quick resynchronisation (not sure if mail clients are actually using it that).

each message now has a "modseq". it is increased for each change. with
condstore, imap clients can request changes since a certain modseq. that
already allows quickly finding changes since a previous connection. condstore
also allows storing (e.g. setting new message flags) only when the modseq of a
message hasn't changed.

qresync should make it fast for clients to get a full list of changed messages
for a mailbox, including removals.

we now also keep basic metadata of messages that have been removed (expunged).
just enough (uid, modseq) to tell client that the messages have been removed.
this does mean we have to be careful when querying messages from the database.
we must now often filter the expunged messages out.

we also keep "createseq", the modseq when a message was created. this will be
useful for the jmap implementation.
2023-07-24 21:25:50 +02:00

69 lines
2 KiB
Go

package smtpserver
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"io"
"os"
"github.com/mjl-/bstore"
"github.com/mjl-/mox/message"
"github.com/mjl-/mox/mlog"
"github.com/mjl-/mox/moxio"
"github.com/mjl-/mox/store"
)
// rejectPresent returns whether the message is already present in the rejects mailbox.
func rejectPresent(log *mlog.Log, acc *store.Account, rejectsMailbox string, m *store.Message, f *os.File) (present bool, msgID string, hash []byte, rerr error) {
if p, err := message.Parse(store.FileMsgReader(m.MsgPrefix, f)); err != nil {
log.Infox("parsing reject message for message-id", err)
} else if header, err := p.Header(); err != nil {
log.Infox("parsing reject message header for message-id", err)
} else {
msgID = header.Get("Message-Id")
}
// We must not read MsgPrefix, it will likely change for subsequent deliveries.
h := sha256.New()
if _, err := io.Copy(h, &moxio.AtReader{R: f}); err != nil {
log.Infox("copying reject message to hash", err)
} else {
hash = h.Sum(nil)
}
if msgID == "" && len(hash) == 0 {
return false, "", nil, fmt.Errorf("no message-id or hash for determining reject message presence")
}
var exists bool
var err error
acc.WithRLock(func() {
err = acc.DB.Read(context.TODO(), func(tx *bstore.Tx) error {
mbq := bstore.QueryTx[store.Mailbox](tx)
mbq.FilterNonzero(store.Mailbox{Name: rejectsMailbox})
mb, err := mbq.Get()
if err == bstore.ErrAbsent {
return nil
}
if err != nil {
return fmt.Errorf("looking for rejects mailbox: %w", err)
}
q := bstore.QueryTx[store.Message](tx)
q.FilterNonzero(store.Message{MailboxID: mb.ID})
q.FilterEqual("Expunged", false)
q.FilterFn(func(m store.Message) bool {
return msgID != "" && m.MessageID == msgID || len(hash) > 0 && bytes.Equal(m.MessageHash, hash)
})
exists, err = q.Exists()
return err
})
})
if err != nil {
return false, "", nil, fmt.Errorf("querying for presence of reject message: %w", err)
}
return exists, msgID, hash, nil
}