| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package config |
| 18 |
|
| 19 |
import ( |
| 20 |
_ "embed" |
| 21 |
"encoding/json" |
| 22 |
"fmt" |
| 23 |
"io/ioutil" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"strconv" |
| 27 |
"strings" |
| 28 |
|
| 29 |
"gopkg.in/yaml.v3" |
| 30 |
|
| 31 |
"maunium.net/go/mautrix" |
| 32 |
"maunium.net/go/mautrix/id" |
| 33 |
"maunium.net/go/mautrix/pushrules" |
| 34 |
|
| •35 |
"code.rocketnine.space/tslocum/cbind" |
| •36 |
"github.com/gdamore/tcell/v2" |
| 37 |
|
| 38 |
"maunium.net/go/gomuks/debug" |
| 39 |
"maunium.net/go/gomuks/matrix/rooms" |
| 40 |
) |
| 41 |
|
| 42 |
type AuthCache struct { |
| 43 |
NextBatch string `yaml:"next_batch"` |
| 44 |
FilterID string `yaml:"filter_id"` |
| 45 |
FilterVersion int `yaml:"filter_version"` |
| 46 |
InitialSyncDone bool `yaml:"initial_sync_done"` |
| 47 |
} |
| 48 |
|
| 49 |
type UserPreferences struct { |
| 50 |
HideUserList bool `yaml:"hide_user_list"` |
| 51 |
HideRoomList bool `yaml:"hide_room_list"` |
| 52 |
HideTimestamp bool `yaml:"hide_timestamp"` |
| 53 |
BareMessageView bool `yaml:"bare_message_view"` |
| 54 |
DisableImages bool `yaml:"disable_images"` |
| 55 |
DisableTypingNotifs bool `yaml:"disable_typing_notifs"` |
| 56 |
DisableEmojis bool `yaml:"disable_emojis"` |
| 57 |
DisableMarkdown bool `yaml:"disable_markdown"` |
| 58 |
DisableHTML bool `yaml:"disable_html"` |
| 59 |
DisableDownloads bool `yaml:"disable_downloads"` |
| 60 |
DisableNotifications bool `yaml:"disable_notifications"` |
| 61 |
DisableShowURLs bool `yaml:"disable_show_urls"` |
| 62 |
AltEnterToSend bool `yaml:"alt_enter_to_send"` |
| 63 |
|
| 64 |
InlineURLMode string `yaml:"inline_url_mode"` |
| 65 |
} |
| 66 |
|
| 67 |
var InlineURLsProbablySupported bool |
| 68 |
|
| 69 |
func init() { |
| 70 |
vteVersion, _ := strconv.Atoi(os.Getenv("VTE_VERSION")) |
| 71 |
term := os.Getenv("TERM") |
| 72 |
// Enable inline URLs by default on VTE 0.50.0+ |
| 73 |
InlineURLsProbablySupported = vteVersion > 5000 || |
| 74 |
os.Getenv("TERM_PROGRAM") == "iTerm.app" || |
| 75 |
term == "foot" || |
| 76 |
term == "xterm-kitty" |
| 77 |
} |
| 78 |
|
| 79 |
func (up *UserPreferences) EnableInlineURLs() bool { |
| 80 |
return up.InlineURLMode == "enable" || (InlineURLsProbablySupported && up.InlineURLMode != "disable") |
| 81 |
} |
| 82 |
|
| 83 |
type Keybind struct { |
| 84 |
Mod tcell.ModMask |
| 85 |
Key tcell.Key |
| 86 |
Ch rune |
| 87 |
} |
| 88 |
|
| 89 |
type ParsedKeybindings struct { |
| 90 |
Main map[Keybind]string |
| 91 |
Room map[Keybind]string |
| 92 |
Modal map[Keybind]string |
| 93 |
Visual map[Keybind]string |
| 94 |
} |
| 95 |
|
| 96 |
type RawKeybindings struct { |
| 97 |
Main map[string]string `yaml:"main,omitempty"` |
| 98 |
Room map[string]string `yaml:"room,omitempty"` |
| 99 |
Modal map[string]string `yaml:"modal,omitempty"` |
| 100 |
Visual map[string]string `yaml:"visual,omitempty"` |
| 101 |
} |
| 102 |
|
| 103 |
// Config contains the main config of gomuks. |
| 104 |
type Config struct { |
| 105 |
UserID id.UserID `yaml:"mxid"` |
| 106 |
DeviceID id.DeviceID `yaml:"device_id"` |
| 107 |
AccessToken string `yaml:"access_token"` |
| 108 |
HS string `yaml:"homeserver"` |
| 109 |
|
| 110 |
RoomCacheSize int `yaml:"room_cache_size"` |
| 111 |
RoomCacheAge int64 `yaml:"room_cache_age"` |
| 112 |
|
| 113 |
NotifySound bool `yaml:"notify_sound"` |
| 114 |
SendToVerifiedOnly bool `yaml:"send_to_verified_only"` |
| 115 |
|
| 116 |
Backspace1RemovesWord bool `yaml:"backspace1_removes_word"` |
| 117 |
Backspace2RemovesWord bool `yaml:"backspace2_removes_word"` |
| 118 |
|
| 119 |
AlwaysClearScreen bool `yaml:"always_clear_screen"` |
| 120 |
|
| 121 |
Dir string `yaml:"-"` |
| 122 |
DataDir string `yaml:"data_dir"` |
| 123 |
CacheDir string `yaml:"cache_dir"` |
| 124 |
HistoryPath string `yaml:"history_path"` |
| 125 |
RoomListPath string `yaml:"room_list_path"` |
| 126 |
MediaDir string `yaml:"media_dir"` |
| 127 |
DownloadDir string `yaml:"download_dir"` |
| 128 |
StateDir string `yaml:"state_dir"` |
| 129 |
|
| 130 |
Preferences UserPreferences `yaml:"-"` |
| 131 |
AuthCache AuthCache `yaml:"-"` |
| 132 |
Rooms *rooms.RoomCache `yaml:"-"` |
| 133 |
PushRules *pushrules.PushRuleset `yaml:"-"` |
| 134 |
Keybindings ParsedKeybindings `yaml:"-"` |
| 135 |
|
| 136 |
nosave bool |
| 137 |
} |
| 138 |
|
| 139 |
// NewConfig creates a config that loads data from the given directory. |
| 140 |
func NewConfig(configDir, dataDir, cacheDir, downloadDir string) *Config { |
| 141 |
return &Config{ |
| 142 |
Dir: configDir, |
| 143 |
DataDir: dataDir, |
| 144 |
CacheDir: cacheDir, |
| 145 |
DownloadDir: downloadDir, |
| 146 |
HistoryPath: filepath.Join(cacheDir, "history.db"), |
| 147 |
RoomListPath: filepath.Join(cacheDir, "rooms.gob.gz"), |
| 148 |
StateDir: filepath.Join(cacheDir, "state"), |
| 149 |
MediaDir: filepath.Join(cacheDir, "media"), |
| 150 |
|
| 151 |
RoomCacheSize: 32, |
| 152 |
RoomCacheAge: 1 * 60, |
| 153 |
|
| 154 |
NotifySound: true, |
| 155 |
SendToVerifiedOnly: false, |
| 156 |
Backspace1RemovesWord: true, |
| 157 |
AlwaysClearScreen: true, |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Clear clears the session cache and removes all history. |
| 162 |
func (config *Config) Clear() { |
| 163 |
_ = os.Remove(config.HistoryPath) |
| 164 |
_ = os.Remove(config.RoomListPath) |
| 165 |
_ = os.RemoveAll(config.StateDir) |
| 166 |
_ = os.RemoveAll(config.MediaDir) |
| 167 |
_ = os.RemoveAll(config.CacheDir) |
| 168 |
config.nosave = true |
| 169 |
} |
| 170 |
|
| 171 |
// ClearData clears non-temporary session data. |
| 172 |
func (config *Config) ClearData() { |
| 173 |
_ = os.RemoveAll(config.DataDir) |
| 174 |
} |
| 175 |
|
| 176 |
func (config *Config) CreateCacheDirs() { |
| 177 |
_ = os.MkdirAll(config.CacheDir, 0700) |
| 178 |
_ = os.MkdirAll(config.DataDir, 0700) |
| 179 |
_ = os.MkdirAll(config.StateDir, 0700) |
| 180 |
_ = os.MkdirAll(config.MediaDir, 0700) |
| 181 |
} |
| 182 |
|
| 183 |
func (config *Config) DeleteSession() { |
| 184 |
config.AuthCache.NextBatch = "" |
| 185 |
config.AuthCache.InitialSyncDone = false |
| 186 |
config.AccessToken = "" |
| 187 |
config.DeviceID = "" |
| 188 |
config.Rooms = rooms.NewRoomCache(config.RoomListPath, config.StateDir, config.RoomCacheSize, config.RoomCacheAge, config.GetUserID) |
| 189 |
config.PushRules = nil |
| 190 |
|
| 191 |
config.ClearData() |
| 192 |
config.Clear() |
| 193 |
config.nosave = false |
| 194 |
config.CreateCacheDirs() |
| 195 |
} |
| 196 |
|
| 197 |
func (config *Config) LoadAll() { |
| 198 |
config.Load() |
| 199 |
config.Rooms = rooms.NewRoomCache(config.RoomListPath, config.StateDir, config.RoomCacheSize, config.RoomCacheAge, config.GetUserID) |
| 200 |
config.LoadAuthCache() |
| 201 |
config.LoadPushRules() |
| 202 |
config.LoadPreferences() |
| 203 |
config.LoadKeybindings() |
| 204 |
err := config.Rooms.LoadList() |
| 205 |
if err != nil { |
| 206 |
panic(err) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// Load loads the config from config.yaml in the directory given to the config struct. |
| 211 |
func (config *Config) Load() { |
| 212 |
err := config.load("config", config.Dir, "config.yaml", config) |
| 213 |
if err != nil { |
| 214 |
panic(fmt.Errorf("failed to load config.yaml: %w", err)) |
| 215 |
} |
| 216 |
config.CreateCacheDirs() |
| 217 |
} |
| 218 |
|
| 219 |
func (config *Config) SaveAll() { |
| 220 |
config.Save() |
| 221 |
config.SaveAuthCache() |
| 222 |
config.SavePushRules() |
| 223 |
config.SavePreferences() |
| 224 |
err := config.Rooms.SaveList() |
| 225 |
if err != nil { |
| 226 |
panic(err) |
| 227 |
} |
| 228 |
config.Rooms.SaveLoadedRooms() |
| 229 |
} |
| 230 |
|
| 231 |
// Save saves this config to config.yaml in the directory given to the config struct. |
| 232 |
func (config *Config) Save() { |
| 233 |
config.save("config", config.Dir, "config.yaml", config) |
| 234 |
} |
| 235 |
|
| 236 |
func (config *Config) LoadPreferences() { |
| 237 |
_ = config.load("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences) |
| 238 |
} |
| 239 |
|
| 240 |
func (config *Config) SavePreferences() { |
| 241 |
config.save("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences) |
| 242 |
} |
| 243 |
|
| 244 |
//go:embed keybindings.yaml |
| 245 |
var DefaultKeybindings string |
| 246 |
|
| 247 |
func parseKeybindings(input map[string]string) (output map[Keybind]string) { |
| 248 |
output = make(map[Keybind]string, len(input)) |
| 249 |
for shortcut, action := range input { |
| 250 |
mod, key, ch, err := cbind.Decode(shortcut) |
| 251 |
if err != nil { |
| 252 |
panic(fmt.Errorf("failed to parse keybinding %s -> %s: %w", shortcut, action, err)) |
| 253 |
} |
| 254 |
// TODO find out if other keys are parsed incorrectly like this |
| 255 |
if key == tcell.KeyEscape { |
| 256 |
ch = 0 |
| 257 |
} |
| 258 |
parsedShortcut := Keybind{ |
| 259 |
Mod: mod, |
| 260 |
Key: key, |
| 261 |
Ch: ch, |
| 262 |
} |
| 263 |
output[parsedShortcut] = action |
| 264 |
} |
| 265 |
return |
| 266 |
} |
| 267 |
|
| 268 |
func (config *Config) LoadKeybindings() { |
| 269 |
var inputConfig RawKeybindings |
| 270 |
|
| 271 |
err := yaml.Unmarshal([]byte(DefaultKeybindings), &inputConfig) |
| 272 |
if err != nil { |
| 273 |
panic(fmt.Errorf("failed to unmarshal default keybindings: %w", err)) |
| 274 |
} |
| 275 |
_ = config.load("keybindings", config.Dir, "keybindings.yaml", &inputConfig) |
| 276 |
|
| 277 |
config.Keybindings.Main = parseKeybindings(inputConfig.Main) |
| 278 |
config.Keybindings.Room = parseKeybindings(inputConfig.Room) |
| 279 |
config.Keybindings.Modal = parseKeybindings(inputConfig.Modal) |
| 280 |
config.Keybindings.Visual = parseKeybindings(inputConfig.Visual) |
| 281 |
} |
| 282 |
|
| 283 |
func (config *Config) SaveKeybindings() { |
| 284 |
config.save("keybindings", config.Dir, "keybindings.yaml", &config.Keybindings) |
| 285 |
} |
| 286 |
|
| 287 |
func (config *Config) LoadAuthCache() { |
| 288 |
err := config.load("auth cache", config.CacheDir, "auth-cache.yaml", &config.AuthCache) |
| 289 |
if err != nil { |
| 290 |
panic(fmt.Errorf("failed to load auth-cache.yaml: %w", err)) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
func (config *Config) SaveAuthCache() { |
| 295 |
config.save("auth cache", config.CacheDir, "auth-cache.yaml", &config.AuthCache) |
| 296 |
} |
| 297 |
|
| 298 |
func (config *Config) LoadPushRules() { |
| 299 |
_ = config.load("push rules", config.CacheDir, "pushrules.json", &config.PushRules) |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
func (config *Config) SavePushRules() { |
| 304 |
if config.PushRules == nil { |
| 305 |
return |
| 306 |
} |
| 307 |
config.save("push rules", config.CacheDir, "pushrules.json", &config.PushRules) |
| 308 |
} |
| 309 |
|
| 310 |
func (config *Config) load(name, dir, file string, target interface{}) error { |
| 311 |
err := os.MkdirAll(dir, 0700) |
| 312 |
if err != nil { |
| 313 |
debug.Print("Failed to create", dir) |
| 314 |
return err |
| 315 |
} |
| 316 |
|
| 317 |
path := filepath.Join(dir, file) |
| 318 |
data, err := ioutil.ReadFile(path) |
| 319 |
if err != nil { |
| 320 |
if os.IsNotExist(err) { |
| 321 |
return nil |
| 322 |
} |
| 323 |
debug.Print("Failed to read", name, "from", path) |
| 324 |
return err |
| 325 |
} |
| 326 |
|
| 327 |
if strings.HasSuffix(file, ".yaml") { |
| 328 |
err = yaml.Unmarshal(data, target) |
| 329 |
} else { |
| 330 |
err = json.Unmarshal(data, target) |
| 331 |
} |
| 332 |
if err != nil { |
| 333 |
debug.Print("Failed to parse", name, "at", path) |
| 334 |
return err |
| 335 |
} |
| 336 |
return nil |
| 337 |
} |
| 338 |
|
| 339 |
func (config *Config) save(name, dir, file string, source interface{}) { |
| 340 |
if config.nosave { |
| 341 |
return |
| 342 |
} |
| 343 |
|
| 344 |
err := os.MkdirAll(dir, 0700) |
| 345 |
if err != nil { |
| 346 |
debug.Print("Failed to create", dir) |
| 347 |
panic(err) |
| 348 |
} |
| 349 |
var data []byte |
| 350 |
if strings.HasSuffix(file, ".yaml") { |
| 351 |
data, err = yaml.Marshal(source) |
| 352 |
} else { |
| 353 |
data, err = json.Marshal(source) |
| 354 |
} |
| 355 |
if err != nil { |
| 356 |
debug.Print("Failed to marshal", name) |
| 357 |
panic(err) |
| 358 |
} |
| 359 |
|
| 360 |
path := filepath.Join(dir, file) |
| 361 |
err = ioutil.WriteFile(path, data, 0600) |
| 362 |
if err != nil { |
| 363 |
debug.Print("Failed to write", name, "to", path) |
| 364 |
panic(err) |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
func (config *Config) GetUserID() id.UserID { |
| 369 |
return config.UserID |
| 370 |
} |
| 371 |
|
| 372 |
const FilterVersion = 1 |
| 373 |
|
| 374 |
func (config *Config) SaveFilterID(_ id.UserID, filterID string) { |
| 375 |
config.AuthCache.FilterID = filterID |
| 376 |
config.AuthCache.FilterVersion = FilterVersion |
| 377 |
config.SaveAuthCache() |
| 378 |
} |
| 379 |
|
| 380 |
func (config *Config) LoadFilterID(_ id.UserID) string { |
| 381 |
if config.AuthCache.FilterVersion != FilterVersion { |
| 382 |
return "" |
| 383 |
} |
| 384 |
return config.AuthCache.FilterID |
| 385 |
} |
| 386 |
|
| 387 |
func (config *Config) SaveNextBatch(_ id.UserID, nextBatch string) { |
| 388 |
config.AuthCache.NextBatch = nextBatch |
| 389 |
config.SaveAuthCache() |
| 390 |
} |
| 391 |
|
| 392 |
func (config *Config) LoadNextBatch(_ id.UserID) string { |
| 393 |
return config.AuthCache.NextBatch |
| 394 |
} |
| 395 |
|
| 396 |
func (config *Config) SaveRoom(_ *mautrix.Room) { |
| 397 |
panic("SaveRoom is not supported") |
| 398 |
} |
| 399 |
|
| 400 |
func (config *Config) LoadRoom(_ id.RoomID) *mautrix.Room { |
| 401 |
panic("LoadRoom is not supported") |
| 402 |
} |