hon hon hon baguette
This commit is contained in:
19
Makefile
19
Makefile
@@ -6,20 +6,21 @@
|
|||||||
# By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ #
|
# By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/08/09 15:08:49 by vavaas #+# #+# #
|
# Created: 2023/08/09 15:08:49 by vavaas #+# #+# #
|
||||||
# Updated: 2024/01/22 17:32:49 by maldavid ### ########.fr #
|
# Updated: 2024/01/25 15:22:06 by maldavid ### ########.fr #
|
||||||
# #
|
# #
|
||||||
#******************************************************************************#
|
#******************************************************************************#
|
||||||
|
|
||||||
NAME = ircserv
|
NAME = ircserv
|
||||||
|
|
||||||
SRCS = srcs/main.cpp \
|
SRCS = srcs/main.cpp \
|
||||||
srcs/logs.cpp \
|
srcs/logs.cpp \
|
||||||
srcs/server.cpp \
|
srcs/ansi.cpp \
|
||||||
srcs/server_functions.cpp \
|
srcs/server.cpp \
|
||||||
srcs/client.cpp \
|
srcs/client.cpp \
|
||||||
srcs/channel.cpp \
|
srcs/channel.cpp \
|
||||||
srcs/message.cpp \
|
srcs/message.cpp \
|
||||||
srcs/ansi.cpp \
|
srcs/unstd/string.cpp \
|
||||||
|
srcs/server_functions.cpp \
|
||||||
|
|
||||||
OBJ_DIR = objs
|
OBJ_DIR = objs
|
||||||
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
|
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/21 11:35:21 by maldavid #+# #+# */
|
/* Created: 2024/01/21 11:35:21 by maldavid #+# #+# */
|
||||||
/* Updated: 2024/01/22 11:51:25 by maldavid ### ########.fr */
|
/* Updated: 2024/01/25 13:09:57 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -27,14 +27,18 @@ namespace irc
|
|||||||
inline unstd::SharedPtr<class Client> getClient() const { return _client; }
|
inline unstd::SharedPtr<class Client> getClient() const { return _client; }
|
||||||
inline const std::string& getRawMsg() const { return _raw_msg; }
|
inline const std::string& getRawMsg() const { return _raw_msg; }
|
||||||
inline const std::string& getCmd() const { return _command; }
|
inline const std::string& getCmd() const { return _command; }
|
||||||
|
inline const std::vector<std::string>& getArgs() const { return _args; }
|
||||||
|
inline const std::string& getReason() const { return _reason; }
|
||||||
inline const std::vector<std::string>& getTokens() const { return _tokens; }
|
inline const std::vector<std::string>& getTokens() const { return _tokens; }
|
||||||
|
|
||||||
~Message();
|
~Message();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> _tokens;
|
std::vector<std::string> _tokens;
|
||||||
|
std::vector<std::string> _args;
|
||||||
const std::string _raw_msg;
|
const std::string _raw_msg;
|
||||||
std::string _command;
|
std::string _command;
|
||||||
|
std::string _reason;
|
||||||
unstd::SharedPtr<class Client> _client;
|
unstd::SharedPtr<class Client> _client;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */
|
/* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */
|
||||||
/* Updated: 2024/01/24 19:57:00 by maldavid ### ########.fr */
|
/* Updated: 2024/01/25 16:39:18 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
@@ -62,6 +62,9 @@ namespace irc
|
|||||||
void handlePing(unstd::SharedPtr<class Client> client, const class Message& msg);
|
void handlePing(unstd::SharedPtr<class Client> client, const class Message& msg);
|
||||||
void handleMode(unstd::SharedPtr<class Client> client, const class Message& msg);
|
void handleMode(unstd::SharedPtr<class Client> client, const class Message& msg);
|
||||||
|
|
||||||
|
bool isUserKnown(const std::string& user) const;
|
||||||
|
bool isChannelKnown(const std::string& channel) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<class Channel> _channels;
|
std::vector<class Channel> _channels;
|
||||||
std::vector<unstd::SharedPtr<class Client> > _client;
|
std::vector<unstd::SharedPtr<class Client> > _client;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/20 19:13:26 by maldavid #+# #+# */
|
/* Created: 2024/01/20 19:13:26 by maldavid #+# #+# */
|
||||||
/* Updated: 2024/01/20 19:14:55 by maldavid ### ########.fr */
|
/* Updated: 2024/01/25 15:20:00 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -14,6 +14,8 @@
|
|||||||
#define __UNSTD_STRING__
|
#define __UNSTD_STRING__
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace unstd
|
namespace unstd
|
||||||
{
|
{
|
||||||
@@ -24,6 +26,8 @@ namespace unstd
|
|||||||
ss << n;
|
ss << n;
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> split(const std::string& s, char delim);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,14 +6,13 @@
|
|||||||
/* 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/25 02:33:59 by vvaas ### ########.fr */
|
/* Updated: 2024/01/25 12:46:20 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#include <channel.hpp>
|
#include <channel.hpp>
|
||||||
#include <logs.hpp>
|
#include <logs.hpp>
|
||||||
#include <errorscode.hpp>
|
#include <errorscode.hpp>
|
||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
namespace irc
|
namespace irc
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/21 11:38:34 by maldavid #+# #+# */
|
/* 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;
|
return;
|
||||||
_command = tokens[0];
|
_command = tokens[0];
|
||||||
_tokens = tokens;
|
_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() {}
|
Message::~Message() {}
|
||||||
|
|||||||
@@ -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/25 00:09:16 by vvaas ### ########.fr */
|
/* Updated: 2024/01/25 16:39:09 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
@@ -22,6 +22,12 @@
|
|||||||
|
|
||||||
namespace irc
|
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)
|
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));
|
std::memset(&_s_data, 0, sizeof(sockaddr));
|
||||||
@@ -158,6 +164,26 @@ namespace irc
|
|||||||
return true;
|
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()
|
Server::~Server()
|
||||||
{
|
{
|
||||||
closeMainSocket();
|
closeMainSocket();
|
||||||
|
|||||||
@@ -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/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 <errorscode.hpp>
|
||||||
#include <irc.hpp>
|
#include <irc.hpp>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <unstd/string.hpp>
|
||||||
|
|
||||||
namespace irc
|
namespace irc
|
||||||
{
|
{
|
||||||
@@ -143,7 +144,7 @@ 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)
|
if(msg.getArgs().empty())
|
||||||
{
|
{
|
||||||
client->sendCode(ERR_NEEDMOREPARAMS, "Need more params");
|
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());
|
||||||
@@ -183,7 +184,7 @@ namespace irc
|
|||||||
|
|
||||||
void Server::handlePrivMsg(unstd::SharedPtr<class Client> client, const Message& msg)
|
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");
|
client->sendCode(ERR_NORECIPIENT, "No recipient given\n");
|
||||||
return;
|
return;
|
||||||
@@ -237,7 +238,7 @@ namespace irc
|
|||||||
|
|
||||||
void Server::handleNotice(unstd::SharedPtr<class Client> client, const Message& msg)
|
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());
|
logs::report(log_error, "NOTICE, invalid command '%s'", msg.getRawMsg().c_str());
|
||||||
return;
|
return;
|
||||||
@@ -292,8 +293,34 @@ namespace irc
|
|||||||
|
|
||||||
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
|
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||||
{
|
{
|
||||||
(void)client;
|
if(msg.getArgs().empty())
|
||||||
(void)msg;
|
{
|
||||||
|
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)
|
void Server::handleTopic(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||||
|
|||||||
39
srcs/unstd/string.cpp
Normal file
39
srcs/unstd/string.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user