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