fix fix fix

This commit is contained in:
mochaoui
2024-04-28 15:54:08 +01:00
parent 5338af5e4f
commit e9c1d60f46
4 changed files with 98 additions and 1 deletions

56
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,56 @@
{
"files.associations": {
"iostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
}

View File

@@ -560,7 +560,8 @@ void Server::handleClientData(int fd)
std::cout << "Received data from client " << fd << ": " << command << std::endl;
int auth = getClientByFd(fd).getAuthentication();
//******************* FROM THERE IM STARTING TOP GGG ************ .
//******************* FROM THERE IM STARTING TOP GGG ************
if ((startsWith(command, "pass") || startsWith(command, "PASS")) && auth == 0)
{
std::string cmd, password;
@@ -579,7 +580,34 @@ void Server::handleClientData(int fd)
getClientByFd(fd).setAuthentication(1);
}
}
else if (startsWith(command, "QUIT"))
{
// Iterate over channels
std::map<std::string, Channel>::iterator it;
for (it = channels.begin(); it != channels.end(); ++it)
{
Channel& channel = it->second;
std::string nickname = channel.getNickname(fd);
// Check if the user is part of the channel
if (channel.isUserInChannel(nickname))
{
// Mark the user as disconnected in the channel
channel.ejectUserfromusers(fd);
}
}
std::map<int, std::string>::iterator userIt;
userIt = usernames.find(fd);
if (userIt != usernames.end()) {
usernames.erase(userIt);
}
std::map<int, std::string>::iterator nickIt;
userIt = nicknames.find(fd);
if (userIt != nicknames.end()) {
nicknames.erase(userIt);
}
clientCleanup(fd);
}
else if (startsWith(command, "PING"))
{
std::istringstream iss(command);

View File

@@ -80,6 +80,17 @@ int Channel::getUserFd(const std::string& username) const {
return -1; // Return -1 if username not found
}
bool Channel::isUserInChannel(const std::string& nickname) const {
// Search for the nickname in the userMap
std::map<std::string, int>::const_iterator it = userFdMap.find(nickname);
// If the nickname is found and the user is connected, return true
if (it != userFdMap.end() && it->second) {
return true;
}
// Otherwise, return false
return false;
}
// Get all clients' usernames in the channel
std::vector<std::string> Channel::getClients() const {
std::vector<std::string> clients;

View File

@@ -32,6 +32,7 @@ private:
int opperatorfd;
bool issettop;
bool isinveted;
bool channelconnect;
public:
@@ -62,6 +63,7 @@ public:
void addClientinveted(const std::string& client, int fd);
void addOperator(const std::string& operatorName, int fd);
int getUserFd(const std::string& username) const;
bool isUserInChannel(const std::string& nickname) const;
std::vector<std::string> getClients() const;
std::string getNickname(int fd) const;
bool isOperator(int fd);