config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ParsedAvatar id.ContentURI `yaml:"-"`
  52. } `yaml:"bot"`
  53. EphemeralEvents bool `yaml:"ephemeral_events"`
  54. ASToken string `yaml:"as_token"`
  55. HSToken string `yaml:"hs_token"`
  56. } `yaml:"appservice"`
  57. Metrics struct {
  58. Enabled bool `yaml:"enabled"`
  59. Listen string `yaml:"listen"`
  60. } `yaml:"metrics"`
  61. WhatsApp struct {
  62. OSName string `yaml:"os_name"`
  63. BrowserName string `yaml:"browser_name"`
  64. } `yaml:"whatsapp"`
  65. Bridge BridgeConfig `yaml:"bridge"`
  66. Logging appservice.LogConfig `yaml:"logging"`
  67. }
  68. func (config *Config) CanAutoDoublePuppet(userID id.UserID) bool {
  69. _, homeserver, _ := userID.Parse()
  70. _, hasSecret := config.Bridge.LoginSharedSecretMap[homeserver]
  71. return hasSecret
  72. }
  73. func (config *Config) CanDoublePuppetBackfill(userID id.UserID) bool {
  74. if !config.Bridge.HistorySync.DoublePuppetBackfill {
  75. return false
  76. }
  77. _, homeserver, _ := userID.Parse()
  78. // Batch sending can only use local users, so don't allow double puppets on other servers.
  79. if homeserver != config.Homeserver.Domain {
  80. return false
  81. }
  82. return true
  83. }
  84. func Load(data []byte, upgraded bool) (*Config, error) {
  85. var config = &Config{}
  86. if !upgraded {
  87. // Fallback: if config upgrading failed, load example config for base values
  88. err := yaml.Unmarshal([]byte(ExampleConfig), config)
  89. if err != nil {
  90. return nil, fmt.Errorf("failed to unmarshal example config: %w", err)
  91. }
  92. }
  93. err := yaml.Unmarshal(data, config)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return config, err
  98. }
  99. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  100. as := appservice.Create()
  101. as.HomeserverDomain = config.Homeserver.Domain
  102. as.HomeserverURL = config.Homeserver.Address
  103. as.Host.Hostname = config.AppService.Hostname
  104. as.Host.Port = config.AppService.Port
  105. as.MessageSendCheckpointEndpoint = config.Homeserver.MessageSendCheckpointEndpoint
  106. as.DefaultHTTPRetries = 4
  107. var err error
  108. as.Registration, err = config.GetRegistration()
  109. return as, err
  110. }