This commit is contained in:
mochaoui
2024-05-01 11:46:14 +01:00
parent 17288d3fe3
commit 5c8c145c5b
5 changed files with 72 additions and 158 deletions

View File

@@ -540,22 +540,17 @@ void Server::handleClientData(int fd)
std::cout << "Received data from client " << fd << ": " << command << std::endl; std::cout << "Received data from client " << fd << ": " << command << std::endl;
int auth = client.getAuthentication(); int auth = client.getAuthentication();
//******************* FROM THERE IM STARTING TOP GGG ************
if ((startsWith(command, "pass ") || startsWith(command, "PASS ")) && auth == 0) if ((startsWith(command, "pass ") || startsWith(command, "PASS ")) && auth == 0)
{ {
std::string passwordLine = command.substr(command.find(" ") + 1); std::string passwordLine = command.substr(command.find(" ") + 1);
passwordLine = trim(passwordLine); passwordLine = trim(passwordLine);
// Check if the password starts and ends with a quote
if (!passwordLine.empty() && ((passwordLine[0] == '"' && passwordLine.size() > 1 && passwordLine[passwordLine.size() - 1] == '"') || if (!passwordLine.empty() && ((passwordLine[0] == '"' && passwordLine.size() > 1 && passwordLine[passwordLine.size() - 1] == '"') ||
(passwordLine[0] == '\'' && passwordLine.size() > 1 && passwordLine[passwordLine.size() - 1] == '\''))) (passwordLine[0] == '\'' && passwordLine.size() > 1 && passwordLine[passwordLine.size() - 1] == '\'')))
{ {
// Remove the quotes
passwordLine = passwordLine.substr(1, passwordLine.size() - 2); passwordLine = passwordLine.substr(1, passwordLine.size() - 2);
} }
// Validate the password
if (passwordLine.empty()) if (passwordLine.empty())
{ {
std::string errorMessage = "Error: Password cannot be empty\n"; std::string errorMessage = "Error: Password cannot be empty\n";
@@ -597,9 +592,31 @@ void Server::handleClientData(int fd)
usernames.erase(userIt); usernames.erase(userIt);
} }
std::map<int, std::string>::iterator nickIt; std::map<int, std::string>::iterator nickIt;
userIt = nicknames.find(fd); nickIt = nicknames.find(fd);
if (userIt != nicknames.end()) { if (nickIt != nicknames.end()) {
nicknames.erase(userIt); nicknames.erase(nickIt);
}
for (std::map<std::string, Channel>::iterator it = channels.begin(); it != channels.end(); ++it) {
std::map<std::string, int> &usersfdmap = it->second.getUserFdMap();
for (std::map<std::string, int>::iterator it3 = usersfdmap.begin(); it3 != usersfdmap.end(); ++it3) {
if (it3->second == fd)
usersfdmap.erase(it3);
}
}
for (std::map<std::string, Channel>::iterator it1 = channels.begin(); it1 != channels.end(); ++it1) {
std::map<std::string, int> &invitedusrmap = it1->second.invitedUserss();
for (std::map<std::string, int>::iterator it3 = invitedusrmap.begin(); it3 != invitedusrmap.end(); ++it3) {
if (it3->second == fd)
invitedusrmap.erase(it3);
}
}
for (std::map<std::string, Channel>::iterator it2 = channels.begin(); it2 != channels.end(); ++it2) {
std::map<std::string, int> &operatorsmap = it2->second.getOperators();
for (std::map<std::string, int>::iterator it3 = operatorsmap.begin(); it3 != operatorsmap.end(); ++it3) {
if (it3->second == fd)
operatorsmap.erase(it3);
}
} }
clientCleanup(fd); clientCleanup(fd);
} }
@@ -1147,7 +1164,6 @@ void Server::handleClientData(int fd)
} }
} }
} }
//**************** STOOOOOOP HERE TOP G ...
client.clearCommand(); client.clearCommand();
} }
} }
@@ -1188,18 +1204,14 @@ Client& Server::getClientByFd(int fd) {
} }
int randomInRange(int min, int max) { int randomInRange(int min, int max) {
// Check for invalid range
if (min > max) { if (min > max) {
return -1; // Or throw an exception return -1;
} }
// Calculate range size (inclusive)
int range_size = max - min + 1; int range_size = max - min + 1;
// Generate random number between 0 and RAND_MAX (scaled by range size)
double random_double = (double)rand() / (RAND_MAX + 1.0) * range_size; double random_double = (double)rand() / (RAND_MAX + 1.0) * range_size;
// Convert to integer and shift by minimum value
return (int)random_double + min; return (int)random_double + min;
} }

View File

