From 01559e27972460e1f09136810ad99eb9a7ddb50b Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 25 Jan 2024 18:12:12 +0100 Subject: [PATCH] dfsdkjbsdf --- includes/channel.hpp | 10 ++++++-- includes/server.hpp | 5 +++- srcs/channel.cpp | 51 +++++++++++++++++++++++++++++++++------ srcs/message.cpp | 4 +-- srcs/server.cpp | 22 ++++++++++++++++- srcs/server_functions.cpp | 25 +++++++++++++++++-- 6 files changed, 101 insertions(+), 16 deletions(-) diff --git a/includes/channel.hpp b/includes/channel.hpp index 0d7fdcf..cdbd7a4 100644 --- a/includes/channel.hpp +++ b/includes/channel.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */ -/* Updated: 2024/01/25 17:59:52 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 18:11:58 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -45,13 +45,19 @@ namespace irc void ModOperator(unstd::SharedPtr client, const std::string &clientname, bool mode); inline bool removeOperator(unstd::SharedPtr op) { return _operators.erase(op); } void changeMode(unstd::SharedPtr client, const Message& msg); - bool isOp(unstd::SharedPtr client); + bool isOp(unstd::SharedPtr client) const; void handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice = false) const; + inline bool hasClient(unstd::SharedPtr client) const { return _clients.find(client) != _clients.end(); } + bool hasClient(std::string client) const; + inline bool isInviteOnly() const { return _invite_only; } void setTopic(unstd::SharedPtr client, const std::string& new_topic); void showModes(void) const; + + bool kick(unstd::SharedPtr op, unstd::SharedPtr target, const std::string& reason); + ~Channel(); private: diff --git a/includes/server.hpp b/includes/server.hpp index d2f1573..1b4991e 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */ -/* Updated: 2024/01/25 16:39:18 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 17:21:25 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -62,6 +62,9 @@ namespace irc void handlePing(unstd::SharedPtr client, const class Message& msg); void handleMode(unstd::SharedPtr client, const class Message& msg); + class Channel* getChannelByName(const std::string& name); + unstd::SharedPtr getClientByName(const std::string& name); + bool isUserKnown(const std::string& user) const; bool isChannelKnown(const std::string& channel) const; diff --git a/srcs/channel.cpp b/srcs/channel.cpp index f1784af..8a78a92 100644 --- a/srcs/channel.cpp +++ b/srcs/channel.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ -/* Updated: 2024/01/25 18:09:11 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 18:11:45 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -19,6 +19,7 @@ namespace irc { typedef std::set >::iterator client_it; + typedef std::set >::const_iterator client_const_it; Channel::Channel(const std::string& name) : _name(name), _channel_size(-1) {} @@ -57,9 +58,9 @@ namespace irc } bool Channel::removeClient(unstd::SharedPtr client) { - if (!_clients.erase(client)) + if(!_clients.erase(client)) return (false); - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(client->getNickName(), "PART", _name); client->sendMsg(client->getNickName(), "PART", _name); return (true); @@ -138,6 +139,16 @@ namespace irc showModes(); } + bool Channel::hasClient(std::string client) const + { + for(client_const_it it = _clients.begin(); it != _clients.end(); ++it) + { + if(const_cast&>(*it)->getNickName() == client) + return true; + } + return false; + } + void Channel::setTopic(unstd::SharedPtr client, const std::string& new_topic) { if(_topic_op_restrict && !isOp(client)) @@ -148,14 +159,38 @@ namespace irc _topic = new_topic; } - bool Channel::isOp(unstd::SharedPtr client) + bool Channel::isOp(unstd::SharedPtr client) const { - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_const_it it = _clients.begin(); it != _clients.end(); ++it) { - if (const_cast&>(*it)->getNickName() == client->getNickName()) - return (true); + if(const_cast&>(*it)->getNickName() == client->getNickName()) + return true; } - return (false); + return false; } + + bool Channel::kick(unstd::SharedPtr op, unstd::SharedPtr target, const std::string& reason) + { + if(!hasClient(op)) + { + op->sendCode(ERR_NOTONCHANNEL, _name + " you're not on that channel"); + return false; + } + if(!hasClient(target)) + { + op->sendCode(ERR_USERNOTINCHANNEL, const_cast(target->getNickName()) + std::string(" " + _name) + " they aren't on that channel"); + return false; + } + if(!isOp(op)) + { + op->sendCode(ERR_CHANOPRIVSNEEDED, const_cast(_name) + " you're not channel operator"); + return false; + } + for(client_it it = _clients.begin(); it != _clients.end(); ++it) + const_cast&>(*it)->sendMsg(op->getNickName(), "KICK " + _name + ' ' + target->getNickName(), reason); + _clients.erase(target); + return true; + } + Channel::~Channel() {} } diff --git a/srcs/message.cpp b/srcs/message.cpp index b992b54..9abfe7d 100644 --- a/srcs/message.cpp +++ b/srcs/message.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 11:38:34 by maldavid #+# #+# */ -/* Updated: 2024/01/25 13:21:33 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 18:06:49 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,7 +56,7 @@ namespace irc _command = tokens[0]; _tokens = tokens; bool reason = false; - for(std::vector::iterator it = tokens.begin(); it != tokens.end(); ++it) + for(std::vector::iterator it = tokens.begin() + 1; it < tokens.end(); ++it) { if((*it)[0] == ':') reason = true; diff --git a/srcs/server.cpp b/srcs/server.cpp index f39dff8..7c2e909 100644 --- a/srcs/server.cpp +++ b/srcs/server.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ -/* Updated: 2024/01/25 16:39:09 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 17:23:12 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -184,6 +184,26 @@ namespace irc return false; } + Channel* Server::getChannelByName(const std::string& name) + { + for(channel_it it = _channels.begin(); it < _channels.end(); ++it) + { + if(it->getName() == name) + return &*it; + } + return NULL; + } + + unstd::SharedPtr Server::getClientByName(const std::string& name) + { + for(client_it it = _client.begin(); it < _client.end(); ++it) + { + if((*it)->getNickName() == name) + return *it; + } + return unstd::SharedPtr(NULL); + } + Server::~Server() { closeMainSocket(); diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 920f9bb..1c5f114 100644 --- a/srcs/server_functions.cpp +++ b/srcs/server_functions.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */ -/* Updated: 2024/01/25 18:04:02 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 18:11:24 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -293,11 +293,13 @@ namespace irc void Server::handleKick(unstd::SharedPtr client, const Message& msg) { + std::cout << "kick" << std::endl; if(msg.getArgs().empty()) { logs::report(log_error, "KICK, invalid command '%s'", msg.getRawMsg().c_str()); return; } + std::cout << "kick" << std::endl; typedef std::vector::iterator name_it; std::vector channels = unstd::split(msg.getArgs()[0], ','); @@ -308,18 +310,37 @@ namespace irc client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters"); return; } + std::cout << "kick" << std::endl; for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel) { + std::cout << *user << " " << *channel << std::endl; if(!isUserKnown(*user)) { client->sendCode(ERR_NOSUCHNICK, const_cast(*user) + " no such nick"); continue; } + std::cout << *user << " " << *channel << std::endl; if(!isChannelKnown(*channel)) { - client->sendCode(ERR_NOSUCHNICK, const_cast(*channel) + " no such channel"); + client->sendCode(ERR_NOSUCHCHANNEL, const_cast(*channel) + " no such channel"); continue; } + std::cout << *user << " " << *channel << std::endl; + + Channel* channel_target = getChannelByName(*channel); + if(channel_target == NULL) + logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->c_str()); + unstd::SharedPtr client_target = getClientByName(*user); + if(client_target.get() == NULL) + logs::report(log_fatal_error, "(KICK), cannot get client '%s' by name; panic !", user->c_str()); + + if(!channel_target->kick(client, client_target, msg.getReason())) + { + logs::report(log_error, "could not kick %s because why not", user->c_str()); + continue; + } + client->printUserHeader(); + std::cout << "kicked " << *user << " from " << *channel << std::endl; } }