make the clear the regular and the operators users and start handling the permition and the commande of each one
This commit is contained in:
59
Server.cpp
59
Server.cpp
@@ -158,8 +158,11 @@ bool startsWith(const std::string& str, const std::string& prefix) {
|
|||||||
nicknames[fd] = nickname; // Associate the nickname with the client's socket descriptor
|
nicknames[fd] = nickname; // Associate the nickname with the client's socket descriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::setUsername(int fd, const std::string& username) {
|
void Server::setUsernameoperators(int fd, const std::string& username) {
|
||||||
usernames[fd] = username; // Associate the nickname with the client's socket descriptor
|
usernamesoperators[fd] = username; // Associate the nickname with the client's socket descriptor
|
||||||
|
}
|
||||||
|
void Server::setUsernameregular(int fd, const std::string& username) {
|
||||||
|
usernamesregulars[fd] = username; // Associate the nickname with the client's socket descriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendResponse(int fd, const std::string& message) {
|
void sendResponse(int fd, const std::string& message) {
|
||||||
@@ -201,13 +204,23 @@ int findUserFd(const std::string& nickname, const std::map<int, std::string>& us
|
|||||||
|
|
||||||
|
|
||||||
void Server::handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message) {
|
void Server::handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message) {
|
||||||
// Find the recipient's connection (socket file descriptor)
|
// Find the recipient's connection (socket file descriptor) for operators
|
||||||
int recipientFd = findUserFd(recipient, usernames);
|
int recipientFd = findUserFd(recipient, usernamesoperators);
|
||||||
|
|
||||||
if (recipientFd != -1) {
|
if (recipientFd != -1) {
|
||||||
// Forward the private message to the recipient's client
|
// Forward the private message to the recipient's client
|
||||||
std::string privateMessage = "PRIVATE " + usernames[senderFd] + ": " + message + "\n";
|
std::string privateMessage = "PRIVATE " + usernamesoperators[senderFd] + ": " + message + "\n";
|
||||||
send(recipientFd, privateMessage.c_str(), privateMessage.length(), 0);
|
send(recipientFd, privateMessage.c_str(), privateMessage.length(), 0);
|
||||||
|
return; // Exit the function to avoid executing the second block
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the recipient's connection (socket file descriptor) for regular users
|
||||||
|
int recipientFd1 = findUserFd(recipient, usernamesregulars);
|
||||||
|
|
||||||
|
if (recipientFd1 != -1) {
|
||||||
|
// Forward the private message to the recipient's client
|
||||||
|
std::string privateMessage = "PRIVATE " + usernamesregulars[senderFd] + ": " + message + "\n";
|
||||||
|
send(recipientFd1, privateMessage.c_str(), privateMessage.length(), 0);
|
||||||
} else {
|
} else {
|
||||||
// Handle case where recipient is not found (e.g., user not online)
|
// Handle case where recipient is not found (e.g., user not online)
|
||||||
std::string errorMessage = "Error: User '" + recipient + "' not found or offline\n";
|
std::string errorMessage = "Error: User '" + recipient + "' not found or offline\n";
|
||||||
@@ -247,7 +260,7 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen
|
|||||||
|
|
||||||
// If the file descriptor is found, send the message to the client
|
// If the file descriptor is found, send the message to the client
|
||||||
if (recipientFd != -1) {
|
if (recipientFd != -1) {
|
||||||
std::string message = "PRIVMSG " + channel + " :" + senderNickname + ": " + msg + "\r\n";
|
std::string message = "[" + channel + " channel" + "] " + senderNickname + " : " + msg + "\r\n";
|
||||||
send(recipientFd, message.c_str(), message.size(), 0);
|
send(recipientFd, message.c_str(), message.size(), 0);
|
||||||
} else {
|
} else {
|
||||||
// If the file descriptor is not found, print an error message
|
// If the file descriptor is not found, print an error message
|
||||||
@@ -256,6 +269,11 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Server::isOperator(int fd) {
|
||||||
|
// Check if the file descriptor exists in the map of operators
|
||||||
|
return usernamesoperators.find(fd) != usernamesoperators.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -295,13 +313,30 @@ void Server::handleClientData(int fd) {
|
|||||||
setNickname(fd, nickname);
|
setNickname(fd, nickname);
|
||||||
|
|
||||||
// Send a response back to the client confirming the action
|
// Send a response back to the client confirming the action
|
||||||
sendResponse(fd, "Nickname set to: " + nickname);
|
sendResponse(fd, "Nickname set to: " + nickname + '\n');
|
||||||
} else if (startsWith(command, "/setuser ")) {
|
} else if (startsWith(command, "/setuser ")) {
|
||||||
std::string username = command.substr(9);
|
|
||||||
|
|
||||||
setUsername(fd, username);
|
std::istringstream iss(command);
|
||||||
|
std::string cmd, username, privilege_level;
|
||||||
|
iss >> cmd >> username;
|
||||||
|
username = trim(username);
|
||||||
|
std::getline(iss, privilege_level);
|
||||||
|
privilege_level = trim(privilege_level);
|
||||||
|
std::cout << "this is the privilege : " << privilege_level << std::endl;
|
||||||
|
|
||||||
|
if (privilege_level == "operators" )
|
||||||
|
{
|
||||||
|
//si moskir hna atbda lkhdma dyalk
|
||||||
|
std::cout << "we need to handle this " << std::endl;
|
||||||
|
setUsernameoperators(fd, username);
|
||||||
|
sendResponse(fd, "Username set to: " + username + " with privilege_level : " + privilege_level + '\n');
|
||||||
|
}
|
||||||
|
else if (privilege_level == "regular") {
|
||||||
|
setUsernameregular(fd, username);
|
||||||
|
sendResponse(fd, "Username set to: " + username + " with privilege_level : " + privilege_level + '\n');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
sendResponse(fd, "Username set to: " + username);
|
|
||||||
|
|
||||||
// Process other commands or messages
|
// Process other commands or messages
|
||||||
// processCommand(fd, command);
|
// processCommand(fd, command);
|
||||||
@@ -346,6 +381,10 @@ void Server::handleClientData(int fd) {
|
|||||||
// Print the extracted recipient and message for debugging
|
// Print the extracted recipient and message for debugging
|
||||||
std::cout << "Recipient: " << recipient << std::endl;
|
std::cout << "Recipient: " << recipient << std::endl;
|
||||||
std::cout << "Message: " << message << std::endl;
|
std::cout << "Message: " << message << std::endl;
|
||||||
|
}
|
||||||
|
else if(startsWith(command, "/kick ") && isOperator(fd)) {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
//**************** STOOOOOOP HERE TOP G ...
|
//**************** STOOOOOOP HERE TOP G ...
|
||||||
break;
|
break;
|
||||||
|
@@ -29,7 +29,8 @@ class Server {
|
|||||||
std::vector<Client> _clients;
|
std::vector<Client> _clients;
|
||||||
// THAT'S THA DATA OF TOOOP GGG START FROM THERE .
|
// THAT'S THA DATA OF TOOOP GGG START FROM THERE .
|
||||||
std::map<int, std::string> nicknames; // Replace unordered_map with map
|
std::map<int, std::string> nicknames; // Replace unordered_map with map
|
||||||
std::map<int, std::string> usernames; // Replace unordered_map with map
|
std::map<int, std::string> usernamesoperators; // Replace unordered_map with map
|
||||||
|
std::map<int, std::string> usernamesregulars;
|
||||||
std::map<std::string, std::vector<std::string> > channels; //here a chanel name and list of client in every chanel
|
std::map<std::string, std::vector<std::string> > channels; //here a chanel name and list of client in every chanel
|
||||||
|
|
||||||
|
|
||||||
@@ -39,11 +40,13 @@ class Server {
|
|||||||
~Server();
|
~Server();
|
||||||
// THAT'S MY FUNCTIONS START FROM THERE
|
// THAT'S MY FUNCTIONS START FROM THERE
|
||||||
void setNickname(int fd, const std::string& nickname);
|
void setNickname(int fd, const std::string& nickname);
|
||||||
void setUsername(int fd, const std::string& username);
|
void setUsernameoperators(int fd, const std::string& username);
|
||||||
|
void setUsernameregular(int fd, const std::string& username);
|
||||||
void createChannel(const std::string& channel, const std::string& nickname);
|
void createChannel(const std::string& channel, const std::string& nickname);
|
||||||
void handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message);
|
void handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message);
|
||||||
void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg);
|
void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg);
|
||||||
int findUserFd1(const std::string& nickname);
|
int findUserFd1(const std::string& nickname);
|
||||||
|
bool Server::isOperator(int fd);
|
||||||
// AND END HERE.
|
// AND END HERE.
|
||||||
void parseArgs(int ac, char **av);
|
void parseArgs(int ac, char **av);
|
||||||
static void receiveSignal(int signum);
|
static void receiveSignal(int signum);
|
||||||
|
Reference in New Issue
Block a user