config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package config
  17. import (
  18. "io/ioutil"
  19. "gopkg.in/yaml.v2"
  20. "maunium.net/go/mautrix/appservice"
  21. )
  22. type Config struct {
  23. Homeserver struct {
  24. Address string `yaml:"address"`
  25. Domain string `yaml:"domain"`
  26. } `yaml:"homeserver"`
  27. AppService struct {
  28. Address string `yaml:"address"`
  29. Hostname string `yaml:"hostname"`
  30. Port uint16 `yaml:"port"`
  31. Database struct {
  32. Type string `yaml:"type"`
  33. URI string `yaml:"uri"`
  34. MaxOpenConns int `yaml:"max_open_conns"`
  35. MaxIdleConns int `yaml:"max_idle_conns"`
  36. } `yaml:"database"`
  37. StateStore string `yaml:"state_store_path,omitempty"`
  38. Provisioning struct {
  39. Prefix string `yaml:"prefix"`
  40. SharedSecret string `yaml:"shared_secret"`
  41. } `yaml:"provisioning"`
  42. ID string `yaml:"id"`
  43. Bot struct {
  44. Username string `yaml:"username"`
  45. Displayname string `yaml:"displayname"`
  46. Avatar string `yaml:"avatar"`
  47. } `yaml:"bot"`
  48. ASToken string `yaml:"as_token"`
  49. HSToken string `yaml:"hs_token"`
  50. } `yaml:"appservice"`
  51. Metrics struct {
  52. Enabled bool `yaml:"enabled"`
  53. Listen string `yaml:"listen"`
  54. } `yaml:"metrics"`
  55. Bridge BridgeConfig `yaml:"bridge"`
  56. Logging appservice.LogConfig `yaml:"logging"`
  57. }
  58. func (config *Config) setDefaults() {
  59. config.AppService.Database.MaxOpenConns = 20
  60. config.AppService.Database.MaxIdleConns = 2
  61. config.Bridge.setDefaults()
  62. }
  63. func Load(path string) (*Config, error) {
  64. data, err := ioutil.ReadFile(path)
  65. if err != nil {
  66. return nil, err
  67. }
  68. var config = &Config{}
  69. config.setDefaults()
  70. err = yaml.Unmarshal(data, config)
  71. return config, err
  72. }
  73. func (config *Config) Save(path string) error {
  74. data, err := yaml.Marshal(config)
  75. if err != nil {
  76. return err
  77. }
  78. return ioutil.WriteFile(path, data, 0600)
  79. }
  80. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  81. as := appservice.Create()
  82. as.HomeserverDomain = config.Homeserver.Domain
  83. as.HomeserverURL = config.Homeserver.Address
  84. as.Host.Hostname = config.AppService.Hostname
  85. as.Host.Port = config.AppService.Port
  86. var err error
  87. as.Registration, err = config.GetRegistration()
  88. return as, err
  89. }