aboutsummaryrefslogtreecommitdiffstats
path: root/worker/jmap/connect.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-06-04 14:05:22 +0200
committerRobin Jarry <robin@jarry.cc>2023-06-21 17:29:21 +0200
commitbe0bfc1ae28b49be6546626ff9eaadce5464a6c8 (patch)
tree60b9d8b812408db281c00d5a71197e118a736337 /worker/jmap/connect.go
parentb4ae11b4ec20b61a18e1ce08ea65014bcebc2f16 (diff)
downloadaerc-be0bfc1ae28b49be6546626ff9eaadce5464a6c8.tar.gz
worker: add jmap support
Add support for JMAP backends. This is on par with IMAP features with some additions specific to JMAP: * tagging * sending emails This makes use of git.sr.ht/~rockorager/go-jmap for the low level interaction with the JMAP server. The transport is JSON over HTTPS. For now, only oauthbearer with token is supported. If this proves useful, we may need to file for an official three-legged oauth support at JMAP providers. I have tested most features and this seems to be reliable. There are some quirks with the use-labels option. Especially when moving and deleting messages from the "All mail" virtual folder (see aerc-jmap(5)). Overall, the user experience is nice and there are a lot less background updates issues than with IMAP (damn IDLE mode hanging after restoring from sleep). I know that not everyone has access to a JMAP provider. For those interested, there are at least these two commercial offerings: https://www.fastmail.com/ https://www.topicbox.com/ And, if you host your own mail, you can use a JMAP capable server: https://stalw.art/jmap/ https://www.cyrusimap.org/imap/download/installation/http/jmap.html Link: https://www.rfc-editor.org/rfc/rfc8620.html Link: https://www.rfc-editor.org/rfc/rfc8621.html Signed-off-by: Robin Jarry <robin@jarry.cc> Tested-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'worker/jmap/connect.go')
-rw-r--r--worker/jmap/connect.go133
1 files changed, 133 insertions, 0 deletions
diff --git a/worker/jmap/connect.go b/worker/jmap/connect.go
new file mode 100644
index 00000000..affa9474
--- /dev/null
+++ b/worker/jmap/connect.go
@@ -0,0 +1,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
+}