3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-14 07:59:31 +01:00

enforce nicks via lowercase strings

This commit is contained in:
Jeremy Latt 2014-02-17 15:25:32 -08:00
parent 6d8b381927
commit a55b912941
2 changed files with 19 additions and 6 deletions

View File

@ -152,6 +152,10 @@ func (client *Client) Reply(reply Reply) {
client.replies <- reply
}
func (client *Client) IdleTime() time.Duration {
return time.Since(client.atime)
}
func (client *Client) HasNick() bool {
return client.nick != ""
}

View File

@ -3,6 +3,7 @@ package irc
import (
"errors"
"fmt"
"strings"
)
//
@ -74,22 +75,30 @@ type ClientNameMap map[string]*Client
var (
ErrNickMissing = errors.New("nick missing")
ErrNicknameInUse = errors.New("nickname in use")
ErrNicknameMismatch = errors.New("nickname mismatch")
)
func (clients ClientNameMap) Get(nick string) *Client {
return clients[strings.ToLower(nick)]
}
func (clients ClientNameMap) Add(client *Client) error {
if !client.HasNick() {
return ErrNickMissing
}
if clients[client.nick] != nil {
if clients.Get(client.nick) != nil {
return ErrNicknameInUse
}
clients[client.nick] = client
clients[strings.ToLower(client.nick)] = client
return nil
}
func (clients ClientNameMap) Remove(client *Client) error {
if clients[client.nick] != client {
return fmt.Errorf("%s: mismatch", client.nick)
if !client.HasNick() {
return ErrNickMissing
}
if clients.Get(client.nick) != client {
return ErrNicknameMismatch
}
delete(clients, client.nick)
return nil