aboutsummaryrefslogtreecommitdiffstats
path: root/worker/jmap/connect.go
blob: affa9474a09e147eec655ee027a13ac7a5447b23 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package jmap

import (
	"encoding/json"
	"fmt"
	"io"
	"net/url"
	"strings"
	"sync/atomic"

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

func (w *JMAPWorker) handleConnect(msg *types.Connect) error {
	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 session, err := w.cache.GetSession(); err != nil {
		if err := client.Authenticate(); err != nil {
			return err
		}
		if err := w.cache.PutSession(client.Session); err != nil {
			w.w.Warnf("PutSession: %s", err)
		}
	} else {
		client.Session = session
	}

	switch {
	case client == nil:
		fallthrough
	case client.Session == nil:
		fallthrough
	case client.Session.PrimaryAccounts == nil:
		break
	default:
		w.accountId = client.Session.PrimaryAccounts[mail.URI]
	}

	w.client = client

	return w.GetIdentities()
}

func (w *JMAPWorker) GetIdentities() error {
	u, err := url.Parse(w.config.account.Outgoing.Value)
	if err != nil {
		return fmt.Errorf("GetIdentities: %w", err)
	}
	if !strings.HasPrefix(u.Scheme, "jmap") {
		// no need for identities
		return nil
	}

	var req jmap.Request

	req.Invoke(&identity.Get{Account: w.accountId})
	resp, err := w.Do(&req)
	if err != nil {
		return err
	}
	for _, inv := range resp.Responses {
		switch r := inv.Args.(type) {
		case *identity.GetResponse:
			for _, ident := range r.List {
				w.identities[ident.Email] = ident
			}
		case *jmap.MethodError:
			return wrapMethodError(r)
		}
	}

	return nil
}

var seqnum uint64

func (w *JMAPWorker) Do(req *jmap.Request) (*jmap.Response, error) {
	seq := atomic.AddUint64(&seqnum, 1)
	body, _ := json.Marshal(req.Calls)
	w.w.Debugf(">%d> POST %s", seq, body)
	resp, err := w.client.Do(req)
	if err == nil {
		w.w.Debugf("<%d< done", seq)
	} else {
		w.w.Debugf("<%d< %s", seq, err)
	}
	return resp, err
}

func (w *JMAPWorker) Download(blobID jmap.ID) (io.ReadCloser, error) {
	seq := atomic.AddUint64(&seqnum, 1)
	replacer := strings.NewReplacer(
		"{accountId}", string(w.accountId),
		"{blobId}", string(blobID),
		"{type}", "application/octet-stream",
		"{name}", "filename",
	)
	url := replacer.Replace(w.client.Session.DownloadURL)
	w.w.Debugf(">%d> GET %s", seq, url)
	rd, err := w.client.Download(w.accountId, blobID)
	if err == nil {
		w.w.Debugf("<%d< 200 OK", seq)
	} else {
		w.w.Debugf("<%d< %s", seq, err)
	}
	return rd, err
}

func (w *JMAPWorker) Upload(reader io.Reader) (*jmap.UploadResponse, error) {
	seq := atomic.AddUint64(&seqnum, 1)
	url := strings.ReplaceAll(w.client.Session.UploadURL,
		"{accountId}", string(w.accountId))
	w.w.Debugf(">%d> POST %s", seq, url)
	resp, err := w.client.Upload(w.accountId, reader)
	if err == nil {
		w.w.Debugf("<%d< 200 OK", seq)
	} else {
		w.w.Debugf("<%d< %s", seq, err)
	}
	return resp, err
}