Topic cmd #39

Merged
aortigos merged 1 commits from topic-cmd into main 2026-05-25 08:32:06 +00:00
6 changed files with 71 additions and 2 deletions
Showing only changes of commit 1497a3c979 - Show all commits

View File

@@ -33,6 +33,7 @@ Channel& Channel::operator=(const Channel &other)
name_ = other.name_;
members_ = other.members_;
operators_ = other.operators_;
topic_ = other.topic_;
isInviteOnly_ = other.isInviteOnly_;
invitedMembers_ = other.invitedMembers_;
}
@@ -81,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)
{
bool value;

View File

@@ -28,6 +28,8 @@ class Channel
std::set<int> members_;
std::set<int> operators_;
std::string topic_;
bool isInviteOnly_;
std::set<int> invitedMembers_;
@@ -43,6 +45,10 @@ 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);
void removeMember(int fd);

View File

@@ -5,6 +5,7 @@ SRC = main.cpp Server/Server.cpp User/User.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/25 09:46:07 by aortigos ### ########.fr */
/* Updated: 2026/05/25 10:15:18 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -121,6 +121,7 @@ Server::Server(int port, const std::string& password) :
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_

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/25 09:46:21 by aortigos ### ########.fr */
/* Updated: 2026/05/25 10:15:08 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -70,6 +70,7 @@ class Server
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();

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