asynchronous operating for requesting mail

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2021-08-05 15:48:28 +01:00
parent 2ba4e0eddc
commit d98c3c539e

45
main.go
View File

@ -13,23 +13,36 @@ func main() {
cfgFile, mailbox, msgs := ui()
cfg := NewConfig(*cfgFile)
logger := logger.New(cfg.Logger.Path, cfg.Logger.Mode, cfg.Logger.Level)
chErr, chDone := make(chan error), make(chan bool)
for _, account := range cfg.Account {
conn, err := mail.Login(cfg.Addr, account.Username, account.Password)
if err != nil {
logger.Error.Fatal(err)
}
defer conn.Client.Logout()
messages, err := conn.GetMessages(*mailbox, int32(*msgs))
if err != nil {
logger.Error.Fatal(err)
}
path := "./msgs/" + account.Username
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0775)
}
if err := mail.WriteMessages(path, messages); err != nil {
logger.Error.Fatal(err)
go func(username, password string, chErr chan<- error, chDone chan<- bool) {
defer func() {
chDone <- true
}()
conn, err := mail.Login(cfg.Addr, username, password)
if err != nil {
chErr <- err
}
defer conn.Client.Logout()
messages, err := conn.GetMessages(*mailbox, int32(*msgs))
if err != nil {
chErr <- err
}
path := "./msgs/" + username
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0775)
}
if err := mail.WriteMessages(path, messages); err != nil {
chErr <- err
}
}(account.Username, account.Password, chErr, chDone)
}
for c := 0; c < len(cfg.Account); {
select {
case err := <-chErr:
logger.Error.Error(err)
case <-chDone:
c++
}
}
}