Switch startup, config, commands and db migrations to mautrix-go systems
This commit is contained in:
114
config/config.go
114
config/config.go
@@ -1,101 +1,35 @@
|
||||
// mautrix-discord - A Matrix-Discord puppeting bridge.
|
||||
// Copyright (C) 2022 Tulir Asokan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Homeserver homeserver `yaml:"homeserver"`
|
||||
Appservice appservice `yaml:"appservice"`
|
||||
Bridge bridge `yaml:"bridge"`
|
||||
Logging logging `yaml:"logging"`
|
||||
*bridgeconfig.BaseConfig `yaml:",inline"`
|
||||
|
||||
filename string `yaml:"-"`
|
||||
Bridge BridgeConfig `yaml:"bridge"`
|
||||
}
|
||||
|
||||
var configUpdated bool
|
||||
func (config *Config) CanAutoDoublePuppet(userID id.UserID) bool {
|
||||
_, homeserver, _ := userID.Parse()
|
||||
_, hasSecret := config.Bridge.LoginSharedSecretMap[homeserver]
|
||||
|
||||
func (cfg *Config) validate() error {
|
||||
if err := cfg.Homeserver.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Appservice.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Bridge.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Logging.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if configUpdated {
|
||||
return cfg.Save(cfg.filename)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
type rawConfig Config
|
||||
|
||||
raw := rawConfig{
|
||||
filename: cfg.filename,
|
||||
}
|
||||
|
||||
if err := unmarshal(&raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*cfg = Config(raw)
|
||||
|
||||
return cfg.validate()
|
||||
}
|
||||
|
||||
func FromBytes(filename string, data []byte) (*Config, error) {
|
||||
cfg := Config{
|
||||
filename: filename,
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := cfg.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func FromString(str string) (*Config, error) {
|
||||
return FromBytes("", []byte(str))
|
||||
}
|
||||
|
||||
func FromFile(filename string) (*Config, error) {
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return FromBytes(filename, data)
|
||||
}
|
||||
|
||||
func (cfg *Config) Save(filename string) error {
|
||||
if filename == "" {
|
||||
return fmt.Errorf("no filename specified yep")
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(filename, data, 0600)
|
||||
return hasSecret
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user