Skip to content

Commit

Permalink
Merge pull request #59 from ArthanIRC/send-fixes
Browse files Browse the repository at this point in the history
fix: failure in registration procedure is now protected
  • Loading branch information
mortebrume authored Sep 18, 2024
2 parents c5055a2 + 0c6dfe3 commit ed82644
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
7 changes: 4 additions & 3 deletions bot/src/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ 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<string> 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();
Expand Down
3 changes: 3 additions & 0 deletions bot/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion include/Commands/PassCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class PassCommand : public Command {
private:
Client* client;
std::string _password;

public:
Expand Down
14 changes: 13 additions & 1 deletion include/Exception.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#pragma once

#include <exception>
#include <string>

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 {};
1 change: 1 addition & 0 deletions src/ClientSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void ClientSocket::executeCommand(string data, Client* client) {
} catch (RegFailedException&) {
sendMessage(Replies::ERR_REGFAILED());
removeSelf();
throw RegFailedException();
}
delete c;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/PassCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit ed82644

Please sign in to comment.