aboutsummaryrefslogtreecommitdiff
path: root/worker/jmap/connect.go
blob: b4ce45eceb7a835cd886f726aa3887813c7d8f3e (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
package jmap

import (
	"git.sr.ht/~rjarry/aerc/worker/types"
	"git.sr.ht/~rockorager/go-jmap"
	"git.sr.ht/~rockorager/go-jmap/mail"
)

func (w *JMAPWorker) handleConnect(msg *types.Connect) error {
	w.w.Debugf("connecting")
	client := &jmap.Client{SessionEndpoint: w.config.endpoint}

	if w.config.oauth {
		pass, _ := w.config.user.Password()
		client.WithAccessToken(pass)
	} else {
		user := w.config.user.Username()
		pass, _ := w.config.user.Password()
		client.WithBasicAuth(user, pass)
	}

	if err := client.Authenticate(); err != nil {
		return err
	}

	w.client = client

	return nil
}

func (w *JMAPWorker) accountId() jmap.ID {
	switch {
	case w.client == nil:
		fallthrough
	case w.client.Session == nil:
		fallthrough
	case w.client.Session.PrimaryAccounts == nil:
		return jmap.ID("")
	default:
		return w.client.Session.PrimaryAccounts[mail.URI]
	}
}