feat(channel): starting the channel class

This commit is contained in:
Raphael 2025-05-20 22:55:43 +02:00
parent cb5f92db0c
commit 12cc225603
3 changed files with 206 additions and 2 deletions

View file

@ -68,10 +68,17 @@ class Channel {
- _topic : string
+ getName() : string
+ getTopic() : string
+ isUserInChannel(User *user) : bool
+ getUsers() : list<User>
+ 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
}
' ====================================

43
include/channel.hpp Normal file
View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* channel.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <list>
class Channel {
private:
std::string _name;
std::string _password;
std::string _topic;
User *_owner;
std::list<User *> _operators;
std::list<User *> _users;
public:
// getters
std::string getName() const;
std::string getTopic() const;
User *getOwner() const;
std::list<User *> getOperators() const;
std::list<User *> 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);
};

154
sources/channel/channel.cpp Normal file
View file

@ -0,0 +1,154 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* channel.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<User *> 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<User *> 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<User *>::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<User *>::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;
}
}