fixed david

This commit is contained in:
2024-02-06 12:36:27 +01:00
parent e46bc4e602
commit 160c02fc75
23 changed files with 360 additions and 359 deletions

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:54:56 by vvaas #+# #+# */
/* Updated: 2024/02/06 11:53:03 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -20,6 +20,7 @@
#include <cstdlib>
#include <signal.h>
#include <cerrno>
bool active = true;
Bot::Bot() : _channel_created(false), _logged(false), _fd(-1)
@@ -27,7 +28,7 @@ Bot::Bot() : _channel_created(false), _logged(false), _fd(-1)
Bot::~Bot()
{
if(_fd >= 0)
if (_fd >= 0)
close(_fd);
}
@@ -53,17 +54,17 @@ bool Bot::init(const std::string &ip, const std::string &port, const std::string
_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(portval);
_serv_addr.sin_addr.s_addr = inet_addr(ip.c_str());
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_error, "connect error");
return (false);
}
if(fcntl(_fd, F_SETFL, O_NONBLOCK) < 0)
if (fcntl(_fd, F_SETFL, O_NONBLOCK) < 0)
{
irc::logs::report(irc::log_error, "fcntl error");
return (false);
@@ -75,48 +76,48 @@ bool Bot::init(const std::string &ip, const std::string &port, const std::string
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");
}
void Bot::handle_response(std::string buffer)
{
if(!_logged && buffer == ":YipIRC 001 greg :Welcome to YipIRC 😀, your nickname is : 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");
}
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<std::string>::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it)
for (std::vector<std::string>::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 (active)
{
if(recv(_fd, buffer, 1024, 0) > 0)
if (recv(_fd, buffer, 1024, 0) > 0)
handle_response(buffer);
std::memset(buffer, 0, sizeof(buffer));
}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:36:23 by vvaas #+# #+# */
/* Updated: 2024/02/06 11:47:20 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/******************************************************************************/