forked from clementlecorre/mail-to-telegram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.go
48 lines (37 loc) · 894 Bytes
/
fetch.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
package main
import (
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"log"
"time"
)
type IdleMailClient struct {
Client *client.Client
Index uint32
}
func (ec *IdleMailClient) ListenForEmails() {
const tick = time.Second * 15
firstTime := true
for range time.NewTicker(tick).C {
mb, err := ec.Client.Select("INBOX", false)
if err != nil {
log.Fatal("Selecting mailbox error: ", err)
continue
}
if firstTime {
firstTime = false
ec.Index = mb.Messages
log.Println("First time index: ", ec.Index)
}
if mb.Messages <= ec.Index {
continue
}
for ec.Index < mb.Messages {
ec.Index += 1
log.Println("Index update: ", ec.Index)
(&ReadClient{Client: ec.Client}).Read(imap.MailboxStatus{Messages: ec.Index})
//ec.UpdatesCh <- imap.MailboxStatus{Messages: ec.Index}
}
}
log.Fatal("idle main client exited")
}