cleanup and switch to just sync token

This commit is contained in:
stryan 2023-10-12 20:51:08 -04:00
parent 54d876a989
commit 547977237c
4 changed files with 66 additions and 23 deletions

View File

@ -1,8 +1,11 @@
package matrixbotlib
import (
"io"
"os"
"strings"
"gopkg.in/yaml.v2"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
@ -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{

View File

@ -1,19 +1,20 @@
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
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
@ -21,23 +22,33 @@ type MatrixClientConfig struct {
// LoadMatrixClientConfig reads info from a file
func LoadMatrixClientConfig(filename string) (*MatrixClientConfig, error) {
yamlFile, err := ioutil.ReadFile(filename)
cnf := &MatrixClientConfig{}
cnf := MatrixClientConfig{}
if filename != "" {
yamlFile, err := os.ReadFile(filename)
if err == nil {
err = yaml.Unmarshal(yamlFile, cnf)
err = yaml.Unmarshal(yamlFile, &cnf)
} else {
return cnf, err
return &cnf, err
}
if err != nil {
return cnf, err
return &cnf, err
}
cnf.filename = filename
return cnf, nil
} else {
err := envconfig.Process("matrix", &cnf)
if err != nil {
return nil, err
}
}
return &cnf, nil
}
// 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
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=