From 940daefd3e3a8dd6dd3dd40c4cac963096f14384 Mon Sep 17 00:00:00 2001 From: Namonay Date: Wed, 24 Jan 2024 19:27:52 +0100 Subject: [PATCH] caca --- .vscode/settings.json | 57 +++++++++++++++++++++++++++++++++++++++ includes/client.hpp | 3 ++- includes/errorscode.hpp | 2 +- srcs/client.cpp | 19 ++++++++++++- srcs/server.cpp | 4 +-- srcs/server_functions.cpp | 27 +++++++++++++++++-- 6 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5b3d663 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,57 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "set": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "ranges": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/includes/client.hpp b/includes/client.hpp index a8c6fdc..68a8b5c 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:33:17 by maldavid #+# #+# */ -/* Updated: 2024/01/24 15:00:39 by maldavid ### ########.fr */ +/* Updated: 2024/01/24 18:34:36 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -53,6 +53,7 @@ namespace irc void sendPlainText(const std::string& str); void sendCode(const std::string& code, const std::string& msg); void sendMsg(const std::string& sender, const std::string& cmd, const std::string& trailing); + void sendModular(std::string message, ...); ~Client(); diff --git a/includes/errorscode.hpp b/includes/errorscode.hpp index 83b5ce4..be95a18 100644 --- a/includes/errorscode.hpp +++ b/includes/errorscode.hpp @@ -32,7 +32,7 @@ #define ERR_NEEDMOREPARAMS "461" #define ERR_ALREADYREGISTRED "462", "Unauthorized command (already registered)" #define ERR_NOPERMFORHOST "463" -#define ERR_PASSWDMISMATCH "464", "Password incorrect" +#define ERR_PASSWDMISMATCH "464", "Password incorrect\n" #define ERR_YOUREBANNEDCREEP "465" #define ERR_YOUWILLBEBANNED "466" #define ERR_KEYSET "467" diff --git a/srcs/client.cpp b/srcs/client.cpp index deacc22..2b3b2a2 100644 --- a/srcs/client.cpp +++ b/srcs/client.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:35:52 by maldavid #+# #+# */ -/* Updated: 2024/01/24 15:01:44 by maldavid ### ########.fr */ +/* Updated: 2024/01/24 18:42:31 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -15,6 +15,11 @@ #include #include #include +#include +#include +#include +#include +#include namespace irc { @@ -42,6 +47,18 @@ namespace irc logs::report(log_error, "server failed to send a message from '%s' to '%s' (:sadge:)", sender.c_str(), _username.c_str()); } + void Client::sendModular(std::string message, ...) + { + char buffer[LOGS_BUFFER_SIZE]; + + va_list al; + va_start(al, message); + vsnprintf(buffer, LOGS_BUFFER_SIZE, message.c_str(), al); + va_end(al); + + if (send(_fd, buffer, std::strlen(buffer), 0) < static_cast(std::strlen(buffer))) + logs::report(log_error, "server failed to send a message to '%s'", _nickname.c_str()); + } void Client::printUserHeader() const { std::cout << AnsiColor::green << "[User " << _id; diff --git a/srcs/server.cpp b/srcs/server.cpp index a1a248f..f728f1b 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/24 15:14:19 by maldavid ### ########.fr */ +/* Updated: 2024/01/24 19:06:59 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -70,7 +70,7 @@ namespace irc while(recv((*it)->getFD(), buffer, INPUT_SIZE, 0) > 0) // read() but for socket fd { (*it)->newMsgInFlight(buffer); - //std::cout << buffer << std::endl; + std::cout << buffer << std::endl; while(handleMessage(*it)); std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining } diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index f5f6178..f319dbc 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/24 15:35:06 by maldavid ### ########.fr */ +/* Updated: 2024/01/24 19:23:56 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -76,7 +76,8 @@ namespace irc if(msg.getTokens()[1] == _password) { client->login(); - client->sendCode(RPL_WELCOME, "Welcome :)\n"); + client->sendCode(RPL_WELCOME, "\n"); + client->sendPlainText("Welcome to yipiirc :)\n"); } else { @@ -177,6 +178,28 @@ namespace irc return; } channel_it it; + if (msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#') + { + for (client_it itc = _client.begin(); itc != _client.end(); ++itc) + { + if ((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName()) + { + std::string complete_msg; + if(msg.getTokens().size() > 2) + { + for(std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) + { + complete_msg.append(*tit); + complete_msg.append(" "); + } + complete_msg.erase(complete_msg.begin()); + } + (*itc)->sendMsg(client->getNickName(), "PRIVMSG " + (*itc)->getNickName(), complete_msg); + break; + } + } + return ; + } for(it = _channels.begin(); it != _channels.end(); ++it) { if(msg.getTokens()[1] == it->getName())