diff --git a/formatter.go b/formatter.go index 6ac2d51..24c0590 100644 --- a/formatter.go +++ b/formatter.go @@ -156,11 +156,14 @@ func (br *DiscordBridge) pillConverter(displayname, mxid, eventID string, ctx fo return displayname } +const discordLinkPattern = `https?://[^<\p{Zs}\x{feff}]*[^"'),.:;\]\p{Zs}\x{feff}]` + // Discord links start with http:// or https://, contain at least two characters afterwards, // don't contain < or whitespace anywhere, and don't end with "'),.:;] // // Zero-width whitespace is mostly in the Format category and is allowed, except \uFEFF isn't for some reason -var discordLinkRegex = regexp.MustCompile(`https?://[^<\p{Zs}\x{feff}]*[^"'),.:;\]\p{Zs}\x{feff}]`) +var discordLinkRegex = regexp.MustCompile(discordLinkPattern) +var discordLinkRegexFull = regexp.MustCompile("^" + discordLinkPattern + "$") var discordMarkdownEscaper = strings.NewReplacer( `\`, `\\`, diff --git a/portal_convert.go b/portal_convert.go index f0148a1..4192b5c 100644 --- a/portal_convert.go +++ b/portal_convert.go @@ -621,9 +621,14 @@ func getEmbedType(msg *discordgo.Message, embed *discordgo.MessageEmbed) BridgeE } func isPlainGifMessage(msg *discordgo.Message) bool { - return len(msg.Embeds) == 1 && msg.Embeds[0].URL == msg.Content && - ((msg.Embeds[0].Type == discordgo.EmbedTypeGifv && msg.Embeds[0].Video != nil) || - (msg.Embeds[0].Type == discordgo.EmbedTypeImage && msg.Embeds[0].Image == nil && msg.Embeds[0].Thumbnail != nil)) + if len(msg.Embeds) != 1 { + return false + } + embed := msg.Embeds[0] + isGifVideo := embed.Type == discordgo.EmbedTypeGifv && embed.Video != nil + isGifImage := embed.Type == discordgo.EmbedTypeImage && embed.Image == nil && embed.Thumbnail != nil + contentIsOnlyURL := msg.Content == embed.URL || discordLinkRegexFull.MatchString(msg.Content) + return contentIsOnlyURL && (isGifVideo || isGifImage) } func (portal *Portal) convertDiscordMentions(msg *discordgo.Message, syncGhosts bool) *event.Mentions {