2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2019-01-12 14:54:04 +01:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-08-13 00:00:23 +02:00
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"io/ioutil"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2018-08-13 00:00:23 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2020-05-09 13:31:06 +02:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
2018-08-13 00:00:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Homeserver struct {
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
Domain string `yaml:"domain"`
|
|
|
|
} `yaml:"homeserver"`
|
|
|
|
|
|
|
|
AppService struct {
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
Hostname string `yaml:"hostname"`
|
|
|
|
Port uint16 `yaml:"port"`
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
Database struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
URI string `yaml:"uri"`
|
2019-05-17 22:33:27 +02:00
|
|
|
|
|
|
|
MaxOpenConns int `yaml:"max_open_conns"`
|
|
|
|
MaxIdleConns int `yaml:"max_idle_conns"`
|
2018-08-13 22:24:44 +02:00
|
|
|
} `yaml:"database"`
|
2018-08-13 00:00:23 +02:00
|
|
|
|
2019-08-25 16:25:19 +02:00
|
|
|
StateStore string `yaml:"state_store_path,omitempty"`
|
2018-08-16 23:11:28 +02:00
|
|
|
|
2020-02-09 19:32:14 +01:00
|
|
|
Provisioning struct {
|
|
|
|
Prefix string `yaml:"prefix"`
|
|
|
|
SharedSecret string `yaml:"shared_secret"`
|
|
|
|
} `yaml:"provisioning"`
|
|
|
|
|
2018-08-13 00:00:23 +02:00
|
|
|
ID string `yaml:"id"`
|
|
|
|
Bot struct {
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
Displayname string `yaml:"displayname"`
|
|
|
|
Avatar string `yaml:"avatar"`
|
|
|
|
} `yaml:"bot"`
|
|
|
|
|
|
|
|
ASToken string `yaml:"as_token"`
|
|
|
|
HSToken string `yaml:"hs_token"`
|
|
|
|
} `yaml:"appservice"`
|
|
|
|
|
2020-06-17 16:50:06 +02:00
|
|
|
Metrics struct {
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
} `yaml:"metrics"`
|
|
|
|
|
2020-06-25 16:59:44 +02:00
|
|
|
WhatsApp struct {
|
|
|
|
DeviceName string `yaml:"device_name"`
|
|
|
|
ShortName string `yaml:"short_name"`
|
|
|
|
} `yaml:"whatsapp"`
|
|
|
|
|
2018-08-13 00:00:23 +02:00
|
|
|
Bridge BridgeConfig `yaml:"bridge"`
|
|
|
|
|
|
|
|
Logging appservice.LogConfig `yaml:"logging"`
|
|
|
|
}
|
|
|
|
|
2019-05-17 22:33:27 +02:00
|
|
|
func (config *Config) setDefaults() {
|
|
|
|
config.AppService.Database.MaxOpenConns = 20
|
|
|
|
config.AppService.Database.MaxIdleConns = 2
|
2020-06-25 16:59:44 +02:00
|
|
|
config.WhatsApp.DeviceName = "Mautrix-WhatsApp bridge"
|
|
|
|
config.WhatsApp.ShortName = "mx-wa"
|
2019-05-17 22:53:57 +02:00
|
|
|
config.Bridge.setDefaults()
|
2019-05-17 22:33:27 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 00:00:23 +02:00
|
|
|
func Load(path string) (*Config, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = &Config{}
|
2019-05-17 22:33:27 +02:00
|
|
|
config.setDefaults()
|
2018-08-13 00:00:23 +02:00
|
|
|
err = yaml.Unmarshal(data, config)
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) Save(path string) error {
|
|
|
|
data, err := yaml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(path, data, 0600)
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
func (config *Config) MakeAppService() (*appservice.AppService, error) {
|
2018-08-13 00:00:23 +02:00
|
|
|
as := appservice.Create()
|
|
|
|
as.HomeserverDomain = config.Homeserver.Domain
|
|
|
|
as.HomeserverURL = config.Homeserver.Address
|
2018-08-16 18:20:07 +02:00
|
|
|
as.Host.Hostname = config.AppService.Hostname
|
|
|
|
as.Host.Port = config.AppService.Port
|
2018-08-13 00:00:23 +02:00
|
|
|
var err error
|
|
|
|
as.Registration, err = config.GetRegistration()
|
|
|
|
return as, err
|
|
|
|
}
|