diff --git a/diagram.puml b/diagram.puml index 2f89a8b..03742fb 100644 --- a/diagram.puml +++ b/diagram.puml @@ -68,10 +68,17 @@ class Channel { - _topic : string + getName() : string + + getTopic() : string + + isUserInChannel(User *user) : bool + + getUsers() : list + + getOwner() : User + + isOperator(user : User) : bool + + isUserInChannel(User *user) : bool + setTopic(newTopic : string) : void + addOperator(user : User) : void + + addUser(user : User) : void + removeOperator(user : User) : void - + isOperator(user : User) : bool + + removeUser(user : User) : void } ' ==================================== @@ -151,4 +158,4 @@ PollManager --> CommandDispatcher.CommandDispatcher : calls CommandDispatcher.CommandDispatcher --> Command : dispatches CommandDispatcher.CommandDispatcher --> User : parses commands -@enduml \ No newline at end of file +@enduml diff --git a/include/channel.hpp b/include/channel.hpp new file mode 100644 index 0000000..0d7cb2f --- /dev/null +++ b/include/channel.hpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* channel.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */ +/* Updated: 2025/05/20 22:53:57 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "user.hpp" +#include +#include + +class Channel { + private: + std::string _name; + std::string _password; + std::string _topic; + User *_owner; + std::list _operators; + std::list _users; + public: + // getters + std::string getName() const; + std::string getTopic() const; + User *getOwner() const; + std::list getOperators() const; + std::list getUsers() const; + bool isOperator(User *user) const; + bool isUserInChannel(User *user) const; + + // setters + void setTopic(const std::string &topic); + void addOperator(User *user); + void addUser(User *user); + void removeUser(User *user); + void removeOperator(User *user); +}; diff --git a/sources/channel/channel.cpp b/sources/channel/channel.cpp new file mode 100644 index 0000000..0c577be --- /dev/null +++ b/sources/channel/channel.cpp @@ -0,0 +1,154 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* channel.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */ +/* Updated: 2025/05/20 22:55:20 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "channel.hpp" + +/** + * @brief Get the name of the channel + * + * @return string with the channel name + */ +std::string Channel::getName() const { + return this->_name; +} + +/** + * @brief Get the topic of the channel + * +* @return string with the channel topic + */ +std::string Channel::getTopic() const { + return this->_topic; +} + +/** + * @brief Get the owner of the channel + * + * @return User which own the channel + */ +User *Channel::getOwner() const { + return this->_owner; +} + +/** + * @brief Get the list of the Users in the channel + * + * @return list of Users in the channel + */ +std::list Channel::getUsers() const { + return this->_users; +} + +/** + * @brief Get the list of the Operators in the channel + * + * @return list of Operators in the channel + */ +std::list Channel::getOperators() const { + return this->_operators; +} + +/** + * @brief Check if the user is an operator + * + * @param user to check the status + * @return true if the user is an operator, false otherwise + */ +bool Channel::isOperator(User *user) const { + for (std::list::const_iterator it = this->_operators.begin(); it != this->_operators.end(); ++it) { + if (*it == user) { + return true; + } + } + return false; +} + +/** + * @brief Check if the user is in the channel + * + * @param user to check the status + * @return true if the user is in the channel, false otherwise + */ +bool Channel::isUserInChannel(User *user) const { + for (std::list::const_iterator it = this->_users.begin(); it != this->_users.end(); ++it) { + if (*it == user) { + return true; + } + } + return false; +} + +/** + * @brief Setter for the topic of the channel + * + * @param topic new topic to set + */ +void Channel::setTopic(const std::string &topic) { + this->_topic = topic; +} + +/** + * @brief Setter to set a new operator in the channel + * + * @param user to set as operator + */ +void Channel::addOperator(User *user) { + if (this->isOperator(user) == false) { + this->_operators.push_back(user); + } + else { + std::cerr << user->getNickname() << " is already an operator in the channel " << this->_name << std::endl; + } +} + +/** + * @brief Setter to set a new user in the channel + * + * @param user to add in the channel + */ +void Channel::addUser(User *user) { + if (this->isUserInChannel(user) == false) { + this->_users.push_back(user); + } + else { + std::cerr << user->getNickname() << " is already in the channel " << this->_name << std::endl; + } +} + +/** + * @brief Setter to remove a user from the channel + * + * @param user to remove from the channel + */ +void Channel::removeUser(User *user) { + if (this->isUserInChannel(user) == true) { + this->_users.remove(user); + } + else { + std::cerr << user->getNickname() << " is not in the channel " << this->_name << std::endl; + } +} + +/** + * @brief Setter to remove an operator from the channel + * + * @param user to remove from the channel + */ +void Channel::removeOperator(User *user) { + if (this->isOperator(user) == true) { + this->_operators.remove(user); + } + else { + std::cerr << user->getNickname() << " is not an operator in the channel " << this->_name << std::endl; + } +} +