From 547977237c7df715da220fb99ad3c282fe4f826f Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 12 Oct 2023 20:51:08 -0400 Subject: [PATCH] cleanup and switch to just sync token --- client.go | 37 +++++++++++++++++++++++++++++++++---- config.go | 49 ++++++++++++++++++++++++++++++------------------- go.mod | 1 + go.sum | 2 ++ 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/client.go b/client.go index 48bc043..b7d9444 100644 --- a/client.go +++ b/client.go @@ -1,18 +1,21 @@ package matrixbotlib import ( + "io" + "os" "strings" + "gopkg.in/yaml.v2" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) -//NewMatrixClient returns a new logged in Mautrix Client struct +// NewMatrixClient returns a new logged in Mautrix Client struct func NewMatrixClient(config *MatrixClientConfig, store mautrix.Storer) (*mautrix.Client, error) { var client *mautrix.Client var err error - //make sure username is lower case otherwise token login breaks + // make sure username is lower case otherwise token login breaks uname := strings.ToLower(config.Username) if config.Token == "" { client, err = mautrix.NewClient(config.Homeserver, "", "") @@ -37,11 +40,37 @@ func NewMatrixClient(config *MatrixClientConfig, store mautrix.Storer) (*mautrix return client, err } config.Token = loginRes.AccessToken - WriteMatrixClientConfig(config) + err = SyncToken(config) + if err != nil { + return nil, err + } } return client, err } +func SyncToken(config *MatrixClientConfig) error { + file, err := os.OpenFile(config.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600) + if err != nil { + return err + } + defer file.Close() + + t := make(map[string]interface{}) + data, err := io.ReadAll(file) + if err != nil { + return err + } + yaml.Unmarshal([]byte(data), &t) + t["token"] = config.Token + enc := yaml.NewEncoder(file) + + err = enc.Encode(t) + if err != nil { + return err + } + return nil +} + // SetupAccountDataStore sets the client to use a AccountData store and filter appropriately func SetupAccountDataStore(client *mautrix.Client, token string) error { dataFilter := &mautrix.Filter{ @@ -56,7 +85,7 @@ func SetupAccountDataStore(client *mautrix.Client, token string) error { store := mautrix.NewAccountDataStore(token, client) fID, err := client.CreateFilter(dataFilter) if err != nil { - //don't want to continue if we can't keep state + // don't want to continue if we can't keep state return err } uid := client.UserID diff --git a/config.go b/config.go index 4b712c5..fbd5e44 100644 --- a/config.go +++ b/config.go @@ -1,43 +1,54 @@ package matrixbotlib import ( - "io/ioutil" + "errors" "os" + "github.com/kelseyhightower/envconfig" "gopkg.in/yaml.v2" ) -//MatrixClientConfig represents the config requires to log into the matrix server +// MatrixClientConfig represents the config requires to log into the matrix server type MatrixClientConfig struct { - Homeserver string `yaml:"homeserver"` - Domain string `yaml:"domain"` + Homeserver string `required:"true" yaml:"homeserver"` + Domain string `required:"true" yaml:"domain"` Dimension string `yaml:"dimension"` - Username string `yaml:"username"` - Password string `yaml:"password"` + Username string `required:"true" yaml:"username"` + Password string `required:"true" yaml:"password"` Statefile string `yaml:"statefile,omitempty"` Token string `yaml:"token"` filename string } -//LoadMatrixClientConfig reads info from a file +// LoadMatrixClientConfig reads info from a file func LoadMatrixClientConfig(filename string) (*MatrixClientConfig, error) { - yamlFile, err := ioutil.ReadFile(filename) - cnf := &MatrixClientConfig{} - if err == nil { - err = yaml.Unmarshal(yamlFile, cnf) + cnf := MatrixClientConfig{} + if filename != "" { + yamlFile, err := os.ReadFile(filename) + if err == nil { + err = yaml.Unmarshal(yamlFile, &cnf) + } else { + return &cnf, err + } + if err != nil { + return &cnf, err + } + cnf.filename = filename } else { - return cnf, err + err := envconfig.Process("matrix", &cnf) + if err != nil { + return nil, err + } } - if err != nil { - return cnf, err - } - cnf.filename = filename - return cnf, nil + return &cnf, nil } -//WriteMatrixClientConfig saves current running config to a file +// WriteMatrixClientConfig saves current running config to a file func WriteMatrixClientConfig(cnf *MatrixClientConfig) error { - file, err := os.OpenFile(cnf.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + if cnf.filename == "" { + return errors.New("not loaded from config file") + } + file, err := os.OpenFile(cnf.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600) if err != nil { return err } diff --git a/go.mod b/go.mod index 9f2be92..e424944 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/kelseyhightower/envconfig v1.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/rs/zerolog v1.29.1 // indirect diff --git a/go.sum b/go.sum index 51d949c..54a8a0a 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=