config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Asmux bool `yaml:"asmux"`
  27. } `yaml:"homeserver"`
  28. AppService struct {
  29. Address string `yaml:"address"`
  30. Hostname string `yaml:"hostname"`
  31. Port uint16 `yaml:"port"`
  32. Database struct {
  33. Type string `yaml:"type"`
  34. URI string `yaml:"uri"`
  35. MaxOpenConns int `yaml:"max_open_conns"`
  36. MaxIdleConns int `yaml:"max_idle_conns"`
  37. } `yaml:"database"`
  38. StateStore string `yaml:"state_store_path,omitempty"`
  39. Provisioning struct {
  40. Prefix string `yaml:"prefix"`
  41. SharedSecret string `yaml:"shared_secret"`
  42. } `yaml:"provisioning"`
  43. ID string `yaml:"id"`
  44. Bot struct {
  45. Username string `yaml:"username"`
  46. Displayname string `yaml:"displayname"`
  47. Avatar string `yaml:"avatar"`
  48. } `yaml:"bot"`
  49. ASToken string `yaml:"as_token"`
  50. HSToken string `yaml:"hs_token"`
  51. } `yaml:"appservice"`
  52. Metrics struct {
  53. Enabled bool `yaml:"enabled"`
  54. Listen string `yaml:"listen"`
  55. } `yaml:"metrics"`
  56. WhatsApp struct {
  57. OSName string `yaml:"os_name"`
  58. BrowserName string `yaml:"browser_name"`
  59. } `yaml:"whatsapp"`
  60. Bridge BridgeConfig `yaml:"bridge"`
  61. Logging appservice.LogConfig `yaml:"logging"`
  62. }
  63. func (config *Config) setDefaults() {
  64. config.AppService.Database.MaxOpenConns = 20
  65. config.AppService.Database.MaxIdleConns = 2
  66. config.WhatsApp.OSName = "Mautrix-WhatsApp bridge"
  67. config.WhatsApp.BrowserName = "mx-wa"
  68. config.Bridge.setDefaults()
  69. }
  70. func Load(path string) (*Config, error) {
  71. data, err := ioutil.ReadFile(path)
  72. if err != nil {
  73. return nil, err
  74. }
  75. var config = &Config{}
  76. config.setDefaults()
  77. err = yaml.Unmarshal(data, config)
  78. return config, err
  79. }
  80. func (config *Config) Save(path string) error {
  81. data, err := yaml.Marshal(config)
  82. if err != nil {
  83. return err
  84. }
  85. return ioutil.WriteFile(path, data, 0600)
  86. }
  87. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  88. as := appservice.Create()
  89. as.HomeserverDomain = config.Homeserver.Domain
  90. as.HomeserverURL = config.Homeserver.Address
  91. as.Host.Hostname = config.AppService.Hostname
  92. as.Host.Port = config.AppService.Port
  93. var err error
  94. as.Registration, err = config.GetRegistration()
  95. return as, err
  96. }