Improve nick change handling

This commit is contained in:
Ken-Håvard Lieng 2017-04-11 06:04:59 +02:00
parent 0a6d8bfb20
commit e84f2dd993
4 changed files with 22 additions and 8 deletions

View file

@ -64,6 +64,12 @@ func (c *Client) GetNick() string {
return c.nick
}
func (c *Client) setNick(nick string) {
c.lock.Lock()
c.nick = nick
c.lock.Unlock()
}
func (c *Client) Connected() bool {
c.lock.Lock()
defer c.lock.Unlock()
@ -73,10 +79,6 @@ func (c *Client) Connected() bool {
func (c *Client) Nick(nick string) {
c.Write("NICK " + nick)
c.lock.Lock()
c.nick = nick
c.lock.Unlock()
}
func (c *Client) Oper(name, password string) {

View file

@ -175,7 +175,6 @@ func (c *Client) recv() {
}
msg := parseMessage(line)
c.Messages <- msg
switch msg.Command {
case Ping:
@ -186,14 +185,24 @@ func (c *Client) recv() {
c.addChannel(msg.Params[0])
}
case Nick:
if msg.Nick == c.GetNick() {
c.setNick(msg.LastParam())
}
case ReplyWelcome:
c.once.Do(c.ready.Done)
case ErrNicknameInUse:
if c.HandleNickInUse != nil {
c.nick = c.HandleNickInUse(c.nick)
c.writeNick(c.nick)
newNick := c.HandleNickInUse(msg.Params[1])
// Set the nick here aswell incase this happens during registration
// since there will be no NICK message to confirm it then
c.setNick(newNick)
go c.writeNick(newNick)
}
}
c.Messages <- msg
}
}