package main import ( "flag" "fmt" "log" "os" "time" "git.0cd.xyz/michael/gomail/mail" "git.0cd.xyz/michael/gtools/logger" ) 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 { 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 := 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 := 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++ } } } func ui() (cfgFile, mailbox *string, msgs *int) { flag.Usage = func() { fmt.Printf("Usage of %s:\n", os.Args[0]) flag.PrintDefaults() } home, ok := os.LookupEnv("HOME") if !ok { log.Fatal("Unable to find home directory") } cfgFile = flag.String("cfgFile", home+"/.config/gomail/config.yaml", "path to config file") mailbox = flag.String("mailbox", "inbox", "mailbox to scan messages") msgs = flag.Int("msgs", 100, "Number of messages to pull") flag.Parse() return cfgFile, mailbox, msgs }