2022-09-27 15:06:18 +00:00
|
|
|
package ban
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jaredfolkins/badactor"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
)
|
|
|
|
|
2022-12-07 13:09:36 +00:00
|
|
|
type ActionCallback func(action Action, actor string, rule *Rule) error
|
|
|
|
|
|
|
|
type Action int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Jailed Action = iota
|
|
|
|
Released
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a Action) String() string {
|
|
|
|
switch a {
|
|
|
|
case Jailed:
|
|
|
|
return "Jailed"
|
|
|
|
case Released:
|
|
|
|
return "Released"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 15:06:18 +00:00
|
|
|
type Rule struct {
|
2022-12-07 13:09:36 +00:00
|
|
|
Name string
|
|
|
|
Message string
|
|
|
|
Code codes.Code
|
|
|
|
StrikeLimit int
|
|
|
|
JailDuration time.Duration
|
|
|
|
// Callback is an optional function to call when an Actor isJailed or released because of timeServed
|
|
|
|
Callback ActionCallback
|
2022-09-27 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type action struct {
|
2022-12-07 13:09:36 +00:00
|
|
|
fn ActionCallback
|
2022-09-27 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a2 *action) WhenJailed(a *badactor.Actor, r *badactor.Rule) error {
|
2022-12-07 13:09:36 +00:00
|
|
|
if a2.fn == nil {
|
|
|
|
return nil
|
2022-09-27 15:06:18 +00:00
|
|
|
}
|
2022-12-07 13:09:36 +00:00
|
|
|
return a2.fn(Jailed, a.Name(), &Rule{
|
|
|
|
Name: r.Name,
|
|
|
|
Message: r.Message,
|
|
|
|
StrikeLimit: r.StrikeLimit,
|
|
|
|
JailDuration: r.ExpireBase,
|
|
|
|
})
|
2022-09-27 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a2 *action) WhenTimeServed(a *badactor.Actor, r *badactor.Rule) error {
|
2022-12-07 13:09:36 +00:00
|
|
|
if a2.fn == nil {
|
|
|
|
return nil
|
2022-09-27 15:06:18 +00:00
|
|
|
}
|
2022-12-07 13:09:36 +00:00
|
|
|
return a2.fn(Released, a.Name(), &Rule{
|
|
|
|
Name: r.Name,
|
|
|
|
Message: r.Message,
|
|
|
|
StrikeLimit: r.StrikeLimit,
|
|
|
|
JailDuration: r.ExpireBase,
|
|
|
|
})
|
2022-09-27 15:06:18 +00:00
|
|
|
}
|