79 lines
1.8 KiB
Go
79 lines
1.8 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)
|
|
chErr, chDone := make(chan error), make(chan bool)
|
|
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)
|
|
}
|
|
for c := 0; c < len(cfg.Account); {
|
|
select {
|
|
case err := <-chErr:
|
|
log.Println(err)
|
|
case <-chDone:
|
|
c++
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|