Debian Codegraph @

worker/jmap/configure.go

raw
1 package jmap
2
3 import (
4 "fmt"
5 "net/url"
6 "strings"
7 "time"
8
9 "git.sr.ht/~rjarry/aerc/worker/jmap/cache"
10 "git.sr.ht/~rjarry/aerc/worker/types"
11 )
12
13 func (w *JMAPWorker) handleConfigure(msg *types.Configure) error {
14 w.config.cacheState = parseBool(msg.Config.Params["cache-state"])
15 w.config.cacheBlobs = parseBool(msg.Config.Params["cache-blobs"])
16 w.config.useLabels = parseBool(msg.Config.Params["use-labels"])
17 w.cache = cache.NewJMAPCache(
18 w.config.cacheState, w.config.cacheBlobs, msg.Config.Name)
19
20 u, err := url.Parse(msg.Config.Source)
21 if err != nil {
22 return err
23 }
24
25 if strings.HasSuffix(u.Scheme, "+oauthbearer") {
26 w.config.oauth = true
27 } else {
28 if u.User == nil {
29 return fmt.Errorf("user:password not specified")
30 } else if u.User.Username() == "" {
31 return fmt.Errorf("username not specified")
32 } else if _, ok := u.User.Password(); !ok {
33 return fmt.Errorf("password not specified")
34 }
35 }
36
37 u.RawQuery = ""
38 u.Fragment = ""
39 w.config.user = u.User
40 u.User = nil
41 u.Scheme = "https"
42
43 w.config.endpoint = u.String()
44 w.config.account = msg.Config
45 w.config.allMail = msg.Config.Params["all-mail"]
46 if w.config.allMail == "" {
47 w.config.allMail = "All mail"
48 }
49 if ping, ok := msg.Config.Params["server-ping"]; ok {
50 dur, err := time.ParseDuration(ping)
51 if err != nil {
52 return fmt.Errorf("server-ping: %w", err)
53 }
54 w.config.serverPing = dur
55 }
56
57 return nil
58 }
59
60 func parseBool(val string) bool {
61 switch strings.ToLower(val) {
62 case "1", "t", "true", "yes", "y", "on":
63 return true
64 }
65 return false
66 }