-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlook_monitor.go
178 lines (149 loc) · 3.32 KB
/
outlook_monitor.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
package main
import (
"log"
"os"
"time"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/gen2brain/beeep"
)
var (
allIds []uint32
logger *log.Logger
email string
password string
)
type EmailHeader struct {
Sender string
Subject string
}
func setupLogger() *os.File {
f, err := os.OpenFile("log/outlook.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logger.Println(err)
}
logger = log.New(f, "", log.LstdFlags)
return f
}
func addToDatabase(emailMap map[uint32]EmailHeader) {
for id, _ := range emailMap {
allIds = append(allIds, id)
}
}
func clearDatabase() {
allIds = nil
}
func contains(list []uint32, element uint32) bool {
for _, v := range list {
if element == v {
return true
}
}
return false
}
func sendNotifications(emailMap map[uint32]EmailHeader) {
for id, header := range emailMap {
if contains(allIds, id) {
continue
} else {
err := beeep.Notify(header.Sender, header.Subject, "assets/outlook.ico")
if err != nil {
logger.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
}
}
}
func getNameAndEmail(msg *imap.Message) string {
from := msg.Envelope.From[0]
return from.PersonalName + " <" + from.MailboxName + "@" + from.HostName + ">"
}
func connectToMailServer() *client.Client {
logger.Println("Connecting to server...")
// Connect to server
server := "outlook.office365.com:993"
c, err := client.DialTLS(server, nil)
if err != nil {
logger.Fatal(err)
}
return c
}
func getUnreadEmails(c *client.Client) map[uint32]EmailHeader {
// Select INBOX
readonly := true
_, err := c.Select("INBOX", readonly)
if err != nil {
logger.Fatal(err)
}
// Set search criteria
criteria := imap.NewSearchCriteria()
criteria.WithoutFlags = []string{imap.SeenFlag}
ids, err := c.Search(criteria)
if err != nil {
logger.Fatal(err)
}
var headers []EmailHeader
emailMap := make(map[uint32]EmailHeader)
if len(ids) > 0 {
logger.Println("# IDs found:", len(ids), "--", ids)
seqset := new(imap.SeqSet)
seqset.AddNum(ids...)
messages := make(chan *imap.Message, 10)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
}()
for msg := range messages {
h := EmailHeader{
Sender: getNameAndEmail(msg),
Subject: msg.Envelope.Subject,
}
headers = append(headers, h)
}
if err := <-done; err != nil {
logger.Fatal(err)
}
}
if len(ids) != len(headers) {
logger.Fatal("Length of Email IDs and Email Headers are mismatched.")
}
for i := 0; i < len(ids); i++ {
emailMap[ids[i]] = headers[i]
}
return emailMap
}
func checkEmail() {
f := setupLogger()
defer f.Close()
// Connect to Outlook server
c := connectToMailServer()
// Login
err := c.Login(email, password)
if err != nil {
logger.Fatal(err)
}
defer c.Logout()
// Get Unseen Emails
emailMap := getUnreadEmails(c)
if len(emailMap) > 0 {
sendNotifications(emailMap)
addToDatabase(emailMap)
}
}
func main() {
// Set these with `export OUTLOOK_EMAIL=...`
email = os.Getenv("OUTLOOK_EMAIL")
password = os.Getenv("OUTLOOK_PASSWORD")
interval := 30 * time.Second
resetTime := 24 * time.Hour
for i := 0; ; i++ {
checkEmail()
time.Sleep(interval)
// Empty database every 24 hours
if time.Duration(i)*interval > resetTime {
clearDatabase()
i = 0
}
}
}