Merge pull request #26 from EniumRaphael/raph

🛠️ Patch Notes — ft_irc v1.3.3
This commit is contained in:
Raphaël 2025-06-19 12:16:41 +00:00 committed by GitHub
commit 3da06efaee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 125 additions and 47 deletions

View file

@ -6,7 +6,7 @@
# By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ # # By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# # # Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
# Updated: 2025/06/17 17:06:04 by sben-tay ### ########.fr # # Updated: 2025/06/19 11:29:58 by rparodi ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -39,6 +39,7 @@ SRC = sources/channel/channel.cpp \
sources/commands/ping.cpp \ sources/commands/ping.cpp \
sources/commands/pong.cpp \ sources/commands/pong.cpp \
sources/commands/privmsg.cpp \ sources/commands/privmsg.cpp \
sources/commands/topic.cpp \
sources/commands/userCmd.cpp \ sources/commands/userCmd.cpp \
sources/commands/whois.cpp \ sources/commands/whois.cpp \
sources/commands/whowas.cpp \ sources/commands/whowas.cpp \

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */ /* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */
/* Updated: 2025/06/19 01:15:59 by sben-tay ### ########.fr */ /* Updated: 2025/06/19 13:42:03 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,7 @@ class Channel {
std::string getTopic() const; std::string getTopic() const;
size_t getMaxUsers() const; size_t getMaxUsers() const;
User *getOwner() const; User *getOwner() const;
std::list<User *> getOperators() const; std::list<User *>& getOperators();
std::list<User *>& getUsers(); std::list<User *>& getUsers();
std::list<User *> getInvited() const; std::list<User *> getInvited() const;
std::string getPassword() const; std::string getPassword() const;
@ -43,6 +43,8 @@ class Channel {
void setPassword(const std::string &newPass); void setPassword(const std::string &newPass);
void addOperator(User *user); void addOperator(User *user);
void addUser(User *user); void addUser(User *user);
void addInvited(User *user);
bool isInvited(User *user) const;
void removeUser(User *user); void removeUser(User *user);
void removeOperator(User *user); void removeOperator(User *user);

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */ /* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */
/* Updated: 2025/06/17 23:14:07 by sben-tay ### ########.fr */ /* Updated: 2025/06/19 13:00:30 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@
namespace cmd namespace cmd
{ {
void dispatch(User *user, Channel *channel, Server *server, std::string &line); void dispatch(User *user, Channel *channel, Server *server, std::string &line);
std::vector<std::string> split(std::string &line); std::vector<std::string> split(std::string &line, char sep);
template <typename T> template <typename T>
T searchList(const std::list<T> &list, const std::string &name); T searchList(const std::list<T> &list, const std::string &name);

View file

@ -6,15 +6,19 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */ /* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */
/* Updated: 2025/06/19 01:16:03 by sben-tay ### ########.fr */ /* Updated: 2025/06/19 13:58:39 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "channel.hpp" #include "channel.hpp"
#include <iostream> #include <iostream>
#include <algorithm>
Channel::Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite) Channel::Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite) : _name(name), _owner(owner), _maxUsers(maxUsers), _needInvite(needInvite) {
:_name(name), _owner(owner), _maxUsers(maxUsers), _needInvite(needInvite) {} this->_protectTopic = false;
this->_maxUsers = ~0;
this->_topic = "";
}
/** /**
* @brief Get the name of the channel * @brief Get the name of the channel
@ -57,7 +61,7 @@ std::list<User *>& Channel::getUsers() {
* *
* @return list of Operators in the channel * @return list of Operators in the channel
*/ */
std::list<User *> Channel::getOperators() const { std::list<User *>& Channel::getOperators() {
return this->_operators; return this->_operators;
} }
@ -239,3 +243,18 @@ void Channel::sendAllClientInAChannel(const std::string &toSend, User *exclude)
(*it)->appendToWriteBuffer(toSend); (*it)->appendToWriteBuffer(toSend);
} }
} }
void Channel::addInvited(User *user) {
if (user && !isInvited(user))
_invited.push_back(user);
}
bool Channel::isInvited(User *user) const {
if (user)
{
if (std::find(_invited.begin(), _invited.end(), user) != _invited.end())
return true;
}
std::cerr << user->getName() << " is not invited to the channel " << this->_name << std::endl;
return false;
}

View file

