2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// Copyright (c) 2014-2015 Edmund Huber
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-02-09 07:06:10 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-02-18 00:25:32 +01:00
|
|
|
"strings"
|
2017-04-17 13:01:39 +02:00
|
|
|
"sync"
|
2014-02-09 07:06:10 +01:00
|
|
|
)
|
|
|
|
|
2017-04-30 03:12:25 +02:00
|
|
|
// ChannelNameMap is a map that converts channel names to actual channel objects.
|
2017-04-17 13:01:39 +02:00
|
|
|
type ChannelNameMap struct {
|
|
|
|
ChansLock sync.RWMutex
|
|
|
|
Chans map[string]*Channel
|
|
|
|
}
|
|
|
|
|
2017-04-30 03:12:25 +02:00
|
|
|
// NewChannelNameMap returns a new ChannelNameMap.
|
|
|
|
func NewChannelNameMap() *ChannelNameMap {
|
2017-04-17 13:01:39 +02:00
|
|
|
var channels ChannelNameMap
|
|
|
|
channels.Chans = make(map[string]*Channel)
|
2017-04-30 03:12:25 +02:00
|
|
|
return &channels
|
2017-04-17 13:01:39 +02:00
|
|
|
}
|
2014-02-09 08:15:05 +01:00
|
|
|
|
2017-04-30 03:12:25 +02:00
|
|
|
// Get returns the given channel if it exists.
|
|
|
|
func (channels *ChannelNameMap) Get(name string) *Channel {
|
2016-10-11 15:51:46 +02:00
|
|
|
name, err := CasefoldChannel(name)
|
|
|
|
if err == nil {
|
2017-04-17 13:01:39 +02:00
|
|
|
channels.ChansLock.RLock()
|
|
|
|
defer channels.ChansLock.RUnlock()
|
|
|
|
return channels.Chans[name]
|
2016-10-11 15:51:46 +02:00
|
|
|
}
|
|
|
|
return nil
|
2014-02-26 05:17:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-30 03:12:25 +02:00
|
|
|
// Add adds the given channel to our map.
|
|
|
|
func (channels *ChannelNameMap) Add(channel *Channel) error {
|
2017-04-17 13:01:39 +02:00
|
|
|
channels.ChansLock.Lock()
|
|
|
|
defer channels.ChansLock.Unlock()
|
|
|
|
if channels.Chans[channel.nameCasefolded] != nil {
|
2014-02-09 08:15:05 +01:00
|
|
|
return fmt.Errorf("%s: already set", channel.name)
|
|
|
|
}
|
2017-04-17 13:01:39 +02:00
|
|
|
channels.Chans[channel.nameCasefolded] = channel
|
2014-02-09 08:15:05 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-30 03:12:25 +02:00
|
|
|
// Remove removes the given channel from our map.
|
|
|
|
func (channels *ChannelNameMap) Remove(channel *Channel) error {
|
2017-04-17 13:01:39 +02:00
|
|
|
channels.ChansLock.Lock()
|
|
|
|
defer channels.ChansLock.Unlock()
|
|
|
|
if channel != channels.Chans[channel.nameCasefolded] {
|
2014-02-09 08:15:05 +01:00
|
|
|
return fmt.Errorf("%s: mismatch", channel.name)
|
|
|
|
}
|
2017-04-17 13:01:39 +02:00
|
|
|
delete(channels.Chans, channel.nameCasefolded)
|
2014-02-09 08:15:05 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-30 03:12:25 +02:00
|
|
|
// Len returns how many channels we have.
|
|
|
|
func (channels *ChannelNameMap) Len() int {
|
2017-04-17 13:01:39 +02:00
|
|
|
channels.ChansLock.RLock()
|
|
|
|
defer channels.ChansLock.RUnlock()
|
|
|
|
return len(channels.Chans)
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// ModeSet holds a set of modes.
|
2017-03-24 03:23:21 +01:00
|
|
|
type ModeSet map[Mode]bool
|
2014-02-15 06:57:08 +01:00
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// String returns the modes in this set.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (set ModeSet) String() string {
|
2014-02-26 00:57:35 +01:00
|
|
|
if len(set) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2014-02-25 20:11:34 +01:00
|
|
|
strs := make([]string, len(set))
|
|
|
|
index := 0
|
|
|
|
for mode := range set {
|
|
|
|
strs[index] = mode.String()
|
2017-04-30 03:12:25 +02:00
|
|
|
index++
|
2014-02-25 20:11:34 +01:00
|
|
|
}
|
|
|
|
return strings.Join(strs, "")
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// ClientSet is a set of clients.
|
2014-02-17 02:23:47 +01:00
|
|
|
type ClientSet map[*Client]bool
|
2014-02-09 08:15:05 +01:00
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Add adds the given client to this set.
|
2014-02-09 08:15:05 +01:00
|
|
|
func (clients ClientSet) Add(client *Client) {
|
2014-02-17 02:23:47 +01:00
|
|
|
clients[client] = true
|
2014-02-09 08:15:05 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Remove removes the given client from this set.
|
2014-02-09 08:15:05 +01:00
|
|
|
func (clients ClientSet) Remove(client *Client) {
|
|
|
|
delete(clients, client)
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Has returns true if the given client is in this set.
|
2014-02-17 02:23:47 +01:00
|
|
|
func (clients ClientSet) Has(client *Client) bool {
|
|
|
|
return clients[client]
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// MemberSet is a set of members with modes.
|
2017-03-24 03:23:21 +01:00
|
|
|
type MemberSet map[*Client]ModeSet
|
2014-02-17 02:23:47 +01:00
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Add adds the given client to this set.
|
2014-02-17 02:23:47 +01:00
|
|
|
func (members MemberSet) Add(member *Client) {
|
2017-03-24 03:23:21 +01:00
|
|
|
members[member] = make(ModeSet)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Remove removes the given client from this set.
|
2014-02-17 02:23:47 +01:00
|
|
|
func (members MemberSet) Remove(member *Client) {
|
|
|
|
delete(members, member)
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Has returns true if the given client is in this set.
|
2014-02-17 02:23:47 +01:00
|
|
|
func (members MemberSet) Has(member *Client) bool {
|
|
|
|
_, ok := members[member]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// HasMode returns true if the given client is in this set with the given mode.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (members MemberSet) HasMode(member *Client, mode Mode) bool {
|
2014-02-17 02:23:47 +01:00
|
|
|
modes, ok := members[member]
|
2014-02-15 06:57:08 +01:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return modes[mode]
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// AnyHasMode returns true if any of our clients has the given mode.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (members MemberSet) AnyHasMode(mode Mode) bool {
|
2014-03-22 22:25:24 +01:00
|
|
|
for _, modes := range members {
|
2014-03-23 06:47:21 +01:00
|
|
|
if modes[mode] {
|
2014-03-22 22:25:24 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// ChannelSet is a set of channels.
|
2014-02-09 08:15:05 +01:00
|
|
|
type ChannelSet map[*Channel]bool
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Add adds the given channel to this set.
|
2014-02-09 08:15:05 +01:00
|
|
|
func (channels ChannelSet) Add(channel *Channel) {
|
|
|
|
channels[channel] = true
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Remove removes the given channel from this set.
|
2014-02-09 08:15:05 +01:00
|
|
|
func (channels ChannelSet) Remove(channel *Channel) {
|
|
|
|
delete(channels, channel)
|
|
|
|
}
|