make banlist consistent, fixes #57

This commit is contained in:
Aine
2023-02-13 22:05:48 +02:00
parent 19e2047a2b
commit 3e0ecc1c02
3 changed files with 17 additions and 18 deletions

View File

@@ -124,7 +124,7 @@ func (b *Bot) IsTrusted(addr net.Addr) bool {
// Ban an address
func (b *Bot) Ban(addr net.Addr) {
if !b.cfg.BanlistEnalbed() {
if !b.cfg.GetBot().BanlistEnabled() {
return
}
if b.IsTrusted(addr) {

View File

@@ -334,6 +334,10 @@ func (b *Bot) runBanlistAdd(ctx context.Context, commandSlice []string) {
b.runBanlist(ctx, commandSlice)
return
}
if !b.cfg.GetBot().BanlistEnabled() {
b.SendNotice(ctx, evt.RoomID, "banlist is disabled, you have to enable it first, kupo")
return
}
banlist := b.cfg.GetBanlist()
ips := commandSlice[1:]
@@ -361,6 +365,10 @@ func (b *Bot) runBanlistRemove(ctx context.Context, commandSlice []string) {
b.runBanlist(ctx, commandSlice)
return
}
if !b.cfg.GetBot().BanlistEnabled() {
b.SendNotice(ctx, evt.RoomID, "banlist is disabled, you have to enable it first, kupo")
return
}
banlist := b.cfg.GetBanlist()
ips := commandSlice[1:]
@@ -384,6 +392,10 @@ func (b *Bot) runBanlistRemove(ctx context.Context, commandSlice []string) {
func (b *Bot) runBanlistReset(ctx context.Context) {
evt := eventFromContext(ctx)
if !b.cfg.GetBot().BanlistEnabled() {
b.SendNotice(ctx, evt.RoomID, "banlist is disabled, you have to enable it first, kupo")
return
}
err := b.cfg.SetBanlist(config.List{})
if err != nil {

View File

@@ -10,8 +10,6 @@ import (
// Manager of configs
type Manager struct {
bl List
ble bool
mu utils.Mutex
log *logger.Logger
lp *linkpearl.Linkpearl
@@ -21,20 +19,13 @@ type Manager struct {
func New(lp *linkpearl.Linkpearl, log *logger.Logger) *Manager {
m := &Manager{
mu: utils.NewMutex(),
bl: make(List, 0),
lp: lp,
log: log,
}
m.ble = m.GetBot().BanlistEnabled()
return m
}
// BanlistEnalbed or not
func (m *Manager) BanlistEnalbed() bool {
return m.ble
}
// GetBot config
func (m *Manager) GetBot() Bot {
var err error
@@ -47,14 +38,12 @@ func (m *Manager) GetBot() Bot {
config = make(Bot, 0)
return config
}
m.ble = config.BanlistEnabled()
return config
}
// SetBot config
func (m *Manager) SetBot(cfg Bot) error {
m.ble = cfg.BanlistEnabled()
return utils.UnwrapError(m.lp.SetAccountData(acBotKey, cfg))
}
@@ -75,8 +64,8 @@ func (m *Manager) SetRoom(roomID id.RoomID, cfg Room) error {
// GetBanlist config
func (m *Manager) GetBanlist() List {
if len(m.bl) > 0 || !m.ble {
return m.bl
if !m.GetBot().BanlistEnabled() {
return make(List, 0)
}
m.mu.Lock("banlist")
@@ -89,22 +78,20 @@ func (m *Manager) GetBanlist() List {
config = make(List, 0)
return config
}
m.bl = config
return config
}
// SetBanlist config
func (m *Manager) SetBanlist(cfg List) error {
if !m.ble {
if !m.GetBot().BanlistEnabled() {
return nil
}
m.mu.Lock("banlist")
defer m.mu.Unlock("banlist")
if cfg == nil {
cfg = make(List, 0)
}
m.bl = cfg
defer m.mu.Unlock("banlist")
return utils.UnwrapError(m.lp.SetAccountData(acBanlistKey, cfg))
}