forked from emersion/neutron
/
util.go
191 lines (158 loc) · 4.4 KB
/
util.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package imap
import (
"bufio"
"encoding/base64"
"errors"
"io"
"mime"
"net/textproto"
"strconv"
"strings"
"github.com/emersion/neutron/backend"
_textproto "github.com/emersion/neutron/backend/util/textproto"
"github.com/emersion/go-imap"
)
func formatAttachmentId(mailbox string, uid uint32, part string) string {
raw := mailbox + "/" + strconv.Itoa(int(uid))
if part != "" {
raw += "#" + part
}
return base64.URLEncoding.EncodeToString([]byte(raw))
}
func formatMessageId(mailbox string, uid uint32) string {
return formatAttachmentId(mailbox, uid, "")
}
func parseAttachmentId(id string) (mailbox string, uid uint32, part string, err error) {
decoded, err := base64.URLEncoding.DecodeString(id)
if err != nil {
return
}
fstParts := strings.SplitN(string(decoded), "/", 2)
if len(fstParts) != 2 {
err = errors.New("Invalid message ID: does not contain separator")
return
}
sndParts := strings.SplitN(fstParts[1], "#", 2)
uidInt, err := strconv.Atoi(sndParts[0])
if err != nil {
return
}
mailbox = fstParts[0]
uid = uint32(uidInt)
if len(sndParts) == 2 {
part = sndParts[1]
}
return
}
func parseMessageId(id string) (mailbox string, uid uint32, err error) {
mailbox, uid, _, err = parseAttachmentId(id)
return
}
func parseMessage(msg *backend.Message, src *imap.Message) {
msg.Order = int(src.SeqNum)
msg.Size = int(src.Size)
for _, flag := range src.Flags {
switch flag {
case imap.SeenFlag:
msg.IsRead = 1
case imap.AnsweredFlag:
msg.IsReplied = 1
case imap.FlaggedFlag:
msg.Starred = 1
msg.LabelIDs = append(msg.LabelIDs, backend.StarredLabel)
case imap.DraftFlag:
msg.Type = backend.DraftType
}
}
}
func bodyStructureAttachments(structure *imap.BodyStructure) []*backend.Attachment {
// Non-multipart messages don't contain attachments
if structure.MIMEType != "multipart" || structure.MIMESubType == "alternative" {
return nil
}
var attachments []*backend.Attachment
for i, part := range structure.Parts {
if part.MIMEType == "multipart" {
attachments = append(attachments, bodyStructureAttachments(part)...)
continue
}
// Apple Mail doesn't format well headers
// First child is message content
if part.MIMEType == "text" && i == 0 {
continue
}
attachments = append(attachments, &backend.Attachment{
ID: part.Id,
Name: part.Params["name"],
MIMEType: part.MIMEType + "/" + part.MIMESubType,
Size: int(part.Size),
})
}
return attachments
}
func getPreferredPart(structure *imap.BodyStructure) (path string, part *imap.BodyStructure) {
part = structure
for i, p := range structure.Parts {
if p.MIMEType == "multipart" && p.MIMESubType == "alternative" {
path, part = getPreferredPart(p)
path = strconv.Itoa(i+1) + "." + path
}
if p.MIMEType != "text" {
continue
}
if part.MIMEType == "multipart" || p.MIMESubType == "html" {
part = p
path = strconv.Itoa(i+1)
}
}
return
}
func decodePart(part *imap.BodyStructure, r io.Reader) io.Reader {
return _textproto.Decode(r, part.Encoding, part.Params["charset"])
}
func parseAttachment(r io.Reader) (att *backend.Attachment, body io.Reader) {
br := bufio.NewReader(r)
h, err := textproto.NewReader(br).ReadMIMEHeader()
if err != nil {
return
}
mediaType, params, _ := mime.ParseMediaType(h.Get("Content-Type"))
att = &backend.Attachment{
ID: h.Get("Content-Id"),
Name: params["name"],
MIMEType: mediaType,
}
if size := h.Get("Content-Size"); size != "" {
att.Size, _ = strconv.Atoi(size)
}
body = _textproto.Decode(br, h.Get("Content-Encoding"), params["charset"])
return
}
func parseAddress(addr *imap.Address) *backend.Email {
return &backend.Email{
Name: _textproto.DecodeWord(addr.PersonalName),
Address: addr.MailboxName + "@" + addr.HostName,
}
}
func parseAddressList(list []*imap.Address) []*backend.Email {
emails := make([]*backend.Email, len(list))
for i, addr := range list {
emails[i] = parseAddress(addr)
}
return emails
}
func parseEnvelope(msg *backend.Message, envelope *imap.Envelope) {
if !envelope.Date.IsZero() {
msg.Time = envelope.Date.Unix()
}
msg.Subject = envelope.Subject // _textproto.DecodeWord()
if len(envelope.Sender) > 0 {
msg.Sender = parseAddress(envelope.Sender[0])
}
if len(envelope.ReplyTo) > 0 {
msg.ReplyTo = parseAddress(envelope.ReplyTo[0])
}
msg.ToList = parseAddressList(envelope.To)
msg.CCList = parseAddressList(envelope.Cc)
msg.BCCList = parseAddressList(envelope.Bcc)
}