Compare commits

...

22 Commits

Author SHA1 Message Date
0582a52aad Merge pull request 'Topic cmd' (#39) from topic-cmd into main
Reviewed-on: #39
2026-05-25 08:32:05 +00:00
aortigos
1497a3c979 Topic cmd 2026-05-25 10:31:42 +02:00
fe094406d0 Merge pull request 'Check if channel is empty' (#38) from fix-privmsg-cmd into main
Reviewed-on: #38
2026-05-25 08:03:48 +00:00
aortigos
fa65a4a2a7 Check if channel is empty 2026-05-25 10:03:23 +02:00
1ad176b41d Merge pull request 'Fixed kick command, doesnt accept empty values' (#37) from fix-kick-cmd into main
Reviewed-on: #37
2026-05-25 08:00:12 +00:00
aortigos
248da65e44 Fixed kick command, doesnt accept empty values 2026-05-25 09:56:20 +02:00
b5ae2778fc Merge pull request 'Updated tasks.md' (#36) from update-tasks into main
Reviewed-on: #36
2026-05-25 07:48:43 +00:00
aortigos
2ae69c33a8 Updated tasks.md 2026-05-25 09:48:11 +02:00
396891122c Merge pull request 'kick-command' (#35) from kick-command into main
Reviewed-on: #35
2026-05-25 07:47:14 +00:00
aortigos
6b87fe8840 Kick command added 2026-05-25 09:46:45 +02:00
aortigos
dec6930a9a removed // test function 2026-05-25 09:45:31 +02:00
aortigos
e0d977d6b5 removed // test function 2026-05-25 09:44:50 +02:00
e60ea7aab6 Merge pull request 'privmsg-to-users' (#34) from privmsg-to-users into main
Reviewed-on: #34
2026-05-25 07:26:37 +00:00
aortigos
91533c0b9f visual fix 2026-05-25 09:26:03 +02:00
aortigos
e5e08d4cd0 Now users can send privmsg to other user 2026-05-25 09:23:05 +02:00
dec5a64118 Merge pull request 'updated tasks' (#33) from update-tasks into main
Reviewed-on: #33
2026-05-25 07:05:18 +00:00
b325076d01 Merge pull request 'invite-cmd' (#32) from invite-cmd into main
Reviewed-on: #32
2026-05-23 19:51:47 +00:00
iherman-
065f6ca10e cleanup 2026-05-23 21:50:54 +02:00
iherman-
bed4006e90 Added working INVITE command 2026-05-23 21:38:02 +02:00
edd4019f4e Merge pull request 'Added MODE and invite only functionality' (#31) from invite-only-channels into main
Reviewed-on: #31
2026-05-23 16:40:38 +00:00
iherman-
dd4de38e5f Added MODE and invite only functionality 2026-05-23 18:39:18 +02:00
aortigos
85d6bae03a updated tasks 2026-05-22 13:00:06 +02:00
14 changed files with 373 additions and 54 deletions

View File

@@ -33,6 +33,9 @@ Channel& Channel::operator=(const Channel &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);
@@ -41,7 +44,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
@@ -53,7 +56,12 @@ const std::set<int> &Channel::getMembers() const
}
// 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); }
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); }
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);
}
}
// 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; }

View File

@@ -24,9 +24,15 @@ class Channel
{
private:
std::string name_;
std::set<int> members_;
std::set<int> operators_;
std::string topic_;
bool isInviteOnly_;
std::set<int> invitedMembers_;
public:
Channel();
Channel(std::string &name);
@@ -39,6 +45,9 @@ class Channel
std::string getName() const;
const std::set<int> &getMembers() const;
// Topic
void setTopic(std::string content);
std::string getTopic();
// Users
void addMember(int fd);
@@ -50,8 +59,14 @@ 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 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;
};

View File

@@ -4,6 +4,8 @@ SRC = main.cpp Server/Server.cpp User/User.cpp \
Channel/Channel.cpp \
cmds/pass.cpp cmds/nick.cpp cmds/user.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

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,6 +118,10 @@ Server::Server(int port, const std::string& password) :
commands_["JOIN"] = &Server::join_cmd;
commands_["QUIT"] = &Server::quit_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_
<< "\nServer Password: " << password_
@@ -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;
// should also remove client from all channels its in
clients_.erase(client->fd);
return (sockets_.erase(client));
}

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 privmsg_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:
Server();

View File

@@ -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);
}

View File

@@ -38,7 +38,7 @@ class User
User& operator=(const User &other);
~User();
void send(const std::string &msg);
void send(const std::string &msg) const;
// Channels
void joinChannel(const std::string &channel);

72
cmds/invite.cpp Normal file
View 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);
}

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* join.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
// creates channel
if (it == channels_.end())
{
channels_[args] = Channel(args);
@@ -51,15 +52,24 @@ void Server::join_cmd(User &client, std::istringstream &ss)
else
{
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");
return ;
}
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++)
@@ -76,4 +86,3 @@ void Server::join_cmd(User &client, std::istringstream &ss)
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
}
}
}

58
cmds/kick.cpp Normal file
View 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
View 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 :(
}

View File

@@ -6,16 +6,12 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
// Test function, need to change all the error messages
void Server::privmsg_cmd(User &client, std::istringstream &ss)
{
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 (channel.empty())
return (client.send(":" SERVER_NAME " 411 " + client.getNick() + " :No recipient given (PRIVMSG)\r\n"));
if (message.empty()) return (client.send(":" SERVER_NAME " 412 " + client.getNick() + " :No text to send\r\n"));
if (message[0] == ' ')
message = message.substr(1);
if (channel[0] == '#')
{
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()) return (client.send(":" SERVER_NAME " 412 " + client.getNick() + " :No text to send\r\n"));
if (message[0] == ' ')
message = message.substr(1);
std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n";
return (it->second.broadcast(msg, clients_, client.getFd()));
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
View 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);
}
}

View File

@@ -42,14 +42,23 @@ Represents a connected IRC client.
### Third stage
- [✓] Dispatcher (select function for each command)
- [ ] Implement generic parser (extract command and pass args to command function)
- [ ] Client has nickname and username
- [ ] PASS command for authenticate
- [✓] Implement generic parser (extract command and pass args to command function)
- [✓] Client has nickname and username
- [✓] PASS command for authenticate
### Fourth stage
- [ ] Client can create/connect to channels
- [ ] JOIN command (user can create channel)
- [ ] PRIVMSG command (send message to a channel)
- [✓] Client can create/connect to channels
- [✓] JOIN command (user can create 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...*