Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: ircserv does not raise Valgrind flags anymore #36

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:24:04 by gilmar #+# #+# */
/* Updated: 2024/05/30 22:54:51 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 12:09:15 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -49,6 +49,7 @@ class Channel
void join(Client* client);
void kick(Client* client);
void part(Client* client);
void quit(Client* client);
void invite(Client* client);
void broadcast(Client* sender, std::string target, std::string message);

Expand Down
17 changes: 10 additions & 7 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:19:06 by gilmar #+# #+# */
/* Updated: 2024/05/30 22:45:05 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 12:49:07 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,28 +38,31 @@ class Client //-> class for client
{
public:
Client();
~Client();

Client(const Client& other);

int get_fd() const;
bool get_is_logged() const;
bool get_is_operator() const;
bool get_is_authenticated() const;
std::string get_buffer() const;
std::string get_nickname() const;
std::string get_username() const;
std::string get_password() const;
std::string get_hostname() const;
std::vector<std::string> get_channels_invited() const;
std::string get_ip_address() const;
bool get_is_authenticated() const;
bool get_is_operator() const;
std::vector<std::string> get_channels_invited() const;

void set_fd(const int fd);
void set_is_logged(bool is_logged);
void set_ip_add(const std::string& ipadd);
void set_is_operator(bool is_operator);
void set_is_authenticated(bool is_authenticated);
void set_buffer(const std::string& buffer);
void set_nickname(const std::string& nickname);
void set_username(const std::string& username);
void set_password(const std::string& password);
void set_is_authenticated(bool is_authenticated);
void set_is_operator(bool is_operator);
void set_ip_add(const std::string& ipadd);

bool is_channel_invited(const std::string& channel);
void add_channel_invited(const std::string& channel);
Expand Down
23 changes: 14 additions & 9 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:23:47 by gilmar #+# #+# */
/* Updated: 2024/05/30 23:45:41 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 13:40:50 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -52,16 +52,16 @@ class Server
Server();
~Server();
// This contructor is to be used as a workaround for mockup tests
Server(std::string password, std::vector<Client> _clients,
std::vector<Channel> _channels);
Server(std::string password, std::vector<Client*> _clients,
std::vector<Channel*> _channels);

void init(const std::string& port, const std::string& password);

private:
int _port; //-> server port
int _server_fdsocket; //-> server socket file descriptor
std::string _password; //-> server password
std::vector<Client> _clients; //-> vector of clients
std::vector<Client*> _clients; //-> vector of clients
std::vector<struct pollfd> _fds; //-> vector of pollfd
struct sockaddr_in _server_addr; //-> server address
std::vector<Channel*> _channels; //-> vector of channels
Expand All @@ -87,6 +87,7 @@ class Server
void _handler_client_nickname(const std::string& nickname, const int fd);
void _handler_client_username(const std::string& username, const int fd);
void _handler_client_password(const std::string& password, const int fd);

void _handler_bot_marvin(const std::string& buffer, int fd);
void _handler_bot_time(const std::string& buffer, int fd);
void _handler_bot_whois(const std::string& buffer, int fd);
Expand Down Expand Up @@ -130,26 +131,30 @@ class Server
static const int _command_list_size = 16; //-> command list size
static const command_handler
_command_list[_command_list_size]; //-> command list

void _execute_command(const std::string buffer,
const int fd); //-> execute command
void _close_fds(); //-> close file descriptors

void _close_fds(); //-> close file descriptors
std::string _cleanse_buffer(
const std::string& buffer,
const std::string& chars_to_remove); //-> parse received buffer

std::vector<std::string> _split_buffer(
const std::string& buffer,
const std::string& delimiter); //-> split string

Client* _get_client(const int fd); //-> get client
Client* _get_client(const int fd); //-> get client
Client* _get_client(const std::string nickname); // -> get nickname
Channel* _get_channel(const std::string& channel_name); //-> get channel
void _add_channel(
Channel* channel); // -> add a new channel to server channels
bool _client_is_ready_to_login(const int fd);
Client* _get_client(const std::string nickname); // -> get nickname
Channel* _get_channel(const std::string& channel_name); //-> get channel

std::string toupper(const std::string& str);

void _remove_client_from_channels(const int fd);
void _remove_client_from_server(const int fd);
void _remove_client_fd(const int fd);
};

#endif // SERVER_HPP
63 changes: 39 additions & 24 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:26:17 by gilmar #+# #+# */
/* Updated: 2024/05/30 23:17:45 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 13:53:16 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,6 +30,9 @@ Channel::Channel()
_operator_clients = std::vector<Client*>();
}

/*
** ---------------------------- COPY CONSTRUCTOR ------------------------------
*/
Channel::Channel(std::string name)
{
_limit = -1;
Expand All @@ -45,18 +48,16 @@ Channel::Channel(std::string name)
}

/*
** -------------------------------- DESTRUCTOR --------------------------------
** ------------------------------- DESTRUCTOR ---------------------------------
*/

Channel::~Channel()
{
this->_clients.clear();
this->_operator_clients.clear();
return;
_clients.clear();
_operator_clients.clear();
}

/*
** --------------------------------- ACCESSOR ---------------------------------
** ------------------------------- ACCESSORS ----------------------------------
*/

