Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 95ea5535981bfe289d2c0793cb17cb11a233795e Author: Alexandre GV <contact@alexandregv.fr> Date: Thu May 13 22:39:25 2021 +0000 discord: Add AllowMention to restrict allowed mentions (#1462) * Add DisablePingEveryoneHere/DisablePingRoles/DisablePingUsers keys to config * Add basic AllowedMentions behavior to discord webhooks * Initialize b.AllowedMentions on Discord Bridger init * Call b.getAllowedMentions on each webhook to allow config hot reloading * Add AllowedMentions on all Discord webhooks/messages * Add DisablePingEveryoneHere/DisablePingRoles/DisablePingUsers to matterbridge.toml.sample * Change 'Disable' for 'Allow' and revert logic in Discord AllowedMentions * Update Discord AllowedMentions in matterbridge.toml.sample * Fix typo in DisableWebPagePreview * Replace 'AllowPingEveryoneHere' with 'AllowPingEveryone' * Replace 3 AllowPingEveryone/Roles/Users bools with an array * Fix typo diff --git a/bridge/config/config.go b/bridge/config/config.go index 9893520..b5e03fc 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -8527 +8528 @@ type ChannelMember struct {  type ChannelMembers []ChannelMember    type Protocol struct { - AuthCode string // steam - BindAddress string // mattermost, slack // DEPRECATED - Buffer int // api - Charset string // irc - ClientID string // msteams - ColorNicks bool // only irc for now - Debug bool // general - DebugLevel int // only for irc now - DisableWebPagePreview bool // telegram - EditSuffix string // mattermost, slack, discord, telegram, gitter - EditDisable bool // mattermost, slack, discord, telegram, gitter - HTMLDisable bool // matrix - IconURL string // mattermost, slack - IgnoreFailureOnStart bool // general - IgnoreNicks string // all protocols - IgnoreMessages string // all protocols - Jid string // xmpp - JoinDelay string // all protocols - Label string // all protocols - Login string // mattermost, matrix - LogFile string // general + AllowMention []string // discord + AuthCode string // steam + BindAddress string // mattermost, slack // DEPRECATED + Buffer int // api + Charset string // irc + ClientID string // msteams + ColorNicks bool // only irc for now + Debug bool // general + DebugLevel int // only for irc now + DisableWebPagePreview bool // telegram + EditSuffix string // mattermost, slack, discord, telegram, gitter + EditDisable bool // mattermost, slack, discord, telegram, gitter + HTMLDisable bool // matrix + IconURL string // mattermost, slack + IgnoreFailureOnStart bool // general + IgnoreNicks string // all protocols + IgnoreMessages string // all protocols + Jid string // xmpp + JoinDelay string // all protocols + Label string // all protocols + Login string // mattermost, matrix + LogFile string // general   MediaDownloadBlackList []string   MediaDownloadPath string // Basically MediaServerUpload, but instead of uploading it, just write it to a file on the same server.   MediaDownloadSize int // all protocols diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 9cc83dd..2fcf0ab 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -3047 +3048 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st   }     m := discordgo.MessageSend{ - Content: msg.Username + msg.Text, + Content: msg.Username + msg.Text, + AllowedMentions: b.getAllowedMentions(),   }     if msg.ParentValid() { @@ -3358 +3369 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri   Reader: bytes.NewReader(*fi.Data),   }   m := discordgo.MessageSend{ - Content: msg.Username + fi.Comment, - Files: []*discordgo.File{&file}, + Content: msg.Username + fi.Comment, + Files: []*discordgo.File{&file}, + AllowedMentions: b.getAllowedMentions(),   }   _, err = b.c.ChannelMessageSendComplex(channelID, &m)   if err != nil { diff --git a/bridge/discord/helpers.go b/bridge/discord/helpers.go index 9545a3a..4e453ad 100644 --- a/bridge/discord/helpers.go +++ b/bridge/discord/helpers.go @@ -96 +930 @@ import (   "github.com/matterbridge/discordgo"  )   +func (b *Bdiscord) getAllowedMentions() *discordgo.MessageAllowedMentions { + // If AllowMention is not specified, then allow all mentions (default Discord behavior) + if !b.IsKeySet("AllowMention") { + return nil + } + + // Otherwise, allow only the mentions that are specified + allowedMentionTypes := make([]discordgo.AllowedMentionType, 0, 3) + for _, m := range b.GetStringSlice("AllowMention") { + switch m { + case "everyone": + allowedMentionTypes = append(allowedMentionTypes, discordgo.AllowedMentionTypeEveryone) + case "roles": + allowedMentionTypes = append(allowedMentionTypes, discordgo.AllowedMentionTypeRoles) + case "users": + allowedMentionTypes = append(allowedMentionTypes, discordgo.AllowedMentionTypeUsers) + } + } + + return &discordgo.MessageAllowedMentions{ + Parse: allowedMentionTypes, + } +} +  func (b *Bdiscord) getNick(user *discordgo.User, guildID string) string {   b.membersMutex.RLock()   defer b.membersMutex.RUnlock() diff --git a/bridge/discord/webhook.go b/bridge/discord/webhook.go index 7dacc7c..9177db0 100644 --- a/bridge/discord/webhook.go +++ b/bridge/discord/webhook.go @@ -639 +6310 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg   res, err = b.transmitter.Send(   channelID,   &discordgo.WebhookParams{ - Content: msg.Text, - Username: msg.Username, - AvatarURL: msg.Avatar, + Content: msg.Text, + Username: msg.Username, + AvatarURL: msg.Avatar, + AllowedMentions: b.getAllowedMentions(),   },   )   if err != nil { @@ -8810 +8911 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg   _, e2 := b.transmitter.Send(   channelID,   &discordgo.WebhookParams{ - Username: msg.Username, - AvatarURL: msg.Avatar, - File: &file, - Content: content, + Username: msg.Username, + AvatarURL: msg.Avatar, + File: &file, + Content: content, + AllowedMentions: b.getAllowedMentions(),   },   )   if e2 != nil { @@ -1248 +1269 @@ func (b *Bdiscord) handleEventWebhook(msg *config.Message, channelID string) (st   if msg.ID != "" {   b.Log.Debugf("Editing webhook message")   err := b.transmitter.Edit(channelID, msg.ID, &discordgo.WebhookParams{ - Content: msg.Text, - Username: msg.Username, + Content: msg.Text, + Username: msg.Username, + AllowedMentions: b.getAllowedMentions(),   })   if err == nil {   return msg.ID, nil diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index b44ea2f..6a5d406 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -8486 +84814 @@ Server="yourservername"  ## All settings below can be reloaded by editing the file.  ## They are also all optional.   +# AllowMention controls which mentions are allowed. If not specified, all mentions are allowed. +# Note that even when a mention is not allowed, it will still be displayed nicely and be clickable. It just prevents the ping/notification. +# +# "everyone" allows @everyone and @here mentions +# "roles" allows @role mentions +# "users" allows @user mentions +AllowMention=["everyone", "roles", "users"] +  # ShowEmbeds shows the title, description and URL of embedded messages (sent by other bots)  ShowEmbeds=false