| 1 |
package jmap |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"io" |
| 7 |
"net" |
| 8 |
"net/http" |
| 9 |
"strings" |
| 10 |
"sync/atomic" |
| 11 |
"time" |
| 12 |
|
| 13 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 14 |
"git.sr.ht/~rockorager/go-jmap" |
| 15 |
"git.sr.ht/~rockorager/go-jmap/mail" |
| 16 |
"git.sr.ht/~rockorager/go-jmap/mail/identity" |
| 17 |
"golang.org/x/oauth2" |
| 18 |
) |
| 19 |
|
| 20 |
func (w *JMAPWorker) handleConnect(msg *types.Connect) error { |
| 21 |
w.client = &jmap.Client{SessionEndpoint: w.config.endpoint} |
| 22 |
|
| 23 |
if w.config.oauth { |
| 24 |
pass, _ := w.config.user.Password() |
| 25 |
w.client.WithAccessToken(pass) |
| 26 |
} else { |
| 27 |
user := w.config.user.Username() |
| 28 |
pass, _ := w.config.user.Password() |
| 29 |
w.client.WithBasicAuth(user, pass) |
| 30 |
} |
| 31 |
|
| 32 |
if transport, ok := w.client.HttpClient.Transport.(*oauth2.Transport); ok { |
| 33 |
if httpTransport, ok := transport.Base.(*http.Transport); ok { |
| 34 |
// Enable TCP keepalive to detect dead connections faster |
| 35 |
httpTransport.DialContext = (&net.Dialer{ |
| 36 |
Timeout: 30 * time.Second, |
| 37 |
KeepAlive: 15 * time.Second, |
| 38 |
}).DialContext |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if session, err := w.cache.GetSession(); err == nil { |
| 43 |
w.client.Session = session |
| 44 |
} |
| 45 |
if w.client.Session == nil { |
| 46 |
if err := w.UpdateSession(); err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
go w.monitorChanges() |
| 52 |
|
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
func (w *JMAPWorker) AccountId() jmap.ID { |
| 57 |
switch { |
| 58 |
case w.client == nil: |
| 59 |
fallthrough |
| 60 |
case w.client.Session == nil: |
| 61 |
fallthrough |
| 62 |
case w.client.Session.PrimaryAccounts == nil: |
| 63 |
return "" |
| 64 |
default: |
| 65 |
return w.client.Session.PrimaryAccounts[mail.URI] |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func (w *JMAPWorker) UpdateSession() error { |
| 70 |
if err := w.client.Authenticate(); err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
if err := w.cache.PutSession(w.client.Session); err != nil { |
| 74 |
w.w.Warnf("PutSession: %s", err) |
| 75 |
} |
| 76 |
return nil |
| 77 |
} |
| 78 |
|
| 79 |
func (w *JMAPWorker) GetIdentities() error { |
| 80 |
var req jmap.Request |
| 81 |
|
| 82 |
req.Invoke(&identity.Get{Account: w.AccountId()}) |
| 83 |
resp, err := w.Do(context.TODO(), &req) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
for _, inv := range resp.Responses { |
| 88 |
switch r := inv.Args.(type) { |
| 89 |
case *identity.GetResponse: |
| 90 |
for _, ident := range r.List { |
| 91 |
w.identities[ident.Email] = ident |
| 92 |
} |
| 93 |
case *jmap.MethodError: |
| 94 |
return wrapMethodError(r) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return nil |
| 99 |
} |
| 100 |
|
| 101 |
var seqnum uint64 |
| 102 |
|
| 103 |
func (w *JMAPWorker) Do(ctx context.Context, req *jmap.Request) (*jmap.Response, error) { |
| 104 |
if ctx != nil { |
| 105 |
req.Context = ctx |
| 106 |
} |
| 107 |
seq := atomic.AddUint64(&seqnum, 1) |
| 108 |
body, _ := json.Marshal(req.Calls) |
| 109 |
w.w.Debugf(">%d> POST %s", seq, body) |
| 110 |
resp, err := w.client.Do(req) |
| 111 |
if err != nil { |
| 112 |
w.w.Debugf("<%d< %s", seq, err) |
| 113 |
// Try to update session in case an endpoint changed |
| 114 |
err := w.UpdateSession() |
| 115 |
if err != nil { |
| 116 |
return nil, err |
| 117 |
} |
| 118 |
// And try again if we succeeded |
| 119 |
resp, err = w.client.Do(req) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
} |
| 124 |
if resp.SessionState != w.client.Session.State { |
| 125 |
if err := w.UpdateSession(); err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
} |
| 129 |
w.w.Debugf("<%d< done", seq) |
| 130 |
return resp, err |
| 131 |
} |
| 132 |
|
| 133 |
func (w *JMAPWorker) Download(ctx context.Context, blobID jmap.ID) (io.ReadCloser, error) { |
| 134 |
seq := atomic.AddUint64(&seqnum, 1) |
| 135 |
replacer := strings.NewReplacer( |
| 136 |
"{accountId}", string(w.AccountId()), |
| 137 |
"{blobId}", string(blobID), |
| 138 |
"{type}", "application/octet-stream", |
| 139 |
"{name}", "filename", |
| 140 |
) |
| 141 |
url := replacer.Replace(w.client.Session.DownloadURL) |
| 142 |
w.w.Debugf(">%d> GET %s", seq, url) |
| 143 |
rd, err := w.client.DownloadWithContext(ctx, w.AccountId(), blobID) |
| 144 |
if err == nil { |
| 145 |
w.w.Debugf("<%d< 200 OK", seq) |
| 146 |
} else { |
| 147 |
w.w.Debugf("<%d< %s", seq, err) |
| 148 |
} |
| 149 |
return rd, err |
| 150 |
} |
| 151 |
|
| 152 |
func (w *JMAPWorker) Upload(reader io.Reader) (*jmap.UploadResponse, error) { |
| 153 |
seq := atomic.AddUint64(&seqnum, 1) |
| 154 |
url := strings.ReplaceAll(w.client.Session.UploadURL, |
| 155 |
"{accountId}", string(w.AccountId())) |
| 156 |
w.w.Debugf(">%d> POST %s", seq, url) |
| 157 |
resp, err := w.client.Upload(w.AccountId(), reader) |
| 158 |
if err == nil { |
| 159 |
w.w.Debugf("<%d< 200 OK", seq) |
| 160 |
} else { |
| 161 |
w.w.Debugf("<%d< %s", seq, err) |
| 162 |
} |
| 163 |
return resp, err |
| 164 |
} |