Merge pull request #17 from EniumRaphael/samy

🛠️ Patch Notes - IRC Server Update v1.2.3-beta
This commit is contained in:
Raphaël 2025-06-14 23:47:19 +02:00 committed by GitHub
commit 8295ebe33e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 108 additions and 72 deletions

View file

@ -1,14 +1,14 @@
/******************************************************************************/ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* server.hpp :+: :+: :+: */ /* server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: omoudni <omoudni@student.42paris.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/05/29 12:17:55 by rparodi ### ########.fr */ /* Updated: 2025/06/14 23:08:11 by sben-tay ### ########.fr */
/* */ /* */
/******************************************************************************/ /* ************************************************************************** */
#pragma once #pragma once
@ -21,6 +21,7 @@
class User; class User;
class Channel; class Channel;
class Server class Server
{ {
private: private:
int _port; int _port;

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:10:24 by sben-tay #+# #+# */ /* Created: 2025/06/08 22:10:24 by sben-tay #+# #+# */
/* Updated: 2025/06/09 14:05:55 by rparodi ### ########.fr */ /* Updated: 2025/06/14 22:26:07 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,13 +16,19 @@
using namespace cmd; using namespace cmd;
e_code Cap::checkArgs() { e_code Cap::checkArgs() {
return _PARSING_OK; if (_args.size() < 2){
return ERR_NEEDMOREPARAMS;
_sender->appendToReadBuffer(_command);
}
return (_PARSING_OK);
} }
void cmd::Cap::execute() { void cmd::Cap::execute() {
if (this->checkArgs() != _PARSING_OK) if (this->checkArgs() != _PARSING_OK)
return; return;
if (_args.size() >= 2 && _args[1] == "LS") { if (_args.size() >= 2 && _args[1] == "ls") {
std::string reply = "CAP * LS :\r\n"; std::string reply = "CAP * LS :\r\n";
_sender->appendToWriteBuffer(reply); _sender->appendToWriteBuffer(reply);
DEBUG_MSG("Replied to CAP LS"); DEBUG_MSG("Replied to CAP LS");

View file

@ -77,7 +77,7 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &
WARNING_MSG("No command found in line: " << line); WARNING_MSG("No command found in line: " << line);
return; return;
} }
std::cout << command_name << std::endl; DEBUG_MSG("Command Name = [" << command_name << "]");
switch (command_name[0]) { switch (command_name[0]) {
case 'c': case 'c':
if (command_name == "cap") { if (command_name == "cap") {
@ -110,10 +110,10 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &
} }
break; break;
case 'n': case 'n':
if (command_name == "NICK") { if (command_name == "nick") {
Nick(user, channel, server, line).execute(); Nick(user, channel, server, line).execute();
} else if (command_name == "notice") { } else if (command_name == "notice") {
Notice(user, channel, server, line).execute(); Notice(user, channel, server, line).execute();
} }
break; break;
case 'p': case 'p':

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* invite.cpp :+: :+: :+: */ /* invite.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/05 11:10:36 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:24:05 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,7 +61,7 @@ e_code Invite::checkArgs() {
* @note To invite a peapol to join a channel (from an operator) * @note To invite a peapol to join a channel (from an operator)
*/ */
void Invite::execute() { void Invite::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for INVITE command (see warning message)"); ERROR_MSG("Invalid arguments for INVITE command (see warning message)");
return; return;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* join.cpp :+: :+: :+: */ /* join.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/05 22:34:07 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:23:58 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,7 +49,7 @@ e_code Join::checkArgs() {
* @note To join a new channel * @note To join a new channel
*/ */
void Join::execute() { void Join::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for Join command (see warning message)"); ERROR_MSG("Invalid arguments for Join command (see warning message)");
return; return;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* kick.cpp :+: :+: :+: */ /* kick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/09 14:37:11 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:24:16 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,7 +61,7 @@ e_code Kick::checkArgs() {
* @note To kick a user from a channel * @note To kick a user from a channel
*/ */
void Kick::execute() { void Kick::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for INVITE command (see warning message)"); ERROR_MSG("Invalid arguments for INVITE command (see warning message)");
return; return;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* list.cpp :+: :+: :+: */ /* list.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/09 15:33:52 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:24:21 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,7 +47,7 @@ e_code List::checkArgs() {
* @note To list the channel * @note To list the channel
*/ */
void List::execute() { void List::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for LIST command (see warning message)"); ERROR_MSG("Invalid arguments for LIST command (see warning message)");
return; return;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* modes.cpp :+: :+: :+: */ /* modes.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/10 16:29:52 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:24:27 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -112,7 +112,7 @@ e_code Mode::checkArgs() {
* @note To invite a peapol to join a channel (from an operator) * @note To invite a peapol to join a channel (from an operator)
*/ */
void Mode::execute() { void Mode::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for INVITE command (see warning message)"); ERROR_MSG("Invalid arguments for INVITE command (see warning message)");
return; return;
} }

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/08 22:18:49 by sben-tay ### ########.fr */ /* Updated: 2025/06/12 17:52:12 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,12 +20,9 @@ e_code cmd::Nick::checkArgs() {
WARNING_MSG("Nick: Not enough arguments"); WARNING_MSG("Nick: Not enough arguments");
return ERR_NONICKNAMEGIVEN; return ERR_NONICKNAMEGIVEN;
} }
if (_sender->isRegistered()) {
WARNING_MSG(_sender->getName() << " is already registered");
return ERR_ALREADYREGISTERED;
}
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) {
WARNING_MSG("Nick already in use: " << _args[1]); WARNING_MSG("Nick already in use: " << _args[1]);
return ERR_NICKNAMEINUSE; return ERR_NICKNAMEINUSE;
} }
@ -37,7 +34,7 @@ e_code cmd::Nick::checkArgs() {
* @note To change the nickname of the user * @note To change the nickname of the user
*/ */
void cmd::Nick::execute() { void cmd::Nick::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for Nick command (see warning message)"); ERROR_MSG("Invalid arguments for Nick command (see warning message)");
return; return;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* notice.cpp :+: :+: :+: */ /* notice.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/05 22:49:09 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:24:35 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,7 +49,7 @@ e_code Notice::checkArgs() {
* @note To send a private message to a user / a channel (like privmsg but without error) * @note To send a private message to a user / a channel (like privmsg but without error)
*/ */
void Notice::execute() { void Notice::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for NOTICE command (see warning message)"); ERROR_MSG("Invalid arguments for NOTICE command (see warning message)");
return; return;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* part.cpp :+: :+: :+: */ /* part.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/05 22:50:21 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:24:39 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,7 +48,7 @@ e_code Part::checkArgs() {
* @note To leave a channel givent in parameter * @note To leave a channel givent in parameter
*/ */
void Part::execute() { void Part::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for Part command (see warning message)"); ERROR_MSG("Invalid arguments for Part command (see warning message)");
return; return;
} }

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/10 16:27:03 by rparodi ### ########.fr */ /* Updated: 2025/06/12 17:52:35 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,7 @@ e_code Pass::checkArgs() {
*/ */
void Pass::execute() { void Pass::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for Pass command (see warning message)"); ERROR_MSG("Invalid arguments for Pass command (see warning message)");
DEBUG_MSG("skill issues"); DEBUG_MSG("skill issues");
return; return;
@ -41,4 +41,5 @@ void Pass::execute() {
return; return;
} }
_sender->setHasPass(true); _sender->setHasPass(true);
_sender->checkRegistration();
} }

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/08 20:13:52 by sben-tay ### ########.fr */ /* Updated: 2025/06/14 23:25:45 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@
using namespace cmd; using namespace cmd;
e_code Ping::checkArgs() { e_code Ping::checkArgs() {
if (_args.size() < 3) { if (_args.size() < 2) {
WARNING_MSG("Not enough arguments for PING command"); WARNING_MSG("Not enough arguments for PING command");
return ERR_NEEDMOREPARAMS; return ERR_NEEDMOREPARAMS;
} }
@ -32,11 +32,10 @@ e_code Ping::checkArgs() {
*/ */
void Ping::execute() { void Ping::execute() {
clock_t start = clock() / CLOCKS_PER_SEC; if (checkArgs() != _PARSING_OK) {
if (checkArgs() == _PARSING_OK) {
ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)"); ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
return; return;
} }
clock_t diff = Pong().answer(start); _sender->appendToWriteBuffer("PONG " + _args[1] + "\r\n");
INFO_MSG(diff); DEBUG_MSG(_sender->getWriteBuffer());
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* privmsg.cpp :+: :+: :+: */ /* privmsg.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@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/05 22:46:54 by rparodi ### ########.fr */ /* Updated: 2025/06/12 13:25:02 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,7 +49,7 @@ e_code PrivMsg::checkArgs() {
* @note To send a private message to a user / a channel * @note To send a private message to a user / a channel
*/ */
void PrivMsg::execute() { void PrivMsg::execute() {
if (checkArgs() == _PARSING_OK) { if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)"); ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
return; return;
} }

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/08 22:19:02 by sben-tay ### ########.fr */ /* Updated: 2025/06/12 17:55:06 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,10 +20,6 @@ e_code cmd::userCmd::checkArgs() {
WARNING_MSG("USER: Not enough parameters"); WARNING_MSG("USER: Not enough parameters");
return ERR_NEEDMOREPARAMS; return ERR_NEEDMOREPARAMS;
} }
if (_sender->isRegistered()) {
WARNING_MSG(_sender->getName() << " is already registered");
return ERR_ALREADYREGISTERED;
}
return _PARSING_OK; return _PARSING_OK;
} }

View file

@ -1,14 +1,14 @@
/******************************************************************************/ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* Server.cpp :+: :+: :+: */ /* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: omoudni <omoudni@student.42paris.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/03 14:56:06 by rparodi ### ########.fr */ /* Updated: 2025/06/14 23:16:54 by sben-tay ### ########.fr */
/* */ /* */
/******************************************************************************/ /* ************************************************************************** */
#include "color.hpp" #include "color.hpp"
#include "server.hpp" #include "server.hpp"
@ -22,6 +22,7 @@
#include <unistd.h> #include <unistd.h>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <sstream>
/** /**
* @brief The constructor of the Server class. * @brief The constructor of the Server class.
@ -48,6 +49,8 @@ Server::~Server()
} }
} }
std::vector<std::string> splitLines(const std::string& input);
void Server::start() void Server::start()
{ {
_serverFd = socket(AF_INET, SOCK_STREAM, 0); _serverFd = socket(AF_INET, SOCK_STREAM, 0);
@ -84,7 +87,6 @@ void Server::start()
readyClients.clear(); readyClients.clear();
_pollManager.pollLoop(_serverFd, newClients, disconnected, _pollManager.pollLoop(_serverFd, newClients, disconnected,
readyClients); readyClients);
std::cout << "Poll loop finished" << std::endl;
std::cout << "New clients: " << newClients.size() << std::endl; std::cout << "New clients: " << newClients.size() << std::endl;
for (size_t i = 0; i < newClients.size(); ++i) for (size_t i = 0; i < newClients.size(); ++i)
{ {
@ -103,17 +105,36 @@ void Server::start()
if (_users.count(fd)) if (_users.count(fd))
{ {
_users[fd]->appendToReadBuffer(data); _users[fd]->appendToReadBuffer(data);
std::string cmd; std::string rawCmd;
while (!(cmd = _users[fd]->extractFullCommand()).empty()) while (!(rawCmd = _users[fd]->extractFullCommand()).empty())
{ {
std::vector<std::string> lines = splitLines(rawCmd);
for (size_t i = 0; i < lines.size(); ++i) {
std::cout << "Client " << fd << " says: " << lines[i] << std::endl;
cmd::dispatch(_users[fd], NULL, this, lines[i]);
}
// This prints every command/message received from any client // This prints every command/message received from any client
std::cout << "Client " << fd << " says: " << cmd << std::endl;
cmd::dispatch(_users[fd], NULL, this, cmd);
} }
} }
} }
for (std::vector<std::pair<int, std::string > > ::iterator it = readyClients.begin(); it != readyClients.end(); ++it)
// Optionally: handle server shutdown, signals, etc. {
int fd = it->first;
if (_users.count(fd) && _users[fd]->isReadyToSend())
{
std::string writeBuffer = _users[fd]->getWriteBuffer();
ssize_t bytesSent = send(fd, writeBuffer.c_str(), writeBuffer.size(), 0);
if (bytesSent < 0)
{
std::cerr << "Erreur send" << std::endl;
}
else {
_users[fd]->clearWriteBuffer();
}
}
}
std::cout << "Poll loop finished" << std::endl;
} }
close(_serverFd); close(_serverFd);
} }
@ -170,3 +191,16 @@ std::list<User *> Server::getUsersList() const {
std::list<Channel *> Server::getChannelsList() const { std::list<Channel *> Server::getChannelsList() const {
return this->_channels; return this->_channels;
} }
std::vector<std::string> splitLines(const std::string& input) {
std::vector<std::string> lines;
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line)) {
if (!line.empty() && line[line.length() - 1] == '\r')
line.erase(line.length() - 1); // retirer le \r final
lines.push_back(line);
}
return lines;
}

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/21 20:37:12 by omoudni #+# #+# */ /* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
/* Updated: 2025/06/08 21:41:26 by sben-tay ### ########.fr */ /* Updated: 2025/06/14 23:27:43 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -85,6 +85,7 @@ void User::checkRegistration()
{ {
if (!_registered && _hasNick && _hasUser && _hasPass) if (!_registered && _hasNick && _hasUser && _hasPass)
{ {
std::cout << "JE SUIS ENREGISTRE" << std::endl;
_registered = true; _registered = true;
std::string welcome = ":localhost 001 " + _nickname + std::string welcome = ":localhost 001 " + _nickname +
" :Welcome to the IRC server " + getPrefix() + "\r\n"; " :Welcome to the IRC server " + getPrefix() + "\r\n";
@ -101,18 +102,19 @@ bool User::isReadyToSend() const
// Extract full command from read buffer // Extract full command from read buffer
std::string User::extractFullCommand() { std::string User::extractFullCommand() {
std::string command;
size_t pos = _read_buffer.find("\r\n"); size_t pos = _read_buffer.find("\r\n");
if (pos == std::string::npos) if (pos == std::string::npos)
pos = _read_buffer.find("\n"); // fallback pos = _read_buffer.find("\n"); // fallback
if (pos != std::string::npos) { if (pos != std::string::npos) {
command = _read_buffer.substr(0, pos); std::string command = _read_buffer.substr(0, pos);
_read_buffer.erase(0, pos + 1); if (_read_buffer.substr(pos, 2) == "\r\n")
if (_read_buffer[pos] == '\r') // clean up stray \r _read_buffer.erase(0, pos + 2);
_read_buffer.erase(0, 1); else
_read_buffer.erase(0, pos + 1);
return command;
} }
return command; return "";
} }
void User::setHasNick(bool value) { _hasNick = value; } void User::setHasNick(bool value) { _hasNick = value; }