From 79b3a8ed26660bda191fc2e7faa6cb7e6afce6e8 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 30 Jan 2024 20:17:08 +0100 Subject: [PATCH] c est metaphysique --- includes/channel.hpp | 4 ++-- srcs/channel.cpp | 18 ++++++++++++------ srcs/server_functions.cpp | 5 ++--- srcs_bonus/bot.cpp | 32 ++++++++++++++++---------------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/includes/channel.hpp b/includes/channel.hpp index 33d36bf..1663ada 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/30 17:23:28 by maldavid ### ########.fr */ +/* Updated: 2024/01/30 20:14:08 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -38,7 +38,7 @@ namespace irc inline const std::string& getPassword() const { return _password; } void addClient(unstd::SharedPtr client, bool op = false); - bool removeClient(unstd::SharedPtr client); + bool removeClient(unstd::SharedPtr client, std::string reason = ""); inline std::size_t getNumberOfClients() const { return _clients.size(); } inline int getChannelSize() const { return _channel_size; } diff --git a/srcs/channel.cpp b/srcs/channel.cpp index e7560e7..bfb76ac 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/30 18:48:32 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 20:15:29 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -71,16 +71,22 @@ namespace irc client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel"); } - bool Channel::removeClient(unstd::SharedPtr client) + bool Channel::removeClient(unstd::SharedPtr client, std::string reason) { if(!_clients.erase(client)) - return (false); + 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); + { + if(reason.empty()) + const_cast&>(*it)->sendMsg(client->getNickName(), "PART", _name); + else + const_cast&>(*it)->sendMsg(client->getNickName(), "QUIT", reason); + } + if(reason.empty()) + client->sendMsg(client->getNickName(), "PART", _name); + return true; } void Channel::sendWho(unstd::SharedPtr client) diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index b5be1a2..03baa77 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/30 18:50:47 by maldavid ### ########.fr */ +/* Updated: 2024/01/30 20:13:48 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -119,9 +119,8 @@ namespace irc void Server::handleQuit(unstd::SharedPtr client, const Message& msg) { - (void)msg; for(channel_it it = _channels.begin(); it != _channels.end(); ++it) - it->removeClient(client); + it->removeClient(client, msg.getReason()); client->printUserHeader(); std::cout << "quit" << std::endl; } diff --git a/srcs_bonus/bot.cpp b/srcs_bonus/bot.cpp index a995cdc..bcace13 100644 --- a/srcs_bonus/bot.cpp +++ b/srcs_bonus/bot.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/30 01:54:56 by vvaas #+# #+# */ -/* Updated: 2024/01/30 18:34:07 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 18:52:24 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -24,7 +24,7 @@ Bot::Bot() : _channel_created(false), _logged(false) Bot::~Bot() { - if (_fd >= 0) + if(_fd >= 0) close(_fd); } @@ -35,12 +35,12 @@ void Bot::init() _connect_commands.push_back("USER greg_Bot 0 * :Botrealname\r\n"); _connect_commands.push_back("JOIN #greg\r\n"); _fd = socket(AF_INET, SOCK_STREAM, 0); - if (_fd == -1) + if(_fd == -1) irc::logs::report(irc::log_fatal_error, "FD error"); _serv_addr.sin_family = AF_INET; _serv_addr.sin_port = htons(PORT); _serv_addr.sin_addr.s_addr = inet_addr(IP); - if (connect(_fd, (struct sockaddr*)&_serv_addr, sizeof(_serv_addr)) < 0) + if(connect(_fd, (struct sockaddr*)&_serv_addr, sizeof(_serv_addr)) < 0) irc::logs::report(irc::log_fatal_error, "connect error"); if(fcntl(_fd, F_SETFL, O_NONBLOCK) < 0) irc::logs::report(irc::log_fatal_error, "fcntl() error"); @@ -48,7 +48,7 @@ void Bot::init() void Bot::send_message(const std::string &content) { - if (send(_fd, content.c_str(), content.length(), 0) < 0) + if(send(_fd, content.c_str(), content.length(), 0) < 0) irc::logs::report(irc::log_fatal_error, "send error"); } @@ -56,43 +56,43 @@ void Bot::handle_response(std::string buffer) { std::cout << buffer << std::flush; - if (!_logged && buffer == ":yipirc 001 greg :Welcome to yipirc :), greg\r\n") + if(!_logged && buffer == ":yipirc 001 greg :Welcome to yipirc :), greg\r\n") { _logged = true; irc::logs::report(irc::log_message, "Logged in succesfully"); } - else if (!_logged) + else if(!_logged) return ; - if (!_channel_created && buffer == ":greg JOIN :#greg\r\n") + if(!_channel_created && buffer == ":greg JOIN :#greg\r\n") { _channel_created = true; irc::logs::report(irc::log_message, "Created the channel succesfully"); } - else if (!_channel_created) + else if(!_channel_created) return ; - if (buffer.find("KICK #greg greg :") != std::string::npos || buffer.find("explose") != std::string::npos) + if(buffer.find("KICK #greg greg :") != std::string::npos || buffer.find("explose") != std::string::npos) { send_message("QUIT: Explose\r\n"); std::exit(0); } - if (buffer.find("quoi") != std::string::npos) + if(buffer.find("quoi") != std::string::npos) send_message("PRIVMSG #greg :feur\r\n"); } void Bot::connect_to_server() { char buffer[1024]; - for (std::vector::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it) + for(std::vector::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it) { - if (send(_fd, (*it).c_str(), (*it).size(), 0) < 0) + if(send(_fd, (*it).c_str(), (*it).size(), 0) < 0) irc::logs::report(irc::log_fatal_error, "send error"); - if (recv(_fd, buffer, 1024, 0) > 0) + if(recv(_fd, buffer, 1024, 0) > 0) handle_response(buffer); } while (true) { - if (recv(_fd, buffer, 1024, 0) > 0) + if(recv(_fd, buffer, 1024, 0) > 0) handle_response(buffer); std::memset(buffer, 0, sizeof(buffer)); } -} \ No newline at end of file +}