BareRTC/pkg/banned_users.go

74 lines
1.4 KiB
Go
Raw Normal View History

2023-03-14 04:26:37 +00:00
package barertc
2023-08-05 03:31:21 +00:00
import (
"fmt"
"strings"
"sync"
"time"
)
2023-03-14 04:26:37 +00:00
/* Functions to handle banned users */
// Ban is an entry on the ban list.
type Ban struct {
Username string
ExpiresAt time.Time
}
2023-08-05 03:31:21 +00:00
// Global storage for banned users in memory.
var (
banList = map[string]Ban{}
banListMu sync.RWMutex
)
// BanUser adds a user to the ban list.
func BanUser(username string, duration time.Duration) {
banListMu.Lock()
defer banListMu.Unlock()
banList[username] = Ban{
Username: username,
ExpiresAt: time.Now().Add(duration),
}
}
// UnbanUser lifts the ban of a user early.
func UnbanUser(username string) bool {
banListMu.RLock()
defer banListMu.RUnlock()
_, ok := banList[username]
if ok {
delete(banList, username)
}
return ok
}
// StringifyBannedUsers returns a stringified list of all the current banned users.
func StringifyBannedUsers() string {
var lines = []string{}
banListMu.RLock()
defer banListMu.RUnlock()
for username, ban := range banList {
lines = append(lines, fmt.Sprintf(
"* `%s` banned until %s",
username,
ban.ExpiresAt.Format(time.RFC3339),
))
}
return strings.Join(lines, "\n")
}
// IsBanned returns whether the username is currently banned.
func IsBanned(username string) bool {
banListMu.Lock()
defer banListMu.Unlock()
ban, ok := banList[username]
if ok {
// Has the ban expired?
if time.Now().After(ban.ExpiresAt) {
delete(banList, username)
return false
}
}
return ok
}