AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH

This commit is contained in:
Kbz-8
2024-01-22 14:54:39 +01:00
parent 6728b47953
commit be35fc2616
12 changed files with 360 additions and 98 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */
/* Updated: 2024/01/21 10:36:49 by maldavid ### ########.fr */
/* Updated: 2024/01/22 14:29:55 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,7 @@
namespace irc
{
Channel::Channel()
Channel::Channel(const std::string& name) : _name(name)
{
}

View File

@@ -6,17 +6,39 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:35:52 by maldavid #+# #+# */
/* Updated: 2024/01/22 01:27:30 by vvaas ### ########.fr */
/* Updated: 2024/01/22 12:25:28 by maldavid ### ########.fr */
/* */
/******************************************************************************/
#include <client.hpp>
#include <ansi.hpp>
#include <iostream>
#include <logs.hpp>
namespace irc
{
Client::Client(int fd, sockaddr_in sock, int id) : _s_data(sock), _fd(fd), _id(id)
Client::Client(int fd, sockaddr_in sock, int id) : _s_data(sock), _fd(fd), _id(id) {}
void Client::printUserHeader() const
{
std::cout << AnsiColor::green << "[User " << _id;
if(!_realname.empty())
std::cout << " " << _realname;
if(!_username.empty())
std::cout << " {username " << _username << "}";
if(!_nickname.empty())
std::cout << " {nickname " << _nickname << "}";
std::cout << "] : " << AnsiColor::reset;
}
std::string Client::getNextMsg()
{
std::size_t finder = _msg_in_flight.find("\r\n");
if(finder == std::string::npos)
logs::report(log_fatal_error, "client %d [getNextMsg()] : cannot get any other message, panic !", _id);
std::string msg = _msg_in_flight.substr(0, finder);
_msg_in_flight = _msg_in_flight.substr(finder + 2);
return msg;
}
Client::~Client() {}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:17:47 by maldavid #+# #+# */
/* Updated: 2024/01/21 09:30:13 by maldavid ### ########.fr */
/* Updated: 2024/01/22 09:46:54 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,8 +15,7 @@
#include <cstdarg>
#include <cstdlib>
#include <cstdio>
#define BUFFER_SIZE 4096
#include <config.hpp>
namespace irc
{
@@ -24,11 +23,11 @@ namespace irc
{
void report(LogType type, std::string message, ...)
{
char buffer[BUFFER_SIZE];
char buffer[LOGS_BUFFER_SIZE];
va_list al;
va_start(al, message);
vsnprintf(buffer, BUFFER_SIZE, message.c_str(), al);
vsnprintf(buffer, LOGS_BUFFER_SIZE, message.c_str(), al);
va_end(al);
switch(type)

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */
/* Updated: 2024/01/21 16:14:07 by vvaas ### ########.fr */
/* Updated: 2024/01/22 09:30:16 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -33,5 +33,7 @@ int main(int ac, char** av)
irc::logs::report(irc::log_fatal_error, "invalid port");
irc::Server server(port, av[2]);
server.wait();
return 0;
}

View File

@@ -6,18 +6,55 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 11:38:34 by maldavid #+# #+# */
/* Updated: 2024/01/21 12:03:47 by maldavid ### ########.fr */
/* Updated: 2024/01/22 13:02:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <message.hpp>
#include <client.hpp>
#include <vector>
#include <cctype>
namespace irc
{
namespace details
{
void split(const std::string& s, std::vector<std::string>& elems)
{
std::string token;
for(std::string::const_iterator it = s.begin(); it != s.end();)
{
if(std::isspace(*it))
{
elems.push_back(token);
token.clear();
while(std::isspace(*it) && it != s.end())
it++;
}
else
{
token.push_back(*it);
it++;
}
}
elems.push_back(token);
}
std::vector<std::string> split(const std::string& s)
{
std::vector<std::string> elems;
split(s, elems);
return elems;
}
}
Message::Message(unstd::SharedPtr<class Client> client, const std::string& msg) : _raw_msg(msg), _client(client)
{
std::vector<std::string> tokens = details::split(msg);
if(tokens.empty())
return;
_command = tokens[0];
_tokens = tokens;
}
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/22 01:42:10 by vvaas ### ########.fr */
/* Updated: 2024/01/22 14:54:21 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -19,114 +19,251 @@
#include <stack>
#include <fcntl.h>
#include <ansi.hpp>
/** Commands to handle
* NICK
* USER
* QUIT
* PART
* JOIN
* PRIVMSG
* NOTICE
* KICK
* MOTD
* TOPIC
* PING
* MODE
*/
#include <config.hpp>
#include <message.hpp>
#include <algorithm>
namespace irc
{
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));
init_socket();
wait();
initSocket();
}
void Server::init_socket_data(void)
void Server::initSocketData()
{
_s_data.sin_family = AF_INET;
_s_data.sin_addr.s_addr = INADDR_ANY;
_s_data.sin_port = htons(_port);
_main_socket = socket(AF_INET, SOCK_STREAM, 0); // AF_INET == IPv4, SOCK_STREAM == TCP
if (_main_socket < 0)
if(_main_socket < 0)
logs::report(log_fatal_error, "socket error");
logs::report(log_message, "socket succesfully started");
}
void Server::init_socket(void)
void Server::initSocket()
{
int opt = 0;
init_socket_data();
if (setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends
initSocketData();
if(setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends
logs::report(log_fatal_error, "setsockopt() error (tout a pete)");
if (bind(_main_socket, (struct sockaddr *)&_s_data, sizeof(_s_data)) != 0) // Bind _main_socket to localhost
if(bind(_main_socket, reinterpret_cast<sockaddr*>(&_s_data), sizeof(_s_data)) != 0) // Bind _main_socket to localhost
logs::report(log_fatal_error, "bind error");
logs::report(log_message, "bind successful, starting listen loop");
if (listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS
if(listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS
logs::report(log_fatal_error, "listen error");
logs::report(log_message, "listen queue created successfully");
logs::report(log_message, "Server is running");
logs::report(log_message, "server is up and running");
}
void Server::handle_input(void)
void Server::handleInput()
{
char buffer[1024] = {0};
char buffer[INPUT_SIZE] = { 0 };
for (std::vector<Client>::iterator it = _client.begin(); it != _client.end(); it++)
for(std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
{
if (!FD_ISSET(it->get_fd(), &_fd_set))
continue ;
while (recv(it->get_fd(), buffer, 1024, 0) > 0) // read() but for socket fd
if(!FD_ISSET((*it)->getFD(), &_fd_set))
continue;
while(recv((*it)->getFD(), buffer, INPUT_SIZE, 0) > 0) // read() but for socket fd
{
std::cout << AnsiColor::cyan << "[User " << it->get_string_id() << "] : " << AnsiColor::reset << buffer << std::flush;
memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining
(*it)->newMsgInFlight(buffer);
while(handleMessage(*it));
std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining
}
if (recv(it->get_fd(), buffer, 1024, 0) == 0) // recv return 0 if an user disconnect
if(recv((*it)->getFD(), buffer, INPUT_SIZE, 0) == 0) // recv return 0 if an user disconnect
{
std::cout << AnsiColor::cyan << "[User " << it->get_string_id() << "] : " << AnsiColor::reset << "Disconnected" << std::endl;
it = _client.erase(it) - 1; // magic bitch
logs::report(log_message, "User %d disconnected", (*it)->getID());
it = _client.erase(it) - 1;
}
}
}
void Server::wait(void)
void Server::wait()
{
int tmp;
int fd = 0;
int i = 0;
socklen_t len = sizeof(sockaddr_in);
while (_active)
while(_active)
{
FD_ZERO(&_fd_set);
FD_SET(_main_socket, &_fd_set);
for (std::vector<Client>::iterator it = _client.begin(); it != _client.end(); it++)
FD_SET(it->get_fd(), &_fd_set);
for(std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
FD_SET((*it)->getFD(), &_fd_set);
tmp = select(MAX_USERS, &_fd_set, NULL, NULL, NULL); // SELECT blocks till a connection or message is received, and let only those in _fd_set
if (tmp < 0)
if(tmp < 0)
logs::report(log_fatal_error, "select fd error");
if (FD_ISSET(_main_socket, &_fd_set)) // if it's a new connection
if(FD_ISSET(_main_socket, &_fd_set)) // if it's a new connection
{
sockaddr_in cli_sock;
fd = accept(_main_socket, (sockaddr *)&cli_sock, &len); // adds the new connection
if (fd < 0)
if(fd < 0)
logs::report(log_fatal_error, "accept() error");
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
logs::report(log_fatal_error, "fcntl() error");
_client.push_back(Client(fd, cli_sock, i++)); // put the client into the vector used in handle_input
std::cout << AnsiColor::cyan << "[User " << _client.back().get_string_id() << "] : " << AnsiColor::reset << "Connected" << std::endl;
unstd::SharedPtr<Client> new_client(new Client(fd, cli_sock, i++));
_client.push_back(new_client); // put the client into the vector used in handle_input
logs::report(log_message, "User %d connected", _client.back()->getID());
}
handle_input();
handleInput();
}
}
bool Server::handleMessage(unstd::SharedPtr<Client> client)
{
if(client->getMsgInFlight().empty()) // if there are no commands just return
return false;
const Message msg(client, client->getNextMsg());
if(msg.getCmd() == "NICK")
handleNick(client, msg);
else if(msg.getCmd() == "USER")
handleUser(client, msg);
else if(msg.getCmd() == "QUIT")
handleQuit(client, msg);
else if(msg.getCmd() == "PART")
handlePart(client, msg);
else if(msg.getCmd() == "JOIN")
handleJoin(client, msg);
else if(msg.getCmd() == "PRIVMSG")
handlePrivMsg(client, msg);
else if(msg.getCmd() == "NOTICE")
handleNotice(client, msg);
else if(msg.getCmd() == "KICK")
handleKick(client, msg);
else if(msg.getCmd() == "MOTD")
handleMotD(client, msg);
else if(msg.getCmd() == "TOPIC")
handleTopic(client, msg);
else if(msg.getCmd() == "PING")
handlePing(client, msg);
else if(msg.getCmd() == "MODE")
handleMode(client, msg);
return true;
}
void Server::handleNick(unstd::SharedPtr<class Client> client, const Message& msg)
{
// TODO : handle nick collisions
if(msg.getTokens().size() != 2 && msg.getTokens().size() != 3)
{
logs::report(log_error, "NICK, invalid command '%s'", msg.getRawMsg().c_str());
return;
}
client->printUserHeader();
client->setNewNickName(msg.getTokens()[1]);
std::cout << "new nickname, " << client->getNickName() << std::endl;
}
void Server::handleUser(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getTokens().size() != 5)
{
logs::report(log_error, "USER, invalid command '%s'", msg.getRawMsg().c_str());
return;
}
client->printUserHeader();
client->setNewUserName(msg.getTokens()[1]);
std::cout << "new username, " << client->getUserName() << std::endl;
//client->printUserHeader();
//client->setNewRealName(msg.getTokens()[1]);
//std::cout << "new realname, " << client->getRealName() << std::endl;
}
void Server::handleQuit(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)msg;
client->printUserHeader();
std::cout << "quit" << std::endl;
}
void Server::handlePart(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
void Server::handleJoin(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getTokens().size() > 3)
{
logs::report(log_error, "JOIN, invalid command '%s'", msg.getRawMsg().c_str());
return;
}
std::vector<Channel>::const_iterator it;
for(it = _channels.begin(); it != _channels.end(); ++it)
{
if(msg.getTokens()[1] == it->getName())
break;
}
if(it == _channels.end())
_channels.push_back(Channel(msg.getTokens()[1]));
client->printUserHeader();
std::cout << "joining new channel, " << msg.getTokens()[1] << std::endl;
}
void Server::handlePrivMsg(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
void Server::handleNotice(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
void Server::handleMotD(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
void Server::handleTopic(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
void Server::handlePing(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
void Server::handleMode(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
(void)msg;
}
Server::~Server()
{
if (_main_socket > 0)
close(_main_socket);
}
}