[core] Fix: Message matcher matched also event content

which lead to weird behavior and generic message matchers responding to
CLEARMSG or JOIN events

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-04-08 01:10:49 +02:00
parent e80ed0dbf3
commit 3dc60b62b8
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 41 additions and 9 deletions

View file

@ -99,7 +99,7 @@ func (r *Rule) Matches(m *irc.Message, event *string, timerStore TimerStore, msg
r.allowExecuteDisable, r.allowExecuteDisable,
r.allowExecuteChannelWhitelist, r.allowExecuteChannelWhitelist,
r.allowExecuteUserWhitelist, r.allowExecuteUserWhitelist,
r.allowExecuteEventWhitelist, r.allowExecuteEventMatch,
r.allowExecuteMessageMatcherWhitelist, r.allowExecuteMessageMatcherWhitelist,
r.allowExecuteMessageMatcherBlacklist, r.allowExecuteMessageMatcherBlacklist,
r.allowExecuteBadgeBlacklist, r.allowExecuteBadgeBlacklist,
@ -358,20 +358,52 @@ func (r *Rule) allowExecuteDisableOnTemplate(logger *log.Entry, m *irc.Message,
return true return true
} }
func (r *Rule) allowExecuteEventWhitelist(logger *log.Entry, _ *irc.Message, event *string, _ twitch.BadgeCollection, _ *FieldCollection) bool { func (r *Rule) allowExecuteEventMatch(logger *log.Entry, _ *irc.Message, event *string, _ twitch.BadgeCollection, _ *FieldCollection) bool {
if r.MatchEvent == nil || *r.MatchEvent == "" { // The user defines either no event to match or they define an
// No match criteria set, does not speak against matching // event to match. We now need to ensure this match is valid for
// the current execution:
//
// - If the user gave no event we MUST NOT have an event defined
// - If the user gave an event we MUST have the same event defined
//
// To aid fighting spam we do define some excemption from these
// rules:
//
// - Bits are sent using IRC messages and might contains spam
// therefore we additionally match them through a message
// matcher
// - Resubs are also IRC messages and might be abused to spam
// though this is quite unlikely. Even though it's unlikely
// we also allow a match for message matchers to aid mods
//
// As all set events are always pointer to non-empty strings we
// assume an empty string in case either is not set and then
// compare the string contents.
var mE, gE string
if r.MatchEvent != nil {
mE = *r.MatchEvent
}
if event != nil {
gE = *event
}
if mE == gE {
// Event does exactly match
return true
}
if mE == "" && str.StringInSlice(gE, []string{"bits", "resub"}) {
// Additional message matchers - see explanation above
return true return true
} }
if event == nil || *r.MatchEvent != *event {
logger.Trace("Non-Match: Event") logger.Trace("Non-Match: Event")
return false return false
} }
return true
}
func (r *Rule) allowExecuteMessageMatcherBlacklist(logger *log.Entry, m *irc.Message, _ *string, _ twitch.BadgeCollection, _ *FieldCollection) bool { func (r *Rule) allowExecuteMessageMatcherBlacklist(logger *log.Entry, m *irc.Message, _ *string, _ twitch.BadgeCollection, _ *FieldCollection) bool {
if len(r.DisableOnMatchMessages) == 0 { if len(r.DisableOnMatchMessages) == 0 {
// No match criteria set, does not speak against matching // No match criteria set, does not speak against matching

View file

@ -157,7 +157,7 @@ func TestAllowExecuteEventWhitelist(t *testing.T) {
"foobar": false, "foobar": false,
"test": true, "test": true,
} { } {
if res := r.allowExecuteEventWhitelist(testLogger, nil, &evt, twitch.BadgeCollection{}, nil); exp != res { if res := r.allowExecuteEventMatch(testLogger, nil, &evt, twitch.BadgeCollection{}, nil); exp != res {
t.Errorf("Event %q yield unexpected result: exp=%v res=%v", evt, exp, res) t.Errorf("Event %q yield unexpected result: exp=%v res=%v", evt, exp, res)
} }
} }