asynchronous operating for requesting mail
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
2ba4e0eddc
commit
d98c3c539e
25
main.go
25
main.go
@ -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)
|
||||
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 {
|
||||
logger.Error.Fatal(err)
|
||||
chErr <- err
|
||||
}
|
||||
defer conn.Client.Logout()
|
||||
messages, err := conn.GetMessages(*mailbox, int32(*msgs))
|
||||
if err != nil {
|
||||
logger.Error.Fatal(err)
|
||||
chErr <- err
|
||||
}
|
||||
path := "./msgs/" + account.Username
|
||||
path := "./msgs/" + 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)
|
||||
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++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user