Files
ft_irc/cmds/kick.cpp
2026-05-25 09:56:20 +02:00

59 lines
2.7 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* kick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/25 09:27:35 by aortigos #+# #+# */
/* Updated: 2026/05/25 09:52:27 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::kick_cmd(User &client, std::istringstream &ss)
{
std::string channel;
std::string user;
std::string reason;
ss >> channel >> user;
getline(ss, reason);
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
if (channel.empty() || user.empty())
return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " KICK :not enough parameters\r\n"));
if (!reason.empty() && reason[0] == ' ')
reason = reason.substr(1);
if (!reason.empty() && reason[0] == ':')
reason = reason.substr(1);
if (reason.empty()) reason = "Kicked";
if (channel[0] != '#') return (client.send(""));
std::map<std::string, Channel>::iterator it_channels = channels_.find(channel);
if (it_channels == channels_.end())
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
if (!it_channels->second.hasOperator(client.getFd()))
return (client.send(":" SERVER_NAME " 482 " + client.getNick() + " " + channel + " :You're not channel operator\r\n"));
int userId = -1;
for (std::map<int, User>::iterator it_clients = clients_.begin(); it_clients != clients_.end(); it_clients++)
{
if (it_clients->second.getNick() == user)
userId = it_clients->second.getFd();
}
if (userId == -1)
return (client.send(":" SERVER_NAME " 401 " + client.getNick() + " " + user + " :No such nick\r\n"));
if (!it_channels->second.hasMember(userId))
return (client.send(":" SERVER_NAME " 441 " + client.getNick() + " " + user + " " + channel + " :They aren't on that channel\r\n"));
it_channels->second.broadcast(":" + client.getNick() + "!" + client.getUsername() + "@localhost KICK " + channel + " " + user + " :" + reason + "\r\n", clients_, -1);
clients_[userId].leaveChannel(channel);
it_channels->second.removeMember(userId);
}