kick-command #35

Merged
aortigos merged 3 commits from kick-command into main 2026-05-25 07:47:14 +00:00
5 changed files with 78 additions and 11 deletions

View File

@@ -4,7 +4,7 @@ SRC = main.cpp Server/Server.cpp User/User.cpp \
Channel/Channel.cpp \
cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \
cmds/join.cpp cmds/privmsg.cpp cmds/quit.cpp \
cmds/mode.cpp cmds/invite.cpp
cmds/mode.cpp cmds/invite.cpp cmds/kick.cpp \
HEADERS = Server/Server.hpp User/User.hpp

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
/* Updated: 2026/05/23 21:31:06 by iherman- ### ########.fr */
/* Updated: 2026/05/25 09:46:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -120,6 +120,7 @@ Server::Server(int port, const std::string& password) :
commands_["PRIVMSG"] = &Server::privmsg_cmd;
commands_["MODE"] = & Server::mode_cmd;
commands_["INVITE"] = &Server::invite_cmd;
commands_["KICK"] = &Server::kick_cmd;
std::cout << "Server port: " << port_
<< "\nServer Password: " << password_

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/23 21:29:13 by iherman- ### ########.fr */
/* Updated: 2026/05/25 09:46:21 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -69,6 +69,7 @@ class Server
void quit_cmd(User &client, std::istringstream &ss);
void mode_cmd(User &client, std::istringstream &ss);
void invite_cmd(User &client, std::istringstream &ss);
void kick_cmd(User &client, std::istringstream &ss);
public:
Server();

69
cmds/kick.cpp Normal file
View File

@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* kick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/25 09:27:35 by aortigos #+# #+# */
/* Updated: 2026/05/25 09:46:13 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* privmsg.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/25 09:25:49 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 (!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);
}

View File

@@ -6,16 +6,12 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/25 09:25:49 by aortigos ### ########.fr */
/* Updated: 2026/05/25 09:44:38 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
// Test function, need to change all the error messages
void Server::privmsg_cmd(User &client, std::istringstream &ss)
{
std::string channel;