dfsdkjbsdf

This commit is contained in:
Kbz-8
2024-01-25 18:12:12 +01:00
parent 98e4c4e2c3
commit 01559e2797
6 changed files with 101 additions and 16 deletions

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */
/* Updated: 2024/01/25 17:59:52 by vvaas ### ########.fr */
/* Updated: 2024/01/25 18:11:58 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -45,13 +45,19 @@ namespace irc
void ModOperator(unstd::SharedPtr<class Client> client, const std::string &clientname, bool mode);
inline bool removeOperator(unstd::SharedPtr<Client> op) { return _operators.erase(op); }
void changeMode(unstd::SharedPtr<class Client> client, const Message& msg);
bool isOp(unstd::SharedPtr<Client> client);
bool isOp(unstd::SharedPtr<Client> client) const;
void handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice = false) const;
inline bool hasClient(unstd::SharedPtr<Client> client) const { return _clients.find(client) != _clients.end(); }
bool hasClient(std::string client) const;
inline bool isInviteOnly() const { return _invite_only; }
void setTopic(unstd::SharedPtr<Client> client, const std::string& new_topic);
void showModes(void) const;
bool kick(unstd::SharedPtr<Client> op, unstd::SharedPtr<Client> target, const std::string& reason);
~Channel();
private:

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */
/* Updated: 2024/01/25 16:39:18 by maldavid ### ########.fr */
/* Updated: 2024/01/25 17:21:25 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -62,6 +62,9 @@ namespace irc
void handlePing(unstd::SharedPtr<class Client> client, const class Message& msg);
void handleMode(unstd::SharedPtr<class Client> client, const class Message& msg);
class Channel* getChannelByName(const std::string& name);
unstd::SharedPtr<class Client> getClientByName(const std::string& name);
bool isUserKnown(const std::string& user) const;
bool isChannelKnown(const std::string& channel) const;

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 18:09:11 by vvaas ### ########.fr */
/* Updated: 2024/01/25 18:11:45 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -19,6 +19,7 @@
namespace irc
{
typedef std::set<unstd::SharedPtr<Client> >::iterator client_it;
typedef std::set<unstd::SharedPtr<Client> >::const_iterator client_const_it;
Channel::Channel(const std::string& name) : _name(name), _channel_size(-1) {}
@@ -138,6 +139,16 @@ namespace irc
showModes();
}
bool Channel::hasClient(std::string client) const
{
for(client_const_it it = _clients.begin(); it != _clients.end(); ++it)
{
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client)
return true;
}
return false;
}
void Channel::setTopic(unstd::SharedPtr<Client> client, const std::string& new_topic)
{
if(_topic_op_restrict && !isOp(client))
@@ -148,14 +159,38 @@ namespace irc
_topic = new_topic;
}
bool Channel::isOp(unstd::SharedPtr<Client> client)
bool Channel::isOp(unstd::SharedPtr<Client> client) const
{
for (client_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->getNickName())
return (true);
return true;
}
return (false);
return false;
}
bool Channel::kick(unstd::SharedPtr<Client> op, unstd::SharedPtr<Client> target, const std::string& reason)
{
if(!hasClient(op))
{
op->sendCode(ERR_NOTONCHANNEL, _name + " you're not on that channel");
return false;
}
if(!hasClient(target))
{
op->sendCode(ERR_USERNOTINCHANNEL, const_cast<std::string&>(target->getNickName()) + std::string(" " + _name) + " they aren't on that channel");
return false;
}
if(!isOp(op))
{
op->sendCode(ERR_CHANOPRIVSNEEDED, const_cast<std::string&>(_name) + " you're not channel operator");
return false;
}
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);
_clients.erase(target);
return true;
}
Channel::~Channel() {}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 11:38:34 by maldavid #+# #+# */
/* Updated: 2024/01/25 13:21:33 by maldavid ### ########.fr */
/* Updated: 2024/01/25 18:06:49 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -56,7 +56,7 @@ namespace irc
_command = tokens[0];
_tokens = tokens;
bool reason = false;
for(std::vector<std::string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
for(std::vector<std::string>::iterator it = tokens.begin() + 1; it < tokens.end(); ++it)
{
if((*it)[0] == ':')
reason = true;

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */
/* Updated: 2024/01/25 16:39:09 by maldavid ### ########.fr */
/* Updated: 2024/01/25 17:23:12 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -184,6 +184,26 @@ namespace irc
return false;
}
Channel* Server::getChannelByName(const std::string& name)
{
for(channel_it it = _channels.begin(); it < _channels.end(); ++it)
{
if(it->getName() == name)
return &*it;
}
return NULL;
}
unstd::SharedPtr<Client> Server::getClientByName(const std::string& name)
{
for(client_it it = _client.begin(); it < _client.end(); ++it)
{
if((*it)->getNickName() == name)
return *it;
}
return unstd::SharedPtr<Client>(NULL);
}
Server::~Server()
{
closeMainSocket();

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */
/* Updated: 2024/01/25 18:04:02 by vvaas ### ########.fr */
/* Updated: 2024/01/25 18:11:24 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -293,11 +293,13 @@ namespace irc
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
{
std::cout << "kick" << std::endl;
if(msg.getArgs().empty())
{
logs::report(log_error, "KICK, invalid command '%s'", msg.getRawMsg().c_str());
return;
}
std::cout << "kick" << std::endl;
typedef std::vector<std::string>::iterator name_it;
std::vector<std::string> channels = unstd::split(msg.getArgs()[0], ',');
@@ -308,18 +310,37 @@ namespace irc
client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters");
return;
}
std::cout << "kick" << std::endl;
for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel)
{
std::cout << *user << " " << *channel << std::endl;
if(!isUserKnown(*user))
{
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(*user) + " no such nick");
continue;
}
std::cout << *user << " " << *channel << std::endl;
if(!isChannelKnown(*channel))
{
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(*channel) + " no such channel");
client->sendCode(ERR_NOSUCHCHANNEL, const_cast<std::string&>(*channel) + " no such channel");
continue;
}
std::cout << *user << " " << *channel << std::endl;
Channel* channel_target = getChannelByName(*channel);
if(channel_target == NULL)
logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->c_str());
unstd::SharedPtr<Client> client_target = getClientByName(*user);
if(client_target.get() == NULL)
logs::report(log_fatal_error, "(KICK), cannot get client '%s' by name; panic !", user->c_str());
if(!channel_target->kick(client, client_target, msg.getReason()))
{
logs::report(log_error, "could not kick %s because why not", user->c_str());
continue;
}
client->printUserHeader();
std::cout << "kicked " << *user << " from " << *channel << std::endl;
}
}