From 8e09525237bb5b6c97944270ba55c0964a0e32d9 Mon Sep 17 00:00:00 2001 From: Namonay Date: Thu, 25 Jan 2024 21:55:35 +0100 Subject: [PATCH] gg --- srcs/channel.cpp | 9 +++++---- srcs/client.cpp | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 1bb0de5..817ba4b 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 21:21:12 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 21:46:29 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -72,14 +72,15 @@ namespace irc void Channel::sendWho(unstd::SharedPtr client) { - std::string clientlist(RPL_NAMREPLY " " + client->getNickName() + " = " + getName() + ": "); + std::string clientlist(":yipirc " RPL_NAMREPLY " " + client->getNickName() + " = " + getName() + ": "); for (client_it it = _clients.begin(); it != _clients.end(); ++it) { clientlist += const_cast&>(*it)->getNickName() + ' '; } - client->sendModular("%s", clientlist.c_str()); - client->sendModular("%s %s %s : End of names list", RPL_ENDOFNAMES, client->getNickName().c_str(), getName().c_str()); + logs::report(log_message, "%s", clientlist.c_str()); + client->sendModular("%s\r\n", clientlist.c_str()); + client->sendModular("%s %s %s : End of names list\r\n", RPL_ENDOFNAMES, client->getNickName().c_str(), getName().c_str()); } void Channel::handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice) const diff --git a/srcs/client.cpp b/srcs/client.cpp index 80b9b98..56f0f59 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/25 21:22:34 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 21:55:14 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -20,6 +20,7 @@ #include #include #include +#include namespace irc { @@ -27,7 +28,7 @@ namespace irc void Client::sendCode(const std::string& code, const std::string& msg) { - const std::string command = code + " :" + msg + "\r\n"; + const std::string command = ":yipirc " + code + " : " + msg + "\r"; #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif @@ -55,18 +56,33 @@ namespace irc } void Client::sendModular(std::string message, ...) + { + std::string buffer; + + va_list al; + va_list al_copy; + + va_start(al, message); + __builtin_va_copy(al_copy, al); + + int len = vsnprintf(NULL, 0, message.c_str(), al); + + if(len > 0) { - char buffer[LOGS_BUFFER_SIZE]; - - va_list al; - va_start(al, message); - vsnprintf(buffer, LOGS_BUFFER_SIZE, message.c_str(), al); - va_end(al); - - logs::report(log_message,"<- S %s", buffer); - 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()); + std::vector tmp(len + 1); + vsnprintf(&tmp[0], tmp.size(), message.c_str(), al_copy); + buffer = std::string(&tmp[0], len); + buffer += "\r"; } + va_end(al_copy); + va_end(al); + +#ifdef DEBUG + logs::report(log_message,"sending '%s'", buffer.c_str()); +#endif + if (send(_fd, buffer.c_str(), buffer.length(), 0) < static_cast(buffer.length())) + 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;