optimize ban checks
This commit is contained in:
@@ -97,10 +97,7 @@ func (b *Bot) IsGreylisted(addr net.Addr) bool {
|
|||||||
|
|
||||||
// IsBanned checks if address is banned
|
// IsBanned checks if address is banned
|
||||||
func (b *Bot) IsBanned(addr net.Addr) bool {
|
func (b *Bot) IsBanned(addr net.Addr) bool {
|
||||||
if !b.getBotSettings().BanlistEnabled() {
|
return b.banlist.Has(addr)
|
||||||
return false
|
|
||||||
}
|
|
||||||
return b.getBanlist().Has(addr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ban an address
|
// Ban an address
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type Bot struct {
|
|||||||
allowedUsers []*regexp.Regexp
|
allowedUsers []*regexp.Regexp
|
||||||
allowedAdmins []*regexp.Regexp
|
allowedAdmins []*regexp.Regexp
|
||||||
commands commandList
|
commands commandList
|
||||||
|
banlist bglist
|
||||||
rooms sync.Map
|
rooms sync.Map
|
||||||
sendmail func(string, string, string) error
|
sendmail func(string, string, string) error
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
@@ -102,6 +103,7 @@ func (b *Bot) Start(statusMsg string) error {
|
|||||||
if err := b.syncRooms(); err != nil {
|
if err := b.syncRooms(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
b.syncBanlist()
|
||||||
|
|
||||||
b.initSync()
|
b.initSync()
|
||||||
b.log.Info("Postmoogle has been started")
|
b.log.Info("Postmoogle has been started")
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ func (b *Bot) runBanlist(ctx context.Context, commandSlice []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
b.Error(ctx, evt.RoomID, "cannot set bot config: %v", err)
|
b.Error(ctx, evt.RoomID, "cannot set bot config: %v", err)
|
||||||
}
|
}
|
||||||
|
b.syncBanlist()
|
||||||
b.SendNotice(ctx, evt.RoomID, "banlist has been updated")
|
b.SendNotice(ctx, evt.RoomID, "banlist has been updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
bot/data.go
11
bot/data.go
@@ -50,3 +50,14 @@ func (b *Bot) syncRooms() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) syncBanlist() {
|
||||||
|
b.lock("banlist")
|
||||||
|
defer b.unlock("banlist")
|
||||||
|
|
||||||
|
if !b.getBotSettings().BanlistEnabled() {
|
||||||
|
b.banlist = make(bglist, 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.banlist = b.getBanlist()
|
||||||
|
}
|
||||||
|
|||||||
@@ -82,13 +82,20 @@ func (b *Bot) getBanlist() bglist {
|
|||||||
b.log.Error("cannot get banlist: %v", utils.UnwrapError(err))
|
b.log.Error("cannot get banlist: %v", utils.UnwrapError(err))
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
config = map[string]string{}
|
config = make(bglist, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) setBanlist(cfg bglist) error {
|
func (b *Bot) setBanlist(cfg bglist) error {
|
||||||
|
b.lock("banlist")
|
||||||
|
if cfg == nil {
|
||||||
|
cfg = make(bglist, 0)
|
||||||
|
}
|
||||||
|
b.banlist = cfg
|
||||||
|
defer b.unlock("banlist")
|
||||||
|
|
||||||
return utils.UnwrapError(b.lp.SetAccountData(acBanlistKey, cfg))
|
return utils.UnwrapError(b.lp.SetAccountData(acBanlistKey, cfg))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +105,7 @@ func (b *Bot) getGreylist() bglist {
|
|||||||
b.log.Error("cannot get banlist: %v", utils.UnwrapError(err))
|
b.log.Error("cannot get banlist: %v", utils.UnwrapError(err))
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
config = map[string]string{}
|
config = make(bglist, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|||||||
Reference in New Issue
Block a user