fixed client authentication bugs
This commit is contained in:
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"iosfwd": "cpp"
|
||||
}
|
||||
}
|
14
Client.cpp
14
Client.cpp
@@ -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() {}
|
||||
|
||||
@@ -41,4 +41,12 @@ 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;
|
||||
}
|
||||
|
@@ -10,7 +10,7 @@ class Client {
|
||||
std::string name;
|
||||
std::string nick;
|
||||
std::string user;
|
||||
bool _isRegistered;
|
||||
int _authentication;
|
||||
public:
|
||||
Client();
|
||||
Client(int fd);
|
||||
@@ -30,6 +30,9 @@ class Client {
|
||||
// Getter and setter for user
|
||||
std::string getUser() const;
|
||||
void setUser(const std::string& newUser);
|
||||
|
||||
int getAuthentication() const;
|
||||
void setAuthentication(int auth);
|
||||
|
||||
};
|
||||
|
||||
|
2
Makefile
2
Makefile
@@ -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)
|
||||
|
||||
|
31
Server.cpp
31
Server.cpp
@@ -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) {
|
||||
|
@@ -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);
|
||||
@@ -89,7 +89,8 @@ class Server {
|
||||
void handleClientData(int fd);
|
||||
void clientCleanup(int fd);
|
||||
void closeFds();
|
||||
|
||||
|
||||
Client& getClientByFd(int fd);
|
||||
};
|
||||
|
||||
int randomInRange(int min, int max);
|
||||
|
Reference in New Issue
Block a user