3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 05:19:25 +01:00
ergo/irc/client_lookup_set.go

318 lines
7.6 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
2014-03-06 22:10:54 +01:00
package irc
import (
"errors"
"fmt"
"log"
"regexp"
2014-03-06 22:10:54 +01:00
"strings"
2017-06-15 18:14:19 +02:00
"github.com/goshuirc/irc-go/ircmatch"
2017-06-15 18:14:19 +02:00
"sync"
2014-03-06 22:10:54 +01:00
)
var (
ErrNickMissing = errors.New("nick missing")
ErrNicknameInUse = errors.New("nickname in use")
ErrNicknameMismatch = errors.New("nickname mismatch")
)
2017-03-25 00:19:13 +01:00
// ExpandUserHost takes a userhost, and returns an expanded version.
func ExpandUserHost(userhost string) (expanded string) {
expanded = userhost
// fill in missing wildcards for nicks
2017-03-25 00:19:13 +01:00
//TODO(dan): this would fail with dan@lol, fix that.
if !strings.Contains(expanded, "!") {
expanded += "!*"
}
if !strings.Contains(expanded, "@") {
expanded += "@*"
}
return
}
2017-03-25 00:19:13 +01:00
// ClientLookupSet represents a way to store, search and lookup clients.
2014-03-06 22:10:54 +01:00
type ClientLookupSet struct {
2017-01-10 16:06:02 +01:00
ByNickMutex sync.RWMutex
ByNick map[string]*Client
2014-03-06 22:10:54 +01:00
}
2017-03-25 00:19:13 +01:00
// NewClientLookupSet returns a new lookup set.
2014-03-06 22:10:54 +01:00
func NewClientLookupSet() *ClientLookupSet {
return &ClientLookupSet{
ByNick: make(map[string]*Client),
2014-03-06 22:10:54 +01:00
}
}
2017-03-25 00:19:13 +01:00
// Count returns how many clients are in the lookup set.
2016-11-06 02:05:29 +01:00
func (clients *ClientLookupSet) Count() int {
2017-01-10 16:06:02 +01:00
clients.ByNickMutex.RLock()
defer clients.ByNickMutex.RUnlock()
count := len(clients.ByNick)
return count
2016-11-06 02:05:29 +01:00
}
2017-03-25 00:19:13 +01:00
// Has returns whether or not the given client exists.
//TODO(dan): This seems like ripe ground for a race, if code does Has then Get, and assumes the Get will return a client.
2016-10-16 12:14:56 +02:00
func (clients *ClientLookupSet) Has(nick string) bool {
casefoldedName, err := CasefoldName(nick)
if err == nil {
return false
}
2017-01-10 16:06:02 +01:00
clients.ByNickMutex.RLock()
defer clients.ByNickMutex.RUnlock()
_, exists := clients.ByNick[casefoldedName]
2016-10-16 12:14:56 +02:00
return exists
}
2016-11-16 03:02:22 +01:00
// getNoMutex is used internally, for getting clients when no mutex is required (i.e. is already set).
func (clients *ClientLookupSet) getNoMutex(nick string) *Client {
casefoldedName, err := CasefoldName(nick)
if err == nil {
cli := clients.ByNick[casefoldedName]
return cli
}
return nil
}
2017-03-25 00:19:13 +01:00
// Get retrieves a client from the set, if they exist.
func (clients *ClientLookupSet) Get(nick string) *Client {
casefoldedName, err := CasefoldName(nick)
if err == nil {
2017-01-10 16:06:02 +01:00
clients.ByNickMutex.RLock()
defer clients.ByNickMutex.RUnlock()
cli := clients.ByNick[casefoldedName]
return cli
}
return nil
2014-03-06 22:10:54 +01:00
}
2017-03-25 00:19:13 +01:00
// Add adds a client to the lookup set.
func (clients *ClientLookupSet) Add(client *Client, nick string) error {
nick, err := CasefoldName(nick)
if err != nil {
return err
2014-03-06 22:10:54 +01:00
}
clients.ByNickMutex.Lock()
2016-11-16 03:02:22 +01:00
defer clients.ByNickMutex.Unlock()
if clients.getNoMutex(nick) != nil {
2014-03-06 22:10:54 +01:00
return ErrNicknameInUse
}
clients.ByNick[nick] = client
2014-03-06 22:10:54 +01:00
return nil
}
2017-03-25 00:19:13 +01:00
// Remove removes a client from the lookup set.
2014-03-06 22:10:54 +01:00
func (clients *ClientLookupSet) Remove(client *Client) error {
if !client.HasNick() {
return ErrNickMissing
}
2016-11-16 03:02:22 +01:00
clients.ByNickMutex.Lock()
defer clients.ByNickMutex.Unlock()
if clients.getNoMutex(client.nick) != client {
2014-03-06 22:10:54 +01:00
return ErrNicknameMismatch
}
delete(clients.ByNick, client.nickCasefolded)
2014-03-06 22:10:54 +01:00
return nil
}
2017-03-25 00:19:13 +01:00
// Replace renames an existing client in the lookup set.
2016-11-16 03:02:22 +01:00
func (clients *ClientLookupSet) Replace(oldNick, newNick string, client *Client) error {
// get casefolded nicknames
oldNick, err := CasefoldName(oldNick)
if err != nil {
return err
}
newNick, err = CasefoldName(newNick)
if err != nil {
return err
}
// remove and replace
clients.ByNickMutex.Lock()
defer clients.ByNickMutex.Unlock()
oldClient := clients.ByNick[newNick]
if oldClient == nil || oldClient == client {
// whoo
} else {
2016-11-16 03:02:22 +01:00
return ErrNicknameInUse
}
if oldNick == newNick {
// if they're only changing case, don't need to remove+re-add them
return nil
}
delete(clients.ByNick, oldNick)
clients.ByNick[newNick] = client
return nil
}
2017-03-25 00:19:13 +01:00
// AllWithCaps returns all clients with the given capabilities.
func (clients *ClientLookupSet) AllWithCaps(caps ...Capability) (set ClientSet) {
set = make(ClientSet)
2017-01-10 16:06:02 +01:00
clients.ByNickMutex.RLock()
defer clients.ByNickMutex.RUnlock()
var client *Client
for _, client = range clients.ByNick {
// make sure they have all the required caps
for _, Cap := range caps {
if !client.capabilities[Cap] {
continue
}
}
set.Add(client)
}
return set
}
2017-03-25 00:19:13 +01:00
// FindAll returns all clients that match the given userhost mask.
func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) {
2014-03-06 22:10:54 +01:00
set = make(ClientSet)
userhost, err := Casefold(ExpandUserHost(userhost))
if err != nil {
return set
}
matcher := ircmatch.MakeMatch(userhost)
2017-01-10 16:06:02 +01:00
clients.ByNickMutex.RLock()
defer clients.ByNickMutex.RUnlock()
for _, client := range clients.ByNick {
if matcher.Match(client.nickMaskCasefolded) {
set.Add(client)
2014-03-06 22:10:54 +01:00
}
}
return set
2014-03-06 22:10:54 +01:00
}
2017-03-25 00:19:13 +01:00
// Find returns the first client that matches the given userhost mask.
func (clients *ClientLookupSet) Find(userhost string) *Client {
userhost, err := Casefold(ExpandUserHost(userhost))
if err != nil {
return nil
}
matcher := ircmatch.MakeMatch(userhost)
var matchedClient *Client
2014-03-06 22:10:54 +01:00
2017-01-10 16:06:02 +01:00
clients.ByNickMutex.RLock()
defer clients.ByNickMutex.RUnlock()
for _, client := range clients.ByNick {
if matcher.Match(client.nickMaskCasefolded) {
matchedClient = client
break
}
2014-03-06 22:10:54 +01:00
}
return matchedClient
2014-03-06 22:10:54 +01:00
}
2014-03-08 02:09:49 +01:00
//
// usermask to regexp
//
//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?)
2014-03-08 02:09:49 +01:00
type UserMaskSet struct {
masks map[string]bool
2014-03-08 02:09:49 +01:00
regexp *regexp.Regexp
}
func NewUserMaskSet() *UserMaskSet {
return &UserMaskSet{
masks: make(map[string]bool),
2014-03-08 02:09:49 +01:00
}
}
func (set *UserMaskSet) Add(mask string) bool {
casefoldedMask, err := Casefold(mask)
if err != nil {
log.Println(fmt.Sprintf("ERROR: Could not add mask to usermaskset: [%s]", mask))
return false
}
if set.masks[casefoldedMask] {
2014-03-08 02:35:58 +01:00
return false
}
set.masks[casefoldedMask] = true
2014-03-08 02:09:49 +01:00
set.setRegexp()
2014-03-08 02:35:58 +01:00
return true
2014-03-08 02:09:49 +01:00
}
func (set *UserMaskSet) AddAll(masks []string) (added bool) {
for _, mask := range masks {
if !added && !set.masks[mask] {
added = true
}
set.masks[mask] = true
}
set.setRegexp()
return
}
func (set *UserMaskSet) Remove(mask string) bool {
2014-03-08 02:35:58 +01:00
if !set.masks[mask] {
return false
}
2014-03-08 02:09:49 +01:00
delete(set.masks, mask)
set.setRegexp()
2014-03-08 02:35:58 +01:00
return true
2014-03-08 02:09:49 +01:00
}
func (set *UserMaskSet) Match(userhost string) bool {
2014-03-08 02:09:49 +01:00
if set.regexp == nil {
return false
}
return set.regexp.MatchString(userhost)
2014-03-08 02:09:49 +01:00
}
2014-03-08 03:14:02 +01:00
func (set *UserMaskSet) String() string {
masks := make([]string, len(set.masks))
index := 0
for mask := range set.masks {
masks[index] = mask
2014-03-08 03:14:02 +01:00
index += 1
}
return strings.Join(masks, " ")
}
// Generate a regular expression from the set of user mask
// strings. Masks are split at the two types of wildcards, `*` and
// `?`. All the pieces are meta-escaped. `*` is replaced with `.*`,
// the regexp equivalent. Likewise, `?` is replaced with `.`. The
// parts are re-joined and finally all masks are joined into a big
// or-expression.
2014-03-08 02:09:49 +01:00
func (set *UserMaskSet) setRegexp() {
if len(set.masks) == 0 {
set.regexp = nil
return
}
maskExprs := make([]string, len(set.masks))
index := 0
for mask := range set.masks {
manyParts := strings.Split(mask, "*")
2014-03-08 02:09:49 +01:00
manyExprs := make([]string, len(manyParts))
for mindex, manyPart := range manyParts {
oneParts := strings.Split(manyPart, "?")
oneExprs := make([]string, len(oneParts))
for oindex, onePart := range oneParts {
oneExprs[oindex] = regexp.QuoteMeta(onePart)
}
manyExprs[mindex] = strings.Join(oneExprs, ".")
}
maskExprs[index] = strings.Join(manyExprs, ".*")
}
expr := "^" + strings.Join(maskExprs, "|") + "$"
set.regexp, _ = regexp.Compile(expr)
}