refactoring of code to make it more readable

This commit is contained in:
Michael 2022-05-07 03:57:16 +01:00
parent c4a60e0796
commit e516bab46a
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C
4 changed files with 46 additions and 67 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ logs
msgs
config.yaml
go.sum
.vscode

View File

@ -10,23 +10,20 @@ import (
// Config application configuration
type Config struct {
Path string `yaml:"path"`
Account []struct {
Addr string `yaml:"addr"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
Proxy struct {
Enabled bool `yaml:"enabled"`
Addr string `yaml:"addr"`
Proto string `yaml:"proto"`
}
Logger struct {
Account []Account
Logger struct {
Path string `yaml:"path"`
Mode string `yaml:"mode"`
Level int `yaml:"level"`
}
}
type Account struct {
Addr string `yaml:"addr"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// NewConfig - create new config instance from file path
func NewConfig(configFile string) (config *Config) {
file, err := ioutil.ReadFile(configFile)

View File

@ -44,11 +44,7 @@ func LoginWithProxy(conn Conn, network string, proxyAddress string, auth *proxy.
if err != nil {
return nil, err
}
mail, err = login(conn, client)
if err != nil {
return nil, err
}
return mail, nil
return login(conn, client)
}
func Login(conn Conn) (mail *Mail, err error) {
@ -56,11 +52,7 @@ func Login(conn Conn) (mail *Mail, err error) {
if err != nil {
return nil, err
}
mail, err = login(conn, client)
if err != nil {
return nil, err
}
return mail, nil
return login(conn, client)
}
func (mail *Mail) ListMailboxes() {
@ -109,10 +101,7 @@ func (mail *Mail) GetMessages(mailbox string, path string) error {
return err
}
}
if err := <-done; err != nil {
return err
}
return nil
return <-done
}
func Write(path string, msg *imap.Message, literal imap.Literal) error {

74
main.go
View File

@ -13,53 +13,37 @@ import (
func main() {
cfgFile, mailbox, list := ui()
cfg := NewConfig(*cfgFile)
chErr, chDone := make(chan error), make(chan bool)
ch := make(chan error)
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)
go getmail(account, cfg.Path, *mailbox, *list, ch)
}
for c := 0; c < len(cfg.Account); {
select {
case err := <-chErr:
log.Println(err)
case <-chDone:
c++
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])
@ -76,3 +60,11 @@ func ui() (cfgFile, mailbox *string, list *bool) {
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)
}
}
}