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-01 04:21:33 +01:00
|
|
|
type CapSubCommand string
|
|
|
|
|
|
|
|
type Capability string
|
|
|
|
|
2014-03-08 22:59:48 +01:00
|
|
|
func (capability Capability) String() string {
|
|
|
|
return string(capability)
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:21:33 +01:00
|
|
|
type CapModifier rune
|
|
|
|
|
2014-03-02 21:54:48 +01:00
|
|
|
func (mod CapModifier) String() string {
|
|
|
|
return string(mod)
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:21:33 +01:00
|
|
|
type CapState uint
|
|
|
|
|
|
|
|
type CapabilitySet map[Capability]bool
|
|
|
|
|
|
|
|
func (set CapabilitySet) String() string {
|
|
|
|
strs := make([]string, len(set))
|
|
|
|
index := 0
|
|
|
|
for capability := range set {
|
|
|
|
strs[index] = string(capability)
|
2014-03-02 21:54:48 +01:00
|
|
|
index += 1
|
2014-03-01 04:21:33 +01:00
|
|
|
}
|
|
|
|
return strings.Join(strs, " ")
|
|
|
|
}
|
|
|
|
|
2014-03-08 22:59:48 +01:00
|
|
|
func (set CapabilitySet) DisableString() string {
|
|
|
|
parts := make([]string, len(set))
|
|
|
|
index := 0
|
|
|
|
for capability := range set {
|
|
|
|
parts[index] = Disable.String() + capability.String()
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
return strings.Join(parts, " ")
|
|
|
|
}
|
|
|
|
|
2014-02-09 07:42:14 +01:00
|
|
|
// add, remove, list modes
|
2014-02-09 07:06:10 +01:00
|
|
|
type ModeOp rune
|
2014-02-09 07:42:14 +01:00
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
func (op ModeOp) String() string {
|
|
|
|
return string(op)
|
|
|
|
}
|
|
|
|
|
2014-02-09 07:42:14 +01:00
|
|
|
// user mode flags
|
2014-02-09 07:06:10 +01:00
|
|
|
type UserMode rune
|
2014-02-09 07:42:14 +01:00
|
|
|
|
2014-02-09 17:53:06 +01:00
|
|
|
func (mode UserMode) String() string {
|
2014-03-01 04:21:33 +01:00
|
|
|
return string(mode)
|
2014-02-09 17:53:06 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 04:25:14 +01:00
|
|
|
type Phase uint
|
|
|
|
|
2014-02-17 02:23:47 +01:00
|
|
|
type ReplyCode interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type StringCode string
|
|
|
|
|
|
|
|
func (code StringCode) String() string {
|
|
|
|
return string(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
type NumericCode uint
|
2014-02-15 04:25:14 +01:00
|
|
|
|
2014-02-17 02:23:47 +01:00
|
|
|
func (code NumericCode) String() string {
|
2014-02-15 04:25:14 +01:00
|
|
|
return fmt.Sprintf("%03d", code)
|
|
|
|
}
|
|
|
|
|
2014-02-09 07:42:14 +01:00
|
|
|
// channel mode flags
|
2014-02-09 07:06:10 +01:00
|
|
|
type ChannelMode rune
|
2014-02-09 07:42:14 +01:00
|
|
|
|
2014-02-09 08:33:56 +01:00
|
|
|
func (mode ChannelMode) String() string {
|
2014-03-01 04:21:33 +01:00
|
|
|
return string(mode)
|
2014-02-09 08:33:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
type ChannelNameMap map[string]*Channel
|
|
|
|
|
2014-02-26 05:17:26 +01:00
|
|
|
func (channels ChannelNameMap) Get(name string) *Channel {
|
|
|
|
return channels[strings.ToLower(name)]
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
func (channels ChannelNameMap) Add(channel *Channel) error {
|
|
|
|
if channels[channel.name] != nil {
|
|
|
|
return fmt.Errorf("%s: already set", channel.name)
|
|
|
|
}
|
|
|
|
channels[channel.name] = channel
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channels ChannelNameMap) Remove(channel *Channel) error {
|
|
|
|
if channel != channels[channel.name] {
|
|
|
|
return fmt.Errorf("%s: mismatch", channel.name)
|
|
|
|
}
|
|
|
|
delete(channels, channel.name)
|
|
|
|
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-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-02-09 17:48:11 +01:00
|
|
|
type Identifier interface {
|
|
|
|
Id() string
|
|
|
|
Nick() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Replier interface {
|
2014-02-20 07:20:34 +01:00
|
|
|
Reply(...string)
|
2014-02-09 17:48:11 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 07:06:10 +01:00
|
|
|
type Command interface {
|
2014-02-17 08:29:11 +01:00
|
|
|
Code() StringCode
|
2014-02-09 07:06:10 +01:00
|
|
|
Client() *Client
|
2014-02-15 03:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServerCommand interface {
|
|
|
|
Command
|
2014-02-09 07:06:10 +01:00
|
|
|
HandleServer(*Server)
|
|
|
|
}
|
|
|
|
|
2014-02-15 03:28:36 +01:00
|
|
|
type AuthServerCommand interface {
|
|
|
|
Command
|
|
|
|
HandleAuthServer(*Server)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegServerCommand interface {
|
|
|
|
Command
|
|
|
|
HandleRegServer(*Server)
|
|
|
|
}
|