diff --git a/includes/channel.hpp b/includes/channel.hpp index a5b926c..0d7fdcf 100644 --- a/includes/channel.hpp +++ b/includes/channel.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */ -/* Updated: 2024/01/25 02:18:30 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 17:59:52 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -42,7 +42,7 @@ namespace irc inline std::size_t getNumberOfClients() const { return _clients.size(); } inline int getChannelSize() const { return _channel_size; } - inline void addOperator(unstd::SharedPtr op) { _operators.insert(op); } + void ModOperator(unstd::SharedPtr client, const std::string &clientname, bool mode); inline bool removeOperator(unstd::SharedPtr op) { return _operators.erase(op); } void changeMode(unstd::SharedPtr client, const Message& msg); bool isOp(unstd::SharedPtr client); @@ -51,7 +51,7 @@ namespace irc inline bool isInviteOnly() const { return _invite_only; } void setTopic(unstd::SharedPtr client, const std::string& new_topic); - + void showModes(void) const; ~Channel(); private: diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 844ab87..f1784af 100644 --- a/srcs/channel.cpp +++ b/srcs/channel.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ -/* Updated: 2024/01/25 12:46:20 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 18:09:11 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -14,6 +14,8 @@ #include #include #include +#include + namespace irc { typedef std::set >::iterator client_it; @@ -32,6 +34,27 @@ namespace irc for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(client->getNickName(), "JOIN", _name); } + void Channel::ModOperator(unstd::SharedPtr client, const std::string &clientname, bool mode) + { + client_it it; + for (it = _clients.begin(); it != _clients.end(); ++it) + { + if (const_cast&>(*it)->getNickName() == clientname) + _operators.insert(const_cast&>(*it)); + } + if (it == _clients.end()) + client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel"); + else + { + std::string out; + if (mode) + out = clientname + ": +o"; + else + out = clientname + ": -o"; + for (it = _clients.begin(); it != _clients.end(); ++it) + const_cast&>(*it)->sendMsg(client->getNickName(), "MODE", out); + } + } bool Channel::removeClient(unstd::SharedPtr client) { if (!_clients.erase(client)) @@ -53,6 +76,24 @@ namespace irc } } + void Channel::showModes(void) const + { + std::string modes = "+"; + + if (_invite_only) + modes += 'i'; + if (_topic_op_restrict) + modes += 't'; + if (_password.size() > 0) + modes += 'k'; + if (_channel_size != -1) + { + modes += 'l'; + modes += " " + unstd::toString(_channel_size); + } + for(client_it it = _clients.begin(); it != _clients.end(); ++it) + const_cast&>(*it)->sendCode(RPL_CHANNELMODEIS, modes); + } void Channel::changeMode(unstd::SharedPtr client, const Message& msg) { bool modevalue = (msg.getTokens()[2][0] != '-'); @@ -66,16 +107,26 @@ namespace irc _topic_op_restrict = modevalue; break; case 'k': - logs::report(log_message, "%s password set as %s", getName().c_str(), msg.getTokens()[3].c_str()); - _password = msg.getTokens()[3]; + if (modevalue && msg.getTokens().size() < 3) + { + logs::report(log_message, "%s password set as %s", getName().c_str(), msg.getTokens()[3].c_str()); + _password = msg.getTokens()[3]; + } + else if (msg.getTokens().size() < 2) + { + _password = ""; + logs::report(log_message, "password removed on %s", getName().c_str()); + } break; case 'o': - if (isOp(client)) - modevalue = !modevalue; // todo sa me clc :( + if (isOp(client) && modevalue) + ModOperator(client, msg.getTokens()[3], modevalue); else client->sendCode(ERR_CHANOPRIVSNEEDED, "You need to be operator to execute this command"); break; case 'l': + if (msg.getTokens().size() < 3 && modevalue) + return ; if (static_cast(getNumberOfClients()) > std::atoi(msg.getTokens()[3].c_str())) return ; if (modevalue) @@ -83,6 +134,8 @@ namespace irc if (!modevalue) _channel_size = -1; } + logs::report(log_message, "%s MODES : i:%d t:%d k:%s l:%d", getName().c_str(), _invite_only, _topic_op_restrict, _password.c_str(), _channel_size); + showModes(); } void Channel::setTopic(unstd::SharedPtr client, const std::string& new_topic) diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 6df3cdb..920f9bb 100644 --- a/srcs/server_functions.cpp +++ b/srcs/server_functions.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */ -/* Updated: 2024/01/25 17:03:18 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 18:04:02 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -357,7 +357,7 @@ namespace irc void Server::handleMode(unstd::SharedPtr client, const Message& msg) { logs::report(log_message, "Mode requested"); - if (msg.getTokens().size() != 3 || (msg.getTokens().size() != 4 && !std::strchr("koi", msg.getTokens()[2][1]))) + if (msg.getTokens().size() < 3) return ; logs::report(log_message, "Mode parsing ok"); channel_it it;