3// todo: if fetch fails part-way through the command, we wouldn't be storing the messages that were parsed. should we try harder to get parsed form of messages stored in db?
15 "golang.org/x/exp/maps"
17 "github.com/mjl-/bstore"
19 "github.com/mjl-/mox/message"
20 "github.com/mjl-/mox/mox-"
21 "github.com/mjl-/mox/moxio"
22 "github.com/mjl-/mox/store"
25// functions to handle fetch attribute requests are defined on fetchCmd.
30 tx *bstore.Tx // Writable tx, for storing message when first parsed as mime parts.
31 changes []store.Change // For updated Seen flag.
34 needModseq bool // Whether untagged responses needs modseq.
35 expungeIssued bool // Set if a message cannot be read. Can happen for expunged messages.
36 modseq store.ModSeq // Initialized on first change, for marking messages as seen.
37 isUID bool // If this is a UID FETCH command.
38 hasChangedSince bool // Whether CHANGEDSINCE was set. Enables MODSEQ in response.
39 deltaCounts store.MailboxCounts // By marking \Seen, the number of unread/unseen messages will go down. We update counts at the end.
41 // Loaded when first needed, closed when message was processed.
42 m *store.Message // Message currently being processed.
47// error when processing an attribute. we typically just don't respond with requested attributes that encounter a failure.
48type attrError struct{ err error }
50func (e attrError) Error() string {
54// raise error processing an attribute.
55func (cmd *fetchCmd) xerrorf(format string, args ...any) {
56 panic(attrError{fmt.Errorf(format, args...)})
59func (cmd *fetchCmd) xcheckf(err error, format string, args ...any) {
61 msg := fmt.Sprintf(format, args...)
62 cmd.xerrorf("%s: %w", msg, err)
66// Fetch returns information about messages, be it email envelopes, headers,
67// bodies, full messages, flags.
70func (c *conn) cmdxFetch(isUID bool, tag, cmdstr string, p *parser) {
79 atts := p.xfetchAtts(isUID)
80 var changedSince int64
81 var haveChangedSince bool
87 seen := map[string]bool{}
90 if isUID && p.conn.enabled[capQresync] {
92 w = p.xtakelist("CHANGEDSINCE", "VANISHED")
94 w = p.xtakelist("CHANGEDSINCE")
97 xsyntaxErrorf("duplicate fetch modifier %s", w)
103 changedSince = p.xnumber64()
104 // workaround: ios mail (16.5.1) was seen sending changedSince 0 on an existing account that got condstore enabled.
105 if changedSince == 0 && mox.Pedantic {
107 xsyntaxErrorf("changedsince modseq must be > 0")
110 p.conn.xensureCondstore(nil)
111 haveChangedSince = true
122 if vanished && !haveChangedSince {
123 xsyntaxErrorf("VANISHED can only be used with CHANGEDSINCE")
128 // We don't use c.account.WithRLock because we write to the client while reading messages.
129 // We get the rlock, then we check the mailbox, release the lock and read the messages.
130 // The db transaction still locks out any changes to the database...
132 runlock := c.account.RUnlock
133 // Note: we call runlock in a closure because we replace it below.
138 var vanishedUIDs []store.UID
139 cmd := &fetchCmd{conn: c, mailboxID: c.mailboxID, isUID: isUID, hasChangedSince: haveChangedSince}
140 c.xdbwrite(func(tx *bstore.Tx) {
143 // Ensure the mailbox still exists.
144 mb := c.xmailboxID(tx, c.mailboxID)
148 // With changedSince, the client is likely asking for a small set of changes. Use a
149 // database query to trim down the uids we need to look at.
151 if changedSince > 0 {
152 q := bstore.QueryTx[store.Message](tx)
153 q.FilterNonzero(store.Message{MailboxID: c.mailboxID})
154 q.FilterGreater("ModSeq", store.ModSeqFromClient(changedSince))
156 q.FilterEqual("Expunged", false)
158 err := q.ForEach(func(m store.Message) error {
160 vanishedUIDs = append(vanishedUIDs, m.UID)
162 if nums.containsUID(m.UID, c.uids, c.searchResult) {
163 uids = append(uids, m.UID)
166 seq := c.sequence(m.UID)
167 if seq > 0 && nums.containsSeq(seq, c.uids, c.searchResult) {
168 uids = append(uids, m.UID)
173 xcheckf(err, "looking up messages with changedsince")
175 uids = c.xnumSetUIDs(isUID, nums)
180 delModSeq, err := c.account.HighestDeletedModSeq(tx)
181 xcheckf(err, "looking up highest deleted modseq")
182 if changedSince < delModSeq.Client() {
183 // First sort the uids we already found, for fast lookup.
184 sort.Slice(vanishedUIDs, func(i, j int) bool {
185 return vanishedUIDs[i] < vanishedUIDs[j]
188 // We'll be gathering any more vanished uids in more.
189 more := map[store.UID]struct{}{}
190 checkVanished := func(uid store.UID) {
191 if uidSearch(c.uids, uid) <= 0 && uidSearch(vanishedUIDs, uid) <= 0 {
192 more[uid] = struct{}{}
195 // Now look through the requested uids. We may have a searchResult, handle it
196 // separately from a numset with potential stars, over which we can more easily
198 if nums.searchResult {
199 for _, uid := range c.searchResult {
203 iter := nums.interpretStar(c.uids).newIter()
205 num, ok := iter.Next()
209 checkVanished(store.UID(num))
212 vanishedUIDs = append(vanishedUIDs, maps.Keys(more)...)
216 // Release the account lock.
218 runlock = func() {} // Prevent defer from unlocking again.
221 if len(vanishedUIDs) > 0 {
222 // Mention all vanished UIDs in compact numset form.
224 sort.Slice(vanishedUIDs, func(i, j int) bool {
225 return vanishedUIDs[i] < vanishedUIDs[j]
227 // No hard limit on response sizes, but clients are recommended to not send more
228 // than 8k. We send a more conservative max 4k.
229 for _, s := range compactUIDSet(vanishedUIDs).Strings(4*1024 - 32) {
230 c.bwritelinef("* VANISHED (EARLIER) %s", s)
234 for _, uid := range uids {
236 cmd.conn.log.Debug("processing uid", slog.Any("uid", uid))
240 var zeromc store.MailboxCounts
241 if cmd.deltaCounts != zeromc {
242 mb.Add(cmd.deltaCounts) // Unseen/Unread will be <= 0.
243 err := tx.Update(&mb)
244 xcheckf(err, "updating mailbox counts")
245 cmd.changes = append(cmd.changes, mb.ChangeCounts())
246 // No need to update account total message size.
250 if len(cmd.changes) > 0 {
251 // Broadcast seen updates to other connections.
252 c.broadcast(cmd.changes)
255 if cmd.expungeIssued {
257 c.writeresultf("%s NO [EXPUNGEISSUED] at least one message was expunged", tag)
263func (cmd *fetchCmd) xmodseq() store.ModSeq {
266 cmd.modseq, err = cmd.conn.account.NextModSeq(cmd.tx)
267 cmd.xcheckf(err, "assigning next modseq")
272func (cmd *fetchCmd) xensureMessage() *store.Message {
277 q := bstore.QueryTx[store.Message](cmd.tx)
278 q.FilterNonzero(store.Message{MailboxID: cmd.mailboxID, UID: cmd.uid})
279 q.FilterEqual("Expunged", false)
281 cmd.xcheckf(err, "get message for uid %d", cmd.uid)
286func (cmd *fetchCmd) xensureParsed() (*store.MsgReader, *message.Part) {
288 return cmd.msgr, cmd.part
291 m := cmd.xensureMessage()
293 cmd.msgr = cmd.conn.account.MessageReader(*m)
296 err := cmd.msgr.Close()
297 cmd.conn.xsanity(err, "closing messagereader")
302 p, err := m.LoadPart(cmd.msgr)
303 xcheckf(err, "load parsed message")
305 return cmd.msgr, cmd.part
308func (cmd *fetchCmd) process(atts []fetchAtt) {
313 err := cmd.msgr.Close()
314 cmd.conn.xsanity(err, "closing messagereader")
322 err, ok := x.(attrError)
326 if errors.Is(err, bstore.ErrAbsent) {
327 cmd.expungeIssued = true
330 cmd.conn.log.Infox("processing fetch attribute", err, slog.Any("uid", cmd.uid))
331 xuserErrorf("processing fetch attribute: %v", err)
334 data := listspace{bare("UID"), number(cmd.uid)}
337 cmd.needFlags = false
338 cmd.needModseq = false
340 for _, a := range atts {
341 data = append(data, cmd.xprocessAtt(a)...)
345 m := cmd.xensureMessage()
346 cmd.deltaCounts.Sub(m.MailboxCounts())
349 cmd.deltaCounts.Add(m.MailboxCounts())
350 m.ModSeq = cmd.xmodseq()
351 err := cmd.tx.Update(m)
352 xcheckf(err, "marking message as seen")
353 // No need to update account total message size.
355 cmd.changes = append(cmd.changes, m.ChangeFlags(origFlags))
359 m := cmd.xensureMessage()
360 data = append(data, bare("FLAGS"), flaglist(m.Flags, m.Keywords))
363 // The wording around when to include the MODSEQ attribute is hard to follow and is
364 // specified and refined in several places.
366 // An additional rule applies to "QRESYNC servers" (we'll assume it only applies
367 // when QRESYNC is enabled on a connection): setting the \Seen flag also triggers
371 // subsequent untagged fetch responses", then lists cases, but leaves out FETCH/UID
372 // FETCH. That appears intentional, it is not a list of examples, it is the full
373 // list, and the "all subsequent untagged fetch responses" doesn't mean "all", just
374 // those covering the listed cases. That makes sense, because otherwise all the
375 // other mentioning of cases elsewhere in the RFC would be too superfluous.
378 if cmd.needModseq || cmd.hasChangedSince || cmd.conn.enabled[capQresync] && (cmd.isUID || cmd.markSeen) {
379 m := cmd.xensureMessage()
380 data = append(data, bare("MODSEQ"), listspace{bare(fmt.Sprintf("%d", m.ModSeq.Client()))})
383 // Write errors are turned into panics because we write through c.
384 fmt.Fprintf(cmd.conn.bw, "* %d FETCH ", cmd.conn.xsequence(cmd.uid))
385 data.writeTo(cmd.conn, cmd.conn.bw)
386 cmd.conn.bw.Write([]byte("\r\n"))
389// result for one attribute. if processing fails, e.g. because data was requested
390// that doesn't exist and cannot be represented in imap, the attribute is simply
391// not returned to the user. in this case, the returned value is a nil list.
392func (cmd *fetchCmd) xprocessAtt(a fetchAtt) []token {
398 _, part := cmd.xensureParsed()
399 envelope := xenvelope(part)
400 return []token{bare("ENVELOPE"), envelope}
404 m := cmd.xensureMessage()
405 return []token{bare("INTERNALDATE"), dquote(m.Received.Format("_2-Jan-2006 15:04:05 -0700"))}
408 m := cmd.xensureMessage()
409 // For messages in storage from before we implemented this extension, we don't have
410 // a savedate, and we return nil. This is normally meant to be per mailbox, but
412 var savedate token = nilt
413 if m.SaveDate != nil {
414 savedate = dquote(m.SaveDate.Format("_2-Jan-2006 15:04:05 -0700"))
416 return []token{bare("SAVEDATE"), savedate}
418 case "BODYSTRUCTURE":
419 _, part := cmd.xensureParsed()
420 bs := xbodystructure(part, true)
421 return []token{bare("BODYSTRUCTURE"), bs}
424 respField, t := cmd.xbody(a)
428 return []token{bare(respField), t}
431 _, p := cmd.xensureParsed()
432 if len(a.sectionBinary) == 0 {
433 // Must return the size of the entire message but with decoded body.
434 // todo: make this less expensive and/or cache the result?
435 n, err := io.Copy(io.Discard, cmd.xbinaryMessageReader(p))
436 cmd.xcheckf(err, "reading message as binary for its size")
437 return []token{bare(cmd.sectionRespField(a)), number(uint32(n))}
439 p = cmd.xpartnumsDeref(a.sectionBinary, p)
440 if len(p.Parts) > 0 || p.Message != nil {
442 cmd.xerrorf("binary only allowed on leaf parts, not multipart/* or message/rfc822 or message/global")
444 return []token{bare(cmd.sectionRespField(a)), number(p.DecodedSize)}
447 respField, t := cmd.xbinary(a)
451 return []token{bare(respField), t}
454 m := cmd.xensureMessage()
455 return []token{bare("RFC822.SIZE"), number(m.Size)}
457 case "RFC822.HEADER":
461 section: §ionSpec{
462 msgtext: §ionMsgtext{s: "HEADER"},
465 respField, t := cmd.xbody(ba)
469 return []token{bare(a.field), t}
474 section: §ionSpec{},
476 respField, t := cmd.xbody(ba)
480 return []token{bare(a.field), t}
485 section: §ionSpec{
486 msgtext: §ionMsgtext{s: "TEXT"},
489 respField, t := cmd.xbody(ba)
493 return []token{bare(a.field), t}
499 cmd.needModseq = true
502 xserverErrorf("field %q not yet implemented", a.field)
508func xenvelope(p *message.Part) token {
509 var env message.Envelope
510 if p.Envelope != nil {
513 var date token = nilt
514 if !env.Date.IsZero() {
516 date = string0(env.Date.Format("Mon, 2 Jan 2006 15:04:05 -0700"))
518 var subject token = nilt
519 if env.Subject != "" {
520 subject = string0(env.Subject)
522 var inReplyTo token = nilt
523 if env.InReplyTo != "" {
524 inReplyTo = string0(env.InReplyTo)
526 var messageID token = nilt
527 if env.MessageID != "" {
528 messageID = string0(env.MessageID)
531 addresses := func(l []message.Address) token {
536 for _, a := range l {
537 var name token = nilt
539 name = string0(a.Name)
541 user := string0(a.User)
542 var host token = nilt
544 host = string0(a.Host)
546 r = append(r, listspace{name, nilt, user, host})
553 if len(sender) == 0 {
556 replyTo := env.ReplyTo
557 if len(replyTo) == 0 {
575func (cmd *fetchCmd) peekOrSeen(peek bool) {
576 if cmd.conn.readonly || peek {
579 m := cmd.xensureMessage()
586// reader that returns the message, but with header Content-Transfer-Encoding left out.
587func (cmd *fetchCmd) xbinaryMessageReader(p *message.Part) io.Reader {
588 hr := cmd.xmodifiedHeader(p, []string{"Content-Transfer-Encoding"}, true)
589 return io.MultiReader(hr, p.Reader())
592// return header with only fields, or with everything except fields if "not" is set.
593func (cmd *fetchCmd) xmodifiedHeader(p *message.Part, fields []string, not bool) io.Reader {
594 h, err := io.ReadAll(p.HeaderReader())
595 cmd.xcheckf(err, "reading header")
597 matchesFields := func(line []byte) bool {
598 k := bytes.TrimRight(bytes.SplitN(line, []byte(":"), 2)[0], " \t")
599 for _, f := range fields {
600 if bytes.EqualFold(k, []byte(f)) {
608 hb := &bytes.Buffer{}
611 i := bytes.Index(line, []byte("\r\n"))
617 match = matchesFields(line) || match && (bytes.HasPrefix(line, []byte(" ")) || bytes.HasPrefix(line, []byte("\t")))
618 if match != not || len(line) == 2 {
625func (cmd *fetchCmd) xbinary(a fetchAtt) (string, token) {
626 _, part := cmd.xensureParsed()
628 cmd.peekOrSeen(a.peek)
629 if len(a.sectionBinary) == 0 {
630 r := cmd.xbinaryMessageReader(part)
631 if a.partial != nil {
632 r = cmd.xpartialReader(a.partial, r)
634 return cmd.sectionRespField(a), readerSyncliteral{r}
638 if len(a.sectionBinary) > 0 {
639 p = cmd.xpartnumsDeref(a.sectionBinary, p)
641 if len(p.Parts) != 0 || p.Message != nil {
643 cmd.xerrorf("binary only allowed on leaf parts, not multipart/* or message/rfc822 or message/global")
646 switch p.ContentTransferEncoding {
647 case "", "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE":
650 xusercodeErrorf("UNKNOWN-CTE", "unknown Content-Transfer-Encoding %q", p.ContentTransferEncoding)
654 if a.partial != nil {
655 r = cmd.xpartialReader(a.partial, r)
657 return cmd.sectionRespField(a), readerSyncliteral{r}
660func (cmd *fetchCmd) xpartialReader(partial *partial, r io.Reader) io.Reader {
661 n, err := io.Copy(io.Discard, io.LimitReader(r, int64(partial.offset)))
662 cmd.xcheckf(err, "skipping to offset for partial")
663 if n != int64(partial.offset) {
666 return io.LimitReader(r, int64(partial.count))
669func (cmd *fetchCmd) xbody(a fetchAtt) (string, token) {
670 msgr, part := cmd.xensureParsed()
672 if a.section == nil {
673 // Non-extensible form of BODYSTRUCTURE.
674 return a.field, xbodystructure(part, false)
677 cmd.peekOrSeen(a.peek)
679 respField := cmd.sectionRespField(a)
681 if a.section.msgtext == nil && a.section.part == nil {
682 m := cmd.xensureMessage()
685 if a.partial != nil {
686 offset = int64(a.partial.offset)
690 count = int64(a.partial.count)
691 if offset+count > m.Size {
692 count = m.Size - offset
695 return respField, readerSizeSyncliteral{&moxio.AtReader{R: msgr, Offset: offset}, count, false}
698 sr := cmd.xsection(a.section, part)
700 if a.partial != nil {
701 n, err := io.Copy(io.Discard, io.LimitReader(sr, int64(a.partial.offset)))
702 cmd.xcheckf(err, "skipping to offset for partial")
703 if n != int64(a.partial.offset) {
706 return respField, readerSyncliteral{io.LimitReader(sr, int64(a.partial.count))}
708 return respField, readerSyncliteral{sr}
711func (cmd *fetchCmd) xpartnumsDeref(nums []uint32, p *message.Part) *message.Part {
713 if (len(p.Parts) == 0 && p.Message == nil) && len(nums) == 1 && nums[0] == 1 {
718 for i, num := range nums {
719 index := int(num - 1)
720 if p.Message != nil {
721 err := p.SetMessageReaderAt()
722 cmd.xcheckf(err, "preparing submessage")
723 return cmd.xpartnumsDeref(nums[i:], p.Message)
725 if index < 0 || index >= len(p.Parts) {
726 cmd.xerrorf("requested part does not exist")
733func (cmd *fetchCmd) xsection(section *sectionSpec, p *message.Part) io.Reader {
734 if section.part == nil {
735 return cmd.xsectionMsgtext(section.msgtext, p)
738 p = cmd.xpartnumsDeref(section.part.part, p)
740 if section.part.text == nil {
745 if p.Message != nil {
746 err := p.SetMessageReaderAt()
747 cmd.xcheckf(err, "preparing submessage")
751 if !section.part.text.mime {
752 return cmd.xsectionMsgtext(section.part.text.msgtext, p)
756 h, err := io.ReadAll(p.HeaderReader())
757 cmd.xcheckf(err, "reading header")
759 matchesFields := func(line []byte) bool {
760 k := textproto.CanonicalMIMEHeaderKey(string(bytes.TrimRight(bytes.SplitN(line, []byte(":"), 2)[0], " \t")))
762 return (p.Envelope != nil && k == "Mime-Version") || strings.HasPrefix(k, "Content-")
766 hb := &bytes.Buffer{}
769 i := bytes.Index(line, []byte("\r\n"))
775 match = matchesFields(line) || match && (bytes.HasPrefix(line, []byte(" ")) || bytes.HasPrefix(line, []byte("\t")))
776 if match || len(line) == 2 {
783func (cmd *fetchCmd) xsectionMsgtext(smt *sectionMsgtext, p *message.Part) io.Reader {
784 if smt.s == "HEADER" {
785 return p.HeaderReader()
789 case "HEADER.FIELDS":
790 return cmd.xmodifiedHeader(p, smt.headers, false)
792 case "HEADER.FIELDS.NOT":
793 return cmd.xmodifiedHeader(p, smt.headers, true)
796 // It appears imap clients expect to get the body of the message, not a "text body"
800 panic(serverError{fmt.Errorf("missing case")})
803func (cmd *fetchCmd) sectionRespField(a fetchAtt) string {
805 if len(a.sectionBinary) > 0 {
806 s += fmt.Sprintf("%d", a.sectionBinary[0])
807 for _, v := range a.sectionBinary[1:] {
808 s += "." + fmt.Sprintf("%d", v)
810 } else if a.section != nil {
811 if a.section.part != nil {
813 s += fmt.Sprintf("%d", p.part[0])
814 for _, v := range p.part[1:] {
815 s += "." + fmt.Sprintf("%d", v)
821 s += "." + cmd.sectionMsgtextName(p.text.msgtext)
824 } else if a.section.msgtext != nil {
825 s += cmd.sectionMsgtextName(a.section.msgtext)
830 if a.field != "BINARY" && a.partial != nil {
831 s += fmt.Sprintf("<%d>", a.partial.offset)
836func (cmd *fetchCmd) sectionMsgtextName(smt *sectionMsgtext) string {
838 if strings.HasPrefix(smt.s, "HEADER.FIELDS") {
840 for _, h := range smt.headers {
841 l = append(l, astring(h))
843 s += " " + l.pack(cmd.conn)
848func bodyFldParams(params map[string]string) token {
849 if len(params) == 0 {
852 // Ensure same ordering, easier for testing.
854 for k := range params {
855 keys = append(keys, k)
858 l := make(listspace, 2*len(keys))
860 for _, k := range keys {
861 l[i] = string0(strings.ToUpper(k))
862 l[i+1] = string0(params[k])
868func bodyFldEnc(s string) token {
869 up := strings.ToUpper(s)
871 case "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE":
877// xbodystructure returns a "body".
878// calls itself for multipart messages and message/{rfc822,global}.
879func xbodystructure(p *message.Part, extensible bool) token {
880 if p.MediaType == "MULTIPART" {
883 for i := range p.Parts {
884 bodies = append(bodies, xbodystructure(&p.Parts[i], extensible))
886 r := listspace{bodies, string0(p.MediaSubType)}
888 if len(p.ContentTypeParams) == 0 {
891 params := make(listspace, 0, 2*len(p.ContentTypeParams))
892 for k, v := range p.ContentTypeParams {
893 params = append(params, string0(k), string0(v))
895 r = append(r, params)
903 if p.MediaType == "TEXT" {
909 nilOrString(p.ContentID),
910 nilOrString(p.ContentDescription),
911 bodyFldEnc(p.ContentTransferEncoding),
912 number(p.EndOffset - p.BodyOffset),
913 number(p.RawLineCount),
915 } else if p.MediaType == "MESSAGE" && (p.MediaSubType == "RFC822" || p.MediaSubType == "GLOBAL") {
917 // note: we don't have to prepare p.Message for reading, because we aren't going to read from it.
922 nilOrString(p.ContentID),
923 nilOrString(p.ContentDescription),
924 bodyFldEnc(p.ContentTransferEncoding),
925 number(p.EndOffset - p.BodyOffset),
926 xenvelope(p.Message),
927 xbodystructure(p.Message, extensible),
928 number(p.RawLineCount), // todo: or mp.RawLineCount?
933 case "APPLICATION", "AUDIO", "IMAGE", "FONT", "MESSAGE", "MODEL", "VIDEO":
934 media = dquote(p.MediaType)
936 media = string0(p.MediaType)
943 nilOrString(p.ContentID),
944 nilOrString(p.ContentDescription),
945 bodyFldEnc(p.ContentTransferEncoding),
946 number(p.EndOffset - p.BodyOffset),
949 // todo: if "extensible", we could add the value of the "content-md5" header. we don't have it in our parsed data structure, so we don't add it. likely no one would use it, also not any of the other optional fields.
../rfc/9051:6366