Skip to content

Commit

Permalink
Merge pull request #110 from jdomingu98/87-kick-bug-manejo-de-argumentos
Browse files Browse the repository at this point in the history
feat(kick): more options to kick
  • Loading branch information
jdomingu98 authored Apr 24, 2024
2 parents 99392ea + 1811656 commit c78112d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
10 changes: 6 additions & 4 deletions includes/commands/KickCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class User;
*/
class KickCommand : public ACommand {
private:
std::vector<std::string> _channels;
const std::vector<User> _users;
std::string _comment;
std::vector<std::string> _channels;
const std::vector<User> _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<std::string> &channels, const std::vector<User> &users, std::string comment);
KickCommand(const std::vector<std::string> &channels, const std::vector<User> &users, const std::string &comment);
~KickCommand();

void execute(int clientFd);
Expand Down
53 changes: 33 additions & 20 deletions src/commands/KickCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param users The users to kick
* @param comment The comment for the kick
*/
KickCommand::KickCommand(const std::vector<std::string> &channels, const std::vector<User> &users, std::string comment)
KickCommand::KickCommand(const std::vector<std::string> &channels, const std::vector<User> &users, const std::string &comment)
: ACommand(true), _channels(channels), _users(users), _comment(comment) {}

/**
Expand All @@ -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<User> 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<User> 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);
}
6 changes: 5 additions & 1 deletion src/parser/KickParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ ACommand *KickParser::parse(const std::vector<std::string>& 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);
}

0 comments on commit c78112d

Please sign in to comment.