correctifs

This commit is contained in:
2024-05-19 19:30:11 +02:00
parent 3660d41dea
commit f2725946e0
26 changed files with 629 additions and 234 deletions

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/16 15:23:58 by fgras-ca #+# #+# */
/* Updated: 2024/05/16 16:03:22 by fgras-ca ### ########.fr */
/* Updated: 2024/05/19 19:18:11 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,16 +17,21 @@
#include "Server.hpp"
#include "Channel.hpp"
#include "Utils.hpp"
#include "RPL.hpp"
#include <sstream>
#include <string>
#include <fstream>
// Forward declarations
class Server;
class Client;
class Channel;
void sendWelcomeMessages(Client *client, Server *server);
void sendMotd(Client *client, Server *server);
void broadcastChannelList(Client *client, Server *server);
void handlePartCommand(Server *server, Client *client, const std::string &command);
void handleNickCommand(Server *server, Client *client, const std::string &command);
void handlePrivmsgCommand(Server *server, Client *client, const std::string &command);
#endif
#endif // ADDITIONALCOMMANDS_HPP

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:41:35 by fgras-ca #+# #+# */
/* Updated: 2024/05/17 20:08:13 by fgras-ca ### ########.fr */
/* Updated: 2024/05/19 15:14:50 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,6 +17,7 @@
#include <vector>
#include <algorithm>
#include <set>
#include "RPL.hpp"
class Client;

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:15:42 by fgras-ca #+# #+# */
/* Updated: 2024/05/17 17:21:39 by fgras-ca ### ########.fr */
/* Updated: 2024/05/19 15:38:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,13 +18,15 @@
class Client
{
public:
Client(int fd, const std::string &nickname, const std::string &user);
Client(int fd, const std::string &nickname, const std::string &user, const std::string &host, const std::string &password, const std::string &realname);
int getFd() const;
const std::string &getNickname() const;
void setNickname(const std::string &nickname);
const std::string &getUser() const;
void setUser(const std::string &user);
const std::string &getHost() const;
void setHost(const std::string &host);
const std::string &getPassword() const;
void setPassword(const std::string &password);
const std::string &getRealName() const;
@@ -38,10 +40,11 @@ private:
int _fd;
std::string _nickname;
std::string _user;
std::string _host;
std::string _password;
std::string _realname;
bool _authenticated;
bool _operator;
};
#endif
#endif // CLIENT_HPP

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:30:07 by fgras-ca #+# #+# */
/* Updated: 2024/05/17 18:51:44 by fgras-ca ### ########.fr */
/* Updated: 2024/05/19 18:56:44 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,24 +18,31 @@
#include "Client.hpp"
#include "Channel.hpp"
#include "Server.hpp"
#include <poll.h>
#include "CommandHandler.hpp"
#include "AdditionalCommands.hpp"
#include "RPL.hpp"
#include "Utils.hpp"
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <fcntl.h>
#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>
#include <fstream>
class Server;
class Client;
class ClientManager
{
public:
ClientManager(Server *server);
void acceptClient();
void broadcastChannelList(Client *client);
void handleClient(int client_fd);
void removeClient(int client_fd);

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:14:12 by fgras-ca #+# #+# */
/* Updated: 2024/05/17 17:10:38 by fgras-ca ### ########.fr */
/* Updated: 2024/05/19 19:13:53 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,6 +20,7 @@
#include "AdditionalCommands.hpp"
#include "ModeWhoHandler.hpp"
#include "CommandHandler.hpp"
#include "RPL.hpp"
#include <string>
#include <sstream>
@@ -27,6 +28,7 @@
class Server;
class ModeWhoHandler;
class AdditinalCommands;
class CommandHandler
{
@@ -35,7 +37,16 @@ private:
public:
CommandHandler(Server *server);
bool isValidNickname(const std::string& nickname);
bool isNicknameInUse(const std::string& nickname);
void handleNick(Client* client, const std::vector<std::string>& tokens);
void handleUser(Client* client, const std::vector<std::string>& tokens);
void handlePingCommand(Client* client, const std::vector<std::string>& tokens);
void handleCommand(Client *client, const std::string &command);
void handleCapCommand(Client* client, const std::vector<std::string>& tokens);
void handlePassCommand(Client* client, const std::vector<std::string>& tokens);
void processCommand(Client *client, const std::string &command);
void handleJoinCommand(Client *client, const std::string &channelName);
std::string getUsersList(Channel *channel);

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/17 16:08:48 by fgras-ca #+# #+# */
/* Updated: 2024/05/17 18:08:06 by fgras-ca ### ########.fr */
/* Updated: 2024/05/19 15:15:39 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,6 +17,10 @@
#include "Client.hpp"
#include "Channel.hpp"
#include "Utils.hpp"
#include "RPL.hpp"
#include <sstream>
#include <iostream>
class Server;

221
ft_irc3/includes/RPL.hpp Normal file
View File

@@ -0,0 +1,221 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RPL.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:47 by fgras-ca #+# #+# */
/* Updated: 2024/05/19 19:07:25 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RPL_HPP
#define RPL_HPP
#include <string>
#include <sstream>
#include "Client.hpp"
#define SERVER_NAME "IRC_Server"
#define SERVER_VERSION "1.0"
#define USER_MODES "iw"
#define CHANNEL_MODES "nt"
#define CHANNEL_MODES_WITH_PARAMS "kl"
// Macros pour accéder aux champs du client
#define CLIENT_FD(client) (client->getFd())
#define CLIENT_NICK(client) ((client)->getNickname())
#define CLIENT_USER(client) ((client)->getUser())
#define CLIENT_HOST(client) ((client)->getHost())
// Fonctions pour générer les réponses RPL
inline std::string RPL_WELCOME(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 001 " << CLIENT_FD(client)
<< " :Welcome to the Internet Relay Network " << CLIENT_NICK(client)
<< "!" << CLIENT_USER(client) << "@" << CLIENT_HOST(client) << "\r\n";
return oss.str();
}
inline std::string RPL_YOURHOST(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 002 " << CLIENT_FD(client)
<< " :Your host is " << SERVER_NAME << ", running version " << SERVER_VERSION << "\r\n";
return oss.str();
}
inline std::string RPL_CREATED(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 003 " << CLIENT_FD(client)
<< " :This server was created " << __DATE__ << "\r\n";
return oss.str();
}
inline std::string RPL_MYINFO(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 004 " << CLIENT_FD(client) << " "
<< SERVER_NAME << " " << SERVER_VERSION << " "
<< USER_MODES << " " << CHANNEL_MODES << " "
<< CHANNEL_MODES_WITH_PARAMS << "\r\n";
return oss.str();
}
inline std::string RPL_ISUPPORT(Client* client, const std::string& tokens) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 005 " << CLIENT_FD(client)
<< " " << tokens << " :are supported by this server\r\n";
return oss.str();
}
inline std::string RPL_MOTDSTART(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 375 " << CLIENT_FD(client)
<< " :- " << SERVER_NAME << " Message of the day - \r\n";
return oss.str();
}
inline std::string RPL_MOTD(Client* client, const std::string& line) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 372 " << CLIENT_FD(client)
<< " :- " << line << "\r\n";
return oss.str();
}
inline std::string RPL_ENDOFMOTD(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 376 " << CLIENT_FD(client)
<< " :End of /MOTD command.\r\n";
return oss.str();
}
inline std::string ERR_NOMOTD(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 422 " << CLIENT_FD(client)
<< " :MOTD File is missing\r\n";
return oss.str();
}
// RPL Channel List Messages
inline std::string RPL_LIST(Client* client, const std::string& channel, int numVisible, const std::string& topic) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 322 " << CLIENT_FD(client) << " " << channel
<< " " << numVisible << " :" << topic << "\r\n";
return oss.str();
}
inline std::string RPL_LISTEND(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 323 " << CLIENT_FD(client)
<< " :End of /LIST\r\n";
return oss.str();
}
//RPL Password
// RPL Error Messages
inline std::string ERR_NEEDMOREPARAMS(Client* client, const std::string& command)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 461 " << CLIENT_FD(client) << " " << command << " :Not enough parameters\r\n";
return oss.str();
}
inline std::string ERR_ALREADYREGISTERED(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 462 " << CLIENT_FD(client) << " :You may not reregister\r\n";
return oss.str();
}
inline std::string ERR_PASSWDMISMATCH(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 464 " << CLIENT_FD(client) << " :Password incorrect\r\n";
return oss.str();
}
inline std::string ERR_NOTREGISTERED(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 451 " << CLIENT_FD(client) << " :You have not registered\r\n";
return oss.str();
}
inline std::string ERR_NONICKNAMEGIVEN(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 431 " << CLIENT_FD(client) << " :No nickname given\r\n";
return oss.str();
}
inline std::string ERR_ERRONEUSNICKNAME(Client* client, const std::string& nickname)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 432 " << CLIENT_FD(client) << " " << nickname << " :Erroneous nickname\r\n";
return oss.str();
}
inline std::string ERR_NICKNAMEINUSE(Client* client, const std::string& nickname)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 433 " << CLIENT_FD(client) << " " << nickname << " :Nickname is already in use\r\n";
return oss.str();
}
// RPL Error Messages
inline std::string ERR_NOORIGIN(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 409 " << CLIENT_FD(client) << " :No origin specified\r\n";
return oss.str();
}
// PONG Reply
inline std::string RPL_PONG(const std::string& token)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " PONG " << SERVER_NAME << " " << token << "\r\n";
return oss.str();
}
// ERR_UNKNOWNCOMMAND (421)
inline std::string ERR_UNKNOWNCOMMAND(Client* client, const std::string& command)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 421 " << CLIENT_FD(client) << " " << command << " :Unknown command\r\n";
return oss.str();
}
// RPL Channel List Messages
inline std::string RPL_LIST(int clientFd, const std::string& channel, int numVisible, const std::string& topic)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 322 " << clientFd << " " << channel << " " << numVisible << " :" << topic << "\r\n";
return oss.str();
}
inline std::string RPL_LISTEND(int clientFd)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 323 " << clientFd << " :End of /LIST\r\n";
return oss.str();
}
inline std::string ERR_NEEDMOREPARAMS(int clientFd, const std::string& command)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 461 " << clientFd << " " << command << " :Not enough parameters\r\n";
return oss.str();
}
inline std::string ERR_UNKNOWNCOMMAND(int clientFd, const std::string& command)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 421 " << clientFd << " " << command << " :Unknown command\r\n";
return oss.str();
}
#endif // RPL_HPP

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:15:13 by fgras-ca #+# #+# */
/* Updated: 2024/05/17 20:03:06 by fgras-ca ### ########.fr */
/* Updated: 2024/05/19 16:30:56 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,6 +19,7 @@
#include "ClientManager.hpp"
#include "CommandHandler.hpp"
#include "AdditionalCommands.hpp"
#include "RPL.hpp"
#include <sys/socket.h>
#include <netinet/in.h>