This commit is contained in:
2024-05-16 23:31:03 +02:00
parent 58606f36a6
commit aaa1457b61
29 changed files with 1375 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AdditionalCommands.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#ifndef ADDITIONALCOMMANDS_HPP
#define ADDITIONALCOMMANDS_HPP
#include "Client.hpp"
#include "Server.hpp"
#include "Channel.hpp"
#include "Utils.hpp"
#include <sstream>
#include <string>
class Server;
class Client;
class Channel;
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

View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Channel.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:41:35 by fgras-ca #+# #+# */
/* Updated: 2024/05/15 12:48:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHANNEL_HPP
#define CHANNEL_HPP
#include <string>
#include <vector>
#include <algorithm>
class Client;
class Channel {
public:
Channel(const std::string &name);
~Channel();
const std::string &getName() const;
void addClient(Client *client);
void removeClient(Client *client);
bool isEmpty() const;
const std::vector<Client *> &getClients() const;
private:
std::string _name;
std::vector<Client *> _clients;
};
#endif

View File

@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:15:42 by fgras-ca #+# #+# */
/* Updated: 2024/05/16 18:18:02 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CLIENT_HPP
#define CLIENT_HPP
#include <string>
class Client
{
public:
Client(int fd);
void setPassword(const std::string &password);
const std::string &getPassword() const;
void setNickname(const std::string &nickname);
const std::string &getNickname() const;
void setUser(const std::string &user);
const std::string &getUser() const;
void setRealName(const std::string &realname);
const std::string &getRealName() const;
bool isAuthenticated() const;
void authenticate();
int getFd() const;
private:
int _fd;
std::string _password;
std::string _nickname;
std::string _user;
std::string _realname;
bool _authenticated;
};
#endif

View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClientManager.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:30:07 by fgras-ca #+# #+# */
/* Updated: 2024/05/16 16:26:03 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CLIENTMANAGER_HPP
#define CLIENTMANAGER_HPP
#include <map>
#include <vector>
#include "Client.hpp"
#include "Channel.hpp"
#include "Server.hpp"
#include <poll.h>
#include "CommandHandler.hpp"
#include "Utils.hpp"
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <fcntl.h>
class Server;
class ClientManager
{
public:
ClientManager(Server *server);
void acceptClient();
void handleClient(int client_fd);
void removeClient(int client_fd);
private:
Server *_server;
};
#endif

View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CommandHandler.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:14:12 by fgras-ca #+# #+# */
/* Updated: 2024/05/16 17:35:46 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef COMMANDHANDLER_HPP
#define COMMANDHANDLER_HPP
#include "Utils.hpp"
#include "Server.hpp"
#include "Client.hpp"
#include "Channel.hpp"
#include "AdditionalCommands.hpp"
#include <string>
#include <sstream>
#include <algorithm>
class Server;
class CommandHandler
{
private:
Server *_server;
public:
CommandHandler(Server *server);
void handleCommand(Client *client, const std::string &command);
void processCommand(Client *client, const std::string &command);
void handleJoinCommand(Client *client, const std::string &channelName);
std::string getUsersList(Channel *channel);
};
#endif

View File

@@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:15:13 by fgras-ca #+# #+# */
/* Updated: 2024/05/16 17:40:34 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SERVER_HPP
#define SERVER_HPP
#include "Utils.hpp"
#include "Client.hpp"
#include "Channel.hpp"
#include "ClientManager.hpp"
#include "CommandHandler.hpp"
#include "AdditionalCommands.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <unistd.h>
#include <string>
#include <vector>
#include <map>
#include <poll.h>
class Client;
class Channel;
class ClientManager;
class CommandHandler;
class Server
{
public:
Server(int port, const std::string &password);
~Server();
void run();
void log(const std::string &message, const std::string &color = "\033[0m");
void sendToClient(int client_fd, const std::string &message);
// Méthodes d'accès pour les canaux et les clients
std::map<std::string, Channel *> &getChannels();
std::map<int, Client *> &getClients();
const std::string &getPassword() const;
protected:
int _server_fd;
int _port;
std::string _password;
std::map<int, Client *> _clients;
std::map<std::string, Channel *> _channels;
std::vector<struct pollfd> _poll_fds;
ClientManager *_clientManager;
CommandHandler *_commandHandler;
friend class ClientManager;
friend class CommandHandler;
private:
void initServer();
void handleServerCommands();
void acceptClient();
void removeClient(int client_fd);
};
#endif

View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Utils.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:16:02 by fgras-ca #+# #+# */
/* Updated: 2024/05/16 18:14:58 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef UTILS_HPP
#define UTILS_HPP
#include <string>
#include <iostream>
#include <vector>
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
std::vector<std::string> split(const std::string &input, const std::string &delimiters);
#endif