Merge pull request #7 from EniumRaphael/raph

feat(cmd): adding the start of parsing commands
This commit is contained in:
Raphaël 2025-06-02 01:24:05 +02:00 committed by GitHub
commit 95155af988
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 327 additions and 26 deletions

View file

@ -6,7 +6,7 @@
# By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ # # By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# # # 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 # Sources
SRC = sources/channel/channel.cpp \ 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/PollManager.cpp \
sources/core/Server.cpp \ sources/core/Server.cpp \
sources/core/check.cpp \ sources/core/check.cpp \
sources/core/main.cpp \ sources/core/main.cpp \
sources/core/parser.cpp \ sources/core/parser.cpp \
sources/user/user.cpp \ sources/user/user.cpp
sources/commands/commands.cpp \
sources/commands/pass.cpp \
sources/commands/invite.cpp
INC_DIR = include/core \ INC_DIR = include/core \
include/commands \ include/commands \

View file

@ -19,6 +19,7 @@
irssi irssi
tmux tmux
lld lld
fd
] ++ ( ] ++ (
if pkgs.stdenv.isLinux then [ if pkgs.stdenv.isLinux then [
valgrind valgrind

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:17:31 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 { class cmd::Invite : public ACommand {
public: public:
Invite(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void); virtual void execute(void);
virtual bool checkArgs(); virtual bool checkArgs();
}; };

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:17:31 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 { class cmd::Join : public ACommand {
public: public:
Join(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void); virtual void execute(void);
virtual bool checkArgs(); virtual bool checkArgs();
}; };

22
include/commands/nick.hpp Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nick.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
};

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* notice.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
};

22
include/commands/part.hpp Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
};

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* privmsg.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
};

47
sources/commands/nick.cpp Normal file
View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* notice.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
}

55
sources/commands/part.cpp Normal file
View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* privmsg.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
}

View file

@ -6,7 +6,7 @@
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */ /* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 20:37:12 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; return this->_nickname;
} }
void User::setUsername(const std::string &username) void User::setUsername(const std::string &username) {
{
_username = username; _username = username;
_hasUser = true; _hasUser = true;
checkRegistration(); checkRegistration();
} }
// Setter for nickname (with basic checks) // Setter for nickname (with basic checks)
void User::setNickname(const std::string &nickname) void User::setNickname(const std::string &nickname) {
{ if (nickname.empty()) {
if (nickname.empty())
{
throw std::invalid_argument("Nickname cannot be 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'"); 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"); 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"); throw std::invalid_argument("The nickname is the same");
} } else {
else
{
_nickname = nickname; _nickname = nickname;
_hasNick = true; _hasNick = true;
checkRegistration(); checkRegistration();