make the nickname and username nicked in the server don't worry about that
This commit is contained in:
463
Server.cpp
463
Server.cpp
@@ -123,18 +123,18 @@ void Server::handleClientConnection() {
|
|||||||
|
|
||||||
|
|
||||||
std::string art =
|
std::string art =
|
||||||
" ________ ______ ______ \n"
|
" ________ ______ \n"
|
||||||
"/ | / \\ / \\ \n"
|
"/ | / \\ \n"
|
||||||
"$$$$$$$/______ ______ /$$$$$$ | /$$$$$$ | ______ ______ __ __ ______ ______ \n"
|
"$$$$$$$/______ ______ /$$$$$$ | \n"
|
||||||
" $$ | / \\ / \\ $$ | _$$/ $$ \\__$$/ / \\ / \\ / \\ / |/ \\ / \\\n"
|
" $$ | / \\ / \\ $$ | _$$/ \n"
|
||||||
" $$ |/$$$$$$ |/$$$$$$ |$$ |/ | $$ \\ /$$$$$$ |/$$$$$$ |$$ \\ /$$//$$$$$$ |/$$$$$$ |\n"
|
" $$ |/$$$$$$ |/$$$$$$ |$$ |/ | \n"
|
||||||
" $$ |$$ | $$ |$$ | $$ |$$ |$$$$ | $$$$$$ |$$ $$ |$$ | $$/ $$ /$$/ $$ $$ |$$ | $$/ \n"
|
" $$ |$$ | $$ |$$ | $$ |$$ |$$$$ | \n"
|
||||||
" $$ |$$ \\__$$ |$$ |__$$ |$$ \\__$$ | / \\__$$ |$$$$$$$$/ $$ | $$ $$/ $$$$$$$$/ $$ | \n"
|
" $$ |$$ \\__$$ |$$ |__$$ |$$ \\__$$ | \n"
|
||||||
" $$ |$$ $$/ $$ $$/ $$ $$/ $$ $$/ $$ |$$ | $$$/ $$ |$$ | \n"
|
" $$ |$$ $$/ $$ $$/ $$ $$/ \n"
|
||||||
" $$/ $$$$$$/ $$$$$$$/ $$$$$$/ $$$$$$/ $$$$$$$/ $$/ $/ $$$$$$$/ $$/ \n"
|
" $$/ $$$$$$/ $$$$$$$/ $$$$$$/ \n"
|
||||||
" $$ | \n"
|
" $$ | \n"
|
||||||
" $$ | \n"
|
" $$ | \n"
|
||||||
" $$/ \n";
|
" $$/ \n";
|
||||||
|
|
||||||
send(newFd, art.c_str(), art.length(), 0);
|
send(newFd, art.c_str(), art.length(), 0);
|
||||||
|
|
||||||
@@ -155,33 +155,22 @@ std::string art =
|
|||||||
|
|
||||||
// FUNCTIONS OF TOP GGGGGG.. START FROM HEEEREEE
|
// FUNCTIONS OF TOP GGGGGG.. START FROM HEEEREEE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//triiiiiiiiiiiimmmmmm
|
//triiiiiiiiiiiimmmmmm
|
||||||
std::string trim(const std::string& str) {
|
std::string trim(const std::string& str) {
|
||||||
// Find the first non-whitespace character
|
|
||||||
size_t first = str.find_first_not_of(" \t\n\r");
|
size_t first = str.find_first_not_of(" \t\n\r");
|
||||||
|
|
||||||
// If the string is all whitespace, return an empty string
|
|
||||||
if (std::string::npos == first) {
|
if (std::string::npos == first) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the last non-whitespace character
|
|
||||||
size_t last = str.find_last_not_of(" \t\n\r");
|
size_t last = str.find_last_not_of(" \t\n\r");
|
||||||
|
|
||||||
// Return the trimmed substring
|
|
||||||
return str.substr(first, last - first + 1);
|
return str.substr(first, last - first + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Server::formatCreationTime() {
|
std::string Server::formatCreationTime() {
|
||||||
// Get the current time
|
|
||||||
std::time_t currentTime = std::time(NULL);
|
std::time_t currentTime = std::time(NULL);
|
||||||
// Convert the current time to tm struct for easier manipulation
|
|
||||||
std::tm* localTime = std::localtime(¤tTime);
|
std::tm* localTime = std::localtime(¤tTime);
|
||||||
|
|
||||||
// Format the time string manually
|
char buffer[80];
|
||||||
char buffer[80]; // Buffer to hold the formatted time string
|
|
||||||
std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", localTime);
|
std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", localTime);
|
||||||
return std::string(buffer);
|
return std::string(buffer);
|
||||||
}
|
}
|
||||||
@@ -211,178 +200,96 @@ void Server::createChannel(const std::string& channelName, const std::string& ni
|
|||||||
newChannel.addClient(nickname, fd);
|
newChannel.addClient(nickname, fd);
|
||||||
newChannel.addOperator(nickname, fd);
|
newChannel.addOperator(nickname, fd);
|
||||||
opperatorfd = fd;
|
opperatorfd = fd;
|
||||||
|
std::string creationTimeMessage = constructCreationTimeMessage(channelName);
|
||||||
|
|
||||||
// Send JOIN message to the client
|
|
||||||
std::string joinMessage = ":" + nickname + " JOIN #" + channelName + "\n";
|
std::string joinMessage = ":" + nickname + " JOIN #" + channelName + "\n";
|
||||||
send(fd, joinMessage.c_str(), joinMessage.length(), 0);
|
send(fd, joinMessage.c_str(), joinMessage.length(), 0);
|
||||||
|
|
||||||
// Send MODE message to the client
|
|
||||||
std::string modeMessage = ":irc.TopG MODE #" + channelName + " +nt\n";
|
std::string modeMessage = ":irc.TopG MODE #" + channelName + " +nt\n";
|
||||||
send(fd, modeMessage.c_str(), modeMessage.length(), 0);
|
send(fd, modeMessage.c_str(), modeMessage.length(), 0);
|
||||||
|
|
||||||
// Send NAMES message to the client
|
|
||||||
std::string namesMessage = ":irc.TopG 353 " + nickname + " = #" + channelName + " :@" + nickname + "\n";
|
std::string namesMessage = ":irc.TopG 353 " + nickname + " = #" + channelName + " :@" + nickname + "\n";
|
||||||
send(fd, namesMessage.c_str(), namesMessage.length(), 0);
|
send(fd, namesMessage.c_str(), namesMessage.length(), 0);
|
||||||
|
|
||||||
// Send END OF NAMES message to the client
|
|
||||||
std::string endOfNamesMessage = ":irc.TopG 366 " + nickname + " #" + channelName + " :End of /NAMES list.\n";
|
std::string endOfNamesMessage = ":irc.TopG 366 " + nickname + " #" + channelName + " :End of /NAMES list.\n";
|
||||||
send(fd, endOfNamesMessage.c_str(), endOfNamesMessage.length(), 0);
|
send(fd, endOfNamesMessage.c_str(), endOfNamesMessage.length(), 0);
|
||||||
|
std::string channelMessage = ":irc.TopG 354 " + channelName + " " + creationTimeMessage + "\n";
|
||||||
std::string creationTimeMessage = constructCreationTimeMessage(channelName);
|
send(fd, channelMessage.c_str(), channelMessage.length(), 0);
|
||||||
std::string channelMessage = ":irc.TopG 354 " + channelName + " " + creationTimeMessage + "\n";
|
|
||||||
send(fd, channelMessage.c_str(), channelMessage.length(), 0);
|
|
||||||
|
|
||||||
// Insert the new channel into the map
|
// Insert the new channel into the map
|
||||||
channels.insert(std::make_pair(channelName, newChannel));
|
channels.insert(std::make_pair(channelName, newChannel));
|
||||||
|
|
||||||
} else {
|
|
||||||
// Channel already exists, just add the user to it
|
|
||||||
it->second.addClient(nickname, fd);
|
|
||||||
std::string operators = channels[channelName].getOperatorNickname(opperatorfd);
|
|
||||||
|
|
||||||
// Send JOIN message to the client
|
|
||||||
std::string joinMessage = ":" + nickname + " JOIN #" + channelName + "\n";
|
|
||||||
send(fd, joinMessage.c_str(), joinMessage.length(), 0);
|
|
||||||
|
|
||||||
// Send CHANNEL TOPIC message to the client
|
|
||||||
std::cout << "this is the topi and he good : "<< channels[channelName].getTopic() << std::endl;
|
|
||||||
std::string topicMessage = ":irc.TopG 332 " + nickname + " #" + channelName + " :" + channels[channelName].getTopic() + " https://irc.com\n";
|
|
||||||
send(fd, topicMessage.c_str(), topicMessage.length(), 0);
|
|
||||||
|
|
||||||
// Send CHANNEL CREATION TIME message to the client
|
|
||||||
|
|
||||||
// Send NAMES message to the client
|
|
||||||
std::string namesMessage = ":irc.TopG 353 " + nickname + " @ #" + channelName + " :";
|
|
||||||
|
|
||||||
const std::vector<std::string>& clients = channels[channelName].getClients();
|
|
||||||
|
|
||||||
|
|
||||||
for (size_t i = 0; i < clients.size(); ++i) {
|
|
||||||
const std::string& user = clients[i];
|
|
||||||
if (user == operators) {
|
|
||||||
namesMessage += "@" + user;
|
|
||||||
} else {
|
|
||||||
namesMessage += user;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i < clients.size() - 1) {
|
|
||||||
namesMessage += " ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
namesMessage += "\n";
|
|
||||||
send(fd, namesMessage.c_str(), namesMessage.length(), 0);
|
|
||||||
|
|
||||||
// Send END OF NAMES message to the client
|
else
|
||||||
std::string endOfNamesMessage = ":irc.example.com 366 " + nickname + " #" + channelName + " :End of /NAMES list.\n";
|
{
|
||||||
send(fd, endOfNamesMessage.c_str(), endOfNamesMessage.length(), 0);
|
// Channel already exists, just add the user to it
|
||||||
|
it->second.addClient(nickname, fd);
|
||||||
|
std::string operators = channels[channelName].getOperatorNickname(opperatorfd);
|
||||||
|
std::string creationTimeMessage = constructJoinedTimeMessage(channelName);
|
||||||
|
|
||||||
std::string creationTimeMessage = constructJoinedTimeMessage(channelName);
|
|
||||||
std::string channelMessage = ":irc.example.com 354 " + channelName + " " + creationTimeMessage + "\n";
|
|
||||||
send(fd, channelMessage.c_str(), channelMessage.length(), 0);
|
|
||||||
|
|
||||||
smallbroadcastMessageforjoin(nickname, channelName);
|
// Send JOIN message to the client
|
||||||
}
|
std::string joinMessage = ":" + nickname + " JOIN #" + channelName + "\n";
|
||||||
|
send(fd, joinMessage.c_str(), joinMessage.length(), 0);
|
||||||
|
std::cout << "this is the topi and he good : "<< channels[channelName].getTopic() << std::endl;
|
||||||
|
std::string topicMessage = ":irc.TopG 332 " + nickname + " #" + channelName + " :" + channels[channelName].getTopic() + " https://irc.com\n";
|
||||||
|
send(fd, topicMessage.c_str(), topicMessage.length(), 0);
|
||||||
|
std::string namesMessage = ":irc.TopG 353 " + nickname + " @ #" + channelName + " :";
|
||||||
|
const std::vector<std::string>& clients = channels[channelName].getClients();
|
||||||
|
|
||||||
|
|
||||||
|
for (size_t i = 0; i < clients.size(); ++i) {
|
||||||
|
const std::string& user = clients[i];
|
||||||
|
if (user == operators) {
|
||||||
|
namesMessage += "@" + user;
|
||||||
|
} else {
|
||||||
|
namesMessage += user;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i < clients.size() - 1) {
|
||||||
|
namesMessage += " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
namesMessage += "\n";
|
||||||
|
send(fd, namesMessage.c_str(), namesMessage.length(), 0);
|
||||||
|
std::string endOfNamesMessage = ":irc.example.com 366 " + nickname + " #" + channelName + " :End of /NAMES list.\n";
|
||||||
|
send(fd, endOfNamesMessage.c_str(), endOfNamesMessage.length(), 0);
|
||||||
|
std::string channelMessage = ":irc.example.com 354 " + channelName + " " + creationTimeMessage + "\n";
|
||||||
|
send(fd, channelMessage.c_str(), channelMessage.length(), 0);
|
||||||
|
|
||||||
|
smallbroadcastMessageforjoin(nickname, channelName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool startsWith(const std::string& str, const std::string& prefix) {
|
bool startsWith(const std::string& str, const std::string& prefix) {
|
||||||
return str.substr(0, prefix.length()) == prefix;
|
return str.substr(0, prefix.length()) == prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::setNickname(int fd, const std::string& nickname) {
|
void Server::setNickname(int fd, const std::string& nickname) {
|
||||||
nicknames[fd] = nickname; // Associate the nickname with the client's socket descriptor
|
nicknames[fd] = nickname;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::setUsernameoperators(int fd, const std::string& username) {
|
void Server::setUsernames(int fd, const std::string& username) {
|
||||||
usernamesoperators[fd] = username; // Associate the nickname with the client's socket descriptor
|
usernames[fd] = username;
|
||||||
}
|
}
|
||||||
void Server::setUsernames(int fd, const std::string& username) {
|
|
||||||
usernames[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
|
|
||||||
}
|
|
||||||
|
|
||||||
//it's a just a response for my client
|
|
||||||
void sendResponse(int fd, const std::string& message) {
|
|
||||||
// Convert the message string to a C-style string
|
|
||||||
const char* msg = message.c_str();
|
|
||||||
|
|
||||||
// Get the length of the message
|
|
||||||
size_t len = strlen(msg);
|
|
||||||
|
|
||||||
// Send the message to the client
|
|
||||||
ssize_t bytesSent = send(fd, msg, len, 0);
|
|
||||||
|
|
||||||
// Check if the send operation was successful
|
|
||||||
if (bytesSent == -1) {
|
|
||||||
// Handle send error
|
|
||||||
// You can log an error message or take appropriate action
|
|
||||||
std::cerr << "Error sending response to client <" << fd << ">" << std::endl;
|
|
||||||
} else {
|
|
||||||
// Output the sent message to the console
|
|
||||||
std::cout << "Response sent to client <" << fd << ">: " << message << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//here finding the users for the handling the privet msg
|
|
||||||
int findUserFd(const std::string& nickname, const std::map<int, std::string>& usernames) {
|
|
||||||
// Iterate over each entry in the usernames map
|
|
||||||
for (std::map<int, std::string>::const_iterator it = usernames.begin(); it != usernames.end(); ++it) {
|
|
||||||
// Check if the nickname matches
|
|
||||||
if (it->second == nickname) {
|
|
||||||
// Return the file descriptor associated with the nickname
|
|
||||||
return it->first;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Return -1 if the nickname is not found
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
///iterate to find the username with the fd
|
|
||||||
|
|
||||||
// std::string Server::findUsernameforsending(int fd) {
|
|
||||||
// std::map<int, std::string>::iterator it;
|
|
||||||
// for (it = usernames.begin(); it != usernames.end(); ++it) {
|
|
||||||
// if (it->first == fd) {
|
|
||||||
// return it->second; // Return the file descriptor if the nickname matches
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//forget it
|
|
||||||
|
|
||||||
//handling privet msg between users only
|
//handling privet msg between users only
|
||||||
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)
|
|
||||||
int recipientFd = findUserFd1(recipient);
|
int recipientFd = findUserFd1(recipient);
|
||||||
// std::string sendernameuser = findUsernameforsending(senderFd);
|
|
||||||
|
|
||||||
if (recipientFd != -1) {
|
if (recipientFd != -1) {
|
||||||
// Forward the private message to the recipient's client
|
|
||||||
std::string privateMessage = ":" + nicknames[senderFd] + " PRIVMSG " + recipient + " :" + message + "\r\n";
|
std::string privateMessage = ":" + nicknames[senderFd] + " PRIVMSG " + recipient + " :" + message + "\r\n";
|
||||||
send(recipientFd, privateMessage.c_str(), privateMessage.length(), 0);
|
send(recipientFd, privateMessage.c_str(), privateMessage.length(), 0);
|
||||||
} else {
|
} else {
|
||||||
// Handle case where recipient is not found (e.g., user not online)
|
|
||||||
std::string errorMessage = ":server.host NOTICE " + nicknames[senderFd] + " :Error: User '" + recipient + "' not found or offline\r\n";
|
std::string errorMessage = ":server.host NOTICE " + nicknames[senderFd] + " :Error: User '" + recipient + "' not found or offline\r\n";
|
||||||
send(senderFd, errorMessage.c_str(), errorMessage.length(), 0);
|
send(senderFd, errorMessage.c_str(), errorMessage.length(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::handleInvitation(int senderFd, const std::string& recipient, std::string channelName) {
|
void Server::handleInvitation(int senderFd, const std::string& recipient, std::string channelName) {
|
||||||
// Find the recipient's connection (socket file descriptor)
|
|
||||||
int recipientFd = findUserFd1(recipient);
|
int recipientFd = findUserFd1(recipient);
|
||||||
// std::string sendernameuser = findUsernameforsending(senderFd);
|
|
||||||
|
|
||||||
if (recipientFd != -1) {
|
if (recipientFd != -1) {
|
||||||
// Construct the invitation message
|
|
||||||
std::string inviteMessage = ":" + nicknames[senderFd] + " INVITE " + recipient + " :#" + channelName + "\r\n";
|
std::string inviteMessage = ":" + nicknames[senderFd] + " INVITE " + recipient + " :#" + channelName + "\r\n";
|
||||||
|
|
||||||
// Send the invitation message to the recipient
|
|
||||||
send(recipientFd, inviteMessage.c_str(), inviteMessage.length(), 0);
|
send(recipientFd, inviteMessage.c_str(), inviteMessage.length(), 0);
|
||||||
} else {
|
} else {
|
||||||
// Handle case where recipient is not found (e.g., user not online)
|
|
||||||
std::string errorMessage = ":server.host NOTICE " + nicknames[senderFd] + " :Error: User '" + recipient + "' not found or offline\r\n";
|
std::string errorMessage = ":server.host NOTICE " + nicknames[senderFd] + " :Error: User '" + recipient + "' not found or offline\r\n";
|
||||||
send(senderFd, errorMessage.c_str(), errorMessage.length(), 0);
|
send(senderFd, errorMessage.c_str(), errorMessage.length(), 0);
|
||||||
}
|
}
|
||||||
@@ -393,25 +300,45 @@ int Server::findUserFd1(const std::string& username) {
|
|||||||
std::map<int, std::string>::iterator it;
|
std::map<int, std::string>::iterator it;
|
||||||
for (it = nicknames.begin(); it != nicknames.end(); ++it) {
|
for (it = nicknames.begin(); it != nicknames.end(); ++it) {
|
||||||
if (it->second == username) {
|
if (it->second == username) {
|
||||||
return it->first; // Return the file descriptor if the nickname matches
|
return it->first;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1; // Return -1 if the nickname is not found
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Server::dontputthesamenick(const std::string& nickname) {
|
||||||
|
std::map<int, std::string>::iterator it;
|
||||||
|
for (it = nicknames.begin(); it != nicknames.end(); ++it) {
|
||||||
|
if (it->second == nickname) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Server::dontputthesameusername(const std::string& username) {
|
||||||
|
std::map<int, std::string>::iterator it;
|
||||||
|
for (it = usernames.begin(); it != usernames.end(); ++it) {
|
||||||
|
if (it->second == username) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Server::findUserFdforkickregulars(const std::string& username) {
|
int Server::findUserFdforkickregulars(const std::string& username) {
|
||||||
std::map<int, std::string>::iterator it;
|
std::map<int, std::string>::iterator it;
|
||||||
for (it = usernamesregulars.begin(); it != usernamesregulars.end(); ++it) {
|
for (it = usernamesregulars.begin(); it != usernamesregulars.end(); ++it) {
|
||||||
if (it->second == username) {
|
if (it->second == username) {
|
||||||
return it->first; // Return the file descriptor if the nickname matches
|
return it->first;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1; // Return -1 if the nickname is not found
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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, int fd) {
|
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);
|
std::map<std::string, Channel>::iterator it = channels.find(channel);
|
||||||
if (it == channels.end()) {
|
if (it == channels.end()) {
|
||||||
std::cerr << "Channel " << channel << " does not exist" << std::endl;
|
std::cerr << "Channel " << channel << " does not exist" << std::endl;
|
||||||
@@ -422,19 +349,13 @@ 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";
|
std::string message = ":" + senderNickname + " PRIVMSG #" + channel + " :" + msg + "\r\n";
|
||||||
|
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
// Iterate over the vector of clients and send the message to each one
|
|
||||||
for (size_t i = 0; i < clients.size(); ++i) {
|
for (size_t i = 0; i < clients.size(); ++i) {
|
||||||
// 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 client name : "<< client << std::endl;
|
||||||
std::cout << "this is the nickname name : " << senderNickname << std::endl;
|
std::cout << "this is the nickname name : " << senderNickname << std::endl;
|
||||||
|
|
||||||
@@ -442,20 +363,19 @@ void Server::broadcastMessage(const std::string& channel, const std::string& sen
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 (recipientFd != -1) {
|
if (recipientFd != -1) {
|
||||||
std::cout << message << std::endl;
|
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
|
|
||||||
std::cerr << "Client " << client << " not found" << std::endl;
|
std::cerr << "Client " << client << " not found" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Server::smallbroadcastMessagefortheckick(std::string nicknamesender , const std::string& channelname, const std::string& usertokick, const std::string& reason) {
|
void Server::smallbroadcastMessagefortheckick(std::string nicknamesender , const std::string& channelname, const std::string& usertokick, const std::string& reason) {
|
||||||
std::map<std::string, Channel>::iterator it = channels.find(channelname);
|
std::map<std::string, Channel>::iterator it = channels.find(channelname);
|
||||||
if (it == channels.end()) {
|
if (it == channels.end()) {
|
||||||
@@ -464,12 +384,9 @@ void Server::smallbroadcastMessagefortheckick(std::string nicknamesender , const
|
|||||||
}
|
}
|
||||||
std::string kickMessage = ":" + nicknamesender + " KICK #" + channelname + " " + usertokick + " :" + reason + "\n";
|
std::string kickMessage = ":" + nicknamesender + " KICK #" + channelname + " " + usertokick + " :" + reason + "\n";
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
// Iterate over the vector of clients and send the message to each one
|
|
||||||
for (size_t i = 0; i < clients.size(); ++i) {
|
for (size_t i = 0; i < clients.size(); ++i) {
|
||||||
// Get the current client nickname
|
|
||||||
const std::string& client = clients[i];
|
const std::string& client = clients[i];
|
||||||
|
|
||||||
if (client == nicknamesender) {
|
if (client == nicknamesender) {
|
||||||
@@ -492,12 +409,9 @@ void Server::smallbroadcastMessageforjoin(std::string nicknamesender , const std
|
|||||||
}
|
}
|
||||||
std::string joinMessage = ":" + nicknamesender + " JOIN #" + channelname + "\n";
|
std::string joinMessage = ":" + nicknamesender + " JOIN #" + channelname + "\n";
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
// Iterate over the vector of clients and send the message to each one
|
|
||||||
for (size_t i = 0; i < clients.size(); ++i) {
|
for (size_t i = 0; i < clients.size(); ++i) {
|
||||||
// Get the current client nickname
|
|
||||||
const std::string& client = clients[i];
|
const std::string& client = clients[i];
|
||||||
|
|
||||||
if (client == nicknamesender) {
|
if (client == nicknamesender) {
|
||||||
@@ -520,15 +434,11 @@ void Server::smallbroadcastMessageforTopic(std::string nicknamesender, const std
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct the topic message
|
|
||||||
std::string topicMessage = ":" + nicknamesender + " TOPIC #" + channelname + " :" + topic + "\n";
|
std::string topicMessage = ":" + nicknamesender + " TOPIC #" + channelname + " :" + topic + "\n";
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
// Iterate over the vector of clients and send the message to each one
|
|
||||||
for (size_t i = 0; i < clients.size(); ++i) {
|
for (size_t i = 0; i < clients.size(); ++i) {
|
||||||
// Get the current client nickname
|
|
||||||
const std::string& client = clients[i];
|
const std::string& client = clients[i];
|
||||||
|
|
||||||
if (client == nicknamesender) {
|
if (client == nicknamesender) {
|
||||||
@@ -545,13 +455,13 @@ void Server::smallbroadcastMessageforTopic(std::string nicknamesender, const std
|
|||||||
|
|
||||||
|
|
||||||
void Server::smallbroadcastMOOD(std::string nicknamesender, const std::string& channelname, std::string mode, std::string receiver) {
|
void Server::smallbroadcastMOOD(std::string nicknamesender, const std::string& channelname, std::string mode, std::string receiver) {
|
||||||
|
|
||||||
std::map<std::string, Channel>::iterator it = channels.find(channelname);
|
std::map<std::string, Channel>::iterator it = channels.find(channelname);
|
||||||
if (it == channels.end()) {
|
if (it == channels.end()) {
|
||||||
std::cerr << "Channel " << channelname << " does not exist" << std::endl;
|
std::cerr << "Channel " << channelname << " does not exist" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct the mode change message
|
|
||||||
std::string modeChangeMessage;
|
std::string modeChangeMessage;
|
||||||
if (mode == "+t") {
|
if (mode == "+t") {
|
||||||
modeChangeMessage = ":server.host MODE #" + channelname + " +t by " + nicknamesender + "\n";
|
modeChangeMessage = ":server.host MODE #" + channelname + " +t by " + nicknamesender + "\n";
|
||||||
@@ -571,15 +481,11 @@ void Server::smallbroadcastMOOD(std::string nicknamesender, const std::string& c
|
|||||||
modeChangeMessage = ":server.host MODE #" + channelname + " -i by " + nicknamesender + "\n";
|
modeChangeMessage = ":server.host MODE #" + channelname + " -i by " + nicknamesender + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the file descriptor of the sender
|
|
||||||
int senderFd = it->second.getUserFd(nicknamesender);
|
int senderFd = it->second.getUserFd(nicknamesender);
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
// Iterate over the vector of clients and send the message to each one
|
|
||||||
for (size_t i = 0; i < clients.size(); ++i) {
|
for (size_t i = 0; i < clients.size(); ++i) {
|
||||||
// Get the current client nickname
|
|
||||||
const std::string& client = clients[i];
|
const std::string& client = clients[i];
|
||||||
|
|
||||||
int recipientFd = it->second.getUserFd(client);
|
int recipientFd = it->second.getUserFd(client);
|
||||||
@@ -592,24 +498,10 @@ void Server::smallbroadcastMOOD(std::string nicknamesender, const std::string& c
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//check if ope or not
|
|
||||||
// bool Server::isOperator(int fd) {
|
|
||||||
// // Check if the file descriptor exists in the map of operators
|
|
||||||
// int fd =
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
void Server::kickUser(int fd) {
|
void Server::kickUser(int fd) {
|
||||||
// Close the socket connection for the given file descriptor
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
// Remove the user from any data structures tracking active connections
|
|
||||||
// For example, if you have a map of file descriptors to usernames:
|
|
||||||
std::map<int, std::string>::iterator it = usernamesregulars.find(fd);
|
std::map<int, std::string>::iterator it = usernamesregulars.find(fd);
|
||||||
if (it != usernamesregulars.end()) {
|
if (it != usernamesregulars.end()) {
|
||||||
// Remove the user from the map
|
|
||||||
usernamesregulars.erase(it);
|
usernamesregulars.erase(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -663,96 +555,89 @@ void Server::handleClientData(int fd) {
|
|||||||
else if (startsWith(command, "PING"))
|
else if (startsWith(command, "PING"))
|
||||||
{
|
{
|
||||||
std::istringstream iss(command);
|
std::istringstream iss(command);
|
||||||
|
|
||||||
std::string serverHostname = command.substr(5); // Skip the "PING " prefix
|
std::string serverHostname = command.substr(5); // Skip the "PING " prefix
|
||||||
|
|
||||||
// Construct the PONG message with the server hostname
|
|
||||||
std::string pongMessage = "PONG " + serverHostname + "\r\n";
|
std::string pongMessage = "PONG " + serverHostname + "\r\n";
|
||||||
|
|
||||||
// Send the PONG message back to the client
|
|
||||||
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") && a == 1) {
|
|
||||||
|
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;
|
||||||
nick = trim(nick);
|
nick = trim(nick);
|
||||||
setNickname(fd, nick);
|
if (dontputthesamenick(nick) == true)
|
||||||
for (size_t i = 0; i < _clients.size(); ++i) {
|
{
|
||||||
if (_clients[i].getFd() == fd) {
|
std::string confirmation = "Please Use a Different Nickname : \n";
|
||||||
_clients[i].setNick(nick);
|
send(fd, confirmation.c_str(), confirmation.length(), 0);
|
||||||
std::cout << "Password set for client " << fd << ": " << nick << std::endl;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Extract the nickname from the command
|
else
|
||||||
// std::string nickname = command.substr(8); // Assuming "/setnick " is 9 characters long
|
{
|
||||||
|
setNickname(fd, nick);
|
||||||
|
for (size_t i = 0; i < _clients.size(); ++i)
|
||||||
|
{
|
||||||
|
if (_clients[i].getFd() == fd)
|
||||||
|
{
|
||||||
|
_clients[i].setNick(nick);
|
||||||
|
std::cout << "Password set for client " << fd << ": " << nick << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::string confirmation = "Please Enter Your Username : \n";
|
||||||
|
send(fd, confirmation.c_str(), confirmation.length(), 0);
|
||||||
|
|
||||||
// Handle setting the nickname for the client's connection
|
a = 2;
|
||||||
|
|
||||||
|
}
|
||||||
// Send a response back to the client confirming the action
|
}
|
||||||
std::string confirmation = "Please Enter Your Username : \n";
|
|
||||||
send(fd, confirmation.c_str(), confirmation.length(), 0);
|
else if (startsWith(command, "user") && a == 2)
|
||||||
|
{
|
||||||
a = 2;
|
|
||||||
} 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;
|
||||||
iss >> cmd >> username >> dontworry >> dontworry1 >> realname;
|
iss >> cmd >> username >> dontworry >> dontworry1 >> realname;
|
||||||
// Remove leading and trailing whitespace from parameters
|
|
||||||
username = trim(username);
|
username = trim(username);
|
||||||
dontworry = trim(dontworry);
|
dontworry = trim(dontworry);
|
||||||
dontworry1 = trim(dontworry1);
|
dontworry1 = trim(dontworry1);
|
||||||
realname = trim(realname);
|
realname = trim(realname);
|
||||||
|
|
||||||
setUsernames(fd, username);
|
if (dontputthesameusername(username) == true)
|
||||||
|
{
|
||||||
for (size_t i = 0; i < _clients.size(); ++i) {
|
std::string confirmation = "Please Use a Different Nickname : \n";
|
||||||
if (_clients[i].getFd() == fd) {
|
send(fd, confirmation.c_str(), confirmation.length(), 0);
|
||||||
_clients[i].setUser(username);
|
|
||||||
_clients[i].setName(realname);
|
|
||||||
nickname = _clients[i].getNick();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
a = 0;
|
else
|
||||||
|
{
|
||||||
|
setUsernames(fd, username);
|
||||||
|
|
||||||
std::string one = ":irc.l9oroch 001 " + nickname + " :Welcome to the TopG Network, " + nickname + '\n';
|
for (size_t i = 0; i < _clients.size(); ++i)
|
||||||
std::string two = ":irc.l9oroch 002 " + nickname + " :Your host is TopG, running version 4.5" + '\n';
|
{
|
||||||
std::string tre = ":irc.l9oroch 003 " + nickname + " :This server was created " + formatCreationTime() + '\n';
|
if (_clients[i].getFd() == fd)
|
||||||
std::string foor = ":irc.l9oroch 004 " + nickname + " TopG TopG(enterprise)-2.3(12)-netty(5.4c)-proxy(0.9) TopG TopG TopG" + '\n';
|
{
|
||||||
send(fd, one.c_str(), one.length(), 0);
|
_clients[i].setUser(username);
|
||||||
send(fd, two.c_str(), two.length(), 0);
|
_clients[i].setName(realname);
|
||||||
send(fd, tre.c_str(), tre.length(), 0);
|
nickname = _clients[i].getNick();
|
||||||
send(fd, foor.c_str(), foor.length(), 0);
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a = 0;
|
||||||
|
|
||||||
|
std::string one = ":irc.l9oroch 001 " + nickname + " :Welcome to the TopG Network, " + nickname + '\n';
|
||||||
|
std::string two = ":irc.l9oroch 002 " + nickname + " :Your host is TopG, running version 4.5" + '\n';
|
||||||
|
std::string tre = ":irc.l9oroch 003 " + nickname + " :This server was created " + formatCreationTime() + '\n';
|
||||||
|
std::string foor = ":irc.l9oroch 004 " + nickname + " TopG TopG(enterprise)-2.3(12)-netty(5.4c)-proxy(0.9) TopG TopG TopG" + '\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);
|
||||||
|
send(fd, foor.c_str(), foor.length(), 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (startsWith(command, "JOIN ")) {
|
||||||
|
|
||||||
|
|
||||||
// if (privilege_level == "operator" )
|
|
||||||
// {
|
|
||||||
// //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');
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
// Process other commands or messages
|
|
||||||
// processCommand(fd, command);
|
|
||||||
} else if (startsWith(command, "JOIN ")) {
|
|
||||||
std::string nick;
|
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) {
|
||||||
@@ -807,8 +692,9 @@ void Server::handleClientData(int fd) {
|
|||||||
createChannel(channelName, nick, fd);
|
createChannel(channelName, nick, fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (startsWith(command, "PRIVMSG ")) {
|
}
|
||||||
// Extract the recipient and the message from the command
|
else if (startsWith(command, "PRIVMSG "))
|
||||||
|
{
|
||||||
std::istringstream iss(command);
|
std::istringstream iss(command);
|
||||||
std::string cmd, recipient, message;
|
std::string cmd, recipient, message;
|
||||||
std::string niiick;
|
std::string niiick;
|
||||||
@@ -822,12 +708,14 @@ void Server::handleClientData(int fd) {
|
|||||||
|
|
||||||
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;
|
||||||
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)
|
||||||
|
{
|
||||||
niiick = _clients[i].getNick();
|
niiick = _clients[i].getNick();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
broadcastMessage(recipient, niiick, message, fd);
|
broadcastMessage(recipient, niiick, message, fd);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -837,8 +725,10 @@ 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 ")) {
|
|
||||||
// Extract the channel name and user to be kicked
|
|
||||||
|
else if (startsWith(command, "KICK "))
|
||||||
|
{
|
||||||
std::string channelName, userToKick, reason;
|
std::string channelName, userToKick, reason;
|
||||||
std::istringstream iss(command.substr(6));
|
std::istringstream iss(command.substr(6));
|
||||||
iss >> channelName >> userToKick;
|
iss >> channelName >> userToKick;
|
||||||
@@ -847,30 +737,34 @@ void Server::handleClientData(int fd) {
|
|||||||
userToKick = trim(userToKick);
|
userToKick = trim(userToKick);
|
||||||
reason = trim(reason);
|
reason = trim(reason);
|
||||||
|
|
||||||
// Check if the sender is an operator in the specified channel
|
if (channels.find(channelName) != channels.end() && channels[channelName].isOperator(fd))
|
||||||
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);
|
int userFd = channels[channelName].findUserFdForKickRegulars(userToKick);
|
||||||
if (userFd != -1) {
|
if (userFd != -1)
|
||||||
// Kick the user
|
{
|
||||||
// kickUser(userFd);
|
|
||||||
channels[channelName].ejectUserfromusers(userFd);
|
channels[channelName].ejectUserfromusers(userFd);
|
||||||
channels[channelName].ejectUserfromivited(userToKick);
|
channels[channelName].ejectUserfromivited(userToKick);
|
||||||
// isinveted = 0;
|
|
||||||
std::string kickMessage = ":" + channels[channelName].getNickname(fd) + " KICK #" + channelName + " " + userToKick + " :" + reason + "\n";
|
std::string kickMessage = ":" + channels[channelName].getNickname(fd) + " KICK #" + channelName + " " + userToKick + " :" + reason + "\n";
|
||||||
smallbroadcastMessagefortheckick(channels[channelName].getNickname(fd), channelName, userToKick, reason);
|
smallbroadcastMessagefortheckick(channels[channelName].getNickname(fd), channelName, userToKick, reason);
|
||||||
send(fd, kickMessage.c_str(), kickMessage.length(), 0);
|
send(fd, kickMessage.c_str(), kickMessage.length(), 0);
|
||||||
send(userFd , kickMessage.c_str(), kickMessage.length(), 0);
|
send(userFd , kickMessage.c_str(), kickMessage.length(), 0);
|
||||||
} else {
|
}
|
||||||
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error: the user : " + userToKick + " is not found or offline." + "\r\n";
|
else
|
||||||
|
{
|
||||||
|
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error: the user : " + userToKick + " is not found or offline." + "\r\n";
|
||||||
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error1: You are not authorized to execute this command " + userToKick + "\r\n";
|
else
|
||||||
send(fd, errorMessage.c_str(), errorMessage.size(), 0); }
|
{
|
||||||
|
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error1: You are not authorized to execute this command " + userToKick + "\r\n";
|
||||||
|
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (startsWith(command, "TOPIC ")){
|
|
||||||
|
|
||||||
|
else if (startsWith(command, "TOPIC "))
|
||||||
|
{
|
||||||
std::string channelName, topic;
|
std::string channelName, topic;
|
||||||
std::istringstream iss(command.substr(7));
|
std::istringstream iss(command.substr(7));
|
||||||
iss >> channelName;
|
iss >> channelName;
|
||||||
@@ -885,13 +779,16 @@ void Server::handleClientData(int fd) {
|
|||||||
std::string topicMessage = ":" + channels[channelName].getNickname(fd) + " TOPIC #" + channelName + " :" + topic + "\n";
|
std::string topicMessage = ":" + channels[channelName].getNickname(fd) + " TOPIC #" + channelName + " :" + topic + "\n";
|
||||||
send(fd, topicMessage.c_str(), topicMessage.size(), 0);
|
send(fd, topicMessage.c_str(), topicMessage.size(), 0);
|
||||||
smallbroadcastMessageforTopic(channels[channelName].getNickname(fd), channelName, topic );
|
smallbroadcastMessageforTopic(channels[channelName].getNickname(fd), channelName, topic );
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error2: You are not authorized to execute this command " + "\r\n";
|
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error2: You are not authorized to execute this command " + "\r\n";
|
||||||
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (startsWith(command, "INVITE "))
|
else if (startsWith(command, "INVITE "))
|
||||||
{
|
{
|
||||||
std::string channelName, nickname;
|
std::string channelName, nickname;
|
||||||
@@ -905,11 +802,15 @@ void Server::handleClientData(int fd) {
|
|||||||
channels[channelName].addClientinveted(nickname, fd);
|
channels[channelName].addClientinveted(nickname, fd);
|
||||||
std::cout << "this client is invited : " << nickname << std::endl;
|
std::cout << "this client is invited : " << nickname << std::endl;
|
||||||
handleInvitation(fd, nickname, channelName);
|
handleInvitation(fd, nickname, channelName);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error3: You are not authorized to execute this command " + "\r\n";
|
std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error3: You are not authorized to execute this command " + "\r\n";
|
||||||
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
else if (startsWith(command, "MODE "))
|
else if (startsWith(command, "MODE "))
|
||||||
{
|
{
|
||||||
std::string channelName, mode , nick;
|
std::string channelName, mode , nick;
|
||||||
|
10
Server.hpp
10
Server.hpp
@@ -68,6 +68,8 @@ class Server {
|
|||||||
bool isOperator(int fd);
|
bool isOperator(int fd);
|
||||||
void kickUser(int fd);
|
void kickUser(int fd);
|
||||||
int findUserFdforkickregulars(const std::string& username);
|
int findUserFdforkickregulars(const std::string& username);
|
||||||
|
bool dontputthesamenick(const std::string& nickname);
|
||||||
|
bool dontputthesameusername(const std::string& username);
|
||||||
// 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);
|
||||||
@@ -81,14 +83,6 @@ class Server {
|
|||||||
void handleClientData(int fd);
|
void handleClientData(int fd);
|
||||||
void clientCleanup(int fd);
|
void clientCleanup(int fd);
|
||||||
void closeFds();
|
void closeFds();
|
||||||
|
|
||||||
void debugPrintChannels() {
|
|
||||||
std::cout << "List of channels:" << std::endl;
|
|
||||||
std::map<std::string, Channel>::iterator it;
|
|
||||||
for (it = channels.begin(); it != channels.end(); ++it) {
|
|
||||||
std::cout << "- " << it->first << std::endl; // Print the channel name (it->first)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
177
channel.cpp
177
channel.cpp
@@ -0,0 +1,177 @@
|
|||||||
|
#include "channel.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
Channel::Channel(){}
|
||||||
|
|
||||||
|
Channel::Channel(const std::string& name) : Channelname(name), opperatorfd(false), issettop(false), isinveted(false) {}
|
||||||
|
|
||||||
|
Channel::~Channel() {}
|
||||||
|
|
||||||
|
|
||||||
|
void Channel::setoperator(int value)
|
||||||
|
{
|
||||||
|
opperatorfd = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::getoperator()
|
||||||
|
{
|
||||||
|
return opperatorfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::setbooltopic(bool value)
|
||||||
|
{
|
||||||
|
issettop = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::getbooltopic()
|
||||||
|
{
|
||||||
|
return issettop;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::setboolinvited(bool value)
|
||||||
|
{
|
||||||
|
isinveted = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::getboolinvited()
|
||||||
|
{
|
||||||
|
return isinveted;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::setTopic(const std::string& newTopic) {
|
||||||
|
topic = newTopic;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get topic function
|
||||||
|
std::string Channel::getTopic() const {
|
||||||
|
return topic;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::addClient(const std::string& client, int fd) {
|
||||||
|
userFdMap[client] = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::addClientinveted(const std::string& client, int fd) {
|
||||||
|
invitedUsers[client] = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add an operator to the channel
|
||||||
|
void Channel::addOperator(const std::string& operatorName, int fd) {
|
||||||
|
// Store the operator name and file descriptor in the map
|
||||||
|
operators[operatorName] = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Channel::getUserFd(const std::string& username) const {
|
||||||
|
std::map<std::string, int>::const_iterator it = userFdMap.find(username);
|
||||||
|
if (it != userFdMap.end()) {
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
return -1; // Return -1 if username not found
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all clients' usernames in the channel
|
||||||
|
std::vector<std::string> Channel::getClients() const {
|
||||||
|
std::vector<std::string> clients;
|
||||||
|
std::map<std::string, int>::const_iterator it;
|
||||||
|
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
|
||||||
|
clients.push_back(it->first);
|
||||||
|
}
|
||||||
|
return clients;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Channel::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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Channel::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
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::isInvited(std::string nickname) {
|
||||||
|
// Iterate through the map of operators
|
||||||
|
for (std::map<std::string, int>::iterator it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
|
||||||
|
// Check if the file descriptor matches
|
||||||
|
if (it->first == nickname) {
|
||||||
|
return true; // Found the file descriptor in the map
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false; // File descriptor not found in the map
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int Channel::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 Channel::ejectUserfromusers(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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::ejectUserfromivited(std::string nickname) {
|
||||||
|
// Iterate over the map to find the user with the given file descriptor
|
||||||
|
std::map<std::string, int>::iterator it;
|
||||||
|
for (it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
|
||||||
|
if (it->first == nickname) {
|
||||||
|
// Erase the user from the map
|
||||||
|
invitedUsers.erase(it);
|
||||||
|
std::cout << "the user earased " << std::endl;
|
||||||
|
return; // Exit the function after removing the user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Channel::getOperatorNickname(int fd) const {
|
||||||
|
std::map<std::string, int>::const_iterator it;
|
||||||
|
for (it = operators.begin(); it != operators.end(); ++it) {
|
||||||
|
if (it->second == fd) {
|
||||||
|
return it->first;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""; // Return empty string if operator not found
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::removeOperator(const std::string& operatorName )
|
||||||
|
{
|
||||||
|
// Iterate through the map to find the operator
|
||||||
|
std::map<std::string, int>::iterator it;
|
||||||
|
for (it = operators.begin(); it != operators.end(); ++it) {
|
||||||
|
if (it->first == operatorName) {
|
||||||
|
// Erase the operator from the map
|
||||||
|
operators.erase(it);
|
||||||
|
return; // Exit the function after removing the operator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
178
channel.hpp
178
channel.hpp
@@ -24,172 +24,58 @@ private:
|
|||||||
std::string Channelname;
|
std::string Channelname;
|
||||||
std::string topic;
|
std::string topic;
|
||||||
std::string key;
|
std::string key;
|
||||||
std::vector<std::string> users;
|
|
||||||
std::string pass;
|
std::string pass;
|
||||||
// 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::map<std::string, int> invitedUsers;
|
std::map<std::string, int> invitedUsers;
|
||||||
std::map<std::string, int> operators;
|
std::map<std::string, int> operators;
|
||||||
|
int opperatorfd;
|
||||||
|
bool issettop;
|
||||||
|
bool isinveted;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructors
|
|
||||||
Channel() {}
|
|
||||||
|
|
||||||
Channel(const std::string& name) : Channelname(name) {}
|
Channel();
|
||||||
|
Channel(const std::string& name);
|
||||||
|
~Channel();
|
||||||
|
void setTopic(const std::string& newTopic);
|
||||||
|
std::string getTopic() const;
|
||||||
|
|
||||||
// Destructor
|
//checks
|
||||||
~Channel() {}
|
void setoperator(int value);
|
||||||
|
bool getoperator();
|
||||||
|
|
||||||
|
//checks
|
||||||
|
void setbooltopic(bool value);
|
||||||
|
bool getbooltopic();
|
||||||
|
|
||||||
|
//checks
|
||||||
|
void setboolinvited(bool value);
|
||||||
|
bool getboolinvited();
|
||||||
|
|
||||||
|
|
||||||
|
void addClient(const std::string& client, int fd);
|
||||||
void setTopic(const std::string& newTopic) {
|
void addClientinveted(const std::string& client, int fd);
|
||||||
topic = newTopic;
|
void addOperator(const std::string& operatorName, int fd);
|
||||||
}
|
int getUserFd(const std::string& username) const;
|
||||||
|
std::vector<std::string> getClients() const;
|
||||||
|
std::string getNickname(int fd) const;
|
||||||
|
bool isOperator(int fd);
|
||||||
|
bool isInvited(std::string nickname);
|
||||||
|
int findUserFdForKickRegulars(const std::string& username);
|
||||||
|
void ejectUserfromusers(int fd);
|
||||||
|
void ejectUserfromivited(std::string nickname);
|
||||||
|
std::string getOperatorNickname(int fd) const;
|
||||||
|
void removeOperator(const std::string& operatorName );
|
||||||
|
|
||||||
void setPass(const std::string &Newpass)
|
void setPass(const std::string &Newpass)
|
||||||
{
|
{
|
||||||
pass = Newpass;
|
pass = Newpass;
|
||||||
}
|
}
|
||||||
// Get topic function
|
|
||||||
std::string getTopic() const {
|
|
||||||
return topic;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getPass()
|
std::string getPass()
|
||||||
{
|
{
|
||||||
return pass;
|
return pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addClient(const std::string& client, int fd) {
|
|
||||||
userFdMap[client] = fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addClientinveted(const std::string& client, int fd) {
|
|
||||||
invitedUsers[client] = fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add an operator to the channel
|
|
||||||
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 {
|
|
||||||
std::map<std::string, int>::const_iterator it = userFdMap.find(username);
|
|
||||||
if (it != userFdMap.end()) {
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
return -1; // Return -1 if username not found
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all clients' usernames in the channel
|
|
||||||
std::vector<std::string> getClients() const {
|
|
||||||
std::vector<std::string> clients;
|
|
||||||
std::map<std::string, int>::const_iterator it;
|
|
||||||
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
|
|
||||||
clients.push_back(it->first);
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isInvited(std::string nickname) {
|
|
||||||
// Iterate through the map of operators
|
|
||||||
for (std::map<std::string, int>::iterator it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
|
|
||||||
// Check if the file descriptor matches
|
|
||||||
if (it->first == nickname) {
|
|
||||||
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 ejectUserfromusers(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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ejectUserfromivited(std::string nickname) {
|
|
||||||
// Iterate over the map to find the user with the given file descriptor
|
|
||||||
std::map<std::string, int>::iterator it;
|
|
||||||
for (it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
|
|
||||||
if (it->first == nickname) {
|
|
||||||
// Erase the user from the map
|
|
||||||
invitedUsers.erase(it);
|
|
||||||
std::cout << "the user earased " << std::endl;
|
|
||||||
return; // Exit the function after removing the user
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getOperatorNickname(int fd) const {
|
|
||||||
std::map<std::string, int>::const_iterator it;
|
|
||||||
for (it = operators.begin(); it != operators.end(); ++it) {
|
|
||||||
if (it->second == fd) {
|
|
||||||
return it->first;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""; // Return empty string if operator not found
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeOperator(const std::string& operatorName )
|
|
||||||
{
|
|
||||||
// Iterate through the map to find the operator
|
|
||||||
std::map<std::string, int>::iterator it;
|
|
||||||
for (it = operators.begin(); it != operators.end(); ++it) {
|
|
||||||
if (it->first == operatorName) {
|
|
||||||
// Erase the operator from the map
|
|
||||||
operators.erase(it);
|
|
||||||
return; // Exit the function after removing the operator
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove an operator from the channel
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user