5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-25 00:41:37 +00:00

Allow regexs in ignoreNicks. Closes #690 (#720)

This commit is contained in:
Wim 2019-02-17 21:49:28 +01:00 committed by GitHub
parent aeaea0574f
commit 4525fa31aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 45 deletions

View File

@ -237,38 +237,6 @@ func (gw *Gateway) ignoreTextEmpty(msg *config.Message) bool {
return true return true
} }
// ignoreTexts returns true if msg.Text matches any of the input regexes.
func (gw *Gateway) ignoreTexts(msg *config.Message, input []string) bool {
for _, entry := range input {
if entry == "" {
continue
}
// TODO do not compile regexps everytime
re, err := regexp.Compile(entry)
if err != nil {
flog.Errorf("incorrect regexp %s for %s", entry, msg.Account)
continue
}
if re.MatchString(msg.Text) {
flog.Debugf("matching %s. ignoring %s from %s", entry, msg.Text, msg.Account)
return true
}
}
return false
}
// ignoreNicks returns true if msg.Username matches any of the input regexes.
func (gw *Gateway) ignoreNicks(msg *config.Message, input []string) bool {
// is the username in IgnoreNicks field
for _, entry := range input {
if msg.Username == entry {
flog.Debugf("ignoring %s from %s", msg.Username, msg.Account)
return true
}
}
return false
}
func (gw *Gateway) ignoreMessage(msg *config.Message) bool { func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
// if we don't have the bridge, ignore it // if we don't have the bridge, ignore it
if _, ok := gw.Bridges[msg.Account]; !ok { if _, ok := gw.Bridges[msg.Account]; !ok {
@ -277,7 +245,7 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
igNicks := strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreNicks")) igNicks := strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreNicks"))
igMessages := strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreMessages")) igMessages := strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreMessages"))
if gw.ignoreTextEmpty(msg) || gw.ignoreNicks(msg, igNicks) || gw.ignoreTexts(msg, igMessages) { if gw.ignoreTextEmpty(msg) || gw.ignoreText(msg.Username, igNicks) || gw.ignoreText(msg.Text, igMessages) {
return true return true
} }
@ -435,3 +403,23 @@ func getChannelID(msg config.Message) string {
func isAPI(account string) bool { func isAPI(account string) bool {
return strings.HasPrefix(account, "api.") return strings.HasPrefix(account, "api.")
} }
// ignoreText returns true if text matches any of the input regexes.
func (gw *Gateway) ignoreText(text string, input []string) bool {
for _, entry := range input {
if entry == "" {
continue
}
// TODO do not compile regexps everytime
re, err := regexp.Compile(entry)
if err != nil {
flog.Errorf("incorrect regexp %s", entry)
continue
}
if re.MatchString(text) {
flog.Debugf("matching %s. ignoring %s", entry, text)
return true
}
}
return false
}

View File

@ -434,68 +434,68 @@ func TestIgnoreTextEmpty(t *testing.T) {
func TestIgnoreTexts(t *testing.T) { func TestIgnoreTexts(t *testing.T) {
msgTests := map[string]struct { msgTests := map[string]struct {
input *config.Message input string
re []string re []string
output bool output bool
}{ }{
"no regex": { "no regex": {
input: &config.Message{Text: "a text message"}, input: "a text message",
re: []string{}, re: []string{},
output: false, output: false,
}, },
"simple regex": { "simple regex": {
input: &config.Message{Text: "a text message"}, input: "a text message",
re: []string{"text"}, re: []string{"text"},
output: true, output: true,
}, },
"multiple regex fail": { "multiple regex fail": {
input: &config.Message{Text: "a text message"}, input: "a text message",
re: []string{"abc", "123$"}, re: []string{"abc", "123$"},
output: false, output: false,
}, },
"multiple regex pass": { "multiple regex pass": {
input: &config.Message{Text: "a text message"}, input: "a text message",
re: []string{"lala", "sage$"}, re: []string{"lala", "sage$"},
output: true, output: true,
}, },
} }
gw := &Gateway{} gw := &Gateway{}
for testname, testcase := range msgTests { for testname, testcase := range msgTests {
output := gw.ignoreTexts(testcase.input, testcase.re) output := gw.ignoreText(testcase.input, testcase.re)
assert.Equalf(t, testcase.output, output, "case '%s' failed", testname) assert.Equalf(t, testcase.output, output, "case '%s' failed", testname)
} }
} }
func TestIgnoreNicks(t *testing.T) { func TestIgnoreNicks(t *testing.T) {
msgTests := map[string]struct { msgTests := map[string]struct {
input *config.Message input string
re []string re []string
output bool output bool
}{ }{
"no entry": { "no entry": {
input: &config.Message{Username: "user", Text: "a text message"}, input: "user",
re: []string{}, re: []string{},
output: false, output: false,
}, },
"one entry": { "one entry": {
input: &config.Message{Username: "user", Text: "a text message"}, input: "user",
re: []string{"user"}, re: []string{"user"},
output: true, output: true,
}, },
"multiple entries": { "multiple entries": {
input: &config.Message{Username: "user", Text: "a text message"}, input: "user",
re: []string{"abc", "user"}, re: []string{"abc", "user"},
output: true, output: true,
}, },
"multiple entries fail": { "multiple entries fail": {
input: &config.Message{Username: "user", Text: "a text message"}, input: "user",
re: []string{"abc", "def"}, re: []string{"abc", "def"},
output: false, output: false,
}, },
} }
gw := &Gateway{} gw := &Gateway{}
for testname, testcase := range msgTests { for testname, testcase := range msgTests {
output := gw.ignoreNicks(testcase.input, testcase.re) output := gw.ignoreText(testcase.input, testcase.re)
assert.Equalf(t, testcase.output, output, "case '%s' failed", testname) assert.Equalf(t, testcase.output, output, "case '%s' failed", testname)
} }
} }

View File

@ -102,6 +102,7 @@ ColorNicks=false
RunCommands=["PRIVMSG user hello","PRIVMSG chanserv something"] RunCommands=["PRIVMSG user hello","PRIVMSG chanserv something"]
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="ircspammer1 ircspammer2" IgnoreNicks="ircspammer1 ircspammer2"
@ -196,6 +197,7 @@ SkipTLSVerify=true
## Settings below can be reloaded by editing the file ## Settings below can be reloaded by editing the file
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="ircspammer1 ircspammer2" IgnoreNicks="ircspammer1 ircspammer2"
@ -276,6 +278,7 @@ Nick="yourlogin"
## Settings below can be reloaded by editing the file ## Settings below can be reloaded by editing the file
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="spammer1 spammer2" IgnoreNicks="spammer1 spammer2"
@ -416,6 +419,7 @@ EditDisable=false
EditSuffix=" (edited)" EditSuffix=" (edited)"
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="ircspammer1 ircspammer2" IgnoreNicks="ircspammer1 ircspammer2"
@ -491,6 +495,7 @@ Token="Yourtokenhere"
## Settings below can be reloaded by editing the file ## Settings below can be reloaded by editing the file
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="ircspammer1 ircspammer2" IgnoreNicks="ircspammer1 ircspammer2"
@ -609,6 +614,7 @@ EditSuffix=" (edited)"
PrefixMessagesWithNick=false PrefixMessagesWithNick=false
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="ircspammer1 ircspammer2" IgnoreNicks="ircspammer1 ircspammer2"
@ -722,6 +728,7 @@ EditDisable=false
EditSuffix=" (edited)" EditSuffix=" (edited)"
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="ircspammer1 ircspammer2" IgnoreNicks="ircspammer1 ircspammer2"
@ -830,6 +837,7 @@ EditDisable=false
EditSuffix=" (edited)" EditSuffix=" (edited)"
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="spammer1 spammer2" IgnoreNicks="spammer1 spammer2"
@ -945,6 +953,7 @@ SkipTLSVerify=true
PrefixMessagesWithNick=false PrefixMessagesWithNick=false
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="ircspammer1 ircspammer2" IgnoreNicks="ircspammer1 ircspammer2"
@ -1031,6 +1040,7 @@ NoHomeServerSuffix=false
PrefixMessagesWithNick=false PrefixMessagesWithNick=false
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="spammer1 spammer2" IgnoreNicks="spammer1 spammer2"
@ -1111,6 +1121,7 @@ Authcode="ABCE12"
PrefixMessagesWithNick=false PrefixMessagesWithNick=false
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="spammer1 spammer2" IgnoreNicks="spammer1 spammer2"
@ -1191,6 +1202,7 @@ Topic="matterbridge"
## Settings below can be reloaded by editing the file ## Settings below can be reloaded by editing the file
#Nicks you want to ignore. #Nicks you want to ignore.
#Regular expressions supported
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL
IgnoreNicks="spammer1 spammer2" IgnoreNicks="spammer1 spammer2"