diff --git a/Makefile b/Makefile index b621a04..8eba51c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: omoudni +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/05/02 15:40:00 by rparodi #+# #+# # -# Updated: 2025/05/29 12:31:53 by rparodi ### ########.fr # +# Updated: 2025/06/02 01:18:57 by rparodi ### ########.fr # # # #******************************************************************************# @@ -25,15 +25,20 @@ SESSION = test-irc # Sources SRC = sources/channel/channel.cpp \ + sources/commands/commands.cpp \ + sources/commands/invite.cpp \ + sources/commands/join.cpp \ + sources/commands/nick.cpp \ + sources/commands/notice.cpp \ + sources/commands/part.cpp \ + sources/commands/pass.cpp \ + sources/commands/privmsg.cpp \ sources/core/PollManager.cpp \ sources/core/Server.cpp \ sources/core/check.cpp \ sources/core/main.cpp \ sources/core/parser.cpp \ - sources/user/user.cpp \ - sources/commands/commands.cpp \ - sources/commands/pass.cpp \ - sources/commands/invite.cpp + sources/user/user.cpp INC_DIR = include/core \ include/commands \ diff --git a/flake.nix b/flake.nix index b27d8ca..ba771de 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,7 @@ irssi tmux lld + fd ] ++ ( if pkgs.stdenv.isLinux then [ valgrind diff --git a/include/commands/invite.hpp b/include/commands/invite.hpp index 11973ed..be4ece6 100644 --- a/include/commands/invite.hpp +++ b/include/commands/invite.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */ -/* Updated: 2025/05/26 16:27:42 by rparodi ### ########.fr */ +/* Updated: 2025/06/02 00:34:16 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ class cmd::Invite : public ACommand { public: + Invite(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); virtual bool checkArgs(); }; diff --git a/include/commands/join.hpp b/include/commands/join.hpp index 135d235..ce8d1f5 100644 --- a/include/commands/join.hpp +++ b/include/commands/join.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */ -/* Updated: 2025/05/26 22:43:47 by rparodi ### ########.fr */ +/* Updated: 2025/06/02 00:33:23 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ class cmd::Join : public ACommand { public: + Join(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); virtual bool checkArgs(); }; diff --git a/include/commands/nick.hpp b/include/commands/nick.hpp new file mode 100644 index 0000000..fdc88ac --- /dev/null +++ b/include/commands/nick.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* nick.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */ +/* Updated: 2025/06/02 00:40:18 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "commands.hpp" + +class cmd::Nick : public ACommand { + public: + Nick(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} + virtual void execute(void); + virtual bool checkArgs(); +}; diff --git a/include/commands/notice.hpp b/include/commands/notice.hpp new file mode 100644 index 0000000..256eb28 --- /dev/null +++ b/include/commands/notice.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* notice.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */ +/* Updated: 2025/06/02 01:18:19 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "commands.hpp" + +class cmd::Notice : public ACommand { + public: + Notice(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} + virtual void execute(void); + virtual bool checkArgs(); +}; diff --git a/include/commands/part.hpp b/include/commands/part.hpp new file mode 100644 index 0000000..4a2bba5 --- /dev/null +++ b/include/commands/part.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */ +/* Updated: 2025/06/02 00:51:23 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "commands.hpp" + +class cmd::Part : public ACommand { + public: + Part(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} + virtual void execute(void); + virtual bool checkArgs(); +}; diff --git a/include/commands/privmsg.hpp b/include/commands/privmsg.hpp new file mode 100644 index 0000000..6c012d2 --- /dev/null +++ b/include/commands/privmsg.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* privmsg.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */ +/* Updated: 2025/06/02 00:58:18 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "commands.hpp" + +class cmd::PrivMsg : public ACommand { + public: + PrivMsg(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} + virtual void execute(void); + virtual bool checkArgs(); +}; diff --git a/sources/commands/nick.cpp b/sources/commands/nick.cpp new file mode 100644 index 0000000..1eec360 --- /dev/null +++ b/sources/commands/nick.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* nick.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ +/* Updated: 2025/06/02 00:55:45 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "nick.hpp" +#include "commands.hpp" +#include "logs.hpp" + +using namespace cmd; + +bool Nick::checkArgs() { + if (this->_uTarget->isRegistered() == false) { + WARNING_MSG("User is not registered for Nick command"); + INFO_MSG("You can only Nick registered users"); + return false; + } + if (_args.size() < 2) { + WARNING_MSG("Not enough arguments for Nick command"); + return false; + } + _uTarget = searchList(this->_users, _args.at(1)); + if (this->_uTarget != NULL) { + WARNING_MSG(_uTarget->getName() << " is already taken") + return false; + } + return true; +} + +/** + * @brief Execute the Nick command + * @note To change the nickname of the user + */ +void Nick::execute() { + if (checkArgs() == false) { + ERROR_MSG("Invalid arguments for Nick command (see warning message)"); + return; + } + // check how the com +} diff --git a/sources/commands/notice.cpp b/sources/commands/notice.cpp new file mode 100644 index 0000000..9985b3c --- /dev/null +++ b/sources/commands/notice.cpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* notice.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ +/* Updated: 2025/06/02 01:18:05 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "notice.hpp" +#include "commands.hpp" +#include "logs.hpp" + +using namespace cmd; + +bool Notice::checkArgs() { + if (_args.size() < 3) { + WARNING_MSG("Not enough arguments for NOTICE command"); + return false; + } + if (_args.at(1).at(0) != '#') { + _uTarget = searchList(this->_users, _args.at(2)); + if (this->_uTarget == NULL) { + WARNING_MSG("User not found"); + return false; + } + if (this->_uTarget->isRegistered() == false) { + WARNING_MSG("User is not registered for NOTICE command"); + INFO_MSG("You can only NOTICE registered users"); + return false; + } + } else { + _cTarget = searchList(_channels, _args.at(1)); + if (_cTarget == NULL) { + WARNING_MSG("Channel not found for NOTICE command"); + INFO_MSG("You can only NOTICE users to channels you are in"); + return false; + } else + _args.at(1).erase(0, 1); + } + return true; +} + +/** + * @brief Execute the PrivMsg command + * @note To send a private message to a user / a channel + */ +void Notice::execute() { + if (checkArgs() == false) { + ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)"); + return; + } + // check how the com +} diff --git a/sources/commands/part.cpp b/sources/commands/part.cpp new file mode 100644 index 0000000..584eb39 --- /dev/null +++ b/sources/commands/part.cpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ +/* Updated: 2025/06/02 00:55:56 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "part.hpp" +#include "commands.hpp" +#include "logs.hpp" + +using namespace cmd; + +bool Part::checkArgs() { + if (_args.size() < 2) { + WARNING_MSG("Not enough arguments for PART command"); + return false; + } + if (_args.at(1).at(0) != '#') { + WARNING_MSG("Invalid channel name for PART command"); + INFO_MSG("Channel names must start with a '#' character"); + return false; + } else + _args.at(1).erase(0, 1); + _cTarget = searchList(_channels, _args.at(1)); + if (_cTarget == NULL) { + WARNING_MSG("Channel not found for PART command"); + INFO_MSG("You can only Part users to channels you are in"); + return false; + } else + _args.at(1).erase(0, 1); + if (searchList(this->_cTarget->getUsers(), this->_uTarget->getName())) { + WARNING_MSG("User is not in the channel he wants to leave"); + INFO_MSG("You cannot leave a channel where ur not a member"); + return false; + } + return true; +} + +/** + * @brief Execute the Part command + * @note To leave a channel givent in parameter + */ +void Part::execute() { + if (checkArgs() == false) { + ERROR_MSG("Invalid arguments for Part command (see warning message)"); + return; + } + // check how the com +} diff --git a/sources/commands/privmsg.cpp b/sources/commands/privmsg.cpp new file mode 100644 index 0000000..c1c3cd4 --- /dev/null +++ b/sources/commands/privmsg.cpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* privmsg.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ +/* Updated: 2025/06/02 01:15:00 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "privmsg.hpp" +#include "commands.hpp" +#include "logs.hpp" + +using namespace cmd; + +bool PrivMsg::checkArgs() { + if (_args.size() < 3) { + WARNING_MSG("Not enough arguments for PRIVMSG command"); + return false; + } + if (_args.at(1).at(0) != '#') { + _uTarget = searchList(this->_users, _args.at(2)); + if (this->_uTarget == NULL) { + WARNING_MSG("User not found"); + return false; + } + if (this->_uTarget->isRegistered() == false) { + WARNING_MSG("User is not registered for PRIVMSG command"); + INFO_MSG("You can only privmsg registered users"); + return false; + } + } else { + _cTarget = searchList(_channels, _args.at(1)); + if (_cTarget == NULL) { + WARNING_MSG("Channel not found for PRIVMSG command"); + INFO_MSG("You can only privmsg users to channels you are in"); + return false; + } else + _args.at(1).erase(0, 1); + } + return true; +} + +/** + * @brief Execute the PrivMsg command + * @note To send a private message to a user / a channel + */ +void PrivMsg::execute() { + if (checkArgs() == false) { + ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)"); + return; + } + // check how the com +} diff --git a/sources/user/user.cpp b/sources/user/user.cpp index bb4de98..5287c7d 100644 --- a/sources/user/user.cpp +++ b/sources/user/user.cpp @@ -6,7 +6,7 @@ /* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */ -/* Updated: 2025/05/29 12:38:46 by rparodi ### ########.fr */ +/* Updated: 2025/05/29 13:30:51 by rparodi ### ########.fr */ /* */ /******************************************************************************/ @@ -31,34 +31,23 @@ std::string User::getName() const { return this->_nickname; } -void User::setUsername(const std::string &username) -{ +void User::setUsername(const std::string &username) { _username = username; _hasUser = true; checkRegistration(); } // Setter for nickname (with basic checks) -void User::setNickname(const std::string &nickname) -{ - if (nickname.empty()) - { +void User::setNickname(const std::string &nickname) { + if (nickname.empty()) { throw std::invalid_argument("Nickname cannot be empty"); - } - else if (nickname == "anonymous") - { + } else if (nickname == "anonymous") { throw std::invalid_argument("Nickname cannot be 'anonymous'"); - } - else if (nickname.length() > 9) - { + } else if (nickname.length() > 9) { throw std::length_error("Nickname is too long"); - } - else if (nickname == _nickname) - { + } else if (nickname == _nickname) { throw std::invalid_argument("The nickname is the same"); - } - else - { + } else { _nickname = nickname; _hasNick = true; checkRegistration();