This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
350 lines (322 loc) · 7.21 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"encoding/json"
"fmt"
"github.com/WAAutoMaton/telegram-mail-bot/imap"
"github.com/WAAutoMaton/telegram-mail-bot/smtp"
"io/ioutil"
"log"
"strconv"
"strings"
"time"
tb "gopkg.in/tucnak/telebot.v2"
)
type Config struct {
Token string
IMAPServer string
SMTPServer string
SMTPHost string
Username string
Password string
UID int
}
func initBotAPI(token string) (bot *tb.Bot) {
b, err := tb.NewBot(tb.Settings{
// You can also set custom API URL.
// If field is empty it equals to "https://api.telegram.org".
Token: token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Fatal(err)
return
}
return b
}
func readConfig() *Config {
b, err := ioutil.ReadFile("config/config.json")
if err != nil {
panic(err)
}
config := &Config{}
err = json.Unmarshal(b, config)
if err != nil {
panic(err)
}
return config
}
type DraftMail struct {
isActive bool
to string
subject string
body string
}
func main() {
config := readConfig()
b := initBotAPI(config.Token)
c := imap.NewClient(config.IMAPServer, config.Username, config.Password)
err := c.Login()
if err != nil {
log.Fatal(err)
}
smtpClient := smtp.NewClient(config.SMTPServer, config.SMTPHost, config.Username, config.Password)
b.Handle("/ping", func(m *tb.Message) {
if !m.Private() {
return
}
_, err := b.Send(m.Sender, "Pong!")
logError(err)
})
b.Handle("/help", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
_, err := b.Send(m.Sender, `Command List:
/help show this message.
/inbox show the number of mails in INBOX.
/mail <ID:int> get a mail (ID 1 is the latest mail.)
/new create a new draft mail.`)
logError(err)
})
b.Handle("/inbox", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
t, err := c.PullMailCount()
text := fmt.Sprintf("There are %v mails in INBOX", strconv.Itoa(t))
if err != nil {
text = fmt.Sprintf("Catched an error while trying to pull INBOX, error message: %v", err.Error())
}
_, err = b.Send(m.Sender, text)
logError(err)
})
b.Handle("/mail", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
var x int
_, err := fmt.Sscanf(m.Payload, "%v", &x)
if err != nil {
b.Send(m.Sender, "Use /mail <ID:int> to get a mail ( ID 1 is the latest mail )")
return
}
n, err := c.PullMailCount()
if err != nil {
b.Send(m.Sender, "Catched an error, error message: "+err.Error())
return
}
text, _, err := c.ReadMail(n - x + 1)
if err != nil {
b.Send(m.Sender, "Catched an error, error message: "+err.Error())
return
}
_, err = b.Send(m.Sender, omitText(text))
logError(err)
})
draftMail := DraftMail{isActive: false}
b.Handle("/new", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
if draftMail.isActive {
_, err := b.Send(m.Sender, "Please use /cancel to delete last draft mail first.")
logError(err)
return
}
draftMail = DraftMail{isActive: true}
_, err := b.Send(m.Sender,
`A new draft mail has been created.
Plase use:
/to <receiver's mail :string>
/subject <string>
/body <string>
to fill the mail.
And then use /send to send it.
Or use /cancel to delete the draft mail.`)
logError(err)
})
b.Handle("/cancel", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
var err error
if draftMail.isActive {
draftMail.isActive = false
_, err = b.Send(m.Sender, "Delete draft mail successfully.")
} else {
_, err = b.Send(m.Sender, "No draft mail should be deleted.")
}
logError(err)
})
b.Handle("/to", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
if !draftMail.isActive {
_, err := b.Send(m.Sender, "No draft mail.\nPlease use /new to create one.")
logError(err)
return
}
to := strings.Trim(m.Payload, " \r\n\t")
draftMail.to = to
_, err := b.Send(m.Sender, "Succeed.")
logError(err)
})
b.Handle("/subject", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
if !draftMail.isActive {
_, err := b.Send(m.Sender, "No draft mail.\nPlease use /new to create one.")
logError(err)
return
}
subject := strings.Trim(m.Payload, " \r\n\t")
draftMail.subject = subject
_, err := b.Send(m.Sender, "Succeed.")
logError(err)
})
b.Handle("/body", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
if !draftMail.isActive {
_, err := b.Send(m.Sender, "No draft mail.\nPlease use /new to create one.")
logError(err)
return
}
body := m.Text
if len(body) < 5 {
_, err := b.Send(m.Sender, "Empty body!")
logError(err)
return
}
body = body[5:]
draftMail.body = body
log.Println(draftMail.body)
_, err := b.Send(m.Sender, "Succeed.")
logError(err)
})
b.Handle("/send", func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
if !draftMail.isActive {
_, err := b.Send(m.Sender, "No draft mail.\nPlease use /new to create one.")
logError(err)
return
}
if len(draftMail.body) == 0 {
_, err := b.Send(m.Sender, "Body is empty!")
logError(err)
return
}
if len(draftMail.subject) == 0 {
_, err := b.Send(m.Sender, "Subject is empty!")
logError(err)
return
}
if len(draftMail.to) == 0 {
_, err := b.Send(m.Sender, "Receiver's mail address is empty!")
logError(err)
return
}
err := smtpClient.Send([]string{draftMail.to}, draftMail.subject, draftMail.body)
if err != nil {
_, err = b.Send(m.Sender, "Catched an error while sending mail: "+err.Error())
} else {
_, err = b.Send(m.Sender, "Succeed.")
}
logError(err)
draftMail.isActive = false
})
b.Handle(tb.OnText, func(m *tb.Message) {
if !m.Private() {
return
}
if m.Sender.ID != config.UID {
b.Send(m.Sender, "You aren't my admin!")
return
}
log.Println(m.Text)
})
_, err = c.UpdateMailCount()
logError(err)
go listen(c, b, config.UID)
b.Start()
}
func listen(c *imap.Client, b *tb.Bot, uid int) {
user := &tb.User{ID: uid, IsBot: false}
d := time.Duration(time.Minute * 4)
t := time.NewTicker(d)
defer t.Stop()
for {
<-t.C
l, r, err := c.GetNewMailRanges()
if err != nil {
log.Println(err)
}
if l == 0 {
continue
}
for i := l; i <= r; i++ {
text, _, err := c.ReadMail(i)
if err != nil {
log.Println(err)
} else {
_, err = b.Send(user, omitText(text))
if err != nil {
log.Println(err)
}
}
}
}
}
func omitText(text string) string {
if len(text) > 4000 {
return text[0:4000] + "\n<--Omit the following-->"
} else {
return text
}
}
func logError(e error) {
if e != nil {
log.Println(e)
}
}