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 "SAVEDATE":
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
411 // returning it per message should be fine. ../rfc/8514:191
412 var savedate token = nilt
413 if m.SaveDate != nil {
414 savedate = dquote(m.SaveDate.Format("_2-Jan-2006 15:04:05 -0700"))
415 }
416 return []token{bare("SAVEDATE"), savedate}
417
418 case "BODYSTRUCTURE":
419 _, part := cmd.xensureParsed()
420 bs := xbodystructure(part, true)
421 return []token{bare("BODYSTRUCTURE"), bs}
422
423 case "BODY":
424 respField, t := cmd.xbody(a)
425 if respField == "" {
426 return nil
427 }
428 return []token{bare(respField), t}
429
430 case "BINARY.SIZE":
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))}
438 }
439 p = cmd.xpartnumsDeref(a.sectionBinary, p)
440 if len(p.Parts) > 0 || p.Message != nil {
441 // ../rfc/9051:4385
442 cmd.xerrorf("binary only allowed on leaf parts, not multipart/* or message/rfc822 or message/global")
443 }
444 return []token{bare(cmd.sectionRespField(a)), number(p.DecodedSize)}
445
446 case "BINARY":
447 respField, t := cmd.xbinary(a)
448 if respField == "" {
449 return nil
450 }
451 return []token{bare(respField), t}
452
453 case "RFC822.SIZE":
454 m := cmd.xensureMessage()
455 return []token{bare("RFC822.SIZE"), number(m.Size)}
456
457 case "RFC822.HEADER":
458 ba := fetchAtt{
459 field: "BODY",
460 peek: true,
461 section: &sectionSpec{
462 msgtext: &sectionMsgtext{s: "HEADER"},
463 },
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":
472 ba := fetchAtt{
473 field: "BODY",
474 section: &sectionSpec{},
475 }
476 respField, t := cmd.xbody(ba)
477 if respField == "" {
478 return nil
479 }
480 return []token{bare(a.field), t}
481
482 case "RFC822.TEXT":
483 ba := fetchAtt{
484 field: "BODY",
485 section: &sectionSpec{
486 msgtext: &sectionMsgtext{s: "TEXT"},
487 },
488 }
489 respField, t := cmd.xbody(ba)
490 if respField == "" {
491 return nil
492 }
493 return []token{bare(a.field), t}
494
495 case "FLAGS":
496 cmd.needFlags = true
497
498 case "MODSEQ":
499 cmd.needModseq = true
500
501 default:
502 xserverErrorf("field %q not yet implemented", a.field)
503 }
504 return nil
505}
506
507// ../rfc/9051:6522
508func xenvelope(p *message.Part) token {
509 var env message.Envelope
510 if p.Envelope != nil {
511 env = *p.Envelope
512 }
513 var date token = nilt
514 if !env.Date.IsZero() {
515 // ../rfc/5322:791
516 date = string0(env.Date.Format("Mon, 2 Jan 2006 15:04:05 -0700"))
517 }
518 var subject token = nilt
519 if env.Subject != "" {
520 subject = string0(env.Subject)
521 }
522 var inReplyTo token = nilt
523 if env.InReplyTo != "" {
524 inReplyTo = string0(env.InReplyTo)
525 }
526 var messageID token = nilt
527 if env.MessageID != "" {
528 messageID = string0(env.MessageID)
529 }
530
531 addresses := func(l []message.Address) token {
532 if len(l) == 0 {
533 return nilt
534 }
535 r := listspace{}
536 for _, a := range l {
537 var name token = nilt
538 if a.Name != "" {
539 name = string0(a.Name)
540 }
541 user := string0(a.User)
542 var host token = nilt
543 if a.Host != "" {
544 host = string0(a.Host)
545 }
546 r = append(r, listspace{name, nilt, user, host})
547 }
548 return r
549 }
550
551 // Empty sender or reply-to result in fall-back to from. ../rfc/9051:6140
552 sender := env.Sender
553 if len(sender) == 0 {
554 sender = env.From
555 }
556 replyTo := env.ReplyTo
557 if len(replyTo) == 0 {
558 replyTo = env.From
559 }
560
561 return listspace{
562 date,
563 subject,
564 addresses(env.From),
565 addresses(sender),
566 addresses(replyTo),
567 addresses(env.To),
568 addresses(env.CC),
569 addresses(env.BCC),
570 inReplyTo,
571 messageID,
572 }
573}
574
575func (cmd *fetchCmd) peekOrSeen(peek bool) {
576 if cmd.conn.readonly || peek {
577 return
578 }
579 m := cmd.xensureMessage()
580 if !m.Seen {
581 cmd.markSeen = true
582 cmd.needFlags = true
583 }
584}
585
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())
590}
591
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")
596
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)) {
601 return true
602 }
603 }
604 return false
605 }
606
607 var match bool
608 hb := &bytes.Buffer{}
609 for len(h) > 0 {
610 line := h
611 i := bytes.Index(line, []byte("\r\n"))
612 if i >= 0 {
613 line = line[:i+2]
614 }
615 h = h[len(line):]
616
617 match = matchesFields(line) || match && (bytes.HasPrefix(line, []byte(" ")) || bytes.HasPrefix(line, []byte("\t")))
618 if match != not || len(line) == 2 {
619 hb.Write(line)
620 }
621 }
622 return hb
623}
624
625func (cmd *fetchCmd) xbinary(a fetchAtt) (string, token) {
626 _, part := cmd.xensureParsed()
627
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)
633 }
634 return cmd.sectionRespField(a), readerSyncliteral{r}
635 }
636
637 p := part
638 if len(a.sectionBinary) > 0 {
639 p = cmd.xpartnumsDeref(a.sectionBinary, p)
640 }
641 if len(p.Parts) != 0 || p.Message != nil {
642 // ../rfc/9051:4385
643 cmd.xerrorf("binary only allowed on leaf parts, not multipart/* or message/rfc822 or message/global")
644 }
645
646 switch p.ContentTransferEncoding {
647 case "", "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE":
648 default:
649 // ../rfc/9051:5913
650 xusercodeErrorf("UNKNOWN-CTE", "unknown Content-Transfer-Encoding %q", p.ContentTransferEncoding)
651 }
652
653 r := p.Reader()
654 if a.partial != nil {
655 r = cmd.xpartialReader(a.partial, r)
656 }
657 return cmd.sectionRespField(a), readerSyncliteral{r}
658}
659
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) {
664 return strings.NewReader("") // ../rfc/3501:3143 ../rfc/9051:4418
665 }
666 return io.LimitReader(r, int64(partial.count))
667}
668
669func (cmd *fetchCmd) xbody(a fetchAtt) (string, token) {
670 msgr, part := cmd.xensureParsed()
671
672 if a.section == nil {
673 // Non-extensible form of BODYSTRUCTURE.
674 return a.field, xbodystructure(part, false)
675 }
676
677 cmd.peekOrSeen(a.peek)
678
679 respField := cmd.sectionRespField(a)
680
681 if a.section.msgtext == nil && a.section.part == nil {
682 m := cmd.xensureMessage()
683 var offset int64
684 count := m.Size
685 if a.partial != nil {
686 offset = int64(a.partial.offset)
687 if offset > m.Size {
688 offset = m.Size
689 }
690 count = int64(a.partial.count)
691 if offset+count > m.Size {
692 count = m.Size - offset
693 }
694 }
695 return respField, readerSizeSyncliteral{&moxio.AtReader{R: msgr, Offset: offset}, count, false}
696 }
697
698 sr := cmd.xsection(a.section, part)
699
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) {
704 return respField, syncliteral("") // ../rfc/3501:3143 ../rfc/9051:4418
705 }
706 return respField, readerSyncliteral{io.LimitReader(sr, int64(a.partial.count))}
707 }
708 return respField, readerSyncliteral{sr}
709}
710
711func (cmd *fetchCmd) xpartnumsDeref(nums []uint32, p *message.Part) *message.Part {
712 // ../rfc/9051:4481
713 if (len(p.Parts) == 0 && p.Message == nil) && len(nums) == 1 && nums[0] == 1 {
714 return p
715 }
716
717 // ../rfc/9051:4485
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)
724 }
725 if index < 0 || index >= len(p.Parts) {
726 cmd.xerrorf("requested part does not exist")
727 }
728 p = &p.Parts[index]
729 }
730 return p
731}
732
733func (cmd *fetchCmd) xsection(section *sectionSpec, p *message.Part) io.Reader {
734 if section.part == nil {
735 return cmd.xsectionMsgtext(section.msgtext, p)
736 }
737
738 p = cmd.xpartnumsDeref(section.part.part, p)
739
740 if section.part.text == nil {
741 return p.RawReader()
742 }
743
744 // ../rfc/9051:4535
745 if p.Message != nil {
746 err := p.SetMessageReaderAt()
747 cmd.xcheckf(err, "preparing submessage")
748 p = p.Message
749 }
750
751 if !section.part.text.mime {
752 return cmd.xsectionMsgtext(section.part.text.msgtext, p)
753 }
754
755 // MIME header, see ../rfc/9051:4534 ../rfc/2045:1645
756 h, err := io.ReadAll(p.HeaderReader())
757 cmd.xcheckf(err, "reading header")
758
759 matchesFields := func(line []byte) bool {
760 k := textproto.CanonicalMIMEHeaderKey(string(bytes.TrimRight(bytes.SplitN(line, []byte(":"), 2)[0], " \t")))
761 // Only add MIME-Version and additional CRLF for messages, not other parts. ../rfc/2045:1645 ../rfc/2045:1652
762 return (p.Envelope != nil && k == "Mime-Version") || strings.HasPrefix(k, "Content-")
763 }
764
765 var match bool
766 hb := &bytes.Buffer{}
767 for len(h) > 0 {
768 line := h
769 i := bytes.Index(line, []byte("\r\n"))
770 if i >= 0 {
771 line = line[:i+2]
772 }
773 h = h[len(line):]
774
775 match = matchesFields(line) || match && (bytes.HasPrefix(line, []byte(" ")) || bytes.HasPrefix(line, []byte("\t")))
776 if match || len(line) == 2 {
777 hb.Write(line)
778 }
779 }
780 return hb
781}
782
783func (cmd *fetchCmd) xsectionMsgtext(smt *sectionMsgtext, p *message.Part) io.Reader {
784 if smt.s == "HEADER" {
785 return p.HeaderReader()
786 }
787
788 switch smt.s {
789 case "HEADER.FIELDS":
790 return cmd.xmodifiedHeader(p, smt.headers, false)
791
792 case "HEADER.FIELDS.NOT":
793 return cmd.xmodifiedHeader(p, smt.headers, true)
794
795 case "TEXT":
796 // It appears imap clients expect to get the body of the message, not a "text body"
797 // which sounds like it means a text/* part of a message. ../rfc/9051:4517
798 return p.RawReader()
799 }
800 panic(serverError{fmt.Errorf("missing case")})
801}
802
803func (cmd *fetchCmd) sectionRespField(a fetchAtt) string {
804 s := a.field + "["
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)
809 }
810 } else if a.section != nil {
811 if a.section.part != nil {
812 p := a.section.part
813 s += fmt.Sprintf("%d", p.part[0])
814 for _, v := range p.part[1:] {
815 s += "." + fmt.Sprintf("%d", v)
816 }
817 if p.text != nil {
818 if p.text.mime {
819 s += ".MIME"
820 } else {
821 s += "." + cmd.sectionMsgtextName(p.text.msgtext)
822 }
823 }
824 } else if a.section.msgtext != nil {
825 s += cmd.sectionMsgtextName(a.section.msgtext)
826 }
827 }
828 s += "]"
829 // binary does not have partial in field, unlike BODY ../rfc/9051:6757
830 if a.field != "BINARY" && a.partial != nil {
831 s += fmt.Sprintf("<%d>", a.partial.offset)
832 }
833 return s
834}
835
836func (cmd *fetchCmd) sectionMsgtextName(smt *sectionMsgtext) string {
837 s := smt.s
838 if strings.HasPrefix(smt.s, "HEADER.FIELDS") {
839 l := listspace{}
840 for _, h := range smt.headers {
841 l = append(l, astring(h))
842 }
843 s += " " + l.pack(cmd.conn)
844 }
845 return s
846}
847
848func bodyFldParams(params map[string]string) token {
849 if len(params) == 0 {
850 return nilt
851 }
852 // Ensure same ordering, easier for testing.
853 var keys []string
854 for k := range params {
855 keys = append(keys, k)
856 }
857 sort.Strings(keys)
858 l := make(listspace, 2*len(keys))
859 i := 0
860 for _, k := range keys {
861 l[i] = string0(strings.ToUpper(k))
862 l[i+1] = string0(params[k])
863 i += 2
864 }
865 return l
866}
867
868func bodyFldEnc(s string) token {
869 up := strings.ToUpper(s)
870 switch up {
871 case "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE":
872 return dquote(up)
873 }
874 return string0(s)
875}
876
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" {
881 // Multipart, ../rfc/9051:6355 ../rfc/9051:6411
882 var bodies concat
883 for i := range p.Parts {
884 bodies = append(bodies, xbodystructure(&p.Parts[i], extensible))
885 }
886 r := listspace{bodies, string0(p.MediaSubType)}
887 if extensible {
888 if len(p.ContentTypeParams) == 0 {
889 r = append(r, nilt)
890 } else {
891 params := make(listspace, 0, 2*len(p.ContentTypeParams))
892 for k, v := range p.ContentTypeParams {
893 params = append(params, string0(k), string0(v))
894 }
895 r = append(r, params)
896 }
897 }
898 return r
899 }
900
901 // ../rfc/9051:6355
902 var r listspace
903 if p.MediaType == "TEXT" {
904 // ../rfc/9051:6404 ../rfc/9051:6418
905 r = listspace{
906 dquote("TEXT"), string0(p.MediaSubType), // ../rfc/9051:6739
907 // ../rfc/9051:6376
908 bodyFldParams(p.ContentTypeParams), // ../rfc/9051:6401
909 nilOrString(p.ContentID),
910 nilOrString(p.ContentDescription),
911 bodyFldEnc(p.ContentTransferEncoding),
912 number(p.EndOffset - p.BodyOffset),
913 number(p.RawLineCount),
914 }
915 } else if p.MediaType == "MESSAGE" && (p.MediaSubType == "RFC822" || p.MediaSubType == "GLOBAL") {
916 // ../rfc/9051:6415
917 // note: we don't have to prepare p.Message for reading, because we aren't going to read from it.
918 r = listspace{
919 dquote("MESSAGE"), dquote(p.MediaSubType), // ../rfc/9051:6732
920 // ../rfc/9051:6376
921 bodyFldParams(p.ContentTypeParams), // ../rfc/9051:6401
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?
929 }
930 } else {
931 var media token
932 switch p.MediaType {
933 case "APPLICATION", "AUDIO", "IMAGE", "FONT", "MESSAGE", "MODEL", "VIDEO":
934 media = dquote(p.MediaType)
935 default:
936 media = string0(p.MediaType)
937 }
938 // ../rfc/9051:6404 ../rfc/9051:6407
939 r = listspace{
940 media, string0(p.MediaSubType), // ../rfc/9051:6723
941 // ../rfc/9051:6376
942 bodyFldParams(p.ContentTypeParams), // ../rfc/9051:6401
943 nilOrString(p.ContentID),
944 nilOrString(p.ContentDescription),
945 bodyFldEnc(p.ContentTransferEncoding),
946 number(p.EndOffset - p.BodyOffset),
947 }
948 }
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
950 return r
951}
952