This commit is contained in:
2024-02-06 11:53:25 +01:00
parent 7555a6427a
commit 24462adbd5
10 changed files with 46 additions and 33 deletions

View File

@@ -6,13 +6,13 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:54:36 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 IP "127.0.0.1"
#define PORT 6667 #define PORT 6667
#define PASSWORD "gg" #define PASSWORD "passwordIRC"
#include <irc.hpp> #include <irc.hpp>
#include <vector> #include <vector>
@@ -22,7 +22,7 @@ class Bot
{ {
public: public:
Bot(); Bot();
bool init(void); bool init(const std::string &ip, const std::string &port, const std::string &password);
void connect_to_server(void); void connect_to_server(void);
void handle_response(std::string buffer); void handle_response(std::string buffer);
void send_message(const std::string &content); void send_message(const std::string &content);

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */ /* 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 */
/* */ /* */
/******************************************************************************/ /******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:33:17 by maldavid #+# #+# */ /* 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 */
/* */ /* */
/******************************************************************************/ /******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 09:45:10 by maldavid #+# #+# */ /* 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 */
/* */ /* */
/******************************************************************************/ /******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ /* 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> client) void Channel::sendWho(unstd::SharedPtr<Client> 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) for(client_it it = _clients.begin(); it != _clients.end(); ++it)
{ {
@@ -102,8 +102,8 @@ namespace irc
clientlist += const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() + ' '; clientlist += const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() + ' ';
} }
client->sendModular("%s", clientlist.c_str()); client->sendModular("%s", clientlist.c_str());
clientlist = ":yipirc " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :"; 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()); 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> client, bool notice) const void Channel::handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice) const

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:35:52 by maldavid #+# #+# */ /* 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) 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 #ifdef DEBUG
logs::report(log_message, "sending '%s'", command.c_str()); logs::report(log_message, "sending '%s'", command.c_str());
#endif #endif
@@ -41,7 +41,7 @@ namespace irc
void Client::sendCode(const std::string& code, const std::string& msg0, const std::string& msg1) 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 #ifdef DEBUG
logs::report(log_message, "sending '%s'", command.c_str()); logs::report(log_message, "sending '%s'", command.c_str());
#endif #endif
@@ -51,7 +51,7 @@ namespace irc
void Client::sendCodeInChannel(const std::string& code, const Channel &chan, const std::string& msg) 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 #ifdef DEBUG
logs::report(log_message, "sending '%s'", command.c_str()); logs::report(log_message, "sending '%s'", command.c_str());
#endif #endif
@@ -156,16 +156,16 @@ namespace irc
tosend = "This server is running since "; tosend = "This server is running since ";
tosend += server.getRunDate(); tosend += server.getRunDate();
sendCode(RPL_CREATED, tosend); 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("There are %u users on %u channels", server.getClientCount(), server.getChannelCount());
sendModular("You are connected using %s", proto->p_name); 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) void Client::kill(const std::string& reason)
{ {
std::string cmd = "KILL " + getNickName(); std::string cmd = "KILL " + getNickName();
sendMsg("yipirc", cmd, reason); sendMsg("YipIRC", cmd, reason);
} }
Client::~Client() {} Client::~Client() {}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ /* 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(&ltime); struct tm tstruct = *localtime(&ltime);
char buf[100]; 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) if (password.empty() || password.find_first_of(" \t\r\v") != std::string::npos)
{ {
logs::report(log_error, "Password is invalid !"); logs::report(log_error, "Password is invalid !");
@@ -44,6 +42,8 @@ namespace irc
} }
std::memset(&_s_data, 0, sizeof(sockaddr)); std::memset(&_s_data, 0, sizeof(sockaddr));
initSocket(); initSocket();
strftime(buf, 100, "%a %b %d %Y %T CET", &tstruct);
_run_date = buf;
} }
void Server::initSocketData() void Server::initSocketData()
@@ -238,6 +238,11 @@ namespace irc
Server::~Server() Server::~Server()
{ {
for (client_it it = _client.begin(); it != _client.end(); ++it)
{
if ((*it)->isWelcomed())
(*it)->kill("Server shutting down");
}
closeMainSocket(); closeMainSocket();
for(int i = 0; i < FD_MAX; ++i) for(int i = 0; i < FD_MAX; ++i)
{ {

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */ /* 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 */
/* */ /* */
/******************************************************************************/ /******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:54:56 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 <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <signal.h> #include <signal.h>
#include <cerrno>
bool active = true; bool active = true;
Bot::Bot() : _channel_created(false), _logged(false), _fd(-1) Bot::Bot() : _channel_created(false), _logged(false), _fd(-1)
@@ -39,9 +39,16 @@ void signalsHandler(int foo)
active = false; 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("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"); _connect_commands.push_back("JOIN #greg\r\n");
@@ -49,8 +56,8 @@ bool Bot::init()
if(_fd == -1) if(_fd == -1)
irc::logs::report(irc::log_fatal_error, "FD error"); irc::logs::report(irc::log_fatal_error, "FD error");
_serv_addr.sin_family = AF_INET; _serv_addr.sin_family = AF_INET;
_serv_addr.sin_port = htons(PORT); _serv_addr.sin_port = htons(portval);
_serv_addr.sin_addr.s_addr = inet_addr(IP); _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"); 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) void Bot::handle_response(std::string buffer)
{ {
std::cout << buffer << std::flush; if(!_logged && buffer == ":YipIRC 001 greg :Welcome to YipIRC 😀, your nickname is : greg\r\n")
if(!_logged && buffer == ":yipirc 001 greg :Welcome to yipirc :), greg\r\n")
{ {
_logged = true; _logged = true;
irc::logs::report(irc::log_message, "Logged in succesfully"); irc::logs::report(irc::log_message, "Logged in succesfully");

View File

@@ -6,17 +6,20 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:36:23 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 <Bot.hpp> #include <Bot.hpp>
#include <logs.hpp>
int main() int main(int argc, char **argv)
{ {
if (argc != 4)
irc::logs::report(irc::log_fatal_error, "./greg <ip address> <port> <password>");
Bot greg; Bot greg;
if (!greg.init()) if (!greg.init(argv[1], argv[2], argv[3]))
return 1; return 1;
greg.connect_to_server(); greg.connect_to_server();
return 0; return 0;