@@ -4,8 +4,8 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <unistd.h> // Include for the send function #include <unistd.h>
#include <cstring> // Include for the strlen function #include <cstring>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <csignal> #include <csignal>
@@ -37,10 +37,8 @@ class Server {
std::string _password; std::string _password;
std::vector<struct pollfd> _fds; std::vector<struct pollfd> _fds;
std::vector<Client> _clients; std::vector<Client> _clients;
// THAT'S THA DATA OF TOOOP GGG START FROM THERE .
std::map<int, std::string> nicknames; std::map<int, std::string> nicknames;
std::map<int, std::string> usernames; std::map<int, std::string> usernames;
std::map<std::string, Channel> channels; std::map<std::string, Channel> channels;
@@ -48,11 +46,8 @@ class Server {
public: public:
Server(); Server();
~Server(); ~Server();
// THAT'S MY FUNCTIONS START FROM THERE
void setNickname(int fd, const std::string& nickname); void setNickname(int fd, const std::string& nickname);
std::string getPassowrd() const; std::string getPassowrd() const;
void setPassword(const std::string& password);
void setUsernameoperators(int fd, const std::string& username);
void setUsernames(int fd, const std::string& username); void setUsernames(int fd, const std::string& username);
std::string formatCreationTime(); std::string formatCreationTime();
std::string constructCreationTimeMessage(const std::string& channelName); std::string constructCreationTimeMessage(const std::string& channelName);
@@ -67,13 +62,8 @@ class Server {
void smallbroadcastMessageforTopic(std::string nicknamesender, const std::string& channelname, std::string topic); void smallbroadcastMessageforTopic(std::string nicknamesender, const std::string& channelname, std::string topic);
void smallbroadcastMOOD(std::string nicknamesender, const std::string& channelname, std::string mode, std::string receiver); void smallbroadcastMOOD(std::string nicknamesender, const std::string& channelname, std::string mode, std::string receiver);
int findUserFd1(const std::string& username); int findUserFd1(const std::string& username);
std::string findUsernameforsending(int fd);
bool isOperator(int fd);
void kickUser(int fd);
int findUserFdforkickregulars(const std::string& username);
bool dontputthesamenick(const std::string& nickname); bool dontputthesamenick(const std::string& nickname);
bool dontputthesameusername(const std::string& username); bool dontputthesameusername(const std::string& username);
// 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);
void init(); void init();

View File

@@ -3,21 +3,11 @@
Channel::Channel(){} Channel::Channel(){}
Channel::Channel(const std::string& name) : Channelname(name), opperatorfd(false), issettop(false), isinveted(false) {} Channel::Channel(const std::string& name) : Channelname(name) {}
Channel::~Channel() {} Channel::~Channel() {}
void Channel::setoperator(int value)
{
opperatorfd = value;
}
int Channel::getoperator()
{
return opperatorfd;
}
void Channel::setlimitchannel(int value) void Channel::setlimitchannel(int value)
{ {
limite = value; limite = value;
@@ -28,31 +18,11 @@ int Channel::getlimitechannel()
return limite; return limite;
} }
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) { void Channel::setTopic(const std::string& newTopic) {
topic = newTopic; topic = newTopic;
} }
// Get topic function
std::string Channel::getTopic() const { std::string Channel::getTopic() const {
return topic; return topic;
@@ -66,9 +36,7 @@ void Channel::addClientinveted(const std::string& client, int fd) {
invitedUsers[client] = fd; invitedUsers[client] = fd;
} }
// Add an operator to the channel
void Channel::addOperator(const std::string& operatorName, int fd) { void Channel::addOperator(const std::string& operatorName, int fd) {
// Store the operator name and file descriptor in the map
operators[operatorName] = fd; operators[operatorName] = fd;
} }
@@ -77,21 +45,18 @@ int Channel::getUserFd(const std::string& username) const {
if (it != userFdMap.end()) { if (it != userFdMap.end()) {
return it->second; return it->second;
} }
return -1; // Return -1 if username not found return -1;
} }
bool Channel::isUserInChannel(const std::string& nickname) const { bool Channel::isUserInChannel(const std::string& nickname) const {
// Search for the nickname in the userMap
std::map<std::string, int>::const_iterator it = userFdMap.find(nickname); std::map<std::string, int>::const_iterator it = userFdMap.find(nickname);
// If the nickname is found and the user is connected, return true
if (it != userFdMap.end() && it->second) { if (it != userFdMap.end() && it->second) {
return true; return true;
} }
// Otherwise, return false
return false; return false;
} }
// Get all clients' usernames in the channel
std::vector<std::string> Channel::getClients() const { std::vector<std::string> Channel::getClients() const {
std::vector<std::string> clients; std::vector<std::string> clients;
std::map<std::string, int>::const_iterator it; std::map<std::string, int>::const_iterator it;
@@ -105,71 +70,63 @@ std::string Channel::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 "";
return ""; // Return an empty string if the file descriptor is not found
} }
bool Channel::isOperator(int fd) { 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) { for (std::map<std::string, int>::iterator it = operators.begin(); it != operators.end(); ++it) {
// Check if the file descriptor matches
if (it->second == fd) { if (it->second == fd) {
return true; // Found the file descriptor in the map return true;
} }
} }
return false; // File descriptor not found in the map return false;
} }
bool Channel::isInvited(std::string nickname) { 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) { for (std::map<std::string, int>::iterator it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
// Check if the file descriptor matches
if (it->first == nickname) { if (it->first == nickname) {
return true; // Found the file descriptor in the map return true;
} }
} }
return false; // File descriptor not found in the map return false;
} }
int Channel::findUserFdForKickRegulars(const std::string& username) { int Channel::findUserFdForKickRegulars(const std::string& username) {
// Iterate through the userFdMap to find the user
std::map<std::string, int>::iterator it; std::map<std::string, int>::iterator it;
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) { for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
if (it->first == username) { if (it->first == username) {
return it->second; // Return the file descriptor if the username matches return it->second;
} }
} }
return -1; // Return -1 if the user is not found return -1;
} }
void Channel::ejectUserfromusers(int fd) { 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; std::map<std::string, int>::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) {
// Erase the user from the map
userFdMap.erase(it); userFdMap.erase(it);
std::cout << "the user earased " << std::endl; std::cout << "the user earased " << std::endl;
return; // Exit the function after removing the user return;
} }
} }
} }
void Channel::ejectUserfromivited(std::string nickname) { 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; std::map<std::string, int>::iterator it;
for (it = invitedUsers.begin(); it != invitedUsers.end(); ++it) { for (it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
if (it->first == nickname) { if (it->first == nickname) {
// Erase the user from the map
invitedUsers.erase(it); invitedUsers.erase(it);
std::cout << "the user earased " << std::endl; std::cout << "the user earased " << std::endl;
return; // Exit the function after removing the user return;
} }
} }
} }
@@ -181,20 +138,39 @@ std::string Channel::getOperatorNickname(int fd) const {
return it->first; return it->first;
} }
} }
return ""; // Return empty string if operator not found return "";
} }
void Channel::removeOperator(const std::string& operatorName ) void Channel::removeOperator(const std::string& operatorName )
{ {
// Iterate through the map to find the operator
std::map<std::string, int>::iterator it; std::map<std::string, int>::iterator it;
for (it = operators.begin(); it != operators.end(); ++it) { for (it = operators.begin(); it != operators.end(); ++it) {
if (it->first == operatorName) { if (it->first == operatorName) {
// Erase the operator from the map
operators.erase(it); operators.erase(it);
return; // Exit the function after removing the operator return;
} }
} }
} }
std::map<std::string, int>& Channel::getUserFdMap() {
return userFdMap;
}
std::map<std::string, int>& Channel::invitedUserss() {
return invitedUsers;
}
std::map<std::string, int>& Channel::getOperators() {
return operators;
}
void Channel::setPass(const std::string &Newpass)
{
pass = Newpass;
}
std::string Channel::getPass()
{
return pass;
}

