From a10815e1e2d4b6472bd3262a3c2472cb9c05a315 Mon Sep 17 00:00:00 2001 From: Samy BEN TAYEB Date: Thu, 19 Jun 2025 13:56:15 +0200 Subject: [PATCH] new methodes on class Channel(), command INVITE finished. --- include/channel.hpp | 6 ++++-- sources/channel/channel.cpp | 20 ++++++++++++++++++-- sources/commands/invite.cpp | 19 +++++++++++++------ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/channel.hpp b/include/channel.hpp index f2e30d6..bab998d 100644 --- a/include/channel.hpp +++ b/include/channel.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; size_t getMaxUsers() const; User *getOwner() const; - std::list getOperators() const; + std::list& getOperators(); std::list& getUsers(); std::list getInvited() const; std::string getPassword() const; @@ -43,6 +43,8 @@ class Channel { void setPassword(const std::string &newPass); void addOperator(User *user); void addUser(User *user); + void addInvited(User *user); + bool isInvited(User *user) const; void removeUser(User *user); void removeOperator(User *user); diff --git a/sources/channel/channel.cpp b/sources/channel/channel.cpp index ca94356..fc0ae6f 100644 --- a/sources/channel/channel.cpp +++ b/sources/channel/channel.cpp @@ -6,12 +6,13 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include Channel::Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite) : _name(name), _owner(owner), _maxUsers(maxUsers), _needInvite(needInvite) { this->_protectTopic = false; @@ -60,7 +61,7 @@ std::list& Channel::getUsers() { * * @return list of Operators in the channel */ -std::list Channel::getOperators() const { +std::list& Channel::getOperators() { return this->_operators; } @@ -242,3 +243,18 @@ void Channel::sendAllClientInAChannel(const std::string &toSend, User *exclude) (*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; +} diff --git a/sources/commands/invite.cpp b/sources/commands/invite.cpp index a4a3545..39994ce 100644 --- a/sources/commands/invite.cpp +++ b/sources/commands/invite.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; } else _args.at(1).erase(0, 1); - _cTarget = searchList(_channels, _args.at(1)); + _cTarget = searchList(_server->getChannelsList(), _args.at(1)); if (_cTarget == NULL) { WARNING_MSG("Channel not found for INVITE command"); INFO_MSG("You can only invite users to channels you are in"); 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"); return ERR_NOPRIVILEGES; } @@ -61,10 +60,18 @@ e_code Invite::checkArgs() { * @brief Execute the invite command * @note To invite a peapol to join a channel (from an operator) */ + void Invite::execute() { if (checkArgs() != _PARSING_OK) { ERROR_MSG("Invalid arguments for INVITE command (see warning message)"); 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); }