2021-05-12 16:26:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-06-11 11:52:42 +00:00
|
|
|
registerAction(func() Actor { return &ActorDelay{} })
|
|
|
|
}
|
2021-05-12 16:26:17 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
type ActorDelay struct {
|
|
|
|
Delay time.Duration `json:"delay" yaml:"delay"`
|
|
|
|
DelayJitter time.Duration `json:"delay_jitter" yaml:"delay_jitter"`
|
|
|
|
}
|
2021-05-12 16:26:17 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
func (a ActorDelay) Execute(c *irc.Client, m *irc.Message, r *Rule) error {
|
|
|
|
if a.Delay == 0 && a.DelayJitter == 0 {
|
2021-05-12 16:26:17 +00:00
|
|
|
return nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
totalDelay := a.Delay
|
|
|
|
if a.DelayJitter > 0 {
|
|
|
|
totalDelay += time.Duration(rand.Int63n(int64(a.DelayJitter))) // #nosec: G404 // It's just time, no need for crypto/rand
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(totalDelay)
|
|
|
|
return nil
|
2021-05-12 16:26:17 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
|
|
|
func (a ActorDelay) IsAsync() bool { return false }
|
|
|
|
func (a ActorDelay) Name() string { return "delay" }
|