config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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"`
  38. ID string `yaml:"id"`
  39. Bot struct {
  40. Username string `yaml:"username"`
  41. Displayname string `yaml:"displayname"`
  42. Avatar string `yaml:"avatar"`
  43. } `yaml:"bot"`
  44. ASToken string `yaml:"as_token"`
  45. HSToken string `yaml:"hs_token"`
  46. } `yaml:"appservice"`
  47. Bridge BridgeConfig `yaml:"bridge"`
  48. Logging appservice.LogConfig `yaml:"logging"`
  49. }
  50. func (config *Config) setDefaults() {
  51. config.AppService.Database.MaxOpenConns = 20
  52. config.AppService.Database.MaxIdleConns = 2
  53. config.Bridge.setDefaults()
  54. }
  55. func Load(path string) (*Config, error) {
  56. data, err := ioutil.ReadFile(path)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var config = &Config{}
  61. config.setDefaults()
  62. err = yaml.Unmarshal(data, config)
  63. return config, err
  64. }
  65. func (config *Config) Save(path string) error {
  66. data, err := yaml.Marshal(config)
  67. if err != nil {
  68. return err
  69. }
  70. return ioutil.WriteFile(path, data, 0600)
  71. }
  72. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  73. as := appservice.Create()
  74. as.HomeserverDomain = config.Homeserver.Domain
  75. as.HomeserverURL = config.Homeserver.Address
  76. as.Host.Hostname = config.AppService.Hostname
  77. as.Host.Port = config.AppService.Port
  78. var err error
  79. as.Registration, err = config.GetRegistration()
  80. return as, err
  81. }