diff --git a/inc/Client.hpp b/inc/Client.hpp index a8bb0ec..95b09b9 100644 --- a/inc/Client.hpp +++ b/inc/Client.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -7,6 +8,8 @@ #include #include "CommandDispatcher.hpp" #include "User.hpp" +#include "RecvParser.hpp" + class User; class CommandDispatcher; /* @@ -28,7 +31,8 @@ class Client { bool _authenticated = false; bool _registered = false; CommandDispatcher* _test; - + std::queue> _msg_queue; + RecvParser _parser; public: explicit Client(int fd); ~Client(); @@ -41,6 +45,7 @@ class Client { std::function& getHandler(uint32_t eventType); User* getUser(void); CommandDispatcher* getDispatch(void); + RecvParser& getParser(void); void authenticate(void); bool isAuthenticated(void) const; bool& accessRegistered(void); diff --git a/inc/RecvParser.hpp b/inc/RecvParser.hpp index 2731dcf..c19f8e1 100644 --- a/inc/RecvParser.hpp +++ b/inc/RecvParser.hpp @@ -7,7 +7,7 @@ #include #include "Message.hpp" -/** +/** * @class RecvParser * @brief A class for parsing data read by recv() into a message queue */ @@ -16,6 +16,7 @@ class RecvParser public: RecvParser(std::queue> &msg_queue); + std::queue> &getQueue(void); void feed(const char *read_buf, size_t len); private: diff --git a/src/Client.cpp b/src/Client.cpp index 9566863..eb19606 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -1,7 +1,7 @@ #include "Client.hpp" -Client::Client(int fd) : _self(new User()), _test(new CommandDispatcher()), _fd(fd) {} +Client::Client(int fd) : _self(new User()), _test(new CommandDispatcher()), _parser(RecvParser(_msg_queue)), _fd(fd) {} Client::Client(const Client& other) : _IN(other._IN), @@ -9,6 +9,7 @@ _RDHUP(other._RDHUP), _HUP(other._HUP), _self(new User(*other._self)), _test(other._test), +_parser(other._parser), _fd(other._fd), _initialized(other._initialized){} @@ -33,6 +34,10 @@ CommandDispatcher* Client::getDispatch(void) { return _test; } +RecvParser& Client::getParser(void) { + return _parser; +} + void Client::setHandler(uint32_t eventType, std::function handler) { switch (eventType) { case EPOLLIN: diff --git a/src/CommandDispatcher.cpp b/src/CommandDispatcher.cpp index 8bd7a31..3ccbf9b 100644 --- a/src/CommandDispatcher.cpp +++ b/src/CommandDispatcher.cpp @@ -34,8 +34,8 @@ bool CommandDispatcher::dispatch(const std::unique_ptr &msg, int fd) { if (auto cmd = _handlers.find(msg->command); cmd != _handlers.end()) { - if (!irc->checkPassword() && - !irc->getClient(fd).isAuthenticated() && + if (not irc->checkPassword() && + not irc->getClient(fd).isAuthenticated() && msg->command != "PASS" && msg->command != "CAP") { @@ -46,6 +46,8 @@ bool CommandDispatcher::dispatch(const std::unique_ptr &msg, int fd) irc->removeClient(fd); return false; } + if (msg->command == "QUIT") + return cmd->second->execute(*msg, fd), false; cmd->second->execute(*msg, fd); if (msg->command != "QUIT" && not irc->getClient(fd).getUser()->getNick().empty() && diff --git a/src/Handler.cpp b/src/Handler.cpp index f44ff0d..7db46a9 100644 --- a/src/Handler.cpp +++ b/src/Handler.cpp @@ -5,8 +5,9 @@ using namespace std; void Handler::clientWrite(int fd) { ssize_t messageLen = 0; vector buf(BUFSIZ); - queue> msg_queue; - RecvParser parser(msg_queue); + Client& client = irc->getClient(fd); + RecvParser& parser = client.getParser(); + std::queue> &msg_queue = parser.getQueue(); messageLen = recv(fd, &buf[0], buf.size(), 0); if (messageLen == -1) diff --git a/src/RecvParser.cpp b/src/RecvParser.cpp index db68470..7ae2add 100644 --- a/src/RecvParser.cpp +++ b/src/RecvParser.cpp @@ -3,6 +3,11 @@ RecvParser::RecvParser(std::queue> &msg_queue) : _output(msg_queue){} +std::queue> &RecvParser::getQueue(void) +{ + return _output; +} + /** * Feed the recv() buffer into std::string buffer * @param read_buf The buffer to read