diff --git a/includes/commands/KickCommand.hpp b/includes/commands/KickCommand.hpp index 2119e08..1c923b0 100644 --- a/includes/commands/KickCommand.hpp +++ b/includes/commands/KickCommand.hpp @@ -15,12 +15,14 @@ class User; */ class KickCommand : public ACommand { private: - std::vector _channels; - const std::vector _users; - std::string _comment; + std::vector _channels; + const std::vector _users; + const std::string _comment; + + void kickUserFromChannel(Channel &channel, const User &user, const std::string &kickedUser, const std::string &comment); public: - KickCommand(const std::vector &channels, const std::vector &users, std::string comment); + KickCommand(const std::vector &channels, const std::vector &users, const std::string &comment); ~KickCommand(); void execute(int clientFd); diff --git a/src/commands/KickCommand.cpp b/src/commands/KickCommand.cpp index 503fb07..a72c115 100644 --- a/src/commands/KickCommand.cpp +++ b/src/commands/KickCommand.cpp @@ -7,7 +7,7 @@ * @param users The users to kick * @param comment The comment for the kick */ -KickCommand::KickCommand(const std::vector &channels, const std::vector &users, std::string comment) +KickCommand::KickCommand(const std::vector &channels, const std::vector &users, const std::string &comment) : ACommand(true), _channels(channels), _users(users), _comment(comment) {} /** @@ -26,33 +26,46 @@ void KickCommand::execute(int clientFd) { User &user = server.getUserByFd(clientFd); std::string nickname = user.getNickname(); - - // Doy por hecho que _channels.size() == _users.size() + std::string comment = this->_comment.empty() ? nickname : this->_comment; std::string kickedUser; - for (size_t i = 0; i < this->_channels.size(); i++) { - kickedUser = (this->_users)[i].getNickname(); + size_t pos; + for (size_t i = 0; i < this->_channels.size(); i++) { Channel &channel = server.getChannelByName(this->_channels[i]); if (!channel.isUserInChannel(nickname)) throw NotOnChannelException(this->_channels[i]); - + if (!channel.isOper(nickname)) throw ChanOPrivsNeededException(this->_channels[i]); - - if (!channel.isUserInChannel(kickedUser)) - throw UserNotInChannelException(kickedUser, this->_channels[i]); - - std::vector channelUsers = channel.getAllUsers(); - std::string comment = _comment.empty() ? nickname : _comment; - - for (size_t j = 0; j < channelUsers.size(); j++) { - Logger::debug("Sending KICK message of user " + kickedUser + " to user " + channelUsers[j].getNickname().c_str()); - server.sendMessage(channelUsers[j].getFd(), - KICK_MSG(nickname, user.getUsername(), user.getHostname(), - this->_channels[i], kickedUser, comment)); + + pos = this->_channels.size() == this->_users.size() ? i : 0; + kickedUser = this->_users[pos].getNickname(); + if (this->_channels.size() == 1) { + for (size_t j = 0; j < this->_users.size(); j++) { + kickedUser = this->_users[j].getNickname(); + kickUserFromChannel(channel, user, kickedUser, comment); + } + continue; } - channelUsers.clear(); - channel.removeUser(kickedUser); + kickUserFromChannel(channel, user, kickedUser, comment); + } +} + +void KickCommand::kickUserFromChannel(Channel &channel, const User &user, + const std::string &kickedUser, const std::string &comment) { + Server &server = Server::getInstance(); + + if (!channel.isUserInChannel(kickedUser)) + throw UserNotInChannelException(kickedUser, channel.getName()); + + std::vector channelUsers = channel.getAllUsers(); + + for (size_t j = 0; j < channelUsers.size(); j++) { + Logger::debug("Sending KICK message of user " + kickedUser + " to user " + channelUsers[j].getNickname().c_str()); + server.sendMessage(channelUsers[j].getFd(), + KICK_MSG(user.getNickname(), user.getUsername(), user.getHostname(), + channel.getName(), kickedUser, comment)); } + channel.removeUser(kickedUser); } \ No newline at end of file diff --git a/src/parser/KickParser.cpp b/src/parser/KickParser.cpp index c457d25..324f825 100644 --- a/src/parser/KickParser.cpp +++ b/src/parser/KickParser.cpp @@ -31,8 +31,12 @@ ACommand *KickParser::parse(const std::vector& tokens) { User &user = Server::getInstance().getUserByNickname(usersList[i]); users.push_back(user); } + usersList.clear(); + + if (channels.size() != users.size() && channels.size() != 1 && users.size() != 1) + throw NeedMoreParamsException("KICK"); const std::string comment = tokens.size() > 3 ? tokens[3] : NONE; - usersList.clear(); + return new KickCommand(channels, users, comment); } \ No newline at end of file