hon hon hon baguette

This commit is contained in:
Kbz-8
2024-01-25 17:04:42 +01:00
parent 92847d7c19
commit 3953cb8d0a
9 changed files with 138 additions and 22 deletions

View File

@@ -6,14 +6,13 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */
/* Updated: 2024/01/25 02:33:59 by vvaas ### ########.fr */
/* Updated: 2024/01/25 12:46:20 by maldavid ### ########.fr */
/* */
/******************************************************************************/
#include <channel.hpp>
#include <logs.hpp>
#include <errorscode.hpp>
#include <iostream>
#include <cstdlib>
namespace irc
{

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/22 13:02:56 by maldavid ### ########.fr */
/* Updated: 2024/01/25 13:21:33 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -55,6 +55,19 @@ namespace irc
return;
_command = tokens[0];
_tokens = tokens;
bool reason = false;
for(std::vector<std::string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
{
if((*it)[0] == ':')
reason = true;
if(!reason)
_args.push_back(*it);
else
{
_reason.append(*it);
_reason.push_back(' ');
}
}
}
Message::~Message() {}

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 00:09:16 by vvaas ### ########.fr */
/* Updated: 2024/01/25 16:39:09 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -22,6 +22,12 @@
namespace irc
{
typedef std::vector<unstd::SharedPtr<Client> >::iterator client_it;
typedef std::vector<unstd::SharedPtr<Client> >::const_iterator client_const_it;
typedef std::vector<Channel>::iterator channel_it;
typedef std::vector<Channel>::const_iterator channel_const_it;
Server::Server(int port, const std::string& password) : _s_len(sizeof(_s_data)), _password(password), _port(port), _active(true)
{
std::memset(&_s_data, 0, sizeof(sockaddr));
@@ -158,6 +164,26 @@ namespace irc
return true;
}
bool Server::isUserKnown(const std::string& user) const
{
for(client_const_it it = _client.begin(); it < _client.end(); ++it)
{
if(const_cast<unstd::SharedPtr<Client>&>(*it)->getNickName() == user)
return true;
}
return false;
}
bool Server::isChannelKnown(const std::string& channel) const
{
for(channel_const_it it = _channels.begin(); it < _channels.end(); ++it)
{
if(it->getName() == channel)
return true;
}
return false;
}
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 02:43:54 by vvaas ### ########.fr */
/* Updated: 2024/01/25 17:03:18 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -22,6 +22,7 @@
#include <errorscode.hpp>
#include <irc.hpp>
#include <cstring>
#include <unstd/string.hpp>
namespace irc
{
@@ -143,7 +144,7 @@ namespace irc
void Server::handleJoin(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getTokens().size() < 2)
if(msg.getArgs().empty())
{
client->sendCode(ERR_NEEDMOREPARAMS, "Need more params");
logs::report(log_error, "JOIN, invalid command '%s'", msg.getRawMsg().c_str());
@@ -183,7 +184,7 @@ namespace irc
void Server::handlePrivMsg(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getTokens().size() < 2)
if(msg.getArgs().empty())
{
client->sendCode(ERR_NORECIPIENT, "No recipient given\n");
return;
@@ -237,7 +238,7 @@ namespace irc
void Server::handleNotice(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getTokens().size() < 2)
if(msg.getArgs().empty())
{
logs::report(log_error, "NOTICE, invalid command '%s'", msg.getRawMsg().c_str());
return;
@@ -292,8 +293,34 @@ namespace irc
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
if(msg.getArgs().empty())
{
logs::report(log_error, "KICK, invalid command '%s'", msg.getRawMsg().c_str());
return;
}
typedef std::vector<std::string>::iterator name_it;
std::vector<std::string> channels = unstd::split(msg.getArgs()[0], ',');
std::vector<std::string> users = unstd::split(msg.getArgs()[1], ',');
if(users.size() != channels.size())
{
client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters");
return;
}
for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel)
{
if(!isUserKnown(*user))
{
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(*user) + " no such nick");
continue;
}
if(!isChannelKnown(*channel))
{
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(*channel) + " no such channel");
continue;
}
}
}
void Server::handleTopic(unstd::SharedPtr<class Client> client, const Message& msg)

39
srcs/unstd/string.cpp Normal file
View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* string.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/25 15:19:30 by maldavid #+# #+# */
/* Updated: 2024/01/25 15:20:20 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <unstd/string.hpp>
namespace unstd
{
std::vector<std::string> split(const std::string& s, char delim)
{
std::vector<std::string> elems;
std::string token;
for(std::string::const_iterator it = s.begin(); it != s.end();)
{
if(*it == delim)
{
elems.push_back(token);
token.clear();
while(*it == delim && it != s.end())
it++;
}
else
{
token.push_back(*it);
it++;
}
}
elems.push_back(token);
return elems;
}
}