2021-07-02 20:09:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-08-24 22:50:29 +00:00
|
|
|
"log"
|
2021-07-02 20:09:38 +00:00
|
|
|
"os"
|
2021-08-24 22:50:29 +00:00
|
|
|
"time"
|
2021-07-02 20:09:38 +00:00
|
|
|
|
|
|
|
"git.0cd.xyz/michael/gomail/mail"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-01-20 19:19:04 +00:00
|
|
|
cfgFile, mailbox, list := ui()
|
2021-07-03 00:08:46 +00:00
|
|
|
cfg := NewConfig(*cfgFile)
|
2022-05-07 02:57:16 +00:00
|
|
|
ch := make(chan error)
|
2021-07-22 00:49:45 +00:00
|
|
|
for _, account := range cfg.Account {
|
2022-05-07 02:57:16 +00:00
|
|
|
go getmail(account, cfg.Path, *mailbox, *list, ch)
|
|
|
|
}
|
|
|
|
for c := 0; c < len(cfg.Account); c++ {
|
|
|
|
if <-ch != nil {
|
|
|
|
log.Println(<-ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 19:19:04 +00:00
|
|
|
|
2022-05-07 02:57:16 +00:00
|
|
|
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
|
2021-08-05 14:48:28 +00:00
|
|
|
}
|
2022-05-07 02:57:16 +00:00
|
|
|
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
|
2021-07-22 00:49:45 +00:00
|
|
|
}
|
2022-05-07 02:57:16 +00:00
|
|
|
ch <- nil
|
|
|
|
return
|
2021-07-02 20:09:38 +00:00
|
|
|
}
|
2022-05-07 02:57:16 +00:00
|
|
|
conn.ListMailboxes()
|
|
|
|
ch <- nil
|
2021-07-02 20:09:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 19:19:04 +00:00
|
|
|
func ui() (cfgFile, mailbox *string, list *bool) {
|
2021-07-02 20:09:38 +00:00
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Printf("Usage of %s:\n", os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
2021-08-29 22:11:59 +00:00
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2021-08-24 22:50:29 +00:00
|
|
|
}
|
|
|
|
cfgFile = flag.String("cfgFile", home+"/.config/gomail/config.yaml", "path to config file")
|
2021-07-02 20:09:38 +00:00
|
|
|
mailbox = flag.String("mailbox", "inbox", "mailbox to scan messages")
|
2022-01-20 19:19:04 +00:00
|
|
|
list = flag.Bool("list", false, "list all mailboxes")
|
2021-07-02 20:09:38 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
2022-01-20 19:19:04 +00:00
|
|
|
return cfgFile, mailbox, list
|
2021-07-02 20:09:38 +00:00
|
|
|
}
|
2022-05-07 02:57:16 +00:00
|
|
|
|
|
|
|
func makePath(paths ...string) {
|
|
|
|
for _, p := range paths {
|
|
|
|
if _, err := os.Stat(p); os.IsNotExist(err) {
|
|
|
|
os.Mkdir(p, 0775)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|