diff --git a/.gitignore b/.gitignore index a257473..9aae355 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ gomail logs msgs config.yaml -go.sum \ No newline at end of file +go.sum +.vscode \ No newline at end of file diff --git a/config.go b/config.go index 133907c..3186511 100644 --- a/config.go +++ b/config.go @@ -10,23 +10,20 @@ import ( // Config application configuration type Config struct { Path string `yaml:"path"` - Account []struct { - Addr string `yaml:"addr"` - Username string `yaml:"username"` - Password string `yaml:"password"` - } - Proxy struct { - Enabled bool `yaml:"enabled"` - Addr string `yaml:"addr"` - Proto string `yaml:"proto"` - } - Logger struct { + Account []Account + Logger struct { Path string `yaml:"path"` Mode string `yaml:"mode"` Level int `yaml:"level"` } } +type Account struct { + Addr string `yaml:"addr"` + Username string `yaml:"username"` + Password string `yaml:"password"` +} + // NewConfig - create new config instance from file path func NewConfig(configFile string) (config *Config) { file, err := ioutil.ReadFile(configFile) diff --git a/mail/mail.go b/mail/mail.go index 2810d2a..4360e84 100644 --- a/mail/mail.go +++ b/mail/mail.go @@ -44,11 +44,7 @@ func LoginWithProxy(conn Conn, network string, proxyAddress string, auth *proxy. if err != nil { return nil, err } - mail, err = login(conn, client) - if err != nil { - return nil, err - } - return mail, nil + return login(conn, client) } func Login(conn Conn) (mail *Mail, err error) { @@ -56,11 +52,7 @@ func Login(conn Conn) (mail *Mail, err error) { if err != nil { return nil, err } - mail, err = login(conn, client) - if err != nil { - return nil, err - } - return mail, nil + return login(conn, client) } func (mail *Mail) ListMailboxes() { @@ -109,10 +101,7 @@ func (mail *Mail) GetMessages(mailbox string, path string) error { return err } } - if err := <-done; err != nil { - return err - } - return nil + return <-done } func Write(path string, msg *imap.Message, literal imap.Literal) error { diff --git a/main.go b/main.go index feba0ec..688f78d 100644 --- a/main.go +++ b/main.go @@ -13,53 +13,37 @@ import ( func main() { cfgFile, mailbox, list := ui() cfg := NewConfig(*cfgFile) - chErr, chDone := make(chan error), make(chan bool) + ch := make(chan error) for _, account := range cfg.Account { - go func(addr, username, password string, chErr chan<- error, chDone chan<- bool) { - defer func() { - chDone <- true - }() - auth := mail.Conn{Addr: addr, Username: username, Password: password} - var conn *mail.Mail - var err error - if cfg.Proxy.Enabled { - conn, err = mail.LoginWithProxy(auth, cfg.Proxy.Proto, cfg.Proxy.Addr, nil) - if err != nil { - chErr <- err - } - } else { - conn, err = mail.Login(auth) - if err != nil { - chErr <- err - } - } - defer conn.Client.Logout() - - if !*list { - path := cfg.Path + username + "/" + time.Now().Format("2006-01-02-15:04:05") - for _, p := range []string{cfg.Path + username, path} { - if _, err := os.Stat(p); os.IsNotExist(err) { - os.Mkdir(p, 0775) - } - } - if err = conn.GetMessages(*mailbox, path); err != nil { - chErr <- err - } - return - } - conn.ListMailboxes() - }(account.Addr, account.Username, account.Password, chErr, chDone) + go getmail(account, cfg.Path, *mailbox, *list, ch) } - for c := 0; c < len(cfg.Account); { - select { - case err := <-chErr: - log.Println(err) - case <-chDone: - c++ + for c := 0; c < len(cfg.Account); c++ { + if <-ch != nil { + log.Println(<-ch) } } } +func getmail(account Account, ph, mailbox string, list bool, ch chan error) { + conn, err := mail.Login(mail.Conn{Addr: account.Addr, Username: account.Username, Password: account.Password}) + if err != nil { + ch <- err + return + } + defer conn.Client.Logout() + if !list { + path := ph + account.Username + "/" + time.Now().Format("2006-01-02-15:04:05") + makePath(ph+account.Username, path) + if err = conn.GetMessages(mailbox, path); err != nil { + ch <- err + } + ch <- nil + return + } + conn.ListMailboxes() + ch <- nil +} + func ui() (cfgFile, mailbox *string, list *bool) { flag.Usage = func() { fmt.Printf("Usage of %s:\n", os.Args[0]) @@ -76,3 +60,11 @@ func ui() (cfgFile, mailbox *string, list *bool) { flag.Parse() return cfgFile, mailbox, list } + +func makePath(paths ...string) { + for _, p := range paths { + if _, err := os.Stat(p); os.IsNotExist(err) { + os.Mkdir(p, 0775) + } + } +}