2014-02-09 07:06:10 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2014-02-13 19:57:00 +01:00
|
|
|
"errors"
|
2014-02-09 07:06:10 +01:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
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-02-09 07:42:14 +01:00
|
|
|
// a string with wildcards
|
|
|
|
type Mask string
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return fmt.Sprintf("%c", mode)
|
|
|
|
}
|
|
|
|
|
2014-02-15 04:25:14 +01:00
|
|
|
type Phase uint
|
|
|
|
|
|
|
|
type Numeric uint
|
|
|
|
|
|
|
|
func (code Numeric) String() string {
|
|
|
|
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 {
|
|
|
|
return fmt.Sprintf("%c", mode)
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
type ChannelNameMap map[string]*Channel
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientNameMap map[string]*Client
|
|
|
|
|
2014-02-13 19:57:00 +01:00
|
|
|
var (
|
|
|
|
ErrNickMissing = errors.New("nick missing")
|
|
|
|
ErrNicknameInUse = errors.New("nickname in use")
|
|
|
|
)
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
func (clients ClientNameMap) Add(client *Client) error {
|
2014-02-13 19:57:00 +01:00
|
|
|
if !client.HasNick() {
|
|
|
|
return ErrNickMissing
|
|
|
|
}
|
2014-02-09 08:15:05 +01:00
|
|
|
if clients[client.nick] != nil {
|
2014-02-13 19:57:00 +01:00
|
|
|
return ErrNicknameInUse
|
2014-02-09 08:15:05 +01:00
|
|
|
}
|
|
|
|
clients[client.nick] = client
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clients ClientNameMap) Remove(client *Client) error {
|
|
|
|
if clients[client.nick] != client {
|
|
|
|
return fmt.Errorf("%s: mismatch", client.nick)
|
|
|
|
}
|
|
|
|
delete(clients, client.nick)
|
|
|
|
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-16 04:49:20 +01:00
|
|
|
type ClientSet map[*Client]ChannelModeSet
|
2014-02-09 08:15:05 +01:00
|
|
|
|
|
|
|
func (clients ClientSet) Add(client *Client) {
|
2014-02-16 04:49:20 +01:00
|
|
|
clients[client] = make(ChannelModeSet)
|
2014-02-09 08:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clients ClientSet) Remove(client *Client) {
|
|
|
|
delete(clients, client)
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
func (clients ClientSet) HasMode(client *Client, mode ChannelMode) bool {
|
2014-02-15 06:57:08 +01:00
|
|
|
modes, ok := clients[client]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return modes[mode]
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
func (clients ClientSet) Has(client *Client) bool {
|
2014-02-15 06:57:08 +01:00
|
|
|
_, ok := clients[client]
|
|
|
|
return ok
|
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-15 04:35:25 +01:00
|
|
|
Reply(Reply)
|
2014-02-09 17:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Reply interface {
|
2014-02-12 00:00:19 +01:00
|
|
|
Format(*Client) []string
|
2014-02-09 17:48:11 +01:00
|
|
|
Source() Identifier
|
|
|
|
}
|
|
|
|
|
2014-02-09 07:06:10 +01:00
|
|
|
type Command interface {
|
2014-02-15 03:28:36 +01:00
|
|
|
Name() string
|
2014-02-09 07:06:10 +01:00
|
|
|
Client() *Client
|
|
|
|
Source() Identifier
|
|
|
|
Reply(Reply)
|
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)
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
type ChannelCommand interface {
|
|
|
|
Command
|
|
|
|
HandleChannel(channel *Channel)
|
|
|
|
}
|
|
|
|
|
2014-02-09 07:42:14 +01:00
|
|
|
//
|
2014-02-09 07:06:10 +01:00
|
|
|
// structs
|
2014-02-09 07:42:14 +01:00
|
|
|
//
|
2014-02-09 07:06:10 +01:00
|
|
|
|
|
|
|
type UserMask struct {
|
|
|
|
nickname Mask
|
|
|
|
username Mask
|
|
|
|
hostname Mask
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mask *UserMask) String() string {
|
|
|
|
return fmt.Sprintf("%s!%s@%s", mask.nickname, mask.username, mask.hostname)
|
|
|
|
}
|