je suis aigri
This commit is contained in:
117
srcs/channel.cpp
117
srcs/channel.cpp
@@ -6,12 +6,13 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/29 23:47:14 by vvaas ### ########.fr */
|
||||
/* Updated: 2024/01/30 17:33:40 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
#include <channel.hpp>
|
||||
#include <logs.hpp>
|
||||
#include <cerrno>
|
||||
#include <errorscode.hpp>
|
||||
#include <cstdlib>
|
||||
#include <unstd/string.hpp>
|
||||
@@ -37,38 +38,37 @@ namespace irc
|
||||
sendWho(client);
|
||||
}
|
||||
|
||||
void Channel::ModOperator(unstd::SharedPtr<class Client> client, const std::string &clientname, bool mode)
|
||||
void Channel::modOperator(unstd::SharedPtr<class Client> client, const std::string& clientname, bool mode)
|
||||
{
|
||||
client_it it;
|
||||
logs::report(log_message, "mode operator, mode = %d", mode);
|
||||
if (mode)
|
||||
if(mode)
|
||||
{
|
||||
for (it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for(it = _clients.begin(); it != _clients.end(); ++it)
|
||||
{
|
||||
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
|
||||
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
|
||||
{
|
||||
_operators.insert(const_cast<unstd::SharedPtr<irc::Client>&>(*it));
|
||||
break ;
|
||||
}
|
||||
}
|
||||
if (it == _clients.end())
|
||||
client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel");
|
||||
}
|
||||
else
|
||||
{
|
||||
for (it = _operators.begin(); it != _operators.end(); ++it)
|
||||
{
|
||||
logs::report(log_message, "nickname %s, clientname : %s", const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName().c_str(), clientname.c_str());
|
||||
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
|
||||
{
|
||||
logs::report(log_message, "found %s to erase", const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName().c_str());
|
||||
_operators.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (it == _operators.end())
|
||||
if(it == _clients.end())
|
||||
client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel");
|
||||
return;
|
||||
}
|
||||
|
||||
for(it = _operators.begin(); it != _operators.end(); ++it)
|
||||
{
|
||||
logs::report(log_message, "nickname %s, clientname : %s", const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName().c_str(), clientname.c_str());
|
||||
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
|
||||
{
|
||||
logs::report(log_message, "found %s to erase", const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName().c_str());
|
||||
_operators.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(it == _operators.end())
|
||||
client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel");
|
||||
}
|
||||
|
||||
bool Channel::removeClient(unstd::SharedPtr<Client> client)
|
||||
@@ -116,7 +116,7 @@ namespace irc
|
||||
std::string modes;
|
||||
std::string out = "MODE ";
|
||||
|
||||
for (std::vector<std::string>::iterator it = mode.begin() + 1; it != mode.end(); ++it)
|
||||
for(std::vector<std::string>::iterator it = mode.begin() + 1; it != mode.end(); ++it)
|
||||
{
|
||||
modes += *it;
|
||||
modes += " ";
|
||||
@@ -125,67 +125,76 @@ namespace irc
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), out, modes);
|
||||
}
|
||||
|
||||
void Channel::showModes(unstd::SharedPtr<Client> client)
|
||||
{
|
||||
std::string modes = " +";
|
||||
|
||||
if (_invite_only)
|
||||
if(_invite_only)
|
||||
modes += 'i';
|
||||
if (_topic_op_restrict)
|
||||
if(_topic_op_restrict)
|
||||
modes += 't';
|
||||
if (_password.size() > 0)
|
||||
if(_password.size() > 0)
|
||||
modes += 'k';
|
||||
if (_channel_size != -1)
|
||||
if(_channel_size != -1)
|
||||
modes += 'l';
|
||||
if (modes.size() <= 2)
|
||||
if(modes.size() <= 2)
|
||||
return ;
|
||||
client->sendCode(RPL_CHANNELMODEIS, modes);
|
||||
}
|
||||
|
||||
void Channel::changeMode(unstd::SharedPtr<class Client> 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 :)
|
||||
logs::report(log_message, "tokensize : %d, mode : %c, modevalue %d", msg.getTokens().size(), msg.getTokens()[2][1], modevalue);
|
||||
switch (msg.getTokens()[2][1])
|
||||
switch(msg.getTokens()[2][1])
|
||||
{
|
||||
case 'i':
|
||||
_invite_only = modevalue;
|
||||
break;
|
||||
case 't':
|
||||
_topic_op_restrict = modevalue;
|
||||
break;
|
||||
case 'i': _invite_only = modevalue; break;
|
||||
case 't': _topic_op_restrict = modevalue; break;
|
||||
case 'k':
|
||||
if (modevalue && msg.getTokens().size() == 4)
|
||||
{
|
||||
if(modevalue && msg.getTokens().size() == 4)
|
||||
{
|
||||
logs::report(log_message, "%s password set as %s", getName().c_str(), msg.getTokens()[3].c_str());
|
||||
_password = msg.getTokens()[3];
|
||||
logs::report(log_message, "%s password set as %s", _name.c_str(), msg.getArgs()[2].c_str());
|
||||
_password = msg.getArgs()[2];
|
||||
}
|
||||
else if (msg.getTokens().size() < 4)
|
||||
else if(msg.getTokens().size() < 4)
|
||||
{
|
||||
_password = "";
|
||||
logs::report(log_message, "password removed on %s", getName().c_str());
|
||||
_password.clear();
|
||||
logs::report(log_message, "password removed on %s", _name.c_str());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'o':
|
||||
if (isOp(client))
|
||||
ModOperator(client, msg.getTokens()[3], modevalue);
|
||||
else if (!isOp(client))
|
||||
{
|
||||
if(isOp(client))
|
||||
modOperator(client, msg.getTokens()[3], modevalue);
|
||||
else if(!isOp(client))
|
||||
client->sendCode(ERR_CHANOPRIVSNEEDED, "You need to be operator to execute this command");
|
||||
break;
|
||||
}
|
||||
case 'l':
|
||||
if (msg.getTokens().size() < 3 && modevalue)
|
||||
return ;
|
||||
if (!modevalue)
|
||||
{
|
||||
if(msg.getTokens().size() < 3 && modevalue)
|
||||
return;
|
||||
if(!modevalue)
|
||||
{
|
||||
_channel_size = -1;
|
||||
break ;
|
||||
break;
|
||||
}
|
||||
if (modevalue)
|
||||
_channel_size = std::atoi(msg.getTokens()[3].c_str());
|
||||
break ;
|
||||
default :
|
||||
client->sendCode(":yipirc " ERR_UNKNOWNMODE " @", getName().c_str(), "Unknown mode");
|
||||
return ;
|
||||
if(modevalue)
|
||||
{
|
||||
char* end;
|
||||
long tmp = std::strtol(msg.getArgs()[2].c_str(), &end, 10);
|
||||
if(errno == ERANGE || *end != 0 || tmp < 0)
|
||||
logs::report(log_error, "invalid channel size");
|
||||
else
|
||||
_channel_size = tmp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default : client->sendCode(":yipirc " ERR_UNKNOWNMODE " @", getName().c_str(), "Unknown mode"); return;
|
||||
}
|
||||
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);
|
||||
showModesModify(client, msg);
|
||||
|
||||
Reference in New Issue
Block a user