config.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 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. AsyncMedia bool `yaml:"async_media"`
  32. } `yaml:"homeserver"`
  33. AppService struct {
  34. Address string `yaml:"address"`
  35. Hostname string `yaml:"hostname"`
  36. Port uint16 `yaml:"port"`
  37. Database DatabaseConfig `yaml:"database"`
  38. Provisioning struct {
  39. Prefix string `yaml:"prefix"`
  40. SharedSecret string `yaml:"shared_secret"`
  41. SegmentKey string `yaml:"segment_key"`
  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. ParsedAvatar id.ContentURI `yaml:"-"`
  49. } `yaml:"bot"`
  50. EphemeralEvents bool `yaml:"ephemeral_events"`
  51. ASToken string `yaml:"as_token"`
  52. HSToken string `yaml:"hs_token"`
  53. } `yaml:"appservice"`
  54. Metrics struct {
  55. Enabled bool `yaml:"enabled"`
  56. Listen string `yaml:"listen"`
  57. } `yaml:"metrics"`
  58. WhatsApp struct {
  59. OSName string `yaml:"os_name"`
  60. BrowserName string `yaml:"browser_name"`
  61. DebugDecodeBeforeSend bool `yaml:"debug_decode_before_send"`
  62. } `yaml:"whatsapp"`
  63. Bridge BridgeConfig `yaml:"bridge"`
  64. Logging appservice.LogConfig `yaml:"logging"`
  65. }
  66. func (config *Config) CanAutoDoublePuppet(userID id.UserID) bool {
  67. _, homeserver, _ := userID.Parse()
  68. _, hasSecret := config.Bridge.LoginSharedSecretMap[homeserver]
  69. return hasSecret
  70. }
  71. func (config *Config) CanDoublePuppetBackfill(userID id.UserID) bool {
  72. if !config.Bridge.HistorySync.DoublePuppetBackfill {
  73. return false
  74. }
  75. _, homeserver, _ := userID.Parse()
  76. // Batch sending can only use local users, so don't allow double puppets on other servers.
  77. if homeserver != config.Homeserver.Domain {
  78. return false
  79. }
  80. return true
  81. }
  82. func Load(data []byte, upgraded bool) (*Config, error) {
  83. var config = &Config{}
  84. if !upgraded {
  85. // Fallback: if config upgrading failed, load example config for base values
  86. err := yaml.Unmarshal([]byte(ExampleConfig), config)
  87. if err != nil {
  88. return nil, fmt.Errorf("failed to unmarshal example config: %w", err)
  89. }
  90. }
  91. err := yaml.Unmarshal(data, config)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return config, err
  96. }
  97. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  98. as := appservice.Create()
  99. as.HomeserverDomain = config.Homeserver.Domain
  100. as.HomeserverURL = config.Homeserver.Address
  101. as.Host.Hostname = config.AppService.Hostname
  102. as.Host.Port = config.AppService.Port
  103. as.MessageSendCheckpointEndpoint = config.Homeserver.MessageSendCheckpointEndpoint
  104. as.DefaultHTTPRetries = 4
  105. var err error
  106. as.Registration, err = config.GetRegistration()
  107. return as, err
  108. }
  109. type DatabaseConfig struct {
  110. Type string `yaml:"type"`
  111. URI string `yaml:"uri"`
  112. MaxOpenConns int `yaml:"max_open_conns"`
  113. MaxIdleConns int `yaml:"max_idle_conns"`
  114. ConnMaxIdleTime string `yaml:"conn_max_idle_time"`
  115. ConnMaxLifetime string `yaml:"conn_max_lifetime"`
  116. }