1// Package webops implements shared functionality between webapisrv and webmail.
16 "github.com/mjl-/bstore"
18 "github.com/mjl-/mox/junk"
19 "github.com/mjl-/mox/message"
20 "github.com/mjl-/mox/mlog"
21 "github.com/mjl-/mox/moxio"
22 "github.com/mjl-/mox/store"
25var ErrMessageNotFound = errors.New("no such message")
28 DBWrite func(ctx context.Context, acc *store.Account, fn func(tx *bstore.Tx))
29 Checkf func(ctx context.Context, err error, format string, args ...any)
30 Checkuserf func(ctx context.Context, err error, format string, args ...any)
33func (x XOps) mailboxID(ctx context.Context, tx *bstore.Tx, mailboxID int64) store.Mailbox {
35 x.Checkuserf(ctx, errors.New("invalid zero mailbox ID"), "getting mailbox")
37 mb, err := store.MailboxID(tx, mailboxID)
38 if err == bstore.ErrAbsent || err == store.ErrMailboxExpunged {
39 x.Checkuserf(ctx, err, "getting mailbox")
41 x.Checkf(ctx, err, "getting mailbox")
45// messageID returns a non-expunged message or panics with a sherpa error.
46func (x XOps) messageID(ctx context.Context, tx *bstore.Tx, messageID int64) store.Message {
48 x.Checkuserf(ctx, errors.New("invalid zero message id"), "getting message")
50 m := store.Message{ID: messageID}
52 if err == bstore.ErrAbsent {
53 x.Checkuserf(ctx, ErrMessageNotFound, "getting message")
54 } else if err == nil && m.Expunged {
55 x.Checkuserf(ctx, errors.New("message was removed"), "getting message")
57 x.Checkf(ctx, err, "getting message")
61func (x XOps) MessageDelete(ctx context.Context, log mlog.Log, acc *store.Account, messageIDs []int64) {
62 acc.WithWLock(func() {
63 var changes []store.Change
65 x.DBWrite(ctx, acc, func(tx *bstore.Tx) {
66 var modseq store.ModSeq
67 changes = x.MessageDeleteTx(ctx, log, tx, acc, messageIDs, &modseq)
70 store.BroadcastChanges(acc, changes)
74func (x XOps) MessageDeleteTx(ctx context.Context, log mlog.Log, tx *bstore.Tx, acc *store.Account, messageIDs []int64, modseq *store.ModSeq) []store.Change {
75 changes := make([]store.Change, 0, 1+1) // 1 remove, 1 mailbox counts, optimistic that all messages are in 1 mailbox.
80 err := jf.CloseDiscard()
81 log.Check(err, "close junk filter")
88 var changeRemoveUIDs store.ChangeRemoveUIDs
89 xflushMailbox := func() {
91 x.Checkf(ctx, err, "updating mailbox counts")
92 slices.Sort(changeRemoveUIDs.UIDs)
93 changes = append(changes, mb.ChangeCounts(), changeRemoveUIDs)
96 for _, id := range messageIDs {
97 m := x.messageID(ctx, tx, id)
101 *modseq, err = acc.NextModSeq(tx)
102 x.Checkf(ctx, err, "assigning next modseq")
105 if m.MailboxID != mb.ID {
109 mb = x.mailboxID(ctx, tx, m.MailboxID)
111 changeRemoveUIDs = store.ChangeRemoveUIDs{MailboxID: mb.ID, ModSeq: *modseq}
114 if m.Junk != m.Notjunk && jf == nil && conf.JunkFilter != nil {
116 jf, _, err = acc.OpenJunkFilter(ctx, log)
117 x.Checkf(ctx, err, "open junk filter")
120 opts := store.RemoveOpts{JunkFilter: jf}
121 _, _, err := acc.MessageRemove(log, tx, *modseq, &mb, opts, m)
122 x.Checkf(ctx, err, "expunge message")
124 changeRemoveUIDs.UIDs = append(changeRemoveUIDs.UIDs, m.UID)
125 changeRemoveUIDs.MsgIDs = append(changeRemoveUIDs.MsgIDs, m.ID)
133 x.Checkf(ctx, err, "close junk filter")
139func (x XOps) MessageFlagsAdd(ctx context.Context, log mlog.Log, acc *store.Account, messageIDs []int64, flaglist []string) {
140 flags, keywords, err := store.ParseFlagsKeywords(flaglist)
141 x.Checkuserf(ctx, err, "parsing flags")
143 acc.WithRLock(func() {
144 var changes []store.Change
146 x.DBWrite(ctx, acc, func(tx *bstore.Tx) {
147 var modseq store.ModSeq
148 var retrain []store.Message
149 var mb, origmb store.Mailbox
151 for _, mid := range messageIDs {
152 m := x.messageID(ctx, tx, mid)
155 modseq, err = acc.NextModSeq(tx)
156 x.Checkf(ctx, err, "assigning next modseq")
159 if mb.ID != m.MailboxID {
162 err := tx.Update(&mb)
163 x.Checkf(ctx, err, "updating mailbox")
164 if mb.MailboxCounts != origmb.MailboxCounts {
165 changes = append(changes, mb.ChangeCounts())
167 if mb.KeywordsChanged(origmb) {
168 changes = append(changes, mb.ChangeKeywords())
171 mb = x.mailboxID(ctx, tx, m.MailboxID)
174 mb.Keywords, _ = store.MergeKeywords(mb.Keywords, keywords)
176 mb.Sub(m.MailboxCounts())
178 m.Flags = m.Flags.Set(flags, flags)
180 m.Keywords, kwChanged = store.MergeKeywords(m.Keywords, keywords)
181 mb.Add(m.MailboxCounts())
183 if m.Flags == oflags && !kwChanged {
189 x.Checkf(ctx, err, "updating message")
191 changes = append(changes, m.ChangeFlags(oflags))
192 retrain = append(retrain, m)
197 err := tx.Update(&mb)
198 x.Checkf(ctx, err, "updating mailbox")
199 if mb.MailboxCounts != origmb.MailboxCounts {
200 changes = append(changes, mb.ChangeCounts())
202 if mb.KeywordsChanged(origmb) {
203 changes = append(changes, mb.ChangeKeywords())
207 err = acc.RetrainMessages(ctx, log, tx, retrain)
208 x.Checkf(ctx, err, "retraining messages")
211 store.BroadcastChanges(acc, changes)
215func (x XOps) MessageFlagsClear(ctx context.Context, log mlog.Log, acc *store.Account, messageIDs []int64, flaglist []string) {
216 flags, keywords, err := store.ParseFlagsKeywords(flaglist)
217 x.Checkuserf(ctx, err, "parsing flags")
219 acc.WithRLock(func() {
220 var retrain []store.Message
221 var changes []store.Change
223 x.DBWrite(ctx, acc, func(tx *bstore.Tx) {
224 var modseq store.ModSeq
225 var mb, origmb store.Mailbox
227 for _, mid := range messageIDs {
228 m := x.messageID(ctx, tx, mid)
231 modseq, err = acc.NextModSeq(tx)
232 x.Checkf(ctx, err, "assigning next modseq")
235 if mb.ID != m.MailboxID {
238 err := tx.Update(&mb)
239 x.Checkf(ctx, err, "updating counts for mailbox")
240 if mb.MailboxCounts != origmb.MailboxCounts {
241 changes = append(changes, mb.ChangeCounts())
243 // note: cannot remove keywords from mailbox by removing keywords from message.
245 mb = x.mailboxID(ctx, tx, m.MailboxID)
250 mb.Sub(m.MailboxCounts())
251 m.Flags = m.Flags.Set(flags, store.Flags{})
253 m.Keywords, changed = store.RemoveKeywords(m.Keywords, keywords)
254 mb.Add(m.MailboxCounts())
256 if m.Flags == oflags && !changed {
262 x.Checkf(ctx, err, "updating message")
264 changes = append(changes, m.ChangeFlags(oflags))
265 retrain = append(retrain, m)
270 err := tx.Update(&mb)
271 x.Checkf(ctx, err, "updating keywords in mailbox")
272 if mb.MailboxCounts != origmb.MailboxCounts {
273 changes = append(changes, mb.ChangeCounts())
275 // note: cannot remove keywords from mailbox by removing keywords from message.
278 err = acc.RetrainMessages(ctx, log, tx, retrain)
279 x.Checkf(ctx, err, "retraining messages")
282 store.BroadcastChanges(acc, changes)
286// MailboxesMarkRead updates all messages in the referenced mailboxes as seen when
287// they aren't yet. The mailboxes are updated with their unread messages counts,
288// and the changes are propagated.
289func (x XOps) MailboxesMarkRead(ctx context.Context, log mlog.Log, acc *store.Account, mailboxIDs []int64) {
290 acc.WithRLock(func() {
291 var changes []store.Change
293 x.DBWrite(ctx, acc, func(tx *bstore.Tx) {
294 var modseq store.ModSeq
296 // Note: we don't need to retrain, changing the "seen" flag is not relevant.
298 for _, mbID := range mailboxIDs {
299 mb := x.mailboxID(ctx, tx, mbID)
301 // Find messages to update.
302 q := bstore.QueryTx[store.Message](tx)
303 q.FilterNonzero(store.Message{MailboxID: mb.ID})
304 q.FilterEqual("Seen", false)
305 q.FilterEqual("Expunged", false)
308 err := q.ForEach(func(m store.Message) error {
309 have = true // We need to update mailbox.
312 mb.Sub(m.MailboxCounts())
314 mb.Add(m.MailboxCounts())
318 modseq, err = acc.NextModSeq(tx)
319 x.Checkf(ctx, err, "assigning next modseq")
323 x.Checkf(ctx, err, "updating message")
325 changes = append(changes, m.ChangeFlags(oflags))
328 x.Checkf(ctx, err, "listing messages to mark as read")
332 err := tx.Update(&mb)
333 x.Checkf(ctx, err, "updating mailbox")
334 changes = append(changes, mb.ChangeCounts())
339 store.BroadcastChanges(acc, changes)
343// MessageMove moves messages to the mailbox represented by mailboxName, or to mailboxID if mailboxName is empty.
344func (x XOps) MessageMove(ctx context.Context, log mlog.Log, acc *store.Account, messageIDs []int64, mailboxName string, mailboxID int64) {
345 acc.WithWLock(func() {
346 var changes []store.Change
350 for _, id := range newIDs {
351 p := acc.MessagePath(id)
353 log.Check(err, "removing delivered message after failure", slog.String("path", p))
357 x.DBWrite(ctx, acc, func(tx *bstore.Tx) {
358 if mailboxName != "" {
359 mb, err := acc.MailboxFind(tx, mailboxName)
360 x.Checkf(ctx, err, "looking up mailbox name")
362 x.Checkuserf(ctx, errors.New("not found"), "looking up mailbox name")
368 mbDst := x.mailboxID(ctx, tx, mailboxID)
370 if len(messageIDs) == 0 {
374 var modseq store.ModSeq
375 newIDs, changes = x.MessageMoveTx(ctx, log, acc, tx, messageIDs, mbDst, &modseq)
379 store.BroadcastChanges(acc, changes)
383// MessageMoveTx moves message to a new mailbox, which must be different than their
384// current mailbox. Moving a message is done by changing the MailboxID and
385// assigning an appriorate new UID, and then inserting a replacement Message record
386// with new ID that is marked expunged in the original mailbox, along with a
387// MessageErase record so the message gets erased when all sessions stopped
388// referencing the message.
389func (x XOps) MessageMoveTx(ctx context.Context, log mlog.Log, acc *store.Account, tx *bstore.Tx, messageIDs []int64, mbDst store.Mailbox, modseq *store.ModSeq) ([]int64, []store.Change) {
396 for _, id := range newIDs {
397 p := acc.MessagePath(id)
399 log.Check(err, "removing delivered message after failure", slog.String("path", p))
404 // n adds, 1 remove, 2 mailboxcounts, 1 mailboxkeywords, optimistic that messages are in a single source mailbox.
405 changes := make([]store.Change, 0, len(messageIDs)+4)
409 *modseq, err = acc.NextModSeq(tx)
410 x.Checkf(ctx, err, "assigning next modseq")
413 mbDst.ModSeq = *modseq
415 // Get messages. group them by mailbox.
416 l := make([]store.Message, len(messageIDs))
417 for i, id := range messageIDs {
418 l[i] = x.messageID(ctx, tx, id)
419 if l[i].MailboxID == mbDst.ID {
420 // Client should filter out messages that are already in mailbox.
421 x.Checkuserf(ctx, fmt.Errorf("message %d already in destination mailbox", l[i].ID), "moving message")
425 // Sort (group) by mailbox, sort by UID.
426 sort.Slice(l, func(i, j int) bool {
427 if l[i].MailboxID != l[j].MailboxID {
428 return l[i].MailboxID < l[j].MailboxID
430 return l[i].UID < l[j].UID
436 err := jf.CloseDiscard()
437 log.Check(err, "close junk filter")
441 accConf, _ := acc.Conf()
443 var mbSrc store.Mailbox
444 var changeRemoveUIDs store.ChangeRemoveUIDs
445 xflushMailbox := func() {
446 changes = append(changes, changeRemoveUIDs, mbSrc.ChangeCounts())
448 err = tx.Update(&mbSrc)
449 x.Checkf(ctx, err, "updating source mailbox counts")
452 nkeywords := len(mbDst.Keywords)
455 syncDirs := map[string]struct{}{}
457 for _, om := range l {
458 if om.MailboxID != mbSrc.ID {
462 mbSrc = x.mailboxID(ctx, tx, om.MailboxID)
463 mbSrc.ModSeq = *modseq
464 changeRemoveUIDs = store.ChangeRemoveUIDs{MailboxID: mbSrc.ID, ModSeq: *modseq}
468 nm.MailboxID = mbDst.ID
469 nm.UID = mbDst.UIDNext
472 nm.CreateSeq = *modseq
474 if nm.IsReject && nm.MailboxDestinedID != 0 {
475 // Incorrectly delivered to Rejects mailbox. Adjust MailboxOrigID so this message
476 // is used for reputation calculation during future deliveries.
477 nm.MailboxOrigID = nm.MailboxDestinedID
485 nm.JunkFlagsForMailbox(mbDst, accConf)
487 err := tx.Update(&nm)
488 x.Checkf(ctx, err, "updating message with new mailbox")
490 mbDst.Add(nm.MailboxCounts())
492 mbSrc.Sub(om.MailboxCounts())
498 x.Checkf(ctx, err, "inserting expunged message in old mailbox")
500 dstPath := acc.MessagePath(om.ID)
501 dstDir := filepath.Dir(dstPath)
502 if _, ok := syncDirs[dstDir]; !ok {
503 os.MkdirAll(dstDir, 0770)
504 syncDirs[dstDir] = struct{}{}
507 err = moxio.LinkOrCopy(log, dstPath, acc.MessagePath(nm.ID), nil, false)
508 x.Checkf(ctx, err, "duplicating message in old mailbox for current sessions")
509 newIDs = append(newIDs, nm.ID)
510 // We don't sync the directory. In case of a crash and files disappearing, the
511 // eraser will simply not find the file at next startup.
513 err = tx.Insert(&store.MessageErase{ID: om.ID, SkipUpdateDiskUsage: true})
514 x.Checkf(ctx, err, "insert message erase")
516 mbDst.Keywords, _ = store.MergeKeywords(mbDst.Keywords, nm.Keywords)
518 if accConf.JunkFilter != nil && nm.NeedsTraining() {
519 // Lazily open junk filter.
521 jf, _, err = acc.OpenJunkFilter(ctx, log)
522 x.Checkf(ctx, err, "open junk filter")
524 err := acc.RetrainMessage(ctx, log, tx, jf, &nm)
525 x.Checkf(ctx, err, "retrain message after moving")
528 changeRemoveUIDs.UIDs = append(changeRemoveUIDs.UIDs, om.UID)
529 changeRemoveUIDs.MsgIDs = append(changeRemoveUIDs.MsgIDs, om.ID)
530 changes = append(changes, nm.ChangeAddUID())
533 for dir := range syncDirs {
534 err := moxio.SyncDir(log, dir)
535 x.Checkf(ctx, err, "sync directory")
540 changes = append(changes, mbDst.ChangeCounts())
541 if nkeywords > len(mbDst.Keywords) {
542 changes = append(changes, mbDst.ChangeKeywords())
545 err = tx.Update(&mbDst)
546 x.Checkf(ctx, err, "updating destination mailbox with uidnext and modseq")
550 x.Checkf(ctx, err, "saving junk filter")
555 return newIDs, changes
558func isText(p message.Part) bool {
559 return p.MediaType == "" && p.MediaSubType == "" || p.MediaType == "TEXT" && p.MediaSubType == "PLAIN"
562func isHTML(p message.Part) bool {
563 return p.MediaType == "" && p.MediaSubType == "" || p.MediaType == "TEXT" && p.MediaSubType == "HTML"
566func isAlternative(p message.Part) bool {
567 return p.MediaType == "MULTIPART" && p.MediaSubType == "ALTERNATIVE"
570func readPart(p message.Part, maxSize int64) (string, error) {
571 buf, err := io.ReadAll(io.LimitReader(p.ReaderUTF8OrBinary(), maxSize))
573 return "", fmt.Errorf("reading part contents: %v", err)
575 return string(buf), nil
578// ReadableParts returns the contents of the first text and/or html parts,
579// descending into multiparts, truncated to maxSize bytes if longer.
580func ReadableParts(p message.Part, maxSize int64) (text string, html string, found bool, err error) {
581 // todo: may want to merge this logic with webmail's message parsing.
583 // For non-multipart messages, top-level part.
585 data, err := readPart(p, maxSize)
586 return data, "", true, err
587 } else if isHTML(p) {
588 data, err := readPart(p, maxSize)
589 return "", data, true, err
592 // Look in sub-parts. Stop when we have a readable part, don't continue with other
593 // subparts unless we have a multipart/alternative.
594 // todo: we may have to look at disposition "inline".
595 var haveText, haveHTML bool
596 for _, pp := range p.Parts {
599 text, err = readPart(pp, maxSize)
600 if !isAlternative(p) {
603 } else if isHTML(pp) {
605 html, err = readPart(pp, maxSize)
606 if !isAlternative(p) {
611 if haveText || haveHTML {
612 return text, html, true, err
615 // Descend into the subparts.
616 for _, pp := range p.Parts {
617 text, html, found, err = ReadableParts(pp, maxSize)