new methodes on class Channel(), command INVITE finished.

This commit is contained in:
Samy BEN TAYEB 2025-06-19 13:56:15 +02:00
parent c94837e9dc
commit a10815e1e2
3 changed files with 35 additions and 10 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/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,12 +6,13 @@
/* 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 11:33:25 by rparodi ### ########.fr */ /* Updated: 2025/06/19 13:46:05 by sben-tay ### ########.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) : _name(name), _owner(owner), _maxUsers(maxUsers), _needInvite(needInvite) { Channel::Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite) : _name(name), _owner(owner), _maxUsers(maxUsers), _needInvite(needInvite) {
this->_protectTopic = false; this->_protectTopic = false;
@ -60,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;
} }
@ -242,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,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);
} }