This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimap.go
345 lines (300 loc) Β· 8.28 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"code.google.com/p/go-imap/go1/imap"
"fmt"
"time"
"bytes"
"net/mail"
"crypto/tls"
"io/ioutil"
"strings"
"encoding/json"
"sort"
"strconv"
"os/user"
"github.com/coopernurse/gorp"
"log"
)
var dbmap *gorp.DbMap
var useDbStore bool
type Message struct {
Subject string
Date time.Time
From *mail.Address
Flags []string
Labels []string
ThreadID string
MessageID string
}
type MessageDetail struct {
Body string
}
type MessageArray []*Message
type Conversations map[string]MessageArray
func (s MessageArray) Len() int {
return len(s)
}
func (s MessageArray) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s MessageArray) Less(i, j int) bool {
return s[i].Date.Before(s[j].Date)
}
func extractQuotedList (listRaw []imap.Field) []string {
var list []string
for _, item := range listRaw {
uqS, ok := imap.Unquote(item.(string))
if ok == false { uqS = item.(string) }
list = append(list, uqS)
}
return list
}
func listMessages (c *imap.Client, cmd *imap.Command) MessageArray {
var list MessageArray
for _, rsp := range cmd.Data {
header := imap.AsBytes(rsp.MessageInfo().Attrs["BODY[HEADER]"])
threadID, _ := rsp.MessageInfo().Attrs["X-GM-THRID"].(string)
messageID, _ := rsp.MessageInfo().Attrs["X-GM-MSGID"].(string)
flagsRaw := imap.AsList(rsp.MessageInfo().Attrs["FLAGS"])
labelsRaw := imap.AsList(rsp.MessageInfo().Attrs["X-GM-LABELS"])
flags := extractQuotedList(flagsRaw)
labels := extractQuotedList(labelsRaw)
msg, err := mail.ReadMessage(bytes.NewReader(header))
if (err != nil) {
fmt.Println(err.Error())
continue
}
date, err := msg.Header.Date()
if (err != nil) {
fmt.Println(err.Error())
continue
}
fromList, err := msg.Header.AddressList("From")
if (err != nil) {
fmt.Println(err.Error())
continue
}
messageStruct := Message{msg.Header.Get("Subject"), date,
fromList[0], flags, labels, threadID, messageID}
// Insert into db
thread := newThread(threadID, msg.Header.Get("Subject"))
insertThread(dbmap, thread, labels, flags)
message := newMessage(threadID, messageID, date, fromList[0].Name, fromList[0].Address)
insertMessage(dbmap, message)
list = append(list, &messageStruct)
}
cmd.Data = nil
return list
}
func threadSearch (cmd *imap.Command) []*Message {
set, _ := imap.NewSeqSet("")
if _, err := cmd.Result(imap.OK); err != nil {
fmt.Println(err.Error())
return nil
}
results := cmd.Data[0].SearchResults()
if set.AddNum(results...); set.Empty() {
fmt.Println("Error: No search results")
return nil
}
cmd.Data = nil
cmd, err := imap.Wait(c.Fetch(set, "BODY.PEEK[HEADER]", "X-GM-THRID",
"X-GM-MSGID", "FLAGS", "X-GM-LABELS"))
if (err != nil) {
fmt.Println(err.Error())
return nil
}
return listMessages(c, cmd)
}
func listConversations (c *imap.Client, cmd *imap.Command) []byte {
threadCmds := make(map[string]*imap.Command)
conversations := make(Conversations)
for _, rsp := range cmd.Data {
threadid := rsp.MessageInfo().Attrs["X-GM-THRID"].(string)
conversations[threadid] = nil
}
cmd.Data = nil
/* Load threads from db, or retrieve over IMAP */
for threadid, _ := range conversations {
thread, err := retrieveThread(dbmap, threadid)
if useDbStore && err == nil {
conversations[threadid] = retrieveMessages(dbmap, thread)
} else {
selectMailbox(c, "[Gmail]/All Mail", true)
fmt.Println("Fetching thread from IMAP")
threadCmds[threadid], err = c.Search("X-GM-THRID", c.Quote(threadid))
if err != nil {
log.Fatalln("Search failed", err)
delete(threadCmds, threadid)
}
}
}
for threadid, threadCmd := range threadCmds {
conversations[threadid] = threadSearch(threadCmd)
}
/* Convert a hashtable keyed by threadID to a hashtable keyed
/* by latest conversation date */
var keys []string
for key, _ := range conversations { keys = append(keys, key); }
for _, key := range keys {
value := conversations[key]
if (value == nil) {
fmt.Println("Error: conversation with key", key, "wasn't fetched")
continue;
}
sort.Sort(MessageArray(value))
newKey := value[len(value) - 1].Date
delete(conversations, key)
conversations[strconv.FormatInt(newKey.Unix(), 10)] = value
}
bytestring, _ := json.Marshal(conversations)
return bytestring
}
func initClient (debug bool) *imap.Client {
var config *tls.Config
if (debug) {
useDbStore = false
} else {
useDbStore = true
}
// Connect to the server
c, err := imap.DialTLS("imap.gmail.com", config)
if (err != nil) {
fmt.Println(err.Error())
return nil
}
// Print server greeting (first response in the unilateral server data queue)
fmt.Println("Server says hello:", c.Data[0].Info)
c.Data = nil
// Enable encryption, if supported by the server
if c.Caps["STARTTLS"] {
c.StartTLS(nil)
}
// Authenticate
usr, _ := user.Current()
credentialsFile := fmt.Sprintf("%s/%s", usr.HomeDir, ".ibex/credentials")
if c.State() == imap.Login {
b, err := ioutil.ReadFile(credentialsFile)
if (err != nil) {
fmt.Println(err.Error())
return nil
}
userPass := strings.Split(string(b), "\n")
c.Login(userPass[0], userPass[1])
}
// Initiate a db connection
dbmap = initDb(false)
return c
}
func gmailSearch (c *imap.Client, searchString string, limit int) []byte {
set, _ := imap.NewSeqSet("")
cmd, err := imap.Wait(c.Search("X-GM-RAW", c.Quote(searchString)))
if (err != nil) {
fmt.Println(err.Error())
return nil
}
results := cmd.Data[0].SearchResults()
var cut int
if (len(results) < limit) { cut = 0; } else { cut = len(results) - limit; }
if set.AddNum(results[cut:]...); set.Empty() {
fmt.Println("Error: Empty search")
return nil
}
cmd.Data = nil
cmd, err = imap.Wait(c.Fetch(set, "X-GM-THRID"))
if (err != nil) {
fmt.Println(err.Error())
return nil
}
bytestring := listConversations(c, cmd)
return bytestring
}
func listRecent (c *imap.Client, limit uint32) []byte {
set, _ := imap.NewSeqSet("")
if (c.Mailbox == nil) {
fmt.Println("Error: No mailbox selected")
return nil
}
if c.Mailbox.Messages > limit {
set.AddRange(c.Mailbox.Messages - limit, c.Mailbox.Messages)
} else {
set.Add("1:*")
}
cmd, err := imap.Wait(c.Fetch(set, "X-GM-THRID"))
if (err != nil) {
fmt.Println(err.Error())
return nil
}
bytestring := listConversations(c, cmd)
return bytestring
}
func selectMailbox(c *imap.Client, name string, readonly bool) {
if (c.Mailbox == nil || c.Mailbox.Name != name) {
c.Select(name, readonly)
}
}
func fetchMessage (c *imap.Client, messageID string) []byte {
selectMailbox(c, "[Gmail]/All Mail", true)
set, _ := imap.NewSeqSet("")
qS := c.Quote(messageID)
cmd, err := imap.Wait(c.UIDSearch("X-GM-MSGID", qS))
if (err != nil) {
fmt.Println(err.Error())
return nil
}
results := cmd.Data[0].SearchResults()
if len(results) != 1 {
fmt.Println("Error: Could not get message for", messageID)
return nil
}
set.AddNum(results[0])
cmd.Data = nil
var body []byte
cmd, err = imap.Wait(c.UIDFetch(set, "BODY.PEEK[]"))
if (err != nil) {
fmt.Println(err.Error())
return nil
}
body = imap.AsBytes(cmd.Data[0].MessageInfo().Attrs["BODY[]"])
cmd.Data = nil
bytestring, err := json.Marshal(MessageDetail{string(body)})
if (err != nil) {
fmt.Println(err.Error())
return nil
}
return bytestring
}
func imapMain () {
c := initClient(false)
if (c == nil) { return }
// Remember to log out and close the connection when finished
defer c.Logout(30 * time.Second)
// List all top-level mailboxes, wait for the command to finish
fmt.Println("\nTop-level mailboxes:")
cmd, _ := imap.Wait(c.List("", "*"))
for _, rsp := range cmd.Data {
fmt.Println("|--", rsp.MailboxInfo())
}
// Check for new unilateral server data responses
for _, rsp := range c.Data {
fmt.Println("Server data:", rsp)
}
c.Data = nil
// Open a mailbox (synchronous command - no need for imap.Wait)
c.Select("INBOX", true)
fmt.Println("\nMailbox status:\n", c.Mailbox)
fmt.Println(string(listRecent(c, 20)))
c.Select("[Gmail]/All Mail", true)
fmt.Println("\nMessages in All Mail:")
fmt.Println(string(listRecent(c, 20)))
// fmt.Println(string(gmailSearch(c, "has:attachment", 20)))
fmt.Println(string(fetchMessage(c, "1452889506408166778")));
// Check command completion status
if rsp, err := cmd.Result(imap.OK); err != nil {
if err == imap.ErrAborted {
fmt.Println("Fetch command aborted")
} else {
fmt.Println("Fetch error:", rsp.Info)
}
}
}