need fix kick from bot

This commit is contained in:
Samy Ben Tayeb 2025-06-21 19:25:31 +02:00
parent 6716df3af4
commit 3af74931c7
11 changed files with 115 additions and 24 deletions

18
include/bonus.hpp Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bonus.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/21 12:09:57 by sben-tay #+# #+# */
/* Updated: 2025/06/21 18:55:38 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BONUS_HPP
# define BONUS_HPP
// # define BONUS 0
#endif

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */
/* Updated: 2025/06/19 13:42:03 by sben-tay ### ########.fr */
/* Updated: 2025/06/21 14:36:38 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@
class Channel {
public:
Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite);
~Channel();
// getters
std::string getName() const;
std::string getTopic() const;
@ -47,6 +47,8 @@ class Channel {
bool isInvited(User *user) const;
void removeUser(User *user);
void removeOperator(User *user);
void setBotChannel(bool isBot);
bool getBotChannel() const;
// utility functions
void sendAllClientInAChannel(const std::string &toSend, User *exclude = NULL);
@ -62,6 +64,7 @@ class Channel {
std::list<User *> _operators;
std::list<User *> _users;
std::list<User *> _invited;
bool _botChannel;
};

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */
/* Updated: 2025/06/19 13:00:30 by rparodi ### ########.fr */
/* Updated: 2025/06/21 17:53:03 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */
/* Updated: 2025/06/20 19:17:53 by sben-tay ### ########.fr */
/* Updated: 2025/06/21 14:38:06 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include <string>
#include "PollManager.hpp"
class User
{
private:
@ -38,6 +39,7 @@ class User
public:
User(short unsigned fd, PollManager& poll);
User( void ); // default constructor for bot service
short unsigned int getFd() const;
void appendToReadBuffer(const std::string &data);
void appendToWriteBuffer(const std::string &data);
@ -61,10 +63,11 @@ class User
const std::string getRealname(void) const;
const std::string getNickname() const;
const std::string getWriteBuffer() const;
const std::string& getHostname() const;
const std::string& getIpAddress() const;
const std::string getHostname() const;
const std::string getIpAddress() const;
const std::string getName() const;
const std::string getPrefix() const;
const std::string getReadBuffer() const;
bool hasDataToSend() const;
bool getHasPass() const;

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */
/* Updated: 2025/06/19 13:58:39 by rparodi ### ########.fr */
/* Updated: 2025/06/21 19:04:34 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,10 +16,25 @@
Channel::Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite) : _name(name), _owner(owner), _maxUsers(maxUsers), _needInvite(needInvite) {
this->_protectTopic = false;
if (BONUS)
this->_botChannel = true;
else
this->_botChannel = false;
this->_maxUsers = ~0;
this->_topic = "";
}
Channel::~Channel() {
for (std::list<User *>::iterator it = _users.begin(); it != _users.end(); ++it) {
if (*it != _owner) {
delete *it;
}
}
_users.clear();
_operators.clear();
_invited.clear();
}
/**
* @brief Get the name of the channel
*
@ -258,3 +273,7 @@ bool Channel::isInvited(User *user) const {
std::cerr << user->getName() << " is not invited to the channel " << this->_name << std::endl;
return false;
}
void Channel::setBotChannel(bool isBot) { _botChannel = isBot; }
bool Channel::getBotChannel() const { return _botChannel; }

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/19 02:27:50 by sben-tay ### ########.fr */
/* Updated: 2025/06/21 18:51:55 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#include "commands.hpp"
#include "logs.hpp"
#include <list>
#include "bonus.hpp"
using namespace cmd;
@ -65,6 +66,12 @@ void Join::execute() {
for (std::list<User *>::iterator it = _cTarget->getUsers().begin(); it != _cTarget->getUsers().end(); ++it) {
msg353 += (*it)->getNickname() + " ";
}
if (BONUS) {
_cTarget->setBotChannel(true);
std::string msgJoinBot = ":bot!ircbot@localhost JOIN #" + _cTarget->getName() + "\r\n";
_sender->appendToWriteBuffer(msgJoinBot + msgJoin + msg332 + msg353 + "\r\n");
}
else
_sender->appendToWriteBuffer(msgJoin + msg332 + msg353 + "\r\n");
return;
}

View file

@ -6,13 +6,15 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/20 17:01:49 by rparodi ### ########.fr */
/* Updated: 2025/06/21 19:19:30 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
#include "kick.hpp"
#include "commands.hpp"
#include "logs.hpp"
#include "bonus.hpp"
using namespace cmd;
@ -40,7 +42,11 @@ e_code Kick::checkArgs() {
return ERR_CHANOPRIVSNEEDED;
}
_uTarget = searchList(this->_users, _args.at(2));
if (this->_uTarget == NULL) {
if (BONUS && _args.at(2) == "bot" && _cTarget->getBotChannel() == true) {
return _PARSING_OK;
}
else if (this->_uTarget == NULL) {
WARNING_MSG("User not found");
return ERR_NOSUCHNICK;
}
@ -66,8 +72,19 @@ void Kick::execute() {
ERROR_MSG("Invalid arguments for INVITE command (see warning message)");
return;
}
std::cout << "MODE BONUS ?" << BONUS << std::endl;
if (BONUS && _args.at(2) == "bot") {
if (_cTarget->getBotChannel() == true) {
std::cout << "Bot is already in the channel, kicking it" << std::endl;
std::string msgKickBot = ":bot!ircbot@localhost KICK #" + _cTarget->getName() + " bot :Bot kicked from channel\r\n";
this->_sender->appendToWriteBuffer(msgKickBot);
_cTarget->setBotChannel(false);
}
std::cout << "peace" << std::endl;
return;
}
std::string msgPart = ":" + this->_uTarget->getPrefix() + " PART #" + _cTarget->getName() + "\r\n";
std::string msgKick = ":" + this->_uTarget->getPrefix() + " KICK #" + this->_cTarget->getName();
std::string msgKick = ":" + this->_uTarget->getPrefix() + " KICK #" + this->_cTarget->getName() + " " + this->_uTarget->getName() + " :kicked from channel\r\n";
if (_args.size() > 4)
msgKick += " :" + _args.at(4);
msgKick += "\r\n";

View file

@ -6,13 +6,14 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/19 02:28:16 by sben-tay ### ########.fr */
/* Updated: 2025/06/21 18:52:06 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
#include "privmsg.hpp"
#include "commands.hpp"
#include "logs.hpp"
#include "bonus.hpp"
using namespace cmd;
@ -61,12 +62,33 @@ void PrivMsg::execute() {
std::string content = _args.at(2);
std::string msg = ":" + _sender->getPrefix() + " PRIVMSG " + target + " :" + content + "\r\n";
//bonus msgBot
std::string msgBot = ":bot!ircbot@localhost PRIVMSG " + target + " :📜 Liste des commandes disponibles :\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- JOIN #channel → Rejoindre un canal\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- PART #channel → Quitter un canal\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- PRIVMSG <cible> msg → Envoyer un message\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- TOPIC #channel :txt → Voir / modifier le topic\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- MODE #channel +o nick → Ajouter un opérateur\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- INVITE nick #channel → Inviter un utilisateur\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- KICK #channel nick → Éjecter un utilisateur\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- WHO / WHOIS → Infos sur les utilisateurs\r\n";
msgBot += ":bot!ircbot@localhost PRIVMSG " + target + " :- !help → Affiche cette aide\r\n";
// Envoi vers un channel
if (target[0] == '#') {
target.erase(0, 1);
if (_cTarget)
_cTarget->sendAllClientInAChannel(msg, _sender); // Optionnel: évite d'envoyer au sender
_cTarget->sendAllClientInAChannel(msg, _sender);
if (BONUS) {
if (_args.at(2) == "!help") {
std::cout << "BONUS: PING command received, sending PONG" << std::endl;
_cTarget->sendAllClientInAChannel(msgBot);
}
}
}
// Envoi vers un user
else {
if (_uTarget)

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/20 17:55:27 by sben-tay ### ########.fr */
/* Updated: 2025/06/21 14:30:19 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
@ -55,10 +55,10 @@ void Topic::execute() {
if (this->_args.size() == 1) {
if (this->_cTarget->getTopic().empty()) {
std::string msg331 = ":localhost 331 " + this->_sender->getNickname() + " " + this->_cTarget->getName() + " :No topic is set\r\n";
std::string msg331 = ":localhost 331 " + this->_sender->getNickname() + " #" + this->_cTarget->getName() + " :No topic is set\r\n";
this->_sender->appendToWriteBuffer(msg331);
} else {
std::string msg332 = ":localhost 332 " + this->_sender->getNickname() + " " + this->_cTarget->getName() + " :" + this->_cTarget->getTopic() + "\r\n";
std::string msg332 = ":localhost 332 " + this->_sender->getNickname() + " #" + this->_cTarget->getName() + " :" + this->_cTarget->getTopic() + "\r\n";
this->_sender->appendToWriteBuffer(msg332);
}
} else {
@ -70,7 +70,7 @@ void Topic::execute() {
}
this->_cTarget->setTopic(newTopic);
std::string topicMsg = ":" + this->_sender->getPrefix() + " TOPIC " + this->_cTarget->getName() + " :" + newTopic + "\r\n";
std::string topicMsg = ":" + this->_sender->getPrefix() + " TOPIC #" + this->_cTarget->getName() + " :" + newTopic + "\r\n";
this->_cTarget->sendAllClientInAChannel(topicMsg);
}
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* check.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 11:25:04 by rparodi #+# #+# */
/* Updated: 2025/05/13 14:20:47 by rparodi ### ########.fr */
/* Updated: 2025/06/21 18:49:43 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
/* Updated: 2025/06/20 19:18:27 by sben-tay ### ########.fr */
/* Updated: 2025/06/21 14:38:31 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
@ -163,9 +163,11 @@ const std::string User::getPrefix() const { return _nickname + "!" + _username +
const std::string User::getUsername() const { return _username; }
const std::string& User::getHostname() const { return _hostname; }
const std::string User::getHostname() const { return _hostname; }
const std::string& User::getIpAddress() const { return _ipAdress; }
const std::string User::getIpAddress() const { return _ipAdress; }
const std::string User::getReadBuffer() const { return _read_buffer; }
void User::setHostname(const std::string &hostname) { _hostname = hostname; }