gomail/main.go

53 lines
1.0 KiB
Go

package main
import (
"log"
"os"
"time"
"git.0cd.xyz/michael/gomail/mail"
"git.0cd.xyz/michael/gomail/ui"
)
func main() {
cmd := ui.New()
cfg := NewConfig(cmd.ConfigFile)
ch := make(chan error)
for _, account := range cfg.Account {
go getmail(account, cfg.Path, cmd.Mailbox, cmd.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 makePath(paths ...string) {
for _, p := range paths {
if _, err := os.Stat(p); os.IsNotExist(err) {
os.Mkdir(p, 0775)
}
}
}