1
0
Fork 0
mirror of https://github.com/Luzifer/automail.git synced 2024-12-20 13:01:20 +00:00

Fix: Reconnect after connection loss

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-06-28 17:51:52 +02:00
parent e2b309056f
commit 18cf6e9961
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

56
main.go
View file

@ -66,33 +66,51 @@ func main() {
log.WithError(err).Fatal("Unable to load storage file")
}
imapClient, err := client.DialTLS(fmt.Sprintf("%s:%d", cfg.IMAPHost, cfg.IMAPPort), nil)
if err != nil {
log.WithError(err).Fatal("Unable to connect to IMAP server")
}
defer imapClient.Close()
if err = imapClient.Login(cfg.IMAPUser, cfg.IMAPPass); err != nil {
log.WithError(err).Fatal("Unable to login to IMAP server")
}
log.Info("IMAP connected and logged in")
if _, err = imapClient.Select(cfg.Mailbox, false); err != nil {
log.WithError(err).Fatal("Unable to select mailbox")
}
var (
messages = make(chan *imap.Message, 1000)
sigs = make(chan os.Signal)
ticker = time.NewTicker(cfg.FetchInterval)
imapClient *client.Client
messages = make(chan *imap.Message, 1000)
needLogin = make(chan struct{}, 1)
sigs = make(chan os.Signal)
ticker = time.NewTicker(cfg.FetchInterval)
)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
needLogin <- struct{}{}
for {
select {
case <-needLogin:
if imapClient != nil {
imapClient.Close()
}
imapClient, err = client.DialTLS(fmt.Sprintf("%s:%d", cfg.IMAPHost, cfg.IMAPPort), nil)
if err != nil {
log.WithError(err).Fatal("Unable to connect to IMAP server")
}
if err = imapClient.Login(cfg.IMAPUser, cfg.IMAPPass); err != nil {
log.WithError(err).Fatal("Unable to login to IMAP server")
}
log.Info("IMAP connected and logged in")
if _, err = imapClient.Select(cfg.Mailbox, false); err != nil {
log.WithError(err).Fatal("Unable to select mailbox")
}
go func() {
// Trigger re-login when log-out was received
<-imapClient.LoggedOut()
needLogin <- struct{}{}
}()
case <-ticker.C:
if _, err := imapClient.Select(cfg.Mailbox, false); err != nil {
log.WithError(err).Error("Unable to select mailbox")
continue
}
seq, err := imap.ParseSeqSet(fmt.Sprintf("%d:*", store.LastUID+1))
if err != nil {
log.WithError(err).Error("Unable to parse sequence set")