Files
ft_irc/Channel/Channel.cpp
2026-05-25 10:31:42 +02:00

118 lines
3.5 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Channel.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/08 16:41:40 by aortigos #+# #+# */
/* Updated: 2026/05/08 16:41:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Channel.hpp"
//////////////////
// Constructors //
//////////////////
Channel::Channel() { /* std::cout << "Channel default constructor called" << std::endl; */}
Channel::~Channel() { /* std::cout << "Channel destructor called" << std::endl; */}
Channel::Channel(const Channel &other)
{
*this = other;
// std::cout << "Channel copy constructor called" << std::endl;
}
Channel& Channel::operator=(const Channel &other)
{
if (this != &other)
{
name_ = other.name_;
members_ = other.members_;
operators_ = other.operators_;
topic_ = other.topic_;
isInviteOnly_ = other.isInviteOnly_;
invitedMembers_ = other.invitedMembers_;
}
// std::cout << "Channel copy assignment operator called" << std::endl;
return (*this);
}
// Constructor with name
Channel::Channel(std::string &name) : name_(name), isInviteOnly_(false) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
// Getters
std::string Channel::getName() const { return (this->name_); }
const std::set<int> &Channel::getMembers() const
{
return (members_);
}
// Members
void Channel::addMember(int fd)
{
invitedMembers_.erase(fd);
this->members_.insert(fd);
}
void Channel::removeMember(int fd) { this->members_.erase(fd); }
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
// Operators
void Channel::addOperator(int fd) { this->operators_.insert(fd); }
void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); }
void Channel::broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd)
{
for (std::set<int>::iterator member = members_.begin(); member != members_.end(); member++)
{
if ((*member) == excludedFd) continue;
std::map<int, User>::const_iterator user = clients.find(*member);
if (user != clients.end())
user->second.send(msg);
}
}
// Topic
void Channel::setTopic(std::string content) { this->topic_ = content; }
std::string Channel::getTopic() { return (this->topic_); }
// Mode
void Channel::setMode(std::string& mode, std::string& args)
{
bool value;
if (mode[0] == '+')
value = true;
else
value = false;
// very simple to test
if (mode[1] == 'i')
{
isInviteOnly_ = value;
return ;
}
(void) args;
}
void Channel::inviteMember(const User& client)
{
invitedMembers_.insert(client.getFd());
}
bool Channel::isInviteOnly() const { return isInviteOnly_; }
bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; }