diff --git a/Makefile b/Makefile index 1094634..9831d4c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: sben-tay +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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/privmsg.cpp \ sources/commands/userCmd.cpp \ + sources/commands/whois.cpp \ + sources/commands/whowas.cpp \ sources/core/PollManager.cpp \ sources/core/Server.cpp \ sources/core/check.cpp \ diff --git a/include/commands.hpp b/include/commands.hpp index 7029c34..9107ab5 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: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 Unknown; class userCmd; + class Whois; + class Whowas; }; #include "./commands/commands.tpp" diff --git a/include/commands/cap.hpp b/include/commands/cap.hpp index 57c0814..7ea5799 100644 --- a/include/commands/cap.hpp +++ b/include/commands/cap.hpp @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/include/commands/commands.tpp b/include/commands/commands.tpp index 7683e95..adead31 100644 --- a/include/commands/commands.tpp +++ b/include/commands/commands.tpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* commands.tpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ +/* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/include/commands/whois.hpp b/include/commands/whois.hpp new file mode 100644 index 0000000..51bcba8 --- /dev/null +++ b/include/commands/whois.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* whois.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sben-tay +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(); +}; diff --git a/include/commands/whowas.hpp b/include/commands/whowas.hpp new file mode 100644 index 0000000..a98c2f2 --- /dev/null +++ b/include/commands/whowas.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* whowas.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sben-tay +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(); +}; diff --git a/include/server.hpp b/include/server.hpp index 9d0a01a..87626a9 100644 --- a/include/server.hpp +++ b/include/server.hpp @@ -6,13 +6,14 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include +#include #include "core.hpp" #include "channel.hpp" #include "user.hpp" diff --git a/include/user.hpp b/include/user.hpp index ff77dc9..fcae8b3 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 _write_buffer; std::string _username; + std::string _realname; + std::string _ipAdress; bool _hasNick; bool _hasUser; bool _hasPass; @@ -40,11 +42,15 @@ class User void setRegistered(); void appendToReadBuffer(const std::string &data); void appendToWriteBuffer(const std::string &data); + void checkRegistration(); + void resolveHostInfo(); + + // setters and getters void setNickname(const std::string &nickname); void setUsername(const std::string &username); - void checkRegistration(); - - // setters and getters + void setRealname(const std::string &realname); + std::string getUsername(void) const; + std::string getRealname(void) const; void setHasNick(bool value); void setHasUser(bool value); void setHasPass(bool value); @@ -52,6 +58,10 @@ class User void setPassIsValid(bool value); bool getHasPass() 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; std::string getWriteBuffer() const; diff --git a/sources/commands/commands.cpp b/sources/commands/commands.cpp index d0358cd..a26543a 100644 --- a/sources/commands/commands.cpp +++ b/sources/commands/commands.cpp @@ -25,6 +25,8 @@ #include "userCmd.hpp" #include "pass.hpp" #include "part.hpp" +#include "whois.hpp" +#include "whowas.hpp" #include /** @@ -40,7 +42,15 @@ std::vector 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 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); } diff --git a/sources/commands/nick.cpp b/sources/commands/nick.cpp index 6182a58..348ea8e 100644 --- a/sources/commands/nick.cpp +++ b/sources/commands/nick.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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(_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; diff --git a/sources/commands/ping.cpp b/sources/commands/ping.cpp index c6c442e..5bc63c2 100644 --- a/sources/commands/ping.cpp +++ b/sources/commands/ping.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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()); } diff --git a/sources/commands/userCmd.cpp b/sources/commands/userCmd.cpp index ac20202..eb587b7 100644 --- a/sources/commands/userCmd.cpp +++ b/sources/commands/userCmd.cpp @@ -6,7 +6,7 @@ /* 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]); _sender->setUsername(_args[1]); _sender->setHasUser(true); + _sender->setRealname(_args[4]); _sender->checkRegistration(); } + diff --git a/sources/commands/whois.cpp b/sources/commands/whois.cpp new file mode 100644 index 0000000..e5bf36c --- /dev/null +++ b/sources/commands/whois.cpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* whois.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sben-tay +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(_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); +} diff --git a/sources/commands/whowas.cpp b/sources/commands/whowas.cpp new file mode 100644 index 0000000..63920a1 --- /dev/null +++ b/sources/commands/whowas.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* whowas.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sben-tay +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} \ No newline at end of file diff --git a/sources/core/Server.cpp b/sources/core/Server.cpp index 3f5426e..0995527 100644 --- a/sources/core/Server.cpp +++ b/sources/core/Server.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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::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; + } } diff --git a/sources/user/user.cpp b/sources/user/user.cpp index 3256bb7..6279b62 100644 --- a/sources/user/user.cpp +++ b/sources/user/user.cpp @@ -6,12 +6,16 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include +#include +#include +#include // 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; }