aboutsummaryrefslogtreecommitdiffstats
path: root/worker/jmap/connect.go
blob: c5eabe637582ad79dcf2c29f8d01ce0c38f85c6a (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
134
135
package jmap

import (
	"encoding/json"
	"io"
	"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 {
	w.client = &jmap.Client{SessionEndpoint: w.config.endpoint}

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

	if session, err := w.cache.GetSession(); err == nil {
		w.client.Session = session
		if w.GetIdentities() != nil {
			w.client.Session = nil
			w.identities = make(map[string]*identity.Identity)
			if err := w.cache.DeleteSession(); err != nil {
				w.w.Warnf("DeleteSession: %s", err)
			}
		}
	}
	if w.client.Session == nil {
		if err := w.client.Authenticate(); err != nil {
			return err
		}
		if err := w.cache.PutSession(w.client.Session); err != nil {
			w.w.Warnf("PutSession: %s", err)
		}
	}
	if len(w.identities) == 0 {
		if err := w.GetIdentities(); err != nil {
			return err
		}
	}

	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 ""
	default:
		return w.client.Session.PrimaryAccounts[mail.URI]
	}
}

func (w *JMAPWorker) GetIdentities() error {
	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
}