2014-02-09 07:06:10 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-02-18 00:25:32 +01:00
|
|
|
"strings"
|
2014-02-09 07:06:10 +01:00
|
|
|
)
|
|
|
|
|
2014-02-09 07:42:14 +01:00
|
|
|
//
|
2014-02-09 07:06:10 +01:00
|
|
|
// simple types
|
2014-02-09 07:42:14 +01:00
|
|
|
//
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
type ChannelNameMap map[Name]*Channel
|
2014-02-09 08:15:05 +01:00
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (channels ChannelNameMap) Get(name Name) *Channel {
|
|
|
|
return channels[name.ToLower()]
|
2014-02-26 05:17:26 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
func (channels ChannelNameMap) Add(channel *Channel) error {
|
2014-03-09 21:45:36 +01:00
|
|
|
if channels[channel.name.ToLower()] != nil {
|
2014-02-09 08:15:05 +01:00
|
|
|
return fmt.Errorf("%s: already set", channel.name)
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
channels[channel.name.ToLower()] = channel
|
2014-02-09 08:15:05 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channels ChannelNameMap) Remove(channel *Channel) error {
|
2014-03-09 21:45:36 +01:00
|
|
|
if channel != channels[channel.name.ToLower()] {
|
2014-02-09 08:15:05 +01:00
|
|
|
return fmt.Errorf("%s: mismatch", channel.name)
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
delete(channels, channel.name.ToLower())
|
2014-02-09 08:15:05 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
type ChannelModeSet map[ChannelMode]bool
|
2014-02-15 06:57:08 +01:00
|
|
|
|
2014-02-25 20:11:34 +01:00
|
|
|
func (set ChannelModeSet) 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()
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
return strings.Join(strs, "")
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:23:47 +01:00
|
|
|
type ClientSet map[*Client]bool
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (clients ClientSet) Remove(client *Client) {
|
|
|
|
delete(clients, client)
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:23:47 +01:00
|
|
|
func (clients ClientSet) Has(client *Client) bool {
|
|
|
|
return clients[client]
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemberSet map[*Client]ChannelModeSet
|
|
|
|
|
|
|
|
func (members MemberSet) Add(member *Client) {
|
|
|
|
members[member] = make(ChannelModeSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (members MemberSet) Remove(member *Client) {
|
|
|
|
delete(members, member)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (members MemberSet) Has(member *Client) bool {
|
|
|
|
_, ok := members[member]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (members MemberSet) HasMode(member *Client, mode ChannelMode) bool {
|
|
|
|
modes, ok := members[member]
|
2014-02-15 06:57:08 +01:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return modes[mode]
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:25:24 +01:00
|
|
|
func (members MemberSet) AnyHasMode(mode ChannelMode) bool {
|
|
|
|
for _, modes := range members {
|
|
|
|
if modes[Theater] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
type ChannelSet map[*Channel]bool
|
|
|
|
|
|
|
|
func (channels ChannelSet) Add(channel *Channel) {
|
|
|
|
channels[channel] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channels ChannelSet) Remove(channel *Channel) {
|
|
|
|
delete(channels, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channels ChannelSet) First() *Channel {
|
|
|
|
for channel := range channels {
|
|
|
|
return channel
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-09 07:42:14 +01:00
|
|
|
//
|
2014-02-09 07:06:10 +01:00
|
|
|
// interfaces
|
2014-02-09 07:42:14 +01:00
|
|
|
//
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2014-03-13 01:52:25 +01:00
|
|
|
type Identifiable interface {
|
2014-03-09 21:45:36 +01:00
|
|
|
Id() Name
|
|
|
|
Nick() Name
|
2014-02-09 17:48:11 +01:00
|
|
|
}
|