From fd011571bcfbb551c619855b13af41e4a9ed4487 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sun, 8 Jun 2025 23:28:01 +0200 Subject: [PATCH] feat(cmds): now compiling + removing the const line --- include/commands.hpp | 12 ++++----- include/commands/cap.hpp | 8 +++--- include/commands/invite.hpp | 2 +- include/commands/join.hpp | 2 +- include/commands/nick.hpp | 2 +- include/commands/notice.hpp | 2 +- include/commands/part.hpp | 2 +- include/commands/pass.hpp | 2 +- include/commands/ping.hpp | 2 +- include/commands/privmsg.hpp | 2 +- include/commands/userCmd.hpp | 2 +- sources/commands/cap.cpp | 14 ++++++----- sources/commands/commands.cpp | 46 +++++++++++++++++++---------------- 13 files changed, 53 insertions(+), 45 deletions(-) diff --git a/include/commands.hpp b/include/commands.hpp index 206f5b1..7029c34 100644 --- a/include/commands.hpp +++ b/include/commands.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 split(const std::string &line); + void dispatch(User *user, Channel *channel, Server *server, std::string &line); + std::vector split(std::string &line); template T searchList(const std::list &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" diff --git a/include/commands/cap.hpp b/include/commands/cap.hpp index ca66fd1..57c0814 100644 --- a/include/commands/cap.hpp +++ b/include/commands/cap.hpp @@ -6,16 +6,18 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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(); -}; \ No newline at end of file + virtual e_code checkArgs(); +}; diff --git a/include/commands/invite.hpp b/include/commands/invite.hpp index 8192dab..b9e5179 100644 --- a/include/commands/invite.hpp +++ b/include/commands/invite.hpp @@ -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(); }; diff --git a/include/commands/join.hpp b/include/commands/join.hpp index c5ab577..63049c7 100644 --- a/include/commands/join.hpp +++ b/include/commands/join.hpp @@ -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(); }; diff --git a/include/commands/nick.hpp b/include/commands/nick.hpp index ee2234a..0d26160 100644 --- a/include/commands/nick.hpp +++ b/include/commands/nick.hpp @@ -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); }; diff --git a/include/commands/notice.hpp b/include/commands/notice.hpp index 8e7b669..6dc4aab 100644 --- a/include/commands/notice.hpp +++ b/include/commands/notice.hpp @@ -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(); }; diff --git a/include/commands/part.hpp b/include/commands/part.hpp index 898603d..471597e 100644 --- a/include/commands/part.hpp +++ b/include/commands/part.hpp @@ -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(); }; diff --git a/include/commands/pass.hpp b/include/commands/pass.hpp index 8965c9a..56849eb 100644 --- a/include/commands/pass.hpp +++ b/include/commands/pass.hpp @@ -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(); }; diff --git a/include/commands/ping.hpp b/include/commands/ping.hpp index 4e8b0d2..3de2b97 100644 --- a/include/commands/ping.hpp +++ b/include/commands/ping.hpp @@ -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(); }; diff --git a/include/commands/privmsg.hpp b/include/commands/privmsg.hpp index a55c84d..e85315c 100644 --- a/include/commands/privmsg.hpp +++ b/include/commands/privmsg.hpp @@ -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(); }; diff --git a/include/commands/userCmd.hpp b/include/commands/userCmd.hpp index d27efe1..c64fb01 100644 --- a/include/commands/userCmd.hpp +++ b/include/commands/userCmd.hpp @@ -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(); }; \ No newline at end of file diff --git a/sources/commands/cap.cpp b/sources/commands/cap.cpp index a30f546..71f5073 100644 --- a/sources/commands/cap.cpp +++ b/sources/commands/cap.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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"); } diff --git a/sources/commands/commands.cpp b/sources/commands/commands.cpp index 467924b..c11b7e5 100644 --- a/sources/commands/commands.cpp +++ b/sources/commands/commands.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include /** * @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 cmd::split(const std::string &line) { +std::vector cmd::split(std::string &line) { std::vector 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 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 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);