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

388 lines
9.1 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 (
"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"
"github.com/oragono/oragono/irc/caps"
2017-06-15 18:14:19 +02:00
"sync"
2014-03-06 22:10:54 +01:00
)
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-11-22 10:41:11 +01:00
// ClientManager keeps track of clients by nick, enforcing uniqueness of casefolded nicks
type ClientManager struct {
sync.RWMutex // tier 2
2017-11-22 22:35:38 +01:00
byNick map[string]*Client
bySkeleton map[string]*Client
2014-03-06 22:10:54 +01:00
}
2017-11-22 10:41:11 +01:00
// NewClientManager returns a new ClientManager.
func NewClientManager() *ClientManager {
return &ClientManager{
byNick: make(map[string]*Client),
bySkeleton: make(map[string]*Client),
2014-03-06 22:10:54 +01:00
}
}
2017-11-22 10:41:11 +01:00
// Count returns how many clients are in the manager.
func (clients *ClientManager) Count() int {
clients.RLock()
defer clients.RUnlock()
count := len(clients.byNick)
return count
2016-11-06 02:05:29 +01:00
}
2017-11-22 10:41:11 +01:00
// Get retrieves a client from the manager, if they exist.
func (clients *ClientManager) Get(nick string) *Client {
2016-10-16 12:14:56 +02:00
casefoldedName, err := CasefoldName(nick)
if err == nil {
2017-11-22 10:41:11 +01:00
clients.RLock()
defer clients.RUnlock()
cli := clients.byNick[casefoldedName]
return cli
}
return nil
}
func (clients *ClientManager) removeInternal(client *Client) (err error) {
2017-11-22 22:35:38 +01:00
// requires holding the writable Lock()
oldcfnick, oldskeleton := client.uniqueIdentifiers()
if oldcfnick == "*" || oldcfnick == "" {
return errNickMissing
}
2017-11-22 10:41:11 +01:00
currentEntry, present := clients.byNick[oldcfnick]
if present {
if currentEntry == client {
delete(clients.byNick, oldcfnick)
} else {
// this shouldn't happen, but we can ignore it
client.server.logger.Warning("internal", "clients for nick out of sync", oldcfnick)
err = errNickMissing
2017-11-22 10:41:11 +01:00
}
} else {
err = errNickMissing
}
currentEntry, present = clients.bySkeleton[oldskeleton]
if present {
if currentEntry == client {
delete(clients.bySkeleton, oldskeleton)
} else {
client.server.logger.Warning("internal", "clients for skeleton out of sync", oldskeleton)
err = errNickMissing
}
} else {
err = errNickMissing
2014-03-06 22:10:54 +01:00
}
2017-11-22 10:41:11 +01:00
return
2014-03-06 22:10:54 +01:00
}
2017-03-25 00:19:13 +01:00
// Remove removes a client from the lookup set.
2017-11-22 10:41:11 +01:00
func (clients *ClientManager) Remove(client *Client) error {
clients.Lock()
defer clients.Unlock()
return clients.removeInternal(client)
}
// Resume atomically replaces `oldClient` with `newClient`, updating
// newClient's data to match. It is the caller's responsibility first
// to verify that the resume is allowed, and then later to call oldClient.destroy().
func (clients *ClientManager) Resume(newClient, oldClient *Client) (err error) {
clients.Lock()
defer clients.Unlock()
// atomically grant the new client the old nick
err = clients.removeInternal(oldClient)
if err != nil {
// oldClient no longer owns its nick, fail out
return err
}
// nick has been reclaimed, grant it to the new client
clients.removeInternal(newClient)
oldcfnick, oldskeleton := oldClient.uniqueIdentifiers()
clients.byNick[oldcfnick] = newClient
clients.bySkeleton[oldskeleton] = newClient
newClient.copyResumeData(oldClient)
2014-03-06 22:10:54 +01:00
return nil
}
2017-11-22 10:41:11 +01:00
// SetNick sets a client's nickname, validating it against nicknames in use
func (clients *ClientManager) SetNick(client *Client, newNick string) error {
newcfnick, err := CasefoldName(newNick)
2016-11-16 03:02:22 +01:00
if err != nil {
return err
}
newSkeleton, err := Skeleton(newNick)
if err != nil {
return err
}
2016-11-16 03:02:22 +01:00
reservedAccount, method := client.server.accounts.EnforcementStatus(newcfnick, newSkeleton)
2017-11-22 10:41:11 +01:00
clients.Lock()
defer clients.Unlock()
2016-11-16 03:02:22 +01:00
2017-11-22 10:41:11 +01:00
currentNewEntry := clients.byNick[newcfnick]
// the client may just be changing case
if currentNewEntry != nil && currentNewEntry != client {
2018-02-03 13:03:36 +01:00
return errNicknameInUse
2016-11-16 03:02:22 +01:00
}
// analogous checks for skeletons
skeletonHolder := clients.bySkeleton[newSkeleton]
if skeletonHolder != nil && skeletonHolder != client {
return errNicknameInUse
}
if method == NickReservationStrict && reservedAccount != "" && reservedAccount != client.Account() {
return errNicknameReserved
}
clients.removeInternal(client)
2017-11-22 10:41:11 +01:00
clients.byNick[newcfnick] = client
clients.bySkeleton[newSkeleton] = client
client.updateNick(newNick, newcfnick, newSkeleton)
2017-11-22 10:41:11 +01:00
return nil
}
2016-11-16 03:02:22 +01:00
2017-11-22 10:41:11 +01:00
func (clients *ClientManager) AllClients() (result []*Client) {
clients.RLock()
defer clients.RUnlock()
result = make([]*Client, len(clients.byNick))
i := 0
2017-11-22 22:35:38 +01:00
for _, client := range clients.byNick {
2017-11-22 10:41:11 +01:00
result[i] = client
i++
2016-11-16 03:02:22 +01:00
}
2017-11-22 10:41:11 +01:00
return
2016-11-16 03:02:22 +01:00
}
2017-03-25 00:19:13 +01:00
// AllWithCaps returns all clients with the given capabilities.
2017-11-22 10:41:11 +01:00
func (clients *ClientManager) AllWithCaps(capabs ...caps.Capability) (set ClientSet) {
set = make(ClientSet)
2017-11-22 10:41:11 +01:00
clients.RLock()
defer clients.RUnlock()
var client *Client
2017-11-22 10:41:11 +01:00
for _, client = range clients.byNick {
// make sure they have all the required caps
for _, capab := range capabs {
if !client.capabilities.Has(capab) {
continue
}
}
set.Add(client)
}
return set
}
2017-03-25 00:19:13 +01:00
// FindAll returns all clients that match the given userhost mask.
2017-11-22 10:41:11 +01:00
func (clients *ClientManager) 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-11-22 10:41:11 +01:00
clients.RLock()
defer clients.RUnlock()
for _, client := range clients.byNick {
2018-12-30 12:45:23 +01:00
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.
2017-11-22 10:41:11 +01:00
func (clients *ClientManager) 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-11-22 10:41:11 +01:00
clients.RLock()
defer clients.RUnlock()
for _, client := range clients.byNick {
2018-12-30 12:45:23 +01:00
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?)
2017-10-05 15:29:34 +02:00
// UserMaskSet holds a set of client masks and lets you match hostnames to them.
2014-03-08 02:09:49 +01:00
type UserMaskSet struct {
2017-10-23 01:50:16 +02:00
sync.RWMutex
masks map[string]bool
2014-03-08 02:09:49 +01:00
regexp *regexp.Regexp
}
2017-10-05 15:29:34 +02:00
// NewUserMaskSet returns a new UserMaskSet.
2014-03-08 02:09:49 +01:00
func NewUserMaskSet() *UserMaskSet {
return &UserMaskSet{
masks: make(map[string]bool),
2014-03-08 02:09:49 +01:00
}
}
2017-10-05 15:29:34 +02:00
// Add adds the given mask to this set.
func (set *UserMaskSet) Add(mask string) (added bool) {
casefoldedMask, err := Casefold(mask)
if err != nil {
log.Println(fmt.Sprintf("ERROR: Could not add mask to usermaskset: [%s]", mask))
return false
}
2017-10-23 01:50:16 +02:00
set.Lock()
added = !set.masks[casefoldedMask]
if added {
set.masks[casefoldedMask] = true
}
2017-10-23 01:50:16 +02:00
set.Unlock()
if added {
2017-10-23 01:50:16 +02:00
set.setRegexp()
2014-03-08 02:35:58 +01:00
}
return
2014-03-08 02:09:49 +01:00
}
2017-10-05 15:29:34 +02:00
// AddAll adds the given masks to this set.
func (set *UserMaskSet) AddAll(masks []string) (added bool) {
2017-10-23 01:50:16 +02:00
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
}
2017-10-05 15:29:34 +02:00
// Remove removes the given mask from this set.
func (set *UserMaskSet) Remove(mask string) (removed bool) {
2017-10-23 01:50:16 +02:00
set.Lock()
removed = set.masks[mask]
if removed {
delete(set.masks, mask)
}
2017-10-23 01:50:16 +02:00
set.Unlock()
if removed {
2017-10-23 01:50:16 +02:00
set.setRegexp()
2014-03-08 02:35:58 +01:00
}
return
2014-03-08 02:09:49 +01:00
}
2017-10-05 15:29:34 +02:00
// Match matches the given n!u@h.
func (set *UserMaskSet) Match(userhost string) bool {
2017-10-23 01:50:16 +02:00
set.RLock()
regexp := set.regexp
set.RUnlock()
if regexp == nil {
2014-03-08 02:09:49 +01:00
return false
}
2017-10-23 01:50:16 +02:00
return regexp.MatchString(userhost)
2014-03-08 02:09:49 +01:00
}
2017-10-05 15:29:34 +02:00
// String returns the masks in this set.
2014-03-08 03:14:02 +01:00
func (set *UserMaskSet) String() string {
2017-10-23 01:50:16 +02:00
set.RLock()
2014-03-08 03:14:02 +01:00
masks := make([]string, len(set.masks))
index := 0
for mask := range set.masks {
masks[index] = mask
2017-10-05 15:29:34 +02:00
index++
2014-03-08 03:14:02 +01:00
}
2017-10-23 01:50:16 +02:00
set.RUnlock()
2014-03-08 03:14:02 +01:00
return strings.Join(masks, " ")
}
2017-10-23 01:50:16 +02:00
func (set *UserMaskSet) Length() int {
set.RLock()
defer set.RUnlock()
return len(set.masks)
}
2017-10-05 15:29:34 +02:00
// setRegexp generates 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() {
2017-10-23 01:50:16 +02:00
var re *regexp.Regexp
2014-03-08 02:09:49 +01:00
2017-10-23 01:50:16 +02:00
set.RLock()
2014-03-08 02:09:49 +01:00
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, ".*")
2017-10-23 01:50:16 +02:00
index++
2014-03-08 02:09:49 +01:00
}
2017-10-23 01:50:16 +02:00
set.RUnlock()
if index > 0 {
expr := "^" + strings.Join(maskExprs, "|") + "$"
re, _ = regexp.Compile(expr)
}
set.Lock()
set.regexp = re
set.Unlock()
2014-03-08 02:09:49 +01:00
}