aboutsummaryrefslogtreecommitdiff
path: root/worker/jmap/configure.go
blob: dd6f3f36bec274b777a3ea7d1e227e7bfc52a65e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package jmap

import (
	"fmt"
	"net/url"
	"strings"
	"time"

	"git.sr.ht/~rjarry/aerc/worker/jmap/cache"
	"git.sr.ht/~rjarry/aerc/worker/types"
)

func (w *JMAPWorker) handleConfigure(msg *types.Configure) error {
	w.config.cacheState = parseBool(msg.Config.Params["cache-state"])
	w.config.cacheBlobs = parseBool(msg.Config.Params["cache-blobs"])
	w.config.useLabels = parseBool(msg.Config.Params["use-labels"])
	w.cache = cache.NewJMAPCache(
		w.config.cacheState, w.config.cacheBlobs, msg.Config.Name)

	u, err := url.Parse(msg.Config.Source)
	if err != nil {
		return err
	}

	if strings.HasSuffix(u.Scheme, "+oauthbearer") {
		w.config.oauth = true
	} else {
		if u.User == nil {
			return fmt.Errorf("user:password not specified")
		} else if u.User.Username() == "" {
			return fmt.Errorf("username not specified")
		} else if _, ok := u.User.Password(); !ok {
			return fmt.Errorf("password not specified")
		}
	}

	u.RawQuery = ""
	u.Fragment = ""
	w.config.user = u.User
	u.User = nil
	u.Scheme = "https"

	w.config.endpoint = u.String()
	w.config.account = msg.Config
	w.config.allMail = msg.Config.Params["all-mail"]
	if w.config.allMail == "" {
		w.config.allMail = "All mail"
	}
	if ping, ok := msg.Config.Params["server-ping"]; ok {
		dur, err := time.ParseDuration(ping)
		if err != nil {
			return fmt.Errorf("server-ping: %w", err)
		}
		w.config.serverPing = dur
	}

	return nil
}

func parseBool(val string) bool {
	switch strings.ToLower(val) {
	case "1", "t", "true", "yes", "y", "on":
		return true
	}
	return false
}