fixed client authentication bugs

This commit is contained in:
Bettercallous
2024-04-27 21:51:21 +01:00
parent f01089a434
commit 5338af5e4f
6 changed files with 36 additions and 26 deletions

View File

@@ -1,5 +0,0 @@
{
"files.associations": {
"iosfwd": "cpp"
}
}

View File

@@ -1,8 +1,8 @@
#include "Client.hpp"
Client::Client() : _isRegistered(false) {}
Client::Client() {}
Client::Client(int fd) : _fd(fd) {}
Client::Client(int fd) : _fd(fd), _authentication(0) {}
Client::~Client() {}
@@ -42,3 +42,11 @@ std::string Client::getUser() const {
void Client::setUser(const std::string& newUser) {
user = newUser;
}
int Client::getAuthentication() const {
return _authentication;
}
void Client::setAuthentication(int auth) {
_authentication = auth;
}

View File

@@ -10,7 +10,7 @@ class Client {
std::string name;
std::string nick;
std::string user;
bool _isRegistered;
int _authentication;
public:
Client();
Client(int fd);
@@ -31,6 +31,9 @@ class Client {
std::string getUser() const;
void setUser(const std::string& newUser);
int getAuthentication() const;
void setAuthentication(int auth);
};
#endif

View File

@@ -6,7 +6,7 @@ OBJ = ${SRC:.cpp=.o}
CXX = c++
CXXFLAGS = -std=c++98 -fsanitize=address -g3 #-Wall -Wextra -Werror
CXXFLAGS = -std=c++98 -fsanitize=address -g3 -Wall -Wextra -Werror
all : $(NAME)

View File

@@ -1,6 +1,5 @@
#include "Server.hpp"
int a = 0;
int opperatorfd = 0;
int issettop = 0;
int isinveted = 0;
@@ -347,7 +346,7 @@ int Server::findUserFdforkickregulars(const std::string& username) {
// brodcasting msg to all nicks in the channel
void Server::broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg, int fd) {
void Server::broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg) {
std::map<std::string, Channel>::iterator it = channels.find(channel);
if (it == channels.end()) {
std::cerr << "Channel " << channel << " does not exist" << std::endl;
@@ -559,9 +558,10 @@ void Server::handleClientData(int fd)
} else {
command.append(buffer, bytesRead - 1);
std::cout << "Received data from client " << fd << ": " << command << std::endl;
int auth = getClientByFd(fd).getAuthentication();
//******************* FROM THERE IM STARTING TOP GGG ************ .
if (startsWith(command, "pass") || startsWith(command, "PASS"))
if ((startsWith(command, "pass") || startsWith(command, "PASS")) && auth == 0)
{
std::string cmd, password;
std::istringstream iss(command);
@@ -576,7 +576,7 @@ void Server::handleClientData(int fd)
else {
std::string confirmation = "Please Enter Your Nickname : \n";
send(fd, confirmation.c_str(), confirmation.length(), 0);
a = 1;
getClientByFd(fd).setAuthentication(1);
}
}
@@ -589,7 +589,7 @@ void Server::handleClientData(int fd)
std::cout << "ping was sent" << std::endl;
}
else if ((startsWith(command, "nick") || startsWith(command, "NICK")) && a == 1)
else if ((startsWith(command, "nick") || startsWith(command, "NICK")) && auth == 1)
{
std::string cmd, nick;
std::istringstream iss(command);
@@ -620,13 +620,11 @@ void Server::handleClientData(int fd)
}
std::string confirmation = "Please Enter Your Username : \n";
send(fd, confirmation.c_str(), confirmation.length(), 0);
a = 2;
getClientByFd(fd).setAuthentication(2);
}
}
else if ((startsWith(command, "user") || startsWith(command, "USER")) && a == 2)
else if ((startsWith(command, "user") || startsWith(command, "USER")) && auth == 2)
{
std::istringstream iss(command);
@@ -662,7 +660,6 @@ void Server::handleClientData(int fd)
break;
}
}
a = 0;
std::string one = ":irc.l9oroch 001 " + nickname + " :Welcome to the TopG Network, " + nickname + '\n';
std::string two = ":irc.l9oroch 002 " + nickname + " :Your host is TopG, running version 4.5" + '\n';
@@ -789,7 +786,7 @@ void Server::handleClientData(int fd)
break;
}
}
broadcastMessage(recipient, niiick, message, fd);
broadcastMessage(recipient, niiick, message);
}
else
{
@@ -947,7 +944,6 @@ void Server::handleClientData(int fd)
else if (startsWith(command, "MODE ") || startsWith(command, "mode "))
{
std::string channelName, mode , nick;
int limit;
std::istringstream iss(command.substr(5));
iss >> channelName >> mode >> nick;
@@ -965,8 +961,6 @@ void Server::handleClientData(int fd)
// if (mode == "+l") // what is this ?
// nick = trim(nick);
std::map<std::string, Channel>::iterator it = channels.find(channelName);
std::cout << "this is the mode : " << mode << std::endl;
if (mode == "+o")
@@ -1164,6 +1158,15 @@ void Server::clientCleanup(int fd) {
}
}
Client& Server::getClientByFd(int fd) {
size_t i = 0;
for (; i < _clients.size(); ++i) {
if (_clients[i].getFd() == fd)
break;
}
return _clients[i];
}
int randomInRange(int min, int max) {
// Check for invalid range
if (min > max) {

View File

@@ -64,7 +64,7 @@ class Server {
void createChannel(const std::string& channel, const std::string& nickname, int fd);
void handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message);
void handleInvitation(int senderFd, const std::string& recipient, std::string channelName);
void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg, int fd);
void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg);
void smallbroadcastMessagefortheckick(std::string nicknamesender , const std::string& channelname, const std::string& usertokick, const std::string& reason);
void smallbroadcastMessageforjoin(std::string nicknamesender , const std::string& channelname);
void smallbroadcastMessageforTopic(std::string nicknamesender, const std::string& channelname, std::string topic);
@@ -90,6 +90,7 @@ class Server {
void clientCleanup(int fd);
void closeFds();
Client& getClientByFd(int fd);
};
int randomInRange(int min, int max);