config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. } `yaml:"homeserver"`
  32. AppService struct {
  33. Address string `yaml:"address"`
  34. Hostname string `yaml:"hostname"`
  35. Port uint16 `yaml:"port"`
  36. Database DatabaseConfig `yaml:"database"`
  37. Provisioning struct {
  38. Prefix string `yaml:"prefix"`
  39. SharedSecret string `yaml:"shared_secret"`
  40. SegmentKey string `yaml:"segment_key"`
  41. } `yaml:"provisioning"`
  42. ID string `yaml:"id"`
  43. Bot struct {
  44. Username string `yaml:"username"`
  45. Displayname string `yaml:"displayname"`
  46. Avatar string `yaml:"avatar"`
  47. ParsedAvatar id.ContentURI `yaml:"-"`
  48. } `yaml:"bot"`
  49. EphemeralEvents bool `yaml:"ephemeral_events"`
  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) CanAutoDoublePuppet(userID id.UserID) bool {
  65. _, homeserver, _ := userID.Parse()
  66. _, hasSecret := config.Bridge.LoginSharedSecretMap[homeserver]
  67. return hasSecret
  68. }
  69. func (config *Config) CanDoublePuppetBackfill(userID id.UserID) bool {
  70. if !config.Bridge.HistorySync.DoublePuppetBackfill {
  71. return false
  72. }
  73. _, homeserver, _ := userID.Parse()
  74. // Batch sending can only use local users, so don't allow double puppets on other servers.
  75. if homeserver != config.Homeserver.Domain {
  76. return false
  77. }
  78. return true
  79. }
  80. func Load(data []byte, upgraded bool) (*Config, error) {
  81. var config = &Config{}
  82. if !upgraded {
  83. // Fallback: if config upgrading failed, load example config for base values
  84. err := yaml.Unmarshal([]byte(ExampleConfig), config)
  85. if err != nil {
  86. return nil, fmt.Errorf("failed to unmarshal example config: %w", err)
  87. }
  88. }
  89. err := yaml.Unmarshal(data, config)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return config, err
  94. }
  95. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  96. as := appservice.Create()
  97. as.HomeserverDomain = config.Homeserver.Domain
  98. as.HomeserverURL = config.Homeserver.Address
  99. as.Host.Hostname = config.AppService.Hostname
  100. as.Host.Port = config.AppService.Port
  101. as.MessageSendCheckpointEndpoint = config.Homeserver.MessageSendCheckpointEndpoint
  102. as.DefaultHTTPRetries = 4
  103. var err error
  104. as.Registration, err = config.GetRegistration()
  105. return as, err
  106. }
  107. type DatabaseConfig struct {
  108. Type string `yaml:"type"`
  109. URI string `yaml:"uri"`
  110. MaxOpenConns int `yaml:"max_open_conns"`
  111. MaxIdleConns int `yaml:"max_idle_conns"`
  112. ConnMaxIdleTime string `yaml:"conn_max_idle_time"`
  113. ConnMaxLifetime string `yaml:"conn_max_lifetime"`
  114. }