1package imapserver
2
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?
4
5import (
6 "bytes"
7 "errors"
8 "fmt"
9 "io"
10 "log/slog"
11 "net/textproto"
12 "sort"
13 "strings"
14
15 "golang.org/x/exp/maps"
16
17 "github.com/mjl-/bstore"
18
19 "github.com/mjl-/mox/message"
20 "github.com/mjl-/mox/mox-"
21 "github.com/mjl-/mox/moxio"
22 "github.com/mjl-/mox/store"
23)
24
25// functions to handle fetch attribute requests are defined on fetchCmd.
26type fetchCmd struct {
27 conn *conn
28 mailboxID int64
29 uid store.UID
30 tx *bstore.Tx // Writable tx, for storing message when first parsed as mime parts.
31 changes []store.Change // For updated Seen flag.
32 markSeen bool
33 needFlags bool
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.
40
41 // Loaded when first needed, closed when message was processed.
42 m *store.Message // Message currently being processed.
43 msgr *store.MsgReader
44 part *message.Part
45}
46
47// error when processing an attribute. we typically just don't respond with requested attributes that encounter a failure.
48type attrError struct{ err error }
49
50func (e attrError) Error() string {
51 return e.err.Error()
52}
53
54// raise error processing an attribute.
55func (cmd *fetchCmd) xerrorf(format string, args ...any) {
56 panic(attrError{fmt.Errorf(format, args...)})
57}
58
59func (cmd *fetchCmd) xcheckf(err error, format string, args ...any) {
60 if err != nil {
61 msg := fmt.Sprintf(format, args...)
62 cmd.xerrorf("%s: %w", msg, err)
63 }
64}
65
66// Fetch returns information about messages, be it email envelopes, headers,
67// bodies, full messages, flags.
68//
69// State: Selected
70func (c *conn) cmdxFetch(isUID bool, tag, cmdstr string, p *parser) {
71 // Command: ../rfc/9051:4330 ../rfc/3501:2992 ../rfc/7162:864
72 // Examples: ../rfc/9051:4463 ../rfc/9051:4520 ../rfc/7162:880
73 // Response syntax: ../rfc/9051:6742 ../rfc/3501:4864 ../rfc/7162:2490
74
75 // Request syntax: ../rfc/9051:6553 ../rfc/3501:4748 ../rfc/4466:535 ../rfc/7162:2475
76 p.xspace()
77 nums := p.xnumSet()
78 p.xspace()
79 atts := p.xfetchAtts(isUID)
80 var changedSince int64
81 var haveChangedSince bool
82 var vanished bool
83 if p.space() {
84 // ../rfc/4466:542
85 // ../rfc/7162:2479
86 p.xtake("(")
87 seen := map[string]bool{}
88 for {
89 var w string
90 if isUID && p.conn.enabled[capQresync] {
91 // Vanished only valid for uid fetch, and only for qresync. ../rfc/7162:1693
92 w = p.xtakelist("CHANGEDSINCE", "VANISHED")
93 } else {
94 w = p.xtakelist("CHANGEDSINCE")
95 }
96 if seen[w] {
97 xsyntaxErrorf("duplicate fetch modifier %s", w)
98 }
99 seen[w] = true
100 switch w {
101 case "CHANGEDSINCE":
102 p.xspace()
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 {
106 // ../rfc/7162:2551
107 xsyntaxErrorf("changedsince modseq must be > 0")
108 }
109 // CHANGEDSINCE is a CONDSTORE-enabling parameter. ../rfc/7162:380
110 p.conn.xensureCondstore(nil)
111 haveChangedSince = true
112 case "VANISHED":
113 vanished = true
114 }
115 if p.take(")") {
116 break
117 }
118 p.xspace()
119 }
120
121 // ../rfc/7162:1701
122 if vanished && !haveChangedSince {
123 xsyntaxErrorf("VANISHED can only be used with CHANGEDSINCE")
124 }
125 }
126 p.xempty()
127
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...
131 c.account.RLock()
132 runlock := c.account.RUnlock
133 // Note: we call runlock in a closure because we replace it below.
134 defer func() {
135 runlock()
136 }()
137
138 var vanishedUIDs []store.UID
139 cmd := &fetchCmd{conn: c, mailboxID: c.mailboxID, isUID: isUID, hasChangedSince: haveChangedSince}
140 c.xdbwrite(func(tx *bstore.Tx) {
141 cmd.tx = tx
142
143 // Ensure the mailbox still exists.
144 mb := c.xmailboxID(tx, c.mailboxID)
145
146 var uids []store.UID
147
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.
150 // ../rfc/7162:871
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))
155 if !vanished {
156 q.FilterEqual("Expunged", false)
157 }
158 err := q.ForEach(func(m store.Message) error {
159 if m.Expunged {
160 vanishedUIDs = append(vanishedUIDs, m.UID)
161 } else if isUID {
162 if nums.containsUID(m.UID, c.uids, c.searchResult) {
163 uids = append(uids, m.UID)
164 }
165 } else {
166 seq := c.sequence(m.UID)
167 if seq > 0 && nums.containsSeq(seq, c.uids, c.searchResult) {
168 uids = append(uids, m.UID)
169 }
170 }
171 return nil
172 })
173 xcheckf(err, "looking up messages with changedsince")
174 } else {
175 uids = c.xnumSetUIDs(isUID, nums)
176 }
177
178 // Send vanished for all missing requested UIDs. ../rfc/7162:1718
179 if vanished {
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]
186 })
187
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{}{}
193 }
194 }
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
197 // iterate.
198 if nums.searchResult {
199 for _, uid := range c.searchResult {
200 checkVanished(uid)
201 }
202 } else {
203 iter := nums.interpretStar(c.uids).newIter()
204 for {
205 num, ok := iter.Next()
206 if !ok {
207 break
208 }
209 checkVanished(store.UID(num))
210 }
211 }
212 vanishedUIDs = append(vanishedUIDs, maps.Keys(more)...)
213 }
214 }
215
216 // Release the account lock.
217 runlock()
218 runlock = func() {} // Prevent defer from unlocking again.
219
220 // First report all vanished UIDs. ../rfc/7162:1714
221 if len(vanishedUIDs) > 0 {
222 // Mention all vanished UIDs in compact numset form.
223 // ../rfc/7162:1985
224 sort.Slice(vanishedUIDs, func(i, j int) bool {
225 return vanishedUIDs[i] < vanishedUIDs[j]
226 })
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)
231 }
232 }
233
234 for _, uid := range uids {
235 cmd.uid = uid
236 cmd.conn.log.Debug("processing uid", slog.Any("uid", uid))
237 cmd.process(atts)
238 }
239
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.
247 }
248 })
249
250 if len(cmd.changes) > 0 {
251 // Broadcast seen updates to other connections.
252 c.broadcast(cmd.changes)
253 }
254
255 if cmd.expungeIssued {
256 // ../rfc/2180:343
257 c.writeresultf("%s NO [EXPUNGEISSUED] at least one message was expunged", tag)
258 } else {
259 c.ok(tag, cmdstr)
260 }
261}
262
263func (cmd *fetchCmd) xmodseq() store.ModSeq {
264 if cmd.modseq == 0 {
265 var err error
266 cmd.modseq, err = cmd.conn.account.NextModSeq(cmd.tx)
267 cmd.xcheckf(err, "assigning next modseq")
268 }
269 return cmd.modseq
270}
271
272func (cmd *fetchCmd) xensureMessage() *store.Message {
273 if cmd.m != nil {
274 return cmd.m
275 }
276
277 q := bstore.QueryTx[store.Message](cmd.tx)
278 q.FilterNonzero(store.Message{MailboxID: cmd.mailboxID, UID: cmd.uid})
279 q.FilterEqual("Expunged", false)
280 m, err := q.Get()
281 cmd.xcheckf(err, "get message for uid %d", cmd.uid)
282 cmd.m = &m
283 return cmd.m
284}
285
286func (cmd *fetchCmd) xensureParsed() (*store.MsgReader, *message.Part) {
287 if cmd.msgr != nil {
288 return cmd.msgr, cmd.part
289 }
290
291 m := cmd.xensureMessage()
292
293 cmd.msgr = cmd.conn.account.MessageReader(*m)
294 defer func() {
295 if cmd.part == nil {
296 err := cmd.msgr.Close()
297 cmd.conn.xsanity(err, "closing messagereader")
298 cmd.msgr = nil
299 }
300 }()
301
302 p, err := m.LoadPart(cmd.msgr)
303 xcheckf(err, "load parsed message")
304 cmd.part = &p
305 return cmd.msgr, cmd.part
306}
307
308func (cmd *fetchCmd) process(atts []fetchAtt) {
309 defer func() {
310 cmd.m = nil
311 cmd.part = nil
312 if cmd.msgr != nil {
313 err := cmd.msgr.Close()
314 cmd.conn.xsanity(err, "closing messagereader")
315 cmd.msgr = nil
316 }
317
318 x := recover()
319 if x == nil {
320 return
321 }
322 err, ok := x.(attrError)
323 if !ok {
324 panic(x)
325 }
326 if errors.Is(err, bstore.ErrAbsent) {
327 cmd.expungeIssued = true
328 return
329 }
330 cmd.conn.log.Infox("processing fetch attribute", err, slog.Any("uid", cmd.uid))
331 xuserErrorf("processing fetch attribute: %v", err)
332 }()
333
334 data := listspace{bare("UID"), number(cmd.uid)}
335
336 cmd.markSeen = false
337 cmd.needFlags = false
338 cmd.needModseq = false
339
340 for _, a := range atts {
341 data = append(data, cmd.xprocessAtt(a)...)
342 }
343
344 if cmd.markSeen {
345 m := cmd.xensureMessage()
346 cmd.deltaCounts.Sub(m.MailboxCounts())
347 origFlags := m.Flags
348 m.Seen = true
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.
354
355 cmd.changes = append(cmd.changes, m.ChangeFlags(origFlags))
356 }
357
358 if cmd.needFlags {
359 m := cmd.xensureMessage()
360 data = append(data, bare("FLAGS"), flaglist(m.Flags, m.Keywords))
361 }
362
363 // The wording around when to include the MODSEQ attribute is hard to follow and is
364 // specified and refined in several places.
365 //
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
368 // sending MODSEQ, and so does a UID FETCH command. ../rfc/7162:1421
369 //
370 // For example, ../rfc/7162:389 says the server must include modseq in "all
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.
376 //
377 // ../rfc/7162:877 ../rfc/7162:388 ../rfc/7162:909 ../rfc/7162:1426
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()))})
381 }
382
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"))
387}
388
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 {
393 switch a.field {
394 case "UID":
395 // Always present.
396 return nil
397 case "ENVELOPE":
398 _, part := cmd.xensureParsed()
399 envelope := xenvelope(part)
400 return []token{bare("ENVELOPE"), envelope}
401
402 case "INTERNALDATE":
403 // ../rfc/9051:6753 ../rfc/9051:6502
404 m := cmd.xensureMessage()
405 return []token{bare("INTERNALDATE"), dquote(m.Received.Format("_2-Jan-2006 15:04:05 -0700"))}
406
407 case "BODYSTRUCTURE":
408 _, part := cmd.xensureParsed()
409 bs := xbodystructure(part, true)
410 return []token{bare("BODYSTRUCTURE"), bs}
411
412 case "BODY":
413 respField, t := cmd.xbody(a)
414 if respField == "" {
415 return nil
416 }
417 return []token{bare(respField), t}
418
419 case "BINARY.SIZE":
420 _, p := cmd.xensureParsed()
421 if len(a.sectionBinary) == 0 {
422 // Must return the size of the entire message but with decoded body.
423 // todo: make this less expensive and/or cache the result?
424 n, err := io.Copy(io.Discard, cmd.xbinaryMessageReader(p))
425 cmd.xcheckf(err, "reading message as binary for its size")
426 return []token{bare(cmd.sectionRespField(a)), number(uint32(n))}
427 }
428 p = cmd.xpartnumsDeref(a.sectionBinary, p)
429 if len(p.Parts) > 0 || p.Message != nil {
430 // ../rfc/9051:4385
431 cmd.xerrorf("binary only allowed on leaf parts, not multipart/* or message/rfc822 or message/global")
432 }
433 return []token{bare(cmd.sectionRespField(a)), number(p.DecodedSize)}
434
435 case "BINARY":
436 respField, t := cmd.xbinary(a)
437 if respField == "" {
438 return nil
439 }
440 return []token{bare(respField), t}
441
442 case "RFC822.SIZE":
443 m := cmd.xensureMessage()
444 return []token{bare("RFC822.SIZE"), number(m.Size)}
445
446 case "RFC822.HEADER":
447 ba := fetchAtt{
448 field: "BODY",
449 peek: true,
450 section: &sectionSpec{
451 msgtext: &sectionMsgtext{s: "HEADER"},
452 },
453 }
454 respField, t := cmd.xbody(ba)
455 if respField == "" {
456 return nil
457 }
458 return []token{bare(a.field), t}
459
460 case "RFC822":
461 ba := fetchAtt{
462 field: "BODY",
463 section: &sectionSpec{},
464 }
465 respField, t := cmd.xbody(ba)
466 if respField == "" {
467 return nil
468 }
469 return []token{bare(a.field), t}
470
471 case "RFC822.TEXT":
472 ba := fetchAtt{
473 field: "BODY",
474 section: &sectionSpec{
475 msgtext: &sectionMsgtext{s: "TEXT"},
476 },
477 }
478 respField, t := cmd.xbody(ba)
479 if respField == "" {
480 return nil
481 }
482 return []token{bare(a.field), t}
483
484 case "FLAGS":
485 cmd.needFlags = true
486
487 case "MODSEQ":
488 cmd.needModseq = true
489
490 default:
491 xserverErrorf("field %q not yet implemented", a.field)
492 }
493 return nil
494}
495
496// ../rfc/9051:6522
497func xenvelope(p *message.Part) token {
498 var env message.Envelope
499 if p.Envelope != nil {
500 env = *p.Envelope
501 }
502 var date token = nilt
503 if !env.Date.IsZero() {
504 // ../rfc/5322:791
505 date = string0(env.Date.Format("Mon, 2 Jan 2006 15:04:05 -0700"))
506 }
507 var subject token = nilt
508 if env.Subject != "" {
509 subject = string0(env.Subject)
510 }
511 var inReplyTo token = nilt
512 if env.InReplyTo != "" {
513 inReplyTo = string0(env.InReplyTo)
514 }
515 var messageID token = nilt
516 if env.MessageID != "" {
517 messageID = string0(env.MessageID)
518 }
519
520 addresses := func(l []message.Address) token {
521 if len(l) == 0 {
522 return nilt
523 }
524 r := listspace{}
525 for _, a := range l {
526 var name token = nilt
527 if a.Name != "" {
528 name = string0(a.Name)
529 }
530 user := string0(a.User)
531 var host token = nilt
532 if a.Host != "" {
533 host = string0(a.Host)
534 }
535 r = append(r, listspace{name, nilt, user, host})
536 }
537 return r
538 }
539
540 // Empty sender or reply-to result in fall-back to from. ../rfc/9051:6140
541 sender := env.Sender
542 if len(sender) == 0 {
543 sender = env.From
544 }
545 replyTo := env.ReplyTo
546 if len(replyTo) == 0 {
547 replyTo = env.From
548 }
549
550 return listspace{
551 date,
552 subject,
553 addresses(env.From),
554 addresses(sender),
555 addresses(replyTo),
556 addresses(env.To),
557 addresses(env.CC),
558 addresses(env.BCC),
559 inReplyTo,
560 messageID,
561 }
562}
563
564func (cmd *fetchCmd) peekOrSeen(peek bool) {
565 if cmd.conn.readonly || peek {
566 return
567 }
568 m := cmd.xensureMessage()
569 if !m.Seen {
570 cmd.markSeen = true
571 cmd.needFlags = true
572 }
573}
574
575// reader that returns the message, but with header Content-Transfer-Encoding left out.
576func (cmd *fetchCmd) xbinaryMessageReader(p *message.Part) io.Reader {
577 hr := cmd.xmodifiedHeader(p, []string{"Content-Transfer-Encoding"}, true)
578 return io.MultiReader(hr, p.Reader())
579}
580
581// return header with only fields, or with everything except fields if "not" is set.
582func (cmd *fetchCmd) xmodifiedHeader(p *message.Part, fields []string, not bool) io.Reader {
583 h, err := io.ReadAll(p.HeaderReader())
584 cmd.xcheckf(err, "reading header")
585
586 matchesFields := func(line []byte) bool {
587 k := bytes.TrimRight(bytes.SplitN(line, []byte(":"), 2)[0], " \t")
588 for _, f := range fields {
589 if bytes.EqualFold(k, []byte(f)) {
590 return true
591 }
592 }
593 return false
594 }
595
596 var match bool
597 hb := &bytes.Buffer{}
598 for len(h) > 0 {
599 line := h
600 i := bytes.Index(line, []byte("\r\n"))
601 if i >= 0 {
602 line = line[:i+2]
603 }
604 h = h[len(line):]
605
606 match = matchesFields(line) || match && (bytes.HasPrefix(line, []byte(" ")) || bytes.HasPrefix(line, []byte("\t")))
607 if match != not || len(line) == 2 {
608 hb.Write(line)
609 }
610 }
611 return hb
612}
613
614func (cmd *fetchCmd) xbinary(a fetchAtt) (string, token) {
615 _, part := cmd.xensureParsed()
616
617 cmd.peekOrSeen(a.peek)
618 if len(a.sectionBinary) == 0 {
619 r := cmd.xbinaryMessageReader(part)
620 if a.partial != nil {
621 r = cmd.xpartialReader(a.partial, r)
622 }
623 return cmd.sectionRespField(a), readerSyncliteral{r}
624 }
625
626 p := part
627 if len(a.sectionBinary) > 0 {
628 p = cmd.xpartnumsDeref(a.sectionBinary, p)
629 }
630 if len(p.Parts) != 0 || p.Message != nil {
631 // ../rfc/9051:4385
632 cmd.xerrorf("binary only allowed on leaf parts, not multipart/* or message/rfc822 or message/global")
633 }
634
635 switch p.ContentTransferEncoding {
636 case "", "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE":
637 default:
638 // ../rfc/9051:5913
639 xusercodeErrorf("UNKNOWN-CTE", "unknown Content-Transfer-Encoding %q", p.ContentTransferEncoding)
640 }
641
642 r := p.Reader()
643 if a.partial != nil {
644 r = cmd.xpartialReader(a.partial, r)
645 }
646 return cmd.sectionRespField(a), readerSyncliteral{r}
647}
648
649func (cmd *fetchCmd) xpartialReader(partial *partial, r io.Reader) io.Reader {
650 n, err := io.Copy(io.Discard, io.LimitReader(r, int64(partial.offset)))
651 cmd.xcheckf(err, "skipping to offset for partial")
652 if n != int64(partial.offset) {
653 return strings.NewReader("") // ../rfc/3501:3143 ../rfc/9051:4418
654 }
655 return io.LimitReader(r, int64(partial.count))
656}
657
658func (cmd *fetchCmd) xbody(a fetchAtt) (string, token) {
659 msgr, part := cmd.xensureParsed()
660
661 if a.section == nil {
662 // Non-extensible form of BODYSTRUCTURE.
663 return a.field, xbodystructure(part, false)
664 }
665
666 cmd.peekOrSeen(a.peek)
667
668 respField := cmd.sectionRespField(a)
669
670 if a.section.msgtext == nil && a.section.part == nil {
671 m := cmd.xensureMessage()
672 var offset int64
673 count := m.Size
674 if a.partial != nil {
675 offset = int64(a.partial.offset)
676 if offset > m.Size {
677 offset = m.Size
678 }
679 count = int64(a.partial.count)
680 if offset+count > m.Size {
681 count = m.Size - offset
682 }
683 }
684 return respField, readerSizeSyncliteral{&moxio.AtReader{R: msgr, Offset: offset}, count}
685 }
686
687 sr := cmd.xsection(a.section, part)
688
689 if a.partial != nil {
690 n, err := io.Copy(io.Discard, io.LimitReader(sr, int64(a.partial.offset)))
691 cmd.xcheckf(err, "skipping to offset for partial")
692 if n != int64(a.partial.offset) {
693 return respField, syncliteral("") // ../rfc/3501:3143 ../rfc/9051:4418
694 }
695 return respField, readerSyncliteral{io.LimitReader(sr, int64(a.partial.count))}
696 }
697 return respField, readerSyncliteral{sr}
698}
699
700func (cmd *fetchCmd) xpartnumsDeref(nums []uint32, p *message.Part) *message.Part {
701 // ../rfc/9051:4481
702 if (len(p.Parts) == 0 && p.Message == nil) && len(nums) == 1 && nums[0] == 1 {
703 return p
704 }
705
706 // ../rfc/9051:4485
707 for i, num := range nums {
708 index := int(num - 1)
709 if p.Message != nil {
710 err := p.SetMessageReaderAt()
711 cmd.xcheckf(err, "preparing submessage")
712 return cmd.xpartnumsDeref(nums[i:], p.Message)
713 }
714 if index < 0 || index >= len(p.Parts) {
715 cmd.xerrorf("requested part does not exist")
716 }
717 p = &p.Parts[index]
718 }
719 return p
720}
721
722func (cmd *fetchCmd) xsection(section *sectionSpec, p *message.Part) io.Reader {
723 if section.part == nil {
724 return cmd.xsectionMsgtext(section.msgtext, p)
725 }
726
727 p = cmd.xpartnumsDeref(section.part.part, p)
728
729 if section.part.text == nil {
730 return p.RawReader()
731 }
732
733 // ../rfc/9051:4535
734 if p.Message != nil {
735 err := p.SetMessageReaderAt()
736 cmd.xcheckf(err, "preparing submessage")
737 p = p.Message
738 }
739
740 if !section.part.text.mime {
741 return cmd.xsectionMsgtext(section.part.text.msgtext, p)
742 }
743
744 // MIME header, see ../rfc/9051:4534 ../rfc/2045:1645
745 h, err := io.ReadAll(p.HeaderReader())
746 cmd.xcheckf(err, "reading header")
747
748 matchesFields := func(line []byte) bool {
749 k := textproto.CanonicalMIMEHeaderKey(string(bytes.TrimRight(bytes.SplitN(line, []byte(":"), 2)[0], " \t")))
750 // Only add MIME-Version and additional CRLF for messages, not other parts. ../rfc/2045:1645 ../rfc/2045:1652
751 return (p.Envelope != nil && k == "Mime-Version") || strings.HasPrefix(k, "Content-")
752 }
753
754 var match bool
755 hb := &bytes.Buffer{}
756 for len(h) > 0 {
757 line := h
758 i := bytes.Index(line, []byte("\r\n"))
759 if i >= 0 {
760 line = line[:i+2]
761 }
762 h = h[len(line):]
763
764 match = matchesFields(line) || match && (bytes.HasPrefix(line, []byte(" ")) || bytes.HasPrefix(line, []byte("\t")))
765 if match || len(line) == 2 {
766 hb.Write(line)
767 }
768 }
769 return hb
770}
771
772func (cmd *fetchCmd) xsectionMsgtext(smt *sectionMsgtext, p *message.Part) io.Reader {
773 if smt.s == "HEADER" {
774 return p.HeaderReader()
775 }
776
777 switch smt.s {
778 case "HEADER.FIELDS":
779 return cmd.xmodifiedHeader(p, smt.headers, false)
780
781 case "HEADER.FIELDS.NOT":
782 return cmd.xmodifiedHeader(p, smt.headers, true)
783
784 case "TEXT":
785 // It appears imap clients expect to get the body of the message, not a "text body"
786 // which sounds like it means a text/* part of a message. ../rfc/9051:4517
787 return p.RawReader()
788 }
789 panic(serverError{fmt.Errorf("missing case")})
790}
791
792func (cmd *fetchCmd) sectionRespField(a fetchAtt) string {
793 s := a.field + "["
794 if len(a.sectionBinary) > 0 {
795 s += fmt.Sprintf("%d", a.sectionBinary[0])
796 for _, v := range a.sectionBinary[1:] {
797 s += "." + fmt.Sprintf("%d", v)
798 }
799 } else if a.section != nil {
800 if a.section.part != nil {
801 p := a.section.part
802 s += fmt.Sprintf("%d", p.part[0])
803 for _, v := range p.part[1:] {
804 s += "." + fmt.Sprintf("%d", v)
805 }
806 if p.text != nil {
807 if p.text.mime {
808 s += ".MIME"
809 } else {
810 s += "." + cmd.sectionMsgtextName(p.text.msgtext)
811 }
812 }
813 } else if a.section.msgtext != nil {
814 s += cmd.sectionMsgtextName(a.section.msgtext)
815 }
816 }
817 s += "]"
818 // binary does not have partial in field, unlike BODY ../rfc/9051:6757
819 if a.field != "BINARY" && a.partial != nil {
820 s += fmt.Sprintf("<%d>", a.partial.offset)
821 }
822 return s
823}
824
825func (cmd *fetchCmd) sectionMsgtextName(smt *sectionMsgtext) string {
826 s := smt.s
827 if strings.HasPrefix(smt.s, "HEADER.FIELDS") {
828 l := listspace{}
829 for _, h := range smt.headers {
830 l = append(l, astring(h))
831 }
832 s += " " + l.pack(cmd.conn)
833 }
834 return s
835}
836
837func bodyFldParams(params map[string]string) token {
838 if len(params) == 0 {
839 return nilt
840 }
841 // Ensure same ordering, easier for testing.
842 var keys []string
843 for k := range params {
844 keys = append(keys, k)
845 }
846 sort.Strings(keys)
847 l := make(listspace, 2*len(keys))
848 i := 0
849 for _, k := range keys {
850 l[i] = string0(strings.ToUpper(k))
851 l[i+1] = string0(params[k])
852 i += 2
853 }
854 return l
855}
856
857func bodyFldEnc(s string) token {
858 up := strings.ToUpper(s)
859 switch up {
860 case "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE":
861 return dquote(up)
862 }
863 return string0(s)
864}
865
866// xbodystructure returns a "body".
867// calls itself for multipart messages and message/{rfc822,global}.
868func xbodystructure(p *message.Part, extensible bool) token {
869 if p.MediaType == "MULTIPART" {
870 // Multipart, ../rfc/9051:6355 ../rfc/9051:6411
871 var bodies concat
872 for i := range p.Parts {
873 bodies = append(bodies, xbodystructure(&p.Parts[i], extensible))
874 }
875 r := listspace{bodies, string0(p.MediaSubType)}
876 if extensible {
877 if len(p.ContentTypeParams) == 0 {
878 r = append(r, nilt)
879 } else {
880 params := make(listspace, 0, 2*len(p.ContentTypeParams))
881 for k, v := range p.ContentTypeParams {
882 params = append(params, string0(k), string0(v))
883 }
884 r = append(r, params)
885 }
886 }
887 return r
888 }
889
890 // ../rfc/9051:6355
891 var r listspace
892 if p.MediaType == "TEXT" {
893 // ../rfc/9051:6404 ../rfc/9051:6418
894 r = listspace{
895 dquote("TEXT"), string0(p.MediaSubType), // ../rfc/9051:6739
896 // ../rfc/9051:6376
897 bodyFldParams(p.ContentTypeParams), // ../rfc/9051:6401
898 nilOrString(p.ContentID),
899 nilOrString(p.ContentDescription),
900 bodyFldEnc(p.ContentTransferEncoding),
901 number(p.EndOffset - p.BodyOffset),
902 number(p.RawLineCount),
903 }
904 } else if p.MediaType == "MESSAGE" && (p.MediaSubType == "RFC822" || p.MediaSubType == "GLOBAL") {
905 // ../rfc/9051:6415
906 // note: we don't have to prepare p.Message for reading, because we aren't going to read from it.
907 r = listspace{
908 dquote("MESSAGE"), dquote(p.MediaSubType), // ../rfc/9051:6732
909 // ../rfc/9051:6376
910 bodyFldParams(p.ContentTypeParams), // ../rfc/9051:6401
911 nilOrString(p.ContentID),
912 nilOrString(p.ContentDescription),
913 bodyFldEnc(p.ContentTransferEncoding),
914 number(p.EndOffset - p.BodyOffset),
915 xenvelope(p.Message),
916 xbodystructure(p.Message, extensible),
917 number(p.RawLineCount), // todo: or mp.RawLineCount?
918 }
919 } else {
920 var media token
921 switch p.MediaType {
922 case "APPLICATION", "AUDIO", "IMAGE", "FONT", "MESSAGE", "MODEL", "VIDEO":
923 media = dquote(p.MediaType)
924 default:
925 media = string0(p.MediaType)
926 }
927 // ../rfc/9051:6404 ../rfc/9051:6407
928 r = listspace{
929 media, string0(p.MediaSubType), // ../rfc/9051:6723
930 // ../rfc/9051:6376
931 bodyFldParams(p.ContentTypeParams), // ../rfc/9051:6401
932 nilOrString(p.ContentID),
933 nilOrString(p.ContentDescription),
934 bodyFldEnc(p.ContentTransferEncoding),
935 number(p.EndOffset - p.BodyOffset),
936 }
937 }
938 // 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
939 return r
940}
941