mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-15 00:19:24 +01:00
some examples: this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting" ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ] you can use multiple entries for multiplebots this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else" ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ] OPTIONAL (default empty) ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
This commit is contained in:
parent
5095db8a43
commit
3190703dc8
@ -351,6 +351,8 @@ func (gw *Gateway) modifyMessage(msg *config.Message) {
|
||||
msg.Text = re.ReplaceAllString(msg.Text, replace)
|
||||
}
|
||||
|
||||
gw.handleExtractNicks(msg)
|
||||
|
||||
// messages from api have Gateway specified, don't overwrite
|
||||
if msg.Protocol != apiProtocol {
|
||||
msg.Gateway = gw.Name
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/42wim/matterbridge/bridge"
|
||||
@ -225,3 +226,41 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
|
||||
}
|
||||
return brMsgIDs
|
||||
}
|
||||
|
||||
func (gw *Gateway) handleExtractNicks(msg *config.Message) {
|
||||
var err error
|
||||
br := gw.Bridges[msg.Account]
|
||||
for _, outer := range br.GetStringSlice2D("ExtractNicks") {
|
||||
search := outer[0]
|
||||
replace := outer[1]
|
||||
msg.Username, msg.Text, err = extractNick(search, replace, msg.Username, msg.Text)
|
||||
if err != nil {
|
||||
flog.Errorf("regexp in %s failed: %s", msg.Account, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// extractNick searches for a username (based on "search" a regular expression).
|
||||
// if this matches it extracts a nick (based on "extract" another regular expression) from text
|
||||
// and replaces username with this result.
|
||||
// returns error if the regexp doesn't compile.
|
||||
func extractNick(search, extract, username, text string) (string, string, error) {
|
||||
re, err := regexp.Compile(search)
|
||||
if err != nil {
|
||||
return username, text, err
|
||||
}
|
||||
if re.MatchString(username) {
|
||||
re, err = regexp.Compile(extract)
|
||||
if err != nil {
|
||||
return username, text, err
|
||||
}
|
||||
res := re.FindAllStringSubmatch(text, 1)
|
||||
// only replace if we have exactly 1 match
|
||||
if len(res) > 0 && len(res[0]) == 2 {
|
||||
username = res[0][1]
|
||||
text = strings.Replace(text, res[0][0], "", 1)
|
||||
}
|
||||
}
|
||||
return username, text, nil
|
||||
}
|
||||
|
75
gateway/handlers_test.go
Normal file
75
gateway/handlers_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"github.com/42wim/matterbridge/bridge"
|
||||
"github.com/42wim/matterbridge/bridge/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIgnoreEvent(t *testing.T) {
|
||||
eventTests := map[string]struct {
|
||||
input string
|
||||
dest *bridge.Bridge
|
||||
output bool
|
||||
}{
|
||||
"avatar mattermost": {
|
||||
input: config.EventAvatarDownload,
|
||||
dest: &bridge.Bridge{Protocol: "mattermost"},
|
||||
output: false,
|
||||
},
|
||||
"avatar slack": {
|
||||
input: config.EventAvatarDownload,
|
||||
dest: &bridge.Bridge{Protocol: "slack"},
|
||||
output: true,
|
||||
},
|
||||
"avatar telegram": {
|
||||
input: config.EventAvatarDownload,
|
||||
dest: &bridge.Bridge{Protocol: "telegram"},
|
||||
output: false,
|
||||
},
|
||||
}
|
||||
gw := &Gateway{}
|
||||
for testname, testcase := range eventTests {
|
||||
output := gw.ignoreEvent(testcase.input, testcase.dest)
|
||||
assert.Equalf(t, testcase.output, output, "case '%s' failed", testname)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestExtractNick(t *testing.T) {
|
||||
eventTests := map[string]struct {
|
||||
search string
|
||||
extract string
|
||||
username string
|
||||
text string
|
||||
resultUsername string
|
||||
resultText string
|
||||
}{
|
||||
"test1": {
|
||||
search: "fromgitter",
|
||||
extract: "<(.*?)>\\s+",
|
||||
username: "fromgitter",
|
||||
text: "<userx> blahblah",
|
||||
resultUsername: "userx",
|
||||
resultText: "blahblah",
|
||||
},
|
||||
"test2": {
|
||||
search: "<.*?bot>",
|
||||
//extract: `\((.*?)\)\s+`,
|
||||
extract: "\\((.*?)\\)\\s+",
|
||||
username: "<matterbot>",
|
||||
text: "(userx) blahblah (abc) test",
|
||||
resultUsername: "userx",
|
||||
resultText: "blahblah (abc) test",
|
||||
},
|
||||
}
|
||||
// gw := &Gateway{}
|
||||
for testname, testcase := range eventTests {
|
||||
resultUsername, resultText, _ := extractNick(testcase.search, testcase.extract, testcase.username, testcase.text)
|
||||
assert.Equalf(t, testcase.resultUsername, resultUsername, "case '%s' failed", testname)
|
||||
assert.Equalf(t, testcase.resultText, resultText, "case '%s' failed", testname)
|
||||
}
|
||||
|
||||
}
|
@ -130,6 +130,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -225,6 +236,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#OPTIONAL (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -306,6 +328,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -447,6 +480,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -523,6 +567,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -646,6 +701,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -764,6 +830,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -873,6 +950,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -989,6 +1077,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -1076,6 +1175,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -1157,6 +1267,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
@ -1275,6 +1396,17 @@ ReplaceMessages=[ ["cat","dog"] ]
|
||||
#optional (default empty)
|
||||
ReplaceNicks=[ ["user--","user"] ]
|
||||
|
||||
#Extractnicks is used to for example rewrite messages from other relaybots
|
||||
#See https://github.com/42wim/matterbridge/issues/713 and https://github.com/42wim/matterbridge/issues/466
|
||||
#some examples:
|
||||
#this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
|
||||
#you can use multiple entries for multiplebots
|
||||
#this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
|
||||
#ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
|
||||
#OPTIONAL (default empty)
|
||||
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
|
||||
|
||||
#extra label that can be used in the RemoteNickFormat
|
||||
#optional (default empty)
|
||||
Label=""
|
||||
|
Loading…
Reference in New Issue
Block a user