View File

@@ -26,12 +26,9 @@ private:
std::string key; std::string key;
std::string pass; std::string pass;
int limite; int limite;
std::map<std::string, int> userFdMap; // Mapping of usernames to file descriptors std::map<std::string, int> userFdMap;
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;
bool channelconnect; bool channelconnect;
public: public:
@@ -41,23 +38,9 @@ public:
~Channel(); ~Channel();
void setTopic(const std::string& newTopic); void setTopic(const std::string& newTopic);
std::string getTopic() const; std::string getTopic() const;
//checks
void setoperator(int value);
int getoperator();
//check
void setlimitchannel(int value); void setlimitchannel(int value);
int getlimitechannel(); int getlimitechannel();
//checks
void setbooltopic(bool value);
bool getbooltopic();
//checks
void setboolinvited(bool value);
bool getboolinvited();
void addClient(const std::string& client, int fd); void addClient(const std::string& client, int fd);
void addClientinveted(const std::string& client, int fd); void addClientinveted(const std::string& client, int fd);
@@ -75,15 +58,12 @@ public:
void removeOperator(const std::string& operatorName ); void removeOperator(const std::string& operatorName );
void setPass(const std::string &Newpass) void setPass(const std::string &Newpass);
{
pass = Newpass;
}
std::string getPass() std::string getPass();
{ std::map<std::string, int>& getUserFdMap();
return pass; std::map<std::string, int>& invitedUserss();
} std::map<std::string, int>& getOperators();
}; };

View File

@@ -1,44 +0,0 @@
remember the quote commande
create object for each instence : channel and client and server
client : fd , pass ,name , nick , user , and remember the lastchannel *
channel : _limite : it's limite of the users joined the channel
-> nameofchannel
->topic
->key
->hastopic haskey and creationtime : if created .
->victor of clients
->vicotr of invited users
->victor of operators
//tpic seter .
//tpic time .
this if for testing this how the commande behav : /connect liberachat
////remember this when you connect to irssi you need to prompte the client to put a password
//remmember the username have 4 parameter check rfc
after seting the data , password user , nick ---> responce 001, 002, 003, 004
and make sure the irssi client need all the responce to recognized you as a server be awake
rmreconns
window close