-
Notifications
You must be signed in to change notification settings - Fork 2
/
store_address_mode.go
112 lines (89 loc) · 2.99 KB
/
store_address_mode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2022 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package store
import (
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
)
type addressMode string
const (
splitMode addressMode = "split"
combinedMode addressMode = "combined"
modeKey = "mode"
)
// getAddressMode returns the current address mode (split or combined) of the store.
// It first looks in the local cache but if that is not yet set, it loads it from the database.
func (store *Store) getAddressMode() (mode addressMode, err error) {
if store.addressMode != "" {
mode = store.addressMode
return
}
tx := func(tx *bolt.Tx) (err error) {
b := tx.Bucket(addressModeBucket)
dbMode := b.Get([]byte(modeKey))
if dbMode == nil {
return errors.New("address mode not set")
}
mode = addressMode(dbMode)
return
}
err = store.db.View(tx)
return
}
// IsCombinedMode returns whether the store is set to combined mode.
func (store *Store) IsCombinedMode() bool {
return store.addressMode == combinedMode
}
// UseCombinedMode sets whether the store should be set to combined mode.
func (store *Store) UseCombinedMode(useCombined bool) (err error) {
if useCombined {
err = store.switchAddressMode(combinedMode)
} else {
err = store.switchAddressMode(splitMode)
}
return
}
// switchAddressMode sets the address mode to the given value and rebuilds the mailboxes.
func (store *Store) switchAddressMode(mode addressMode) (err error) {
if store.addressMode == mode {
log.Debug("The store is using the correct address mode")
return
}
if err = store.setAddressMode(mode); err != nil {
log.WithError(err).Error("Could not set store address mode")
return
}
if err = store.RebuildMailboxes(); err != nil {
log.WithError(err).Error("Could not rebuild mailboxes after switching address mode")
return
}
return
}
// setAddressMode sets the current address mode (split or combined) of the store.
// It writes to database and updates the local value in the store object.
func (store *Store) setAddressMode(mode addressMode) (err error) {
store.log.WithField("mode", string(mode)).Info("Setting store address mode")
tx := func(tx *bolt.Tx) (err error) {
b := tx.Bucket(addressModeBucket)
return b.Put([]byte(modeKey), []byte(mode))
}
if err = store.db.Update(tx); err != nil {
return
}
store.addressMode = mode
return
}