From d86982a4fa765857ed2f02868bbd1b8473a6ef57 Mon Sep 17 00:00:00 2001 From: jdomingu98 Date: Sun, 28 Apr 2024 16:22:17 +0200 Subject: [PATCH] fix(nick): nick message sent to all users in user channels --- includes/Responses.hpp | 1 + src/commands/NickCommand.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/Responses.hpp b/includes/Responses.hpp index 4790e3e..52a8959 100644 --- a/includes/Responses.hpp +++ b/includes/Responses.hpp @@ -31,6 +31,7 @@ class Channel; # define JOIN_MSG(channelName) " JOIN " + (channelName) # define KICK_MSG(channelName, kickedUser, comment) " KICK " + (channelName) + " " + (kickedUser) + " :" + (comment) # define MODE_MSG(channel, flag, modeParams) " MODE " + (channel) + " " + (flag) + " " + (modeParams) +# define NICK_MSG(nickname) " NICK " + (nickname) # define PART_MSG(channelName) " PART " + (channelName) # define PRIVMSG_MSG(destination, message) " PRIVMSG " + (destination) + " :" + (message) # define QUIT_MSG(message) " QUIT :" + (message) diff --git a/src/commands/NickCommand.cpp b/src/commands/NickCommand.cpp index 108d6fe..4ee0feb 100644 --- a/src/commands/NickCommand.cpp +++ b/src/commands/NickCommand.cpp @@ -37,13 +37,22 @@ void NickCommand::execute(int clientFd) { User &user = server.getUserByFd(clientFd); - if (server.isNicknameInUse(this->_nickname)) + if (user.getNickname() != this->_nickname && server.isNicknameInUse(this->_nickname)) throw NicknameInUseException(this->_nickname); if (!NickCommand::isValidNickname()) throw ErroneousNicknameException(this->_nickname); + if (user.isRegistered() && user.getNickname() != this->_nickname) { + std::vector userChannels = server.getChannels(); + for (std::vector::iterator it = userChannels.begin(); it != userChannels.end(); it++) { + if (it->isUserOnChannel(user.getNickname())) + it->broadcastToChannel(CMD_MSG(user.getNickname(), user.getUsername(), user.getHostname(), NICK_MSG(this->_nickname))); + } + } + user.setNickname(this->_nickname); + if (!user.isRegistered() && user.canRegister()) server.attemptUserRegistration(clientFd); }