bridge.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "fmt"
  19. "strings"
  20. "text/template"
  21. "github.com/bwmarrin/discordgo"
  22. "maunium.net/go/mautrix/bridge/bridgeconfig"
  23. )
  24. type BridgeConfig struct {
  25. UsernameTemplate string `yaml:"username_template"`
  26. DisplaynameTemplate string `yaml:"displayname_template"`
  27. ChannelnameTemplate string `yaml:"channelname_template"`
  28. CommandPrefix string `yaml:"command_prefix"`
  29. ManagementRoomText bridgeconfig.ManagementRoomTexts `yaml:"management_room_text"`
  30. PortalMessageBuffer int `yaml:"portal_message_buffer"`
  31. SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
  32. SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
  33. DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"`
  34. DefaultBridgePresence bool `yaml:"default_bridge_presence"`
  35. DoublePuppetServerMap map[string]string `yaml:"double_puppet_server_map"`
  36. DoublePuppetAllowDiscovery bool `yaml:"double_puppet_allow_discovery"`
  37. LoginSharedSecretMap map[string]string `yaml:"login_shared_secret_map"`
  38. Encryption bridgeconfig.EncryptionConfig `yaml:"encryption"`
  39. Provisioning struct {
  40. Prefix string `yaml:"prefix"`
  41. SharedSecret string `yaml:"shared_secret"`
  42. } `yaml:"provisioning"`
  43. Permissions bridgeconfig.PermissionConfig `yaml:"permissions"`
  44. usernameTemplate *template.Template `yaml:"-"`
  45. displaynameTemplate *template.Template `yaml:"-"`
  46. channelnameTemplate *template.Template `yaml:"-"`
  47. }
  48. type umBridgeConfig BridgeConfig
  49. func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  50. err := unmarshal((*umBridgeConfig)(bc))
  51. if err != nil {
  52. return err
  53. }
  54. bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
  55. if err != nil {
  56. return err
  57. } else if !strings.Contains(bc.FormatUsername("1234567890"), "1234567890") {
  58. return fmt.Errorf("username template is missing user ID placeholder")
  59. }
  60. bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
  61. if err != nil {
  62. return err
  63. }
  64. bc.channelnameTemplate, err = template.New("channelname").Parse(bc.ChannelnameTemplate)
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. var _ bridgeconfig.BridgeConfig = (*BridgeConfig)(nil)
  71. func (bc BridgeConfig) GetEncryptionConfig() bridgeconfig.EncryptionConfig {
  72. return bc.Encryption
  73. }
  74. func (bc BridgeConfig) GetCommandPrefix() string {
  75. return bc.CommandPrefix
  76. }
  77. func (bc BridgeConfig) GetManagementRoomTexts() bridgeconfig.ManagementRoomTexts {
  78. return bc.ManagementRoomText
  79. }
  80. func (bc BridgeConfig) FormatUsername(userid string) string {
  81. var buffer strings.Builder
  82. _ = bc.usernameTemplate.Execute(&buffer, userid)
  83. return buffer.String()
  84. }
  85. func (bc BridgeConfig) FormatDisplayname(user *discordgo.User) string {
  86. var buffer strings.Builder
  87. _ = bc.displaynameTemplate.Execute(&buffer, user)
  88. return buffer.String()
  89. }
  90. type wrappedChannel struct {
  91. *discordgo.Channel
  92. Guild string
  93. Folder string
  94. }
  95. func (bc BridgeConfig) FormatChannelname(channel *discordgo.Channel, session *discordgo.Session) (string, error) {
  96. var buffer strings.Builder
  97. var guildName, folderName string
  98. if channel.Type != discordgo.ChannelTypeDM && channel.Type != discordgo.ChannelTypeGroupDM {
  99. guild, err := session.Guild(channel.GuildID)
  100. if err != nil {
  101. return "", fmt.Errorf("find guild: %w", err)
  102. }
  103. guildName = guild.Name
  104. folder, err := session.Channel(channel.ParentID)
  105. if err == nil {
  106. folderName = folder.Name
  107. }
  108. } else {
  109. // Group DM's can have a name, but DM's can't, so if we didn't get a
  110. // name return a comma separated list of the formatted user names.
  111. if channel.Name == "" {
  112. recipients := make([]string, len(channel.Recipients))
  113. for idx, user := range channel.Recipients {
  114. recipients[idx] = bc.FormatDisplayname(user)
  115. }
  116. return strings.Join(recipients, ", "), nil
  117. }
  118. }
  119. _ = bc.channelnameTemplate.Execute(&buffer, wrappedChannel{
  120. Channel: channel,
  121. Guild: guildName,
  122. Folder: folderName,
  123. })
  124. return buffer.String(), nil
  125. }