Merge pull request #20 from EniumRaphael/samy

Patch v1.2.1 – WHOIS, WHOWAS, Connexion distante & Refactors
This commit is contained in:
Samy BEN TAYEB 2025-06-17 18:20:26 +02:00 committed by GitHub
commit b188655ea8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 250 additions and 34 deletions

View file

@ -6,7 +6,7 @@
# By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ # # By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# # # Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
# Updated: 2025/06/10 13:40:37 by rparodi ### ########.fr # # Updated: 2025/06/17 17:06:04 by sben-tay ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -40,6 +40,8 @@ SRC = sources/channel/channel.cpp \
sources/commands/pong.cpp \ sources/commands/pong.cpp \
sources/commands/privmsg.cpp \ sources/commands/privmsg.cpp \
sources/commands/userCmd.cpp \ sources/commands/userCmd.cpp \
sources/commands/whois.cpp \
sources/commands/whowas.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 \

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */ /* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */
/* Updated: 2025/06/08 22:57:21 by rparodi ### ########.fr */ /* Updated: 2025/06/17 17:36:38 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -63,6 +63,8 @@ namespace cmd
class Topic; class Topic;
class Unknown; class Unknown;
class userCmd; class userCmd;
class Whois;
class Whowas;
}; };
#include "./commands/commands.tpp" #include "./commands/commands.tpp"

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/08 22:08:44 by sben-tay #+# #+# */ /* Created: 2025/06/08 22:08:44 by sben-tay #+# #+# */
/* Updated: 2025/06/08 22:42:04 by rparodi ### ########.fr */ /* Updated: 2025/06/17 17:35:53 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* commands.tpp :+: :+: :+: */ /* commands.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:34:30 by rparodi #+# #+# */ /* Created: 2025/05/24 17:34:30 by rparodi #+# #+# */
/* Updated: 2025/05/27 14:34:56 by rparodi ### ########.fr */ /* Updated: 2025/06/17 17:36:11 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* whois.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/17 14:02:02 by sben-tay #+# #+# */
/* Updated: 2025/06/17 14:03:15 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "commands.hpp"
class cmd::Whois : public ACommand {
public:
Whois(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* whowas.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/17 17:01:16 by sben-tay #+# #+# */
/* Updated: 2025/06/17 17:01:27 by sben-tay ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "commands.hpp"
class cmd::Whowas : public ACommand {
public:
Whowas(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
virtual void execute(void);
virtual e_code checkArgs();
};

View file

@ -6,13 +6,14 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */ /* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */
/* Updated: 2025/06/16 18:01:10 by sben-tay ### ########.fr */ /* Updated: 2025/06/17 17:36:46 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include <list> #include <list>
#include <map>
#include "core.hpp" #include "core.hpp"
#include "channel.hpp" #include "channel.hpp"
#include "user.hpp" #include "user.hpp"

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */ /* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */
/* Updated: 2025/06/16 18:36:01 by sben-tay ### ########.fr */ /* Updated: 2025/06/17 15:59:19 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,8 @@ class User
std::string _read_buffer; std::string _read_buffer;
std::string _write_buffer; std::string _write_buffer;
std::string _username; std::string _username;
std::string _realname;
std::string _ipAdress;
bool _hasNick; bool _hasNick;
bool _hasUser; bool _hasUser;
bool _hasPass; bool _hasPass;
@ -40,11 +42,15 @@ class User
void setRegistered(); void setRegistered();
void appendToReadBuffer(const std::string &data); void appendToReadBuffer(const std::string &data);
void appendToWriteBuffer(const std::string &data); void appendToWriteBuffer(const std::string &data);
void checkRegistration();
void resolveHostInfo();
// setters and getters
void setNickname(const std::string &nickname); void setNickname(const std::string &nickname);
void setUsername(const std::string &username); void setUsername(const std::string &username);
void checkRegistration(); void setRealname(const std::string &realname);
std::string getUsername(void) const;
// setters and getters std::string getRealname(void) const;
void setHasNick(bool value); void setHasNick(bool value);
void setHasUser(bool value); void setHasUser(bool value);
void setHasPass(bool value); void setHasPass(bool value);
@ -52,6 +58,10 @@ class User
void setPassIsValid(bool value); void setPassIsValid(bool value);
bool getHasPass() const; bool getHasPass() const;
std::string getNickname() const; std::string getNickname() const;
const std::string& getHostname() const;
const std::string& getIpAddress() const;
void setHostname(const std::string &hostname);
void setIpAddress(const std::string &ip);
bool hasDataToSend() const; bool hasDataToSend() const;
std::string getWriteBuffer() const; std::string getWriteBuffer() const;

View file

@ -25,6 +25,8 @@
#include "userCmd.hpp" #include "userCmd.hpp"
#include "pass.hpp" #include "pass.hpp"
#include "part.hpp" #include "part.hpp"
#include "whois.hpp"
#include "whowas.hpp"
#include <iterator> #include <iterator>
/** /**
@ -40,7 +42,15 @@ std::vector<std::string> cmd::split(std::string &line) {
size_t start = 0; size_t start = 0;
size_t end; 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); std::string arg = line.substr(start, end - start);
if (!arg.empty()) { if (!arg.empty()) {
for (size_t i = 0; i < arg.length(); ++i) 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; 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; return args;
} }
@ -135,6 +139,13 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &
userCmd(user, channel, server, line).execute(); userCmd(user, channel, server, line).execute();
} }
break; 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: default:
WARNING_MSG("Unknown command: " << command_name); WARNING_MSG("Unknown command: " << command_name);
} }

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* 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; return ERR_NONICKNAMEGIVEN;
} }
User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin
if (existing != NULL) { if (existing != NULL) {
_sender->appendToWriteBuffer(":" + _sender->getPrefix() + " 433 * " + _args[1] + " :Nickname is already in use\r\n"); std::string msg433 = ":" + _sender->getPrefix() + " 433 * " + _args[1] + " :Nickname is already in use\r\n";
_sender->appendToWriteBuffer(msg433);
return ERR_NICKNAMEINUSE; return ERR_NICKNAMEINUSE;
} }
return _PARSING_OK; return _PARSING_OK;

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* 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)"); ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
return; return;
} }
_sender->appendToWriteBuffer("PONG " + _args[1] + "\r\n"); std::string msgpong = "PONG " + _args[1] + "\r\n";
_sender->appendToWriteBuffer(msgpong);
DEBUG_MSG(_sender->getWriteBuffer()); DEBUG_MSG(_sender->getWriteBuffer());
} }

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/08 19:16:10 by sben-tay #+# #+# */ /* 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]); DEBUG_MSG("Setting username to " << _args[1]);
_sender->setUsername(_args[1]); _sender->setUsername(_args[1]);
_sender->setHasUser(true); _sender->setHasUser(true);
_sender->setRealname(_args[4]);
_sender->checkRegistration(); _sender->checkRegistration();
} }

View 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);
}

View 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);
}

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
/* Updated: 2025/06/16 18:35:26 by sben-tay ### ########.fr */ /* Updated: 2025/06/17 17:38:39 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -165,7 +165,15 @@ void Server::printUsers() const
for (std::map<int, User *>::const_iterator it = _users.begin(); it != _users.end(); ++it) for (std::map<int, User *>::const_iterator it = _users.begin(); it != _users.end(); ++it)
{ {
std::cout << "User fd: " << it->first << std::endl; std::cout << "User fd: " << it->first << std::endl;
std::cout << "Nickname: " << it->second->getName() << std::endl; std::cout << "Username: " << it->second->getUsername() << std::endl;
std::cout << "Nickname: " << it->second->getNickname() << std::endl;
std::cout << "Realname: " << it->second->getRealname() << std::endl;
std::cout << "hostname: " << it->second->getHostname() << std::endl;
std::cout << "IP Address: " << it->second->getIpAddress() << std::endl;
std::cout << "Registered: " << (it->second->isRegistered() ? "Yes" : "No") << std::endl;
std::cout << "Prefix: " << it->second->getPrefix() << std::endl;
std::cout << "----------------------------------------" << std::endl;
} }
} }

View file

@ -6,12 +6,16 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */ /* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
/* Updated: 2025/06/16 18:36:36 by sben-tay ### ########.fr */ /* Updated: 2025/06/17 17:42:40 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "core.hpp" #include "core.hpp"
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
// Constructor // Constructor
User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \ User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \
@ -87,19 +91,20 @@ void User::checkRegistration()
if (_hasNick && _hasUser && _passReceived && _passIsValid) if (_hasNick && _hasUser && _passReceived && _passIsValid)
{ {
_registered = true; _registered = true;
std::string msg = ":localhost 001 " + _nickname + resolveHostInfo();
std::string msg001 = ":localhost 001 " + _nickname +
" :Welcome to the IRC server " + getPrefix() + "\r\n"; " :Welcome to the IRC server " + getPrefix() + "\r\n";
appendToWriteBuffer(msg); appendToWriteBuffer(msg001);
} }
else if (_hasNick && _hasUser && _passReceived && !_passIsValid) else if (_hasNick && _hasUser && _passReceived && !_passIsValid)
{ {
std::string msg = ":localhost 464 * :Password incorrect\r\n"; std::string msg464 = ":localhost 464 * :Password incorrect\r\n";
appendToWriteBuffer(msg); appendToWriteBuffer(msg464);
} }
else if (_hasNick && _hasUser && !_passReceived) else if (_hasNick && _hasUser && !_passReceived)
{ {
std::string msg = ":localhost 451 * :You must send PASS to register\r\n"; std::string msg451 = ":localhost 451 * :You must send PASS to register\r\n";
appendToWriteBuffer(msg); appendToWriteBuffer(msg451);
} }
} }
@ -146,6 +151,38 @@ std::string User::getWriteBuffer() const { return _write_buffer; }
void User::clearWriteBuffer() { _write_buffer.clear(); } void User::clearWriteBuffer() { _write_buffer.clear(); }
std::string User::getPrefix() const { std::string User::getPrefix() const { return _nickname + "!" + _username + "@" + _hostname; }
return _nickname + "!" + _username + "@localhost";
std::string User::getUsername() const { return _username; }
const std::string& User::getHostname() const { return _hostname; }
const std::string& User::getIpAddress() const { return _ipAdress; }
void User::setHostname(const std::string &hostname) { _hostname = hostname; }
void User::setIpAddress(const std::string &ip) { _ipAdress = ip; }
void User::resolveHostInfo()
{
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
if (getpeername(_fd, (struct sockaddr*)&addr, &len) == -1)
{
std::cerr << "getpeername failed for fd " << _fd << std::endl;
return;
}
_ipAdress = std::string(inet_ntoa(addr.sin_addr));
struct hostent* host = gethostbyaddr((const void*)&addr.sin_addr, sizeof(addr.sin_addr), AF_INET);
if (host)
_hostname = std::string(host->h_name);
else
_hostname = _ipAdress;
} }
void User::setRealname(const std::string &realname) { _realname = realname; }
std::string User::getRealname(void) const { return _realname; }