Channel changes and init JOIN command #25

Merged
aortigos merged 1 commits from fixes into main 2026-05-15 13:36:24 +00:00
5 changed files with 81 additions and 34 deletions

View File

@@ -44,6 +44,9 @@ Channel& Channel::operator=(const Channel &other)
Channel::Channel(std::string name) : name_(name) { /* std::cout << "Channel with name constructor called" << std::endl; */ } Channel::Channel(std::string name) : name_(name) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
// Getters
std::string Channel::getName() const { return (this->name); }
// Members // Members
void Channel::addMember(int fd) { this->members_.insert(fd); } void Channel::addMember(int fd) { this->members_.insert(fd); }
void Channel::removeMember(int fd) { this->members_.erase(fd); } void Channel::removeMember(int fd) { this->members_.erase(fd); }
@@ -53,3 +56,14 @@ bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0);
void Channel::addOperator(int fd) { this->operators_.insert(fd); } void Channel::addOperator(int fd) { this->operators_.insert(fd); }
void Channel::removeOperator(int fd) { this->operators_.erase(fd); } void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); } 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 excludeFd)
{
for(std::set<int>::iterator it = members_.begin(); it != members_.end(); it++)
{
if (*it == excludeFd) continue;
std::map<int, User>::const_iterator user = clients_.find(*it);
if (user != client_.end())
user->second.send(msg);
}
}

View File

@@ -16,13 +16,16 @@
# include <iostream> # include <iostream>
# include <set> # include <set>
# include <map>
# include "../User/User.hpp"
class Channel class Channel
{ {
private: private:
std::string name_; std::string name_;
std::set<int> members_; std::set<int> members_;
std::set<int> operators_; std::set<int> operators_;
public: public:
Channel(); Channel();
@@ -31,15 +34,22 @@ class Channel
Channel& operator=(const Channel &other); Channel& operator=(const Channel &other);
~Channel(); ~Channel();
// Getters
std::string getName() const { return (this->name); }
// Users // Users
void addMember(int fd); void addMember(int fd);
void removeMember(int fd); void removeMember(int fd);
bool hasMember(int fd) const; bool hasMember(int fd) const;
// Operators // Operators
void addOperator(int fd); void addOperator(int fd);
void removeOperator(int fd); void removeOperator(int fd);
bool hasOperator(int fd) const; bool hasOperator(int fd) const;
void broadcast(const std::string &msg, const std::map<int, User>& clients_, int excludeFd);
}; };

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */ /* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/15 12:47:58 by aortigos ### ########.fr */ /* Updated: 2026/05/15 15:29:59 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -23,8 +23,10 @@
# include <map> # include <map>
# include <cctype> # include <cctype>
# include <sstream>
# include "../User/User.hpp" # include "../User/User.hpp"
# include "../Channel/Channel.hpp"
# define SERVER_NAME "irc.server" # define SERVER_NAME "irc.server"
@@ -40,8 +42,9 @@ class Server
std::string password_; std::string password_;
std::vector<struct pollfd> sockets_; std::vector<struct pollfd> sockets_;
std::map<int, User> clients_; std::map<int, User> clients_;
std::map<std::string, Channel> channels_;
std::map<std::string, void (Server::*)(User&, std::istringstream&)> commands_; std::map<std::string, void (Server::*)(User&, std::istringstream&)> commands_;

View File

@@ -15,17 +15,18 @@
# define USER_HPP # define USER_HPP
# include <iostream> # include <iostream>
# include <sys/socket.h>
class User class User
{ {
private: private:
int fd; int fd;
std::string nick; std::string nick;
std::string username; std::string username;
std::string realname; std::string realname;
std::string buffer; std::string buffer;
bool authenticated; bool authenticated;
bool registered; bool registered;
public: public:
User(); User();
@@ -34,29 +35,29 @@ class User
User& operator=(const User &other); User& operator=(const User &other);
~User(); ~User();
void send(const std::string &msg); void send(const std::string &msg);
// Getters // Getters
int getFd() const; int getFd() const;
std::string getNick() const; std::string getNick() const;
std::string getUsername() const; std::string getUsername() const;
std::string getRealname() const; std::string getRealname() const;
std::string &getBuffer(); std::string &getBuffer();
bool isAuthenticated() const; bool isAuthenticated() const;
bool isRegistered() const; bool isRegistered() const;
// Setters // Setters
void setNick(std::string nick); void setNick(std::string nick);
void setUsername(std::string username); void setUsername(std::string username);
void setRealname(std::string realname); void setRealname(std::string realname);
void appendBuffer(std::string buff); void appendBuffer(std::string buff);
void clearBuffer(); void clearBuffer();
void setAuthenticated(bool value); void setAuthenticated(bool value);
void setRegistered(bool value); void setRegistered(bool value);
}; };
#endif #endif

19
cmds/join.cpp Normal file
View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* join.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/15 15:35:44 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::join_cmd(User &client, std::istringstream &ss)
{
}