fixed david
This commit is contained in:
158
srcs/channel.cpp
158
srcs/channel.cpp
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */
|
||||
/* Updated: 2024/02/06 12:31:15 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -26,14 +26,14 @@ namespace irc
|
||||
|
||||
void Channel::addClient(unstd::SharedPtr<Client> client, bool op)
|
||||
{
|
||||
if(!_clients.insert(client).second)
|
||||
if (!_clients.insert(client).second)
|
||||
{
|
||||
client->sendCode(ERR_USERONCHANNEL, "You are already in the channel");
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
if(op)
|
||||
if (op)
|
||||
_operators.insert(client);
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "JOIN", _name);
|
||||
sendWho(client);
|
||||
}
|
||||
@@ -41,52 +41,52 @@ namespace irc
|
||||
void Channel::modOperator(unstd::SharedPtr<class Client> client, const std::string& clientname, bool mode)
|
||||
{
|
||||
client_it it;
|
||||
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)
|
||||
{
|
||||
if(isOp(*it))
|
||||
return;
|
||||
if (isOp(*it))
|
||||
return ;
|
||||
showModesModify(client, mode, 'o', clientname);
|
||||
_operators.insert(const_cast<unstd::SharedPtr<irc::Client>&>(*it));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(it == _clients.end())
|
||||
if (it == _clients.end())
|
||||
client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel");
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
|
||||
for(it = _operators.begin(); it != _operators.end(); ++it)
|
||||
for (it = _operators.begin(); it != _operators.end(); ++it)
|
||||
{
|
||||
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
|
||||
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
|
||||
{
|
||||
showModesModify(client, mode, 'o', clientname);
|
||||
_operators.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(it == _operators.end())
|
||||
if (it == _operators.end())
|
||||
client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel");
|
||||
}
|
||||
|
||||
bool Channel::removeClient(unstd::SharedPtr<Client> client, const std::string& reason, bool quit)
|
||||
{
|
||||
if(!_clients.erase(client))
|
||||
if (!_clients.erase(client))
|
||||
return (false);
|
||||
if(isOp(client))
|
||||
if (isOp(client))
|
||||
_operators.erase(client);
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
{
|
||||
if(!quit)
|
||||
if (!quit)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "PART", _name + ' ' + reason);
|
||||
else
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "QUIT", reason);
|
||||
}
|
||||
if(!quit)
|
||||
if (!quit)
|
||||
client->sendMsg(client->getNickName(), "PART", _name + ' ' + reason);
|
||||
return true;
|
||||
}
|
||||
@@ -95,9 +95,9 @@ namespace irc
|
||||
{
|
||||
std::string clientlist(":YipIRC " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :");
|
||||
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
{
|
||||
if(isOp(const_cast<unstd::SharedPtr<irc::Client>&>(*it)))
|
||||
if (isOp(const_cast<unstd::SharedPtr<irc::Client>&>(*it)))
|
||||
clientlist += '@';
|
||||
clientlist += const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() + ' ';
|
||||
}
|
||||
@@ -109,9 +109,9 @@ namespace irc
|
||||
void Channel::handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice) const
|
||||
{
|
||||
const std::string sender = client ? client->getNickName() : "";
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
{
|
||||
if(client == *it)
|
||||
if (client == *it)
|
||||
continue;
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(sender, (notice ? "NOTICE " : "PRIVMSG ") + _name, msg);
|
||||
}
|
||||
@@ -125,14 +125,14 @@ namespace irc
|
||||
|
||||
modes += (modevalue) ? '+' : '-';
|
||||
modes += flag;
|
||||
if(flag == 'k')
|
||||
if (flag == 'k')
|
||||
modes += " " + _password;
|
||||
if(flag == 'l')
|
||||
if (flag == 'l')
|
||||
modes += " " + unstd::toString(_channel_size);;
|
||||
if(flag == 'o')
|
||||
if (flag == 'o')
|
||||
modes += " " + op;
|
||||
out += getName();
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), out, modes);
|
||||
}
|
||||
|
||||
@@ -140,18 +140,18 @@ namespace irc
|
||||
{
|
||||
std::string modes = _name + " +";
|
||||
|
||||
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)
|
||||
return;
|
||||
if(modes.size() == _name.size() + 2)
|
||||
return;
|
||||
if (modes.size() <= 2)
|
||||
return ;
|
||||
if (modes.size() == _name.size() + 2)
|
||||
return ;
|
||||
client->sendCode(RPL_CHANNELMODEIS, modes);
|
||||
}
|
||||
|
||||
@@ -162,48 +162,48 @@ namespace irc
|
||||
unsigned long arg_nb = 3;
|
||||
int arg_index = 2;
|
||||
|
||||
if(flags.find_first_not_of("itkol+-") != std::string::npos || flags.find_last_of("+-") != 0)
|
||||
if (flags.find_first_not_of("itkol+-") != std::string::npos || flags.find_last_of("+-") != 0)
|
||||
{
|
||||
client->sendCode(ERR_UNKNOWNMODE, "MODE : Unknown mode");
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
for(std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
|
||||
for (std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
|
||||
{
|
||||
if(std::distance(flags.begin(), it) != static_cast<long>(flags.find_first_of(*it)))
|
||||
if (std::distance(flags.begin(), it) != static_cast<long>(flags.find_first_of(*it)))
|
||||
it = flags.erase(it) - 1;
|
||||
}
|
||||
if(flags.find('o') != std::string::npos)
|
||||
if (flags.find('o') != std::string::npos)
|
||||
arg_nb++;
|
||||
if(flags.find('k') != std::string::npos && modevalue)
|
||||
if (flags.find('k') != std::string::npos && modevalue)
|
||||
arg_nb++;
|
||||
if(flags.find('l') != std::string::npos && modevalue)
|
||||
if (flags.find('l') != std::string::npos && modevalue)
|
||||
arg_nb++;
|
||||
if(msg.getTokens().size() < arg_nb)
|
||||
if (msg.getTokens().size() < arg_nb)
|
||||
{
|
||||
client->sendCode(ERR_NEEDMOREPARAMS, "MODE : Need more params");
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
|
||||
for(std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
|
||||
for (std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
|
||||
{
|
||||
switch(*it)
|
||||
{
|
||||
case 'i':
|
||||
if(_invite_only == modevalue)
|
||||
if (_invite_only == modevalue)
|
||||
break;
|
||||
_invite_only = modevalue;
|
||||
showModesModify(client, modevalue, 'i');
|
||||
break;
|
||||
case 't':
|
||||
if(_topic_op_restrict == modevalue)
|
||||
if (_topic_op_restrict == modevalue)
|
||||
break;
|
||||
_topic_op_restrict = modevalue;
|
||||
showModesModify(client, modevalue, 't');
|
||||
break;
|
||||
case 'k':
|
||||
if(modevalue)
|
||||
if (modevalue)
|
||||
{
|
||||
if(msg.getArgs()[arg_index] == _password)
|
||||
if (msg.getArgs()[arg_index] == _password)
|
||||
{
|
||||
arg_index++;
|
||||
break;
|
||||
@@ -214,8 +214,8 @@ namespace irc
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_password.empty())
|
||||
return;
|
||||
if (_password.empty())
|
||||
return ;
|
||||
_password.clear();
|
||||
logs::report(log_message, "password removed on %s", _name.c_str());
|
||||
showModesModify(client, modevalue, 'k');
|
||||
@@ -225,29 +225,29 @@ namespace irc
|
||||
modOperator(client, msg.getArgs()[arg_index++], modevalue);
|
||||
break;
|
||||
case 'l':
|
||||
if(!modevalue && _channel_size == -1)
|
||||
return;
|
||||
if(!modevalue)
|
||||
if (!modevalue && _channel_size == -1)
|
||||
return ;
|
||||
if (!modevalue)
|
||||
{
|
||||
_channel_size = -1;
|
||||
showModesModify(client, modevalue, 'l');
|
||||
break;
|
||||
}
|
||||
if(modevalue)
|
||||
if (modevalue)
|
||||
{
|
||||
char* end;
|
||||
long tmp = std::strtol(msg.getArgs()[arg_index++].c_str(), &end, 10);
|
||||
if(errno == ERANGE || *end != 0 || tmp < 0)
|
||||
if (errno == ERANGE || *end != 0 || tmp < 0)
|
||||
{
|
||||
client->sendCode(ERR_UNKNOWNMODE, "MODE : Invalid channel size");
|
||||
logs::report(log_error, "invalid channel size");
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
if(tmp == _channel_size)
|
||||
return;
|
||||
if (tmp == _channel_size)
|
||||
return ;
|
||||
else
|
||||
{
|
||||
if(tmp == _channel_size)
|
||||
if (tmp == _channel_size)
|
||||
break;
|
||||
_channel_size = tmp;
|
||||
showModesModify(client, modevalue, 'l');
|
||||
@@ -260,9 +260,9 @@ namespace irc
|
||||
|
||||
bool Channel::hasClient(std::string client) const
|
||||
{
|
||||
for(client_const_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for (client_const_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
{
|
||||
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client)
|
||||
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -270,10 +270,10 @@ namespace irc
|
||||
|
||||
void Channel::setTopic(unstd::SharedPtr<Client> client, const std::string& new_topic)
|
||||
{
|
||||
if(_topic_op_restrict && !isOp(client))
|
||||
if (_topic_op_restrict && !isOp(client))
|
||||
{
|
||||
client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges");
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
_topic = new_topic;
|
||||
relayTopic(client, true);
|
||||
@@ -281,26 +281,26 @@ namespace irc
|
||||
|
||||
void Channel::relayTopic(unstd::SharedPtr<Client> client, bool broadcast)
|
||||
{
|
||||
if(!hasClient(client))
|
||||
return;
|
||||
if(_topic.empty())
|
||||
if (!hasClient(client))
|
||||
return ;
|
||||
if (_topic.empty())
|
||||
{
|
||||
if(!broadcast)
|
||||
if (!broadcast)
|
||||
return client->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set");
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++ it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++ it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set");
|
||||
}
|
||||
if(!broadcast)
|
||||
if (!broadcast)
|
||||
return client->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic);
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++ it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++ it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic);
|
||||
}
|
||||
|
||||
bool Channel::isOp(unstd::SharedPtr<Client> client) const
|
||||
{
|
||||
for(client_const_it it = _operators.begin(); it != _operators.end(); ++it)
|
||||
for (client_const_it it = _operators.begin(); it != _operators.end(); ++it)
|
||||
{
|
||||
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client->getNickName())
|
||||
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client->getNickName())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -308,24 +308,24 @@ namespace irc
|
||||
|
||||
bool Channel::kick(unstd::SharedPtr<Client> op, unstd::SharedPtr<Client> target, const std::string& reason)
|
||||
{
|
||||
if(!hasClient(op))
|
||||
if (!hasClient(op))
|
||||
{
|
||||
op->sendCode(ERR_NOTONCHANNEL, _name + " you're not on that channel");
|
||||
return false;
|
||||
}
|
||||
if(!hasClient(target))
|
||||
if (!hasClient(target))
|
||||
{
|
||||
op->sendCodeInChannel(ERR_USERNOTINCHANNEL, *this, "they aren't on that channel");
|
||||
return false;
|
||||
}
|
||||
if(!isOp(op))
|
||||
if (!isOp(op))
|
||||
{
|
||||
op->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *this, "you're not channel operator");
|
||||
return false;
|
||||
}
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(op->getNickName(), "KICK " + _name + ' ' + target->getNickName(), reason);
|
||||
if(isOp(target))
|
||||
if (isOp(target))
|
||||
_operators.erase(target);
|
||||
_clients.erase(target);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user