5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-09-19 21:32:31 +00:00

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
This commit is contained in:
Alexandre GV 2021-05-13 22:39:25 +02:00 committed by GitHub
parent a0bca42a7a
commit ac4aee39e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 33 deletions

View File

@ -85,6 +85,7 @@ type ChannelMember struct {
type ChannelMembers []ChannelMember type ChannelMembers []ChannelMember
type Protocol struct { type Protocol struct {
AllowMention []string // discord
AuthCode string // steam AuthCode string // steam
BindAddress string // mattermost, slack // DEPRECATED BindAddress string // mattermost, slack // DEPRECATED
Buffer int // api Buffer int // api

View File

@ -305,6 +305,7 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st
m := discordgo.MessageSend{ m := discordgo.MessageSend{
Content: msg.Username + msg.Text, Content: msg.Username + msg.Text,
AllowedMentions: b.getAllowedMentions(),
} }
if msg.ParentValid() { if msg.ParentValid() {
@ -337,6 +338,7 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri
m := discordgo.MessageSend{ m := discordgo.MessageSend{
Content: msg.Username + fi.Comment, Content: msg.Username + fi.Comment,
Files: []*discordgo.File{&file}, Files: []*discordgo.File{&file},
AllowedMentions: b.getAllowedMentions(),
} }
_, err = b.c.ChannelMessageSendComplex(channelID, &m) _, err = b.c.ChannelMessageSendComplex(channelID, &m)
if err != nil { if err != nil {

View File

@ -9,6 +9,30 @@ import (
"github.com/matterbridge/discordgo" "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 { func (b *Bdiscord) getNick(user *discordgo.User, guildID string) string {
b.membersMutex.RLock() b.membersMutex.RLock()
defer b.membersMutex.RUnlock() defer b.membersMutex.RUnlock()

View File

@ -66,6 +66,7 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
Content: msg.Text, Content: msg.Text,
Username: msg.Username, Username: msg.Username,
AvatarURL: msg.Avatar, AvatarURL: msg.Avatar,
AllowedMentions: b.getAllowedMentions(),
}, },
) )
if err != nil { if err != nil {
@ -92,6 +93,7 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
AvatarURL: msg.Avatar, AvatarURL: msg.Avatar,
File: &file, File: &file,
Content: content, Content: content,
AllowedMentions: b.getAllowedMentions(),
}, },
) )
if e2 != nil { if e2 != nil {
@ -126,6 +128,7 @@ func (b *Bdiscord) handleEventWebhook(msg *config.Message, channelID string) (st
err := b.transmitter.Edit(channelID, msg.ID, &discordgo.WebhookParams{ err := b.transmitter.Edit(channelID, msg.ID, &discordgo.WebhookParams{
Content: msg.Text, Content: msg.Text,
Username: msg.Username, Username: msg.Username,
AllowedMentions: b.getAllowedMentions(),
}) })
if err == nil { if err == nil {
return msg.ID, nil return msg.ID, nil

View File

@ -848,6 +848,14 @@ Server="yourservername"
## All settings below can be reloaded by editing the file. ## All settings below can be reloaded by editing the file.
## They are also all optional. ## 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 shows the title, description and URL of embedded messages (sent by other bots)
ShowEmbeds=false ShowEmbeds=false