From db1e03ae5c472a242dcaa93c81d916797b206b63 Mon Sep 17 00:00:00 2001 From: mochaoui Date: Wed, 17 Apr 2024 14:13:45 -0500 Subject: [PATCH] broadcast msg done --- Server.cpp | 50 +++++++++++++++++++++++++++++++++++++++----------- channel.hpp | 15 ++++++++++++++- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Server.cpp b/Server.cpp index 17f8d51..c17519f 100644 --- a/Server.cpp +++ b/Server.cpp @@ -275,6 +275,9 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen 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 const std::vector& clients = it->second.getClients(); @@ -283,12 +286,20 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen // Get the current client nickname 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 int recipientFd = it->second.getUserFd(client); // If the file descriptor is found, send the message to the client 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); } else { // 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 // processCommand(fd, command); } else if (startsWith(command, "JOIN ")) { - std::string user; + std::string nick; for (size_t i = 0; i < _clients.size(); ++i) { 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; } } std::string chanelname = command.substr(6); + std::cout << chanelname << std::endl; chanelname = trim(chanelname); - createChannel(chanelname, user, fd); + std::cout << chanelname << std::endl; + createChannel(chanelname, nick, fd); + + } else if (startsWith(command, "PRIVMSG ")) { // Extract the recipient and the message from the command std::istringstream iss(command); std::string cmd, recipient, message; + std::string niiick; iss >> cmd >> recipient; recipient = trim(recipient); std::getline(iss, message); message = trim(message); + message = message.substr(1); if (recipient[0] == '#') { + recipient = recipient.substr(1); 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 - { - if (message[0] == ':') { - message = message.substr(1); - handlePrivateMessage(fd, recipient, message); + + // std::string senderNickname = it->second.getNickname(fd); + broadcastMessage(recipient, niiick, message); } + // else + // { + // if (message[0] == ':') { + // message = message.substr(1); + // handlePrivateMessage(fd, recipient, message); + // } - } + // } // Remove the colon from the recipient if present // if (!recipient.empty() && recipient[1] == ':') { // recipient = recipient.substr(1); diff --git a/channel.hpp b/channel.hpp index 4650333..4d5ba4f 100644 --- a/channel.hpp +++ b/channel.hpp @@ -40,6 +40,9 @@ public: // Add a client to the channel + + + void addClient(const std::string& client, int fd) { userFdMap[client] = fd; } @@ -69,7 +72,7 @@ public: void addOperator(const std::string& operatorName) { operators.push_back(operatorName); } - + int getUserFd(const std::string& username) const { std::map::const_iterator it = userFdMap.find(username); if (it != userFdMap.end()) { @@ -88,6 +91,16 @@ public: return clients; } +std::string getNickname(int fd) const { + std::map::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 };