2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-24 14:59:11 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2020-12-21 00:32:39 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
type timerType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
timerTypePermit timerType = iota
|
|
|
|
timerTypeChatMessage
|
|
|
|
timerTypeCooldown
|
|
|
|
)
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
var timerStore = newTimer()
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
type timerEntry struct {
|
|
|
|
kind timerType
|
|
|
|
time time.Time
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
type timer struct {
|
2021-01-24 00:46:40 +00:00
|
|
|
timers map[string]timerEntry
|
2020-12-21 00:32:39 +00:00
|
|
|
lock *sync.RWMutex
|
2021-01-24 00:46:40 +00:00
|
|
|
kind timerType
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTimer() *timer {
|
|
|
|
return &timer{
|
2021-01-24 00:46:40 +00:00
|
|
|
timers: map[string]timerEntry{},
|
2020-12-21 00:32:39 +00:00
|
|
|
lock: new(sync.RWMutex),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
// Cooldown timer
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
func (t *timer) AddCooldown(ruleID string) {
|
|
|
|
t.add(timerTypeCooldown, t.getCooldownTimerKey(ruleID))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
func (t *timer) InCooldown(ruleID string, cooldown time.Duration) bool {
|
|
|
|
return t.has(t.getCooldownTimerKey(ruleID), cooldown)
|
2020-12-24 14:59:11 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
func (t timer) getCooldownTimerKey(ruleID string) string {
|
|
|
|
h := sha256.New()
|
|
|
|
fmt.Fprintf(h, "%d:%s", timerTypeCooldown, ruleID)
|
|
|
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
// Permit timer
|
|
|
|
|
|
|
|
func (t *timer) AddPermit(channel, username string) {
|
|
|
|
t.add(timerTypePermit, t.getPermitTimerKey(channel, username))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
func (t *timer) HasPermit(channel, username string) bool {
|
|
|
|
return t.has(t.getPermitTimerKey(channel, username), config.PermitTimeout)
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2020-12-24 14:59:11 +00:00
|
|
|
|
|
|
|
func (t timer) getPermitTimerKey(channel, username string) string {
|
|
|
|
h := sha256.New()
|
2021-01-24 00:46:40 +00:00
|
|
|
fmt.Fprintf(h, "%d:%s:%s", timerTypePermit, channel, strings.ToLower(strings.TrimLeft(username, "@")))
|
2020-12-24 14:59:11 +00:00
|
|
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
|
|
|
}
|
2021-01-24 00:46:40 +00:00
|
|
|
|
|
|
|
// Generic
|
|
|
|
|
|
|
|
func (t *timer) add(kind timerType, id string) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
t.timers[id] = timerEntry{kind: kind, time: time.Now()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *timer) has(id string, validity time.Duration) bool {
|
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
|
|
|
|
return time.Since(t.timers[id].time) < validity
|
|
|
|
}
|