summaryrefslogtreecommitdiff
path: root/internal/flood/flood.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/flood/flood.go')
-rw-r--r--internal/flood/flood.go30
1 files changed, 28 insertions, 2 deletions
diff --git a/internal/flood/flood.go b/internal/flood/flood.go
index fce81ae..c737e8f 100644
--- a/internal/flood/flood.go
+++ b/internal/flood/flood.go
@@ -5,6 +5,8 @@ import (
"sort"
"strings"
"unicode/utf8"
+
+ lru "github.com/hashicorp/golang-lru"
)
const (
@@ -15,18 +17,42 @@ const (
type Kicker interface {
Kick(nick string, message ...string)
+ Ban(nick string)
}
type Check struct {
- k Kicker
+ k Kicker
+ cache *lru.Cache
}
func New(k Kicker) *Check {
- return &Check{k: k}
+ cache, _ := lru.New(100)
+ return &Check{k: k, cache: cache}
+}
+
+func (c Check) get(nick string) (int, bool) {
+ if n, ok := c.cache.Get(nick); ok {
+ return n.(int), true
+ }
+ return 0, false
+}
+
+func (c Check) del(nick string) {
+ c.cache.Remove(nick)
+}
+
+func (c Check) set(nick string, n int) {
+ c.cache.Add(nick, n)
}
func (c Check) Check(text, nick string) {
if isFlood(text) {
+ count, _ := c.get(nick)
+ c.set(nick, count+1)
+ if count >= 3 {
+ c.k.Ban(nick)
+ c.del(nick)
+ }
c.k.Kick(nick)
}
}