first commit
This commit is contained in:
@@ -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
|
||||
@@ -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 ¶m);
|
||||
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 ¶m(size_t index) const;
|
||||
size_t paramCount(void) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user