From 67276181224eaee05ebfb21e709d350c00a453eb Mon Sep 17 00:00:00 2001 From: Namonay Date: Thu, 25 Jan 2024 21:19:54 +0100 Subject: [PATCH] gg --- includes/channel.hpp | 4 ++-- includes/server.hpp | 3 ++- srcs/channel.cpp | 20 ++++++++++++++++++-- srcs/client.cpp | 8 +++++--- srcs/server.cpp | 4 +++- srcs/server_functions.cpp | 13 ++++++++++++- 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/includes/channel.hpp b/includes/channel.hpp index cdbd7a4..7c73b59 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 18:11:58 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 19:38:50 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -47,7 +47,7 @@ namespace irc void changeMode(unstd::SharedPtr client, const Message& msg); bool isOp(unstd::SharedPtr client) const; void handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice = false) const; - + void sendWho(unstd::SharedPtr client); inline bool hasClient(unstd::SharedPtr client) const { return _clients.find(client) != _clients.end(); } bool hasClient(std::string client) const; diff --git a/includes/server.hpp b/includes/server.hpp index 1b4991e..579342c 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 17:21:25 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 18:44:01 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -57,6 +57,7 @@ namespace irc void handleInvite(unstd::SharedPtr client, const class Message& msg); void handlePrivMsg(unstd::SharedPtr client, const class Message& msg); void handleNotice(unstd::SharedPtr client, const class Message& msg); + void handleWho(unstd::SharedPtr client, const class Message& msg); void handleKick(unstd::SharedPtr client, const class Message& msg); void handleTopic(unstd::SharedPtr client, const class Message& msg); void handlePing(unstd::SharedPtr client, const class Message& msg); diff --git a/srcs/channel.cpp b/srcs/channel.cpp index ed760e8..700f36d 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:25:27 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 21:15:06 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -58,14 +58,28 @@ namespace irc } bool Channel::removeClient(unstd::SharedPtr client) { - if(!_clients.erase(client)) + if (!_clients.erase(client)) return (false); + if (isOp(client)) + _operators.erase(client); 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); } + void Channel::sendWho(unstd::SharedPtr client) + { + std::string clientlist(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()); + } + void Channel::handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice) const { const std::string sender = client ? client->getNickName() : ""; @@ -189,6 +203,8 @@ namespace irc } for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(op->getNickName(), "KICK " + _name + ' ' + target->getNickName(), reason); + if (isOp(target)) + _operators.erase(target); _clients.erase(target); return true; } diff --git a/srcs/client.cpp b/srcs/client.cpp index 2b3b2a2..53013aa 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 18:42:31 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 20:24:16 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -28,13 +28,14 @@ namespace irc void Client::sendCode(const std::string& code, const std::string& msg) { const std::string command = code + " :" + msg; - //logs::report(log_message, "C<-%s", command.c_str()); + logs::report(log_message, "C<-%s", command.c_str()); if(send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str()); } void Client::sendPlainText(const std::string& str) { + logs::report(log_message,"<- S %s", str.c_str()); if(send(_fd, str.c_str(), str.length(), 0) != static_cast(str.length())) logs::report(log_error, "server failed to send a message to '%s' (:sadge:)", _username.c_str()); } @@ -42,7 +43,7 @@ namespace irc void Client::sendMsg(const std::string& sender, const std::string& cmd, const std::string& trailing) { const std::string out = ':' + sender + ' ' + cmd + " :" + trailing + "\r\n"; - //logs::report(log_message,"<- S %s", out.c_str()); + logs::report(log_message,"<- S %s", out.c_str()); if(send(_fd, out.c_str(), out.length(), 0) != static_cast(out.length())) logs::report(log_error, "server failed to send a message from '%s' to '%s' (:sadge:)", sender.c_str(), _username.c_str()); } @@ -56,6 +57,7 @@ namespace irc 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()); } diff --git a/srcs/server.cpp b/srcs/server.cpp index 5282b76..fba9c98 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 18:32:27 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 18:40:10 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -143,6 +143,8 @@ namespace irc return true; else if(msg.getCmd() == "QUIT") handleQuit(client, msg); + else if(msg.getCmd() == "WHO") + handleWho(client, msg); else if(msg.getCmd() == "PART") handlePart(client, msg); else if(msg.getCmd() == "JOIN") diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 9d661d2..b208fb7 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:15:16 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 20:28:19 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -344,6 +344,17 @@ namespace irc } } + void Server::handleWho(unstd::SharedPtr client, const class Message& msg) + { + irc::Channel *chan; + + logs::report(log_message, "tokensize ok : %d", msg.getTokens().size()); + if (msg.getTokens().size() != 2) + return; + if ((chan = getChannelByName(msg.getTokens()[1])) == NULL) + client->sendCode(ERR_NOSUCHCHANNEL, "No such channel"); + chan->sendWho(client); + } void Server::handleTopic(unstd::SharedPtr client, const Message& msg) { (void)client;