Added MODE and invite only functionality

This commit is contained in:
iherman-
2026-05-23 18:39:18 +02:00
parent 982ca33116
commit dd4de38e5f
9 changed files with 124 additions and 36 deletions

View File

@@ -33,6 +33,8 @@ Channel& Channel::operator=(const Channel &other)
name_ = other.name_;
members_ = other.members_;
operators_ = other.operators_;
isInviteOnly_ = other.isInviteOnly_;
invitedMembers_ = other.invitedMembers_;
}
// std::cout << "Channel copy assignment operator called" << std::endl;
return (*this);
@@ -41,7 +43,7 @@ Channel& Channel::operator=(const Channel &other)
// Constructor with name
Channel::Channel(std::string &name) : name_(name) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
Channel::Channel(std::string &name) : name_(name), isInviteOnly_(false) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
// Getters
@@ -62,14 +64,35 @@ 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, std::map<int, User> &clients, int excludedFd)
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>::iterator user = clients.find(*member);
std::map<int, User>::const_iterator user = clients.find(*member);
if (user != clients.end())
user->second.send(msg);
}
}
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;
}
bool Channel::isInviteOnly() const { return isInviteOnly_; }
bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; }

View File

@@ -24,9 +24,13 @@ class Channel
{
private:
std::string name_;
std::set<int> members_;
std::set<int> members_;
std::set<int> operators_;
bool isInviteOnly_;
std::set<int> invitedMembers_;
public:
Channel();
Channel(std::string &name);
@@ -39,7 +43,6 @@ class Channel
std::string getName() const;
const std::set<int> &getMembers() const;
// Users
void addMember(int fd);
void removeMember(int fd);
@@ -50,8 +53,15 @@ class Channel
void removeOperator(int fd);
bool hasOperator(int fd) const;
void broadcast(const std::string &msg, std::map<int, User> &clients, int excludedFd);
void inviteMember(User& client); // not implemented
void broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd);
// modes
void setMode(std::string& mode, std::string& args);
bool isInviteOnly() const;
bool isInvited(int fd) const;
};