2020-05-13 12:07:44 +00:00
|
|
|
package alps
|
2020-01-28 19:04:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
imapmetadata "github.com/emersion/go-imap-metadata"
|
|
|
|
imapclient "github.com/emersion/go-imap/client"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNoStoreEntry is returned by Store.Get when the entry doesn't exist.
|
2020-05-13 12:07:44 +00:00
|
|
|
var ErrNoStoreEntry = fmt.Errorf("alps: no such entry in store")
|
2020-01-28 19:04:18 +00:00
|
|
|
|
|
|
|
// Store allows storing per-user persistent data.
|
|
|
|
//
|
|
|
|
// Store shouldn't be used from inside Session.DoIMAP.
|
|
|
|
type Store interface {
|
|
|
|
Get(key string, out interface{}) error
|
|
|
|
Put(key string, v interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var warnedTransientStore = false
|
|
|
|
|
|
|
|
func newStore(session *Session, logger echo.Logger) (Store, error) {
|
|
|
|
s, err := newIMAPStore(session)
|
|
|
|
if err == nil {
|
|
|
|
return s, nil
|
|
|
|
} else if err != errIMAPMetadataUnsupported {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !warnedTransientStore {
|
|
|
|
logger.Print("Upstream IMAP server doesn't support the METADATA extension, using transient store instead")
|
|
|
|
warnedTransientStore = true
|
|
|
|
}
|
|
|
|
return newMemoryStore(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type memoryStore struct {
|
|
|
|
locker sync.RWMutex
|
|
|
|
entries map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMemoryStore() *memoryStore {
|
|
|
|
return &memoryStore{entries: make(map[string]interface{})}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memoryStore) Get(key string, out interface{}) error {
|
|
|
|
s.locker.RLock()
|
|
|
|
defer s.locker.RUnlock()
|
|
|
|
|
|
|
|
v, ok := s.entries[key]
|
|
|
|
if !ok {
|
|
|
|
return ErrNoStoreEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
reflect.ValueOf(out).Elem().Set(reflect.ValueOf(v).Elem())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memoryStore) Put(key string, v interface{}) error {
|
|
|
|
s.locker.Lock()
|
|
|
|
s.entries[key] = v
|
|
|
|
s.locker.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type imapStore struct {
|
|
|
|
session *Session
|
|
|
|
cache *memoryStore
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
var errIMAPMetadataUnsupported = fmt.Errorf("alps: IMAP server doesn't support METADATA extension")
|
2020-01-28 19:04:18 +00:00
|
|
|
|
|
|
|
func newIMAPStore(session *Session) (*imapStore, error) {
|
|
|
|
err := session.DoIMAP(func(c *imapclient.Client) error {
|
|
|
|
mc := imapmetadata.NewClient(c)
|
|
|
|
ok, err := mc.SupportMetadata()
|
|
|
|
if err != nil {
|
2020-05-13 12:07:44 +00:00
|
|
|
return fmt.Errorf("alps: failed to check for IMAP METADATA support: %v", err)
|
2020-01-28 19:04:18 +00:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return errIMAPMetadataUnsupported
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &imapStore{session, newMemoryStore()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *imapStore) key(key string) string {
|
2020-05-13 12:07:44 +00:00
|
|
|
return "/private/vendor/alps/" + key
|
2020-01-28 19:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *imapStore) Get(key string, out interface{}) error {
|
|
|
|
if err := s.cache.Get(key, out); err != ErrNoStoreEntry {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var entries map[string]string
|
|
|
|
err := s.session.DoIMAP(func(c *imapclient.Client) error {
|
|
|
|
mc := imapmetadata.NewClient(c)
|
|
|
|
var err error
|
|
|
|
entries, err = mc.GetMetadata("", []string{s.key(key)}, nil)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-13 12:07:44 +00:00
|
|
|
return fmt.Errorf("alps: failed to fetch IMAP store entry %q: %v", key, err)
|
2020-01-28 19:04:18 +00:00
|
|
|
}
|
|
|
|
v, ok := entries[s.key(key)]
|
|
|
|
if !ok {
|
|
|
|
return ErrNoStoreEntry
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(v), out); err != nil {
|
2020-05-13 12:07:44 +00:00
|
|
|
return fmt.Errorf("alps: failed to unmarshal IMAP store entry %q: %v", key, err)
|
2020-01-28 19:04:18 +00:00
|
|
|
}
|
|
|
|
return s.cache.Put(key, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *imapStore) Put(key string, v interface{}) error {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
2020-05-13 12:07:44 +00:00
|
|
|
return fmt.Errorf("alps: failed to marshal IMAP store entry %q: %v", key, err)
|
2020-01-28 19:04:18 +00:00
|
|
|
}
|
|
|
|
entries := map[string]string{
|
|
|
|
s.key(key): string(b),
|
|
|
|
}
|
|
|
|
err = s.session.DoIMAP(func(c *imapclient.Client) error {
|
|
|
|
mc := imapmetadata.NewClient(c)
|
|
|
|
return mc.SetMetadata("", entries)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-13 12:07:44 +00:00
|
|
|
return fmt.Errorf("alps: failed to put IMAP store entry %q: %v", key, err)
|
2020-01-28 19:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.cache.Put(key, v)
|
|
|
|
}
|