diff --git a/includes/channel.hpp b/includes/channel.hpp index afb2633..a5b926c 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/24 15:34:26 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 02:18:30 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -41,10 +41,11 @@ namespace irc bool removeClient(unstd::SharedPtr client); 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); } inline bool removeOperator(unstd::SharedPtr op) { return _operators.erase(op); } - + void changeMode(unstd::SharedPtr client, const Message& msg); + bool isOp(unstd::SharedPtr client); void handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice = false) const; inline bool isInviteOnly() const { return _invite_only; } @@ -59,6 +60,7 @@ namespace irc const std::string _name; std::string _password; std::string _topic; + int _channel_size; bool _invite_only; bool _topic_op_restrict; }; diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 8ebd079..c529ad2 100644 --- a/srcs/channel.cpp +++ b/srcs/channel.cpp @@ -6,19 +6,20 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ -/* Updated: 2024/01/24 15:34:53 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 02:33:59 by vvaas ### ########.fr */ /* */ /******************************************************************************/ #include #include #include - +#include +#include namespace irc { typedef std::set >::iterator client_it; - Channel::Channel(const std::string& name) : _name(name) {} + Channel::Channel(const std::string& name) : _name(name), _channel_size(-1) {} void Channel::addClient(unstd::SharedPtr client, bool op) { @@ -32,10 +33,14 @@ namespace irc for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(client->getNickName(), "JOIN", _name); } - bool Channel::removeClient(unstd::SharedPtr client) { - return _clients.erase(client); + if (!_clients.erase(client)) + return (false); + for (client_it it = _clients.begin(); it != _clients.end(); ++it) + const_cast&>(*it)->sendMsg(client->getNickName(), "PART", _name); + client->sendMsg(client->getNickName(), "PART", _name); + return (true); } void Channel::handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice) const @@ -49,9 +54,41 @@ namespace irc } } + void Channel::changeMode(unstd::SharedPtr client, const Message& msg) + { + bool modevalue = (msg.getTokens()[2][0] != '-'); + // attention on est sur un truc solidement MERDIQUE, a toucher a tes propres risques (je suis proche du nervous breakdown) gl hf :) + switch (msg.getTokens()[2][1]) + { + case 'i': + _invite_only = modevalue; + break; + case 't': + _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]; + break; + case 'o': + if (isOp(client)) + modevalue = !modevalue; // todo sa me clc :( + else + client->sendCode(ERR_CHANOPRIVSNEEDED, "You need to be operator to execute this command"); + break; + case 'l': + if (static_cast(getNumberOfClients()) > std::atoi(msg.getTokens()[3].c_str())) + return ; + if (modevalue) + _channel_size = std::atoi(msg.getTokens()[3].c_str()); + if (!modevalue) + _channel_size = -1; + } + } + void Channel::setTopic(unstd::SharedPtr client, const std::string& new_topic) { - if(_topic_op_restrict && _operators.find(client) == _operators.end() ) + if(_topic_op_restrict && !isOp(client)) { client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges"); return ; @@ -59,5 +96,14 @@ namespace irc _topic = new_topic; } + bool Channel::isOp(unstd::SharedPtr client) + { + for (client_it it = _clients.begin(); it != _clients.end(); ++it) + { + if (const_cast&>(*it)->getNickName() == client->getNickName()) + return (true); + } + return (false); + } Channel::~Channel() {} } diff --git a/srcs/server.cpp b/srcs/server.cpp index c89bff9..f5984b9 100644 --- a/srcs/server.cpp +++ b/srcs/server.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ -/* Updated: 2024/01/24 19:56:43 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 00:09:16 by vvaas ### ########.fr */ /* */ /******************************************************************************/ diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 87e659a..b303781 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/24 21:57:19 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 02:43:54 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -21,6 +21,7 @@ #include #include #include +#include namespace irc { @@ -76,8 +77,7 @@ namespace irc if(msg.getTokens()[1] == _password) { client->login(); - client->sendCode(RPL_WELCOME, "\n"); - client->sendPlainText("Welcome to yipiirc :)\n"); + client->sendCode(RPL_WELCOME, "Welcome to yipiirc :)\n"); } else { @@ -90,7 +90,9 @@ namespace irc { (void)msg; for(channel_it it = _channels.begin(); it != _channels.end(); ++it) + { it->removeClient(client); + } client->printUserHeader(); std::cout << "quit" << std::endl; } @@ -125,12 +127,12 @@ namespace irc } if(chit == _channels.end()) { - logs::report(log_error, "PART, channel not found '%s'", msg.getTokens()[1].c_str()); + client->sendCode(ERR_NOSUCHCHANNEL, "NO such channel"); return; } if(!chit->removeClient(client)) { - logs::report(log_error, "PART, client was not in channel '%s'", msg.getTokens()[1].c_str()); + client->sendCode(ERR_NOTONCHANNEL, "Not on channel"); return; } client->printUserHeader(); @@ -141,8 +143,9 @@ namespace irc void Server::handleJoin(unstd::SharedPtr client, const Message& msg) { - if(msg.getTokens().size() < 2 && msg.getTokens().size() > 3) + if(msg.getTokens().size() < 2) { + client->sendCode(ERR_NEEDMOREPARAMS, "Need more params"); logs::report(log_error, "JOIN, invalid command '%s'", msg.getRawMsg().c_str()); return; } @@ -161,13 +164,21 @@ namespace irc if(it == _channels.end()) { _channels.push_back(Channel(msg.getTokens()[1])); - _channels.back().addClient(client); + _channels.back().addClient(client, true); logs::report(log_message, "channel '%s' has been created", msg.getTokens()[1].c_str()); + return ; } - else - it->addClient(client, true); - client->printUserHeader(); - std::cout << "joining new channel, " << msg.getTokens()[1] << std::endl; + if (msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) + client->sendCode(ERR_BADCHANNELKEY, "Invalid password"); + else if (it->getChannelSize() != -1 && it->getChannelSize() >= static_cast(it->getNumberOfClients())) + client->sendCode(ERR_CHANNELISFULL, "Channel is full"); + else if (it->getPassword().size() == 0) + it->addClient(client); + else if (it->getPassword().size() > 0 && msg.getTokens()[2] == it->getPassword()) + it->addClient(client); + + // client->printUserHeader(); + // std::cout << "joining new channel, " << msg.getTokens()[1] << std::endl; } void Server::handlePrivMsg(unstd::SharedPtr client, const Message& msg) @@ -318,7 +329,18 @@ namespace irc void Server::handleMode(unstd::SharedPtr client, const Message& msg) { - (void)client; - (void)msg; + logs::report(log_message, "Mode requested"); + if (msg.getTokens().size() != 3 || (msg.getTokens().size() != 4 && !std::strchr("koi", msg.getTokens()[2][1]))) + return ; + logs::report(log_message, "Mode parsing ok"); + channel_it it; + for (it = _channels.begin(); it != _channels.end() && it->getName() != msg.getTokens()[1]; ++it); + if (it == _channels.end()) + client->sendCode(ERR_NOSUCHCHANNEL, "No such channel"); + if (!it->isOp(client)) + client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges to execute this command"); + if (msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+') + return ; + it->changeMode(client, msg); } }