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 17:11:43 +00:00
|
|
|
cfgFile, mailbox := ui()
|
2021-07-03 00:08:46 +00:00
|
|
|
cfg := NewConfig(*cfgFile)
|
2021-08-05 14:48:28 +00:00
|
|
|
chErr, chDone := make(chan error), make(chan bool)
|
2021-07-22 00:49:45 +00:00
|
|
|
for _, account := range cfg.Account {
|
2021-08-29 22:11:59 +00:00
|
|
|
go func(addr, username, password string, chErr chan<- error, chDone chan<- bool) {
|
2021-08-05 14:48:28 +00:00
|
|
|
defer func() {
|
|
|
|
chDone <- true
|
|
|
|
}()
|
2021-12-10 04:26:47 +00:00
|
|
|
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
|
|
|
|
}
|
2021-08-05 14:48:28 +00:00
|
|
|
}
|
|
|
|
defer conn.Client.Logout()
|
2021-08-24 22:50:29 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-08-05 14:48:28 +00:00
|
|
|
}
|
2022-01-20 17:11:43 +00:00
|
|
|
if err = conn.GetMessages(*mailbox, path); err != nil {
|
2021-08-05 14:48:28 +00:00
|
|
|
chErr <- err
|
|
|
|
}
|
2021-08-29 22:11:59 +00:00
|
|
|
}(account.Addr, account.Username, account.Password, chErr, chDone)
|
2021-08-05 14:48:28 +00:00
|
|
|
}
|
|
|
|
for c := 0; c < len(cfg.Account); {
|
|
|
|
select {
|
|
|
|
case err := <-chErr:
|
2021-11-17 23:12:19 +00:00
|
|
|
log.Println(err)
|
2021-08-05 14:48:28 +00:00
|
|
|
case <-chDone:
|
|
|
|
c++
|
2021-07-22 00:49:45 +00:00
|
|
|
}
|
2021-07-02 20:09:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:11:43 +00:00
|
|
|
func ui() (cfgFile, mailbox *string) {
|
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")
|
|
|
|
|
|
|
|
flag.Parse()
|
2022-01-20 17:11:43 +00:00
|
|
|
return cfgFile, mailbox
|
2021-07-02 20:09:38 +00:00
|
|
|
}
|