Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally suppress URL embeds for Discord #1889

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions bridge/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Bdiscord struct {
userMemberMap map[string]*discordgo.Member
nickMemberMap map[string]*discordgo.Member

noEmbedPartUrls bool
noEmbedUrls bool

// Webhook specific logic
useAutoWebhooks bool
transmitter *transmitter.Transmitter
Expand All @@ -57,6 +60,12 @@ func New(cfg *bridge.Config) bridge.Bridger {
b.nickMemberMap = make(map[string]*discordgo.Member)
b.channelInfoMap = make(map[string]*config.ChannelInfo)

b.noEmbedPartUrls = b.GetBool("NoEmbedPartUrls")
b.noEmbedUrls = b.GetBool("NoEmbedUrls")
if b.noEmbedPartUrls && b.noEmbedUrls {
b.Log.Info("NoEmbedUrls supersedes NoEmbedPartUrls when both are specified.")
}

b.useAutoWebhooks = b.GetBool("AutoWebhooks")
if b.useAutoWebhooks {
b.Log.Debug("Using automatic webhooks")
Expand Down Expand Up @@ -269,6 +278,10 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
msg.Text = "_" + msg.Text + "_"
}

if b.noEmbedUrls || (msg.Event == config.EventJoinLeave && b.noEmbedPartUrls) {
disableEmbedUrls(&msg.Text)
}

// Handle prefix hint for unthreaded messages.
if msg.ParentNotFound() {
msg.ParentID = ""
Expand Down
5 changes: 5 additions & 0 deletions bridge/discord/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ func (b *Bdiscord) splitURL(url string) (string, string, bool) {
return webhookURLSplit[webhookIdxID], webhookURLSplit[webhookIdxToken], true
}

func disableEmbedUrls(msg *string) {
regex := regexp.MustCompile(`(\w+://\S+)`)
*msg = regex.ReplaceAllString(*msg, "<$1>")
}

func enumerateUsernames(s string) []string {
onlySpace := true
for _, r := range s {
Expand Down
9 changes: 9 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,15 @@ IgnoreNicks=""
# IgnoreMessages="^~~ badword"
IgnoreMessages=""

# Prevent URL embeds by encasing URLs in <> angle brackets.
# Useful if trolls are a problem on the other end of your bridge.
NoEmbedUrls=false

# Prevent URL embeds in part/quit messages by encasing URLs in <> angle brackets.
# Useful if trolls spam in their quit messages or you're tired of seeing embeds for
# IRC client homepages.
NoEmbedPartUrls=false

# ReplaceMessages replaces substrings of messages in outgoing messages.
# Regular expressions are supported.
#
Expand Down