@ -6,25 +6,26 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 16:11:56 by rparodi #+# #+# */ /* Created: 2025/05/24 16:11:56 by rparodi #+# #+# */
/* Updated: 2025/06/10 16:25:57 by rparodi ### ########.fr */ /* Updated: 2025/06/19 13:02:11 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "commands.hpp" #include "commands.hpp"
#include <cctype> #include <cctype>
#include "join.hpp"
#include "privmsg.hpp"
#include "nick.hpp"
#include "invite.hpp"
#include "list.hpp"
#include "cap.hpp" #include "cap.hpp"
#include "modes.hpp" #include "invite.hpp"
#include "ping.hpp" #include "join.hpp"
#include "notice.hpp"
#include "kick.hpp" #include "kick.hpp"
#include "userCmd.hpp" #include "list.hpp"
#include "pass.hpp" #include "modes.hpp"
#include "nick.hpp"
#include "notice.hpp"
#include "part.hpp" #include "part.hpp"
#include "pass.hpp"
#include "ping.hpp"
#include "privmsg.hpp"
#include "topic.hpp"
#include "userCmd.hpp"
#include "whois.hpp" #include "whois.hpp"
#include "whowas.hpp" #include "whowas.hpp"
#include <iterator> #include <iterator>
@ -37,7 +38,7 @@
* @param line line send by the user * @param line line send by the user
*/ */
std::vector<std::string> cmd::split(std::string &line) { std::vector<std::string> cmd::split(std::string &line, char sep) {
std::vector<std::string> args; std::vector<std::string> args;
size_t start = 0; size_t start = 0;
size_t end; size_t end;
@ -48,7 +49,7 @@ std::vector<std::string> cmd::split(std::string &line) {
args.push_back(arg); args.push_back(arg);
break; break;
} }
end = line.find(' ', start); end = line.find(sep, start);
if (end == std::string::npos) if (end == std::string::npos)
end = line.length(); end = line.length();
std::string arg = line.substr(start, end - start); std::string arg = line.substr(start, end - start);
@ -71,7 +72,7 @@ std::vector<std::string> cmd::split(std::string &line) {
* @param line input line from the user * @param line input line from the user
*/ */
void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &line) { void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &line) {
std::vector<std::string> args = cmd::split(line); std::vector<std::string> args = cmd::split(line, ' ');
if (args.empty()) { if (args.empty()) {
DEBUG_MSG("Empty line"); DEBUG_MSG("Empty line");
return; return;
@ -139,6 +140,11 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &
userCmd(user, channel, server, line).execute(); userCmd(user, channel, server, line).execute();
} }
break; break;
case 't':
if (command_name == "topic") {
Topic(user, channel, server, line).execute();
}
break;
case 'w': case 'w':
if (command_name == "whois") { if (command_name == "whois") {
Whois(user, channel, server, line).execute(); Whois(user, channel, server, line).execute();
@ -157,7 +163,7 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &
cmd::ACommand::ACommand(::User *user, ::Channel *channel, ::Server *server, std::string &line) : _sender(user), _channel(channel), _server(server) { cmd::ACommand::ACommand(::User *user, ::Channel *channel, ::Server *server, std::string &line) : _sender(user), _channel(channel), _server(server) {
DEBUG_MSG("ACommand constructor called"); DEBUG_MSG("ACommand constructor called");
_args = split(line); _args = split(line, ' ');
_command = _args.at(0); _command = _args.at(0);
_channels = server->getChannelsList(); _channels = server->getChannelsList();
_users = server->getUsersList(); _users = server->getUsersList();

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/18 12:52:35 by rparodi ### ########.fr */ /* Updated: 2025/06/19 13:53:17 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,14 +28,13 @@ e_code Invite::checkArgs() {
return ERR_NOSUCHCHANNEL; return ERR_NOSUCHCHANNEL;
} else } else
_args.at(1).erase(0, 1); _args.at(1).erase(0, 1);
_cTarget = searchList(_channels, _args.at(1)); _cTarget = searchList(_server->getChannelsList(), _args.at(1));
if (_cTarget == NULL) { if (_cTarget == NULL) {
WARNING_MSG("Channel not found for INVITE command"); WARNING_MSG("Channel not found for INVITE command");
INFO_MSG("You can only invite users to channels you are in"); INFO_MSG("You can only invite users to channels you are in");
return ERR_NOSUCHCHANNEL; return ERR_NOSUCHCHANNEL;
} else }
_args.at(1).erase(0, 1); if (searchList(_cTarget->getOperators(), _sender->getName()) == NULL) {
if (searchList(_cTarget->getOperators(), _sender->getName()) != NULL) {
WARNING_MSG("You are not an operator in the channel for INVITE command"); WARNING_MSG("You are not an operator in the channel for INVITE command");
return ERR_NOPRIVILEGES; return ERR_NOPRIVILEGES;
} }
@ -61,10 +60,18 @@ e_code Invite::checkArgs() {
* @brief Execute the invite command * @brief Execute the invite command
* @note To invite a peapol to join a channel (from an operator) * @note To invite a peapol to join a channel (from an operator)
*/ */
void Invite::execute() { void Invite::execute() {
if (checkArgs() != _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for INVITE command (see warning message)"); ERROR_MSG("Invalid arguments for INVITE command (see warning message)");
return; return;
} }
// check how the com
_cTarget->addInvited(_uTarget);
std::string msgToInvited = ":" + _sender->getPrefix() + " INVITE " + _uTarget->getNickname() + " #" + _cTarget->getName() + "\r\n";
_uTarget->appendToWriteBuffer(msgToInvited);
std::string msg341 = ":localhost 341 " + _sender->getNickname() + " " + _uTarget->getNickname() + " #" + _cTarget->getName() + "\r\n";
_sender->appendToWriteBuffer(msg341);
} }

View file

@ -6,18 +6,20 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/18 12:52:13 by rparodi ### ########.fr */ /* Updated: 2025/06/19 13:50:09 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "list.hpp" #include "list.hpp"
#include "channel.hpp"
#include "commands.hpp" #include "commands.hpp"
#include "logs.hpp" #include "logs.hpp"
#include <sstream>
using namespace cmd; using namespace cmd;
e_code List::checkArgs() { e_code List::checkArgs() {
if (_args.size() < 3) { if (_args.size() < 1) {
std::string msg461 = ":localhost 461 " + this->_sender->getNickname() + " " + this->_command + " :Not enough parameters\r\n"; std::string msg461 = ":localhost 461 " + this->_sender->getNickname() + " " + this->_command + " :Not enough parameters\r\n";
this->_sender->appendToWriteBuffer(msg461); this->_sender->appendToWriteBuffer(msg461);
return ERR_NEEDMOREPARAMS; return ERR_NEEDMOREPARAMS;
@ -27,19 +29,15 @@ e_code List::checkArgs() {
INFO_MSG("You can only LIST registered users"); INFO_MSG("You can only LIST registered users");
return ERR_NOSUCHNICK; return ERR_NOSUCHNICK;
} }
if (_args.at(1).at(0) != '#') { if (this->_args.size() == 2) {
WARNING_MSG("Invalid channel name for LIST command"); std::string tmp_line = this->_args.back();
INFO_MSG("Channel names must start with a '#' character"); this->_args.pop_back();
return ERR_NOSUCHCHANNEL; std::vector<std::string> tmp = split(tmp_line, ',');
} else for (size_t i = 0; i < tmp.size(); i++) {
_args.at(1).erase(0, 1); this->_args.push_back(tmp.back());
_cTarget = searchList(_channels, _args.at(1)); tmp.pop_back();
if (_cTarget == NULL) { }
WARNING_MSG("Channel not found for LIST command"); }
INFO_MSG("You can only LIST users to channels you are in");
return ERR_NOSUCHCHANNEL;
} else
_args.at(1).erase(0, 1);
return _PARSING_OK; return _PARSING_OK;
} }
@ -52,5 +50,49 @@ void List::execute() {
ERROR_MSG("Invalid arguments for LIST command (see warning message)"); ERROR_MSG("Invalid arguments for LIST command (see warning message)");
return; return;
} }
// check how the com std::string msg321 = ":localhost 321 " + _sender->getNickname() + " Channel :Users Name\r\n";
this->_sender->appendToWriteBuffer(msg321);
if (this->_args.size() > 1) {
std::vector<std::string> vec_tmp;
bool to_print = true;
for (size_t i = 1; i < this->_args.size(); ++i) {
DEBUG_MSG("[" << i << "] " << this->_args[i]);
if (this->_args[i][0] == '#')
this->_args[i].erase(0, 1);
else {
WARNING_MSG("Thanks to check if all the Channel have an '#' at the start of the name !");
to_print = false;
break;
}
this->_cTarget = searchList(this->_server->getChannelsList(), this->_args[i]);
if (this->_cTarget == NULL) {
WARNING_MSG("Channel not found, aborting the command !");
to_print = false;
break;
} else {
std::ostringstream tmp;
tmp << ":localhost 322 " << _sender->getNickname()
<< " " << this->_cTarget->getName() << " " <<
this->_cTarget->getUsers().size() <<
" :" << this->_cTarget->getTopic() << "\r\n";
vec_tmp.push_back(tmp.str());
}
}
if (to_print) {
for (size_t j = 0; j < vec_tmp.size(); j++) {
DEBUG_MSG(vec_tmp[j]);
this->_sender->appendToWriteBuffer(vec_tmp[j]);
}
}
} else {
for (std::list<Channel *>::iterator it = this->_server->getChannelsList().begin(); it != this->_server->getChannelsList().end(); it++) {
std::ostringstream msg322; msg322 << ":localhost 322 " << _sender->getNickname()
<< " " << (*it)->getName() << " " <<
(*it)->getUsers().size() <<
" :" << (*it)->getTopic() << "\r\n";
this->_sender->appendToWriteBuffer(msg322.str());
}
}
std::string msg323 = ":localhost 323 " + _sender->getNickname() + " :End of /LIST\r\n";
this->_sender->appendToWriteBuffer(msg323);
} }

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/18 12:53:44 by rparodi ### ########.fr */ /* Updated: 2025/06/19 14:02:08 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -85,6 +85,7 @@ e_code Mode::checkArgs() {
return ERR_NOSUCHCHANNEL; return ERR_NOSUCHCHANNEL;
} else } else
this->_args.at(1).erase(0, 1); this->_args.at(1).erase(0, 1);
_cTarget = searchList(_server->getChannelsList(), _args.at(1));
if (this->_cTarget == NULL) { if (this->_cTarget == NULL) {
WARNING_MSG("Channel not found for INVITE command"); WARNING_MSG("Channel not found for INVITE command");
INFO_MSG("You can only invite users to channels you are in"); INFO_MSG("You can only invite users to channels you are in");