config.go 3.3 KB

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