config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "gopkg.in/yaml.v3"
  20. "maunium.net/go/mautrix/appservice"
  21. "maunium.net/go/mautrix/id"
  22. )
  23. var ExampleConfig string
  24. type Config struct {
  25. Homeserver struct {
  26. Address string `yaml:"address"`
  27. Domain string `yaml:"domain"`
  28. Asmux bool `yaml:"asmux"`
  29. StatusEndpoint string `yaml:"status_endpoint"`
  30. MessageSendCheckpointEndpoint string `yaml:"message_send_checkpoint_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. EphemeralEvents bool `yaml:"ephemeral_events"`
  53. ASToken string `yaml:"as_token"`
  54. HSToken string `yaml:"hs_token"`
  55. } `yaml:"appservice"`
  56. Metrics struct {
  57. Enabled bool `yaml:"enabled"`
  58. Listen string `yaml:"listen"`
  59. } `yaml:"metrics"`
  60. WhatsApp struct {
  61. OSName string `yaml:"os_name"`
  62. BrowserName string `yaml:"browser_name"`
  63. } `yaml:"whatsapp"`
  64. Bridge BridgeConfig `yaml:"bridge"`
  65. Logging appservice.LogConfig `yaml:"logging"`
  66. }
  67. func (config *Config) CanAutoDoublePuppet(userID id.UserID) bool {
  68. _, homeserver, _ := userID.Parse()
  69. _, hasSecret := config.Bridge.LoginSharedSecretMap[homeserver]
  70. return hasSecret
  71. }
  72. func (config *Config) CanDoublePuppetBackfill(userID id.UserID) bool {
  73. if !config.Bridge.HistorySync.DoublePuppetBackfill {
  74. return false
  75. }
  76. _, homeserver, _ := userID.Parse()
  77. // Batch sending can only use local users, so don't allow double puppets on other servers.
  78. if homeserver != config.Homeserver.Domain {
  79. return false
  80. }
  81. return true
  82. }
  83. func Load(data []byte, upgraded bool) (*Config, error) {
  84. var config = &Config{}
  85. if !upgraded {
  86. // Fallback: if config upgrading failed, load example config for base values
  87. err := yaml.Unmarshal([]byte(ExampleConfig), config)
  88. if err != nil {
  89. return nil, fmt.Errorf("failed to unmarshal example config: %w", err)
  90. }
  91. }
  92. err := yaml.Unmarshal(data, config)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return config, err
  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. as.MessageSendCheckpointEndpoint = config.Homeserver.MessageSendCheckpointEndpoint
  105. as.DefaultHTTPRetries = 4
  106. var err error
  107. as.Registration, err = config.GetRegistration()
  108. return as, err
  109. }