From b24dbc149a752da4d2c1e21c2df32b28eb34cc9d Mon Sep 17 00:00:00 2001 From: Arthur Urlic Date: Wed, 18 Sep 2024 11:42:53 +0200 Subject: [PATCH 1/2] fix: failue in registration procedure is protected --- bot/src/Bot.cpp | 6 +++--- bot/src/main.cpp | 3 +++ include/Commands/PassCommand.hpp | 1 - include/Exception.hpp | 14 +++++++++++++- src/ClientSocket.cpp | 1 + src/Commands/PassCommand.cpp | 4 ++-- src/Epoll.cpp | 2 ++ 7 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bot/src/Bot.cpp b/bot/src/Bot.cpp index e05d17f..56591a3 100644 --- a/bot/src/Bot.cpp +++ b/bot/src/Bot.cpp @@ -123,13 +123,13 @@ void Bot::run() { void Bot::login() { _socket.sendMessage("CAP LS 302\r\n"); string data = _socket.receive(); - _socket.sendMessage( - "PASS bonjour\r\nNICK daddy\r\nUSER Dad * * :Dad\r\nCAP END\r\n"); + string rpl = "PASS " + _password + "\r\nNICK daddy\r\nUSER Dad * * :Dad\r\nCAP END\r\n"; + _socket.sendMessage(rpl); data = _socket.receive(); vector message = Message::split(data, ' '); if (message.at(1) != "001") - throw RegFailedException(); + throw RegFailedException(data); this->_nickname = message.at(2); while (data.find("221") == string::npos) { data = _socket.receive(); diff --git a/bot/src/main.cpp b/bot/src/main.cpp index 66979bf..d09725f 100644 --- a/bot/src/main.cpp +++ b/bot/src/main.cpp @@ -35,6 +35,9 @@ int main(int ac, char** av) { try { Bot::getInstance().init(ac, av); Bot::getInstance().run(); + } catch (RegFailedException& e) { + std::cerr << e.what(); + return EXIT_FAILURE; } catch (std::exception& e) { std::cerr << e.what() << "\n"; return EXIT_FAILURE; diff --git a/include/Commands/PassCommand.hpp b/include/Commands/PassCommand.hpp index e7313e6..51ccbbf 100644 --- a/include/Commands/PassCommand.hpp +++ b/include/Commands/PassCommand.hpp @@ -5,7 +5,6 @@ class PassCommand : public Command { private: - Client* client; std::string _password; public: diff --git a/include/Exception.hpp b/include/Exception.hpp index e11a214..c66c730 100644 --- a/include/Exception.hpp +++ b/include/Exception.hpp @@ -1,7 +1,19 @@ #pragma once #include +#include class ClientException : public std::exception {}; -class RegFailedException : public std::exception {}; +class RegFailedException : public std::exception { + std::string _msg; + + public: + RegFailedException(const std::string& msg) { + this->_msg = "Error: Registration failed\n" + msg; + } + RegFailedException() { this->_msg = "Error: registration failed."; } + virtual ~RegFailedException() throw() {} + + virtual const char* what() const throw() { return _msg.c_str(); } +}; class ServerException : public std::exception {}; \ No newline at end of file diff --git a/src/ClientSocket.cpp b/src/ClientSocket.cpp index 6618950..aa0292c 100644 --- a/src/ClientSocket.cpp +++ b/src/ClientSocket.cpp @@ -96,6 +96,7 @@ void ClientSocket::executeCommand(string data, Client* client) { } catch (RegFailedException&) { sendMessage(Replies::ERR_REGFAILED()); removeSelf(); + throw RegFailedException(); } delete c; } diff --git a/src/Commands/PassCommand.cpp b/src/Commands/PassCommand.cpp index 5b4e4b3..f18ab8a 100644 --- a/src/Commands/PassCommand.cpp +++ b/src/Commands/PassCommand.cpp @@ -28,11 +28,11 @@ void PassCommand::run() { return; } if (_client->getState() >= PASS_DONE) { - client->sendMessage(Replies::ERR_ALREADYREGISTERED(_client)); + _client->sendMessage(Replies::ERR_ALREADYREGISTERED(_client)); return; } if (_password != serverPass) { - client->sendMessage(Replies::ERR_PASSWDMISMATCH(_client)); + _client->sendMessage(Replies::ERR_PASSWDMISMATCH(_client)); throw RegFailedException(); } _client->setState(PASS_DONE); diff --git a/src/Epoll.cpp b/src/Epoll.cpp index 6e5ea17..2f4d18f 100644 --- a/src/Epoll.cpp +++ b/src/Epoll.cpp @@ -50,6 +50,8 @@ void Epoll::poll() { ((Socket*)events[i].data.ptr)->onPoll(events[i].events); } catch (ClientException& e) { std::cerr << e.what() << "\n"; + } catch (RegFailedException& e) { + break; } } } From 0c6dfe30b60534020d68fadbe5df168651f4fcd8 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 18 Sep 2024 09:44:11 +0000 Subject: [PATCH 2/2] Auto format code with clang-format --- bot/src/Bot.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/src/Bot.cpp b/bot/src/Bot.cpp index 56591a3..e6f8888 100644 --- a/bot/src/Bot.cpp +++ b/bot/src/Bot.cpp @@ -123,7 +123,8 @@ void Bot::run() { void Bot::login() { _socket.sendMessage("CAP LS 302\r\n"); string data = _socket.receive(); - string rpl = "PASS " + _password + "\r\nNICK daddy\r\nUSER Dad * * :Dad\r\nCAP END\r\n"; + string rpl = "PASS " + _password + + "\r\nNICK daddy\r\nUSER Dad * * :Dad\r\nCAP END\r\n"; _socket.sendMessage(rpl); data = _socket.receive();