This commit is contained in:
2024-01-25 18:09:37 +01:00
parent 3953cb8d0a
commit 98e4c4e2c3
3 changed files with 63 additions and 10 deletions

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <logs.hpp>
#include <errorscode.hpp>
#include <cstdlib>
#include <unstd/string.hpp>
namespace irc
{
typedef std::set<unstd::SharedPtr<Client> >::iterator client_it;
@@ -32,6 +34,27 @@ namespace irc
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "JOIN", _name);
}
void Channel::ModOperator(unstd::SharedPtr<class Client> client, const std::string &clientname, bool mode)
{
client_it it;
for (it = _clients.begin(); it != _clients.end(); ++it)
{
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
_operators.insert(const_cast<unstd::SharedPtr<irc::Client>&>(*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<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "MODE", out);
}
}
bool Channel::removeClient(unstd::SharedPtr<Client> 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<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(RPL_CHANNELMODEIS, modes);
}
void Channel::changeMode(unstd::SharedPtr<class Client> 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<int>(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> client, const std::string& new_topic)