This commit is contained in:
2024-01-30 02:40:37 +01:00
parent 540ebf47b6
commit bbbc4dac16
12 changed files with 233 additions and 39 deletions

19
srcs_bonus/ansi.cpp Normal file
View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ansi.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 19:08:31 by maldavid #+# #+# */
/* Updated: 2024/01/23 09:38:17 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <ansi.hpp>
#include <unstd/string.hpp>
std::ostream& operator<<(std::ostream& os, const AnsiColor::Codes code)
{
return os << "\033[1;" << unstd::toString(static_cast<int>(code)) << "m";
}

54
srcs_bonus/bot.cpp Normal file
View File

@@ -0,0 +1,54 @@
/******************************************************************************/
/* */
/* ::: :::::::: */
/* Bot.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:54:56 by vvaas #+# #+# */
/* Updated: 2024/01/30 02:40:17 by vvaas ### ########.fr */
/* */
/******************************************************************************/
#include <bot.hpp>
#include <logs.hpp>
#include <string>
#include <iostream>
#include <errno.h>
bot::bot() {}
bot::~bot() {}
void bot::init()
{
_connect_commands.push_back("USER greg_bot 0 * :botrealname\r\n");
_connect_commands.push_back("NICK greg\r\n");
_connect_commands.push_back("PASS " PASSWORD "\r\n");
_fd = socket(AF_INET, SOCK_STREAM, 0);
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)
{
irc::logs::report(irc::log_fatal_error, "connect error");
}
}
void bot::connect_to_server()
{
char buffer[1024];
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)
irc::logs::report(irc::log_fatal_error, "send error");
}
while (true)
{
while (recv(_fd, buffer, 1024, 0) < 0)
std::cout << buffer << std::endl;
}
}

45
srcs_bonus/logs.cpp Normal file
View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* logs.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:17:47 by maldavid #+# #+# */
/* Updated: 2024/01/22 09:46:54 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <logs.hpp>
#include <ansi.hpp>
#include <cstdarg>
#include <cstdlib>
#include <cstdio>
#include <config.hpp>
namespace irc
{
namespace logs
{
void report(LogType type, std::string message, ...)
{
char buffer[LOGS_BUFFER_SIZE];
va_list al;
va_start(al, message);
vsnprintf(buffer, LOGS_BUFFER_SIZE, message.c_str(), al);
va_end(al);
switch(type)
{
case log_message: std::cout << AnsiColor::blue << "[IRC serv] Message : " << AnsiColor::reset << buffer << std::endl; break;
case log_warning: std::cout << AnsiColor::magenta << "[IRC serv] Warning : " << AnsiColor::reset << buffer << std::endl; break;
case log_error: std::cerr << AnsiColor::red << "[IRC serv] Error : " << AnsiColor::reset << buffer << std::endl; break;
case log_fatal_error:
std::cerr << AnsiColor::red << "[IRC serv] Fatal Error : " << AnsiColor::reset << buffer << std::endl;
std::exit(EXIT_FAILURE);
break;
}
}
}
}

View File

@@ -6,26 +6,16 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:36:23 by vvaas #+# #+# */
/* Updated: 2024/01/29 20:41:45 by vvaas ### ########.fr */
/* Updated: 2024/01/30 02:40:04 by vvaas ### ########.fr */
/* */
/******************************************************************************/
#include <irc.hpp>
#include <logs.hpp>
#include <bot.hpp>
#define IP "127.0.0.1"
#define PORT 6667
int main()
{
struct sockaddr_in serv_addr;
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd == -1)
irc::logs::report(irc::log_fatal_error, "FD error");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (connect(fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
irc::logs::report(irc::log_fatal_error, "connect error");
return -1;
}
bot greg;
greg.init();
greg.connect_to_server();
}