/**
Expand Down Expand Up @@ -264,19 +265,19 @@ void Channel::remove_channel_operator(Client* client)
*/
void Channel::remove_channel_client(Client* client)
{
for (std::vector<Client*>::iterator it = this->_clients.begin();
it != this->_clients.end();)
{
if ((*it)->get_nickname() == client->get_nickname())
/* for (std::vector<Client*>::iterator it = this->_clients.begin();
it != this->_clients.end();
++it)
{
std::cout << (*it)->get_nickname() << std::endl;
it = this->_clients.erase(it);
break;
}
else
++it;
}
return;
if ((*it)->get_nickname() == client->get_nickname())
{
this->_clients.erase(it);
return;
}
} */
std::vector<Client*>::iterator it =
std::remove(_clients.begin(), _clients.end(), client);
_clients.erase(it, _clients.end());
}

/*
Expand Down Expand Up @@ -406,6 +407,20 @@ void Channel::part(Client* client)
remove_channel_client(client);
}

/**
* @brief Removes a client operator, if so, from the channel.
*
* This function removes the specified client from the channel by removing them
* as a channel operator. Since the command is QUIT, later the client will be
* removed from server by the end of function _handler_client_quit(). As per
* client object from server is the same client object from channel, there is no
* need to call remove_channel_client() here.
*
* @param client A pointer to the client operator , if so, to be removed from
* the channel.
*/
void Channel::quit(Client* client) { remove_channel_operator(client); }

/**
* @brief Broadcasts a message to all clients in the channel, excluding the
* sender.
Expand All @@ -421,23 +436,23 @@ void Channel::part(Client* client)
*/
void Channel::broadcast(Client* sender, std::string target, std::string message)
{
for (std::vector<Client*>::iterator it = this->_clients.begin();
it != this->_clients.end();
for (std::vector<Client*>::iterator it = this->_operator_clients.begin();
it != this->_operator_clients.end();
it++)
{
if (*it == sender)
continue;
(*it)->broadcast(sender, target, message);
return;
}
for (std::vector<Client*>::iterator it = this->_operator_clients.begin();
it != this->_operator_clients.end();
for (std::vector<Client*>::iterator it = this->_clients.begin();
it != this->_clients.end();
it++)
{
if (*it == sender)
continue;
(*it)->broadcast(sender, target, message);
}
return;
}

/**
Expand Down
82 changes: 50 additions & 32 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:20:02 by gilmar #+# #+# */
/* Updated: 2024/05/30 23:17:50 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 12:48:20 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,7 +31,29 @@ Client::Client()
}

/*
** --------------------------------- ACCESSOR ---------------------------------
** ---------------------------- COPY CONSTRUCTOR ------------------------------
*/
Client::Client(const Client& other)
{
_fd = other._fd;
_is_logged = other._is_logged;
_is_authenticated = other._is_authenticated;
_is_operator = other._is_operator;
_buffer = other._buffer;
_ip_addr = other._ip_addr;
_nickname = other._nickname;
_username = other._username;
_password = other._password;
_channels_invited = other._channels_invited;
}

/*
** ------------------------------- DESTRUCTOR ---------------------------------
*/
Client::~Client() {}

/*
** ------------------------------- ACCESSORS ----------------------------------
*/

/**
Expand Down Expand Up @@ -206,33 +228,23 @@ std::vector<std::string> Client::get_channels_invited() const
return _channels_invited;
}

/*
** ---------------------------- MEMBER FUNCTIONS ------------------------------
*/

/**
* @brief Removes a channel from the list of invited channels.
*
* This function removes the specified channel from the list of channels
* that the client has been invited to join.
*
* @param channel The name of the channel to remove.
*/
/**
* @brief Removes a channel from the list of invited channels.
* @brief Checks if the client is invited to a specific channel.
*
* This function removes the specified channel from the list of channels
* that the client has been invited to join.
* This function checks if the client is invited to the specified channel.
*
* @param channel The name of the channel to be removed.
* @param channel The name of the channel to check.
* @return True if the client is invited to the channel, false otherwise.
*/
void Client::remove_channel_invited(const std::string& channel)
bool Client::is_channel_invited(const std::string& channel)
{
std::vector<std::string>::iterator it;
for (it = _channels_invited.begin(); it != _channels_invited.end(); ++it)
{
if (*it == channel)
{
_channels_invited.erase(it);
break;
}
}
return std::find(_channels_invited.begin(),
_channels_invited.end(),
channel) != _channels_invited.end();
}

/**
Expand All @@ -249,18 +261,24 @@ void Client::add_channel_invited(const std::string& channel)
}

/**
* @brief Checks if the client is invited to a specific channel.
* @brief Removes a channel from the list of invited channels.
*
* This function checks if the client is invited to the specified channel.
* This function removes the specified channel from the list of channels
* that the client has been invited to join.
*
* @param channel The name of the channel to check.
* @return True if the client is invited to the channel, false otherwise.
* @param channel The name of the channel to be removed.
*/
bool Client::is_channel_invited(const std::string& channel)
void Client::remove_channel_invited(const std::string& channel)
{
return std::find(_channels_invited.begin(),
_channels_invited.end(),
channel) != _channels_invited.end();
std::vector<std::string>::iterator it;
for (it = _channels_invited.begin(); it != _channels_invited.end(); ++it)
{
if (*it == channel)
{
_channels_invited.erase(it);
break;
}
}
}

/**
Expand Down
Loading
Loading