Skip to content

Commit

Permalink
fix(nick): nick message sent to all users in user channels
Browse files Browse the repository at this point in the history
  • Loading branch information
jdomingu98 committed Apr 28, 2024
1 parent 2ce1f16 commit d86982a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions includes/Responses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion src/commands/NickCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel> userChannels = server.getChannels();
for (std::vector<Channel>::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);
}
Expand Down

0 comments on commit d86982a

Please sign in to comment.