mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-20 02:00:11 -08:00
fix #644
This commit is contained in:
parent
cddc51d6cf
commit
332f0d8d91
9 changed files with 166 additions and 98 deletions
|
|
@ -5,10 +5,9 @@
|
|||
package irc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/goshuirc/irc-go/ircmatch"
|
||||
|
||||
|
|
@ -251,32 +250,42 @@ func (clients *ClientManager) FindAll(userhost string) (set ClientSet) {
|
|||
//TODO(dan): move this over to generally using glob syntax instead?
|
||||
// kinda more expected in normal ban/etc masks, though regex is useful (probably as an extban?)
|
||||
|
||||
type MaskInfo struct {
|
||||
TimeCreated time.Time
|
||||
CreatorNickmask string
|
||||
CreatorAccount string
|
||||
}
|
||||
|
||||
// UserMaskSet holds a set of client masks and lets you match hostnames to them.
|
||||
type UserMaskSet struct {
|
||||
sync.RWMutex
|
||||
masks map[string]bool
|
||||
masks map[string]MaskInfo
|
||||
regexp *regexp.Regexp
|
||||
}
|
||||
|
||||
// NewUserMaskSet returns a new UserMaskSet.
|
||||
func NewUserMaskSet() *UserMaskSet {
|
||||
return &UserMaskSet{
|
||||
masks: make(map[string]bool),
|
||||
}
|
||||
return new(UserMaskSet)
|
||||
}
|
||||
|
||||
// Add adds the given mask to this set.
|
||||
func (set *UserMaskSet) Add(mask string) (added bool) {
|
||||
casefoldedMask, err := Casefold(mask)
|
||||
func (set *UserMaskSet) Add(mask, creatorNickmask, creatorAccount string) (added bool) {
|
||||
casefoldedMask, err := CanonicalizeMaskWildcard(mask)
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("ERROR: Could not add mask to usermaskset: [%s]", mask))
|
||||
return false
|
||||
}
|
||||
|
||||
set.Lock()
|
||||
added = !set.masks[casefoldedMask]
|
||||
if set.masks == nil {
|
||||
set.masks = make(map[string]MaskInfo)
|
||||
}
|
||||
_, present := set.masks[casefoldedMask]
|
||||
added = !present
|
||||
if added {
|
||||
set.masks[casefoldedMask] = true
|
||||
set.masks[casefoldedMask] = MaskInfo{
|
||||
TimeCreated: time.Now().UTC(),
|
||||
CreatorNickmask: creatorNickmask,
|
||||
CreatorAccount: creatorAccount,
|
||||
}
|
||||
}
|
||||
set.Unlock()
|
||||
|
||||
|
|
@ -286,27 +295,15 @@ func (set *UserMaskSet) Add(mask string) (added bool) {
|
|||
return
|
||||
}
|
||||
|
||||
// AddAll adds the given masks to this set.
|
||||
func (set *UserMaskSet) AddAll(masks []string) (added bool) {
|
||||
set.Lock()
|
||||
defer set.Unlock()
|
||||
|
||||
for _, mask := range masks {
|
||||
if !added && !set.masks[mask] {
|
||||
added = true
|
||||
}
|
||||
set.masks[mask] = true
|
||||
}
|
||||
if added {
|
||||
set.setRegexp()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Remove removes the given mask from this set.
|
||||
func (set *UserMaskSet) Remove(mask string) (removed bool) {
|
||||
mask, err := CanonicalizeMaskWildcard(mask)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
set.Lock()
|
||||
removed = set.masks[mask]
|
||||
_, removed = set.masks[mask]
|
||||
if removed {
|
||||
delete(set.masks, mask)
|
||||
}
|
||||
|
|
@ -318,6 +315,24 @@ func (set *UserMaskSet) Remove(mask string) (removed bool) {
|
|||
return
|
||||
}
|
||||
|
||||
func (set *UserMaskSet) SetMasks(masks map[string]MaskInfo) {
|
||||
set.Lock()
|
||||
set.masks = masks
|
||||
set.Unlock()
|
||||
set.setRegexp()
|
||||
}
|
||||
|
||||
func (set *UserMaskSet) Masks() (result map[string]MaskInfo) {
|
||||
set.RLock()
|
||||
defer set.RUnlock()
|
||||
|
||||
result = make(map[string]MaskInfo, len(set.masks))
|
||||
for mask, info := range set.masks {
|
||||
result[mask] = info
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Match matches the given n!u@h.
|
||||
func (set *UserMaskSet) Match(userhost string) bool {
|
||||
set.RLock()
|
||||
|
|
@ -330,19 +345,6 @@ func (set *UserMaskSet) Match(userhost string) bool {
|
|||
return regexp.MatchString(userhost)
|
||||
}
|
||||
|
||||
// String returns the masks in this set.
|
||||
func (set *UserMaskSet) String() string {
|
||||
set.RLock()
|
||||
masks := make([]string, len(set.masks))
|
||||
index := 0
|
||||
for mask := range set.masks {
|
||||
masks[index] = mask
|
||||
index++
|
||||
}
|
||||
set.RUnlock()
|
||||
return strings.Join(masks, " ")
|
||||
}
|
||||
|
||||
func (set *UserMaskSet) Length() int {
|
||||
set.RLock()
|
||||
defer set.RUnlock()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue