From ba63d5f8bd1ac7cedad1a47f7fcd570d69b9856c Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 12 May 2021 18:26:17 +0200 Subject: [PATCH] Add delay-action Signed-off-by: Knut Ahlers --- action_delay.go | 24 ++++++++++++++++++++++++ rule.go | 26 +++++++++++++++++--------- wiki/Home.md | 16 ++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 action_delay.go diff --git a/action_delay.go b/action_delay.go new file mode 100644 index 0000000..970da70 --- /dev/null +++ b/action_delay.go @@ -0,0 +1,24 @@ +package main + +import ( + "math/rand" + "time" + + "github.com/go-irc/irc" +) + +func init() { + registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error { + if r.Delay == 0 && r.DelayJitter == 0 { + return nil + } + + totalDelay := r.Delay + if r.DelayJitter > 0 { + totalDelay += time.Duration(rand.Int63n(int64(r.DelayJitter))) + } + + time.Sleep(totalDelay) + return nil + }) +} diff --git a/rule.go b/rule.go index 3d71a20..5564ada 100644 --- a/rule.go +++ b/rule.go @@ -250,13 +250,21 @@ func (r *rule) allowExecuteUserWhitelist(logger *log.Entry, m *irc.Message, even } type ruleAction struct { - Ban *string `json:"ban" yaml:"ban"` - Command []string `json:"command" yaml:"command"` - CounterSet *string `json:"counter_set" yaml:"counter_set"` - CounterStep *int64 `json:"counter_step" yaml:"counter_step"` - Counter *string `json:"counter" yaml:"counter"` - DeleteMessage *bool `json:"delete_message" yaml:"delete_message"` - Respond *string `json:"respond" yaml:"respond"` - RespondFallback *string `json:"respond_fallback" yaml:"respond_fallback"` - Timeout *time.Duration `json:"timeout" yaml:"timeout"` + Ban *string `json:"ban" yaml:"ban"` + + Command []string `json:"command" yaml:"command"` + + CounterSet *string `json:"counter_set" yaml:"counter_set"` + CounterStep *int64 `json:"counter_step" yaml:"counter_step"` + Counter *string `json:"counter" yaml:"counter"` + + Delay time.Duration `json:"delay" yaml:"delay"` + DelayJitter time.Duration `json:"delay_jitter" yaml:"delay_jitter"` + + DeleteMessage *bool `json:"delete_message" yaml:"delete_message"` + + Respond *string `json:"respond" yaml:"respond"` + RespondFallback *string `json:"respond_fallback" yaml:"respond_fallback"` + + Timeout *time.Duration `json:"timeout" yaml:"timeout"` } diff --git a/wiki/Home.md b/wiki/Home.md index 8a71101..9a2dddd 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -40,6 +40,10 @@ rules: # See below for examples # applies templating but MUST result in a parseable integer counter_step: 1 # Integer, can be negative or positive, default: +1 + # Introduce a delay between two actions + - delay: 1m # Duration, how long to wait (fixed) + delay_jitter: 1m # Duration, add random delay to fixed delay between 0 and this value + # Issue a delete on the message caught - delete_message: true # Bool, set to true to delete @@ -193,6 +197,18 @@ The example was dumped using this action: match_message: '^!followage' ``` +### Respond to a message after random delay + +```yaml + - actions: + # Respond after 30-40s + - delay: 30s + delay_jitter: 10s + - respond: 'Hey {{ .username }}' + match_channels: ['#mychannel'] + match_message: '^Hi' +``` + ### Send a notification on successful permit ```yaml