-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgs.go
160 lines (124 loc) · 3 KB
/
msgs.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
package main
import (
"crypto/md5"
"strconv"
"unicode"
imap "github.com/emersion/go-imap"
)
var (
shortFetchItem = []imap.FetchItem{
imap.FetchInternalDate,
imap.FetchEnvelope,
imap.FetchRFC822Size,
imap.FetchUid,
}
fullFetchItem = []imap.FetchItem{
imap.FetchFlags,
imap.FetchInternalDate,
imap.FetchRFC822Size,
imap.FetchEnvelope,
imap.FetchUid,
"BODY.PEEK[]",
}
)
const (
hashLen = md5.Size
)
func msgHash(m *imap.Message) [hashLen]byte {
var b []byte
b = append(b, []byte(strconv.FormatInt(m.Envelope.Date.Unix(), 10))...)
b = append(b, stripSpaceToBytes(m.Envelope.Subject)...)
b = append(b, []byte(strconv.FormatUint(uint64(m.Size), 10))...)
b = addresses(m.Envelope.From).appendBytesTo(b)
b = addresses(m.Envelope.To).appendBytesTo(b)
b = addresses(m.Envelope.Cc).appendBytesTo(b)
b = addresses(m.Envelope.Bcc).appendBytesTo(b)
return md5.Sum(b)
}
func msgHashes(cl *imapClient, mi *imapMailboxInfo, fromSafe bool) (map[[hashLen]byte]uint32, error) {
hs := make(map[[hashLen]byte]uint32)
prepFn := mi.preparedName
if fromSafe {
prepFn = mi.preparedSafeName
}
mb, err := cl.Select(prepFn(cl.delim, cl.pathprfx), false)
if err != nil {
return hs, err
}
if mb.Messages == 0 {
return hs, nil
}
seq := &imap.SeqSet{}
seq.AddRange(1, mb.Messages)
msgs := make(chan *imap.Message, 40)
msgsErr := make(chan error, 1)
go func() {
msgsErr <- cl.Fetch(seq, shortFetchItem, msgs)
}()
for msg := range msgs {
if msg.Size == 0 {
continue
}
hs[msgHash(msg)] = msg.Uid
}
return hs, <-msgsErr
}
func missingUIDs(dst, src *imapClient, mi *imapMailboxInfo) ([]uint32, error) {
srcHs, err := msgHashes(src, mi, false)
if err != nil {
return nil, err
}
dstHs, err := msgHashes(dst, mi, true)
if err != nil {
return nil, err
}
var uids []uint32
for hash, uid := range srcHs {
if _, ok := dstHs[hash]; !ok {
uids = append(uids, uid)
}
}
return uids, nil
}
func messages(done chan struct{}, cl *imapClient, mi *imapMailboxInfo, seq *imap.SeqSet) ([]*imap.Message, error) {
var ms []*imap.Message
mb, err := cl.Select(mi.preparedName(cl.delim, cl.pathprfx), false)
if err != nil {
return nil, err
}
if mb.Messages == 0 {
return nil, nil
}
msgs := make(chan *imap.Message, 40)
msgsErr := make(chan error, 1)
go func() {
msgsErr <- cl.UidFetch(seq, fullFetchItem, msgs)
}()
for msg := range msgs {
ms = append(ms, msg)
}
return ms, <-msgsErr
}
func addMsgs(done chan struct{}, cl *imapClient, mi *imapMailboxInfo, msgs []*imap.Message) error {
mbName := mi.preparedSafeName(cl.delim, cl.pathprfx)
bsn, err := imap.ParseBodySectionName(imap.FetchRFC822)
bsn.Peek = true
if err != nil {
return err
}
for _, msg := range msgs {
if err := cl.Append(mbName, msg.Flags, msg.InternalDate, msg.GetBody(bsn)); err != nil {
return err
}
}
return nil
}
func stripSpaceToBytes(s string) []byte {
b := make([]byte, 0, len(s))
for _, r := range s {
if !unicode.IsSpace(r) {
b = append(b, []byte(string(r))...)
}
}
return b
}