8379fddbb2
list and search now fetch BODY.PEEK[HEADER] + BODYSTRUCTURE instead of the whole RFC822 message, so listing a large mailbox no longer downloads every message body and attachment. Header parsing reuses the same go-message path (RFC2047 decoding/formatting preserved); has_attachments is derived from the BODYSTRUCTURE tree. FetchFull keeps fetching the full message for get. Validated end-to-end against a live IMAP account: list/search/get output identical to the prior full-fetch behaviour, has_attachments correct. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
119 lines
3.0 KiB
Go
119 lines
3.0 KiB
Go
// Package mail provides IMAP reading and RFC822 message parsing.
|
|
package mail
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/emersion/go-message/mail"
|
|
)
|
|
|
|
type Header struct {
|
|
UID uint32
|
|
From string
|
|
To string
|
|
Subject string
|
|
Date string
|
|
MessageID string
|
|
HasAttachments bool
|
|
}
|
|
|
|
type Attachment struct {
|
|
Name string
|
|
Size int
|
|
MIME string
|
|
Content []byte
|
|
}
|
|
|
|
type Message struct {
|
|
Header Header
|
|
BodyText string
|
|
Attachments []Attachment
|
|
}
|
|
|
|
func readHeader(mr *mail.Reader, uid uint32) Header {
|
|
h := Header{UID: uid}
|
|
hd := mr.Header
|
|
h.Subject, _ = hd.Subject()
|
|
if addrs, err := hd.AddressList("From"); err == nil && len(addrs) > 0 {
|
|
h.From = addrs[0].String()
|
|
}
|
|
if addrs, err := hd.AddressList("To"); err == nil && len(addrs) > 0 {
|
|
h.To = addrs[0].String()
|
|
}
|
|
if d, err := hd.Date(); err == nil {
|
|
h.Date = d.UTC().Format("Mon, 02 Jan 2006 15:04:05 -0700")
|
|
}
|
|
if msgID, err := hd.MessageID(); err == nil {
|
|
h.MessageID = msgID
|
|
} else {
|
|
h.MessageID = strings.Trim(hd.Get("Message-Id"), "<> ")
|
|
}
|
|
return h
|
|
}
|
|
|
|
// ParseMessage decodes the full message including attachment contents.
|
|
func ParseMessage(uid uint32, raw []byte) (Message, error) {
|
|
mr, err := mail.CreateReader(bytes.NewReader(raw))
|
|
if err != nil {
|
|
return Message{}, err
|
|
}
|
|
m := Message{Header: readHeader(mr, uid)}
|
|
for {
|
|
part, err := mr.NextPart()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return Message{}, err
|
|
}
|
|
switch hdr := part.Header.(type) {
|
|
case *mail.InlineHeader:
|
|
ct, _, _ := hdr.ContentType()
|
|
if strings.HasPrefix(ct, "text/plain") && m.BodyText == "" {
|
|
b, err := io.ReadAll(part.Body)
|
|
if err != nil {
|
|
return Message{}, fmt.Errorf("read message part: %w", err)
|
|
}
|
|
m.BodyText = string(b)
|
|
}
|
|
case *mail.AttachmentHeader:
|
|
name, _ := hdr.Filename()
|
|
ct, _, _ := hdr.ContentType()
|
|
b, err := io.ReadAll(part.Body)
|
|
if err != nil {
|
|
return Message{}, fmt.Errorf("read message part: %w", err)
|
|
}
|
|
m.Attachments = append(m.Attachments, Attachment{
|
|
Name: name, Size: len(b), MIME: ct, Content: b,
|
|
})
|
|
}
|
|
}
|
|
m.Header.HasAttachments = len(m.Attachments) > 0
|
|
return m, nil
|
|
}
|
|
|
|
// ParseHeaderOnly decodes headers and detects attachments without keeping bodies.
|
|
func ParseHeaderOnly(uid uint32, raw []byte) (Header, error) {
|
|
m, err := ParseMessage(uid, raw)
|
|
if err != nil {
|
|
return Header{}, err
|
|
}
|
|
return m.Header, nil
|
|
}
|
|
|
|
// ParseHeaderBytes parses just a message's RFC822 header block (the bytes from
|
|
// an IMAP BODY[HEADER] fetch, with no body) into a Header, reusing the same
|
|
// RFC2047-decoding and formatting as ParseMessage. HasAttachments is left false:
|
|
// the caller, which holds the BODYSTRUCTURE, sets it. This lets list/search read
|
|
// headers without downloading message bodies.
|
|
func ParseHeaderBytes(uid uint32, raw []byte) (Header, error) {
|
|
mr, err := mail.CreateReader(bytes.NewReader(raw))
|
|
if err != nil {
|
|
return Header{}, err
|
|
}
|
|
return readHeader(mr, uid), nil
|
|
}
|