From 702a75a8c0355737e3e62735b59fe30bee7e42f4 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 22 Mar 2018 19:51:20 +0200 Subject: Save history to disk. Fixes #1 --- config/config.go | 24 +++++++++++++++++------- config/session.go | 12 ++++++------ 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index 7d99274..5c1d87c 100644 --- a/config/config.go +++ b/config/config.go @@ -30,19 +30,29 @@ type Config struct { UserID string `yaml:"mxid"` HS string `yaml:"homeserver"` - dir string `yaml:"-"` - Session *Session `yaml:"-"` + Dir string `yaml:"-"` + HistoryDir string `yaml:"history_dir"` + Session *Session `yaml:"-"` } func NewConfig(dir string) *Config { return &Config{ - dir: dir, + Dir: dir, + HistoryDir: filepath.Join(dir, "history"), } } +func (config *Config) Clear() { + if config.Session != nil { + config.Session.Clear() + } + os.RemoveAll(config.HistoryDir) +} + func (config *Config) Load() { - os.MkdirAll(config.dir, 0700) - configPath := filepath.Join(config.dir, "config.yaml") + os.MkdirAll(config.Dir, 0700) + os.MkdirAll(config.HistoryDir, 0700) + configPath := filepath.Join(config.Dir, "config.yaml") data, err := ioutil.ReadFile(configPath) if err != nil { if os.IsNotExist(err) { @@ -61,14 +71,14 @@ func (config *Config) Load() { } func (config *Config) Save() { - os.MkdirAll(config.dir, 0700) + 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") + path := filepath.Join(config.Dir, "config.yaml") err = ioutil.WriteFile(path, data, 0600) if err != nil { debug.Print("Failed to write config to", path) diff --git a/config/session.go b/config/session.go index 3fdc169..2d4b885 100644 --- a/config/session.go +++ b/config/session.go @@ -23,7 +23,7 @@ import ( "maunium.net/go/gomatrix" "maunium.net/go/gomuks/matrix/pushrules" - rooms "maunium.net/go/gomuks/matrix/room" + "maunium.net/go/gomuks/matrix/room" "maunium.net/go/gomuks/ui/debug" ) @@ -45,7 +45,7 @@ func (config *Config) LoadSession(mxid string) error { func (config *Config) NewSession(mxid string) *Session { return &Session{ UserID: mxid, - path: filepath.Join(config.dir, mxid+".session"), + path: filepath.Join(config.Dir, mxid+".session"), Rooms: make(map[string]*rooms.Room), } } @@ -61,13 +61,13 @@ func (s *Session) Clear() { func (s *Session) Load() error { data, err := ioutil.ReadFile(s.path) if err != nil { - debug.Print("Failed to read session from", s.path, err) + debug.Printf("Failed to read session from %s: %v", s.path, err) return err } err = json.Unmarshal(data, s) if err != nil { - debug.Print("Failed to parse session at", s.path, err) + debug.Printf("Failed to parse session at %s: %v", s.path, err) return err } return nil @@ -76,13 +76,13 @@ func (s *Session) Load() error { func (s *Session) Save() error { data, err := json.Marshal(s) if err != nil { - debug.Print("Failed to marshal session of", s.UserID, err) + debug.Printf("Failed to marshal session of %s: %v", s.UserID, err) return err } err = ioutil.WriteFile(s.path, data, 0600) if err != nil { - debug.Print("Failed to write session to", s.path, err) + debug.Printf("Failed to write session of %s to %s: %v", s.UserID, s.path, err) return err } return nil -- cgit v1.2.3