3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-29 15:40:02 +01:00
ergo/irc/types.go

87 lines
1.9 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 (
2014-02-18 00:25:32 +01:00
"strings"
)
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 ""
}
strs := make([]string, len(set))
index := 0
for mode := range set {
strs[index] = mode.String()
index++
}
return strings.Join(strs, "")
}
2017-10-05 15:29:34 +02:00
// ClientSet is a set of clients.
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) {
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.
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
2017-10-05 15:29:34 +02:00
// Add adds the given client to this set.
func (members MemberSet) Add(member *Client) {
2017-03-24 03:23:21 +01:00
members[member] = make(ModeSet)
}
2017-10-05 15:29:34 +02:00
// Remove removes the given client from this set.
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.
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 {
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 {
for _, modes := range members {
if modes[mode] {
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