bridge.go 5.6 KB

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