From 3953cb8d0aecd48890f0074df904af4ea9d7e2ee Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 25 Jan 2024 17:04:42 +0100 Subject: [PATCH] hon hon hon baguette --- Makefile | 19 ++++++++++--------- includes/message.hpp | 6 +++++- includes/server.hpp | 5 ++++- includes/unstd/string.hpp | 6 +++++- srcs/channel.cpp | 3 +-- srcs/message.cpp | 15 ++++++++++++++- srcs/server.cpp | 28 +++++++++++++++++++++++++++- srcs/server_functions.cpp | 39 +++++++++++++++++++++++++++++++++------ srcs/unstd/string.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 srcs/unstd/string.cpp diff --git a/Makefile b/Makefile index 1a0e170..dc89e0d 100644 --- a/Makefile +++ b/Makefile @@ -6,20 +6,21 @@ # By: vvaas +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/08/09 15:08:49 by vavaas #+# #+# # -# Updated: 2024/01/22 17:32:49 by maldavid ### ########.fr # +# Updated: 2024/01/25 15:22:06 by maldavid ### ########.fr # # # #******************************************************************************# NAME = ircserv -SRCS = srcs/main.cpp \ - srcs/logs.cpp \ - srcs/server.cpp \ - srcs/server_functions.cpp \ - srcs/client.cpp \ - srcs/channel.cpp \ - srcs/message.cpp \ - srcs/ansi.cpp \ +SRCS = srcs/main.cpp \ + srcs/logs.cpp \ + srcs/ansi.cpp \ + srcs/server.cpp \ + srcs/client.cpp \ + srcs/channel.cpp \ + srcs/message.cpp \ + srcs/unstd/string.cpp \ + srcs/server_functions.cpp \ OBJ_DIR = objs OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o)) diff --git a/includes/message.hpp b/includes/message.hpp index 0f6874c..57f6e7d 100644 --- a/includes/message.hpp +++ b/includes/message.hpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 11:35:21 by maldavid #+# #+# */ -/* Updated: 2024/01/22 11:51:25 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 13:09:57 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,14 +27,18 @@ namespace irc inline unstd::SharedPtr getClient() const { return _client; } inline const std::string& getRawMsg() const { return _raw_msg; } inline const std::string& getCmd() const { return _command; } + inline const std::vector& getArgs() const { return _args; } + inline const std::string& getReason() const { return _reason; } inline const std::vector& getTokens() const { return _tokens; } ~Message(); private: std::vector _tokens; + std::vector _args; const std::string _raw_msg; std::string _command; + std::string _reason; unstd::SharedPtr _client; }; } diff --git a/includes/server.hpp b/includes/server.hpp index 0c0802c..d2f1573 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/24 19:57:00 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 16:39:18 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); + bool isUserKnown(const std::string& user) const; + bool isChannelKnown(const std::string& channel) const; + private: std::vector _channels; std::vector > _client; diff --git a/includes/unstd/string.hpp b/includes/unstd/string.hpp index 2af4733..ecff7fb 100644 --- a/includes/unstd/string.hpp +++ b/includes/unstd/string.hpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 19:13:26 by maldavid #+# #+# */ -/* Updated: 2024/01/20 19:14:55 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 15:20:00 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ #define __UNSTD_STRING__ #include +#include +#include namespace unstd { @@ -24,6 +26,8 @@ namespace unstd ss << n; return ss.str(); } + + std::vector split(const std::string& s, char delim); } #endif diff --git a/srcs/channel.cpp b/srcs/channel.cpp index c529ad2..844ab87 100644 --- a/srcs/channel.cpp +++ b/srcs/channel.cpp @@ -6,14 +6,13 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ -/* Updated: 2024/01/25 02:33:59 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 12:46:20 by maldavid ### ########.fr */ /* */ /******************************************************************************/ #include #include #include -#include #include namespace irc { diff --git a/srcs/message.cpp b/srcs/message.cpp index 1e788e4..b992b54 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/22 13:02:56 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 13:21:33 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,6 +55,19 @@ namespace irc return; _command = tokens[0]; _tokens = tokens; + bool reason = false; + for(std::vector::iterator it = tokens.begin(); it != tokens.end(); ++it) + { + if((*it)[0] == ':') + reason = true; + if(!reason) + _args.push_back(*it); + else + { + _reason.append(*it); + _reason.push_back(' '); + } + } } Message::~Message() {} diff --git a/srcs/server.cpp b/srcs/server.cpp index f5984b9..f39dff8 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 00:09:16 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 16:39:09 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -22,6 +22,12 @@ namespace irc { + typedef std::vector >::iterator client_it; + typedef std::vector >::const_iterator client_const_it; + + typedef std::vector::iterator channel_it; + typedef std::vector::const_iterator channel_const_it; + Server::Server(int port, const std::string& password) : _s_len(sizeof(_s_data)), _password(password), _port(port), _active(true) { std::memset(&_s_data, 0, sizeof(sockaddr)); @@ -158,6 +164,26 @@ namespace irc return true; } + bool Server::isUserKnown(const std::string& user) const + { + for(client_const_it it = _client.begin(); it < _client.end(); ++it) + { + if(const_cast&>(*it)->getNickName() == user) + return true; + } + return false; + } + + bool Server::isChannelKnown(const std::string& channel) const + { + for(channel_const_it it = _channels.begin(); it < _channels.end(); ++it) + { + if(it->getName() == channel) + return true; + } + return false; + } + Server::~Server() { closeMainSocket(); diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index b303781..6df3cdb 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 02:43:54 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 17:03:18 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -22,6 +22,7 @@ #include #include #include +#include namespace irc { @@ -143,7 +144,7 @@ namespace irc void Server::handleJoin(unstd::SharedPtr client, const Message& msg) { - if(msg.getTokens().size() < 2) + if(msg.getArgs().empty()) { client->sendCode(ERR_NEEDMOREPARAMS, "Need more params"); logs::report(log_error, "JOIN, invalid command '%s'", msg.getRawMsg().c_str()); @@ -183,7 +184,7 @@ namespace irc void Server::handlePrivMsg(unstd::SharedPtr client, const Message& msg) { - if(msg.getTokens().size() < 2) + if(msg.getArgs().empty()) { client->sendCode(ERR_NORECIPIENT, "No recipient given\n"); return; @@ -237,7 +238,7 @@ namespace irc void Server::handleNotice(unstd::SharedPtr client, const Message& msg) { - if(msg.getTokens().size() < 2) + if(msg.getArgs().empty()) { logs::report(log_error, "NOTICE, invalid command '%s'", msg.getRawMsg().c_str()); return; @@ -292,8 +293,34 @@ namespace irc void Server::handleKick(unstd::SharedPtr client, const Message& msg) { - (void)client; - (void)msg; + if(msg.getArgs().empty()) + { + logs::report(log_error, "KICK, invalid command '%s'", msg.getRawMsg().c_str()); + return; + } + + typedef std::vector::iterator name_it; + std::vector channels = unstd::split(msg.getArgs()[0], ','); + std::vector users = unstd::split(msg.getArgs()[1], ','); + + if(users.size() != channels.size()) + { + client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters"); + return; + } + for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel) + { + if(!isUserKnown(*user)) + { + client->sendCode(ERR_NOSUCHNICK, const_cast(*user) + " no such nick"); + continue; + } + if(!isChannelKnown(*channel)) + { + client->sendCode(ERR_NOSUCHNICK, const_cast(*channel) + " no such channel"); + continue; + } + } } void Server::handleTopic(unstd::SharedPtr client, const Message& msg) diff --git a/srcs/unstd/string.cpp b/srcs/unstd/string.cpp new file mode 100644 index 0000000..8c53a8d --- /dev/null +++ b/srcs/unstd/string.cpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* string.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/25 15:19:30 by maldavid #+# #+# */ +/* Updated: 2024/01/25 15:20:20 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +namespace unstd +{ + std::vector split(const std::string& s, char delim) + { + std::vector elems; + std::string token; + for(std::string::const_iterator it = s.begin(); it != s.end();) + { + if(*it == delim) + { + elems.push_back(token); + token.clear(); + while(*it == delim && it != s.end()) + it++; + } + else + { + token.push_back(*it); + it++; + } + } + elems.push_back(token); + return elems; + } +}