fixing vavaas issues
This commit is contained in:
@@ -6,14 +6,13 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/21 09:17:47 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/30 17:07:07 by vvaas ### ########.fr */
|
||||
/* Updated: 2024/02/07 16:53:18 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
#include <logs.hpp>
|
||||
#include <ansi.hpp>
|
||||
#include <cstdarg>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <config.hpp>
|
||||
|
||||
@@ -35,10 +34,8 @@ namespace irc
|
||||
case log_message: std::cout << AnsiColor::blue << "[IRC serv] Message : " << AnsiColor::reset << buffer << std::endl; break;
|
||||
case log_warning: std::cout << AnsiColor::magenta << "[IRC serv] Warning : " << AnsiColor::reset << buffer << std::endl; break;
|
||||
case log_error: std::cerr << AnsiColor::red << "[IRC serv] Error : " << AnsiColor::reset << buffer << std::endl; break;
|
||||
case log_fatal_error:
|
||||
std::cerr << AnsiColor::red << "[IRC serv] Fatal Error : " << AnsiColor::reset << buffer << std::endl;
|
||||
std::exit(EXIT_FAILURE);
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */
|
||||
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
|
||||
/* Updated: 2024/02/07 16:50:24 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -38,14 +38,22 @@ int main(int ac, char** av)
|
||||
return 0;
|
||||
}
|
||||
if(av[1] == NULL || av[2] == NULL)
|
||||
irc::logs::report(irc::log_fatal_error, "invalid argv, argv[1] or argv[2] is NULL (wtf)");
|
||||
{
|
||||
irc::logs::report(irc::log_error, "invalid argv, argv[1] or argv[2] is NULL (wtf)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* end;
|
||||
int port = std::strtol(av[1], &end, 10);
|
||||
if(errno == ERANGE || *end != 0 || port <= 0 || port > 0xFFFF || std::strlen(av[1]) == 0)
|
||||
irc::logs::report(irc::log_fatal_error, "invalid port");
|
||||
{
|
||||
irc::logs::report(irc::log_error, "invalid port");
|
||||
return 1;
|
||||
}
|
||||
|
||||
irc::Server serv(port, av[2]);
|
||||
if(serv.hasFailedInit())
|
||||
return 1;
|
||||
serv_ptr = &serv;
|
||||
signal(SIGINT, signalsHandler);
|
||||
signal(SIGQUIT, signalsHandler);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */
|
||||
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
|
||||
/* Updated: 2024/02/07 16:52:35 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace irc
|
||||
|
||||
Server::Server(int port, const std::string& password) : _s_len(sizeof(_s_data)), _password(password), _port(port), _main_socket(NULL_SOCKET), _active(true)
|
||||
{
|
||||
_init_failed = false;
|
||||
time_t ltime = time(NULL);
|
||||
struct tm tstruct = *localtime(<ime);
|
||||
char buf[100];
|
||||
@@ -53,7 +54,11 @@ namespace irc
|
||||
_s_data.sin_port = htons(_port);
|
||||
_main_socket = socket(AF_INET, SOCK_STREAM, 0); // AF_INET == IPv4, SOCK_STREAM == TCP
|
||||
if(_main_socket < 0)
|
||||
logs::report(log_fatal_error, "socket error");
|
||||
{
|
||||
logs::report(log_error, "socket error");
|
||||
_init_failed = true;
|
||||
return;
|
||||
}
|
||||
logs::report(log_message, "socket succesfully started");
|
||||
}
|
||||
|
||||
@@ -63,14 +68,26 @@ namespace irc
|
||||
|
||||
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)");
|
||||
{
|
||||
logs::report(log_error, "setsockopt() error (tout a pete)");
|
||||
_init_failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
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_error, "bind error");
|
||||
_init_failed = true;
|
||||
return;
|
||||
}
|
||||
logs::report(log_message, "bind successful, starting listen loop");
|
||||
|
||||
if(listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS
|
||||
logs::report(log_fatal_error, "listen error");
|
||||
{
|
||||
logs::report(log_error, "listen error");
|
||||
_init_failed = true;
|
||||
return;
|
||||
}
|
||||
logs::report(log_message, "listen queue created successfully");
|
||||
|
||||
logs::report(log_message, "server is up and running");
|
||||
@@ -133,16 +150,25 @@ namespace irc
|
||||
|
||||
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 && _main_socket != NULL_SOCKET)
|
||||
logs::report(log_fatal_error, "select fd error");
|
||||
{
|
||||
logs::report(log_error, "select fd error");
|
||||
return;
|
||||
}
|
||||
|
||||
if(FD_ISSET(_main_socket, &_fd_set)) // ifit's a new connection
|
||||
{
|
||||
sockaddr_in cli_sock;
|
||||
fd = accept(_main_socket, reinterpret_cast<sockaddr*>(&cli_sock), &len); // adds the new connection
|
||||
if(fd < 0)
|
||||
logs::report(log_fatal_error, "accept() error");
|
||||
{
|
||||
logs::report(log_error, "accept() error");
|
||||
return;
|
||||
}
|
||||
if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
|
||||
logs::report(log_fatal_error, "fcntl() error");
|
||||
{
|
||||
logs::report(log_error, "fcntl() error");
|
||||
return;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */
|
||||
/* Updated: 2024/02/06 13:10:44 by vvaas ### ########.fr */
|
||||
/* Updated: 2024/02/07 16:45:33 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -146,7 +146,10 @@ namespace irc
|
||||
}
|
||||
Channel* channel = getChannelByName(msg.getArgs()[0]);
|
||||
if(channel == NULL)
|
||||
logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->getName().c_str());
|
||||
{
|
||||
logs::report(log_error, "(KICK), cannot get channel '%s' by name; panic !", channel->getName().c_str());
|
||||
return;
|
||||
}
|
||||
if(!channel->removeClient(client, (msg.getReason().empty() ? "" : msg.getReason())))
|
||||
{
|
||||
client->sendCode(ERR_NOTONCHANNEL, "PART : Not on channel");
|
||||
@@ -337,15 +340,21 @@ namespace irc
|
||||
|
||||
Channel* channel_target = getChannelByName(msg.getArgs()[1]);
|
||||
if(channel_target == NULL)
|
||||
logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
|
||||
{
|
||||
logs::report(log_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
unstd::SharedPtr<Client> client_target = getClientByName(msg.getArgs()[0]);
|
||||
if(client_target.get() == NULL)
|
||||
logs::report(log_fatal_error, "(INVITE), cannot get client '%s' by name; panic !", msg.getArgs()[0].c_str());
|
||||
{
|
||||
logs::report(log_error, "(INVITE), cannot get client '%s' by name; panic !", msg.getArgs()[0].c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if(!channel_target->hasClient(client))
|
||||
{
|
||||
logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
|
||||
logs::report(log_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -403,10 +412,16 @@ namespace irc
|
||||
|
||||
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());
|
||||
{
|
||||
logs::report(log_error, "(KICK), cannot get channel '%s' by name; panic !", channel->c_str());
|
||||
return;
|
||||
}
|
||||
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());
|
||||
{
|
||||
logs::report(log_error, "(KICK), cannot get client '%s' by name; panic !", user->c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if(!channel_target->kick(client, client_target, msg.getReason()))
|
||||
continue;
|
||||
@@ -454,7 +469,10 @@ namespace irc
|
||||
}
|
||||
Channel* channel = getChannelByName(msg.getArgs()[0]);
|
||||
if(channel == NULL)
|
||||
logs::report(log_fatal_error, "(TOPIC), cannot get channel '%s' by name; panic !", channel->getName().c_str());
|
||||
{
|
||||
logs::report(log_error, "(TOPIC), cannot get channel '%s' by name; panic !", channel->getName().c_str());
|
||||
return;
|
||||
}
|
||||
if(!channel->hasClient(client))
|
||||
{
|
||||
client->sendCode(ERR_NOTONCHANNEL, msg.getArgs()[0] + " you're not on that channel");
|
||||
|
||||
Reference in New Issue
Block a user