From 24462adbd54e2a5530a64cf26319f961bec9650e Mon Sep 17 00:00:00 2001 From: Namonay Date: Tue, 6 Feb 2024 11:53:25 +0100 Subject: [PATCH] gg --- includes/Bot.hpp | 6 +++--- includes/channel.hpp | 2 +- includes/client.hpp | 2 +- includes/config.hpp | 2 +- srcs/channel.cpp | 8 ++++---- srcs/client.cpp | 14 +++++++------- srcs/server.cpp | 11 ++++++++--- srcs/server_functions.cpp | 2 +- srcs_bonus/bot.cpp | 23 ++++++++++++++--------- srcs_bonus/main.cpp | 9 ++++++--- 10 files changed, 46 insertions(+), 33 deletions(-) diff --git a/includes/Bot.hpp b/includes/Bot.hpp index 456a6bc..c3b2337 100644 --- a/includes/Bot.hpp +++ b/includes/Bot.hpp @@ -6,13 +6,13 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/30 01:54:36 by vvaas #+# #+# */ -/* Updated: 2024/01/30 21:38:16 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:52:46 by vvaas ### ########.fr */ /* */ /******************************************************************************/ #define IP "127.0.0.1" #define PORT 6667 -#define PASSWORD "gg" +#define PASSWORD "passwordIRC" #include #include @@ -22,7 +22,7 @@ class Bot { public: Bot(); - bool init(void); + bool init(const std::string &ip, const std::string &port, const std::string &password); void connect_to_server(void); void handle_response(std::string buffer); void send_message(const std::string &content); diff --git a/includes/channel.hpp b/includes/channel.hpp index 158fff8..7a3afb7 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/02/05 12:45:08 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:37:58 by vvaas ### ########.fr */ /* */ /******************************************************************************/ diff --git a/includes/client.hpp b/includes/client.hpp index d4d9897..03980de 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/02/06 10:08:14 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:38:02 by vvaas ### ########.fr */ /* */ /******************************************************************************/ diff --git a/includes/config.hpp b/includes/config.hpp index dc80f08..8e0e6f0 100644 --- a/includes/config.hpp +++ b/includes/config.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 09:45:10 by maldavid #+# #+# */ -/* Updated: 2024/02/05 16:13:06 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:38:01 by vvaas ### ########.fr */ /* */ /******************************************************************************/ diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 582fa93..c73e147 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/02/06 10:33:44 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:37:55 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -93,7 +93,7 @@ namespace irc void Channel::sendWho(unstd::SharedPtr client) { - std::string clientlist(":yipirc " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :"); + std::string clientlist(":YipIRC " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :"); for(client_it it = _clients.begin(); it != _clients.end(); ++it) { @@ -102,8 +102,8 @@ namespace irc clientlist += const_cast&>(*it)->getNickName() + ' '; } client->sendModular("%s", clientlist.c_str()); - clientlist = ":yipirc " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :"; - client->sendModular(":yipirc %s %s %s : End of names list", RPL_ENDOFNAMES, client->getNickName().c_str(), getName().c_str()); + clientlist = ":YipIRC " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :"; + client->sendModular(":YipIRC %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 diff --git a/srcs/client.cpp b/srcs/client.cpp index 2fdf0f7..3fedce0 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/02/06 10:53:59 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:40:41 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -31,7 +31,7 @@ namespace irc void Client::sendCode(const std::string& code, const std::string& msg) { - const std::string command = ":yipirc " + code + " " + getNickName() + " :" + msg + "\r\n"; + const std::string command = ":YipIRC " + code + " " + getNickName() + " :" + msg + "\r\n"; #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif @@ -41,7 +41,7 @@ namespace irc void Client::sendCode(const std::string& code, const std::string& msg0, const std::string& msg1) { - const std::string command = ":yipirc " + code + " " + msg0 + " :" + msg1 + "\r\n"; + const std::string command = ":YipIRC " + code + " " + msg0 + " :" + msg1 + "\r\n"; #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif @@ -51,7 +51,7 @@ namespace irc void Client::sendCodeInChannel(const std::string& code, const Channel &chan, const std::string& msg) { - const std::string command = ":yipirc " + code + " " + getNickName() + " " + chan.getName() + " :" + msg + "\r\n"; + const std::string command = ":YipIRC " + code + " " + getNickName() + " " + chan.getName() + " :" + msg + "\r\n"; #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif @@ -156,16 +156,16 @@ namespace irc tosend = "This server is running since "; tosend += server.getRunDate(); sendCode(RPL_CREATED, tosend); - sendModular("%s %s %s %s %s %s %s %s", ":yipirc", RPL_MYINFO, getNickName().c_str(), "YipIRC", "1.0", "o", "tikl", "kl"); + sendModular("%s %s %s %s %s %s %s %s", ":YipIRC", RPL_MYINFO, getNickName().c_str(), "YipIRC", "1.0", "o", "tikl", "kl"); sendModular("There are %u users on %u channels", server.getClientCount(), server.getChannelCount()); sendModular("You are connected using %s", proto->p_name); - sendCode(RPL_WELCOME, "Welcome to yipirc :), your nickname is : " + _nickname); + sendCode(RPL_WELCOME, "Welcome to YipIRC 😀, your nickname is : " + _nickname); } void Client::kill(const std::string& reason) { std::string cmd = "KILL " + getNickName(); - sendMsg("yipirc", cmd, reason); + sendMsg("YipIRC", cmd, reason); } Client::~Client() {} diff --git a/srcs/server.cpp b/srcs/server.cpp index a898f9b..101d765 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/02/06 10:22:58 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:37:58 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -35,8 +35,6 @@ namespace irc struct tm tstruct = *localtime(<ime); char buf[100]; - strftime(buf, 100, "%a %b %d %Y %T CET", &tstruct); - _run_date = buf; if (password.empty() || password.find_first_of(" \t\r\v") != std::string::npos) { logs::report(log_error, "Password is invalid !"); @@ -44,6 +42,8 @@ namespace irc } std::memset(&_s_data, 0, sizeof(sockaddr)); initSocket(); + strftime(buf, 100, "%a %b %d %Y %T CET", &tstruct); + _run_date = buf; } void Server::initSocketData() @@ -238,6 +238,11 @@ namespace irc Server::~Server() { + for (client_it it = _client.begin(); it != _client.end(); ++it) + { + if ((*it)->isWelcomed()) + (*it)->kill("Server shutting down"); + } closeMainSocket(); for(int i = 0; i < FD_MAX; ++i) { diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 5fd376b..2a2df72 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/02/06 10:59:29 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:38:00 by vvaas ### ########.fr */ /* */ /******************************************************************************/ diff --git a/srcs_bonus/bot.cpp b/srcs_bonus/bot.cpp index d284cd7..9a7013a 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 21:45:18 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:53:03 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -19,7 +19,7 @@ #include #include #include - +#include bool active = true; Bot::Bot() : _channel_created(false), _logged(false), _fd(-1) @@ -39,9 +39,16 @@ void signalsHandler(int foo) active = false; } -bool Bot::init() +bool Bot::init(const std::string &ip, const std::string &port, const std::string &password) { - _connect_commands.push_back("PASS " PASSWORD "\r\n"); + if (ip.empty() || port.empty() || password.empty()) + irc::logs::report(irc::log_fatal_error, "An argument is empty"); + + char* end; + int portval = std::strtol(port.c_str(), &end, 10); + if (errno == ERANGE || *end != 0 || portval < 0 || portval > 0xFFFF) + irc::logs::report(irc::log_fatal_error, "invalid port"); + _connect_commands.push_back("PASS " + password + "\r\n"); _connect_commands.push_back("NICK greg\r\n"); _connect_commands.push_back("USER greg_Bot 0 * :Botrealname\r\n"); _connect_commands.push_back("JOIN #greg\r\n"); @@ -49,8 +56,8 @@ bool Bot::init() 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); + _serv_addr.sin_port = htons(portval); + _serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); if(connect(_fd, (struct sockaddr*)&_serv_addr, sizeof(_serv_addr)) < 0) { irc::logs::report(irc::log_error, "connect error"); @@ -74,9 +81,7 @@ void Bot::send_message(const std::string &content) 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 😀, your nickname is : greg\r\n") { _logged = true; irc::logs::report(irc::log_message, "Logged in succesfully"); diff --git a/srcs_bonus/main.cpp b/srcs_bonus/main.cpp index 371f34d..f277fda 100644 --- a/srcs_bonus/main.cpp +++ b/srcs_bonus/main.cpp @@ -6,17 +6,20 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:36:23 by vvaas #+# #+# */ -/* Updated: 2024/01/30 21:39:43 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 11:47:20 by vvaas ### ########.fr */ /* */ /******************************************************************************/ #include +#include -int main() +int main(int argc, char **argv) { + if (argc != 4) + irc::logs::report(irc::log_fatal_error, "./greg "); Bot greg; - if (!greg.init()) + if (!greg.init(argv[1], argv[2], argv[3])) return 1; greg.connect_to_server(); return 0;