Fixed duplicate nick registration

This commit is contained in:
Harry Jeffery 2013-08-23 16:24:06 +01:00
parent 8994c1bb55
commit afe61051b7

View File

@ -5,7 +5,6 @@ import (
"fmt"
"io"
"log"
"math/rand"
"net"
"regexp"
"sort"
@ -27,6 +26,7 @@ type Client struct {
signalChan chan int
outputChan chan string
nick string
registered bool
channelMap map[string]*Channel
}
@ -60,6 +60,7 @@ const (
errAlreadyReg
errNoSuchNick
errUnknownCommand
errNotReg
)
var (
@ -127,19 +128,23 @@ func (s *Server) handleEvent(e Event) {
return
}
//Protect the server name from being used
if newNick == s.name {
e.client.reply(errNickInUse, newNick)
return
}
e.client.setNick(newNick)
case command == "USER":
if e.client.nick == "" {
//Give them a unique Guest nick
newNick := fmt.Sprintf("Guest%d", rand.Int())
for s.clientMap[newNick] != nil {
newNick = fmt.Sprintf("Guest%d", rand.Int())
}
}
e.client.reply(rplWelcome)
//This command is completely disused, we use nick to register
case command == "JOIN":
if e.client.registered == false {
e.client.reply(errNotReg)
return
}
if len(args) < 1 {
e.client.reply(errMoreArgs)
return
@ -162,6 +167,11 @@ func (s *Server) handleEvent(e Event) {
}
case command == "PART":
if e.client.registered == false {
e.client.reply(errNotReg)
return
}
if len(args) < 1 {
e.client.reply(errMoreArgs)
return
@ -176,6 +186,11 @@ func (s *Server) handleEvent(e Event) {
}
case command == "PRIVMSG":
if e.client.registered == false {
e.client.reply(errNotReg)
return
}
if len(args) < 2 {
e.client.reply(errMoreArgs)
return
@ -199,9 +214,19 @@ func (s *Server) handleEvent(e Event) {
}
case command == "QUIT":
if e.client.registered == false {
e.client.reply(errNotReg)
return
}
//Stop the client, which will auto part channels and quit
e.client.signalChan <- signalStop
case command == "TOPIC":
if e.client.registered == false {
e.client.reply(errNotReg)
return
}
if len(args) < 1 {
e.client.reply(errMoreArgs)
return
@ -409,6 +434,8 @@ func (c *Client) reply(code int, args ...string) {
c.outputChan <- fmt.Sprintf(":%s 401 %s %s :No such nick/channel", c.server.name, c.nick, args[0])
case errUnknownCommand:
c.outputChan <- fmt.Sprintf(":%s 421 %s %s :Unknown command", c.server.name, c.nick, args[0])
case errNotReg:
c.outputChan <- fmt.Sprintf(":%s 451 :You have not registered", c.server.name)
}
}
@ -425,6 +452,11 @@ func (c *Client) setNick(nick string) {
c.nick = nick
c.server.clientMap[c.nick] = c
if oldNick == "" {
//Oldnick is ""
c.registered = true
c.reply(rplWelcome)
} else {
clients := make([]string, 0, 100)
for _, channel := range c.channelMap {
@ -451,3 +483,4 @@ func (c *Client) setNick(nick string) {
}
}
}
}