broadcast msg done

This commit is contained in:
mochaoui
2024-04-17 14:13:45 -05:00
parent 7a54b230e3
commit db1e03ae5c
2 changed files with 53 additions and 12 deletions

View File

@@ -275,6 +275,9 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen
return; return;
} }
// Construct the IRC message with the correct format for broadcasting
std::string message = ":" + senderNickname + " PRIVMSG #" + channel + " :" + msg + "\r\n";
// Get a reference to the vector of clients in the channel // Get a reference to the vector of clients in the channel
const std::vector<std::string>& clients = it->second.getClients(); const std::vector<std::string>& clients = it->second.getClients();
@@ -283,12 +286,20 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen
// Get the current client nickname // Get the current client nickname
const std::string& client = clients[i]; const std::string& client = clients[i];
// Skip sending the message to the sender
std::cout << "this is the client name : "<< client << std::endl;
std::cout << "this is the nickname name : " << senderNickname << std::endl;
if (client == senderNickname) {
continue;
}
// Find the file descriptor associated with the client nickname // Find the file descriptor associated with the client nickname
int recipientFd = it->second.getUserFd(client); int recipientFd = it->second.getUserFd(client);
// 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 = "[" + channel + " channel" + "] " + senderNickname + " : " + msg + "\r\n"; std::cout << message << std::endl;
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
@@ -448,39 +459,56 @@ void Server::handleClientData(int fd) {
// Process other commands or messages // Process other commands or messages
// processCommand(fd, command); // processCommand(fd, command);
} else if (startsWith(command, "JOIN ")) { } else if (startsWith(command, "JOIN ")) {
std::string user; std::string nick;
for (size_t i = 0; i < _clients.size(); ++i) { for (size_t i = 0; i < _clients.size(); ++i) {
if (_clients[i].getFd() == fd) { if (_clients[i].getFd() == fd) {
user = _clients[i].getUser(); nick = _clients[i].getNick();
std::cout << "this is the nick for checkig : " << nick << std::endl;
break; break;
} }
} }
std::string chanelname = command.substr(6); std::string chanelname = command.substr(6);
std::cout << chanelname << std::endl;
chanelname = trim(chanelname); chanelname = trim(chanelname);
createChannel(chanelname, user, fd); std::cout << chanelname << std::endl;
createChannel(chanelname, nick, fd);
} else if (startsWith(command, "PRIVMSG ")) { } else if (startsWith(command, "PRIVMSG ")) {
// Extract the recipient and the message from the command // Extract the recipient and the message from the command
std::istringstream iss(command); std::istringstream iss(command);
std::string cmd, recipient, message; std::string cmd, recipient, message;
std::string niiick;
iss >> cmd >> recipient; iss >> cmd >> recipient;
recipient = trim(recipient); recipient = trim(recipient);
std::getline(iss, message); std::getline(iss, message);
message = trim(message); message = trim(message);
message = message.substr(1);
if (recipient[0] == '#') if (recipient[0] == '#')
{ {
recipient = recipient.substr(1); recipient = recipient.substr(1);
std::cout << "this the chanel name and was good : " << recipient << std::endl; std::cout << "this the chanel name and was good : " << recipient << std::endl;
broadcastMessage(recipient, nicknames[fd], message); for (size_t i = 0; i < _clients.size(); ++i) {
if (_clients[i].getFd() == fd) {
niiick = _clients[i].getNick();
// std::cout << "Password set for client " << fd << ": " << nick << std::endl;
break;
}
} }
else
{ // std::string senderNickname = it->second.getNickname(fd);
if (message[0] == ':') { broadcastMessage(recipient, niiick, message);
message = message.substr(1);
handlePrivateMessage(fd, recipient, message);
} }
// else
// {
// if (message[0] == ':') {
// message = message.substr(1);
// handlePrivateMessage(fd, recipient, message);
// }
} // }
// Remove the colon from the recipient if present // Remove the colon from the recipient if present
// if (!recipient.empty() && recipient[1] == ':') { // if (!recipient.empty() && recipient[1] == ':') {
// recipient = recipient.substr(1); // recipient = recipient.substr(1);

View File

@@ -40,6 +40,9 @@ public:
// Add a client to the channel // Add a client to the channel
void addClient(const std::string& client, int fd) { void addClient(const std::string& client, int fd) {
userFdMap[client] = fd; userFdMap[client] = fd;
} }
@@ -69,7 +72,7 @@ public:
void addOperator(const std::string& operatorName) { void addOperator(const std::string& operatorName) {
operators.push_back(operatorName); operators.push_back(operatorName);
} }
int getUserFd(const std::string& username) const { int getUserFd(const std::string& username) const {
std::map<std::string, int>::const_iterator it = userFdMap.find(username); std::map<std::string, int>::const_iterator it = userFdMap.find(username);
if (it != userFdMap.end()) { if (it != userFdMap.end()) {
@@ -88,6 +91,16 @@ public:
return clients; return clients;
} }
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
}
// Remove an operator from the channel // Remove an operator from the channel
}; };