Merge pull request 'Topic cmd' (#39) from topic-cmd into main
Reviewed-on: #39
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user