2018-03-13 14:27:12 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program 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.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
package config
|
2018-03-13 14:27:12 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2018-06-01 23:44:21 +02:00
|
|
|
"encoding/json"
|
2018-07-02 09:00:42 +02:00
|
|
|
"strings"
|
|
|
|
|
2018-03-13 14:27:12 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
2018-11-13 23:00:35 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-05-17 15:29:15 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/pushrules"
|
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"`
|
|
|
|
InitialSyncDone bool `yaml:"initial_sync_done"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserPreferences struct {
|
2018-06-30 22:26:40 +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"`
|
2018-07-02 09:00:42 +02:00
|
|
|
DisableEmojis bool `yaml:"disable_emojis"`
|
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 {
|
2018-05-17 15:29:15 +02:00
|
|
|
UserID string `yaml:"mxid"`
|
|
|
|
AccessToken string `yaml:"access_token"`
|
|
|
|
HS string `yaml:"homeserver"`
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
Dir string `yaml:"-"`
|
|
|
|
CacheDir string `yaml:"cache_dir"`
|
|
|
|
HistoryDir string `yaml:"history_dir"`
|
|
|
|
MediaDir string `yaml:"media_dir"`
|
|
|
|
StateDir string `yaml:"state_dir"`
|
|
|
|
|
2018-05-24 22:26:57 +02:00
|
|
|
Preferences UserPreferences `yaml:"-"`
|
|
|
|
AuthCache AuthCache `yaml:"-"`
|
|
|
|
Rooms map[string]*rooms.Room `yaml:"-"`
|
|
|
|
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.
|
2018-04-30 11:06:22 +02:00
|
|
|
func NewConfig(configDir, cacheDir string) *Config {
|
2018-03-13 20:58:43 +01:00
|
|
|
return &Config{
|
2018-04-30 11:06:22 +02:00
|
|
|
Dir: configDir,
|
2018-05-17 15:29:15 +02:00
|
|
|
CacheDir: cacheDir,
|
2018-04-30 11:06:22 +02:00
|
|
|
HistoryDir: filepath.Join(cacheDir, "history"),
|
2018-05-17 15:29:15 +02:00
|
|
|
StateDir: filepath.Join(cacheDir, "state"),
|
2018-04-30 11:06:22 +02:00
|
|
|
MediaDir: filepath.Join(cacheDir, "media"),
|
2018-05-17 15:29:15 +02:00
|
|
|
|
|
|
|
Rooms: make(map[string]*rooms.Room),
|
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() {
|
|
|
|
os.RemoveAll(config.HistoryDir)
|
2018-05-17 15:29:15 +02:00
|
|
|
os.RemoveAll(config.StateDir)
|
2018-04-17 17:13:38 +02:00
|
|
|
os.RemoveAll(config.MediaDir)
|
2018-05-17 15:29:15 +02:00
|
|
|
os.RemoveAll(config.CacheDir)
|
|
|
|
config.nosave = true
|
2018-03-22 18:51:20 +01:00
|
|
|
}
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func (config *Config) CreateCacheDirs() {
|
|
|
|
os.MkdirAll(config.CacheDir, 0700)
|
2018-05-10 14:47:24 +02:00
|
|
|
os.MkdirAll(config.HistoryDir, 0700)
|
2018-05-17 15:29:15 +02:00
|
|
|
os.MkdirAll(config.StateDir, 0700)
|
2018-05-10 14:47:24 +02:00
|
|
|
os.MkdirAll(config.MediaDir, 0700)
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func (config *Config) DeleteSession() {
|
|
|
|
config.AuthCache.NextBatch = ""
|
|
|
|
config.AuthCache.InitialSyncDone = false
|
|
|
|
config.Rooms = make(map[string]*rooms.Room)
|
|
|
|
config.PushRules = nil
|
|
|
|
|
|
|
|
config.Clear()
|
|
|
|
config.nosave = false
|
|
|
|
config.CreateCacheDirs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) LoadAll() {
|
|
|
|
config.Load()
|
|
|
|
config.LoadAuthCache()
|
|
|
|
config.LoadPushRules()
|
2018-05-24 22:26:57 +02:00
|
|
|
config.LoadPreferences()
|
2018-05-17 15:29:15 +02:00
|
|
|
config.LoadRooms()
|
|
|
|
}
|
|
|
|
|
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()
|
2018-05-17 15:29:15 +02:00
|
|
|
config.SaveRooms()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) LoadRooms() {
|
|
|
|
os.MkdirAll(config.StateDir, 0700)
|
|
|
|
|
|
|
|
roomFiles, err := ioutil.ReadDir(config.StateDir)
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to list rooms state caches in", config.StateDir)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, roomFile := range roomFiles {
|
|
|
|
if roomFile.IsDir() || !strings.HasSuffix(roomFile.Name(), ".gmxstate") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path := filepath.Join(config.StateDir, roomFile.Name())
|
|
|
|
room := &rooms.Room{}
|
|
|
|
err = room.Load(path)
|
|
|
|
if err != nil {
|
|
|
|
debug.Printf("Failed to load room state cache from %s: %v", path, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
config.Rooms[room.ID] = room
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) SaveRooms() {
|
|
|
|
if config.nosave {
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 16:24:47 +02:00
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
os.MkdirAll(config.StateDir, 0700)
|
|
|
|
for _, room := range config.Rooms {
|
|
|
|
path := config.getRoomCachePath(room)
|
|
|
|
err := room.Save(path)
|
|
|
|
if err != nil {
|
|
|
|
debug.Printf("Failed to save room state cache to file %s: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 22:26:57 +02:00
|
|
|
func (config *Config) load(name, dir, file string, target interface{}) {
|
|
|
|
os.MkdirAll(dir, 0700)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
os.MkdirAll(dir, 0700)
|
|
|
|
var data []byte
|
|
|
|
var err error
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func (config *Config) GetUserID() string {
|
|
|
|
return config.UserID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) SaveFilterID(_, filterID string) {
|
|
|
|
config.AuthCache.FilterID = filterID
|
|
|
|
config.SaveAuthCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) LoadFilterID(_ string) string {
|
|
|
|
return config.AuthCache.FilterID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) SaveNextBatch(_, nextBatch string) {
|
|
|
|
config.AuthCache.NextBatch = nextBatch
|
|
|
|
config.SaveAuthCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) LoadNextBatch(_ string) string {
|
|
|
|
return config.AuthCache.NextBatch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) GetRoom(roomID string) *rooms.Room {
|
|
|
|
room, _ := config.Rooms[roomID]
|
|
|
|
if room == nil {
|
|
|
|
room = rooms.NewRoom(roomID, config.UserID)
|
|
|
|
config.Rooms[room.ID] = room
|
|
|
|
}
|
|
|
|
return room
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) getRoomCachePath(room *rooms.Room) string {
|
|
|
|
return filepath.Join(config.StateDir, room.ID+".gmxstate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) PutRoom(room *rooms.Room) {
|
|
|
|
config.Rooms[room.ID] = room
|
|
|
|
room.Save(config.getRoomCachePath(room))
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func (config *Config) SaveRoom(room *mautrix.Room) {
|
2018-05-17 15:29:15 +02:00
|
|
|
gmxRoom := config.GetRoom(room.ID)
|
|
|
|
gmxRoom.Room = room
|
|
|
|
gmxRoom.Save(config.getRoomCachePath(gmxRoom))
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func (config *Config) LoadRoom(roomID string) *mautrix.Room {
|
2018-05-17 15:29:15 +02:00
|
|
|
return config.GetRoom(roomID).Room
|
|
|
|
}
|