Merge pull request #9 from EniumRaphael/raph

feat(cmd/p*ng): adding ping/pong commands
This commit is contained in:
Raphaël 2025-06-03 17:56:48 +02:00 committed by GitHub
commit 099017381a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 140 additions and 11 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/06/02 01:18:57 by rparodi ### ########.fr # # Updated: 2025/06/03 16:50:01 by rparodi ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@ -32,6 +32,8 @@ SRC = sources/channel/channel.cpp \
sources/commands/notice.cpp \ sources/commands/notice.cpp \
sources/commands/part.cpp \ sources/commands/part.cpp \
sources/commands/pass.cpp \ sources/commands/pass.cpp \
sources/commands/ping.cpp \
sources/commands/pong.cpp \
sources/commands/privmsg.cpp \ sources/commands/privmsg.cpp \
sources/core/PollManager.cpp \ sources/core/PollManager.cpp \
sources/core/Server.cpp \ sources/core/Server.cpp \
@ -80,7 +82,7 @@ re: header fclean all
$(NAME): $(OBJ) $(NAME): $(OBJ)
@mkdir -p $(OBJDIRNAME) @mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(NAME) $(OBJ) -fuse-ld=lld @$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(NAME) $(OBJ) #-fuse-ld=lld
# Creating the objects # Creating the objects
$(OBJDIRNAME)/%.o: %.cpp $(OBJDIRNAME)/%.o: %.cpp
@ -112,17 +114,16 @@ test: debug
'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""' 'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""'
@tmux attach -t $(SESSION) @tmux attach -t $(SESSION)
# @tmux split-window -h -p 70 -t $(SESSION):0 \
run: re # 'bash -lc "irssi -c localhost -p $(PORT) -w irc || exec yes \"irssi exit code: $?\""'
run: all
@printf '$(GREY) now running with\n\t- Port:\t\t$(GREEN)$(PORT)$(GREY)\n\t- Password:\t$(GREEN)irc$(END)\n' @printf '$(GREY) now running with\n\t- Port:\t\t$(GREEN)$(PORT)$(GREY)\n\t- Password:\t$(GREEN)irc$(END)\n'
@if tmux has-session -t $(SESSION) 2>/dev/null; then \ @if tmux has-session -t $(SESSION) 2>/dev/null; then \
tmux kill-session -t $(SESSION); \ tmux kill-session -t $(SESSION); \
fi fi
@tmux new-session -d -s $(SESSION) \ @tmux new-session -d -s $(SESSION) \
'bash -lc "./$(NAME) $(PORT) irc; exec bash"' 'bash -lc "./$(NAME) $(PORT) irc; exec bash"'
@tmux split-window -h -p 70 -t $(SESSION):0 \ @tmux split-window -v -p 50 -t $(SESSION):0 \
'bash -lc "irssi -c localhost -p $(PORT) -w irc || exec yes \"irssi exit code: $?\""'
@tmux split-window -v -p 50 -t $(SESSION):0.1 \
'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""' 'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""'
@tmux attach -t $(SESSION) @tmux attach -t $(SESSION)

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

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ping.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/02 22:48:36 by rparodi #+# #+# */
/* Updated: 2025/06/02 22:48:48 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "commands.hpp"
class cmd::Ping : public ACommand {
public:
Ping(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual bool checkArgs();
};

21
include/commands/pong.hpp Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pong.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/02 22:48:36 by rparodi #+# #+# */
/* Updated: 2025/06/02 23:01:19 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "commands.hpp"
class cmd::Pong {
public:
Pong(void){}
clock_t answer(clock_t start);
};

View file

@ -6,13 +6,14 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 16:11:56 by rparodi #+# #+# */ /* Created: 2025/05/24 16:11:56 by rparodi #+# #+# */
/* Updated: 2025/05/29 12:48:13 by rparodi ### ########.fr */ /* Updated: 2025/06/03 16:46:58 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "commands.hpp" #include "commands.hpp"
#include "logs.hpp" #include "logs.hpp"
#include "pass.hpp" #include "pass.hpp"
#include "ping.hpp"
/** /**
* @brief To send the line where a command is invoqued to execute * @brief To send the line where a command is invoqued to execute
@ -24,6 +25,8 @@
std::vector<std::string> cmd::split(const std::string &line) { std::vector<std::string> cmd::split(const std::string &line) {
std::vector<std::string> args; std::vector<std::string> args;
std::string arg; std::string arg;
if (line.empty())
return args;
size_t pos = line.find(' '); size_t pos = line.find(' ');
while (pos != std::string::npos) { while (pos != std::string::npos) {
arg = line.substr(0, pos); arg = line.substr(0, pos);
@ -47,7 +50,13 @@ std::vector<std::string> cmd::split(const std::string &line) {
* @param line input line from the user * @param line input line from the user
*/ */
void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::string &line) { void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::string &line) {
std::string command_name = cmd::split(line).at(0); DEBUG_MSG("in dispatch");
std::vector<std::string> args = cmd::split(line);
if (args.empty()) {
ERROR_MSG("Empty line");
return;
}
std::string command_name = args[0];
if (command_name.empty()) { if (command_name.empty()) {
WARNING_MSG("No command found in line: " << line); WARNING_MSG("No command found in line: " << line);
return; return;
@ -95,6 +104,9 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
if (command_name == "pass") { if (command_name == "pass") {
Pass(user, channel, server, line).execute(); Pass(user, channel, server, line).execute();
} }
if (command_name == "ping") {
Ping(user, channel, server, line).execute();
}
// if (command_name == "part") { // if (command_name == "part") {
// Part(user, channel, server, line).execute(); // Part(user, channel, server, line).execute();
// } // }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/02 22:41:12 by rparodi ### ########.fr */ /* Updated: 2025/06/03 15:04:10 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,7 @@ bool Pass::checkArgs() {
WARNING_MSG("Not correct for Pass command"); WARNING_MSG("Not correct for Pass command");
return false; return false;
} }
DEBUG_MSG("coucou");
if (_sender->isRegistered()) { if (_sender->isRegistered()) {
WARNING_MSG(_sender->getName() << " is already is already log in the server !"); WARNING_MSG(_sender->getName() << " is already is already log in the server !");
return false; return false;
@ -35,8 +36,10 @@ bool Pass::checkArgs() {
void Pass::execute() { void Pass::execute() {
if (checkArgs() == false) { if (checkArgs() == false) {
ERROR_MSG("Invalid arguments for Pass command (see warning message)"); ERROR_MSG("Invalid arguments for Pass command (see warning message)");
DEBUG_MSG("skill issues");
return; return;
} }
DEBUG_MSG("mais pas trop skill issues");
if (_args.at(1) != _server->getPassword()) { if (_args.at(1) != _server->getPassword()) {
ERROR_MSG("The password is incorrect"); ERROR_MSG("The password is incorrect");
return; return;

42
sources/commands/ping.cpp Normal file
View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ping.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/03 14:49:27 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ping.hpp"
#include "commands.hpp"
#include "logs.hpp"
#include "pong.hpp"
#include <ctime>
using namespace cmd;
bool Ping::checkArgs() {
if (_args.size() < 3) {
WARNING_MSG("Not enough arguments for PING command");
return false;
}
return true;
}
/**
* @brief Execute the Ping
* @note To send a private message to a user / a channel
*/
void Ping::execute() {
clock_t start = clock() / CLOCKS_PER_SEC;
if (checkArgs() == false) {
ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
return;
}
clock_t diff = Pong().answer(start);
INFO_MSG(diff);
// check how the com
}

26
sources/commands/pong.cpp Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pong.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/02 23:04:53 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "commands.hpp"
#include "logs.hpp"
#include "pong.hpp"
#include <ctime>
using namespace cmd;
/**
* @brief Execute the Pong
* @note answer to PING command (managed by the server)
*/
clock_t Pong::answer(clock_t start) {
return (start - (clock() / CLOCKS_PER_SEC));
}

View file

@ -6,12 +6,13 @@
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */ /* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
/* Updated: 2025/05/29 12:17:15 by rparodi ### ########.fr */ /* Updated: 2025/06/03 14:56:06 by rparodi ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
#include "color.hpp" #include "color.hpp"
#include "server.hpp" #include "server.hpp"
#include "commands.hpp"
#include "core.hpp" #include "core.hpp"
#include "PollManager.hpp" #include "PollManager.hpp"
#include "logs.hpp" #include "logs.hpp"
@ -107,6 +108,7 @@ void Server::start()
{ {
// This prints every command/message received from any client // This prints every command/message received from any client
std::cout << "Client " << fd << " says: " << cmd << std::endl; std::cout << "Client " << fd << " says: " << cmd << std::endl;
cmd::dispatch(_users[fd], NULL, this, cmd);
} }
} }
} }