forked from emersion/go-imap
/
message.go
74 lines (64 loc) · 1.79 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package memory
import (
"bufio"
"bytes"
"io"
"time"
"github.com/emersion/go-message"
"github.com/emersion/go-message/textproto"
"github.com/iampsl/go-imap"
"github.com/iampsl/go-imap/backend/backendutil"
)
type Message struct {
Uid uint32
Date time.Time
Size uint32
Flags []string
Body []byte
}
func (m *Message) entity() (*message.Entity, error) {
return message.Read(bytes.NewReader(m.Body))
}
func (m *Message) headerAndBody() (textproto.Header, io.Reader, error) {
body := bufio.NewReader(bytes.NewReader(m.Body))
hdr, err := textproto.ReadHeader(body)
return hdr, body, err
}
func (m *Message) Fetch(seqNum uint32, items []imap.FetchItem) (*imap.Message, error) {
fetched := imap.NewMessage(seqNum, items)
for _, item := range items {
switch item {
case imap.FetchEnvelope:
hdr, _, _ := m.headerAndBody()
fetched.Envelope, _ = backendutil.FetchEnvelope(hdr)
case imap.FetchBody, imap.FetchBodyStructure:
hdr, body, _ := m.headerAndBody()
fetched.BodyStructure, _ = backendutil.FetchBodyStructure(hdr, body, item == imap.FetchBodyStructure)
case imap.FetchFlags:
fetched.Flags = m.Flags
case imap.FetchInternalDate:
fetched.InternalDate = m.Date
case imap.FetchRFC822Size:
fetched.Size = m.Size
case imap.FetchUid:
fetched.Uid = m.Uid
default:
section, err := imap.ParseBodySectionName(item)
if err != nil {
break
}
body := bufio.NewReader(bytes.NewReader(m.Body))
hdr, err := textproto.ReadHeader(body)
if err != nil {
return nil, err
}
l, _ := backendutil.FetchBodySection(hdr, body, section)
fetched.Body[section] = l
}
}
return fetched, nil
}
func (m *Message) Match(seqNum uint32, c *imap.SearchCriteria) (bool, error) {
e, _ := m.entity()
return backendutil.Match(e, seqNum, m.Uid, m.Date, m.Flags, c)
}