Compare commits

...

20 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
aortigos
85d6bae03a updated tasks 2026-05-22 13:00:06 +02:00
11 changed files with 263 additions and 32 deletions

View File

@@ -33,6 +33,7 @@ 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_; isInviteOnly_ = other.isInviteOnly_;
invitedMembers_ = other.invitedMembers_; invitedMembers_ = other.invitedMembers_;
} }
@@ -55,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); }
@@ -76,6 +82,14 @@ void Channel::broadcast(const std::string &msg, const std::map<int, User> &clien
} }
} }
// 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) void Channel::setMode(std::string& mode, std::string& args)
{ {
bool value; bool value;
@@ -94,5 +108,10 @@ void Channel::setMode(std::string& mode, std::string& args)
(void) args; (void) args;
} }
void Channel::inviteMember(const User& client)
{
invitedMembers_.insert(client.getFd());
}
bool Channel::isInviteOnly() const { return isInviteOnly_; } bool Channel::isInviteOnly() const { return isInviteOnly_; }
bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; } bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; }

View File

@@ -28,6 +28,8 @@ class Channel
std::set<int> members_; std::set<int> members_;
std::set<int> operators_; std::set<int> operators_;
std::string topic_;
bool isInviteOnly_; bool isInviteOnly_;
std::set<int> invitedMembers_; std::set<int> invitedMembers_;
@@ -43,6 +45,10 @@ 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);
void removeMember(int fd); void removeMember(int fd);
@@ -53,13 +59,12 @@ class Channel
void removeOperator(int fd); void removeOperator(int fd);
bool hasOperator(int fd) const; bool hasOperator(int fd) const;
void inviteMember(User& client); // not implemented
void broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd); void broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd);
// modes // modes
void setMode(std::string& mode, std::string& args); void setMode(std::string& mode, std::string& args);
void inviteMember(const User& client);
bool isInviteOnly() const; bool isInviteOnly() const;
bool isInvited(int fd) const; bool isInvited(int fd) const;

View File

@@ -4,7 +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/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

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* Server.cpp :+: :+: :+: */ /* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: iherman- <iherman-@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/23 18:03:11 by iherman- ### ########.fr */ /* Updated: 2026/05/25 10:15:18 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -119,7 +119,10 @@ Server::Server(int port, const std::string& password) :
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_["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;

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* Server.hpp :+: :+: :+: */ /* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: iherman- <iherman-@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/23 17:16:21 by iherman- ### ########.fr */ /* Updated: 2026/05/25 10:15:08 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -68,7 +68,9 @@ class Server
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 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();

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

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

View File

@@ -6,7 +6,7 @@
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */ /* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/23 17:15:27 by iherman- #+# #+# */ /* Created: 2026/05/23 17:15:27 by iherman- #+# #+# */
/* Updated: 2026/05/23 18:35:51 by iherman- ### ########.fr */ /* Updated: 2026/05/23 20:25:02 by iherman- ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -19,7 +19,7 @@ void Server::mode_cmd(User &client, std::istringstream &ss)
std::string args; std::string args;
ss >> target >> mode; ss >> target >> mode;
std::getline(ss, args); 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 (!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")); if (target.empty() || mode.empty()) return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " MODE :Not enough parameters\r\n"));
@@ -27,7 +27,7 @@ void Server::mode_cmd(User &client, std::istringstream &ss)
std::map<std::string, Channel>::iterator channel = channels_.find(target); std::map<std::string, Channel>::iterator channel = channels_.find(target);
if (channel == channels_.end()) if (channel == channels_.end())
{ {
client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel"); client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel\r\n");
return ; return ;
} }
@@ -37,5 +37,5 @@ void Server::mode_cmd(User &client, std::istringstream &ss)
return ; return ;
} }
channel->second.setMode(mode, args); channel->second.setMode(mode, args); // args should prob be a stringstream :(
} }

View File

@@ -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
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 ### 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...*