Switch to better solution for disabling URL embeds.

This commit is contained in:
Kufat 2022-08-08 13:00:06 -04:00
parent e275bbfb2e
commit b82b7b67a8
5 changed files with 27 additions and 12 deletions

View File

@ -36,6 +36,9 @@ type Bdiscord struct {
userMemberMap map[string]*discordgo.Member userMemberMap map[string]*discordgo.Member
nickMemberMap map[string]*discordgo.Member nickMemberMap map[string]*discordgo.Member
noEmbedPartUrls bool
noEmbedUrls bool
// Webhook specific logic // Webhook specific logic
useAutoWebhooks bool useAutoWebhooks bool
transmitter *transmitter.Transmitter transmitter *transmitter.Transmitter
@ -57,6 +60,12 @@ func New(cfg *bridge.Config) bridge.Bridger {
b.nickMemberMap = make(map[string]*discordgo.Member) b.nickMemberMap = make(map[string]*discordgo.Member)
b.channelInfoMap = make(map[string]*config.ChannelInfo) 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")
}
b.useAutoWebhooks = b.GetBool("AutoWebhooks") b.useAutoWebhooks = b.GetBool("AutoWebhooks")
if b.useAutoWebhooks { if b.useAutoWebhooks {
b.Log.Debug("Using automatic webhooks") b.Log.Debug("Using automatic webhooks")
@ -269,6 +278,10 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
msg.Text = "_" + msg.Text + "_" msg.Text = "_" + msg.Text + "_"
} }
if b.noEmbedUrls || (msg.Event == config.EventJoinLeave && b.noEmbedPartUrls) {
disableEmbedUrls(&msg.Text)
}
// Handle prefix hint for unthreaded messages. // Handle prefix hint for unthreaded messages.
if msg.ParentNotFound() { if msg.ParentNotFound() {
msg.ParentID = "" msg.ParentID = ""

View File

@ -233,6 +233,11 @@ func (b *Bdiscord) splitURL(url string) (string, string, bool) {
return webhookURLSplit[webhookIdxID], webhookURLSplit[webhookIdxToken], true 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 { func enumerateUsernames(s string) []string {
onlySpace := true onlySpace := true
for _, r := range s { for _, r := range s {

View File

@ -84,10 +84,6 @@ func isKill(quitmsg string) bool {
return strings.HasPrefix(quitmsg, "Killed") || strings.HasPrefix(quitmsg, "Local kill") || strings.HasPrefix(quitmsg[1:], "-lined") return strings.HasPrefix(quitmsg, "Killed") || strings.HasPrefix(quitmsg, "Local kill") || strings.HasPrefix(quitmsg[1:], "-lined")
} }
func suppressUrls(msg *string) {
*msg = strings.ReplaceAll(*msg, "://", ": //")
}
func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) { func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
if len(event.Params) == 0 { if len(event.Params) == 0 {
b.Log.Debugf("handleJoinPart: empty Params? %#v", event) b.Log.Debugf("handleJoinPart: empty Params? %#v", event)
@ -118,9 +114,6 @@ func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
if event.Command == "PART" && len(event.Params) >= 2 { if event.Command == "PART" && len(event.Params) >= 2 {
partmsg = " with message: " + event.Last() partmsg = " with message: " + event.Last()
} }
if len(partmsg) > 0 && b.SuppressPartQuitURLs {
suppressUrls(&partmsg)
}
msg := config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s" + partmsg, Channel: channel, Account: b.Account, Event: config.EventJoinLeave} msg := config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s" + partmsg, Channel: channel, Account: b.Account, Event: config.EventJoinLeave}
if b.GetBool("verbosejoinpart") { if b.GetBool("verbosejoinpart") {
b.Log.Debugf("<= Sending verbose JOIN_LEAVE event from %s to gateway", b.Account) b.Log.Debugf("<= Sending verbose JOIN_LEAVE event from %s to gateway", b.Account)
@ -296,9 +289,6 @@ func (b *Birc) handleQuit(client *girc.Client, event girc.Event) {
if len(event.Params) >= 1 { if len(event.Params) >= 1 {
quitmsg = " with message: " + event.Last() quitmsg = " with message: " + event.Last()
} }
if len(quitmsg) > 0 && b.SuppressPartQuitURLs {
suppressUrls(&quitmsg)
}
if b.GetBool("verbosejoinpart") { if b.GetBool("verbosejoinpart") {
verbosequit = " (" + event.Source.Ident + "@" + event.Source.Host + ")" verbosequit = " (" + event.Source.Ident + "@" + event.Source.Host + ")"
} }

View File

@ -34,7 +34,6 @@ type Birc struct {
connected chan error connected chan error
Local chan config.Message // local queue for flood control Local chan config.Message // local queue for flood control
FirstConnection, authDone bool FirstConnection, authDone bool
SuppressPartQuitURLs bool
MessageDelay, MessageQueue, MessageLength int MessageDelay, MessageQueue, MessageLength int
ActivityTimeout int64 ActivityTimeout int64
channels map[string]bool channels map[string]bool
@ -76,7 +75,6 @@ func New(cfg *bridge.Config) bridge.Bridger {
} else { } else {
b.ActivityTimeout = 0 // Disable b.ActivityTimeout = 0 // Disable
} }
b.SuppressPartQuitURLs = b.GetBool("SuppressPartQuitURLs")
b.FirstConnection = true b.FirstConnection = true
return b return b
} }

View File

@ -947,6 +947,15 @@ IgnoreNicks=""
# IgnoreMessages="^~~ badword" # IgnoreMessages="^~~ badword"
IgnoreMessages="" 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. # ReplaceMessages replaces substrings of messages in outgoing messages.
# Regular expressions are supported. # Regular expressions are supported.
# #