first commit

This commit is contained in:
lfirmin
2026-05-25 10:24:09 +02:00
commit 332cb7501a
12 changed files with 763 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
#ifndef COMMANDVALIDATOR_HPP
# define COMMANDVALIDATOR_HPP
# include <string>
# include "IrcMessage.hpp"
class CommandValidator
{
public:
CommandValidator(void);
CommandValidator(const CommandValidator &other);
CommandValidator &operator=(const CommandValidator &other);
~CommandValidator(void);
static bool hasEnoughParams(const IrcMessage &msg);
static bool needsRegistration(const std::string &command);
static bool isKnownCommand(const std::string &command);
};
#endif
+43
View File
@@ -0,0 +1,43 @@
#ifndef IRCMESSAGE_HPP
# define IRCMESSAGE_HPP
# include <string>
# include <vector>
class IrcMessage
{
private:
std::string _prefix;
std::string _command;
std::vector<std::string> _params;
std::string _raw;
bool _valid;
std::string _error;
public:
IrcMessage(void);
IrcMessage(const IrcMessage &other);
IrcMessage &operator=(const IrcMessage &other);
~IrcMessage(void);
void setPrefix(const std::string &prefix);
void setCommand(const std::string &command);
void addParam(const std::string &param);
void setRaw(const std::string &raw);
void setValid(bool valid);
void setError(const std::string &error);
const std::string &getPrefix(void) const;
const std::string &getCommand(void) const;
const std::vector<std::string> &getParams(void) const;
const std::string &getRaw(void) const;
bool isValid(void) const;
const std::string &getError(void) const;
bool hasPrefix(void) const;
bool hasParam(size_t index) const;
const std::string &param(size_t index) const;
size_t paramCount(void) const;
};
#endif
+28
View File
@@ -0,0 +1,28 @@
#ifndef IRCPARSER_HPP
# define IRCPARSER_HPP
# include <string>
# include <vector>
# include "IrcMessage.hpp"
class IrcParser
{
private:
static std::string trimLeft(const std::string &s);
static std::string trimRight(const std::string &s);
static std::string toUpper(const std::string &s);
static bool isSpace(char c);
static bool isCommandChar(char c);
static bool isValidCommand(const std::string &command);
static void parseParams(const std::string &s, size_t pos, IrcMessage &msg);
public:
IrcParser(void);
IrcParser(const IrcParser &other);
IrcParser &operator=(const IrcParser &other);
~IrcParser(void);
static IrcMessage parseLine(const std::string &line);
};
#endif
+31
View File
@@ -0,0 +1,31 @@
#ifndef PARSEBUFFER_HPP
# define PARSEBUFFER_HPP
# include <string>
# include <vector>
# include "IrcMessage.hpp"
class ParseBuffer
{
private:
std::string _buffer;
size_t _maxLineSize;
bool extractOneLine(std::string &line);
public:
ParseBuffer(void);
explicit ParseBuffer(size_t maxLineSize);
ParseBuffer(const ParseBuffer &other);
ParseBuffer &operator=(const ParseBuffer &other);
~ParseBuffer(void);
void append(const char *data, size_t len);
std::vector<IrcMessage> extractMessages(void);
void clear(void);
const std::string &raw(void) const;
size_t size(void) const;
};
#endif
+58
View File
@@ -0,0 +1,58 @@
#ifndef SERVER_HPP
# define SERVER_HPP
# include <string>
# include <vector>
# include <map>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <poll.h>
# include <fcntl.h>
# include <unistd.h>
# include "ParseBuffer.hpp"
# include <stdexcept>
# include <cstring>
# include <cerrno>
struct ConnectedUser
{
int fd;
std::string nick;
std::string username;
std::string realname;
std::string hostname;
bool passOk;
bool nickOk;
bool userOk;
ParseBuffer parseBuffer;
explicit ConnectedUser(int fd);
bool isRegistered(void) const;
};
class Server
{
private:
int _serverFd;
int _port;
std::string _password;
std::vector<struct pollfd> _fds;
std::map<int, ConnectedUser> _users;
void acceptNew(void);
void handleRecv(int fd);
void removeUser(int fd);
public:
Server(int port, const std::string &password);
Server(const Server &other);
Server &operator=(const Server &other);
~Server(void);
void start(void);
void run(void);
void sendReply(int fd, const std::string &msg);
};
#endif