diff --git a/includes/bot.hpp b/includes/Bot.hpp similarity index 84% rename from includes/bot.hpp rename to includes/Bot.hpp index d59aca2..7c6dc02 100644 --- a/includes/bot.hpp +++ b/includes/Bot.hpp @@ -1,12 +1,12 @@ /******************************************************************************/ /* */ /* ::: :::::::: */ -/* bot.hpp :+: :+: :+: */ +/* Bot.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/30 01:54:36 by vvaas #+# #+# */ -/* Updated: 2024/01/30 17:24:08 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 18:13:55 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -18,14 +18,15 @@ #include #include -class bot +class Bot { public: - bot(); + Bot(); void init(void); void connect_to_server(void); void handle_response(std::string buffer); - ~bot(); + void send_message(const std::string &content); + ~Bot(); private : bool _channel_created; bool _logged; diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 7cc9822..07cb835 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 17:33:40 by maldavid ### ########.fr */ +/* Updated: 2024/01/30 17:39:44 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -228,14 +228,14 @@ namespace irc if(_topic.empty()) { if(!broadcast) - return client->sendCode(":yipirc " RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set"); + return client->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set"); for(client_it it = _clients.begin(); it != _clients.end(); ++ it) - const_cast&>(*it)->sendCode(":yipirc " RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set"); + const_cast&>(*it)->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set"); } if(!broadcast) - return client->sendCode(":yipirc " RPL_TOPIC, client->getNickName() + " " + _name, _topic); + return client->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic); for(client_it it = _clients.begin(); it != _clients.end(); ++ it) - const_cast&>(*it)->sendCode(":yipirc " RPL_TOPIC, client->getNickName() + " " + _name, _topic); + const_cast&>(*it)->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic); } bool Channel::isOp(unstd::SharedPtr client) const diff --git a/srcs/client.cpp b/srcs/client.cpp index b2c2e14..288bef4 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/30 01:41:49 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 17:48:31 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -139,7 +139,8 @@ namespace irc void Client::kill(const std::string& reason) { - sendMsg("yipirc", "KILL", reason); + std::string cmd = "KILL " + getNickName(); + sendMsg("yipirc", cmd, reason); } Client::~Client() {} diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 1071c7d..5f9f1ab 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 17:35:15 by maldavid ### ########.fr */ +/* Updated: 2024/01/30 18:47:38 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -51,7 +51,8 @@ namespace irc { if ((*it)->getNickName() == nickname) { - client->sendCode(ERR_NICKCOLLISION, nickname + " Nickname is used"); + client->setNewNickName(nickname); + client->sendCode(ERR_NICKCOLLISION, "Nickname is used"); client->kill("Nickname already used"); client->requireDisconnect(); return; @@ -120,9 +121,7 @@ namespace irc { (void)msg; for (channel_it it = _channels.begin(); it != _channels.end(); ++it) - { it->removeClient(client); - } client->printUserHeader(); std::cout << "quit" << std::endl; } diff --git a/srcs_bonus/bot.cpp b/srcs_bonus/bot.cpp index 582a39b..a995cdc 100644 --- a/srcs_bonus/bot.cpp +++ b/srcs_bonus/bot.cpp @@ -6,28 +6,33 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/30 01:54:56 by vvaas #+# #+# */ -/* Updated: 2024/01/30 17:26:11 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 18:34:07 by vvaas ### ########.fr */ /* */ /******************************************************************************/ -#include +#include #include #include #include #include #include #include +#include -bot::bot() : _channel_created(false), _logged(false) +Bot::Bot() : _channel_created(false), _logged(false) {} -bot::~bot() {} +Bot::~Bot() +{ + if (_fd >= 0) + close(_fd); +} -void bot::init() +void Bot::init() { _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("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) @@ -41,9 +46,16 @@ void bot::init() irc::logs::report(irc::log_fatal_error, "fcntl() error"); } -void bot::handle_response(std::string buffer) +void Bot::send_message(const std::string &content) +{ + if (send(_fd, content.c_str(), content.length(), 0) < 0) + irc::logs::report(irc::log_fatal_error, "send error"); +} + +void Bot::handle_response(std::string buffer) { std::cout << buffer << std::flush; + if (!_logged && buffer == ":yipirc 001 greg :Welcome to yipirc :), greg\r\n") { _logged = true; @@ -51,16 +63,23 @@ void bot::handle_response(std::string buffer) } else if (!_logged) return ; - if (!_channel_created && buffer == ":yipirc 353 greg @ #greg :@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) return ; + 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) + send_message("PRIVMSG #greg :feur\r\n"); } -void bot::connect_to_server() +void Bot::connect_to_server() { char buffer[1024]; for (std::vector::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it) diff --git a/srcs_bonus/main.cpp b/srcs_bonus/main.cpp index a51b74f..0883c37 100644 --- a/srcs_bonus/main.cpp +++ b/srcs_bonus/main.cpp @@ -6,15 +6,15 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:36:23 by vvaas #+# #+# */ -/* Updated: 2024/01/30 02:40:04 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 17:58:07 by vvaas ### ########.fr */ /* */ /******************************************************************************/ -#include +#include int main() { - bot greg; + Bot greg; greg.init(); greg.connect_to_server();