feat(cmds): now compiling + removing the const line

This commit is contained in:
Raphael 2025-06-08 23:28:01 +02:00
parent 4531cfbb8c
commit fd011571bc
13 changed files with 53 additions and 45 deletions

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */
/* Updated: 2025/06/08 22:19:40 by sben-tay ### ########.fr */
/* Updated: 2025/06/08 22:57:21 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,8 +22,8 @@
namespace cmd
{
void dispatch(User *user, Channel *channel, Server *server, const std::string &line);
std::vector<std::string> split(const std::string &line);
void dispatch(User *user, Channel *channel, Server *server, std::string &line);
std::vector<std::string> split(std::string &line);
template <typename T>
T searchList(const std::list<T> &list, const std::string &name);
@ -42,9 +42,10 @@ namespace cmd
virtual void execute() = 0;
virtual e_code checkArgs() = 0;
~ACommand();
ACommand(User *user, Channel *channel, Server *server, const std::string &line);
ACommand(User *user, Channel *channel, Server *server, std::string &line);
};
class Cap;
class Invite;
class Join;
class Kick;
@ -54,6 +55,7 @@ namespace cmd
class Nick;
class Notice;
class Part;
class Pass;
class Ping;
class Pong;
class PrivMsg;
@ -61,8 +63,6 @@ namespace cmd
class Topic;
class Unknown;
class userCmd;
class Pass;
class Cap;
};
#include "./commands/commands.tpp"

View file

@ -6,16 +6,18 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/08 22:08:44 by sben-tay #+# #+# */
/* Updated: 2025/06/08 22:16:14 by sben-tay ### ########.fr */
/* Updated: 2025/06/08 22:42:04 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "commands.hpp"
#include "core.hpp"
class cmd::Cap : public ACommand {
public:
Cap(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Cap(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute();
};
virtual e_code checkArgs();
};

View file

@ -16,7 +16,7 @@
class cmd::Invite : public ACommand {
public:
Invite(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Invite(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -16,7 +16,7 @@
class cmd::Join : public ACommand {
public:
Join(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Join(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -16,7 +16,7 @@
class cmd::Nick : public ACommand {
public:
Nick(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Nick(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs(void);
};

View file

@ -16,7 +16,7 @@
class cmd::Notice : public ACommand {
public:
Notice(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Notice(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -16,7 +16,7 @@
class cmd::Part : public ACommand {
public:
Part(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Part(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -17,7 +17,7 @@
namespace cmd {
class Pass : public ACommand {
public:
Pass(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Pass(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -16,7 +16,7 @@
class cmd::Ping : public ACommand {
public:
Ping(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
Ping(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -16,7 +16,7 @@
class cmd::PrivMsg : public ACommand {
public:
PrivMsg(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
PrivMsg(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -16,7 +16,7 @@
class cmd::userCmd : public ACommand {
public:
userCmd(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
userCmd(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute();
virtual e_code checkArgs();
};

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/08 22:10:24 by sben-tay #+# #+# */
/* Updated: 2025/06/08 22:16:23 by sben-tay ### ########.fr */
/* Updated: 2025/06/08 22:45:35 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,17 +15,19 @@
using namespace cmd;
cmd::Cap::Cap(User *user, Channel *channel, Server *server, const std::string &line)
: ACommand(user, channel, server, line)
{
_args = split(line);
_command = "CAP";
e_code Cap::checkArgs() {
return _PARSING_OK;
}
void cmd::Cap::execute() {
DEBUG_MSG("coucou");
if (this->checkArgs() != _PARSING_OK)
return;
DEBUG_MSG("coucou");
if (_args.size() >= 2 && _args[1] == "LS") {
std::string reply = "CAP * LS :\r\n";
_sender->appendToWriteBuffer(reply);
DEBUG_MSG("Replied to CAP LS");
}
DEBUG_MSG("coucou");
}

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 16:11:56 by rparodi #+# #+# */
/* Updated: 2025/06/08 20:20:11 by sben-tay ### ########.fr */
/* Updated: 2025/06/08 23:26:07 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,9 @@
#include "ping.hpp"
#include "nick.hpp"
#include "userCmd.hpp"
#include "cap.hpp"
#include <cctype>
#include <iterator>
/**
* @brief To send the line where a command is invoqued to execute
@ -25,21 +27,26 @@
* @param channel channel where the command is sent
* @param line line send by the user
*/
std::vector<std::string> cmd::split(const std::string &line) {
std::vector<std::string> cmd::split(std::string &line) {
std::vector<std::string> args;
std::string arg;
if (line.empty())
return args;
size_t pos = line.find(' ');
size_t old_pos = 0;
while (pos != std::string::npos) {
arg = line.substr(0, pos);
if (!arg.empty()) {
args.push_back(arg);
arg = line.substr(old_pos, pos);
if (arg.empty()) {
break;
}
pos = line.find(' ');
for (size_t i = 0; i < arg.length(); i++)
arg = std::tolower(line[i]);
args.push_back(arg);
old_pos = pos;
pos = line.find(' ', old_pos + 1);
}
if (!line.empty()) {
args.push_back(line);
return args;
}
return args;
}
@ -52,11 +59,12 @@ std::vector<std::string> cmd::split(const std::string &line) {
* @param server Server where the command is sent
* @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, std::string &line) {
DEBUG_MSG("in dispatch");
std::vector<std::string> args = cmd::split(line);
DEBUG_MSG("in dispatch");
if (args.empty()) {
ERROR_MSG("Empty line");
DEBUG_MSG("Empty line");
return;
}
std::string command_name = args[0];
@ -64,15 +72,11 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
WARNING_MSG("No command found in line: " << line);
return;
}
if (command_name[0] == '/') {
command_name.erase(0, 1);
} else {
WARNING_MSG("Command does not start with '/': " << command_name);
return;
}
std::cout << command_name << std::endl;
DEBUG_MSG(command_name);
switch (command_name[0]) {
case 'c':
if (command_name == "CAP") {
if (command_name == "cap") {
Cap(user, channel, server, line).execute();
}
break;
@ -109,10 +113,10 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
}
break;
case 'p':
if (command_name == "PASS") {
if (command_name == "pass") {
Pass(user, channel, server, line).execute();
}
if (command_name == "PING") {
if (command_name == "ping") {
Ping(user, channel, server, line).execute();
}
// if (command_name == "part") {
@ -120,7 +124,7 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
// }
break;
case 'u':
if (command_name == "USER") {
if (command_name == "user") {
userCmd(user, channel, server, line).execute();
}
break;
@ -133,7 +137,7 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
(void)line;
}
cmd::ACommand::ACommand(::User *user, ::Channel *channel, ::Server *server, const std::string &line) : _sender(user), _channel(channel), _server(server) {
cmd::ACommand::ACommand(::User *user, ::Channel *channel, ::Server *server, std::string &line) : _sender(user), _channel(channel), _server(server) {
DEBUG_MSG("ACommand constructor called");
_args = split(line);
_command = _args.at(0);