71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"git.0cd.xyz/michael/gomail/mail"
|
|
)
|
|
|
|
func main() {
|
|
cfgFile, mailbox, list := ui()
|
|
cfg := NewConfig(*cfgFile)
|
|
ch := make(chan error)
|
|
for _, account := range cfg.Account {
|
|
go getmail(account, cfg.Path, *mailbox, *list, ch)
|
|
}
|
|
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])
|
|
flag.PrintDefaults()
|
|
}
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
cfgFile = flag.String("cfgFile", home+"/.config/gomail/config.yaml", "path to config file")
|
|
mailbox = flag.String("mailbox", "inbox", "mailbox to scan messages")
|
|
list = flag.Bool("list", false, "list all mailboxes")
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|