bridge.go 5.7 KB

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