sefault in part.cpp patched, parsing modified, problem in management POLOUT fixed, privmsg update, Server update, User update

This commit is contained in:
Samy BEN TAYEB 2025-06-19 02:30:03 +02:00
parent 8096edb63d
commit 647241313f
8 changed files with 74 additions and 32 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/20 22:18:17 by rparodi #+# #+# */ /* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */
/* Updated: 2025/06/18 12:24:15 by rparodi ### ########.fr */ /* Updated: 2025/06/19 01:15:59 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,7 +47,7 @@ class Channel {
void removeOperator(User *user); void removeOperator(User *user);
// utility functions // utility functions
void sendAllClientInAChannel(const std::string toSend, User *sender); void sendAllClientInAChannel(const std::string &toSend, User *exclude = NULL);
private: private:
std::string _name; std::string _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/20 21:57:49 by rparodi #+# #+# */ /* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */
/* Updated: 2025/06/18 01:19:32 by sben-tay ### ########.fr */ /* Updated: 2025/06/19 02:22:55 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -70,6 +70,7 @@ class User
bool hasDataToSend() const; bool hasDataToSend() const;
std::string getWriteBuffer() const; std::string getWriteBuffer() const;
void clearWriteBuffer(); void clearWriteBuffer();
void consumeWriteBuffer(size_t len);
std::string getPrefix() const; std::string getPrefix() const;
}; };

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 22:43:24 by rparodi #+# #+# */ /* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */
/* Updated: 2025/06/18 12:20:01 by rparodi ### ########.fr */ /* Updated: 2025/06/19 01:16:03 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -190,10 +190,17 @@ void Channel::addOperator(User *user) {
* @param user to add in the channel * @param user to add in the channel
*/ */
void Channel::addUser(User *user) { void Channel::addUser(User *user) {
if (!user) {
std::cerr << "Error: Attempt to add a null user to the channel " << this->_name << std::endl;
DEBUG_MSG("Attempt to add a null user to the channel " << this->_name);
return;
}
if (this->isUserInChannel(user) == false) { if (this->isUserInChannel(user) == false) {
DEBUG_MSG("Adding user " << user->getName() << " to channel " << this->_name);
this->_users.push_back(user); this->_users.push_back(user);
} }
else { else {
DEBUG_MSG("User " << user->getName() << " is already in channel " << this->_name);
std::cerr << user->getName() << " is already in the channel " << this->_name << std::endl; std::cerr << user->getName() << " is already in the channel " << this->_name << std::endl;
} }
} }
@ -226,11 +233,9 @@ void Channel::removeOperator(User *user) {
} }
} }
void Channel::sendAllClientInAChannel(const std::string toSend, User *sender) { void Channel::sendAllClientInAChannel(const std::string &toSend, User *exclude) {
for(std::list<User *>::iterator it = this->_users.begin(); it != this->_users.end(); ++it) { for (std::list<User *>::iterator it = _users.begin(); it != _users.end(); ++it) {
if (*it == sender) { if (*it != exclude)
continue;
}
(*it)->appendToWriteBuffer(toSend); (*it)->appendToWriteBuffer(toSend);
} }
} }

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/18 12:51:54 by rparodi ### ########.fr */ /* Updated: 2025/06/19 02:27:50 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -89,5 +89,5 @@ void Join::execute() {
for (std::list<User *>::iterator it = _cTarget->getUsers().begin(); it != _cTarget->getUsers().end(); ++it) { for (std::list<User *>::iterator it = _cTarget->getUsers().begin(); it != _cTarget->getUsers().end(); ++it) {
msg353 += (*it)->getNickname() + " "; msg353 += (*it)->getNickname() + " ";
} }
_cTarget->sendAllClientInAChannel(msgJoin + msg332 + msg353 + "\r\n", _sender); _cTarget->sendAllClientInAChannel(msgJoin + msg332 + msg353 + "\r\n");
} }

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/18 12:55:38 by rparodi ### ########.fr */ /* Updated: 2025/06/19 01:13:28 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,24 +19,29 @@ using namespace cmd;
e_code Part::checkArgs() { e_code Part::checkArgs() {
if (_args.size() < 2) { if (_args.size() < 2) {
DEBUG_MSG("Not enough arguments for Part command");
std::string msg461 = ":localhost 461 " + this->_sender->getNickname() + " " + this->_command + " :Not enough parameters\r\n"; std::string msg461 = ":localhost 461 " + this->_sender->getNickname() + " " + this->_command + " :Not enough parameters\r\n";
this->_sender->appendToWriteBuffer(msg461); this->_sender->appendToWriteBuffer(msg461);
return ERR_NEEDMOREPARAMS; return ERR_NEEDMOREPARAMS;
} }
DEBUG_MSG("Arguments size is valid for Part command");
if (_args.at(1).at(0) != '#') { if (_args.at(1).at(0) != '#') {
DEBUG_MSG("Invalid channel name for Part command: " << _args.at(1));
WARNING_MSG("Invalid channel name for PART command"); WARNING_MSG("Invalid channel name for PART command");
INFO_MSG("Channel names must start with a '#' character"); INFO_MSG("Channel names must start with a '#' character");
return ERR_NOSUCHCHANNEL; return ERR_NOSUCHCHANNEL;
} else } else {
_args.at(1).erase(0, 1); _args.at(1).erase(0, 1);
_cTarget = searchList(_channels, _args.at(1)); }
_cTarget = searchList(_server->getChannelsList(), _args.at(1));
if (_cTarget == NULL) { if (_cTarget == NULL) {
DEBUG_MSG("Channel not found for Part command: " << _args.at(1));
WARNING_MSG("Channel not found for PART command"); WARNING_MSG("Channel not found for PART command");
INFO_MSG("You can only Part users to channels you are in"); INFO_MSG("You can only Part users to channels you are in");
return ERR_NOSUCHCHANNEL; return ERR_NOSUCHCHANNEL;
} else }
_args.at(1).erase(0, 1); if (_cTarget->isUserInChannel(_sender) == false) {
if (searchList(this->_cTarget->getUsers(), this->_uTarget->getName())) { DEBUG_MSG("sender is : " << _sender->getName());
WARNING_MSG("User is not in the channel he wants to leave"); WARNING_MSG("User is not in the channel he wants to leave");
INFO_MSG("You cannot leave a channel where ur not a member"); INFO_MSG("You cannot leave a channel where ur not a member");
return ERR_USERNOTINCHANNEL; return ERR_USERNOTINCHANNEL;
@ -48,10 +53,28 @@ e_code Part::checkArgs() {
* @brief Execute the Part command * @brief Execute the Part command
* @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;
} }
// check how the com DEBUG_MSG("EXECUTING PART COMMAND");
std::string msgPart = ":" + _sender->getPrefix() + " PART #" + _cTarget->getName();
if (_args.size() > 2)
msgPart += " :" + _args.at(2);
msgPart += "\r\n";
_cTarget->sendAllClientInAChannel(msgPart);
_cTarget->removeUser(_sender);
_cTarget->removeOperator(_sender);
// remove invited list
if (_cTarget->getUsers().empty()) {
std::list<Channel*>& allChannels = _server->getChannelsList();
allChannels.remove(_cTarget);
delete _cTarget;
DEBUG_MSG("Channel supprimé car vide.");
}
} }

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/18 12:51:56 by rparodi ### ########.fr */ /* Updated: 2025/06/19 02:28:16 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -63,7 +63,7 @@ void PrivMsg::execute() {
// Envoi vers un channel // Envoi vers un channel
if (target[0] == '#') { if (target[0] == '#') {
target.erase(0, 1); // Enlève le # target.erase(0, 1);
if (_cTarget) if (_cTarget)
_cTarget->sendAllClientInAChannel(msg, _sender); // Optionnel: évite d'envoyer au sender _cTarget->sendAllClientInAChannel(msg, _sender); // Optionnel: évite d'envoyer au sender
} }

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/18 01:34:56 by sben-tay ### ########.fr */ /* Updated: 2025/06/19 02:24:05 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -126,7 +126,8 @@ void Server::start()
std::cerr << "Erreur send sur fd " << fd << std::endl; std::cerr << "Erreur send sur fd " << fd << std::endl;
disconnectClient(fd); disconnectClient(fd);
} else { } else {
_users[fd]->clearWriteBuffer(); _users[fd]->consumeWriteBuffer(bytesSent);
if (_users[fd]->getWriteBuffer().empty())
_pollManager.setWritable(fd, false); _pollManager.setWritable(fd, false);
} }
} }

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/18 01:19:52 by sben-tay ### ########.fr */ /* Updated: 2025/06/19 02:23:06 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -117,21 +117,26 @@ bool User::isReadyToSend() const
// Extract full command from read buffer // Extract full command from read buffer
std::string User::extractFullCommand() { std::string User::extractFullCommand() {
size_t pos = _read_buffer.find("\r\n"); size_t pos = _read_buffer.find("\r\n");
if (pos == std::string::npos)
pos = _read_buffer.find("\n"); // fallback
if (pos != std::string::npos) { if (pos != std::string::npos) {
std::string command = _read_buffer.substr(0, pos); std::string command = _read_buffer.substr(0, pos);
if (_read_buffer.substr(pos, 2) == "\r\n")
_read_buffer.erase(0, pos + 2); _read_buffer.erase(0, pos + 2);
else return command;
}
pos = _read_buffer.find('\n');
if (pos != std::string::npos) {
std::string command = _read_buffer.substr(0, pos);
_read_buffer.erase(0, pos + 1); _read_buffer.erase(0, pos + 1);
return command; return command;
} }
return ""; return "";
} }
void User::setHasNick(bool value) { _hasNick = value; } void User::setHasNick(bool value) { _hasNick = value; }
void User::setHasUser(bool value) { _hasUser = value; } void User::setHasUser(bool value) { _hasUser = value; }
@ -187,3 +192,10 @@ void User::resolveHostInfo()
void User::setRealname(const std::string &realname) { _realname = realname; } void User::setRealname(const std::string &realname) { _realname = realname; }
std::string User::getRealname(void) const { return _realname; } std::string User::getRealname(void) const { return _realname; }
void User::consumeWriteBuffer(size_t len) {
if (len >= _write_buffer.size())
_write_buffer.clear();
else
_write_buffer.erase(0, len);
}