From a05ba5e2734f1b195d0b4a4db56de8165be99c4d Mon Sep 17 00:00:00 2001 From: Lukasz Janyst Date: Mon, 2 May 2022 19:16:36 +0200 Subject: [PATCH] cleanup: Make settings read only Issue #6 Issue #3 --- pkg/bridge/bridge.go | 62 --------------------------------- pkg/bridge/types.go | 2 -- pkg/bridge/utils.go | 11 +----- pkg/config/settings/kvs.go | 21 ++--------- pkg/config/settings/kvs_test.go | 21 +---------- pkg/smtp/user.go | 31 +---------------- 6 files changed, 5 insertions(+), 143 deletions(-) diff --git a/pkg/bridge/bridge.go b/pkg/bridge/bridge.go index c3d6af8..f165808 100644 --- a/pkg/bridge/bridge.go +++ b/pkg/bridge/bridge.go @@ -61,7 +61,6 @@ type Bridge struct { func (b *Bridge) Configure(configFile string) error { rand.Seed(time.Now().UnixNano()) - os.Args = StripProcessSerialNumber(os.Args) if err := logging.Init(); err != nil { return err @@ -103,9 +102,6 @@ func (b *Bridge) Configure(configFile string) error { cm.SetCookieJar(jar) - // GODT-1481: Always turn off reporting of unencrypted recipient in v2. - settingsObj.SetBool(settings.ReportOutgoingNoEncKey, false) - cache, err := LoadMessageCache(settingsObj, cacheConf) if err != nil { return err @@ -181,61 +177,3 @@ func (b *Bridge) FactoryReset() { log.WithError(err).Error("Failed to remove bridge users") } } - -// GetKeychainApp returns current keychain helper. -func (b *Bridge) GetKeychainApp() string { - return b.settings.Get(settings.PreferredKeychainKey) -} - -// SetKeychainApp sets current keychain helper. -func (b *Bridge) SetKeychainApp(helper string) { - b.settings.Set(settings.PreferredKeychainKey, helper) -} - -func (b *Bridge) EnableCache() error { - if err := b.Users.EnableCache(); err != nil { - return err - } - - b.settings.SetBool(settings.CacheEnabledKey, true) - - return nil -} - -func (b *Bridge) DisableCache() error { - if err := b.Users.DisableCache(); err != nil { - return err - } - - b.settings.SetBool(settings.CacheEnabledKey, false) - // Reset back to the default location when disabling. - b.settings.Set(settings.CacheLocationKey, b.cacheProvider.GetDefaultMessageCacheDir()) - - return nil -} - -func (b *Bridge) MigrateCache(from, to string) error { - if err := b.Users.MigrateCache(from, to); err != nil { - return err - } - - b.settings.Set(settings.CacheLocationKey, to) - - return nil -} - -// SetProxyAllowed instructs the app whether to use DoH to access an API proxy if necessary. -// It also needs to work before the app is initialised (because we may need to use the proxy at startup). -func (b *Bridge) SetProxyAllowed(proxyAllowed bool) { - b.settings.SetBool(settings.AllowProxyKey, proxyAllowed) - if proxyAllowed { - b.clientManager.AllowProxy() - } else { - b.clientManager.DisallowProxy() - } -} - -// GetProxyAllowed returns whether use of DoH is enabled to access an API proxy if necessary. -func (b *Bridge) GetProxyAllowed() bool { - return b.settings.GetBool(settings.AllowProxyKey) -} diff --git a/pkg/bridge/types.go b/pkg/bridge/types.go index 83a8d43..0e29910 100644 --- a/pkg/bridge/types.go +++ b/pkg/bridge/types.go @@ -31,8 +31,6 @@ type CacheProvider interface { type SettingsProvider interface { Get(key string) string - Set(key string, value string) GetBool(key string) bool - SetBool(key string, val bool) GetInt(key string) int } diff --git a/pkg/bridge/utils.go b/pkg/bridge/utils.go index 5abd0ca..9e5ca09 100644 --- a/pkg/bridge/utils.go +++ b/pkg/bridge/utils.go @@ -93,16 +93,7 @@ func LoadMessageCache(s *settings.Settings, cfg *cfgCache.Cache) (cache.Cache, e compressor = &cache.NoopCompressor{} } - var path string - - if customPath := s.Get(settings.CacheLocationKey); customPath != "" { - path = customPath - } else { - path = cfg.GetDefaultMessageCacheDir() - // Store path so it will allways persist if default location - // will be changed in new version. - s.Set(settings.CacheLocationKey, path) - } + path := cfg.GetDefaultMessageCacheDir() // To prevent memory peaks we set maximal write concurency for store // build jobs. diff --git a/pkg/config/settings/kvs.go b/pkg/config/settings/kvs.go index 27ff4d6..1ea9256 100644 --- a/pkg/config/settings/kvs.go +++ b/pkg/config/settings/kvs.go @@ -20,7 +20,6 @@ package settings import ( "encoding/json" "errors" - "fmt" "io/ioutil" "os" "strconv" @@ -84,7 +83,7 @@ func (p *keyValueStore) save() error { func (p *keyValueStore) setDefault(key, value string) { if p.Get(key) == "" { - p.Set(key, value) + p.set(key, value) } } @@ -125,7 +124,7 @@ func (p *keyValueStore) GetFloat64(key string) float64 { return value } -func (p *keyValueStore) Set(key, value string) { +func (p *keyValueStore) set(key, value string) { p.lock.Lock() p.cache[key] = value p.lock.Unlock() @@ -134,19 +133,3 @@ func (p *keyValueStore) Set(key, value string) { logrus.WithError(err).Warn("Cannot save preferences") } } - -func (p *keyValueStore) SetBool(key string, value bool) { - if value { - p.Set(key, "true") - } else { - p.Set(key, "false") - } -} - -func (p *keyValueStore) SetInt(key string, value int) { - p.Set(key, strconv.Itoa(value)) -} - -func (p *keyValueStore) SetFloat64(key string, value float64) { - p.Set(key, fmt.Sprintf("%v", value)) -} diff --git a/pkg/config/settings/kvs_test.go b/pkg/config/settings/kvs_test.go index 6de6094..b6a9256 100644 --- a/pkg/config/settings/kvs_test.go +++ b/pkg/config/settings/kvs_test.go @@ -91,29 +91,10 @@ func TestKeyValueStoreSet(t *testing.T) { pref, clean := newTestEmptyKeyValueStore(r) defer clean() - pref.Set("str", "value") + pref.set("str", "value") checkSavedKeyValueStore(r, pref.path, "{\n\t\"str\": \"value\"\n}") } -func TestKeyValueStoreSetInt(t *testing.T) { - r := require.New(t) - pref, clean := newTestEmptyKeyValueStore(r) - defer clean() - - pref.SetInt("int", 42) - checkSavedKeyValueStore(r, pref.path, "{\n\t\"int\": \"42\"\n}") -} - -func TestKeyValueStoreSetBool(t *testing.T) { - r := require.New(t) - pref, clean := newTestEmptyKeyValueStore(r) - defer clean() - - pref.SetBool("trueBool", true) - pref.SetBool("falseBool", false) - checkSavedKeyValueStore(r, pref.path, "{\n\t\"falseBool\": \"false\",\n\t\"trueBool\": \"true\"\n}") -} - func newTmpFile(r *require.Assertions) (path string, clean func()) { tmpfile, err := ioutil.TempFile("", "pref.*.json") r.NoError(err) diff --git a/pkg/smtp/user.go b/pkg/smtp/user.go index f6e4465..805ce42 100644 --- a/pkg/smtp/user.go +++ b/pkg/smtp/user.go @@ -32,14 +32,12 @@ import ( "github.com/ProtonMail/gopenpgp/v2/crypto" goSMTPBackend "github.com/emersion/go-smtp" - "github.com/ljanyst/peroxide/pkg/events" "github.com/ljanyst/peroxide/pkg/listener" pkgMsg "github.com/ljanyst/peroxide/pkg/message" "github.com/ljanyst/peroxide/pkg/message/parser" "github.com/ljanyst/peroxide/pkg/pmapi" "github.com/ljanyst/peroxide/pkg/users" "github.com/pkg/errors" - "github.com/sirupsen/logrus" ) type smtpUser struct { @@ -385,16 +383,10 @@ func (su *smtpUser) Send(returnPath string, to []string, messageReader io.Reader if containsUnencryptedRecipients { dec := new(mime.WordDecoder) - subject, err := dec.DecodeHeader(message.Header.Get("Subject")) + _, err := dec.DecodeHeader(message.Header.Get("Subject")) if err != nil { return errors.New("error decoding subject message " + message.Header.Get("Subject")) } - if !su.continueSendingUnencryptedMail(subject) { - if err := su.client().DeleteMessages(context.TODO(), []string{message.ID}); err != nil { - log.WithError(err).Warn("Failed to delete canceled messages") - } - return errors.New("sending was canceled by user") - } } req.PreparePackages() @@ -505,27 +497,6 @@ func (su *smtpUser) handleSenderAndRecipients(m *pmapi.Message, returnPathAddr * return nil } -func (su *smtpUser) continueSendingUnencryptedMail(subject string) bool { - if !su.backend.shouldReportOutgoingNoEnc() { - return true - } - - // GUI should always respond in 10 seconds, but let's have safety timeout - // in case GUI will not respond properly. If GUI didn't respond, we cannot - // be sure if user even saw the notice: better to not send the e-mail. - req := su.backend.confirmer.NewRequest(15 * time.Second) - - su.eventListener.Emit(events.OutgoingNoEncEvent, req.ID()+":"+subject) - - res, err := req.Result() - if err != nil { - logrus.WithError(err).Error("Failed to determine whether to send unencrypted, assuming no") - return false - } - - return res -} - // Logout is called when this User will no longer be used. func (su *smtpUser) Logout() error { log.Debug("SMTP client logged out user ", su.addressID)