oh merde...

This commit is contained in:
2024-01-25 02:44:47 +01:00
parent fd91bb5bde
commit 92847d7c19
4 changed files with 93 additions and 23 deletions

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */ /* 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> client); bool removeClient(unstd::SharedPtr<Client> client);
inline std::size_t getNumberOfClients() const { return _clients.size(); } inline std::size_t getNumberOfClients() const { return _clients.size(); }
inline int getChannelSize() const { return _channel_size; }
inline void addOperator(unstd::SharedPtr<Client> op) { _operators.insert(op); } inline void addOperator(unstd::SharedPtr<Client> op) { _operators.insert(op); }
inline bool removeOperator(unstd::SharedPtr<Client> op) { return _operators.erase(op); } 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);
void handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice = false) const; void handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice = false) const;
inline bool isInviteOnly() const { return _invite_only; } inline bool isInviteOnly() const { return _invite_only; }
@@ -59,6 +60,7 @@ namespace irc
const std::string _name; const std::string _name;
std::string _password; std::string _password;
std::string _topic; std::string _topic;
int _channel_size;
bool _invite_only; bool _invite_only;
bool _topic_op_restrict; bool _topic_op_restrict;
}; };

View File

@@ -6,19 +6,20 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ /* 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 <channel.hpp> #include <channel.hpp>
#include <logs.hpp> #include <logs.hpp>
#include <errorscode.hpp> #include <errorscode.hpp>
#include <iostream>
#include <cstdlib>
namespace irc namespace irc
{ {
typedef std::set<unstd::SharedPtr<Client> >::iterator client_it; typedef std::set<unstd::SharedPtr<Client> >::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> client, bool op) void Channel::addClient(unstd::SharedPtr<Client> client, bool op)
{ {
@@ -32,10 +33,14 @@ namespace irc
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); const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "JOIN", _name);
} }
bool Channel::removeClient(unstd::SharedPtr<Client> client) bool Channel::removeClient(unstd::SharedPtr<Client> client)
{ {
return _clients.erase(client); if (!_clients.erase(client))
return (false);
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "PART", _name);
client->sendMsg(client->getNickName(), "PART", _name);
return (true);
} }
void Channel::handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice) const void Channel::handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice) const
@@ -49,9 +54,41 @@ namespace irc
} }
} }
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 :)
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<int>(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> client, const std::string& new_topic) void Channel::setTopic(unstd::SharedPtr<Client> 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"); client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges");
return ; return ;
@@ -59,5 +96,14 @@ namespace irc
_topic = new_topic; _topic = new_topic;
} }
bool Channel::isOp(unstd::SharedPtr<Client> client)
{
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
{
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client->getNickName())
return (true);
}
return (false);
}
Channel::~Channel() {} Channel::~Channel() {}
} }

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ /* 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 */
/* */ /* */
/******************************************************************************/ /******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */ /* 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 <message.hpp> #include <message.hpp>
#include <errorscode.hpp> #include <errorscode.hpp>
#include <irc.hpp> #include <irc.hpp>
#include <cstring>
namespace irc namespace irc
{ {
@@ -76,8 +77,7 @@ namespace irc
if(msg.getTokens()[1] == _password) if(msg.getTokens()[1] == _password)
{ {
client->login(); client->login();
client->sendCode(RPL_WELCOME, "\n"); client->sendCode(RPL_WELCOME, "Welcome to yipiirc :)\n");
client->sendPlainText("Welcome to yipiirc :)\n");
} }
else else
{ {
@@ -90,7 +90,9 @@ namespace irc
{ {
(void)msg; (void)msg;
for(channel_it it = _channels.begin(); it != _channels.end(); ++it) for(channel_it it = _channels.begin(); it != _channels.end(); ++it)
{
it->removeClient(client); it->removeClient(client);
}
client->printUserHeader(); client->printUserHeader();
std::cout << "quit" << std::endl; std::cout << "quit" << std::endl;
} }
@@ -125,12 +127,12 @@ namespace irc
} }
if(chit == _channels.end()) 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; return;
} }
if(!chit->removeClient(client)) 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; return;
} }
client->printUserHeader(); client->printUserHeader();
@@ -141,8 +143,9 @@ namespace irc
void Server::handleJoin(unstd::SharedPtr<class Client> client, const Message& msg) void Server::handleJoin(unstd::SharedPtr<class Client> 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()); logs::report(log_error, "JOIN, invalid command '%s'", msg.getRawMsg().c_str());
return; return;
} }
@@ -161,13 +164,21 @@ namespace irc
if(it == _channels.end()) if(it == _channels.end())
{ {
_channels.push_back(Channel(msg.getTokens()[1])); _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()); logs::report(log_message, "channel '%s' has been created", msg.getTokens()[1].c_str());
return ;
} }
else if (msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword())
it->addClient(client, true); client->sendCode(ERR_BADCHANNELKEY, "Invalid password");
client->printUserHeader(); else if (it->getChannelSize() != -1 && it->getChannelSize() >= static_cast<int>(it->getNumberOfClients()))
std::cout << "joining new channel, " << msg.getTokens()[1] << std::endl; 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<class Client> client, const Message& msg) void Server::handlePrivMsg(unstd::SharedPtr<class Client> client, const Message& msg)
@@ -318,7 +329,18 @@ namespace irc
void Server::handleMode(unstd::SharedPtr<class Client> client, const Message& msg) void Server::handleMode(unstd::SharedPtr<class Client> client, const Message& msg)
{ {
(void)client; logs::report(log_message, "Mode requested");
(void)msg; 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);
} }
} }