aboutsummaryrefslogtreecommitdiff
path: root/config/config.go
blob: 41cff414178da548e768848ac95d8c0245b82069 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// 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/>.

package config

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"gopkg.in/yaml.v2"
	"maunium.net/go/gomuks/debug"
	"maunium.net/go/gomuks/matrix/rooms"
	"maunium.net/go/gomuks/matrix/pushrules"
	"encoding/json"
	"strings"
	"maunium.net/go/gomatrix"
)

// Config contains the main config of gomuks.
type Config struct {
	UserID      string `yaml:"mxid"`
	AccessToken string `yaml:"access_token"`
	HS          string `yaml:"homeserver"`

	Dir        string `yaml:"-"`
	CacheDir   string `yaml:"cache_dir"`
	HistoryDir string `yaml:"history_dir"`
	MediaDir   string `yaml:"media_dir"`
	StateDir   string `yaml:"state_dir"`

	AuthCache struct {
		NextBatch       string `yaml:"next_batch"`
		FilterID        string `yaml:"filter_id"`
		InitialSyncDone bool   `yaml:"initial_sync_done"`
	} `yaml:"-"`

	Rooms     map[string]*rooms.Room `yaml:"-"`
	PushRules *pushrules.PushRuleset `yaml:"-"`

	nosave bool
}

// NewConfig creates a config that loads data from the given directory.
func NewConfig(configDir, cacheDir string) *Config {
	return &Config{
		Dir:        configDir,
		CacheDir:   cacheDir,
		HistoryDir: filepath.Join(cacheDir, "history"),
		StateDir:   filepath.Join(cacheDir, "state"),
		MediaDir:   filepath.Join(cacheDir, "media"),

		Rooms: make(map[string]*rooms.Room),
	}
}

// Clear clears the session cache and removes all history.
func (config *Config) Clear() {
	os.RemoveAll(config.HistoryDir)
	os.RemoveAll(config.StateDir)
	os.RemoveAll(config.MediaDir)
	os.RemoveAll(config.CacheDir)
	config.nosave = true
}

func (config *Config) CreateCacheDirs() {
	os.MkdirAll(config.CacheDir, 0700)
	os.MkdirAll(config.HistoryDir, 0700)
	os.MkdirAll(config.StateDir, 0700)
	os.MkdirAll(config.MediaDir, 0700)
}

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()
	config.LoadRooms()
}

// Load loads the config from config.yaml in the directory given to the config struct.
func (config *Config) Load() {
	os.MkdirAll(config.Dir, 0700)

	configPath := filepath.Join(config.Dir, "config.yaml")
	data, err := ioutil.ReadFile(configPath)
	if err != nil {
		if os.IsNotExist(err) {
			config.CreateCacheDirs()
			return
		}
		debug.Print("Failed to read config from", configPath)
		panic(err)
	}

	err = yaml.Unmarshal(data, &config)
	if err != nil {
		debug.Print("Failed to parse config at", configPath)
		panic(err)
	}
	config.CreateCacheDirs()
}

func (config *Config) SaveAll() {
	config.Save()
	config.SaveAuthCache()
	config.SavePushRules()
	config.SaveRooms()
}

// Save saves this config to config.yaml in the directory given to the config struct.
func (config *Config) Save() {
	if config.nosave {
		return
	}

	os.MkdirAll(config.Dir, 0700)
	data, err := yaml.Marshal(&config)
	if err != nil {
		debug.Print("Failed to marshal config")
		panic(err)
	}

	path := filepath.Join(config.Dir, "config.yaml")
	err = ioutil.WriteFile(path, data, 0600)
	if err != nil {
		debug.Print("Failed to write config to", path)
		panic(err)
	}
}

func (config *Config) LoadAuthCache() {
	os.MkdirAll(config.Dir, 0700)

	configPath := filepath.Join(config.CacheDir, "auth-cache.yaml")
	data, err := ioutil.ReadFile(configPath)
	if err != nil {
		if os.IsNotExist(err) {
			return
		}
		debug.Print("Failed to read auth cache from", configPath)
		panic(err)
	}

	err = yaml.Unmarshal(data, &config.AuthCache)
	if err != nil {
		debug.Print("Failed to parse auth cache at", configPath)
		panic(err)
	}
}

func (config *Config) SaveAuthCache() {
	if config.nosave {
		return
	}

	os.MkdirAll(config.CacheDir, 0700)
	data, err := yaml.Marshal(&config.AuthCache)
	if err != nil {
		debug.Print("Failed to marshal auth cache")
		panic(err)
	}

	path := filepath.Join(config.CacheDir, "auth-cache.yaml")
	err = ioutil.WriteFile(path, data, 0600)
	if err != nil {
		debug.Print("Failed to write auth cache to", path)
		panic(err)
	}
}

func (config *Config) LoadPushRules() {
	os.MkdirAll(config.CacheDir, 0700)

	pushRulesPath := filepath.Join(config.CacheDir, "pushrules.json")
	data, err := ioutil.ReadFile(pushRulesPath)
	if err != nil {
		if os.IsNotExist(err) {
			return
		}
		debug.Print("Failed to read push rules from", pushRulesPath)
		return
	}

	config.PushRules = &pushrules.PushRuleset{}
	err = json.Unmarshal(data, &config.PushRules)
	if err != nil {
		debug.Print("Failed to parse push rules at", pushRulesPath)
		return
	}
}

func (config *Config) SavePushRules() {
	if config.nosave || config.PushRules == nil {
		return
	}

	os.MkdirAll(config.CacheDir, 0700)
	data, err := json.Marshal(&config.PushRules)
	if err != nil {
		debug.Print("Failed to marshal push rules")
		return
	}

	path := filepath.Join(config.CacheDir, "pushrules.json")
	err = ioutil.WriteFile(path, data, 0600)
	if err != nil {
		debug.Print("Failed to write config to", path)
		return
	}
}

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
	}
	
	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)
		}
	}
}

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))
}

func (config *Config) SaveRoom(room *gomatrix.Room) {
	gmxRoom := config.GetRoom(room.ID)
	gmxRoom.Room = room
	gmxRoom.Save(config.getRoomCachePath(gmxRoom))
}

func (config *Config) LoadRoom(roomID string) *gomatrix.Room {
	return config.GetRoom(roomID).Room
}