gomuks/config/config.go

288 lines
7.6 KiB
Go
Raw Normal View History

// gomuks - A terminal Matrix client written in Go.
2019-01-17 13:13:25 +01:00
// Copyright (C) 2019 Tulir Asokan
//
// 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
// 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.
//
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-18 20:24:03 +01:00
package config
import (
2019-01-17 13:13:25 +01:00
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
2018-07-02 09:00:42 +02:00
"strings"
"gopkg.in/yaml.v2"
2019-01-17 13:13:25 +01:00
2018-11-13 23:00:35 +01:00
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/pushrules"
2019-01-17 13:13:25 +01:00
"maunium.net/go/gomuks/debug"
2018-05-22 16:24:47 +02:00
"maunium.net/go/gomuks/matrix/rooms"
)
type AuthCache struct {
NextBatch string `yaml:"next_batch"`
FilterID string `yaml:"filter_id"`
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"`
}
2018-03-23 22:39:17 +01:00
// Config contains the main config of gomuks.
type Config struct {
UserID id.UserID `yaml:"mxid"`
AccessToken string `yaml:"access_token"`
HS string `yaml:"homeserver"`
2019-06-15 00:11:51 +02:00
RoomCacheSize int `yaml:"room_cache_size"`
RoomCacheAge int64 `yaml:"room_cache_age"`
2020-03-01 11:49:32 +01:00
NotifySound bool `yaml:"notify_sound"`
2019-06-15 00:11:51 +02:00
Dir string `yaml:"-"`
CacheDir string `yaml:"cache_dir"`
HistoryPath string `yaml:"history_path"`
RoomListPath string `yaml:"room_list_path"`
MediaDir string `yaml:"media_dir"`
DownloadDir string `yaml:"download_dir"`
2019-06-15 00:11:51 +02:00
StateDir string `yaml:"state_dir"`
Preferences UserPreferences `yaml:"-"`
AuthCache AuthCache `yaml:"-"`
2019-06-15 00:11:51 +02:00
Rooms *rooms.RoomCache `yaml:"-"`
PushRules *pushrules.PushRuleset `yaml:"-"`
nosave bool
}
2018-03-23 22:39:17 +01:00
// NewConfig creates a config that loads data from the given directory.
func NewConfig(configDir, cacheDir, downloadDir string) *Config {
2018-03-13 20:58:43 +01:00
return &Config{
2019-06-15 00:11:51 +02:00
Dir: configDir,
CacheDir: cacheDir,
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
NotifySound: true,
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)
config.nosave = true
2018-03-22 18:51:20 +01:00
}
func (config *Config) CreateCacheDirs() {
2019-06-15 00:11:51 +02:00
_ = os.MkdirAll(config.CacheDir, 0700)
_ = os.MkdirAll(config.StateDir, 0700)
_ = os.MkdirAll(config.MediaDir, 0700)
2018-05-10 14:47:24 +02:00
}
func (config *Config) DeleteSession() {
config.AuthCache.NextBatch = ""
config.AuthCache.InitialSyncDone = false
2019-04-06 09:57:24 +02:00
config.AccessToken = ""
2019-06-15 00:11:51 +02:00
config.Rooms = rooms.NewRoomCache(config.RoomListPath, config.StateDir, config.RoomCacheSize, config.RoomCacheAge, config.GetUserID)
config.PushRules = nil
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)
config.LoadAuthCache()
config.LoadPushRules()
config.LoadPreferences()
2019-06-15 00:11:51 +02:00
err := config.Rooms.LoadList()
if err != nil {
panic(err)
}
}
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() {
config.load("config", config.Dir, "config.yaml", config)
config.CreateCacheDirs()
}
func (config *Config) SaveAll() {
config.Save()
config.SaveAuthCache()
config.SavePushRules()
config.SavePreferences()
2019-06-15 00:11:51 +02:00
err := config.Rooms.SaveList()
if err != nil {
panic(err)
}
config.Rooms.SaveLoadedRooms()
}
2018-03-23 22:39:17 +01:00
// Save saves this config to config.yaml in the directory given to the config struct.
func (config *Config) Save() {
config.save("config", config.Dir, "config.yaml", config)
}
func (config *Config) LoadPreferences() {
config.load("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences)
}
func (config *Config) SavePreferences() {
config.save("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences)
}
func (config *Config) LoadAuthCache() {
config.load("auth cache", config.CacheDir, "auth-cache.yaml", &config.AuthCache)
}
func (config *Config) SaveAuthCache() {
config.save("auth cache", config.CacheDir, "auth-cache.yaml", &config.AuthCache)
}
func (config *Config) LoadPushRules() {
config.load("push rules", config.CacheDir, "pushrules.json", &config.PushRules)
}
func (config *Config) SavePushRules() {
if config.PushRules == nil {
return
}
config.save("push rules", config.CacheDir, "pushrules.json", &config.PushRules)
}
2019-06-15 00:11:51 +02:00
func (config *Config) load(name, dir, file string, target interface{}) {
err := os.MkdirAll(dir, 0700)
if err != nil {
2019-06-15 00:11:51 +02:00
debug.Print("Failed to create", dir)
panic(err)
}
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)
}
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)
}
}
func (config *Config) GetUserID() id.UserID {
return config.UserID
}
func (config *Config) SaveFilterID(_ id.UserID, filterID string) {
config.AuthCache.FilterID = filterID
config.SaveAuthCache()
}
func (config *Config) LoadFilterID(_ id.UserID) string {
return config.AuthCache.FilterID
}
func (config *Config) SaveNextBatch(_ id.UserID, nextBatch string) {
config.AuthCache.NextBatch = nextBatch
config.SaveAuthCache()
}
func (config *Config) LoadNextBatch(_ id.UserID) string {
return config.AuthCache.NextBatch
}
func (config *Config) SaveRoom(_ *mautrix.Room) {
2019-06-15 00:11:51 +02:00
panic("SaveRoom is not supported")
}
func (config *Config) LoadRoom(_ id.RoomID) *mautrix.Room {
2019-06-15 00:11:51 +02:00
panic("LoadRoom is not supported")
}
func (config *Config) GetRoom(roomID id.RoomID) *rooms.Room {
2019-06-15 00:11:51 +02:00
return config.Rooms.GetOrCreate(roomID)
}
func (config *Config) DisableUnloading() {
config.Rooms.DisableUnloading()
}
func (config *Config) EnableUnloading() {
config.Rooms.EnableUnloading()
}