mirror of
https://github.com/Ladebeze66/ft_irc.git
synced 2026-11-09 13:17:13 +01:00
correctifs
This commit is contained in:
@@ -6,12 +6,47 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/16 15:27:29 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/05/17 20:15:14 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/05/19 19:22:28 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "AdditionalCommands.hpp"
|
||||
|
||||
void sendWelcomeMessages(Client *client, Server *server) {
|
||||
server->sendToClient(client->getFd(), RPL_WELCOME(client));
|
||||
server->sendToClient(client->getFd(), RPL_YOURHOST(client));
|
||||
server->sendToClient(client->getFd(), RPL_CREATED(client));
|
||||
server->sendToClient(client->getFd(), RPL_MYINFO(client));
|
||||
server->sendToClient(client->getFd(), RPL_ISUPPORT(client, "MODES=EXAMPLE"));
|
||||
|
||||
sendMotd(client, server);
|
||||
}
|
||||
|
||||
void sendMotd(Client *client, Server *server) {
|
||||
std::ifstream motdFile("motd.txt");
|
||||
if (motdFile.is_open()) {
|
||||
std::string line;
|
||||
server->sendToClient(client->getFd(), RPL_MOTDSTART(client));
|
||||
while (std::getline(motdFile, line)) {
|
||||
server->sendToClient(client->getFd(), RPL_MOTD(client, line));
|
||||
}
|
||||
server->sendToClient(client->getFd(), RPL_ENDOFMOTD(client));
|
||||
motdFile.close();
|
||||
} else {
|
||||
server->sendToClient(client->getFd(), ERR_NOMOTD(client));
|
||||
}
|
||||
}
|
||||
|
||||
void broadcastChannelList(Client *client, Server *server)
|
||||
{
|
||||
std::map<std::string, Channel *> &channels = server->getChannels();
|
||||
for (std::map<std::string, Channel *>::iterator it = channels.begin(); it != channels.end(); ++it)
|
||||
{
|
||||
server->sendToClient(client->getFd(), RPL_LIST(client->getFd(), it->first, it->second->getClients().size(), "Existing channel"));
|
||||
}
|
||||
server->sendToClient(client->getFd(), RPL_LISTEND(client->getFd()));
|
||||
}
|
||||
|
||||
// Fonction pour gérer la commande PART
|
||||
void handlePartCommand(Server *server, Client *client, const std::string &command)
|
||||
{
|
||||
|
||||
@@ -6,17 +6,16 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/15 12:17:42 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/05/17 17:20:02 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/05/19 15:38:46 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Client.hpp"
|
||||
|
||||
Client::Client(int fd, const std::string &nickname, const std::string &user)
|
||||
: _fd(fd), _nickname(nickname), _user(user), _authenticated(false), _operator(false)
|
||||
Client::Client(int fd, const std::string &nickname, const std::string &user, const std::string &host, const std::string &password, const std::string &realname)
|
||||
: _fd(fd), _nickname(nickname), _user(user), _host(host), _password(password), _realname(realname), _authenticated(false), _operator(false)
|
||||
{
|
||||
}
|
||||
|
||||
int Client::getFd() const
|
||||
{
|
||||
return _fd;
|
||||
@@ -42,6 +41,16 @@ void Client::setUser(const std::string &user)
|
||||
_user = user;
|
||||
}
|
||||
|
||||
const std::string &Client::getHost() const
|
||||
{
|
||||
return _host;
|
||||
}
|
||||
|
||||
void Client::setHost(const std::string &host)
|
||||
{
|
||||
_host = host;
|
||||
}
|
||||
|
||||
const std::string &Client::getPassword() const
|
||||
{
|
||||
return _password;
|
||||
@@ -86,6 +95,8 @@ void Client::setOperator(bool isOperator)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*Client::Client(int fd)
|
||||
|
||||
Description: Constructeur de la classe Client. Initialise le client avec le descripteur de fichier fourni.
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/15 18:32:23 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/05/17 18:53:09 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/05/19 18:55:55 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -26,7 +26,8 @@ void ClientManager::acceptClient()
|
||||
return;
|
||||
}
|
||||
|
||||
_server->_clients[client_fd] = new Client(client_fd, "", ""); // Initialize with default values
|
||||
Client *newClient = new Client(client_fd, "", "", "", "", ""); // Fournir six arguments
|
||||
_server->_clients[client_fd] = newClient;
|
||||
struct pollfd client_pollfd;
|
||||
client_pollfd.fd = client_fd;
|
||||
client_pollfd.events = POLLIN;
|
||||
@@ -35,16 +36,8 @@ void ClientManager::acceptClient()
|
||||
std::stringstream ss;
|
||||
ss << "Client connected: " << client_fd;
|
||||
_server->log(ss.str(), GREEN);
|
||||
}
|
||||
|
||||
void ClientManager::broadcastChannelList(Client *client) {
|
||||
std::map<std::string, Channel *> &channels = _server->getChannels();
|
||||
for (std::map<std::string, Channel *>::iterator it = channels.begin(); it != channels.end(); ++it) {
|
||||
std::stringstream channelMsg;
|
||||
channelMsg << ":server 322 " << client->getNickname() << " " << it->first << " :Existing channel\r\n";
|
||||
_server->sendToClient(client->getFd(), channelMsg.str());
|
||||
}
|
||||
_server->sendToClient(client->getFd(), ":server 323 " + client->getNickname() + " :End of /LIST\r\n");
|
||||
sendWelcomeMessages(newClient, _server);
|
||||
}
|
||||
|
||||
void ClientManager::handleClient(int client_fd)
|
||||
@@ -69,12 +62,10 @@ void ClientManager::handleClient(int client_fd)
|
||||
|
||||
while (std::getline(message_stream, line))
|
||||
{
|
||||
// Suppression des caractères de fin de ligne
|
||||
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
|
||||
line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
|
||||
|
||||
_server->_commandHandler->handleCommand(client, line);
|
||||
broadcastChannelList(_server->_clients[client_fd]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +74,6 @@ void ClientManager::removeClient(int client_fd)
|
||||
Client *client = _server->_clients[client_fd];
|
||||
if (client)
|
||||
{
|
||||
// Retirer le client de tous les canaux auxquels il appartient
|
||||
std::map<std::string, Channel *>::iterator it = _server->_channels.begin();
|
||||
while (it != _server->_channels.end())
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/15 18:26:34 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/05/17 20:05:16 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/05/19 19:13:14 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -17,77 +17,191 @@ CommandHandler::CommandHandler(Server *server)
|
||||
{
|
||||
}
|
||||
|
||||
void CommandHandler::handleCommand(Client *client, const std::string &command)
|
||||
void CommandHandler::handleCommand(Client* client, const std::string& command)
|
||||
{
|
||||
std::vector<std::string> tokens = split(command, " \n\r\t");
|
||||
|
||||
if (tokens.empty())
|
||||
{
|
||||
if (tokens.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string commandType = tokens[0];
|
||||
if (commandType == "CAP")
|
||||
{}
|
||||
else if (commandType == "PASS")
|
||||
{
|
||||
if (tokens.size() > 1 && tokens[1] == _server->_password)
|
||||
{
|
||||
client->setPassword(tokens[1]);
|
||||
_server->sendToClient(client->getFd(), ":server 001 " + client->getNickname() + " :Password accepted!\r\n");
|
||||
_server->log("Client " + client->getNickname() + " provided correct password.", GREEN);
|
||||
_server->sendChannelListToClient(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ":server 464 " + client->getNickname() + " :Invalid password\r\n");
|
||||
_server->log("Client " + client->getNickname() + " failed authentication password.", RED);
|
||||
}
|
||||
}
|
||||
else if (commandType == "NICK")
|
||||
{
|
||||
if (tokens.size() > 1)
|
||||
{
|
||||
client->setNickname(tokens[1]);
|
||||
std::stringstream ss;
|
||||
ss << "Client " << client->getFd() << " Nickname: " << tokens[1];
|
||||
_server->log(ss.str(), GREEN);
|
||||
_server->sendToClient(client->getFd(), ":" + tokens[1] + " NICK " + tokens[1] + "\r\n");
|
||||
}
|
||||
}
|
||||
else if (commandType == "USER")
|
||||
{
|
||||
if (tokens.size() > 4)
|
||||
{
|
||||
client->setUser(tokens[1]);
|
||||
client->setRealName(tokens[4].substr(1)); // remove leading ':'
|
||||
if (client->getPassword() == _server->_password && !client->getNickname().empty())
|
||||
{
|
||||
client->authenticate();
|
||||
std::stringstream welcomeMsg;
|
||||
welcomeMsg << ":server 001 " << client->getNickname() << " :Welcome to the IRC server!\r\n";
|
||||
_server->sendToClient(client->getFd(), welcomeMsg.str());
|
||||
|
||||
_server->log("Client " + client->getNickname() + " authenticated.", GREEN);
|
||||
_server->sendChannelListToClient(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!client->isAuthenticated())
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ":server 451 " + client->getNickname() + " :Please provide the password first\r\n");
|
||||
_server->log("Client " + client->getNickname() + " failed authentication.", RED);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (commandType == "CAP") {
|
||||
handleCapCommand(client, tokens);
|
||||
} else if (commandType == "PASS") {
|
||||
handlePassCommand(client, tokens);
|
||||
} else if (commandType == "NICK") {
|
||||
handleNick(client, tokens);
|
||||
} else if (commandType == "USER") {
|
||||
handleUser(client, tokens);
|
||||
} else {
|
||||
if (!client->isAuthenticated()) {
|
||||
_server->sendToClient(client->getFd(), ERR_NOTREGISTERED(client));
|
||||
_server->log("Client " + client->getNickname() + " attempted to send a command before registering.", RED);
|
||||
} else {
|
||||
processCommand(client, command);
|
||||
}
|
||||
}
|
||||
std::cout << "Client " << client->getFd() << " " << client->getNickname() << " " << client->getUser() << " " << client->getPassword() << " " << client->getRealName() << std::endl;
|
||||
}
|
||||
|
||||
void CommandHandler::handleCapCommand(Client* client, const std::vector<std::string>& tokens)
|
||||
{
|
||||
if (tokens.size() < 2)
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client->getFd(), "CAP"));
|
||||
return;
|
||||
}
|
||||
|
||||
std::string subcommand = tokens[1];
|
||||
|
||||
if (subcommand == "LS")
|
||||
{
|
||||
// Example for CAP LS response
|
||||
std::vector<std::string> capLines;
|
||||
capLines.push_back("multi-prefix extended-join account-notify batch invite-notify tls");
|
||||
capLines.push_back("cap-notify server-time example.org/dummy-cap=dummyvalue example.org/second-dummy-cap");
|
||||
capLines.push_back("userhost-in-names sasl=EXTERNAL,DH-AES,DH-BLOWFISH,ECDSA-NIST256P-CHALLENGE,PLAIN");
|
||||
|
||||
for (size_t i = 0; i < capLines.size(); ++i)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << ":irc.example.com CAP " << client->getNickname() << " LS";
|
||||
if (i != capLines.size() - 1)
|
||||
oss << " * :";
|
||||
else
|
||||
oss << " :";
|
||||
oss << capLines[i] << "\r\n";
|
||||
_server->sendToClient(client->getFd(), oss.str());
|
||||
}
|
||||
}
|
||||
else if (subcommand == "LIST")
|
||||
{
|
||||
// Example for CAP LIST response
|
||||
std::ostringstream oss;
|
||||
oss << ":irc.example.com CAP " << client->getNickname() << " LIST :example.org/example-cap example.org/second-example-cap account-notify invite-notify batch example.org/third-example-cap\r\n";
|
||||
_server->sendToClient(client->getFd(), oss.str());
|
||||
}
|
||||
else if (subcommand == "REQ")
|
||||
{
|
||||
if (tokens.size() < 3)
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client->getFd(), "CAP"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Example for CAP REQ response
|
||||
std::string requestedCaps = tokens[2];
|
||||
std::ostringstream ackResponse;
|
||||
ackResponse << ":irc.example.com CAP " << client->getNickname() << " ACK :" << requestedCaps << "\r\n";
|
||||
_server->sendToClient(client->getFd(), ackResponse.str());
|
||||
}
|
||||
else if (subcommand == "END")
|
||||
{
|
||||
// Example for CAP END response
|
||||
_server->sendToClient(client->getFd(), ":irc.example.com CAP " + client->getNickname() + " END\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unknown CAP subcommand
|
||||
_server->sendToClient(client->getFd(), ERR_UNKNOWNCOMMAND(client->getFd(), "CAP"));
|
||||
}
|
||||
}
|
||||
|
||||
void CommandHandler::handlePassCommand(Client* client, const std::vector<std::string>& tokens)
|
||||
{
|
||||
if (tokens.size() < 2) {
|
||||
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "PASS"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (client->isAuthenticated()) {
|
||||
_server->sendToClient(client->getFd(), ERR_ALREADYREGISTERED(client));
|
||||
return;
|
||||
}
|
||||
|
||||
if (tokens[1] == _server->_password) {
|
||||
client->setPassword(tokens[1]);
|
||||
_server->sendToClient(client->getFd(), ":server NOTICE * :Password accepted\r\n");
|
||||
_server->log("Client " + client->getNickname() + " provided correct password.", GREEN);
|
||||
} else {
|
||||
_server->sendToClient(client->getFd(), ERR_PASSWDMISMATCH(client));
|
||||
_server->log("Client " + client->getNickname() + " failed authentication password.", RED);
|
||||
// Optionally disconnect the client here
|
||||
}
|
||||
}
|
||||
|
||||
bool CommandHandler::isValidNickname(const std::string& nickname)
|
||||
{
|
||||
// Implement nickname validation according to IRC protocol
|
||||
if (nickname.empty() || nickname[0] == '#' || nickname[0] == ':' || nickname.find(' ') != std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CommandHandler::isNicknameInUse(const std::string& nickname)
|
||||
{
|
||||
for (std::map<int, Client*>::iterator it = _server->_clients.begin(); it != _server->_clients.end(); ++it) {
|
||||
if (it->second->getNickname() == nickname) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CommandHandler::handleNick(Client* client, const std::vector<std::string>& tokens)
|
||||
{
|
||||
if (tokens.size() < 2) {
|
||||
_server->sendToClient(client->getFd(), ERR_NONICKNAMEGIVEN(client));
|
||||
return;
|
||||
}
|
||||
|
||||
std::string newNick = tokens[1];
|
||||
|
||||
if (!isValidNickname(newNick)) {
|
||||
_server->sendToClient(client->getFd(), ERR_ERRONEUSNICKNAME(client, newNick));
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNicknameInUse(newNick)) {
|
||||
_server->sendToClient(client->getFd(), ERR_NICKNAMEINUSE(client, newNick));
|
||||
return;
|
||||
}
|
||||
|
||||
std::string oldNick = client->getNickname();
|
||||
client->setNickname(newNick);
|
||||
|
||||
if (!oldNick.empty()) {
|
||||
_server->broadcast(":" + oldNick + " NICK " + newNick + "\r\n");
|
||||
}
|
||||
|
||||
_server->sendToClient(client->getFd(), ":" + newNick + " NICK " + newNick + "\r\n");
|
||||
_server->log("Client " + oldNick + " changed nickname to " + newNick, GREEN);
|
||||
}
|
||||
|
||||
void CommandHandler::handleUser(Client* client, const std::vector<std::string>& tokens)
|
||||
{
|
||||
if (tokens.size() < 5) {
|
||||
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "USER"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (client->isAuthenticated()) {
|
||||
_server->sendToClient(client->getFd(), ERR_ALREADYREGISTERED(client));
|
||||
return;
|
||||
}
|
||||
|
||||
client->setUser(tokens[1]);
|
||||
client->setRealName(tokens[4].substr(1)); // remove leading ':'
|
||||
|
||||
if (client->getPassword() == _server->_password && !client->getNickname().empty()) {
|
||||
client->authenticate();
|
||||
sendWelcomeMessages(client, _server);
|
||||
_server->log("Client " + client->getNickname() + " authenticated.", GREEN);
|
||||
}
|
||||
}
|
||||
|
||||
void CommandHandler::processCommand(Client *client, const std::string &command)
|
||||
{
|
||||
if (command.find("JOIN") == 0)
|
||||
@@ -116,12 +230,19 @@ void CommandHandler::processCommand(Client *client, const std::string &command)
|
||||
{
|
||||
ModeWhoHandler whoHandler(_server);
|
||||
whoHandler.handleWhoCommand(client, command);
|
||||
}
|
||||
else if (command.find("PING") == 0)
|
||||
{
|
||||
std::vector<std::string> tokens = split(command, " ");
|
||||
handlePingCommand(client, tokens);
|
||||
}
|
||||
else if (command.find("LIST") == 0)
|
||||
{
|
||||
broadcastChannelList(client, _server);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << ":server 421 " << client->getNickname() << " " << command << " :Unknown command\r\n";
|
||||
_server->sendToClient(client->getFd(), ss.str());
|
||||
_server->sendToClient(client->getFd(), ERR_UNKNOWNCOMMAND(client, command));
|
||||
_server->log("Message from client " + client->getNickname() + ": " + command, MAGENTA);
|
||||
}
|
||||
}
|
||||
@@ -155,9 +276,6 @@ void CommandHandler::handleJoinCommand(Client *client, const std::string &channe
|
||||
_server->log(ss.str(), MAGENTA);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
std::string CommandHandler::getUsersList(Channel *channel)
|
||||
{
|
||||
std::vector<Client *> clients = channel->getClients();
|
||||
@@ -172,3 +290,14 @@ std::string CommandHandler::getUsersList(Channel *channel)
|
||||
ss << "\r\n";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void CommandHandler::handlePingCommand(Client* client, const std::vector<std::string>& tokens)
|
||||
{
|
||||
if (tokens.size() < 2) {
|
||||
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "PING"));
|
||||
return;
|
||||
}
|
||||
|
||||
std::string token = tokens[1];
|
||||
_server->sendToClient(client->getFd(), RPL_PONG(token));
|
||||
}
|
||||
|
||||
@@ -6,22 +6,12 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/17 16:09:20 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/05/17 18:24:34 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/05/18 16:41:10 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ModeWhoHandler.hpp"
|
||||
#include "Channel.hpp"
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
// Couleurs ANSI pour les logs
|
||||
#define RESET "\033[0m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define RED "\033[31m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
|
||||
ModeWhoHandler::ModeWhoHandler(Server *server)
|
||||
: _server(server)
|
||||
@@ -37,7 +27,7 @@ void ModeWhoHandler::handleModeCommand(Client *client, const std::string &comman
|
||||
std::map<std::string, Channel *> &channels = _server->getChannels();
|
||||
if (channels.find(channelName) == channels.end())
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ":server 403 " + client->getNickname() + " " + channelName + " :No such channel\r\n");
|
||||
_server->sendToClient(client->getFd(), ":" SERVER_NAME " 403 " + client->getNickname() + " " + channelName + " :No such channel\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,12 +45,12 @@ void ModeWhoHandler::handleModeCommand(Client *client, const std::string &comman
|
||||
}
|
||||
else
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ":server 401 " + client->getNickname() + " " + user + " :No such nick/channel\r\n");
|
||||
_server->sendToClient(client->getFd(), ":" SERVER_NAME " 401 " + client->getNickname() + " " + user + " :No such nick/channel\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ":server 482 " + client->getNickname() + " " + channelName + " :You're not channel operator\r\n");
|
||||
_server->sendToClient(client->getFd(), ":" SERVER_NAME " 482 " + client->getNickname() + " " + channelName + " :You're not channel operator\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,7 +64,7 @@ void ModeWhoHandler::handleWhoCommand(Client *client, const std::string &command
|
||||
std::map<std::string, Channel *> &channels = _server->getChannels();
|
||||
if (channels.find(channelName) == channels.end())
|
||||
{
|
||||
_server->sendToClient(client->getFd(), ":server 403 " + client->getNickname() + " " + channelName + " :No such channel\r\n");
|
||||
_server->sendToClient(client->getFd(), ":" SERVER_NAME " 403 " + client->getNickname() + " " + channelName + " :No such channel\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -84,12 +74,12 @@ void ModeWhoHandler::handleWhoCommand(Client *client, const std::string &command
|
||||
for (size_t i = 0; i < channelClients.size(); ++i)
|
||||
{
|
||||
std::stringstream whoMsg;
|
||||
whoMsg << ":server 352 " << client->getNickname() << " " << channelName << " " << channelClients[i]->getNickname() << " "
|
||||
<< channelClients[i]->getUser() << " " << _server->getPassword() << " " << channelClients[i]->getRealName() << "\r\n";
|
||||
whoMsg << ":" SERVER_NAME " 352 " << client->getNickname() << " " << channelName << " " << channelClients[i]->getNickname() << " "
|
||||
<< channelClients[i]->getUser() << " " << channelClients[i]->getHost() << " " << _server->getPassword() << " " << channelClients[i]->getRealName() << "\r\n";
|
||||
_server->sendToClient(client->getFd(), whoMsg.str());
|
||||
}
|
||||
|
||||
std::stringstream endOfWhoMsg;
|
||||
endOfWhoMsg << ":server 315 " << client->getNickname() << " " << channelName << " :End of /WHO list.\r\n";
|
||||
endOfWhoMsg << ":" SERVER_NAME " 315 " << client->getNickname() << " " << channelName << " :End of /WHO list.\r\n";
|
||||
_server->sendToClient(client->getFd(), endOfWhoMsg.str());
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/15 12:17:12 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/05/17 20:03:44 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/05/19 19:21:54 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -72,6 +72,13 @@ void Server::run()
|
||||
server_pollfd.revents = 0;
|
||||
_poll_fds.push_back(server_pollfd);
|
||||
|
||||
// Ajout de l'entrée pour les commandes serveur
|
||||
struct pollfd stdin_pollfd;
|
||||
stdin_pollfd.fd = STDIN_FILENO;
|
||||
stdin_pollfd.events = POLLIN;
|
||||
stdin_pollfd.revents = 0;
|
||||
_poll_fds.push_back(stdin_pollfd);
|
||||
|
||||
while (true)
|
||||
{
|
||||
int poll_count = poll(&_poll_fds[0], _poll_fds.size(), -1);
|
||||
@@ -89,57 +96,59 @@ void Server::run()
|
||||
{
|
||||
_clientManager->acceptClient();
|
||||
}
|
||||
else if (_poll_fds[i].fd == STDIN_FILENO)
|
||||
{
|
||||
handleServerCommands();
|
||||
}
|
||||
else
|
||||
{
|
||||
_clientManager->handleClient(_poll_fds[i].fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleServerCommands();
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, Channel *> &Server::getChannels()
|
||||
{
|
||||
return _channels;
|
||||
}
|
||||
|
||||
const std::string &Server::getPassword() const
|
||||
{
|
||||
return _password;
|
||||
}
|
||||
|
||||
void Server::handleServerCommands()
|
||||
{
|
||||
fd_set readfds;
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(STDIN_FILENO, &readfds);
|
||||
struct timeval tv = {0, 0}; // non-blocking
|
||||
std::string command;
|
||||
std::getline(std::cin, command);
|
||||
|
||||
int activity = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
|
||||
|
||||
if (activity > 0 && FD_ISSET(STDIN_FILENO, &readfds))
|
||||
if (command == "quit")
|
||||
{
|
||||
std::string command;
|
||||
std::getline(std::cin, command);
|
||||
|
||||
if (command == "quit")
|
||||
log("Server shutting down.", YELLOW);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else if (command == "channels")
|
||||
{
|
||||
log("Listing all channels:", BLUE);
|
||||
for (std::map<std::string, Channel*>::iterator it = _channels.begin(); it != _channels.end(); ++it)
|
||||
{
|
||||
log("Server shutting down.", YELLOW);
|
||||
exit(EXIT_SUCCESS);
|
||||
log(it->first, BLUE);
|
||||
}
|
||||
else if (command == "channels")
|
||||
}
|
||||
else if (command == "clients")
|
||||
{
|
||||
log("Listing all clients:", BLUE);
|
||||
for (std::map<int, Client*>::iterator it = _clients.begin(); it != _clients.end(); ++it)
|
||||
{
|
||||
log("Listing all channels:", BLUE);
|
||||
for (std::map<std::string, Channel*>::iterator it = _channels.begin(); it != _channels.end(); ++it)
|
||||
{
|
||||
log(it->first, BLUE);
|
||||
}
|
||||
}
|
||||
else if (command == "clients")
|
||||
{
|
||||
log("Listing all clients:", BLUE);
|
||||
for (std::map<int, Client*>::iterator it = _clients.begin(); it != _clients.end(); ++it)
|
||||
{
|
||||
log(it->second->getNickname(), BLUE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log("Unknown server command.", RED);
|
||||
log(it->second->getNickname(), BLUE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log("Unknown server command.", RED);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::log(const std::string &message, const std::string &color)
|
||||
@@ -147,7 +156,6 @@ void Server::log(const std::string &message, const std::string &color)
|
||||
std::cout << color << message << "\033[0m" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void Server::sendToClient(int client_fd, const std::string &message)
|
||||
{
|
||||
int result = send(client_fd, message.c_str(), message.length(), 0);
|
||||
@@ -165,11 +173,6 @@ void Server::sendToClient(int client_fd, const std::string &message)
|
||||
}
|
||||
}
|
||||
|
||||
const std::string &Server::getPassword() const
|
||||
{
|
||||
return _password;
|
||||
}
|
||||
|
||||
void Server::broadcast(const std::string &message)
|
||||
{
|
||||
for (std::map<int, Client *>::iterator it = _clients.begin(); it != _clients.end(); ++it)
|
||||
@@ -178,16 +181,6 @@ void Server::broadcast(const std::string &message)
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, Channel *> &Server::getChannels()
|
||||
{
|
||||
return _channels;
|
||||
}
|
||||
|
||||
std::map<int, Client *> &Server::getClients()
|
||||
{
|
||||
return _clients;
|
||||
}
|
||||
|
||||
Client* Server::getClientByName(const std::string &name)
|
||||
{
|
||||
for (std::map<int, Client *>::iterator it = _clients.begin(); it != _clients.end(); ++it)
|
||||
@@ -205,17 +198,11 @@ void Server::sendChannelListToClient(Client *client)
|
||||
std::map<std::string, Channel *> &channels = getChannels();
|
||||
for (std::map<std::string, Channel *>::iterator it = channels.begin(); it != channels.end(); ++it)
|
||||
{
|
||||
std::stringstream channelMsg;
|
||||
channelMsg << ":server 322 " << client->getNickname() << " " << it->first << " :" << it->second->getClients().size() << "\r\n";
|
||||
sendToClient(client->getFd(), channelMsg.str());
|
||||
sendToClient(client->getFd(), RPL_LIST(client->getFd(), it->first, it->second->getClients().size(), "Existing channel"));
|
||||
}
|
||||
std::stringstream endOfListMsg;
|
||||
endOfListMsg << ":server 323 " << client->getNickname() << " :End of /LIST\r\n";
|
||||
sendToClient(client->getFd(), endOfListMsg.str());
|
||||
sendToClient(client->getFd(), RPL_LISTEND(client->getFd()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Explications des Fonctions
|
||||
Server::Server(int port, const std::string &password)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user