| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"encoding/gob" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/parse" |
| 14 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 15 |
"git.sr.ht/~rjarry/aerc/models" |
| 16 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 17 |
"github.com/emersion/go-message" |
| 18 |
"github.com/emersion/go-message/mail" |
| 19 |
"github.com/emersion/go-message/textproto" |
| 20 |
"github.com/syndtr/goleveldb/leveldb" |
| 21 |
) |
| 22 |
|
| 23 |
type CachedHeader struct { |
| 24 |
BodyStructure models.BodyStructure |
| 25 |
Envelope models.Envelope |
| 26 |
InternalDate time.Time |
| 27 |
Uid models.UID |
| 28 |
Directory string |
| 29 |
Size uint32 |
| 30 |
Header []byte |
| 31 |
Created time.Time |
| 32 |
Labels []string |
| 33 |
} |
| 34 |
|
| 35 |
var ( |
| 36 |
// cacheTag should be updated when changing the cache |
| 37 |
// structure; this will ensure that the user's cache is cleared and |
| 38 |
// reloaded when the underlying cache structure changes |
| 39 |
cacheTag = []byte("0004") |
| 40 |
cacheTagKey = []byte("cache.tag") |
| 41 |
) |
| 42 |
|
| 43 |
// initCacheDb opens (or creates) the database for the cache. One database is |
| 44 |
// created per account |
| 45 |
func (w *IMAPWorker) initCacheDb(acct string) { |
| 46 |
switch { |
| 47 |
case len(w.config.headersExclude) > 0: |
| 48 |
headerTag := strings.Join(w.config.headersExclude, "") |
| 49 |
cacheTag = append(cacheTag, headerTag...) |
| 50 |
case len(w.config.headers) > 0: |
| 51 |
headerTag := strings.Join(w.config.headers, "") |
| 52 |
cacheTag = append(cacheTag, headerTag...) |
| 53 |
} |
| 54 |
p := xdg.CachePath("aerc", acct) |
| 55 |
db, err := leveldb.OpenFile(p, nil) |
| 56 |
if err != nil { |
| 57 |
w.cache = nil |
| 58 |
w.worker.Errorf("failed opening cache db at %s: %v", p, err) |
| 59 |
return |
| 60 |
} |
| 61 |
w.cache = db |
| 62 |
w.worker.Debugf("cache db opened: %s", p) |
| 63 |
|
| 64 |
tag, err := w.cache.Get(cacheTagKey, nil) |
| 65 |
clearCache := errors.Is(err, leveldb.ErrNotFound) || |
| 66 |
!bytes.Equal(tag, cacheTag) |
| 67 |
switch { |
| 68 |
case clearCache: |
| 69 |
w.worker.Infof("current cache tag is '%s' but found '%s'", |
| 70 |
cacheTag, tag) |
| 71 |
w.worker.Warnf("tag mismatch: clear cache") |
| 72 |
w.clearCache() |
| 73 |
if err = w.cache.Put(cacheTagKey, cacheTag, nil); err != nil { |
| 74 |
w.worker.Errorf("could not set the current cache tag") |
| 75 |
} |
| 76 |
case err != nil: |
| 77 |
w.worker.Errorf("could not get the cache tag from db") |
| 78 |
default: |
| 79 |
if w.config.cacheMaxAge.Hours() > 0 { |
| 80 |
go w.cleanCache(p) |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func (w *IMAPWorker) cacheHeader(mi *models.MessageInfo) { |
| 86 |
key := w.headerKey(mi.Uid) |
| 87 |
w.worker.Debugf("caching header for message %s", key) |
| 88 |
hdr := bytes.NewBuffer(nil) |
| 89 |
err := textproto.WriteHeader(hdr, mi.RFC822Headers.Header.Header) |
| 90 |
if err != nil { |
| 91 |
w.worker.Errorf("cannot write header %s: %v", key, err) |
| 92 |
return |
| 93 |
} |
| 94 |
h := &CachedHeader{ |
| 95 |
BodyStructure: *mi.BodyStructure, |
| 96 |
Envelope: *mi.Envelope, |
| 97 |
InternalDate: mi.InternalDate, |
| 98 |
Uid: mi.Uid, |
| 99 |
Directory: mi.Directory, |
| 100 |
Size: mi.Size, |
| 101 |
Header: hdr.Bytes(), |
| 102 |
Created: time.Now(), |
| 103 |
Labels: mi.Labels, |
| 104 |
} |
| 105 |
data := bytes.NewBuffer(nil) |
| 106 |
enc := gob.NewEncoder(data) |
| 107 |
err = enc.Encode(h) |
| 108 |
if err != nil { |
| 109 |
w.worker.Errorf("cannot encode message %s: %v", key, err) |
| 110 |
return |
| 111 |
} |
| 112 |
err = w.cache.Put(key, data.Bytes(), nil) |
| 113 |
if err != nil { |
| 114 |
w.worker.Errorf("cannot write header for message %s: %v", key, err) |
| 115 |
return |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []models.UID { |
| 120 |
w.worker.Tracef("Retrieving headers from cache: %v", msg.Uids) |
| 121 |
var need []models.UID |
| 122 |
for _, uid := range msg.Uids { |
| 123 |
key := w.headerKey(uid) |
| 124 |
data, err := w.cache.Get(key, nil) |
| 125 |
if err != nil { |
| 126 |
need = append(need, uid) |
| 127 |
continue |
| 128 |
} |
| 129 |
ch := &CachedHeader{} |
| 130 |
dec := gob.NewDecoder(bytes.NewReader(data)) |
| 131 |
err = dec.Decode(ch) |
| 132 |
if err != nil { |
| 133 |
w.worker.Errorf("cannot decode cached header %s: %v", key, err) |
| 134 |
need = append(need, uid) |
| 135 |
continue |
| 136 |
} |
| 137 |
hr := bytes.NewReader(ch.Header) |
| 138 |
textprotoHeader, err := textproto.ReadHeader(bufio.NewReader(hr)) |
| 139 |
if err != nil { |
| 140 |
w.worker.Errorf("cannot read cached header %s: %v", key, err) |
| 141 |
need = append(need, uid) |
| 142 |
continue |
| 143 |
} |
| 144 |
|
| 145 |
hdr := &mail.Header{Header: message.Header{Header: textprotoHeader}} |
| 146 |
mi := &models.MessageInfo{ |
| 147 |
BodyStructure: &ch.BodyStructure, |
| 148 |
Envelope: &ch.Envelope, |
| 149 |
Flags: models.SeenFlag, // Always return a SEEN flag |
| 150 |
Uid: ch.Uid, |
| 151 |
Directory: ch.Directory, |
| 152 |
RFC822Headers: hdr, |
| 153 |
Refs: parse.MsgIDList(hdr, "references"), |
| 154 |
Size: ch.Size, |
| 155 |
Labels: ch.Labels, |
| 156 |
} |
| 157 |
w.worker.PostMessage(&types.MessageInfo{ |
| 158 |
Message: types.RespondTo(msg), |
| 159 |
Info: mi, |
| 160 |
NeedsFlags: true, |
| 161 |
}, nil) |
| 162 |
} |
| 163 |
return need |
| 164 |
} |
| 165 |
|
| 166 |
func (w *IMAPWorker) headerKey(uid models.UID) []byte { |
| 167 |
key := fmt.Sprintf("header.%s.%d.%s", |
| 168 |
w.selected.Name, w.selected.UidValidity, uid) |
| 169 |
return []byte(key) |
| 170 |
} |
| 171 |
|
| 172 |
// cleanCache removes stale entries from the selected mailbox cachedb |
| 173 |
func (w *IMAPWorker) cleanCache(path string) { |
| 174 |
defer log.PanicHandler() |
| 175 |
start := time.Now() |
| 176 |
var scanned, removed int |
| 177 |
iter := w.cache.NewIterator(nil, nil) |
| 178 |
for iter.Next() { |
| 179 |
if bytes.Equal(iter.Key(), cacheTagKey) { |
| 180 |
continue |
| 181 |
} |
| 182 |
data := iter.Value() |
| 183 |
ch := &CachedHeader{} |
| 184 |
dec := gob.NewDecoder(bytes.NewReader(data)) |
| 185 |
err := dec.Decode(ch) |
| 186 |
if err != nil { |
| 187 |
w.worker.Errorf("cannot clean database %d: %v", |
| 188 |
w.selected.UidValidity, err) |
| 189 |
continue |
| 190 |
} |
| 191 |
exp := ch.Created.Add(w.config.cacheMaxAge) |
| 192 |
if exp.Before(time.Now()) { |
| 193 |
err = w.cache.Delete(iter.Key(), nil) |
| 194 |
if err != nil { |
| 195 |
w.worker.Errorf("cannot clean database %d: %v", |
| 196 |
w.selected.UidValidity, err) |
| 197 |
continue |
| 198 |
} |
| 199 |
removed++ |
| 200 |
} |
| 201 |
scanned++ |
| 202 |
} |
| 203 |
iter.Release() |
| 204 |
elapsed := time.Since(start) |
| 205 |
w.worker.Debugf("%s: removed %d/%d expired entries in %s", |
| 206 |
path, removed, scanned, elapsed) |
| 207 |
} |
| 208 |
|
| 209 |
// clearCache clears the entire cache |
| 210 |
func (w *IMAPWorker) clearCache() { |
| 211 |
iter := w.cache.NewIterator(nil, nil) |
| 212 |
for iter.Next() { |
| 213 |
if err := w.cache.Delete(iter.Key(), nil); err != nil { |
| 214 |
w.worker.Errorf("error clearing cache: %v", err) |
| 215 |
} |
| 216 |
} |
| 217 |
iter.Release() |
| 218 |
} |