kick commade done : kick the user from the channel
This commit is contained in:
66
Server.cpp
66
Server.cpp
@@ -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 newChannel(channelName);
|
||||
newChannel.addClient(nickname, fd);
|
||||
newChannel.addOperator(nickname);
|
||||
newChannel.addOperator(nickname, fd);
|
||||
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
|
||||
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
|
||||
}
|
||||
// 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
|
||||
debugPrintChannels();
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
bool Server::isOperator(int fd) {
|
||||
// Check if the file descriptor exists in the map of operators
|
||||
return usernamesoperators.find(fd) != usernamesoperators.end();
|
||||
}
|
||||
// bool Server::isOperator(int fd) {
|
||||
// // Check if the file descriptor exists in the map of operators
|
||||
// int fd =
|
||||
// }
|
||||
|
||||
|
||||
void Server::kickUser(int fd) {
|
||||
@@ -403,7 +408,7 @@ void Server::handleClientData(int fd) {
|
||||
send(fd, pongMessage.c_str(), pongMessage.length(), 0);
|
||||
std::cout << "ping was sent" << std::endl;
|
||||
}
|
||||
else if (startsWith(command, "nick")) {
|
||||
else if (startsWith(command, "nick") && a == 1) {
|
||||
std::string cmd, nick;
|
||||
std::istringstream iss(command);
|
||||
iss >> cmd >> nick;
|
||||
@@ -424,7 +429,7 @@ void Server::handleClientData(int fd) {
|
||||
// Send a response back to the client confirming the action
|
||||
sendResponse(fd, "Nickname set to: " + nick + '\n');
|
||||
a = 2;
|
||||
} else if (startsWith(command, "user")) {
|
||||
} else if (startsWith(command, "user") && a == 2) {
|
||||
|
||||
std::istringstream iss(command);
|
||||
std::string cmd, username, dontworry, dontworry1, realname, nickname;
|
||||
@@ -445,14 +450,15 @@ void Server::handleClientData(int fd) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
a = 0;
|
||||
|
||||
sendResponse(fd, "username set to: " + username + '\n');
|
||||
sendResponse(fd, "realname set to: " + realname + '\n');
|
||||
|
||||
std::string one = ":irc.topg 001 " + nickname + " :Welcome to the topg Network, " + nickname + '\n';
|
||||
std::string two = ":irc.topg 002 " + nickname + " :Your host is topg, 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 foor = ":irc.topg 004 " + nickname + " topg tella(enterprise)-2.3(12)-netty(5.4c)-proxy(0.9) oOiwscrknfbghexzSjFI bhijklmMnoOstvcdSuU bkohv" + '\n';
|
||||
std::string one = ":irc.l9oroch 001 " + nickname + " :Welcome to the l9oroch Network, " + nickname + '\n';
|
||||
std::string two = ":irc.l9oroch 002 " + nickname + " :Your host is l9oroch, running version 3030" + '\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.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, two.c_str(), two.length(), 0);
|
||||
send(fd, tre.c_str(), tre.length(), 0);
|
||||
@@ -519,7 +525,7 @@ void Server::handleClientData(int fd) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
broadcastMessage(recipient, niiick, message);
|
||||
broadcastMessage(recipient, niiick, message, fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -528,22 +534,30 @@ void Server::handleClientData(int fd) {
|
||||
std::cout << "Recipient: " << recipient << std::endl;
|
||||
std::cout << "Message: " << message << std::endl;
|
||||
}
|
||||
else if(startsWith(command, "KICK ") && isOperator(fd)) {
|
||||
std::string userkicked = command.substr(5);
|
||||
userkicked = trim(userkicked);
|
||||
else if (startsWith(command, "KICK ")) {
|
||||
// Extract the channel name and user to be kicked
|
||||
std::string channelName, userToKick;
|
||||
std::istringstream iss(command.substr(6));
|
||||
iss >> channelName >> userToKick;
|
||||
channelName = trim(channelName);
|
||||
userToKick = trim(userToKick);
|
||||
|
||||
int finduserfd = findUserFdforkickregulars(userkicked);
|
||||
if (finduserfd != -1)
|
||||
{
|
||||
kickUser(finduserfd);
|
||||
sendResponse(fd, "User '" + userkicked + "' has been kicked from the server." + '\n');
|
||||
// Check if the sender is an operator in the specified channel
|
||||
if (channels.find(channelName) != channels.end() && channels[channelName].isOperator(fd)) {
|
||||
// Check if the user to be kicked is online
|
||||
int userFd = channels[channelName].findUserFdForKickRegulars(userToKick);
|
||||
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
|
||||
sendResponse(fd, "Error: User '" + userkicked + "' not found or offline.\n");
|
||||
|
||||
} else {
|
||||
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 ...
|
||||
break;
|
||||
|
@@ -52,7 +52,7 @@ class Server {
|
||||
void setUsernameregular(int fd, const std::string& username);
|
||||
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 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);
|
||||
std::string findUsernameforsending(int fd);
|
||||
bool isOperator(int fd);
|
||||
|
57
channel.hpp
57
channel.hpp
@@ -17,7 +17,7 @@
|
||||
#include "Client.hpp"
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
|
||||
#include <string>
|
||||
|
||||
class Channel {
|
||||
private:
|
||||
@@ -28,10 +28,12 @@ private:
|
||||
// std::map<int, std::string> nicknames; // Replace unordered_map with map
|
||||
std::map<std::string, int> userFdMap; // Mapping of usernames to file descriptors
|
||||
std::vector<std::string> invitedUsers;
|
||||
std::vector<std::string> operators;
|
||||
std::map<std::string, int> operators;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
Channel() {}
|
||||
|
||||
Channel(const std::string& name) : Channelname(name) {}
|
||||
|
||||
// Destructor
|
||||
@@ -68,8 +70,9 @@ public:
|
||||
}
|
||||
|
||||
// Add an operator to the channel
|
||||
void addOperator(const std::string& operatorName) {
|
||||
operators.push_back(operatorName);
|
||||
void addOperator(const std::string& operatorName, int fd) {
|
||||
// Store the operator name and file descriptor in the map
|
||||
operators[operatorName] = fd;
|
||||
}
|
||||
|
||||
int getUserFd(const std::string& username) const {
|
||||
@@ -90,16 +93,54 @@ public:
|
||||
return clients;
|
||||
}
|
||||
|
||||
std::string getNickname(int fd) const {
|
||||
std::map<std::string, int>::const_iterator it;
|
||||
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
|
||||
std::string getNickname(int fd) const {
|
||||
std::map<std::string, int>::const_iterator it;
|
||||
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
|
||||
if (it->second == fd) {
|
||||
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
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user