3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-24 21:09:30 +01:00
ergo/irc/types.go

144 lines
3.1 KiB
Go
Raw Normal View History

// 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>
// released under the MIT license
package irc
import (
"fmt"
2014-02-18 00:25:32 +01:00
"strings"
"sync"
)
// ChannelNameMap is a map that converts channel names to actual channel objects.
type ChannelNameMap struct {
ChansLock sync.RWMutex
Chans map[string]*Channel
}
// NewChannelNameMap returns a new ChannelNameMap.
func NewChannelNameMap() *ChannelNameMap {
var channels ChannelNameMap
channels.Chans = make(map[string]*Channel)
return &channels
}
2014-02-09 08:15:05 +01:00
// Get returns the given channel if it exists.
func (channels *ChannelNameMap) Get(name string) *Channel {
name, err := CasefoldChannel(name)
if err == nil {
channels.ChansLock.RLock()
defer channels.ChansLock.RUnlock()
return channels.Chans[name]
}
return nil
}
// Add adds the given channel to our map.
func (channels *ChannelNameMap) Add(channel *Channel) error {
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)
}
channels.Chans[channel.nameCasefolded] = channel
2014-02-09 08:15:05 +01:00
return nil
}
// Remove removes the given channel from our map.
func (channels *ChannelNameMap) Remove(channel *Channel) error {
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)
}
delete(channels.Chans, channel.nameCasefolded)
2014-02-09 08:15:05 +01:00
return nil
}
// Len returns how many channels we have.
func (channels *ChannelNameMap) Len() int {
channels.ChansLock.RLock()
defer channels.ChansLock.RUnlock()
return len(channels.Chans)
}
2017-03-24 03:23:21 +01:00
type ModeSet map[Mode]bool
2014-02-15 06:57:08 +01:00
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 ""
}
strs := make([]string, len(set))
index := 0
for mode := range set {
strs[index] = mode.String()
index++
}
return strings.Join(strs, "")
}
type ClientSet map[*Client]bool
2014-02-09 08:15:05 +01:00
func (clients ClientSet) Add(client *Client) {
clients[client] = true
2014-02-09 08:15:05 +01:00
}
func (clients ClientSet) Remove(client *Client) {
delete(clients, client)
}
func (clients ClientSet) Has(client *Client) bool {
return clients[client]
}
2017-03-24 03:23:21 +01:00
type MemberSet map[*Client]ModeSet
func (members MemberSet) Add(member *Client) {
2017-03-24 03:23:21 +01:00
members[member] = make(ModeSet)
}
func (members MemberSet) Remove(member *Client) {
delete(members, member)
}
func (members MemberSet) Has(member *Client) bool {
_, ok := members[member]
return ok
}
2017-03-24 03:23:21 +01:00
func (members MemberSet) HasMode(member *Client, mode Mode) bool {
modes, ok := members[member]
2014-02-15 06:57:08 +01:00
if !ok {
return false
}
return modes[mode]
}
2017-03-24 03:23:21 +01:00
func (members MemberSet) AnyHasMode(mode Mode) bool {
for _, modes := range members {
if modes[mode] {
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
}