From 49a9f4b40a14d7ff8ae0565ff32c87f3e1c6654f Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 18 Jun 2025 12:59:15 +0200 Subject: [PATCH] feat(cmd/topic): topic command is now working --- include/commands/topic.hpp | 22 +++++++++++++++ sources/commands/topic.cpp | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 include/commands/topic.hpp create mode 100644 sources/commands/topic.cpp diff --git a/include/commands/topic.hpp b/include/commands/topic.hpp new file mode 100644 index 0000000..7656a84 --- /dev/null +++ b/include/commands/topic.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* topic.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/06/02 22:48:36 by rparodi #+# #+# */ +/* Updated: 2025/06/17 22:10:31 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "commands.hpp" + +class cmd::Topic : public ACommand { + public: + Topic(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {} + virtual void execute(void); + virtual e_code checkArgs(); +}; diff --git a/sources/commands/topic.cpp b/sources/commands/topic.cpp new file mode 100644 index 0000000..c9379c9 --- /dev/null +++ b/sources/commands/topic.cpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* topic.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sben-tay +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ +/* Updated: 2025/06/18 12:51:40 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "commands.hpp" +#include "core.hpp" +#include "logs.hpp" +#include "topic.hpp" + +using namespace cmd; + +e_code Topic::checkArgs() { + if (_args.size() < 2) { + std::string msg461 = ":localhost 461 " + this->_sender->getNickname() + " " + this->_command + " :Not enough parameters\r\n"; + this->_sender->appendToWriteBuffer(msg461); + return ERR_NEEDMOREPARAMS; + } + this->_cTarget = searchList(this->_channels, this->_args[1]); + if (this->_cTarget == NULL) + return ERR_NOSUCHCHANNEL; + if (this->_cTarget->getProtectTopic()) { + if (searchList(this->_cTarget->getOperators(), this->_sender->getName()) == NULL) + return ERR_CHANOPRIVSNEEDED; + } else { + if (searchList(this->_cTarget->getUsers(), this->_sender->getName()) == NULL) + return ERR_NOTONCHANNEL; + } + return _PARSING_OK; +} + +/** + * @brief Execute the Topic + * @note To change the topic of the channel + */ + +void Topic::execute() { + if (checkArgs() != _PARSING_OK) { + ERROR_MSG("Invalid arguments for TOPIC command (see warning message)"); + return; + } + if (this->_cTarget->getTopic().empty()) { + std::string msg331 = ":localhost 331 " + this->_sender->getNickname() + " " + this->_cTarget->getName() + " :No topic is set" + "\r\n"; + this->_sender->appendToWriteBuffer(msg331); + } else { + std::string msg332 = ":localhost 332 " + this->_sender->getNickname() + " " + this->_cTarget->getName() + " :" + _cTarget->getTopic() + "\r\n"; + this->_sender->appendToWriteBuffer(msg332); + } +}