config.go 3.4 KB

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