-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (158 loc) · 4.34 KB
/
main.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
package main
import (
"encoding/xml"
"fmt"
"io"
"log"
"os"
"strings"
"time"
"github.com/docopt/docopt-go"
"golang.org/x/oauth2"
)
const (
usage = `gomuche - Google Mail Unread count checker.
Usage:
gomuche auth [-i <client_id> -s <client_secret>]
gomuche check [-v] [-c <code>]
gomuche -h | --help
gomuche -V | --version
Auth options:
-i --client-id=<clientId> Project Client ID.
-s --client-secret=<clientSecret> Project Client Secret.
Check options:
-c --code=<authCode> Auth code, which can be obtained
through 'gomuche auth' command.
-v --verbose Verbose output. This shows errors
instead of just silently exiting
with error code.
Other options:
-h --help Show this helpful info.
-V --version Show version.
`
version = "0.1.0"
)
const (
mailFeedURL = "https://mail.google.com/mail/feed/atom"
authURL = "https://accounts.google.com/o/oauth2/auth"
tokenURL = "https://www.googleapis.com/oauth2/v4/token"
redirectURL = "urn:ietf:wg:oauth:2.0:oob"
)
// Feed represents Google Mail Atom Feed.
type Feed struct {
Title string `xml:"title"`
Tagline string `xml:"tagline"`
Fullcount int `xml:"fullcount"`
Modified *time.Time `xml:"modified"`
}
func main() {
logFile := getLogFile(true)
defer logFile.Close()
log.SetOutput(logFile)
log.SetFlags(log.Ldate | log.Ltime | log.LUTC | log.Lshortfile)
log.Println("Start.")
// parse args
args, err := docopt.Parse(usage, nil, true, "gomuche "+version, false)
if err != nil {
log.Fatalln("Error parsing arguments:", err)
}
log.Printf("Arguments: %v\n", args)
isVerbose := parseVerbose(args)
if isVerbose || args["auth"] == true {
log.SetOutput(io.MultiWriter(logFile, os.Stdout))
}
// run action
switch {
case args["auth"] == true:
clientID := parseClientID(args)
clientSecret := parseClientSecret(args)
oauth2conf := getOauthConfig(clientID, clientSecret)
authAction(oauth2conf)
case args["check"] == true:
code := parseCode(args)
oauth2conf := getOauthConfig("", "")
checkAction(oauth2conf, code)
default:
log.Fatalln("Action is not defined.")
}
}
func parseCode(args map[string]interface{}) string {
if args["--code"] == nil {
return ""
}
return strings.TrimSpace(args["--code"].(string))
}
func parseVerbose(args map[string]interface{}) bool {
if args["--verbose"] == nil {
return false
}
return args["--verbose"].(bool)
}
func parseClientID(args map[string]interface{}) string {
if args["--client-id"] == nil {
return ""
}
return strings.TrimSpace(args["--client-id"].(string))
}
func parseClientSecret(args map[string]interface{}) string {
if args["--client-secret"] == nil {
return ""
}
return strings.TrimSpace(args["--client-secret"].(string))
}
func checkAction(conf *oauth2.Config, code string) {
var token *oauth2.Token
if code != "" {
token = NewTokenFromCode(conf, code)
} else {
token = NewTokenFromFile()
}
tokenSource := conf.TokenSource(oauth2.NoContext, token)
newToken, err := tokenSource.Token()
if err != nil {
log.Fatalln(err)
}
if newToken.AccessToken != token.AccessToken {
SaveToken(newToken)
log.Println("Saved new token:", newToken.AccessToken)
}
client := oauth2.NewClient(oauth2.NoContext, tokenSource)
resp, err := client.Get(mailFeedURL)
if err != nil {
log.Fatalln("Error fetching mail feed:", err)
}
defer resp.Body.Close()
feed := new(Feed)
err = xml.NewDecoder(resp.Body).Decode(feed)
if err != nil {
log.Fatalln(err)
}
fmt.Println(feed.Fullcount)
}
func authAction(conf *oauth2.Config) {
params := conf.AuthCodeURL("state")
fmt.Printf("Visit the URL for the auth dialog:\n%v\n", params)
}
func getOauthConfig(clientID, clientSecret string) *oauth2.Config {
var cfg *Config
if clientID == "" || clientSecret == "" {
cfg = NewConfigFromFile()
} else {
cfg = NewConfig(clientID, clientSecret)
SaveConfig(cfg)
}
if cfg.ClientID == "" || cfg.ClientSecret == "" {
log.Fatalln("Client ID and secret are not specified.")
}
oauth2conf := &oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Scopes: []string{mailFeedURL},
Endpoint: oauth2.Endpoint{
AuthURL: authURL,
TokenURL: tokenURL,
},
RedirectURL: redirectURL,
}
return oauth2conf
}