bridge.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // mautrix-discord - A Matrix-Discord 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. "errors"
  19. "fmt"
  20. "strings"
  21. "text/template"
  22. "github.com/bwmarrin/discordgo"
  23. "maunium.net/go/mautrix/bridge/bridgeconfig"
  24. )
  25. type BridgeConfig struct {
  26. UsernameTemplate string `yaml:"username_template"`
  27. DisplaynameTemplate string `yaml:"displayname_template"`
  28. ChannelNameTemplate string `yaml:"channel_name_template"`
  29. GuildNameTemplate string `yaml:"guild_name_template"`
  30. PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"`
  31. PrivateChannelCreateLimit int `yaml:"startup_private_channel_create_limit"`
  32. PortalMessageBuffer int `yaml:"portal_message_buffer"`
  33. DeliveryReceipts bool `yaml:"delivery_receipts"`
  34. MessageStatusEvents bool `yaml:"message_status_events"`
  35. MessageErrorNotices bool `yaml:"message_error_notices"`
  36. RestrictedRooms bool `yaml:"restricted_rooms"`
  37. SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
  38. ResendBridgeInfo bool `yaml:"resend_bridge_info"`
  39. DeletePortalOnChannelDelete bool `yaml:"delete_portal_on_channel_delete"`
  40. FederateRooms bool `yaml:"federate_rooms"`
  41. DoublePuppetServerMap map[string]string `yaml:"double_puppet_server_map"`
  42. DoublePuppetAllowDiscovery bool `yaml:"double_puppet_allow_discovery"`
  43. LoginSharedSecretMap map[string]string `yaml:"login_shared_secret_map"`
  44. CommandPrefix string `yaml:"command_prefix"`
  45. ManagementRoomText bridgeconfig.ManagementRoomTexts `yaml:"management_room_text"`
  46. Encryption bridgeconfig.EncryptionConfig `yaml:"encryption"`
  47. Provisioning struct {
  48. Prefix string `yaml:"prefix"`
  49. SharedSecret string `yaml:"shared_secret"`
  50. } `yaml:"provisioning"`
  51. Permissions bridgeconfig.PermissionConfig `yaml:"permissions"`
  52. usernameTemplate *template.Template `yaml:"-"`
  53. displaynameTemplate *template.Template `yaml:"-"`
  54. channelNameTemplate *template.Template `yaml:"-"`
  55. guildNameTemplate *template.Template `yaml:"-"`
  56. }
  57. func (bc *BridgeConfig) GetResendBridgeInfo() bool {
  58. return bc.ResendBridgeInfo
  59. }
  60. func (bc *BridgeConfig) EnableMessageStatusEvents() bool {
  61. return bc.MessageStatusEvents
  62. }
  63. func (bc *BridgeConfig) EnableMessageErrorNotices() bool {
  64. return bc.MessageErrorNotices
  65. }
  66. func boolToInt(val bool) int {
  67. if val {
  68. return 1
  69. }
  70. return 0
  71. }
  72. func (bc *BridgeConfig) Validate() error {
  73. _, hasWildcard := bc.Permissions["*"]
  74. _, hasExampleDomain := bc.Permissions["example.com"]
  75. _, hasExampleUser := bc.Permissions["@admin:example.com"]
  76. exampleLen := boolToInt(hasWildcard) + boolToInt(hasExampleUser) + boolToInt(hasExampleDomain)
  77. if len(bc.Permissions) <= exampleLen {
  78. return errors.New("bridge.permissions not configured")
  79. }
  80. return nil
  81. }
  82. type umBridgeConfig BridgeConfig
  83. func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  84. err := unmarshal((*umBridgeConfig)(bc))
  85. if err != nil {
  86. return err
  87. }
  88. bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
  89. if err != nil {
  90. return err
  91. } else if !strings.Contains(bc.FormatUsername("1234567890"), "1234567890") {
  92. return fmt.Errorf("username template is missing user ID placeholder")
  93. }
  94. bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
  95. if err != nil {
  96. return err
  97. }
  98. bc.channelNameTemplate, err = template.New("channel_name").Parse(bc.ChannelNameTemplate)
  99. if err != nil {
  100. return err
  101. }
  102. bc.guildNameTemplate, err = template.New("guild_name").Parse(bc.GuildNameTemplate)
  103. if err != nil {
  104. return err
  105. }
  106. return nil
  107. }
  108. var _ bridgeconfig.BridgeConfig = (*BridgeConfig)(nil)
  109. func (bc BridgeConfig) GetEncryptionConfig() bridgeconfig.EncryptionConfig {
  110. return bc.Encryption
  111. }
  112. func (bc BridgeConfig) GetCommandPrefix() string {
  113. return bc.CommandPrefix
  114. }
  115. func (bc BridgeConfig) GetManagementRoomTexts() bridgeconfig.ManagementRoomTexts {
  116. return bc.ManagementRoomText
  117. }
  118. func (bc BridgeConfig) FormatUsername(userID string) string {
  119. var buffer strings.Builder
  120. _ = bc.usernameTemplate.Execute(&buffer, userID)
  121. return buffer.String()
  122. }
  123. func (bc BridgeConfig) FormatDisplayname(user *discordgo.User) string {
  124. var buffer strings.Builder
  125. _ = bc.displaynameTemplate.Execute(&buffer, user)
  126. return buffer.String()
  127. }
  128. type ChannelNameParams struct {
  129. Name string
  130. ParentName string
  131. GuildName string
  132. NSFW bool
  133. Type discordgo.ChannelType
  134. }
  135. func (bc BridgeConfig) FormatChannelName(params ChannelNameParams) string {
  136. var buffer strings.Builder
  137. _ = bc.channelNameTemplate.Execute(&buffer, params)
  138. return buffer.String()
  139. }
  140. type GuildNameParams struct {
  141. Name string
  142. }
  143. func (bc BridgeConfig) FormatGuildName(params GuildNameParams) string {
  144. var buffer strings.Builder
  145. _ = bc.guildNameTemplate.Execute(&buffer, params)
  146. return buffer.String()
  147. }