make banlist consistent, fixes #57
This commit is contained in:
@@ -124,7 +124,7 @@ func (b *Bot) IsTrusted(addr net.Addr) bool {
|
|||||||
|
|
||||||
// Ban an address
|
// Ban an address
|
||||||
func (b *Bot) Ban(addr net.Addr) {
|
func (b *Bot) Ban(addr net.Addr) {
|
||||||
if !b.cfg.BanlistEnalbed() {
|
if !b.cfg.GetBot().BanlistEnabled() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b.IsTrusted(addr) {
|
if b.IsTrusted(addr) {
|
||||||
|
|||||||
@@ -334,6 +334,10 @@ func (b *Bot) runBanlistAdd(ctx context.Context, commandSlice []string) {
|
|||||||
b.runBanlist(ctx, commandSlice)
|
b.runBanlist(ctx, commandSlice)
|
||||||
return
|
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()
|
banlist := b.cfg.GetBanlist()
|
||||||
|
|
||||||
ips := commandSlice[1:]
|
ips := commandSlice[1:]
|
||||||
@@ -361,6 +365,10 @@ func (b *Bot) runBanlistRemove(ctx context.Context, commandSlice []string) {
|
|||||||
b.runBanlist(ctx, commandSlice)
|
b.runBanlist(ctx, commandSlice)
|
||||||
return
|
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()
|
banlist := b.cfg.GetBanlist()
|
||||||
|
|
||||||
ips := commandSlice[1:]
|
ips := commandSlice[1:]
|
||||||
@@ -384,6 +392,10 @@ func (b *Bot) runBanlistRemove(ctx context.Context, commandSlice []string) {
|
|||||||
|
|
||||||
func (b *Bot) runBanlistReset(ctx context.Context) {
|
func (b *Bot) runBanlistReset(ctx context.Context) {
|
||||||
evt := eventFromContext(ctx)
|
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{})
|
err := b.cfg.SetBanlist(config.List{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import (
|
|||||||
|
|
||||||
// Manager of configs
|
// Manager of configs
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
bl List
|
|
||||||
ble bool
|
|
||||||
mu utils.Mutex
|
mu utils.Mutex
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
lp *linkpearl.Linkpearl
|
lp *linkpearl.Linkpearl
|
||||||
@@ -21,20 +19,13 @@ type Manager struct {
|
|||||||
func New(lp *linkpearl.Linkpearl, log *logger.Logger) *Manager {
|
func New(lp *linkpearl.Linkpearl, log *logger.Logger) *Manager {
|
||||||
m := &Manager{
|
m := &Manager{
|
||||||
mu: utils.NewMutex(),
|
mu: utils.NewMutex(),
|
||||||
bl: make(List, 0),
|
|
||||||
lp: lp,
|
lp: lp,
|
||||||
log: log,
|
log: log,
|
||||||
}
|
}
|
||||||
m.ble = m.GetBot().BanlistEnabled()
|
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// BanlistEnalbed or not
|
|
||||||
func (m *Manager) BanlistEnalbed() bool {
|
|
||||||
return m.ble
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBot config
|
// GetBot config
|
||||||
func (m *Manager) GetBot() Bot {
|
func (m *Manager) GetBot() Bot {
|
||||||
var err error
|
var err error
|
||||||
@@ -47,14 +38,12 @@ func (m *Manager) GetBot() Bot {
|
|||||||
config = make(Bot, 0)
|
config = make(Bot, 0)
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
m.ble = config.BanlistEnabled()
|
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBot config
|
// SetBot config
|
||||||
func (m *Manager) SetBot(cfg Bot) error {
|
func (m *Manager) SetBot(cfg Bot) error {
|
||||||
m.ble = cfg.BanlistEnabled()
|
|
||||||
return utils.UnwrapError(m.lp.SetAccountData(acBotKey, cfg))
|
return utils.UnwrapError(m.lp.SetAccountData(acBotKey, cfg))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +64,8 @@ func (m *Manager) SetRoom(roomID id.RoomID, cfg Room) error {
|
|||||||
|
|
||||||
// GetBanlist config
|
// GetBanlist config
|
||||||
func (m *Manager) GetBanlist() List {
|
func (m *Manager) GetBanlist() List {
|
||||||
if len(m.bl) > 0 || !m.ble {
|
if !m.GetBot().BanlistEnabled() {
|
||||||
return m.bl
|
return make(List, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.mu.Lock("banlist")
|
m.mu.Lock("banlist")
|
||||||
@@ -89,22 +78,20 @@ func (m *Manager) GetBanlist() List {
|
|||||||
config = make(List, 0)
|
config = make(List, 0)
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
m.bl = config
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBanlist config
|
// SetBanlist config
|
||||||
func (m *Manager) SetBanlist(cfg List) error {
|
func (m *Manager) SetBanlist(cfg List) error {
|
||||||
if !m.ble {
|
if !m.GetBot().BanlistEnabled() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
m.mu.Lock("banlist")
|
m.mu.Lock("banlist")
|
||||||
|
defer m.mu.Unlock("banlist")
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
cfg = make(List, 0)
|
cfg = make(List, 0)
|
||||||
}
|
}
|
||||||
m.bl = cfg
|
|
||||||
defer m.mu.Unlock("banlist")
|
|
||||||
|
|
||||||
return utils.UnwrapError(m.lp.SetAccountData(acBanlistKey, cfg))
|
return utils.UnwrapError(m.lp.SetAccountData(acBanlistKey, cfg))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user