Merge pull request 'Doing join command' (#26) from join-cmd into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/26
This commit is contained in:
@@ -41,11 +41,11 @@ Channel& Channel::operator=(const Channel &other)
|
|||||||
|
|
||||||
// Constructor with name
|
// 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) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
|
||||||
|
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
std::string Channel::getName() const { return (this->name); }
|
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); }
|
||||||
@@ -57,13 +57,7 @@ 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)
|
void Channel::broadcast()
|
||||||
{
|
{
|
||||||
for(std::set<int>::iterator it = members_.begin(); it != members_.end(); it++)
|
return ;
|
||||||
{
|
|
||||||
if (*it == excludeFd) continue;
|
|
||||||
std::map<int, User>::const_iterator user = clients_.find(*it);
|
|
||||||
if (user != client_.end())
|
|
||||||
user->second.send(msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -29,14 +29,14 @@ class Channel
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Channel();
|
Channel();
|
||||||
Channel(std::string name);
|
Channel(std::string &name);
|
||||||
Channel(const Channel &other);
|
Channel(const Channel &other);
|
||||||
Channel& operator=(const Channel &other);
|
Channel& operator=(const Channel &other);
|
||||||
~Channel();
|
~Channel();
|
||||||
|
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
std::string getName() const { return (this->name); }
|
std::string getName() const;
|
||||||
|
|
||||||
|
|
||||||
// Users
|
// Users
|
||||||
@@ -49,7 +49,7 @@ class Channel
|
|||||||
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);
|
void broadcast();
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -1,7 +1,9 @@
|
|||||||
NAME = ircserv
|
NAME = ircserv
|
||||||
|
|
||||||
SRC = main.cpp Server/Server.cpp User/User.cpp \
|
SRC = main.cpp Server/Server.cpp User/User.cpp \
|
||||||
|
Channel/Channel.cpp \
|
||||||
cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \
|
cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \
|
||||||
|
cmds/join.cpp
|
||||||
|
|
||||||
HEADERS = Server/Server.hpp User/User.hpp
|
HEADERS = Server/Server.hpp User/User.hpp
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/15 12:48:10 by aortigos ### ########.fr */
|
/* Updated: 2026/05/15 22:00:02 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -115,6 +115,7 @@ Server::Server(int port, const std::string& password) :
|
|||||||
commands_["PASS"] = &Server::pass_cmd;
|
commands_["PASS"] = &Server::pass_cmd;
|
||||||
commands_["NICK"] = &Server::nick_cmd;
|
commands_["NICK"] = &Server::nick_cmd;
|
||||||
commands_["USER"] = &Server::user_cmd;
|
commands_["USER"] = &Server::user_cmd;
|
||||||
|
commands_["JOIN"] = &Server::join_cmd;
|
||||||
|
|
||||||
std::cout << "Server port: " << port_
|
std::cout << "Server port: " << port_
|
||||||
<< "\nServer Password: " << password_
|
<< "\nServer Password: " << password_
|
||||||
|
|||||||
@@ -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 15:29:59 by aortigos ### ########.fr */
|
/* Updated: 2026/05/15 22:00:08 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -62,6 +62,7 @@ class Server
|
|||||||
void pass_cmd(User &client, std::istringstream &ss);
|
void pass_cmd(User &client, std::istringstream &ss);
|
||||||
void nick_cmd(User &client, std::istringstream &ss);
|
void nick_cmd(User &client, std::istringstream &ss);
|
||||||
void user_cmd(User &client, std::istringstream &ss);
|
void user_cmd(User &client, std::istringstream &ss);
|
||||||
|
void join_cmd(User &client, std::istringstream &ss);
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
|
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
|
||||||
/* Updated: 2026/05/15 15:35:44 by aortigos ### ########.fr */
|
/* Updated: 2026/05/15 22:06:36 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -15,5 +15,25 @@
|
|||||||
|
|
||||||
void Server::join_cmd(User &client, std::istringstream &ss)
|
void Server::join_cmd(User &client, std::istringstream &ss)
|
||||||
{
|
{
|
||||||
|
std::string args;
|
||||||
|
|
||||||
|
ss >> args;
|
||||||
|
if (!client.isRegistered()) return (client.send("You are not registered"));
|
||||||
|
if (args.empty()) return (client.send("No name specified\r\n"));
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator it = channels_.find(args);
|
||||||
|
|
||||||
|
if (it == channels_.end())
|
||||||
|
{
|
||||||
|
// Channel doesnt exists, create and join
|
||||||
|
channels_[args] = Channel(args);
|
||||||
|
channels_[args].addMember(client.getFd());
|
||||||
|
channels_[args].addOperator(client.getFd());
|
||||||
|
return (client.send("Channel created\r\n"));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (it->second.hasMember(client.getFd())) return (client.send("You are already in this channel\r\n"));
|
||||||
|
it->second.addMember(client.getFd());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user