2018-03-13 14:27:12 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-03-13 14:27:12 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-03-13 14:27:12 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-03-13 14:27:12 +01:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
package config
|
2018-03-13 14:27:12 +01:00
|
|
|
|
|
|
|
import (
|
2019-01-17 13:13:25 +01:00
|
|
|
"encoding/json"
|
2018-03-13 14:27:12 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-07-02 09:00:42 +02:00
|
|
|
"strings"
|
|
|
|
|
2018-03-13 14:27:12 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-01-17 13:13:25 +01:00
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
"maunium.net/go/mautrix/pushrules"
|
2019-01-17 13:13:25 +01:00
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-05-22 16:24:47 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-03-13 14:27:12 +01:00
|
|
|
)
|
|
|
|
|
2018-05-24 22:26:57 +02:00
|
|
|
type AuthCache struct {
|
|
|
|
NextBatch string `yaml:"next_batch"`
|
|
|
|
FilterID string `yaml:"filter_id"`
|
2020-11-29 01:05:11 +01:00
|
|
|
FilterVersion int `yaml:"filter_version"`
|
2018-05-24 22:26:57 +02:00
|
|
|
InitialSyncDone bool `yaml:"initial_sync_done"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserPreferences struct {
|
2020-04-16 18:42:55 +02:00
|
|
|
HideUserList bool `yaml:"hide_user_list"`
|
|
|
|
HideRoomList bool `yaml:"hide_room_list"`
|
|
|
|
BareMessageView bool `yaml:"bare_message_view"`
|
|
|
|
DisableImages bool `yaml:"disable_images"`
|
|
|
|
DisableTypingNotifs bool `yaml:"disable_typing_notifs"`
|
|
|
|
DisableEmojis bool `yaml:"disable_emojis"`
|
|
|
|
DisableMarkdown bool `yaml:"disable_markdown"`
|
|
|
|
DisableHTML bool `yaml:"disable_html"`
|
|
|
|
DisableDownloads bool `yaml:"disable_downloads"`
|
|
|
|
DisableNotifications bool `yaml:"disable_notifications"`
|
2020-08-17 00:57:25 +02:00
|
|
|
DisableShowURLs bool `yaml:"disable_show_urls"`
|
2021-03-11 21:42:46 +01:00
|
|
|
AltEnterToSend bool `yaml:"alt_enter_to_send"`
|
2018-05-24 22:26:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:39:17 +01:00
|
|
|
// Config contains the main config of gomuks.
|
2018-03-13 14:27:12 +01:00
|
|
|
type Config struct {
|
2020-04-26 23:38:04 +02:00
|
|
|
UserID id.UserID `yaml:"mxid"`
|
|
|
|
DeviceID id.DeviceID `yaml:"device_id"`
|
|
|
|
AccessToken string `yaml:"access_token"`
|
|
|
|
HS string `yaml:"homeserver"`
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
RoomCacheSize int `yaml:"room_cache_size"`
|
|
|
|
RoomCacheAge int64 `yaml:"room_cache_age"`
|
|
|
|
|
2020-07-25 19:54:32 +02:00
|
|
|
NotifySound bool `yaml:"notify_sound"`
|
|
|
|
SendToVerifiedOnly bool `yaml:"send_to_verified_only"`
|
2020-03-01 11:49:32 +01:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
Dir string `yaml:"-"`
|
2020-04-28 21:00:37 +02:00
|
|
|
DataDir string `yaml:"data_dir"`
|
2019-06-15 00:11:51 +02:00
|
|
|
CacheDir string `yaml:"cache_dir"`
|
|
|
|
HistoryPath string `yaml:"history_path"`
|
|
|
|
RoomListPath string `yaml:"room_list_path"`
|
|
|
|
MediaDir string `yaml:"media_dir"`
|
2020-04-08 14:30:29 +02:00
|
|
|
DownloadDir string `yaml:"download_dir"`
|
2019-06-15 00:11:51 +02:00
|
|
|
StateDir string `yaml:"state_dir"`
|
2018-05-17 15:29:15 +02:00
|
|
|
|
2018-05-24 22:26:57 +02:00
|
|
|
Preferences UserPreferences `yaml:"-"`
|
|
|
|
AuthCache AuthCache `yaml:"-"`
|
2019-06-15 00:11:51 +02:00
|
|
|
Rooms *rooms.RoomCache `yaml:"-"`
|
2018-05-24 22:26:57 +02:00
|
|
|
PushRules *pushrules.PushRuleset `yaml:"-"`
|
2018-05-17 15:29:15 +02:00
|
|
|
|
|
|
|
nosave bool
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:39:17 +01:00
|
|
|
// NewConfig creates a config that loads data from the given directory.
|
2020-04-28 21:00:37 +02:00
|
|
|
func NewConfig(configDir, dataDir, cacheDir, downloadDir string) *Config {
|
2018-03-13 20:58:43 +01:00
|
|
|
return &Config{
|
2019-06-15 00:11:51 +02:00
|
|
|
Dir: configDir,
|
2020-04-28 21:00:37 +02:00
|
|
|
DataDir: dataDir,
|
2019-06-15 00:11:51 +02:00
|
|
|
CacheDir: cacheDir,
|
2020-04-08 14:49:42 +02:00
|
|
|
DownloadDir: downloadDir,
|
2019-06-15 00:11:51 +02:00
|
|
|
HistoryPath: filepath.Join(cacheDir, "history.db"),
|
|
|
|
RoomListPath: filepath.Join(cacheDir, "rooms.gob.gz"),
|
|
|
|
StateDir: filepath.Join(cacheDir, "state"),
|
|
|
|
MediaDir: filepath.Join(cacheDir, "media"),
|
|
|
|
|
|
|
|
RoomCacheSize: 32,
|
|
|
|
RoomCacheAge: 1 * 60,
|
2020-03-01 11:49:32 +01:00
|
|
|
|
2020-07-25 19:54:32 +02:00
|
|
|
NotifySound: true,
|
|
|
|
SendToVerifiedOnly: false,
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 22:39:17 +01:00
|
|
|
// Clear clears the session cache and removes all history.
|
2018-03-22 18:51:20 +01:00
|
|
|
func (config *Config) Clear() {
|
2019-06-15 00:11:51 +02:00
|
|
|
_ = os.Remove(config.HistoryPath)
|
|
|
|
_ = os.Remove(config.RoomListPath)
|
|
|
|
_ = os.RemoveAll(config.StateDir)
|
|
|
|
_ = os.RemoveAll(config.MediaDir)
|
|
|
|
_ = os.RemoveAll(config.CacheDir)
|
2018-05-17 15:29:15 +02:00
|
|
|
config.nosave = true
|
2018-03-22 18:51:20 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 21:00:37 +02:00
|
|
|
// ClearData clears non-temporary session data.
|
|
|
|
func (config *Config) ClearData() {
|
|
|
|
_ = os.RemoveAll(config.DataDir)
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func (config *Config) CreateCacheDirs() {
|
2019-06-15 00:11:51 +02:00
|
|
|
_ = os.MkdirAll(config.CacheDir, 0700)
|
2020-04-28 21:00:37 +02:00
|
|
|
_ = os.MkdirAll(config.DataDir, 0700)
|
2019-06-15 00:11:51 +02:00
|
|
|
_ = os.MkdirAll(config.StateDir, 0700)
|
|
|
|
_ = os.MkdirAll(config.MediaDir, 0700)
|
2018-05-10 14:47:24 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func (config *Config) DeleteSession() {
|
|
|
|
config.AuthCache.NextBatch = ""
|
|
|
|
config.AuthCache.InitialSyncDone = false
|
2019-04-06 09:57:24 +02:00
|
|
|
config.AccessToken = ""
|
2020-04-27 23:58:26 +02:00
|
|
|
config.DeviceID = ""
|
2019-06-15 00:11:51 +02:00
|
|
|
config.Rooms = rooms.NewRoomCache(config.RoomListPath, config.StateDir, config.RoomCacheSize, config.RoomCacheAge, config.GetUserID)
|
2018-05-17 15:29:15 +02:00
|
|
|
config.PushRules = nil
|
|
|
|
|
2020-04-28 21:00:37 +02:00
|
|
|
config.ClearData()
|
2018-05-17 15:29:15 +02:00
|
|
|
config.Clear()
|
|
|
|
config.nosave = false
|
|
|
|
config.CreateCacheDirs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) LoadAll() {
|
|
|
|
config.Load()
|
2019-06-15 00:11:51 +02:00
|
|
|
config.Rooms = rooms.NewRoomCache(config.RoomListPath, config.StateDir, config.RoomCacheSize, config.RoomCacheAge, config.GetUserID)
|
2018-05-17 15:29:15 +02:00
|
|
|
config.LoadAuthCache()
|
|
|
|
config.LoadPushRules()
|
2018-05-24 22:26:57 +02:00
|
|
|
config.LoadPreferences()
|
2019-06-15 00:11:51 +02:00
|
|
|
err := config.Rooms.LoadList()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:39:17 +01:00
|
|
|
// Load loads the config from config.yaml in the directory given to the config struct.
|
2018-03-13 20:58:43 +01:00
|
|
|
func (config *Config) Load() {
|
2018-05-24 22:26:57 +02:00
|
|
|
config.load("config", config.Dir, "config.yaml", config)
|
2018-05-17 15:29:15 +02:00
|
|
|
config.CreateCacheDirs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) SaveAll() {
|
|
|
|
config.Save()
|
|
|
|
config.SaveAuthCache()
|
|
|
|
config.SavePushRules()
|
2018-05-24 22:26:57 +02:00
|
|
|
config.SavePreferences()
|
2019-06-15 00:11:51 +02:00
|
|
|
err := config.Rooms.SaveList()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
config.Rooms.SaveLoadedRooms()
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:39:17 +01:00
|
|
|
// Save saves this config to config.yaml in the directory given to the config struct.
|
2018-03-13 14:27:12 +01:00
|
|
|
func (config *Config) Save() {
|
2018-05-24 22:26:57 +02:00
|
|
|
config.save("config", config.Dir, "config.yaml", config)
|
|
|
|
}
|
2018-05-17 15:29:15 +02:00
|
|
|
|
2018-05-24 22:26:57 +02:00
|
|
|
func (config *Config) LoadPreferences() {
|
|
|
|
config.load("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences)
|
|
|
|
}
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2018-05-24 22:26:57 +02:00
|
|
|
func (config *Config) SavePreferences() {
|
|
|
|
config.save("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences)
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
2018-05-17 15:29:15 +02:00
|
|
|
|
|
|
|
func (config *Config) LoadAuthCache() {
|
2018-05-24 22:26:57 +02:00
|
|
|
config.load("auth cache", config.CacheDir, "auth-cache.yaml", &config.AuthCache)
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) SaveAuthCache() {
|
2018-05-24 22:26:57 +02:00
|
|
|
config.save("auth cache", config.CacheDir, "auth-cache.yaml", &config.AuthCache)
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) LoadPushRules() {
|
2018-05-24 22:26:57 +02:00
|
|
|
config.load("push rules", config.CacheDir, "pushrules.json", &config.PushRules)
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) SavePushRules() {
|
2018-05-24 22:26:57 +02:00
|
|
|
if config.PushRules == nil {
|
2018-05-17 15:29:15 +02:00
|
|
|
return
|
|
|
|
}
|
2018-05-24 22:26:57 +02:00
|
|
|
config.save("push rules", config.CacheDir, "pushrules.json", &config.PushRules)
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (config *Config) load(name, dir, file string, target interface{}) {
|
|
|
|
err := os.MkdirAll(dir, 0700)
|
2018-05-17 15:29:15 +02:00
|
|
|
if err != nil {
|
2019-06-15 00:11:51 +02:00
|
|
|
debug.Print("Failed to create", dir)
|
2018-05-17 15:29:15 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-05-24 22:26:57 +02:00
|
|
|
path := filepath.Join(dir, file)
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
debug.Print("Failed to read", name, "from", path)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(file, ".yaml") {
|
|
|
|
err = yaml.Unmarshal(data, target)
|
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(data, target)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to parse", name, "at", path)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) save(name, dir, file string, source interface{}) {
|
|
|
|
if config.nosave {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
err := os.MkdirAll(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to create", dir)
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-05-24 22:26:57 +02:00
|
|
|
var data []byte
|
|
|
|
if strings.HasSuffix(file, ".yaml") {
|
|
|
|
data, err = yaml.Marshal(source)
|
|
|
|
} else {
|
|
|
|
data, err = json.Marshal(source)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to marshal", name)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(dir, file)
|
|
|
|
err = ioutil.WriteFile(path, data, 0600)
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to write", name, "to", path)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (config *Config) GetUserID() id.UserID {
|
2018-05-17 15:29:15 +02:00
|
|
|
return config.UserID
|
|
|
|
}
|
|
|
|
|
2020-11-29 01:05:11 +01:00
|
|
|
const FilterVersion = 1
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (config *Config) SaveFilterID(_ id.UserID, filterID string) {
|
2018-05-17 15:29:15 +02:00
|
|
|
config.AuthCache.FilterID = filterID
|
2020-11-29 01:05:11 +01:00
|
|
|
config.AuthCache.FilterVersion = FilterVersion
|
2018-05-17 15:29:15 +02:00
|
|
|
config.SaveAuthCache()
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (config *Config) LoadFilterID(_ id.UserID) string {
|
2020-11-29 01:05:11 +01:00
|
|
|
if config.AuthCache.FilterVersion != FilterVersion {
|
|
|
|
return ""
|
|
|
|
}
|
2018-05-17 15:29:15 +02:00
|
|
|
return config.AuthCache.FilterID
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (config *Config) SaveNextBatch(_ id.UserID, nextBatch string) {
|
2018-05-17 15:29:15 +02:00
|
|
|
config.AuthCache.NextBatch = nextBatch
|
|
|
|
config.SaveAuthCache()
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (config *Config) LoadNextBatch(_ id.UserID) string {
|
2018-05-17 15:29:15 +02:00
|
|
|
return config.AuthCache.NextBatch
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (config *Config) SaveRoom(_ *mautrix.Room) {
|
2019-06-15 00:11:51 +02:00
|
|
|
panic("SaveRoom is not supported")
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (config *Config) LoadRoom(_ id.RoomID) *mautrix.Room {
|
2019-06-15 00:11:51 +02:00
|
|
|
panic("LoadRoom is not supported")
|
|
|
|
}
|