commands whois, whowas created and fixed, update ping, nick, commands.cpp, update Server and user
This commit is contained in:
parent
597aed08d8
commit
6ffc1e5534
16 changed files with 250 additions and 34 deletions
|
|
@ -25,6 +25,8 @@
|
|||
#include "userCmd.hpp"
|
||||
#include "pass.hpp"
|
||||
#include "part.hpp"
|
||||
#include "whois.hpp"
|
||||
#include "whowas.hpp"
|
||||
#include <iterator>
|
||||
|
||||
/**
|
||||
|
|
@ -40,7 +42,15 @@ std::vector<std::string> cmd::split(std::string &line) {
|
|||
size_t start = 0;
|
||||
size_t end;
|
||||
|
||||
while ((end = line.find(' ', start)) != std::string::npos) {
|
||||
while (start < line.length()) {
|
||||
if (line[start] == ':') {
|
||||
std::string arg = line.substr(start + 1);
|
||||
args.push_back(arg);
|
||||
break;
|
||||
}
|
||||
end = line.find(' ', start);
|
||||
if (end == std::string::npos)
|
||||
end = line.length();
|
||||
std::string arg = line.substr(start, end - start);
|
||||
if (!arg.empty()) {
|
||||
for (size_t i = 0; i < arg.length(); ++i)
|
||||
|
|
@ -49,12 +59,6 @@ std::vector<std::string> cmd::split(std::string &line) {
|
|||
}
|
||||
start = end + 1;
|
||||
}
|
||||
if (start < line.length()) {
|
||||
std::string arg = line.substr(start);
|
||||
for (size_t i = 0; i < arg.length(); ++i)
|
||||
arg[i] = std::tolower(arg[i]);
|
||||
args.push_back(arg);
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
|
|
@ -135,6 +139,13 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &
|
|||
userCmd(user, channel, server, line).execute();
|
||||
}
|
||||
break;
|
||||
case 'w':
|
||||
if (command_name == "whois") {
|
||||
Whois(user, channel, server, line).execute();
|
||||
} else if (command_name == "whowas") {
|
||||
Whowas(user, channel, server, line).execute();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
WARNING_MSG("Unknown command: " << command_name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/16 18:30:15 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/17 16:56:36 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,8 +21,9 @@ e_code cmd::Nick::checkArgs() {
|
|||
return ERR_NONICKNAMEGIVEN;
|
||||
}
|
||||
User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin
|
||||
if (existing != NULL) {
|
||||
_sender->appendToWriteBuffer(":" + _sender->getPrefix() + " 433 * " + _args[1] + " :Nickname is already in use\r\n");
|
||||
if (existing != NULL) {
|
||||
std::string msg433 = ":" + _sender->getPrefix() + " 433 * " + _args[1] + " :Nickname is already in use\r\n";
|
||||
_sender->appendToWriteBuffer(msg433);
|
||||
return ERR_NICKNAMEINUSE;
|
||||
}
|
||||
return _PARSING_OK;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/14 23:25:45 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/17 16:58:06 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -36,6 +36,7 @@ void Ping::execute() {
|
|||
ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
|
||||
return;
|
||||
}
|
||||
_sender->appendToWriteBuffer("PONG " + _args[1] + "\r\n");
|
||||
std::string msgpong = "PONG " + _args[1] + "\r\n";
|
||||
_sender->appendToWriteBuffer(msgpong);
|
||||
DEBUG_MSG(_sender->getWriteBuffer());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/08 19:16:10 by sben-tay #+# #+# */
|
||||
/* Updated: 2025/06/12 17:55:06 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/17 15:41:25 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -32,5 +32,7 @@ void cmd::userCmd::execute() {
|
|||
DEBUG_MSG("Setting username to " << _args[1]);
|
||||
_sender->setUsername(_args[1]);
|
||||
_sender->setHasUser(true);
|
||||
_sender->setRealname(_args[4]);
|
||||
_sender->checkRegistration();
|
||||
}
|
||||
|
||||
|
|
|
|||
52
sources/commands/whois.cpp
Normal file
52
sources/commands/whois.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* whois.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/17 14:01:46 by sben-tay #+# #+# */
|
||||
/* Updated: 2025/06/17 16:55:08 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "whois.hpp"
|
||||
#include "logs.hpp"
|
||||
|
||||
using namespace cmd;
|
||||
|
||||
e_code Whois::checkArgs() {
|
||||
if (_args.size() < 2 || _args[1].empty()) {
|
||||
WARNING_MSG("Whois: Not enough arguments");
|
||||
return ERR_NONICKNAMEGIVEN;
|
||||
}
|
||||
return _PARSING_OK;
|
||||
}
|
||||
|
||||
void Whois::execute()
|
||||
{
|
||||
if (checkArgs() != _PARSING_OK) {
|
||||
ERROR_MSG("Invalid arguments for Whois command (see warning message)");
|
||||
return;
|
||||
}
|
||||
|
||||
User* requestingUser = _sender;
|
||||
User* targetUser = searchList<User*>(_users, _args[1]);
|
||||
if (targetUser == NULL) {
|
||||
std::string errMsg = ":localhost 401 " + requestingUser->getNickname() + " " + _args[1] + " :No such nick/channel\r\n";
|
||||
requestingUser->appendToWriteBuffer(errMsg);
|
||||
return;
|
||||
}
|
||||
|
||||
std::string msg311 = ":localhost 311 " + requestingUser->getNickname() + " " +
|
||||
targetUser->getNickname() + " " +
|
||||
targetUser->getUsername() + " " +
|
||||
targetUser->getHostname() + " * :" +
|
||||
targetUser->getRealname() + "\r\n";
|
||||
|
||||
std::string msg318 = ":localhost 318 " + requestingUser->getNickname() + " " +
|
||||
targetUser->getNickname() + " :End of /WHOIS list\r\n";
|
||||
|
||||
requestingUser->appendToWriteBuffer(msg311);
|
||||
requestingUser->appendToWriteBuffer(msg318);
|
||||
}
|
||||
45
sources/commands/whowas.cpp
Normal file
45
sources/commands/whowas.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* whowas.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/17 17:02:03 by sben-tay #+# #+# */
|
||||
/* Updated: 2025/06/17 17:05:21 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "whowas.hpp"
|
||||
#include "logs.hpp"
|
||||
|
||||
e_code cmd::Whowas::checkArgs() {
|
||||
if (_args.size() < 2 || _args[1].empty()) {
|
||||
WARNING_MSG("Whowas: Not enough arguments");
|
||||
return ERR_NONICKNAMEGIVEN;
|
||||
}
|
||||
return _PARSING_OK;
|
||||
}
|
||||
|
||||
void cmd::Whowas::execute() {
|
||||
if (checkArgs() != _PARSING_OK) {
|
||||
ERROR_MSG("Invalid arguments for Whowas command");
|
||||
return;
|
||||
}
|
||||
|
||||
User* requestingUser = _sender;
|
||||
std::string nickname = _args[1];
|
||||
|
||||
std::string msg406 = ":localhost 406 " + requestingUser->getNickname() + " " +
|
||||
nickname + " :There was no such nickname\r\n";
|
||||
std::string msg369 = ":localhost 369 " + requestingUser->getNickname() + " " +
|
||||
nickname + " :End of WHOWAS\r\n";
|
||||
|
||||
/*
|
||||
plus tard on pourrait implémenter une liste de whowas sur les user deconnectés
|
||||
pour que la fonction soit complete.
|
||||
*/
|
||||
|
||||
requestingUser->appendToWriteBuffer(msg406);
|
||||
requestingUser->appendToWriteBuffer(msg369);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue