-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimap.go
167 lines (127 loc) · 3.15 KB
/
imap.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
package main
import (
"bytes"
"fmt"
"io"
"time"
"code.google.com/p/go-imap/go1/imap"
)
type ImapMsg struct {
folder string
extraCopies []string
body io.Reader
}
type ImapSession struct {
client *imap.Client
isGmail bool
}
/* TODO: chan'ize the whole thing
func imapSink(msgs <-chan ImapMsg) {
existingMBs := map[string]bool{}
for msg := range msgs {
if _, ok := existingMBs[msg.folder]; !ok {
err := ctx.Imap.CreateFolder(msg.folder)
if err != nil {
fmt.Println("unable to create folder", msg.folder, " => ", err)
continue
} else {
existingMBs[msg.folder] = true
}
}
ctx.Imap.Append(msg.folder, msg.extraCopies, msg.body)
}
}
*/
func imapConnect(addr, user, pass string) (*ImapSession, error) {
c, err := imap.DialTLS(addr, nil)
if err != nil {
return nil, err
}
_, err = c.Login(user, pass)
if err != nil {
return nil, err
}
imapSession := &ImapSession{client: c}
if c.Caps["X-GM-EXT-1"] {
imapSession.isGmail = true
}
return imapSession, nil
}
func (i *ImapSession) IsConnected() bool {
return i.client != nil && i.client.State() != imap.Closed
}
func (i *ImapSession) Close() {
i.client.Logout(1 * time.Second)
}
func (i *ImapSession) imapCleanup(folders []string) error {
c := i.client
for _, mbox := range folders {
cmd, err := imap.Wait(c.Select(mbox, false))
if err != nil {
fmt.Println("unable to select", mbox, "=>", err, i.client.State())
continue
}
fmt.Println("cleaning up", mbox)
yesterday := time.Now().Add(-1 * 24 * time.Hour)
cmd = imapMust(imap.Wait(c.UIDSearch("SEEN BEFORE " + yesterday.Format("02-Jan-2006") + " NOT FLAGGED")))
toDelete, _ := imap.NewSeqSet("")
toDelete.AddNum(cmd.Data[0].SearchResults()...)
if !toDelete.Empty() {
fmt.Println("deleting...", toDelete)
if i.isGmail {
imapMust(imap.Wait(c.UIDStore(toDelete, "X-GM-LABELS", imap.NewFlagSet(`\Trash`))))
} else {
imapMust(imap.Wait(c.UIDStore(toDelete, "+FLAGS.SILENT", imap.NewFlagSet(`\Deleted`))))
}
imapMust(imap.Wait(c.Expunge(nil)))
}
}
return nil
}
func imapMust(cmd *imap.Command, err error) *imap.Command {
if err != nil {
panic(err)
}
return cmd
}
func (i *ImapSession) CreateFolder(folder string) error {
c := i.client
mbox := folder
if _, err := imap.Wait(c.Create(mbox)); err != nil {
if rsp, ok := err.(imap.ResponseError); ok && rsp.Status == imap.NO {
return nil
} else {
return err
}
}
return nil
}
func (i *ImapSession) Append(folder string, extraCopies []string, msg io.Reader) error {
c := i.client
mbox := folder
buf := new(bytes.Buffer)
buf.ReadFrom(msg)
now := time.Now()
cmd, err := imap.Wait(c.Append(mbox, nil, &now, imap.NewLiteral(buf.Bytes())))
if err != nil {
fmt.Println(err)
return err
}
rsp, err := cmd.Result(imap.OK)
if err != nil {
fmt.Println(err)
return err
}
uid := imap.AsNumber(rsp.Fields[len(rsp.Fields)-1])
set, _ := imap.NewSeqSet("")
set.AddNum(uid)
imapMust(imap.Wait(c.Select(mbox, false)))
for _, mb := range extraCopies {
if i.isGmail {
imapMust(imap.Wait(c.UIDStore(set, "X-GM-LABELS", imap.NewFlagSet(mb))))
} else {
imapMust(c.UIDCopy(set, mb))
}
}
return nil
}