3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 20:09:41 +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 client.replies <- reply
} }
func (client *Client) IdleTime() time.Duration {
return time.Since(client.atime)
}
func (client *Client) HasNick() bool { func (client *Client) HasNick() bool {
return client.nick != "" return client.nick != ""
} }

View File

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