aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-12-21 20:08:32 +0100
committerRobin Jarry <robin@jarry.cc>2023-01-04 22:57:31 +0100
commitc56027b2e69ec198e41394e5cf906273d80baf79 (patch)
treee14610beb7446e04f47da8ecdb06e616e70d5d29
parent5677f93ff8e0f212be112a110fcfe09663c84f98 (diff)
downloadaerc-c56027b2e69ec198e41394e5cf906273d80baf79.tar.gz
config: cleanup accounts.conf section parsing
Use go ini reflection capabilities where possible. Mark fields that can be trivially parsed and those who need manual parsing. Restrict backend-specific parameters to ini keys that are not listed as ini struct field tags. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
-rw-r--r--config/accounts.go100
1 files changed, 48 insertions, 52 deletions
diff --git a/config/accounts.go b/config/accounts.go
index 54c6556f..aa4bde29 100644
--- a/config/accounts.go
+++ b/config/accounts.go
@@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path"
+ "reflect"
"regexp"
"sort"
"strconv"
@@ -68,25 +69,25 @@ func (c *RemoteConfig) ConnectionString() (string, error) {
}
type AccountConfig struct {
- Archive string
- CopyTo string
- Default string
- Postpone string
- From string
- Aliases string
- Name string
- Source string
- Folders []string
- FoldersExclude []string
- Params map[string]string
- Outgoing RemoteConfig
- SignatureFile string
- SignatureCmd string
- EnableFoldersSort bool `ini:"enable-folders-sort"`
- FoldersSort []string `ini:"folders-sort" delim:","`
- AddressBookCmd string `ini:"address-book-cmd"`
- SendAsUTC bool `ini:"send-as-utc"`
- LocalizedRe *regexp.Regexp
+ Archive string `ini:"archive"`
+ CopyTo string `ini:"copy-to"`
+ Default string `ini:"default"`
+ Postpone string `ini:"postpone"`
+ From string `ini:"from"`
+ Aliases string `ini:"aliases"`
+ Name string `ini:"-"`
+ Source string `ini:"-"`
+ Folders []string `ini:"folders" delim:","`
+ FoldersExclude []string `ini:"folders-exclude" delim:","`
+ Params map[string]string `ini:"-"`
+ Outgoing RemoteConfig `ini:"-"`
+ SignatureFile string `ini:"signature-file"`
+ SignatureCmd string `ini:"signature-cmd"`
+ EnableFoldersSort bool `ini:"enable-folders-sort"`
+ FoldersSort []string `ini:"folders-sort" delim:","`
+ AddressBookCmd string `ini:"address-book-cmd"`
+ SendAsUTC bool `ini:"send-as-utc"`
+ LocalizedRe *regexp.Regexp `ini:"-"`
// CheckMail
CheckMail time.Duration `ini:"check-mail"`
@@ -148,14 +149,6 @@ func parseAccounts(root string, accts []string) error {
}
for key, val := range sec.KeysHash() {
switch key {
- case "folders":
- folders := strings.Split(val, ",")
- sort.Strings(folders)
- account.Folders = folders
- case "folders-exclude":
- folders := strings.Split(val, ",")
- sort.Strings(folders)
- account.FoldersExclude = folders
case "source":
sourceRemoteConfig.Value = val
case "source-cred-cmd":
@@ -170,24 +163,6 @@ func parseAccounts(root string, accts []string) error {
return fmt.Errorf("%s=%s %w", key, val, err)
}
account.Outgoing.CacheCmd = cache
- case "from":
- account.From = val
- case "aliases":
- account.Aliases = val
- case "copy-to":
- account.CopyTo = val
- case "archive":
- account.Archive = val
- case "enable-folders-sort":
- account.EnableFoldersSort, _ = strconv.ParseBool(val)
- case "pgp-key-id":
- account.PgpKeyId = val
- case "pgp-auto-sign":
- account.PgpAutoSign, _ = strconv.ParseBool(val)
- case "pgp-opportunistic-encrypt":
- account.PgpOpportunisticEncrypt, _ = strconv.ParseBool(val)
- case "address-book-cmd":
- account.AddressBookCmd = val
case "subject-re-pattern":
re, err := regexp.Compile(val)
if err != nil {
@@ -195,11 +170,38 @@ func parseAccounts(root string, accts []string) error {
}
account.LocalizedRe = re
default:
- if key != "name" {
+ backendSpecific := true
+ typ := reflect.TypeOf(account)
+ for i := 0; i < typ.NumField(); i++ {
+ field := typ.Field(i)
+ switch field.Tag.Get("ini") {
+ case key:
+ fallthrough
+ case "source":
+ fallthrough
+ case "source-cred-cmd":
+ fallthrough
+ case "outgoing":
+ fallthrough
+ case "outgoing-cred-cmd":
+ fallthrough
+ case "outgoing-cred-cmd-cache":
+ fallthrough
+ case "subject-re-pattern":
+ backendSpecific = false
+ }
+ }
+ if backendSpecific {
account.Params[key] = val
}
}
}
+ source, err := sourceRemoteConfig.ConnectionString()
+ if err != nil {
+ return fmt.Errorf("Invalid source credentials for %s: %w", _sec, err)
+ }
+ account.Source = source
+
if account.Source == "" {
return fmt.Errorf("Expected source for account %s", _sec)
}
@@ -207,12 +209,6 @@ func parseAccounts(root string, accts []string) error {
return fmt.Errorf("Expected from for account %s", _sec)
}
- source, err := sourceRemoteConfig.ConnectionString()
- if err != nil {
- return fmt.Errorf("Invalid source credentials for %s: %w", _sec, err)
- }
- account.Source = source
-
_, err = account.Outgoing.parseValue()
if err != nil {
return fmt.Errorf("Invalid outgoing credentials for %s: %w", _sec, err)