-
Notifications
You must be signed in to change notification settings - Fork 8
/
ezGmail.go
293 lines (263 loc) · 11.5 KB
/
ezGmail.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
package ezGmail
import (
"encoding/json"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
)
// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
cacheFile, err := tokenCacheFile()
if err != nil {
log.Fatalf("Unable to get path to cached credential file. %v", err)
}
tok, err := tokenFromFile(cacheFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(cacheFile, tok)
}
return config.Client(ctx, tok)
}
// getTokenFromWeb uses Config to request a Token.
// It returns the retrieved Token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authURL)
var code string
if _, err := fmt.Scan(&code); err != nil {
log.Fatalf("Unable to read authorization code %v", err)
}
tok, err := config.Exchange(oauth2.NoContext, code)
if err != nil {
log.Fatalf("Unable to retrieve token from web %v", err)
}
return tok
}
// tokenCacheFile generates credential file path/filename.
// It returns the generated credential path/filename.
func tokenCacheFile() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
tokenCacheDir := filepath.Join(usr.HomeDir, ".ezGmail")
os.MkdirAll(tokenCacheDir, 0700)
return filepath.Join(tokenCacheDir,
url.QueryEscape("ezGmail.json")), err
}
// tokenFromFile retrieves a Token from a given file path.
// It returns the retrieved Token and any read error encountered.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
t := &oauth2.Token{}
err = json.NewDecoder(f).Decode(t)
defer f.Close()
return t, err
}
// saveToken uses a file path to create a file and store the
// token in it.
func saveToken(file string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", file)
f, err := os.Create(file)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}
type GmailService struct {
srv *gmail.Service
sUser string
sLabel string
iMaxResults int64
//search query data
sFrom string //
sTo string //
sOlder string //
sNewer string //
sOlderRel string //
sNewerRel string //
sSubject string //
sInPlace string //in:{inbox,sent,trash,spam,anywhere}
sLarger string //
sSmaller string //
sFilename string //
sMatch string
sMatchExact string
sHasAttachment bool
}
func (gs *GmailService) InitSrv() {
// Connect and create gmail.Service object
ctx := context.Background()
b, err := ioutil.ReadFile("client_secret.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
config, err := google.ConfigFromJSON(b, gmail.GmailReadonlyScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(ctx, config)
gs.srv, err = gmail.New(client)
if err != nil {
log.Fatalf("Unable to retrieve gmail Client %v", err)
}
// Assign Defaults
gs.iMaxResults = 50
gs.sUser = "me"
}
func (gs *GmailService) MaxResults (maxres int64 ) *GmailService { gs.iMaxResults = maxres ; return gs }
func (gs *GmailService) From (from string) *GmailService { gs.sFrom = from ; return gs }
func (gs *GmailService) To (to string) *GmailService { gs.sTo = to ; return gs }
func (gs *GmailService) OlderThan (older string) *GmailService { gs.sOlder = older ; return gs }
func (gs *GmailService) NewerThan (newer string) *GmailService { gs.sNewer = newer ; return gs }
func (gs *GmailService) OlderThanRel (older string) *GmailService { gs.sOlderRel = older ; return gs }
func (gs *GmailService) NewerThanRel (newer string) *GmailService { gs.sNewerRel = newer ; return gs }
func (gs *GmailService) Subject (subject string) *GmailService { gs.sSubject = subject ; return gs }
func (gs *GmailService) InInbox () *GmailService { gs.sInPlace = "inbox" ; return gs }
func (gs *GmailService) InSent () *GmailService { gs.sInPlace = "sent" ; return gs }
func (gs *GmailService) InTrash () *GmailService { gs.sInPlace = "trash" ; return gs }
func (gs *GmailService) InSpam () *GmailService { gs.sInPlace = "spam" ; return gs }
func (gs *GmailService) InAnywhere () *GmailService { gs.sInPlace = "anywhere"; return gs }
func (gs *GmailService) LargerThan (larger string) *GmailService { gs.sLarger = larger ; return gs }
func (gs *GmailService) SmallerThan (smaller string) *GmailService { gs.sSmaller = smaller ; return gs }
func (gs *GmailService) Filename (fname string) *GmailService { gs.sFilename = fname ; return gs }
func (gs *GmailService) HasAttachment (hasatt bool ) *GmailService { gs.sHasAttachment = hasatt ; return gs }
func (gs *GmailService) Match (match string) *GmailService { gs.sMatch = match ; return gs }
func (gs *GmailService) MatchExact (matchex string) *GmailService { gs.sMatchExact = matchex ; return gs }
func (gm *GmailMessage) parseMessagePart(origmsg *gmail.MessagePart, gs *GmailService) {
var contentDisp string // Content-Disposition
gm.mimeFlow = append(gm.mimeFlow, origmsg.MimeType)
for _, ii := range(origmsg.Headers) {
if ii.Name == "Subject" { gm.sSubject = ii.Value }
if ii.Name == "Content-Disposition" { contentDisp = ii.Value }
if ii.Name == "Message-ID" { gm.sMessageId = ii.Value }
}
if strings.HasPrefix(origmsg.MimeType,"multipart") {
for _, ii := range(origmsg.Parts) {
gm.parseMessagePart(ii, gs)
}
}
if strings.HasPrefix(contentDisp, "attachment") {
var a = new(GmailAttachment)
a.gmailService = gs
a.bDownloaded = false
a.sMessageId = gm.sMessageId
a.sAttachmentId = origmsg.Body.AttachmentId
a.iSize = origmsg.Body.Size
a.sFilename = strings.Trim(contentDisp[strings.Index(contentDisp, "filename=")+len("filename="):], "\"")
a.sMimeType = origmsg.MimeType
gm.lAttachment = append(gm.lAttachment, a)
} else if origmsg.MimeType == "text/plain" {
gm.sBodyText, _ = base64.URLEncoding.DecodeString(origmsg.Body.Data)
} else if origmsg.MimeType == "text/html" {
gm.sBodyHtml, _ = base64.URLEncoding.DecodeString(origmsg.Body.Data)
}
}
func (gs *GmailService) GetMessages() []*GmailMessage {
messages := gs.GetMessagesRaw()
var gmessages []*GmailMessage
for _, ii := range messages {
var m = new(GmailMessage)
m.parseMessagePart(ii.Payload, gs)
gmessages = append(gmessages, m)
}
return gmessages
}
func (gs *GmailService) GetMessagesRaw() []*gmail.Message {
var messages []*gmail.Message
for _, ii := range (gs.GetListOnly().Messages) {
m, err := gs.srv.Users.Messages.Get(gs.sUser, ii.Id).Do()
if err != nil {
log.Fatalf("Unable to retrieve email messages %v", err)
}
messages = append(messages, m)
}
return messages
}
func (gs *GmailService) GetListOnly () *gmail.ListMessagesResponse {
qStr := ""
gsCall := gs.srv.Users.Messages.List(gs.sUser).MaxResults(gs.iMaxResults)
if len(gs.sLabel) > 0 { gsCall = gsCall.LabelIds(gs.sLabel) }
if len(gs.sFrom) > 0 { qStr += "from:" + gs.sFrom + " " }
if len(gs.sTo) > 0 { qStr += "to:" + gs.sTo + " " }
if len(gs.sOlder) > 0 { qStr += "older:" + gs.sOlder + " " }
if len(gs.sNewer) > 0 { qStr += "newer:" + gs.sNewer + " " }
if len(gs.sOlderRel) > 0 { qStr += "older_than:" + gs.sOlderRel + " " }
if len(gs.sNewerRel) > 0 { qStr += "newer_than:" + gs.sNewerRel + " " }
if len(gs.sSubject) > 0 { qStr += "subject:\"" + gs.sSubject + "\" " }
if len(gs.sInPlace) > 0 { qStr += "in:" + gs.sInPlace + " " }
if len(gs.sLarger) > 0 { qStr += "larger:" + gs.sLarger + " " }
if len(gs.sSmaller) > 0 { qStr += "smaller:" + gs.sSmaller + " " }
if len(gs.sFilename) > 0 { qStr += "filename:\"" + gs.sFilename + "\" " }
if len(gs.sMatch) > 0 { qStr += " " + gs.sMatch + " " }
if len(gs.sMatchExact) > 0 { qStr += " " + gs.sMatchExact + " " }
if gs.sHasAttachment { qStr += "has:attachment " }
if len(qStr) > 0 { gsCall = gsCall.Q(qStr) }
do, err := gsCall.Do()
if err != nil {
log.Fatalf("Unable to retrieve email list %v", err)
}
return do
}
type GmailAttachment struct {
gmailService *GmailService
sAttachmentId string
sMessageId string
sFilename string
sMimeType string
sData []byte
iSize int64
bDownloaded bool
}
func (ga *GmailAttachment) GetFilename () string { return ga.sFilename }
func (ga *GmailAttachment) GetMimeType () string { return ga.sMimeType }
func (ga *GmailAttachment) GetSize () int64 { return ga.iSize }
func (ga *GmailAttachment) IsDownloaded () bool { return ga.bDownloaded }
func (ga *GmailAttachment) GetAttachmentId () string { return ga.sAttachmentId }
func (ga *GmailAttachment) GetMessageId () string { return ga.sMessageId }
func (ga *GmailAttachment) GetData() []byte {
if ga.bDownloaded { return ga.sData }
att, err := ga.gmailService.srv.Users.Messages.Attachments.Get("me", ga.sMessageId, ga.sAttachmentId).Do()
if err != nil {
log.Fatalf("Unable to retrieve email messages %v", err)
}
ga.sData, _ = base64.URLEncoding.DecodeString(att.Data)
return ga.sData
}
type GmailMessage struct {
sRaw *gmail.Message
sMessageId string
sSubject string
sBodyHtml []byte
sBodyText []byte
lAttachment []*GmailAttachment
mHeaders map[string]string
mimeFlow []string // multipart/mixed -> multipart/alternative etc
}
func (gm *GmailMessage) HasSubject () bool { return len(gm.sSubject) > 0 }
func (gm *GmailMessage) HasBodyHtml () bool { return len(gm.sBodyHtml) > 0 }
func (gm *GmailMessage) HasBodyText () bool { return len(gm.sBodyText) > 0 }
func (gm *GmailMessage) HasAttachments () bool { return len(gm.lAttachment) > 0 }
func (gm *GmailMessage) GetSubject () string { return gm.sSubject }
func (gm *GmailMessage) GetBodyText () []byte { return gm.sBodyText }
func (gm *GmailMessage) GetBodyHtml () []byte { return gm.sBodyHtml }
func (gm *GmailMessage) GetAttachments () []*GmailAttachment { return gm.lAttachment }
func (gm *GmailMessage) GetRawMessage () *gmail.Message { return gm.sRaw }
func (gm *GmailMessage) GetMessageId () string { return gm.sMessageId }