JOIN created. compile, need implement MSGPRIVATE
This commit is contained in:
parent
5d8b3d9dbd
commit
2b3b6654e7
7 changed files with 89 additions and 36 deletions
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* channel.hpp :+: :+: :+: */
|
/* channel.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: rparodi <rparodi@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/17 17:52:19 by rparodi ### ########.fr */
|
/* Updated: 2025/06/17 23:38:49 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,17 +17,9 @@
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
class Channel {
|
class Channel {
|
||||||
private:
|
|
||||||
std::string _name;
|
|
||||||
std::string _password;
|
|
||||||
std::string _topic;
|
|
||||||
User *_owner;
|
|
||||||
bool _needInvite;
|
|
||||||
size_t _maxUsers;
|
|
||||||
std::list<User *> _operators;
|
|
||||||
std::list<User *> _users;
|
|
||||||
std::list<User *> _invited;
|
|
||||||
public:
|
public:
|
||||||
|
Channel(const std::string &name, User *owner, size_t maxUsers, bool needInvite);
|
||||||
|
|
||||||
// getters
|
// getters
|
||||||
std::string getName() const;
|
std::string getName() const;
|
||||||
std::string getTopic() const;
|
std::string getTopic() const;
|
||||||
|
|
@ -50,4 +42,20 @@ class Channel {
|
||||||
void addUser(User *user);
|
void addUser(User *user);
|
||||||
void removeUser(User *user);
|
void removeUser(User *user);
|
||||||
void removeOperator(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<User *> _operators;
|
||||||
|
std::list<User *> _users;
|
||||||
|
std::list<User *> _invited;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 17:36:38 by sben-tay ### ########.fr */
|
/* Updated: 2025/06/17 23:14:07 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */
|
/* 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();
|
void start();
|
||||||
unsigned short int getPort() const;
|
unsigned short int getPort() const;
|
||||||
std::list<User *> getUsersList() const;
|
std::list<User *> getUsersList() const;
|
||||||
std::list<Channel *> getChannelsList() const;
|
std::list<Channel *>& getChannelsList();
|
||||||
std::string getPassword() const;
|
std::string getPassword() const;
|
||||||
void showInfo() const;
|
void showInfo() const;
|
||||||
void printUsers() const;
|
void printUsers() const;
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,19 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* channel.cpp :+: :+: :+: */
|
/* channel.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: rparodi <rparodi@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/17 17:22:09 by rparodi ### ########.fr */
|
/* Updated: 2025/06/17 23:51:54 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "channel.hpp"
|
#include "channel.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
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
|
* @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<User *>::iterator it = this->_users.begin(); it != this->_users.end(); ++it) {
|
||||||
|
(*it)->appendToWriteBuffer(toSend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,13 +6,14 @@
|
||||||
/* 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/17 18:48:08 by rparodi ### ########.fr */
|
/* Updated: 2025/06/17 23:50:28 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "join.hpp"
|
#include "join.hpp"
|
||||||
#include "commands.hpp"
|
#include "commands.hpp"
|
||||||
#include "logs.hpp"
|
#include "logs.hpp"
|
||||||
|
#include <list>
|
||||||
|
|
||||||
using namespace cmd;
|
using namespace cmd;
|
||||||
|
|
||||||
|
|
@ -27,20 +28,6 @@ e_code Join::checkArgs() {
|
||||||
return ERR_NOSUCHCHANNEL;
|
return ERR_NOSUCHCHANNEL;
|
||||||
} else
|
} else
|
||||||
_args[1].erase(0, 1);
|
_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;
|
return _PARSING_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,9 +35,59 @@ e_code Join::checkArgs() {
|
||||||
* @brief Execute the join command
|
* @brief Execute the join command
|
||||||
* @note To join a new channel
|
* @note To join a new channel
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void Join::execute() {
|
void Join::execute() {
|
||||||
if (checkArgs() != _PARSING_OK) {
|
if (checkArgs() != _PARSING_OK) {
|
||||||
ERROR_MSG("Invalid arguments for Join command (see warning message)");
|
ERROR_MSG("Invalid arguments for Join command (see warning message)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Récupérer la liste des channels par référence
|
||||||
|
std::list<Channel*>& 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<User *>::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<User *>::iterator it = _cTarget->getUsers().begin(); it != _cTarget->getUsers().end(); ++it) {
|
||||||
|
msg353 += (*it)->getNickname() + " ";
|
||||||
|
}
|
||||||
|
_cTarget->sendAllClientInAChannel(msgJoin + msg332 + msg353 + "\r\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/06/08 19:16:10 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
/* 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<User *> Server::getUsersList() const {
|
||||||
return userList;
|
return userList;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::list<Channel *> Server::getChannelsList() const {
|
std::list<Channel *>& Server::getChannelsList() {
|
||||||
return this->_channels;
|
return this->_channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue