kick commade done : kick the user from the channel

This commit is contained in:
mochaoui
2024-04-18 15:18:53 -05:00
parent a2cf7bc4c8
commit e1ca1fe9a7
3 changed files with 90 additions and 35 deletions

View File

@@ -156,7 +156,7 @@ void Server::createChannel(const std::string& channelName, const std::string& ni
// Channel doesn't exist, so create it and add the user // Channel doesn't exist, so create it and add the user
Channel newChannel(channelName); Channel newChannel(channelName);
newChannel.addClient(nickname, fd); newChannel.addClient(nickname, fd);
newChannel.addOperator(nickname); newChannel.addOperator(nickname, fd);
std::cout << nickname << " : im the operator of the channel guys. " << std::endl; std::cout << nickname << " : im the operator of the channel guys. " << std::endl;
channels.insert(std::make_pair(channelName, newChannel)); // Insert the new channel into the map channels.insert(std::make_pair(channelName, newChannel)); // Insert the new channel into the map
std::cout << "Channel '" << channelName << "' created by '" << nickname << "'" << std::endl; std::cout << "Channel '" << channelName << "' created by '" << nickname << "'" << std::endl;
@@ -274,7 +274,7 @@ int Server::findUserFdforkickregulars(const std::string& username) {
return -1; // Return -1 if the nickname is not found return -1; // Return -1 if the nickname is not found
} }
// brodcasting msg to all nicks in the channel // brodcasting msg to all nicks in the channel
void Server::broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg) { void Server::broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg, int fd) {
// Check if the channel exists // Check if the channel exists
debugPrintChannels(); debugPrintChannels();
std::map<std::string, Channel>::iterator it = channels.find(channel); std::map<std::string, Channel>::iterator it = channels.find(channel);
@@ -283,6 +283,11 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen
return; return;
} }
if (channels[channel].findUserFdForKickRegulars(senderNickname) == -1)
{
std::cout << "this user kicked from the channel" << std::endl;
return;
}
// Construct the IRC message with the correct format for broadcasting // Construct the IRC message with the correct format for broadcasting
std::string message = ":" + senderNickname + " PRIVMSG #" + channel + " :" + msg + "\r\n"; std::string message = ":" + senderNickname + " PRIVMSG #" + channel + " :" + msg + "\r\n";
@@ -317,10 +322,10 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen
} }
//check if ope or not //check if ope or not
bool Server::isOperator(int fd) { // bool Server::isOperator(int fd) {
// Check if the file descriptor exists in the map of operators // // Check if the file descriptor exists in the map of operators
return usernamesoperators.find(fd) != usernamesoperators.end(); // int fd =
} // }
void Server::kickUser(int fd) { void Server::kickUser(int fd) {
@@ -403,7 +408,7 @@ void Server::handleClientData(int fd) {
send(fd, pongMessage.c_str(), pongMessage.length(), 0); send(fd, pongMessage.c_str(), pongMessage.length(), 0);
std::cout << "ping was sent" << std::endl; std::cout << "ping was sent" << std::endl;
} }
else if (startsWith(command, "nick")) { else if (startsWith(command, "nick") && a == 1) {
std::string cmd, nick; std::string cmd, nick;
std::istringstream iss(command); std::istringstream iss(command);
iss >> cmd >> nick; iss >> cmd >> nick;
@@ -424,7 +429,7 @@ void Server::handleClientData(int fd) {
// 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: " + nick + '\n'); sendResponse(fd, "Nickname set to: " + nick + '\n');
a = 2; a = 2;
} else if (startsWith(command, "user")) { } else if (startsWith(command, "user") && a == 2) {
std::istringstream iss(command); std::istringstream iss(command);
std::string cmd, username, dontworry, dontworry1, realname, nickname; std::string cmd, username, dontworry, dontworry1, realname, nickname;
@@ -445,14 +450,15 @@ void Server::handleClientData(int fd) {
break; break;
} }
} }
a = 0;
sendResponse(fd, "username set to: " + username + '\n'); sendResponse(fd, "username set to: " + username + '\n');
sendResponse(fd, "realname set to: " + realname + '\n'); sendResponse(fd, "realname set to: " + realname + '\n');
std::string one = ":irc.topg 001 " + nickname + " :Welcome to the topg Network, " + nickname + '\n'; std::string one = ":irc.l9oroch 001 " + nickname + " :Welcome to the l9oroch Network, " + nickname + '\n';
std::string two = ":irc.topg 002 " + nickname + " :Your host is topg, running version 3030" + '\n'; std::string two = ":irc.l9oroch 002 " + nickname + " :Your host is l9oroch, running version 3030" + '\n';
std::string tre = ":irc.topg 003 " + nickname + " :This server was created Tue Nov 30 2011 at 11:11:25 EET" + '\n'; std::string tre = ":irc.l9oroch 003 " + nickname + " :This server was created Tue Nov 30 2011 at 11:11:25 EET" + '\n';
std::string foor = ":irc.topg 004 " + nickname + " topg tella(enterprise)-2.3(12)-netty(5.4c)-proxy(0.9) oOiwscrknfbghexzSjFI bhijklmMnoOstvcdSuU bkohv" + '\n'; std::string foor = ":irc.l9oroch 004 " + nickname + " l9oroch tella(enterprise)-2.3(12)-netty(5.4c)-proxy(0.9) oOiwscrknfbghexzSjFI bhijklmMnoOstvcdSuU bkohv" + '\n';
send(fd, one.c_str(), one.length(), 0); send(fd, one.c_str(), one.length(), 0);
send(fd, two.c_str(), two.length(), 0); send(fd, two.c_str(), two.length(), 0);
send(fd, tre.c_str(), tre.length(), 0); send(fd, tre.c_str(), tre.length(), 0);
@@ -519,7 +525,7 @@ void Server::handleClientData(int fd) {
break; break;
} }
} }
broadcastMessage(recipient, niiick, message); broadcastMessage(recipient, niiick, message, fd);
} }
else else
{ {
@@ -528,22 +534,30 @@ void Server::handleClientData(int fd) {
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)) { else if (startsWith(command, "KICK ")) {
std::string userkicked = command.substr(5); // Extract the channel name and user to be kicked
userkicked = trim(userkicked); std::string channelName, userToKick;
std::istringstream iss(command.substr(6));
iss >> channelName >> userToKick;
channelName = trim(channelName);
userToKick = trim(userToKick);
int finduserfd = findUserFdforkickregulars(userkicked); // Check if the sender is an operator in the specified channel
if (finduserfd != -1) if (channels.find(channelName) != channels.end() && channels[channelName].isOperator(fd)) {
{ // Check if the user to be kicked is online
kickUser(finduserfd); int userFd = channels[channelName].findUserFdForKickRegulars(userToKick);
sendResponse(fd, "User '" + userkicked + "' has been kicked from the server." + '\n'); if (userFd != -1) {
// Kick the user
// kickUser(userFd);
channels[channelName].ejectUser(userFd);
sendResponse(fd, "User '" + userToKick + "' has been kicked from the server.\n");
} else {
sendResponse(fd, "Error: User '" + userToKick + "' not found or offline.\n");
} }
else } else {
sendResponse(fd, "Error: User '" + userkicked + "' not found or offline.\n"); sendResponse(fd, "Error: You are not authorized to execute this command.\n");
}
} }
else if (startsWith(command, "KICK "))
sendResponse(fd, "Error: You don't have permission to use this command.\n");
//**************** STOOOOOOP HERE TOP G ... //**************** STOOOOOOP HERE TOP G ...
break; break;

View File

@@ -52,7 +52,7 @@ class Server {
void setUsernameregular(int fd, const std::string& username); void setUsernameregular(int fd, const std::string& username);
void createChannel(const std::string& channel, const std::string& nickname, int fd); 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 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 fd);
int findUserFd1(const std::string& username); int findUserFd1(const std::string& username);
std::string findUsernameforsending(int fd); std::string findUsernameforsending(int fd);
bool isOperator(int fd); bool isOperator(int fd);

View File

@@ -17,7 +17,7 @@
#include "Client.hpp" #include "Client.hpp"
#include <cstring> #include <cstring>
#include <map> #include <map>
#include <string>
class Channel { class Channel {
private: private:
@@ -28,10 +28,12 @@ private:
// std::map<int, std::string> nicknames; // Replace unordered_map with map // std::map<int, std::string> nicknames; // Replace unordered_map with map
std::map<std::string, int> userFdMap; // Mapping of usernames to file descriptors std::map<std::string, int> userFdMap; // Mapping of usernames to file descriptors
std::vector<std::string> invitedUsers; std::vector<std::string> invitedUsers;
std::vector<std::string> operators; std::map<std::string, int> operators;
public: public:
// Constructors // Constructors
Channel() {}
Channel(const std::string& name) : Channelname(name) {} Channel(const std::string& name) : Channelname(name) {}
// Destructor // Destructor
@@ -68,8 +70,9 @@ public:
} }
// Add an operator to the channel // Add an operator to the channel
void addOperator(const std::string& operatorName) { void addOperator(const std::string& operatorName, int fd) {
operators.push_back(operatorName); // Store the operator name and file descriptor in the map
operators[operatorName] = fd;
} }
int getUserFd(const std::string& username) const { int getUserFd(const std::string& username) const {
@@ -90,16 +93,54 @@ public:
return clients; return clients;
} }
std::string getNickname(int fd) const { std::string getNickname(int fd) const {
std::map<std::string, int>::const_iterator it; std::map<std::string, int>::const_iterator it;
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) { for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
if (it->second == fd) { if (it->second == fd) {
return it->first; // Return the nickname if the file descriptor matches return it->first; // Return the nickname if the file descriptor matches
} }
}
return ""; // Return an empty string if the file descriptor is not found
} }
return ""; // Return an empty string if the file descriptor is not found
bool isOperator(int fd) {
// Iterate through the map of operators
for (std::map<std::string, int>::iterator it = operators.begin(); it != operators.end(); ++it) {
// Check if the file descriptor matches
if (it->second == fd) {
return true; // Found the file descriptor in the map
}
}
return false; // File descriptor not found in the map
}
int findUserFdForKickRegulars(const std::string& username) {
// Iterate through the userFdMap to find the user
std::map<std::string, int>::iterator it;
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
if (it->first == username) {
return it->second; // Return the file descriptor if the username matches
}
}
return -1; // Return -1 if the user is not found
} }
void ejectUser(int fd) {
// Iterate over the map to find the user with the given file descriptor
std::map<std::string, int>::iterator it;
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
if (it->second == fd) {
// Erase the user from the map
userFdMap.erase(it);
std::cout << "the user earased " << std::endl;
return; // Exit the function after removing the user
}
}
}
// Remove an operator from the channel // Remove an operator from the channel
}; };