bridge.go 6.0 KB

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