diff --git a/include/channel.hpp b/include/channel.hpp index 18dc958..cd10c89 100644 --- a/include/channel.hpp +++ b/include/channel.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* channel.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ +/* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */ -/* Updated: 2025/06/17 17:52:19 by rparodi ### ########.fr */ +/* Updated: 2025/06/17 23:38:49 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,9 @@ #include class Channel { - private: - std::string _name; - std::string _password; - std::string _topic; - User *_owner; - bool _needInvite; - size_t _maxUsers; - std::list _operators; - std::list _users; - std::list _invited; public: + Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite); + // getters std::string getName() const; std::string getTopic() const; @@ -50,4 +42,20 @@ class Channel { void addUser(User *user); void removeUser(User *user); void removeOperator(User *user); + + // utility functions + void sendAllClientInAChannel(const std::string toSend); + + private: + std::string _name; + User *_owner; + std::string _password; + size_t _maxUsers; + bool _needInvite; + std::string _topic; + std::list _operators; + std::list _users; + std::list _invited; }; + + diff --git a/include/commands.hpp b/include/commands.hpp index 9107ab5..438c8db 100644 --- a/include/commands.hpp +++ b/include/commands.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */ -/* Updated: 2025/06/17 17:36:38 by sben-tay ### ########.fr */ +/* Updated: 2025/06/17 23:14:07 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/include/server.hpp b/include/server.hpp index 87626a9..cf059f1 100644 --- a/include/server.hpp +++ b/include/server.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */ -/* Updated: 2025/06/17 17:36:46 by sben-tay ### ########.fr */ +/* Updated: 2025/06/17 23:16:58 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ public: void start(); unsigned short int getPort() const; std::list getUsersList() const; - std::list getChannelsList() const; + std::list& getChannelsList(); std::string getPassword() const; void showInfo() const; void printUsers() const; diff --git a/sources/channel/channel.cpp b/sources/channel/channel.cpp index 0c263e1..cc0e044 100644 --- a/sources/channel/channel.cpp +++ b/sources/channel/channel.cpp @@ -3,16 +3,19 @@ /* ::: :::::::: */ /* channel.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ +/* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */ -/* Updated: 2025/06/17 17:22:09 by rparodi ### ########.fr */ +/* Updated: 2025/06/17 23:51:54 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ #include "channel.hpp" #include +Channel::Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite) + :_name(name), _owner(owner), _maxUsers(maxUsers), _needInvite(needInvite) {} + /** * @brief Get the name of the channel * @@ -216,3 +219,8 @@ void Channel::removeOperator(User *user) { } } +void Channel::sendAllClientInAChannel(const std::string toSend) { + for(std::list::iterator it = this->_users.begin(); it != this->_users.end(); ++it) { + (*it)->appendToWriteBuffer(toSend); + } +} \ No newline at end of file diff --git a/sources/commands/join.cpp b/sources/commands/join.cpp index a751339..a5d5650 100644 --- a/sources/commands/join.cpp +++ b/sources/commands/join.cpp @@ -6,13 +6,14 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/17 18:48:08 by rparodi ### ########.fr */ +/* Updated: 2025/06/17 23:50:28 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ #include "join.hpp" #include "commands.hpp" #include "logs.hpp" +#include using namespace cmd; @@ -27,20 +28,6 @@ e_code Join::checkArgs() { return ERR_NOSUCHCHANNEL; } else _args[1].erase(0, 1); - _cTarget = searchList(_channels, _args.at(1)); - if (_cTarget == NULL) { - WARNING_MSG("Channel not found for Join command"); - INFO_MSG("You can only Join users to channels you are in"); - return ERR_NOSUCHCHANNEL; - } - if (searchList(_cTarget->getOperators(), _sender->getName()) != NULL) { - WARNING_MSG("You are not an operator in the channel for Join command"); - return ERR_NOPRIVILEGES; - } - if (searchList(_cTarget->getInvited(), _sender->getName()) != NULL) { - WARNING_MSG("This channel is private and ur not invited"); - return ERR_INVITEONLYCHAN; - } return _PARSING_OK; } @@ -48,9 +35,59 @@ e_code Join::checkArgs() { * @brief Execute the join command * @note To join a new channel */ + void Join::execute() { if (checkArgs() != _PARSING_OK) { ERROR_MSG("Invalid arguments for Join command (see warning message)"); return; } -} + + // Récupérer la liste des channels par référence + std::list& allChannels = _server->getChannelsList(); + + // msgJoin, msg332, msg353 + + // Vérifier si le channel existe déjà + _cTarget = searchList(allChannels, _args.at(1)); + if (_cTarget == NULL) { + // Le channel n'existe pas, on le crée + _cTarget = new Channel(_args[1], _sender, 50, false); // false = public + allChannels.push_back(_cTarget); + + // Ajout automatique du créateur dans le channel + _cTarget->addUser(_sender); + _cTarget->addOperator(_sender); + + std::string msgJoin = ":" + _sender->getPrefix() + " JOIN #" + _cTarget->getName() + "\r\n"; + std:: string msg332 = ":" + _sender->getPrefix() + " 332 " + _sender->getNickname() + " #" + _cTarget->getName() + " :" + _cTarget->getTopic() + "\r\n"; + std::string msg353 = ":" + _sender->getPrefix() + " 353 " + _sender->getNickname() + " = #" + _cTarget->getName() + " :"; + for (std::list::iterator it = _cTarget->getUsers().begin(); it != _cTarget->getUsers().end(); ++it) { + msg353 += (*it)->getNickname() + " "; + } + _sender->appendToWriteBuffer(msgJoin + msg332 + msg353 + "\r\n"); + return; + } + + // Si le channel est privé, vérifier que l'utilisateur est invité + + if (_cTarget->getNeedInvite() == true) { + if (!searchList(_cTarget->getInvited(), _sender->getName())) { + std::string msgErr = ":localhost 473 " + _sender->getNickname() + " #" + _cTarget->getName() + " :Cannot join channel (+i)\r\n"; + _sender->appendToWriteBuffer(msgErr); + return; + } + } + + // Ajouter le user au channel + _cTarget->addUser(_sender); + + // Envoyer le JOIN à tous les membres du channel + std::string msgJoin = ":" + _sender->getPrefix() + " JOIN #" + _cTarget->getName() + "\r\n"; + std:: string msg332 = ":" + _sender->getPrefix() + " 332 " + _sender->getNickname() + " #" + _cTarget->getName() + " :" + _cTarget->getTopic() + "\r\n"; + std::string msg353 = ":" + _sender->getPrefix() + " 353 " + _sender->getNickname() + " = #" + _cTarget->getName() + " :"; + for (std::list::iterator it = _cTarget->getUsers().begin(); it != _cTarget->getUsers().end(); ++it) { + msg353 += (*it)->getNickname() + " "; + } + _cTarget->sendAllClientInAChannel(msgJoin + msg332 + msg353 + "\r\n"); + +} \ No newline at end of file diff --git a/sources/commands/userCmd.cpp b/sources/commands/userCmd.cpp index eb587b7..3ec72a5 100644 --- a/sources/commands/userCmd.cpp +++ b/sources/commands/userCmd.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/08 19:16:10 by sben-tay #+# #+# */ -/* Updated: 2025/06/17 15:41:25 by sben-tay ### ########.fr */ +/* Updated: 2025/06/17 23:17:54 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/sources/core/Server.cpp b/sources/core/Server.cpp index 0995527..47bdb02 100644 --- a/sources/core/Server.cpp +++ b/sources/core/Server.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ -/* Updated: 2025/06/17 17:38:39 by sben-tay ### ########.fr */ +/* Updated: 2025/06/17 23:16:55 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -194,7 +194,7 @@ std::list Server::getUsersList() const { return userList; } -std::list Server::getChannelsList() const { +std::list& Server::getChannelsList() { return this->_channels; }