Merge pull request #20 from EniumRaphael/samy
Patch v1.2.1 – WHOIS, WHOWAS, Connexion distante & Refactors
This commit is contained in:
commit
b188655ea8
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);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,12 +6,16 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
// Constructor
|
||||
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)
|
||||
{
|
||||
_registered = true;
|
||||
std::string msg = ":localhost 001 " + _nickname +
|
||||
resolveHostInfo();
|
||||
std::string msg001 = ":localhost 001 " + _nickname +
|
||||
" :Welcome to the IRC server " + getPrefix() + "\r\n";
|
||||
appendToWriteBuffer(msg);
|
||||
appendToWriteBuffer(msg001);
|
||||
}
|
||||
else if (_hasNick && _hasUser && _passReceived && !_passIsValid)
|
||||
{
|
||||
std::string msg = ":localhost 464 * :Password incorrect\r\n";
|
||||
appendToWriteBuffer(msg);
|
||||
std::string msg464 = ":localhost 464 * :Password incorrect\r\n";
|
||||
appendToWriteBuffer(msg464);
|
||||
}
|
||||
else if (_hasNick && _hasUser && !_passReceived)
|
||||
{
|
||||
std::string msg = ":localhost 451 * :You must send PASS to register\r\n";
|
||||
appendToWriteBuffer(msg);
|
||||
std::string msg451 = ":localhost 451 * :You must send PASS to register\r\n";
|
||||
appendToWriteBuffer(msg451);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -146,6 +151,38 @@ std::string User::getWriteBuffer() const { return _write_buffer; }
|
|||
|
||||
void User::clearWriteBuffer() { _write_buffer.clear(); }
|
||||
|
||||
std::string User::getPrefix() const {
|
||||
return _nickname + "!" + _username + "@localhost";
|
||||
std::string User::getPrefix() const { return _nickname + "!" + _username + "@" + _hostname; }
|
||||
|
||||
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; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue