| 1 |
package jmap |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/models" |
| 11 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 12 |
"git.sr.ht/~rockorager/go-jmap" |
| 13 |
"git.sr.ht/~rockorager/go-jmap/mail/email" |
| 14 |
"github.com/emersion/go-message/charset" |
| 15 |
) |
| 16 |
|
| 17 |
var bodyProperties = []string{ |
| 18 |
"blobId", |
| 19 |
"charset", |
| 20 |
"cid", |
| 21 |
"disposition", |
| 22 |
"language", |
| 23 |
"location", |
| 24 |
"name", |
| 25 |
"partId", |
| 26 |
"size", |
| 27 |
"subParts", |
| 28 |
"type", |
| 29 |
} |
| 30 |
|
| 31 |
var emailProperties = []string{ |
| 32 |
"id", |
| 33 |
"blobId", |
| 34 |
"threadId", |
| 35 |
"mailboxIds", |
| 36 |
"keywords", |
| 37 |
"size", |
| 38 |
"receivedAt", |
| 39 |
"headers", |
| 40 |
"messageId", |
| 41 |
"inReplyTo", |
| 42 |
"references", |
| 43 |
"from", |
| 44 |
"to", |
| 45 |
"cc", |
| 46 |
"bcc", |
| 47 |
"replyTo", |
| 48 |
"subject", |
| 49 |
"bodyStructure", |
| 50 |
} |
| 51 |
|
| 52 |
func (w *JMAPWorker) getEmails( |
| 53 |
ctx context.Context, uids []models.UID, props, bodyProps []string, |
| 54 |
) ([]*email.Email, error) { |
| 55 |
emailIdsToFetch := make([]jmap.ID, 0, len(uids)) |
| 56 |
currentEmails := make([]*email.Email, 0, len(uids)) |
| 57 |
|
| 58 |
for _, uid := range uids { |
| 59 |
jid := jmap.ID(uid) |
| 60 |
m, err := w.cache.GetEmail(jid) |
| 61 |
if err != nil { |
| 62 |
// Message wasn't in cache; fetch it |
| 63 |
emailIdsToFetch = append(emailIdsToFetch, jid) |
| 64 |
continue |
| 65 |
} |
| 66 |
currentEmails = append(currentEmails, m) |
| 67 |
} |
| 68 |
|
| 69 |
if len(emailIdsToFetch) > 0 { |
| 70 |
var req jmap.Request |
| 71 |
|
| 72 |
req.Invoke(&email.Get{ |
| 73 |
Account: w.AccountId(), |
| 74 |
IDs: emailIdsToFetch, |
| 75 |
Properties: props, |
| 76 |
BodyProperties: bodyProps, |
| 77 |
}) |
| 78 |
|
| 79 |
resp, err := w.Do(ctx, &req) |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
|
| 84 |
for _, inv := range resp.Responses { |
| 85 |
switch r := inv.Args.(type) { |
| 86 |
case *email.GetResponse: |
| 87 |
if err = w.cache.PutEmailState(r.State); err != nil { |
| 88 |
w.w.Warnf("PutEmailState: %s", err) |
| 89 |
} |
| 90 |
for _, m := range r.List { |
| 91 |
currentEmails = append(currentEmails, m) |
| 92 |
if err := w.cache.PutEmail(m.ID, m); err != nil { |
| 93 |
w.w.Warnf("PutEmail: %s", err) |
| 94 |
} |
| 95 |
} |
| 96 |
case *jmap.MethodError: |
| 97 |
return nil, wrapMethodError(r) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return currentEmails, nil |
| 103 |
} |
| 104 |
|
| 105 |
func (w *JMAPWorker) handleFetchMessageHeaders(msg *types.FetchMessageHeaders) error { |
| 106 |
emails, err := w.getEmails(msg.Context(), msg.Uids, emailProperties, bodyProperties) |
| 107 |
if err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
|
| 111 |
var threadsToFetch []jmap.ID |
| 112 |
for _, eml := range emails { |
| 113 |
w.w.PostMessage(&types.MessageInfo{ |
| 114 |
Message: types.RespondTo(msg), |
| 115 |
Info: w.translateMsgInfo(eml, msg.Directory), |
| 116 |
}, nil) |
| 117 |
thread, err := w.cache.GetThread(eml.ThreadID) |
| 118 |
if err != nil { |
| 119 |
threadsToFetch = append(threadsToFetch, eml.ThreadID) |
| 120 |
continue |
| 121 |
} |
| 122 |
for _, id := range thread { |
| 123 |
m, err := w.cache.GetEmail(id) |
| 124 |
if err != nil { |
| 125 |
// This should never happen. If we have the |
| 126 |
// thread in cache, we will have fetched it |
| 127 |
// already or updated it from the update loop |
| 128 |
w.w.Warnf("Email ID %s from Thread %s not in cache", id, eml.ThreadID) |
| 129 |
continue |
| 130 |
} |
| 131 |
// Get the UI updated immediately |
| 132 |
w.w.PostMessage(&types.MessageInfo{ |
| 133 |
Message: types.RespondTo(msg), |
| 134 |
Info: w.translateMsgInfo(m, msg.Directory), |
| 135 |
}, nil) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
threadEmails, err := w.fetchEntireThreads(msg.Context(), threadsToFetch) |
| 140 |
if err != nil { |
| 141 |
return err |
| 142 |
} |
| 143 |
|
| 144 |
for _, m := range threadEmails { |
| 145 |
w.w.PostMessage(&types.MessageInfo{ |
| 146 |
Message: types.RespondTo(msg), |
| 147 |
Info: w.translateMsgInfo(m, msg.Directory), |
| 148 |
}, nil) |
| 149 |
if err := w.cache.PutEmail(m.ID, m); err != nil { |
| 150 |
w.w.Warnf("PutEmail: %s", err) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return nil |
| 155 |
} |
| 156 |
|
| 157 |
func (w *JMAPWorker) handleFetchMessageBodyPart(msg *types.FetchMessageBodyPart) error { |
| 158 |
mails, err := w.getEmails(msg.Context(), []models.UID{msg.Uid}, |
| 159 |
[]string{"id", "bodyStructure"}, bodyProperties) |
| 160 |
if err != nil { |
| 161 |
return err |
| 162 |
} |
| 163 |
if len(mails) != 1 { |
| 164 |
return fmt.Errorf("bug: message %s not found", msg.Uid) |
| 165 |
} |
| 166 |
|
| 167 |
part := mails[0].BodyStructure |
| 168 |
for i, index := range msg.Part { |
| 169 |
index -= 1 // convert to zero based offset |
| 170 |
if index < len(part.SubParts) { |
| 171 |
part = part.SubParts[index] |
| 172 |
} else { |
| 173 |
return fmt.Errorf( |
| 174 |
"bug: invalid part index[%d]: %v", i, msg.Part) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
buf, err := w.cache.GetBlob(part.BlobID) |
| 179 |
if err != nil { |
| 180 |
rd, err := w.Download(msg.Context(), part.BlobID) |
| 181 |
if err != nil { |
| 182 |
return w.wrapDownloadError("part", part.BlobID, err) |
| 183 |
} |
| 184 |
buf, err = io.ReadAll(rd) |
| 185 |
rd.Close() |
| 186 |
if err != nil { |
| 187 |
return err |
| 188 |
} |
| 189 |
if err = w.cache.PutBlob(part.BlobID, buf); err != nil { |
| 190 |
w.w.Warnf("PutBlob: %s", err) |
| 191 |
} |
| 192 |
} |
| 193 |
var reader io.Reader = bytes.NewReader(buf) |
| 194 |
if strings.HasPrefix(part.Type, "text/") && part.Charset != "" { |
| 195 |
r, err := charset.Reader(part.Charset, reader) |
| 196 |
if err != nil { |
| 197 |
w.w.Warnf("charset.Reader: %v", err) |
| 198 |
} else { |
| 199 |
reader = r |
| 200 |
} |
| 201 |
} |
| 202 |
w.w.PostMessage(&types.MessageBodyPart{ |
| 203 |
Message: types.RespondTo(msg), |
| 204 |
Part: &models.MessageBodyPart{ |
| 205 |
Reader: reader, |
| 206 |
Uid: msg.Uid, |
| 207 |
}, |
| 208 |
}, nil) |
| 209 |
|
| 210 |
return nil |
| 211 |
} |
| 212 |
|
| 213 |
func (w *JMAPWorker) handleFetchFullMessages(msg *types.FetchFullMessages) error { |
| 214 |
mails, err := w.getEmails(msg.Context(), msg.Uids, []string{"id", "blobId"}, nil) |
| 215 |
if err != nil { |
| 216 |
return err |
| 217 |
} |
| 218 |
|
| 219 |
for _, mail := range mails { |
| 220 |
buf, err := w.cache.GetBlob(mail.BlobID) |
| 221 |
if err != nil { |
| 222 |
rd, err := w.Download(msg.Context(), mail.BlobID) |
| 223 |
if err != nil { |
| 224 |
return w.wrapDownloadError("full", mail.BlobID, err) |
| 225 |
} |
| 226 |
buf, err = io.ReadAll(rd) |
| 227 |
rd.Close() |
| 228 |
if err != nil { |
| 229 |
return err |
| 230 |
} |
| 231 |
if err = w.cache.PutBlob(mail.BlobID, buf); err != nil { |
| 232 |
w.w.Warnf("PutBlob: %s", err) |
| 233 |
} |
| 234 |
} |
| 235 |
w.w.PostMessage(&types.FullMessage{ |
| 236 |
Message: types.RespondTo(msg), |
| 237 |
Content: &models.FullMessage{ |
| 238 |
Reader: bytes.NewReader(buf), |
| 239 |
Uid: models.UID(mail.ID), |
| 240 |
}, |
| 241 |
}, nil) |
| 242 |
} |
| 243 |
|
| 244 |
return nil |
| 245 |
} |
| 246 |
|
| 247 |
func (w *JMAPWorker) wrapDownloadError(prefix string, blobId jmap.ID, err error) error { |
| 248 |
urlRepl := strings.NewReplacer( |
| 249 |
"{accountId}", string(w.AccountId()), |
| 250 |
"{blobId}", string(blobId), |
| 251 |
"{type}", "application/octet-stream", |
| 252 |
"{name}", "filename", |
| 253 |
) |
| 254 |
url := urlRepl.Replace(w.client.Session.DownloadURL) |
| 255 |
return fmt.Errorf("%s: %q %w", prefix, url, err) |
| 256 |
} |