Compare commits
22 Commits
982ca33116
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0582a52aad | |||
|
|
1497a3c979 | ||
| fe094406d0 | |||
|
|
fa65a4a2a7 | ||
| 1ad176b41d | |||
|
|
248da65e44 | ||
| b5ae2778fc | |||
|
|
2ae69c33a8 | ||
| 396891122c | |||
|
|
6b87fe8840 | ||
|
|
dec6930a9a | ||
|
|
e0d977d6b5 | ||
| e60ea7aab6 | |||
|
|
91533c0b9f | ||
|
|
e5e08d4cd0 | ||
| dec5a64118 | |||
| b325076d01 | |||
|
|
065f6ca10e | ||
|
|
bed4006e90 | ||
| edd4019f4e | |||
|
|
dd4de38e5f | ||
|
|
85d6bae03a |
@@ -33,6 +33,9 @@ Channel& Channel::operator=(const Channel &other)
|
|||||||
name_ = other.name_;
|
name_ = other.name_;
|
||||||
members_ = other.members_;
|
members_ = other.members_;
|
||||||
operators_ = other.operators_;
|
operators_ = other.operators_;
|
||||||
|
topic_ = other.topic_;
|
||||||
|
isInviteOnly_ = other.isInviteOnly_;
|
||||||
|
invitedMembers_ = other.invitedMembers_;
|
||||||
}
|
}
|
||||||
// std::cout << "Channel copy assignment operator called" << std::endl;
|
// std::cout << "Channel copy assignment operator called" << std::endl;
|
||||||
return (*this);
|
return (*this);
|
||||||
@@ -41,7 +44,7 @@ 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), isInviteOnly_(false) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
|
||||||
|
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
@@ -53,7 +56,12 @@ const std::set<int> &Channel::getMembers() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Members
|
// Members
|
||||||
void Channel::addMember(int fd) { this->members_.insert(fd); }
|
void Channel::addMember(int fd)
|
||||||
|
{
|
||||||
|
invitedMembers_.erase(fd);
|
||||||
|
this->members_.insert(fd);
|
||||||
|
}
|
||||||
|
|
||||||
void Channel::removeMember(int fd) { this->members_.erase(fd); }
|
void Channel::removeMember(int fd) { this->members_.erase(fd); }
|
||||||
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
|
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
|
||||||
|
|
||||||
@@ -62,14 +70,48 @@ 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, 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++)
|
for (std::set<int>::iterator member = members_.begin(); member != members_.end(); member++)
|
||||||
{
|
{
|
||||||
if ((*member) == excludedFd) continue;
|
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())
|
if (user != clients.end())
|
||||||
user->second.send(msg);
|
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; }
|
||||||
|
|||||||
@@ -24,9 +24,15 @@ 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_;
|
||||||
|
|
||||||
|
std::string topic_;
|
||||||
|
|
||||||
|
bool isInviteOnly_;
|
||||||
|
std::set<int> invitedMembers_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Channel();
|
Channel();
|
||||||
Channel(std::string &name);
|
Channel(std::string &name);
|
||||||
@@ -39,6 +45,9 @@ class Channel
|
|||||||
std::string getName() const;
|
std::string getName() const;
|
||||||
const std::set<int> &getMembers() const;
|
const std::set<int> &getMembers() const;
|
||||||
|
|
||||||
|
// Topic
|
||||||
|
void setTopic(std::string content);
|
||||||
|
std::string getTopic();
|
||||||
|
|
||||||
// Users
|
// Users
|
||||||
void addMember(int fd);
|
void addMember(int fd);
|
||||||
@@ -50,8 +59,14 @@ 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, std::map<int, User> &clients, int excludedFd);
|
void broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd);
|
||||||
|
|
||||||
|
// modes
|
||||||
|
void setMode(std::string& mode, std::string& args);
|
||||||
|
|
||||||
|
void inviteMember(const User& client);
|
||||||
|
bool isInviteOnly() const;
|
||||||
|
bool isInvited(int fd) const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -4,6 +4,8 @@ SRC = main.cpp Server/Server.cpp User/User.cpp \
|
|||||||
Channel/Channel.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 cmds/privmsg.cpp cmds/quit.cpp \
|
cmds/join.cpp cmds/privmsg.cpp cmds/quit.cpp \
|
||||||
|
cmds/mode.cpp cmds/invite.cpp cmds/kick.cpp \
|
||||||
|
cmds/topic.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/16 11:31:25 by aortigos ### ########.fr */
|
/* Updated: 2026/05/25 10:15:18 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -118,7 +118,11 @@ Server::Server(int port, const std::string& password) :
|
|||||||
commands_["JOIN"] = &Server::join_cmd;
|
commands_["JOIN"] = &Server::join_cmd;
|
||||||
commands_["QUIT"] = &Server::quit_cmd;
|
commands_["QUIT"] = &Server::quit_cmd;
|
||||||
commands_["PRIVMSG"] = &Server::privmsg_cmd;
|
commands_["PRIVMSG"] = &Server::privmsg_cmd;
|
||||||
|
commands_["MODE"] = & Server::mode_cmd;
|
||||||
|
commands_["INVITE"] = &Server::invite_cmd;
|
||||||
|
commands_["KICK"] = &Server::kick_cmd;
|
||||||
|
commands_["TOPIC"] = &Server::topic_cmd;
|
||||||
|
|
||||||
std::cout << "Server port: " << port_
|
std::cout << "Server port: " << port_
|
||||||
<< "\nServer Password: " << password_
|
<< "\nServer Password: " << password_
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
@@ -233,6 +237,8 @@ std::vector<struct pollfd>::iterator Server::removeClient(std::vector<struct pol
|
|||||||
|
|
||||||
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
|
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
|
||||||
|
|
||||||
|
// should also remove client from all channels its in
|
||||||
|
|
||||||
clients_.erase(client->fd);
|
clients_.erase(client->fd);
|
||||||
return (sockets_.erase(client));
|
return (sockets_.erase(client));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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/16 11:45:11 by aortigos ### ########.fr */
|
/* Updated: 2026/05/25 10:15:08 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -67,7 +67,10 @@ class Server
|
|||||||
void join_cmd(User &client, std::istringstream &ss);
|
void join_cmd(User &client, std::istringstream &ss);
|
||||||
void privmsg_cmd(User &client, std::istringstream &ss);
|
void privmsg_cmd(User &client, std::istringstream &ss);
|
||||||
void quit_cmd(User &client, std::istringstream &ss);
|
void quit_cmd(User &client, std::istringstream &ss);
|
||||||
|
void mode_cmd(User &client, std::istringstream &ss);
|
||||||
|
void invite_cmd(User &client, std::istringstream &ss);
|
||||||
|
void kick_cmd(User &client, std::istringstream &ss);
|
||||||
|
void topic_cmd(User &client, std::istringstream &ss);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server();
|
Server();
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ User::~User()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void User::send(const std::string &msg)
|
void User::send(const std::string &msg) const
|
||||||
{
|
{
|
||||||
::send(fd, msg.c_str(), msg.size(), 0);
|
::send(fd, msg.c_str(), msg.size(), 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ 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) const;
|
||||||
|
|
||||||
// Channels
|
// Channels
|
||||||
void joinChannel(const std::string &channel);
|
void joinChannel(const std::string &channel);
|
||||||
|
|||||||
72
cmds/invite.cpp
Normal file
72
cmds/invite.cpp
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* invclient_ite.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/23 20:22:46 by iherman- #+# #+# */
|
||||||
|
/* Updated: 2026/05/23 21:14:14 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::invite_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string target;
|
||||||
|
std::string channel;
|
||||||
|
|
||||||
|
ss >> target >> channel;
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||||
|
|
||||||
|
if (target.empty() || channel.empty())
|
||||||
|
{
|
||||||
|
return client.send(":" SERVER_NAME " 461 * INVITE :Not enough parameters\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify target exists
|
||||||
|
std::map<int, User>::iterator client_it = clients_.begin();
|
||||||
|
for (; client_it != clients_.end(); ++client_it)
|
||||||
|
{
|
||||||
|
if (client_it->second.getNick() == target)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (client_it == clients_.end())
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 401 " + target + " :No such nick\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify channel exist & user is on channel
|
||||||
|
std::map<std::string, Channel>::iterator channel_it = channels_.find(channel);
|
||||||
|
if (channel_it == channels_.end())
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 401 " + target + " :No such channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!channel_it->second.hasMember(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 442 " + channel + " :You are not on that channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel_it->second.hasMember(client_it->second.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 443 " + channel + " :is already on channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel_it->second.isInviteOnly() && !channel_it->second.hasOperator(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 482 " + channel + ":You're not channel operator\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
client_it->second.send(":" + client.getNick() + "!" + client.getUsername() + "@localhost INVITE " + client_it->second.getNick() + " :" + channel_it->second.getName());
|
||||||
|
client.send(std::string(":") + SERVER_NAME " 341 " + client.getNick() + " " + client_it->second.getNick() + " " + channel_it->second.getName());
|
||||||
|
channel_it->second.inviteMember(client_it->second);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* join.cpp :+: :+: :+: */
|
/* join.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
|
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
|
||||||
/* Updated: 2026/05/16 17:04:29 by aortigos ### ########.fr */
|
/* Updated: 2026/05/23 18:32:37 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -23,6 +23,7 @@ void Server::join_cmd(User &client, std::istringstream &ss)
|
|||||||
|
|
||||||
std::map<std::string, Channel>::iterator it = channels_.find(args);
|
std::map<std::string, Channel>::iterator it = channels_.find(args);
|
||||||
|
|
||||||
|
// creates channel
|
||||||
if (it == channels_.end())
|
if (it == channels_.end())
|
||||||
{
|
{
|
||||||
channels_[args] = Channel(args);
|
channels_[args] = Channel(args);
|
||||||
@@ -51,29 +52,37 @@ void Server::join_cmd(User &client, std::istringstream &ss)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (it->second.hasMember(client.getFd()))
|
if (it->second.hasMember(client.getFd()))
|
||||||
client.send(":" SERVER_NAME " 443 " + client.getNick() + " " + args + " :is already on channel\r\n");
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
client.joinChannel(it->first);
|
client.send(":" SERVER_NAME " 443 " + client.getNick() + " " + args + " :is already on channel\r\n");
|
||||||
it->second.addMember(client.getFd());
|
return ;
|
||||||
std::string joinMsg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost JOIN " + args + "\r\n";
|
|
||||||
channels_[args].broadcast(joinMsg, clients_, -1);
|
|
||||||
|
|
||||||
|
|
||||||
std::string namesList = ":" SERVER_NAME " 353 " + client.getNick() + " = " + args + " :";
|
|
||||||
const std::set<int> &members = channels_[args].getMembers();
|
|
||||||
for (std::set<int>::const_iterator m = members.begin(); m != members.end(); m++)
|
|
||||||
{
|
|
||||||
std::map<int, User>::iterator u = clients_.find(*m);
|
|
||||||
if (u != clients_.end())
|
|
||||||
{
|
|
||||||
if (channels_[args].hasOperator(*m))
|
|
||||||
namesList += "@";
|
|
||||||
namesList += u->second.getNick() + " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client.send(namesList + "\r\n");
|
|
||||||
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (it->second.isInviteOnly() && !it->second.isInvited(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 473 " + client.getNick() + " " + args + " :Cannot join channel (+i)\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
it->second.addMember(client.getFd());
|
||||||
|
|
||||||
|
client.joinChannel(it->first);
|
||||||
|
|
||||||
|
std::string joinMsg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost JOIN " + args + "\r\n";
|
||||||
|
channels_[args].broadcast(joinMsg, clients_, -1);
|
||||||
|
|
||||||
|
std::string namesList = ":" SERVER_NAME " 353 " + client.getNick() + " = " + args + " :";
|
||||||
|
const std::set<int> &members = channels_[args].getMembers();
|
||||||
|
for (std::set<int>::const_iterator m = members.begin(); m != members.end(); m++)
|
||||||
|
{
|
||||||
|
std::map<int, User>::iterator u = clients_.find(*m);
|
||||||
|
if (u != clients_.end())
|
||||||
|
{
|
||||||
|
if (channels_[args].hasOperator(*m))
|
||||||
|
namesList += "@";
|
||||||
|
namesList += u->second.getNick() + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client.send(namesList + "\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
58
cmds/kick.cpp
Normal file
58
cmds/kick.cpp
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* kick.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/25 09:27:35 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/25 09:52:27 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::kick_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string channel;
|
||||||
|
std::string user;
|
||||||
|
std::string reason;
|
||||||
|
|
||||||
|
ss >> channel >> user;
|
||||||
|
getline(ss, reason);
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
|
||||||
|
|
||||||
|
if (channel.empty() || user.empty())
|
||||||
|
return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " KICK :not enough parameters\r\n"));
|
||||||
|
if (!reason.empty() && reason[0] == ' ')
|
||||||
|
reason = reason.substr(1);
|
||||||
|
if (!reason.empty() && reason[0] == ':')
|
||||||
|
reason = reason.substr(1);
|
||||||
|
if (reason.empty()) reason = "Kicked";
|
||||||
|
|
||||||
|
if (channel[0] != '#') return (client.send(""));
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator it_channels = channels_.find(channel);
|
||||||
|
|
||||||
|
if (it_channels == channels_.end())
|
||||||
|
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
|
||||||
|
if (!it_channels->second.hasOperator(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 482 " + client.getNick() + " " + channel + " :You're not channel operator\r\n"));
|
||||||
|
|
||||||
|
int userId = -1;
|
||||||
|
for (std::map<int, User>::iterator it_clients = clients_.begin(); it_clients != clients_.end(); it_clients++)
|
||||||
|
{
|
||||||
|
if (it_clients->second.getNick() == user)
|
||||||
|
userId = it_clients->second.getFd();
|
||||||
|
}
|
||||||
|
if (userId == -1)
|
||||||
|
return (client.send(":" SERVER_NAME " 401 " + client.getNick() + " " + user + " :No such nick\r\n"));
|
||||||
|
if (!it_channels->second.hasMember(userId))
|
||||||
|
return (client.send(":" SERVER_NAME " 441 " + client.getNick() + " " + user + " " + channel + " :They aren't on that channel\r\n"));
|
||||||
|
|
||||||
|
it_channels->second.broadcast(":" + client.getNick() + "!" + client.getUsername() + "@localhost KICK " + channel + " " + user + " :" + reason + "\r\n", clients_, -1);
|
||||||
|
clients_[userId].leaveChannel(channel);
|
||||||
|
it_channels->second.removeMember(userId);
|
||||||
|
|
||||||
|
}
|
||||||
41
cmds/mode.cpp
Normal file
41
cmds/mode.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* mode.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/23 17:15:27 by iherman- #+# #+# */
|
||||||
|
/* Updated: 2026/05/23 20:25:02 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::mode_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string target;
|
||||||
|
std::string mode;
|
||||||
|
std::string args;
|
||||||
|
|
||||||
|
ss >> target >> mode;
|
||||||
|
std::getline(ss, args); // might include space in channel name, need to fix
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||||
|
if (target.empty() || mode.empty()) return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " MODE :Not enough parameters\r\n"));
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator channel = channels_.find(target);
|
||||||
|
if (channel == channels_.end())
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!channel->second.hasOperator(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(std::string(":") + SERVER_NAME + " 482 " + client.getNick() + " " + channel->second.getName() + " :You're not channel operator\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
channel->second.setMode(mode, args); // args should prob be a stringstream :(
|
||||||
|
}
|
||||||
@@ -6,16 +6,12 @@
|
|||||||
/* 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/16 11:28:47 by aortigos ### ########.fr */
|
/* Updated: 2026/05/25 10:02:13 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "../Server/Server.hpp"
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
|
||||||
// Test function, need to change all the error messages
|
|
||||||
|
|
||||||
void Server::privmsg_cmd(User &client, std::istringstream &ss)
|
void Server::privmsg_cmd(User &client, std::istringstream &ss)
|
||||||
{
|
{
|
||||||
std::string channel;
|
std::string channel;
|
||||||
@@ -26,16 +22,31 @@ void Server::privmsg_cmd(User &client, std::istringstream &ss)
|
|||||||
|
|
||||||
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
|
||||||
|
|
||||||
std::map<std::string, Channel>::iterator it = channels_.find(channel);
|
if (channel.empty())
|
||||||
|
return (client.send(":" SERVER_NAME " 411 " + client.getNick() + " :No recipient given (PRIVMSG)\r\n"));
|
||||||
|
|
||||||
if (it == channels_.end())
|
|
||||||
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
|
|
||||||
if (!it->second.hasMember(client.getFd()))
|
|
||||||
return (client.send(":" SERVER_NAME " 404 " + client.getNick() + " " + channel + " :Cannot send to channel\r\n"));
|
|
||||||
if (message.empty()) return (client.send(":" SERVER_NAME " 412 " + client.getNick() + " :No text to send\r\n"));
|
if (message.empty()) return (client.send(":" SERVER_NAME " 412 " + client.getNick() + " :No text to send\r\n"));
|
||||||
|
|
||||||
if (message[0] == ' ')
|
if (message[0] == ' ')
|
||||||
message = message.substr(1);
|
message = message.substr(1);
|
||||||
|
|
||||||
std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n";
|
if (channel[0] == '#')
|
||||||
return (it->second.broadcast(msg, clients_, client.getFd()));
|
{
|
||||||
|
std::map<std::string, Channel>::iterator it = channels_.find(channel);
|
||||||
|
|
||||||
|
if (it == channels_.end())
|
||||||
|
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
|
||||||
|
if (!it->second.hasMember(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 404 " + client.getNick() + " " + channel + " :Cannot send to channel\r\n"));
|
||||||
|
|
||||||
|
std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n";
|
||||||
|
it->second.broadcast(msg, clients_, client.getFd());
|
||||||
|
} else {
|
||||||
|
for(std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++)
|
||||||
|
{
|
||||||
|
if (it->second.getNick() == channel)
|
||||||
|
return (it->second.send(":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n"));
|
||||||
|
}
|
||||||
|
client.send(":" SERVER_NAME " 401 " + client.getNick() + " " + channel + " :No such nick\r\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
51
cmds/topic.cpp
Normal file
51
cmds/topic.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* topic.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/25 10:04:16 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/25 10:19:53 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::topic_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string channel;
|
||||||
|
std::string message;
|
||||||
|
|
||||||
|
ss >> channel;
|
||||||
|
getline(ss, message);
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
|
||||||
|
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator it = channels_.find(channel);
|
||||||
|
|
||||||
|
if (it == channels_.end())
|
||||||
|
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
|
||||||
|
|
||||||
|
if (!it->second.hasMember(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 404 " + client.getNick() + " " + channel + " :Cannot send to channel\r\n"));
|
||||||
|
|
||||||
|
if (message.empty())
|
||||||
|
{
|
||||||
|
std::string res = it->second.getTopic();
|
||||||
|
if (res.empty())
|
||||||
|
return (client.send(":" SERVER_NAME " 331 " + client.getNick() + " " + channel + " :No topic is set\r\n"));
|
||||||
|
client.send(":" SERVER_NAME " 332 " + client.getNick() + " " + channel + " :" + res + "\r\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!message.empty() && message[0] == ' ')
|
||||||
|
message = message.substr(1);
|
||||||
|
if (!it->second.hasOperator(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 482 " + client.getNick() + " " + channel + " :You're not channel operator\r\n"));
|
||||||
|
it->second.setTopic(message);
|
||||||
|
it->second.broadcast(":" + client.getNick() + "!" + client.getUsername() + "@localhost TOPIC " + channel + " :" + message + "\r\n", clients_, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
21
tasks.md
21
tasks.md
@@ -42,14 +42,23 @@ Represents a connected IRC client.
|
|||||||
### Third stage
|
### Third stage
|
||||||
|
|
||||||
- [✓] Dispatcher (select function for each command)
|
- [✓] Dispatcher (select function for each command)
|
||||||
- [ ] Implement generic parser (extract command and pass args to command function)
|
- [✓] Implement generic parser (extract command and pass args to command function)
|
||||||
- [ ] Client has nickname and username
|
- [✓] Client has nickname and username
|
||||||
- [ ] PASS command for authenticate
|
- [✓] PASS command for authenticate
|
||||||
|
|
||||||
### Fourth stage
|
### Fourth stage
|
||||||
|
|
||||||
- [ ] Client can create/connect to channels
|
- [✓] Client can create/connect to channels
|
||||||
- [ ] JOIN command (user can create channel)
|
- [✓] JOIN command (user can create channel)
|
||||||
- [ ] PRIVMSG command (send message to a channel)
|
- [✓] PRIVMSG command (send message to a channel)
|
||||||
|
- [✓] QUIT command
|
||||||
|
- [✓] PRIVMSG difference between channels (#) and users
|
||||||
|
|
||||||
|
### Fifth stage
|
||||||
|
|
||||||
|
- [ ] PART command (user leaves a channel)
|
||||||
|
- [✓] KICK command (operator removes a user from channel)
|
||||||
|
- [ ] INVITE command (operator invites a user to channel)
|
||||||
|
- [ ] TOPIC command (view or set channel topic)
|
||||||
|
|
||||||
*It will continue...*
|
*It will continue...*
|
||||||
|
|||||||
Reference in New Issue
Block a user