-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailHandler.go
210 lines (189 loc) · 5.32 KB
/
emailHandler.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
package main
import (
"fmt"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"strings"
"time"
)
//EmailBoxHandler used for handling email checks and sending notifications to user
type EmailBoxHandler struct {
eAccount *StoredEmailAccount
user *StoredUser
lastMsgId uint32
lastMsgTime int64
isRestart bool
connectionOk bool
imapRetriesCounter int
authRetriesCounter int
stop chan struct{}
}
func NewEmailBoxHandler(eAccount *StoredEmailAccount, user *StoredUser) *EmailBoxHandler {
return &EmailBoxHandler{
eAccount: eAccount,
user: user,
lastMsgId: 0,
lastMsgTime: time.Now().Unix(),
stop: make(chan struct{}, 1),
}
}
func (handler *EmailBoxHandler) StartFetchingEmails() {
errMsg := ""
handler.FetchNewEmails()
if !handler.connectionOk {
handler.eAccount.isActive = false
return
}
ticker := time.NewTicker(time.Duration(handler.eAccount.updateT) * time.Minute)
MSGGETTINGLOOP:
for {
select {
case <-handler.stop:
if !handler.isRestart {
errMsg = "Stopped fetching emails for " + handler.eAccount.login
} else {
handler.isRestart = false
}
break MSGGETTINGLOOP
case <-ticker.C:
if !handler.eAccount.isActive {
handler.stop <- struct{}{}
} else {
handler.FetchNewEmails()
}
}
}
ticker.Stop()
log.Println("Stopped worker", handler)
if errMsg != "" {
handler.SendMessageToUser(errMsg)
}
}
func (handler *EmailBoxHandler) FetchNewEmails() {
// Connect to server
c, err := client.DialTLS(handler.eAccount.imapHost, nil)
errMsg := ""
if err != nil {
log.Printf("Error connecting to imap server %s. %v", handler.eAccount.imapHost, err)
handler.imapRetriesCounter += 1
if handler.imapRetriesCounter == 3 {
handler.eAccount.isActive = false
errMsg = fmt.Sprintf("Error connecting to imap server: %s after %d retries",
handler.eAccount.imapHost,
handler.imapRetriesCounter)
handler.SendMessageToUser(errMsg)
handler.connectionOk = false
}
return
}
handler.imapRetriesCounter = 0
// Don't forget to logout
defer c.Logout()
// Login
if err := c.Login(handler.eAccount.login, handler.eAccount.password); err != nil {
log.Printf("Error authenticating in account %s. %v", handler.eAccount.login, err)
handler.authRetriesCounter += 1
if handler.authRetriesCounter == 3 {
handler.eAccount.isActive = false
errMsg = fmt.Sprintf("Error authenticating in account: %s after %d retries",
handler.eAccount.login,
handler.authRetriesCounter)
handler.connectionOk = false
handler.SendMessageToUser(errMsg)
}
return
}
handler.authRetriesCounter = 0
if !handler.connectionOk {
handler.connectionOk = true
uMsg := fmt.Sprintf("Successfully connected to mailbox for %s", handler.eAccount.login)
handler.SendMessageToUser(uMsg)
}
// Select INBOX
mbox, err := c.Select("INBOX", false)
if err != nil {
log.Fatal(err)
}
fmt.Println("Fetching emails")
//always get last 20 messages and compare time with of last known
from := mbox.Messages - 19
to := mbox.Messages
seqset := new(imap.SeqSet)
seqset.AddRange(from, to)
messages := make(chan *imap.Message, 100)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
}()
for msg := range messages {
msgTime := msg.Envelope.Date.Unix()
if msgTime <= handler.lastMsgTime {
continue
}
handler.lastMsgTime = msgTime
if handler.CheckPatterns(msg) {
newUserMsg := fmt.Sprintf("At: %s\n", msg.Envelope.Date.Format("2006-01-02 15:04:05"))
newUserMsg += fmt.Sprintf("Account: %s\n", handler.eAccount.login)
newUserMsg += fmt.Sprintf("From: %s\n", msg.Envelope.From[0].PersonalName)
newUserMsg += fmt.Sprintf("Subject: %s\n", msg.Envelope.Subject)
handler.SendMessageToUser(newUserMsg)
}
}
if err := <-done; err != nil {
newUserMsg := "Error getting emails: " + err.Error()
handler.SendMessageToUser(newUserMsg)
}
handler.lastMsgId = mbox.Messages
}
func (handler *EmailBoxHandler) CheckPatterns(msg *imap.Message) bool {
sendEmail := false
if len(handler.user.Patterns) == 0 {
return true
}
msgSubj := strings.ToLower(msg.Envelope.Subject)
for _, uPattern := range handler.user.Patterns {
if uPattern.Subject != "" {
if strings.Contains(msgSubj, uPattern.Subject) {
sendEmail = true
break
}
}
if uPattern.FromEmail != "" || uPattern.FromPersonalName != "" {
for _, sAddr := range msg.Envelope.From {
msgMaiBoxes := strings.ToLower(sAddr.MailboxName)
msgPerson := strings.ToLower(sAddr.PersonalName)
if msgMaiBoxes == uPattern.FromEmail {
sendEmail = true
break
}
if msgPerson == uPattern.FromPersonalName {
sendEmail = true
break
}
}
if sendEmail {
break
}
}
}
return sendEmail
}
func (handler *EmailBoxHandler) Restart() {
handler.isRestart = true
handler.stop <- struct{}{}
handler.eAccount.isActive = true
go handler.StartFetchingEmails()
}
func (handler *EmailBoxHandler) Stop() {
handler.eAccount.isActive = false
handler.stop <- struct{}{}
}
func (handler *EmailBoxHandler) SendMessageToUser(nMsg string) {
msg := tgbotapi.NewMessage(handler.user.ChatID, nMsg)
_, err := bot.Send(msg)
if err != nil {
log.Println("Error sending message to user. ", err)
}
}