diff --git a/channel.go b/channel.go index eb149f8..12feb94 100644 --- a/channel.go +++ b/channel.go @@ -254,6 +254,14 @@ func (c *Channel) RemoveMember(client *Client) { } } +func (c *Channel) UpdateMemberNick(client *Client, oldNick string) { + c.membersMutex.Lock() + defer c.membersMutex.Unlock() + modes := c.members[oldNick] + delete(c.members, oldNick) + c.members[client.Nickname] = modes +} + // HasMember returns if a client is an existing member of this channel func (c *Channel) HasMember(client *Client) bool { c.membersMutex.RLock() diff --git a/client.go b/client.go index d202e9d..3060d5f 100644 --- a/client.go +++ b/client.go @@ -32,7 +32,7 @@ type Client struct { AwayMessage string channels map[string]*Channel - channelMutex sync.Mutex + channelMutex sync.RWMutex *UserModeSet } @@ -213,3 +213,20 @@ func (c *Client) RemoveChannel(channel *Channel) { func (c *Client) GetChannels() map[string]*Channel { return c.channels } + +// UpdateNick updates the clients nicknamae to a new nickname +func (c *Client) UpdateNick(newNick string) { + oldNick := c.Nickname + c.Nickname = newNick + + c.Server.UpdateClientNick(c, oldNick) + c.channelMutex.RLock() + defer c.channelMutex.RUnlock() + for _, channel := range c.channels { + channel.UpdateMemberNick(c, oldNick) + } + m := irc.Message{Prefix: c.Prefix, Command: irc.NICK, Trailing: newNick} + + c.Encode(&m) + c.Prefix.Name = newNick +} diff --git a/commands.go b/commands.go index 0d8642c..ca5c884 100644 --- a/commands.go +++ b/commands.go @@ -63,7 +63,7 @@ func NickHandler(message *irc.Message, client *Client) { var m irc.Message name := client.Server.Config.Name - nickname := client.Nickname + //nickname := client.Nickname if len(message.Params) == 0 { m = irc.Message{Prefix: &irc.Prefix{Name: name}, Command: irc.ERR_NONICKNAMEGIVEN, Trailing: "No nickname given"} @@ -88,10 +88,12 @@ func NickHandler(message *irc.Message, client *Client) { client.Nickname = newNickname client.Server.AddClientNick(client) client.Welcome() - } else { //change client name - client.Nickname = newNickname - client.Server.UpdateClientNick(client, nickname) + } else if len(client.Username) != 0 { //change client name + fmt.Println("Changing nick from", client.Nickname, "to", newNickname) + client.UpdateNick(newNickname) //fmt.Println("Updating client name") + } else { + client.Nickname = newNickname } }