diff options
author | Robin Jarry <robin@jarry.cc> | 2023-06-04 14:05:22 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-06-21 17:29:21 +0200 |
commit | be0bfc1ae28b49be6546626ff9eaadce5464a6c8 (patch) | |
tree | 60b9d8b812408db281c00d5a71197e118a736337 /worker/jmap/cache/cache.go | |
parent | b4ae11b4ec20b61a18e1ce08ea65014bcebc2f16 (diff) | |
download | aerc-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/cache/cache.go')
-rw-r--r-- | worker/jmap/cache/cache.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/worker/jmap/cache/cache.go b/worker/jmap/cache/cache.go new file mode 100644 index 00000000..ab264744 --- /dev/null +++ b/worker/jmap/cache/cache.go @@ -0,0 +1,79 @@ +package cache + +import ( + "errors" + "os" + "path" + + "github.com/mitchellh/go-homedir" + "github.com/syndtr/goleveldb/leveldb" +) + +type JMAPCache struct { + mem map[string][]byte + file *leveldb.DB + blobsDir string +} + +func NewJMAPCache(state, blobs bool, accountName string) (*JMAPCache, error) { + c := new(JMAPCache) + cacheDir, err := os.UserCacheDir() + if err != nil { + cacheDir, err = homedir.Expand("~/.cache") + if err != nil { + return nil, err + } + } + if state { + dir := path.Join(cacheDir, "aerc", accountName, "state") + _ = os.MkdirAll(dir, 0o700) + c.file, err = leveldb.OpenFile(dir, nil) + if err != nil { + return nil, err + } + } else { + c.mem = make(map[string][]byte) + } + if blobs { + c.blobsDir = path.Join(cacheDir, "aerc", accountName, "blobs") + } + return c, nil +} + +var notfound = errors.New("key not found") + +func (c *JMAPCache) get(key string) ([]byte, error) { + switch { + case c.file != nil: + return c.file.Get([]byte(key), nil) + case c.mem != nil: + value, ok := c.mem[key] + if !ok { + return nil, notfound + } + return value, nil + } + panic("jmap cache with no backend") +} + +func (c *JMAPCache) put(key string, value []byte) error { + switch { + case c.file != nil: + return c.file.Put([]byte(key), value, nil) + case c.mem != nil: + c.mem[key] = value + return nil + } + panic("jmap cache with no backend") +} + +func (c *JMAPCache) delete(key string) error { + switch { + case c.file != nil: + return c.file.Delete([]byte(key), nil) + case c.mem != nil: + delete(c.mem, key) + return nil + } + panic("jmap cache with no backend") +} |