diff options
author | Koni Marti <koni.marti@gmail.com> | 2022-04-30 01:08:54 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-05-04 14:07:15 +0200 |
commit | 4d75137b20664cbdf90159c78d1fbf78779f3416 (patch) | |
tree | f12f127c46539502ebe764f03075a03a4fe5ff4a /worker/imap/configure.go | |
parent | a1b75a99bd277eb1f65a0483359af8e411b124d5 (diff) | |
download | aerc-4d75137b20664cbdf90159c78d1fbf78779f3416.tar.gz |
imap: extract imap config and configure handling
Extract the imap config and move the configure part out of the message
handler.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/imap/configure.go')
-rw-r--r-- | worker/imap/configure.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/worker/imap/configure.go b/worker/imap/configure.go new file mode 100644 index 00000000..ac1d606d --- /dev/null +++ b/worker/imap/configure.go @@ -0,0 +1,99 @@ +package imap + +import ( + "fmt" + "net/url" + "strconv" + "strings" + "time" + + "git.sr.ht/~rjarry/aerc/worker/types" + "golang.org/x/oauth2" +) + +func (w *IMAPWorker) handleConfigure(msg *types.Configure) error { + u, err := url.Parse(msg.Config.Source) + if err != nil { + return err + } + + w.config.scheme = u.Scheme + if strings.HasSuffix(w.config.scheme, "+insecure") { + w.config.scheme = strings.TrimSuffix(w.config.scheme, "+insecure") + w.config.insecure = true + } + + if strings.HasSuffix(w.config.scheme, "+oauthbearer") { + w.config.scheme = strings.TrimSuffix(w.config.scheme, "+oauthbearer") + w.config.oauthBearer.Enabled = true + q := u.Query() + + oauth2 := &oauth2.Config{} + if q.Get("token_endpoint") != "" { + oauth2.ClientID = q.Get("client_id") + oauth2.ClientSecret = q.Get("client_secret") + oauth2.Scopes = []string{q.Get("scope")} + oauth2.Endpoint.TokenURL = q.Get("token_endpoint") + } + w.config.oauthBearer.OAuth2 = oauth2 + } + + w.config.addr = u.Host + if !strings.ContainsRune(w.config.addr, ':') { + w.config.addr += ":" + w.config.scheme + } + + w.config.user = u.User + w.config.folders = msg.Config.Folders + w.config.idle_timeout = 10 * time.Second + w.config.connection_timeout = 30 * time.Second + w.config.keepalive_period = 0 * time.Second + w.config.keepalive_probes = 3 + w.config.keepalive_interval = 3 + for key, value := range msg.Config.Params { + switch key { + case "idle-timeout": + val, err := time.ParseDuration(value) + if err != nil || val < 0 { + return fmt.Errorf( + "invalid idle-timeout value %v: %v", + value, err) + } + w.config.idle_timeout = val + case "connection-timeout": + val, err := time.ParseDuration(value) + if err != nil || val < 0 { + return fmt.Errorf( + "invalid connection-timeout value %v: %v", + value, err) + } + w.config.connection_timeout = val + case "keepalive-period": + val, err := time.ParseDuration(value) + if err != nil || val < 0 { + return fmt.Errorf( + "invalid keepalive-period value %v: %v", + value, err) + } + w.config.keepalive_period = val + case "keepalive-probes": + val, err := strconv.Atoi(value) + if err != nil || val < 0 { + return fmt.Errorf( + "invalid keepalive-probes value %v: %v", + value, err) + } + w.config.keepalive_probes = val + case "keepalive-interval": + val, err := time.ParseDuration(value) + if err != nil || val < 0 { + return fmt.Errorf( + "invalid keepalive-interval value %v: %v", + value, err) + } + w.config.keepalive_interval = int(val.Seconds()) + } + } + + return nil +} |