This commit is contained in:
2024-01-30 18:47:43 +01:00
parent b90087188f
commit e67a6141d2
6 changed files with 48 additions and 28 deletions

View File

@@ -1,12 +1,12 @@
/******************************************************************************/
/* */
/* ::: :::::::: */
/* bot.hpp :+: :+: :+: */
/* Bot.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <vector>
#include <string>
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;

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(":yipirc " RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set");
const_cast<unstd::SharedPtr<irc::Client>&>(*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<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(":yipirc " RPL_TOPIC, client->getNickName() + " " + _name, _topic);
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic);
}
bool Channel::isOp(unstd::SharedPtr<Client> client) const

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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() {}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

View File

@@ -6,28 +6,33 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <bot.hpp>
#include <Bot.hpp>
#include <logs.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <cstdlib>
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<std::string>::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it)

View File

@@ -6,15 +6,15 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <bot.hpp>
#include <Bot.hpp>
int main()
{
bot greg;
Bot greg;
greg.init();
greg.connect_to_server();