2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
2016-10-11 15:51:46 +02:00
|
|
|
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-06 22:10:54 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-10-11 15:51:46 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-03-07 01:51:33 +01:00
|
|
|
"regexp"
|
2014-03-06 22:10:54 +01:00
|
|
|
"strings"
|
2016-09-17 13:23:04 +02:00
|
|
|
|
|
|
|
"github.com/DanielOaks/girc-go/ircmatch"
|
2014-03-06 22:10:54 +01:00
|
|
|
)
|
|
|
|
|
2014-03-07 01:51:33 +01:00
|
|
|
var (
|
|
|
|
ErrNickMissing = errors.New("nick missing")
|
|
|
|
ErrNicknameInUse = errors.New("nickname in use")
|
|
|
|
ErrNicknameMismatch = errors.New("nickname mismatch")
|
|
|
|
)
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func ExpandUserHost(userhost string) (expanded string) {
|
2014-03-07 01:51:33 +01:00
|
|
|
expanded = userhost
|
|
|
|
// fill in missing wildcards for nicks
|
2016-09-17 13:23:04 +02:00
|
|
|
//TODO(dan): this would fail with dan@lol, do we want to accommodate that?
|
2016-10-11 15:51:46 +02:00
|
|
|
if !strings.Contains(expanded, "!") {
|
2014-03-07 01:51:33 +01:00
|
|
|
expanded += "!*"
|
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
if !strings.Contains(expanded, "@") {
|
2014-03-07 01:51:33 +01:00
|
|
|
expanded += "@*"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:10:54 +01:00
|
|
|
type ClientLookupSet struct {
|
2016-10-11 15:51:46 +02:00
|
|
|
byNick map[string]*Client
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientLookupSet() *ClientLookupSet {
|
|
|
|
return &ClientLookupSet{
|
2016-10-11 15:51:46 +02:00
|
|
|
byNick: make(map[string]*Client),
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (clients *ClientLookupSet) Get(nick string) *Client {
|
|
|
|
casefoldedName, err := CasefoldName(nick)
|
|
|
|
if err == nil {
|
|
|
|
return clients.byNick[casefoldedName]
|
|
|
|
}
|
|
|
|
return nil
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clients *ClientLookupSet) Add(client *Client) error {
|
|
|
|
if !client.HasNick() {
|
|
|
|
return ErrNickMissing
|
|
|
|
}
|
|
|
|
if clients.Get(client.nick) != nil {
|
|
|
|
return ErrNicknameInUse
|
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
clients.byNick[client.nickCasefolded] = client
|
2014-03-06 22:10:54 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clients *ClientLookupSet) Remove(client *Client) error {
|
|
|
|
if !client.HasNick() {
|
|
|
|
return ErrNickMissing
|
|
|
|
}
|
|
|
|
if clients.Get(client.nick) != client {
|
|
|
|
return ErrNicknameMismatch
|
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
delete(clients.byNick, client.nickCasefolded)
|
2014-03-06 22:10:54 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) {
|
2014-03-06 22:10:54 +01:00
|
|
|
set = make(ClientSet)
|
2016-09-17 13:23:04 +02:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
userhost, err := Casefold(ExpandUserHost(userhost))
|
|
|
|
if err != nil {
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
matcher := ircmatch.MakeMatch(userhost)
|
2016-09-17 13:23:04 +02:00
|
|
|
|
|
|
|
for _, client := range clients.byNick {
|
2016-10-11 15:51:46 +02:00
|
|
|
if matcher.Match(client.nickMaskCasefolded) {
|
2016-09-17 13:23:04 +02:00
|
|
|
set.Add(client)
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-17 13:23:04 +02:00
|
|
|
|
|
|
|
return set
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (clients *ClientLookupSet) Find(userhost string) *Client {
|
|
|
|
userhost, err := Casefold(ExpandUserHost(userhost))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
matcher := ircmatch.MakeMatch(userhost)
|
2014-03-06 22:10:54 +01:00
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
for _, client := range clients.byNick {
|
2016-10-11 15:51:46 +02:00
|
|
|
if matcher.Match(client.nickMaskCasefolded) {
|
2016-09-17 13:23:04 +02:00
|
|
|
return client
|
2014-03-07 01:51:33 +01:00
|
|
|
}
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
return nil
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
2014-03-08 02:09:49 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// usermask to regexp
|
|
|
|
//
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
//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 {
|
2016-10-11 15:51:46 +02:00
|
|
|
masks map[string]bool
|
2014-03-08 02:09:49 +01:00
|
|
|
regexp *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMaskSet() *UserMaskSet {
|
|
|
|
return &UserMaskSet{
|
2016-10-11 15:51:46 +02:00
|
|
|
masks: make(map[string]bool),
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02: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
|
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (set *UserMaskSet) AddAll(masks []string) (added bool) {
|
2014-03-09 04:22:04 +01:00
|
|
|
for _, mask := range masks {
|
|
|
|
if !added && !set.masks[mask] {
|
|
|
|
added = true
|
|
|
|
}
|
|
|
|
set.masks[mask] = true
|
|
|
|
}
|
|
|
|
set.setRegexp()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (set *UserMaskSet) Match(userhost string) bool {
|
2014-03-08 02:09:49 +01:00
|
|
|
if set.regexp == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
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 {
|
2016-10-11 15:51:46 +02:00
|
|
|
masks[index] = mask
|
2014-03-08 03:14:02 +01:00
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
return strings.Join(masks, " ")
|
|
|
|
}
|
|
|
|
|
2014-03-09 04:22:04 +01:00
|
|
|
// 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 {
|
2016-10-11 15:51:46 +02:00
|
|
|
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)
|
|
|
|
}
|