-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgmail2go.go
147 lines (138 loc) · 3.56 KB
/
gmail2go.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
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"strings"
"github.com/rif/gmail2go/passwd"
"github.com/rif/gmail2go/rss"
)
var (
config = flag.String("config", path.Join(os.Getenv("HOME"), ".gmail2gorc"), "the user accounts file")
account = flag.String("account", "", "adds/updates/deletes user:password to the accounts file (leave password empty to delete)")
color = flag.Bool("color", false, "use terminal output colors")
notify = flag.Bool("notify", false, "send libnotify message")
accountsMap = make(map[string]string)
)
func main() {
flag.Parse()
fin, err := os.Open(*config)
if err == nil {
// if the config file was opened decrypt it and unmarshal accounts map
r := bufio.NewReader(fin)
res, err := passwd.Decrypt(r, make([]byte, 16), make([]byte, 16))
if err != nil {
log.Fatal("Could not decrypt accounts file: ", err)
} else {
dec := json.NewDecoder(res)
if err := dec.Decode(&accountsMap); err != nil {
log.Fatal("Could not decode accounts content to json: ", err)
}
}
}
// if account parameter has a value operate the changes on the map and write the encrypted json
if *account != "" {
fin, err = os.Create(*config)
if err != nil {
log.Fatal("Cannot open account file: ", err)
}
defer fin.Close()
w := bufio.NewWriter(fin)
up := strings.SplitN(*account, ":", 2)
if len(up) != 2 {
log.Fatal("Incorrect set string use user:pass")
}
if up[1] != "" {
accountsMap[up[0]] = up[1]
} else {
delete(accountsMap, up[0])
}
var out bytes.Buffer
enc := json.NewEncoder(&out)
err := enc.Encode(accountsMap)
if err != nil {
log.Fatal("Could not encode accounts to json", err)
} else {
err = passwd.Encrypt(w, &out, make([]byte, 16), make([]byte, 16))
if err != nil {
log.Fatal("Could not encrypt accounts json string to file", err)
}
w.Flush()
}
}
// prepare the colors for display
yellow, green, red, reset := "", "", "", ""
if *color {
yellow, green, red, reset = "\033[1;33m", "\033[0;32m", "\033[0:31m", "\033[0m"
}
emailCountMap := make(map[string][]string)
// show accounts
index := 0
for user, _ := range accountsMap {
if index > 0 {
fmt.Print(green + " | ")
}
fmt.Print(yellow + user)
index++
}
fmt.Println(reset + "\n")
// show unread mails
for user, pass := range accountsMap {
mails, err := rss.Read("https://mail.google.com/mail/feed/atom", user, pass)
if err != nil {
fmt.Println(reset)
log.Print(user+" error: ", err)
continue
}
// iterate over mails
for _, m := range mails {
fmt.Println(yellow+"["+user+"] "+red, m.Author, ": "+reset, m.Title, "\n\t\t", m.Summary)
emailCountMap[user] = append(emailCountMap[user], m.Title)
}
}
// show the notification
if len(emailCountMap) > 0 {
if *notify {
message := ""
for k, v := range emailCountMap {
// pute entire title if only one message
info := fmt.Sprintf("%d", len(v))
if len(v) == 1 {
info = v[0]
}
message += fmt.Sprintf("%v(%v), ", k, info)
}
message = strings.TrimRight(message, ", ")
cmd := exec.Command("/usr/bin/notify-send",
"-i",
"/usr/share/notify-osd/icons/gnome/scalable/status/notification-message-email.svg",
"-a",
"gmail2go",
"You have new mails:",
message)
cmd.Run()
}
os.Exit(0)
}
os.Exit(1)
}
func isPressent(mp map[string]int, searched string) bool {
for k := range mp {
if k == searched {
return true
}
}
return false
}
func getKeyList(mp map[string]int) (keys []string) {
for k := range mp {
keys = append(keys, k)
}
return
}