From 11789fe6339e2228591dfa1516ab4f1143f7e0fd Mon Sep 17 00:00:00 2001 From: Mmokane Date: Sun, 21 Apr 2024 20:55:31 +0100 Subject: [PATCH] the password mode is working, and the invite mode bug is fixed --- .vscode/settings.json | 56 ---------------------------------------- Server.cpp | 59 ++++++++++++++++++++++++++++++++++++++----- channel.hpp | 10 ++++++++ 3 files changed, 63 insertions(+), 62 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ca665d3..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "files.associations": { - "ostream": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "csignal": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "initializer_list": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "numbers": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "cinttypes": "cpp", - "typeinfo": "cpp", - "map": "cpp", - "sstream": "cpp", - "ctime": "cpp", - "iomanip": "cpp" - } -} \ No newline at end of file diff --git a/Server.cpp b/Server.cpp index 0d19cce..c94b7aa 100644 --- a/Server.cpp +++ b/Server.cpp @@ -3,6 +3,7 @@ int a = 0; int opperatorfd = 0; int issettop = 0; int isinveted = 0; +int itHasPass = 0; bool Server::_signal = false; @@ -759,9 +760,14 @@ void Server::handleClientData(int fd) { break; } } - - std::string channelName = command.substr(6); + std::string channelName, pass ; + // std::string channelName = command.substr(6); + std::istringstream iss(command.substr(5)); + iss >> channelName ; + channelName = channelName.substr(1); channelName = trim(channelName); + std::getline(iss, pass); + pass = trim(pass); // Check if the channel already exists std::map::iterator it = channels.find(channelName); @@ -774,15 +780,27 @@ void Server::handleClientData(int fd) { } else if (isinveted == 0){ std::cout << "ha huwa dkhaal l ******** ltania ***********" << std::endl; - createChannel(channelName, nick, fd); + std::cout << "this is the geted pass " << channels[channelName].getPass() << std::endl; + std::cout << "IM PASS " << pass << std::endl; + std::cout << "PASS MODE IS ON" << std::endl; + if (itHasPass == 1 && channels[channelName].getPass() == pass) + createChannel(channelName, nick, fd); + else if (itHasPass == 0) + createChannel(channelName, nick, fd); + else if (itHasPass == 1 && channels[channelName].getPass() != pass) + std::cout << "ZAPI " << std::endl; } + else { // User is not invited, send error message std::string errorMessage = ":server.host NOTICE " + nick + " :Error: you are not invited\r\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); } - } else { + + } + else + { std::cout << "ha huwa dkhal l ********* tania *********** " << std::endl; // Channel does not exist, create the channel @@ -992,9 +1010,38 @@ void Server::handleClientData(int fd) { send(fd, errorMessage.c_str(), errorMessage.size(), 0); } } - else if (mode == "-t") + else if (mode == "-k") { - + std::string ChanPass = (mode.substr(2)); + if (channels.find(channelName) != channels.end() && channels[channelName].isOperator(fd)){ + std::string modeChangeMessage = ":server.host MODE #" + channelName + " -k by " + channels[channelName].getNickname(fd) + "\n"; + send(fd, modeChangeMessage.c_str(), modeChangeMessage.size(), 0); + smallbroadcastMOOD(channels[channelName].getNickname(fd), channelName, mode, nick); + itHasPass = 0; + } + else + { + std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error7: You are not authorized to execute this command " + "\r\n"; + send(fd, errorMessage.c_str(), errorMessage.size(), 0); + } + } + else if (mode == "+k") + { + // std::string ChanPass = (mo.substr(2)); + nick = trim(nick); + std::cout << "HI IM TRIMMED PASS " << nick << std::endl; + channels[channelName].setPass(nick); + if (channels.find(channelName) != channels.end() && channels[channelName].isOperator(fd)){ + std::string modeChangeMessage = ":server.host MODE #" + channelName + " +k by " + channels[channelName].getNickname(fd) + "\n"; + send(fd, modeChangeMessage.c_str(), modeChangeMessage.size(), 0); + smallbroadcastMOOD(channels[channelName].getNickname(fd), channelName, mode, nick); + itHasPass = 1; + } + else + { + std::string errorMessage = ":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error8: You are not authorized to execute this command " + "\r\n"; + send(fd, errorMessage.c_str(), errorMessage.size(), 0); + } } } //**************** STOOOOOOP HERE TOP G ... diff --git a/channel.hpp b/channel.hpp index 75920c5..05e3075 100644 --- a/channel.hpp +++ b/channel.hpp @@ -25,6 +25,7 @@ private: std::string topic; std::string key; std::vector users; + std::string pass; // std::map nicknames; // Replace unordered_map with map std::map userFdMap; // Mapping of usernames to file descriptors std::map invitedUsers; @@ -45,12 +46,21 @@ public: topic = newTopic; } + void setPass(const std::string &Newpass) + { + pass = Newpass; + } // Get topic function std::string getTopic() const { return topic; } + std::string getPass() + { + return pass; + } + void addClient(const std::string& client, int fd) { userFdMap[client] = fd; }