From 6b87fe8840d24c6f71172fbc0e99a0a87bc6b409 Mon Sep 17 00:00:00 2001 From: aortigos Date: Mon, 25 May 2026 09:46:45 +0200 Subject: [PATCH] Kick command added --- Makefile | 2 +- Server/Server.cpp | 7 ++--- Server/Server.hpp | 5 ++-- cmds/kick.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 cmds/kick.cpp diff --git a/Makefile b/Makefile index e0fc1df..95980d3 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Server/Server.cpp b/Server/Server.cpp index 2732f20..bda78ae 100644 --- a/Server/Server.cpp +++ b/Server/Server.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Server.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: iherman- > 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::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::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); + +}