Add Disable and DisableOnTemplate attributes
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
dac0553bdf
commit
9c020099e1
2 changed files with 57 additions and 8 deletions
36
rule.go
36
rule.go
|
@ -25,10 +25,12 @@ type rule struct {
|
|||
|
||||
DisableOnMatchMessages []string `yaml:"disable_on_match_messages"`
|
||||
|
||||
DisableOnOffline bool `yaml:"disable_on_offline"`
|
||||
DisableOnPermit bool `yaml:"disable_on_permit"`
|
||||
DisableOn []string `yaml:"disable_on"`
|
||||
EnableOn []string `yaml:"enable_on"`
|
||||
Disable *bool `yaml:"disable"`
|
||||
DisableOnOffline *bool `yaml:"disable_on_offline"`
|
||||
DisableOnPermit *bool `yaml:"disable_on_permit"`
|
||||
DisableOnTemplate *string `yaml:"disable_on_template"`
|
||||
DisableOn []string `yaml:"disable_on"`
|
||||
EnableOn []string `yaml:"enable_on"`
|
||||
|
||||
matchMessage *regexp.Regexp
|
||||
disableOnMatchMessages []*regexp.Regexp
|
||||
|
@ -60,6 +62,7 @@ func (r *rule) Matches(m *irc.Message, event *string) bool {
|
|||
)
|
||||
|
||||
for _, matcher := range []func(*log.Entry, *irc.Message, *string, badgeCollection) bool{
|
||||
r.allowExecuteDisable,
|
||||
r.allowExecuteChannelWhitelist,
|
||||
r.allowExecuteUserWhitelist,
|
||||
r.allowExecuteEventWhitelist,
|
||||
|
@ -69,6 +72,7 @@ func (r *rule) Matches(m *irc.Message, event *string) bool {
|
|||
r.allowExecuteBadgeWhitelist,
|
||||
r.allowExecuteDisableOnPermit,
|
||||
r.allowExecuteCooldown,
|
||||
r.allowExecuteDisableOnTemplate,
|
||||
r.allowExecuteDisableOnOffline,
|
||||
} {
|
||||
if !matcher(logger, m, event, badges) {
|
||||
|
@ -139,8 +143,12 @@ func (r *rule) allowExecuteCooldown(logger *log.Entry, m *irc.Message, event *st
|
|||
return false
|
||||
}
|
||||
|
||||
func (r *rule) allowExecuteDisable(logger *log.Entry, m *irc.Message, event *string, badges badgeCollection) bool {
|
||||
return r.Disable == nil || !*r.Disable
|
||||
}
|
||||
|
||||
func (r *rule) allowExecuteDisableOnOffline(logger *log.Entry, m *irc.Message, event *string, badges badgeCollection) bool {
|
||||
if !r.DisableOnOffline {
|
||||
if r.DisableOnOffline == nil || !*r.DisableOnOffline {
|
||||
// No match criteria set, does not speak against matching
|
||||
return true
|
||||
}
|
||||
|
@ -159,7 +167,7 @@ func (r *rule) allowExecuteDisableOnOffline(logger *log.Entry, m *irc.Message, e
|
|||
}
|
||||
|
||||
func (r *rule) allowExecuteDisableOnPermit(logger *log.Entry, m *irc.Message, event *string, badges badgeCollection) bool {
|
||||
if r.DisableOnPermit && timerStore.HasPermit(m.Params[0], m.User) {
|
||||
if r.DisableOnPermit == nil || (*r.DisableOnPermit && timerStore.HasPermit(m.Params[0], m.User)) {
|
||||
logger.Trace("Non-Match: Permit")
|
||||
return false
|
||||
}
|
||||
|
@ -167,6 +175,22 @@ func (r *rule) allowExecuteDisableOnPermit(logger *log.Entry, m *irc.Message, ev
|
|||
return true
|
||||
}
|
||||
|
||||
func (r *rule) allowExecuteDisableOnTemplate(logger *log.Entry, m *irc.Message, event *string, badges badgeCollection) bool {
|
||||
if r.DisableOnTemplate == nil {
|
||||
// No match criteria set, does not speak against matching
|
||||
return true
|
||||
}
|
||||
|
||||
res, err := formatMessage(*r.DisableOnTemplate, m, r, nil)
|
||||
if err != nil {
|
||||
logger.WithError(err).Error("Unable to check DisableOnTemplate field")
|
||||
// Caused an error, forbid execution
|
||||
return false
|
||||
}
|
||||
|
||||
return res != "true"
|
||||
}
|
||||
|
||||
func (r *rule) allowExecuteEventWhitelist(logger *log.Entry, m *irc.Message, event *string, badges badgeCollection) bool {
|
||||
if r.MatchEvent == nil {
|
||||
// No match criteria set, does not speak against matching
|
||||
|
|
29
rule_test.go
29
rule_test.go
|
@ -12,6 +12,7 @@ import (
|
|||
var (
|
||||
testLogger = logrus.NewEntry(logrus.StandardLogger())
|
||||
testBadgeLevel0 = func(i int) *int { return &i }(0)
|
||||
testPtrBool = func(b bool) *bool { return &b }
|
||||
)
|
||||
|
||||
func TestAllowExecuteBadgeBlacklist(t *testing.T) {
|
||||
|
@ -76,8 +77,19 @@ func TestAllowExecuteCooldown(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAllowExecuteDisable(t *testing.T) {
|
||||
for exp, r := range map[bool]*rule{
|
||||
true: {Disable: testPtrBool(false)},
|
||||
false: {Disable: testPtrBool(true)},
|
||||
} {
|
||||
if res := r.allowExecuteDisable(testLogger, nil, nil, badgeCollection{}); res != exp {
|
||||
t.Errorf("Disable status %v yield unexpected result: exp=%v res=%v", *r.Disable, exp, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowExecuteDisableOnOffline(t *testing.T) {
|
||||
r := &rule{DisableOnOffline: true}
|
||||
r := &rule{DisableOnOffline: testPtrBool(true)}
|
||||
|
||||
// Fake cache entries to prevent calling the real Twitch API
|
||||
twitch.apiCache.Set([]string{"hasLiveStream", "channel1"}, time.Minute, true)
|
||||
|
@ -94,7 +106,7 @@ func TestAllowExecuteDisableOnOffline(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAllowExecuteDisableOnPermit(t *testing.T) {
|
||||
r := &rule{DisableOnPermit: true}
|
||||
r := &rule{DisableOnPermit: testPtrBool(true)}
|
||||
|
||||
// Permit is using global configuration, so we must fake that one
|
||||
config = &configFile{PermitTimeout: time.Minute}
|
||||
|
@ -111,6 +123,19 @@ func TestAllowExecuteDisableOnPermit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAllowExecuteDisableOnTemplate(t *testing.T) {
|
||||
r := &rule{DisableOnTemplate: func(s string) *string { return &s }(`{{ ne .username "amy" }}`)}
|
||||
|
||||
for msg, exp := range map[string]bool{
|
||||
":amy!amy@foo.example.com PRIVMSG #mychannel :Testing": true,
|
||||
":bob!bob@foo.example.com PRIVMSG #mychannel :Testing": false,
|
||||
} {
|
||||
if res := r.allowExecuteDisableOnTemplate(testLogger, irc.MustParseMessage(msg), nil, badgeCollection{}); exp != res {
|
||||
t.Errorf("Message %q yield unexpected result: exp=%v res=%v", msg, exp, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowExecuteEventWhitelist(t *testing.T) {
|
||||
r := &rule{MatchEvent: func(s string) *string { return &s }("test")}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue