From b5d5e0dbedee34bd5d3edf13616f055d4f227d36 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 9 Jan 2018 21:24:50 -0500 Subject: Parse account configuration --- cmd/aerc/main.go | 27 ++++++++++++++++++--------- config/config.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ worker/imap/worker.go | 6 +++--- worker/worker.go | 20 +++++++++++++++++--- 4 files changed, 83 insertions(+), 15 deletions(-) diff --git a/cmd/aerc/main.go b/cmd/aerc/main.go index 2627a71..d5d0897 100644 --- a/cmd/aerc/main.go +++ b/cmd/aerc/main.go @@ -10,19 +10,28 @@ import ( func main() { var ( - c *config.AercConfig - err error + conf *config.AercConfig + err error ) - if c, err = config.LoadConfig(nil); err != nil { + if conf, err = config.LoadConfig(nil); err != nil { panic(err) } - fmt.Printf("%+v\n", *c) - w := worker.NewWorker("") - go w.Run() - w.PostAction(types.Ping{}) + workers := make([]worker.Worker, 0) + for _, account := range conf.Accounts { + var work worker.Worker + if work, err = worker.NewWorker(account.Source); err != nil { + panic(err) + } + fmt.Printf("Initializing worker %s\n", account.Name) + go work.Run() + work.PostAction(types.Configure{Config: account}) + workers = append(workers, work) + } for { - if msg := w.GetMessage(); msg != nil { - fmt.Printf("<- %T: %v\n", msg, msg) + for _, worker := range workers { + if msg := worker.GetMessage(); msg != nil { + fmt.Printf("<- %T\n", msg) + } } } } diff --git a/config/config.go b/config/config.go index 6a420fa..a7e9d84 100644 --- a/config/config.go +++ b/config/config.go @@ -4,7 +4,9 @@ import ( "github.com/go-ini/ini" "github.com/kyoh86/xdg" + "fmt" "path" + "strings" "unicode" ) @@ -47,6 +49,43 @@ func mapName(raw string) string { return string(newstr) } +func loadAccountConfig(path string) ([]AccountConfig, error) { + var ( + file *ini.File + err error + accounts []AccountConfig + ) + accounts = make([]AccountConfig, 0) + if file, err = ini.Load(path); err != nil { + return nil, err + } + file.NameMapper = mapName + for _, _sec := range file.SectionStrings() { + if _sec == "DEFAULT" { + continue + } + sec := file.Section(_sec) + account := AccountConfig{Name: _sec} + if err = sec.MapTo(&account); err != nil { + return nil, err + } + for key, val := range sec.KeysHash() { + if key == "source" { + account.Source = val + } else if key == "folders" { + account.Folders = strings.Split(val, ",") + } else if key != "name" { + account.Params[key] = val + } + } + if account.Source == "" { + return nil, fmt.Errorf("Expected source for account %s", _sec) + } + accounts = append(accounts, account) + } + return accounts, nil +} + func LoadConfig(root *string) (*AercConfig, error) { var ( err error @@ -80,5 +119,11 @@ func LoadConfig(root *string) (*AercConfig, error) { if ui, err := file.GetSection("ui"); err != nil { ui.MapTo(config.Ui) } + accountsPath := path.Join(*root, "accounts.conf") + if accounts, err := loadAccountConfig(accountsPath); err != nil { + return nil, err + } else { + config.Accounts = accounts + } return config, nil } diff --git a/worker/imap/worker.go b/worker/imap/worker.go index a095e60..97cd4b0 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -34,11 +34,11 @@ func (w *IMAPWorker) PostAction(msg types.WorkerMessage) { func (w *IMAPWorker) handleMessage(_msg types.WorkerMessage) { switch msg := _msg.(type) { case types.Ping: - w.messages <- &types.Ack{ + w.messages <- types.Ack{ Message: types.RespondTo(msg), } default: - w.messages <- &types.Unsupported{ + w.messages <- types.Unsupported{ Message: types.RespondTo(_msg), } } @@ -49,7 +49,7 @@ func (w *IMAPWorker) Run() { for { select { case msg := <-w.actions: - fmt.Printf("<= %T: %+v\n", msg, msg) + fmt.Printf("<= %T\n", msg) w.handleMessage(msg) default: // no-op diff --git a/worker/worker.go b/worker/worker.go index 1f9f04c..a504790 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -3,6 +3,9 @@ package worker import ( "git.sr.ht/~sircmpwn/aerc2/worker/imap" "git.sr.ht/~sircmpwn/aerc2/worker/types" + + "fmt" + "net/url" ) type Worker interface { @@ -12,7 +15,18 @@ type Worker interface { } // Guesses the appropriate worker type based on the given source string -func NewWorker(source string) Worker { - // TODO: Do this properly - return imap.NewIMAPWorker() +func NewWorker(source string) (Worker, error) { + var ( + u *url.URL + err error + ) + if u, err = url.Parse(source); err != nil { + return nil, err + } + switch u.Scheme { + case "imap": + case "imaps": + return imap.NewIMAPWorker(), nil + } + return nil, fmt.Errorf("Unknown backend %s", u.Scheme) } -- cgit v1.2.3