Skip to content

Commit

Permalink
Merge pull request #41 from ygor-sena/gilmar-branch
Browse files Browse the repository at this point in the history
refactor: better unit tests files formatting
  • Loading branch information
gialexan authored Jun 1, 2024
2 parents ca6802c + 40ff1df commit 447d214
Show file tree
Hide file tree
Showing 24 changed files with 411 additions and 661 deletions.
8 changes: 4 additions & 4 deletions include/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Channel.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:24:04 by gilmar #+# #+# */
/* Updated: 2024/05/31 12:09:15 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 23:14:19 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -72,8 +72,8 @@ class Channel
std::string _topic;
std::string _created_at;
std::vector<Client*>
_clients; // -> list of clients that are channel members
std::vector<Client*> _operator_clients; // -> list of channel operators
_clients;
std::vector<Client*> _operator_clients;
};

#endif // CHANNEL_HPP
55 changes: 29 additions & 26 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,40 @@
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:19:06 by gilmar #+# #+# */
/* Updated: 2024/05/31 12:49:07 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 23:13:37 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <arpa/inet.h> //-> for inet_ntoa()
#include <fcntl.h> //-> for fcntl()
#include <netinet/in.h> //-> for sockaddr_in
#include <poll.h> //-> for poll()
#include <sys/socket.h> //-> for socket()
#include <sys/types.h> //-> for socket()
#include <unistd.h> //-> for close()
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <csignal> //-> for signal()
#include <csignal>
#include <iostream>
#include <string>
#include <vector> //-> for vector
#include <vector>
#include "Replies.hpp"

//-------------------------------------------------------//
#define RED "\e[1;31m" //-> for red color
#define WHI "\e[0;37m" //-> for white color
#define GRE "\e[1;32m" //-> for green color
#define YEL "\e[1;33m" //-> for yellow color

#define RED "\e[1;31m"
#define WHI "\e[0;37m"
#define GRE "\e[1;32m"
#define YEL "\e[1;33m"

//-------------------------------------------------------//

class Client //-> class for client
class Client
{
public:
Client();
Expand All @@ -52,6 +54,7 @@ class Client //-> class for client
std::string get_password() const;
std::string get_hostname() const;
std::string get_ip_address() const;
bool get_already_registered() const;
std::vector<std::string> get_channels_invited() const;

void set_fd(const int fd);
Expand All @@ -71,16 +74,16 @@ class Client //-> class for client
void broadcast(Client* sender, std::string target, std::string message);

private:
int _fd; //-> client file descriptor
bool _is_logged; //-> boolean for login
bool _is_authenticated; //-> boolean for authentication
bool _is_operator; //-> boolean for channel operator
std::string _buffer; //-> client buffer
std::string _ip_addr; //-> client ip address
std::string _nickname; //-> client nickname
std::string _username; //-> client username
std::string _password; //-> client password
std::vector<std::string> _channels_invited; //-> vector of channels invited
int _fd;
bool _is_logged;
bool _is_authenticated;
bool _is_operator;
std::string _buffer;
std::string _ip_addr;
std::string _nickname;
std::string _username;
std::string _password;
std::vector<std::string> _channels_invited;
};

#endif // CLIENT_HPP
25 changes: 15 additions & 10 deletions include/Replies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Replies.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/11 18:18:57 by gilmar #+# #+# */
/* Updated: 2024/05/31 20:57:35 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 21:52:57 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,20 +18,22 @@
// refer to https://datatracker.ietf.org/doc/html/rfc1459

#define RPL_CONNECTED(serverhostname, nickname, targethostname) \
(":" + serverhostname + " 001 " + nickname + " :Welcome to the IRC server! " + nickname + "!" + targethostname + CRLF)
(":" + serverhostname + " 001 " + nickname + \
" :Welcome to the IRC server! " + nickname + "!" + targethostname + CRLF)

#define RPL_UMODEIS(nickname, hostname, channelname, set, mode, arg) \
(":" + nickname + "!" + hostname + " MODE " + channelname + " " + set + mode + " " + arg + CRLF)
#define RPL_UMODEIS(nickname, hostname, channelname, set, mode, arg) \
(":" + nickname + "!" + hostname + " MODE " + channelname + " " + set + \
mode + " " + arg + CRLF)

#define RPL_CREATIONTIME(nickname, channelname, creationtime) \
": 329 " + nickname + " " + channelname + " " + creationtime + CRLF

#define RPL_CHANNELMODES(nickname, channelname, modes) \
": 324 " + nickname + " " + channelname + " " + modes + CRLF

#define RPL_CHANGEMODE(hostname, channelname, mode, arguments) J \
(":" + hostname + " MODE " + channelname + " " + mode + " " + arguments + \
CRLF)
#define RPL_CHANGEMODE(hostname, channelname, mode, arguments) \
J(":" + hostname + " MODE " + channelname + " " + mode + " " + arguments + \
CRLF)

#define RPL_NICKCHANGE(oldnickname, nickname) \
(":" + oldnickname + " NICK " + nickname + CRLF)
Expand Down Expand Up @@ -61,8 +63,9 @@
kickername + " " + comment + CRLF)
#define RPL_INVITING(hostname, channelname, invitername, invitedname) \
(":" + hostname + " INVITE " + invitedname + " " + channelname + CRLF)
#define RPL_PRIVMSG(nickname, hostname, receiver, text) \
(":" + nickname + "!" + hostname + " PRIVMSG " + receiver + " " + text + CRLF)
#define RPL_PRIVMSG(nickname, hostname, receiver, text) \
(":" + nickname + "!" + hostname + " PRIVMSG " + receiver + " " + text + \
CRLF)

#define BOT_CMDMARVIN(nickname) \
(": 4242 marvin_bot " + nickname + \
Expand Down Expand Up @@ -137,5 +140,7 @@
#define ERR_NOPRIVILEGES(nickname) \
(": 481 " + nickname + " :Permission Denied- You're not an IRC operator" + \
CRLF)
#define ERR_ERRONEUSNICK(nickname) \
(": 432 " + nickname + " :Erroneus nickname" + CRLF)

#endif // REPLIES_HPP
126 changes: 64 additions & 62 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,43 @@
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:23:47 by gilmar #+# #+# */
/* Updated: 2024/05/31 18:57:35 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 23:12:41 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef SERVER_HPP
#define SERVER_HPP

#include <arpa/inet.h> //-> for inet_ntoa()
#include <fcntl.h> //-> for fcntl()
#include <netinet/in.h> //-> for sockaddr_in
#include <poll.h> //-> for poll()
#include <sys/socket.h> //-> for socket()
#include <sys/types.h> //-> for socket()
#include <unistd.h> //-> for close()
#include <csignal> //-> for signal()
#include <cstdlib> // para a função std::atoi
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map> //-> for map
#include <sstream> //-> for std::stringstream
#include <string> // Para std::to_string
#include <vector> //-> for vector
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <utility>

//-------------------------------------------------------//
#define RED "\e[1;31m" //-> for red color
#define WHI "\e[0;37m" //-> for white color
#define GRE "\e[1;32m" //-> for green color
#define YEL "\e[1;33m" //-> for yellow color

#define RED "\e[1;31m"
#define WHI "\e[0;37m"
#define GRE "\e[1;32m"
#define YEL "\e[1;33m"

//-------------------------------------------------------//

#include "Channel.hpp" //-> for channel class
#include "Client.hpp" //-> for client class
#include "Replies.hpp" //-> for replies class
#include "Channel.hpp"
#include "Client.hpp"
#include "Replies.hpp"

#define SPACE " "
#define CRLF "\r\n"
Expand All @@ -51,28 +54,27 @@ class Server
public:
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);

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

private:
int _port; //-> server port
int _server_fdsocket; //-> server socket file descriptor
int _port;
int _server_fdsocket;
std::string _hostname;
std::string _password; //-> server password
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
std::string _password;
std::vector<Client*> _clients;
std::vector<struct pollfd> _fds;
struct sockaddr_in _server_addr;
std::vector<Channel*> _channels;

int _reply_code; // -> This a workaround to test the IRC commands without
// mocking up a client-server connection

std::string _get_hostname(); //-> get hostname
int get_reply_code(void); // -> Get the reply code to be checked against
// criterion test results
std::string _get_hostname();
void _is_valid_port(const std::string& port);
bool _is_valid_nickname(const std::string& nickname);
bool _is_nickname_in_use(const int fd, const std::string& nickname);
Expand All @@ -96,60 +98,60 @@ class Server
void _handler_bot_whoami(const std::string& buffer, int fd);
void _handler_bot_quote(const std::string& buffer, int fd);

void _handler_invite_only_mode(Channel* channel, bool set);
void _handler_topic_restriction_mode(Channel* channel, bool set);
void _handle_operator_privileges_mode(Channel* channel, Client* client,
bool set);
void _handle_password_mode(Channel* channel, const std::string& argument,
bool set);
void _handle_limit_mode(Channel* channel, Client* client,
const std::string& argument, bool set,
const int fd);
bool _handler_mode_flags(Channel* channel, Client* client, char mode,
bool set, const std::string& arg, const int fd);
bool _parse_mode_command(const std::string& modes, Channel* channel,
Client* client, const std::string& arg,
void _set_channel_limit_mode(Channel* channel, const std::string& limitStr,
bool addMode);
void _set_channel_operator_mode(Channel* channel, Client* client,
bool addMode);
void _set_channel_key_mode(Channel* channel, const std::string& key,
bool addMode);
void _set_topic_restriction_mode(Channel* channel, bool addMode);
void _set_invite_only_mode(Channel* channel, bool addMode);

bool _process_mode_flags(const std::string& modeFlags, Channel* channel,
Client* targetClient, const std::string& argument,
const int fd);
bool _apply_mode_flag(Channel* channel, Client* targetClient, char mode,
bool addMode, const std::string& argument);

static bool _signal; //-> static boolean for signal
static bool _signal;
static void _signal_handler(const int signum);

void _server_loop(); //-> server loop
void _set_server_socket(); //-> server socket creation
void _add_server_signal(); //-> server signal creation
void _accept_new_client(); //-> accept new client
void _clear_client(const int fd); //-> clear clients
void _receive_new_data(const int fd); //-> receive data from a client
void _server_loop();
void _set_server_socket();
void _add_server_signal();
void _accept_new_client();
void _clear_client(const int fd);
void _receive_new_data(const int fd);
void _send_response(
const int fd,
const std::string& response); //-> send response to client
const std::string& response);

struct command_handler
{
std::string command;
void (Server::*handler)(const std::string&, const int);
};

static const int _command_list_size = 16; //-> command list size
static const int _command_list_size = 16;
static const command_handler
_command_list[_command_list_size]; //-> command list
_command_list[_command_list_size];

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

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

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
Client* _get_client(const int fd);
Client* _get_client(const std::string nickname);
Channel* _get_channel(const std::string& channel_name);
void _add_channel(
Channel* channel); // -> add a new channel to server channels
Channel* channel);
bool _client_is_ready_to_login(const int fd);

std::string toupper(const std::string& str);
Expand Down
Binary file added ircserv
Binary file not shown.
5 changes: 3 additions & 2 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Channel.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:26:17 by gilmar #+# #+# */
/* Updated: 2024/05/31 20:00:22 by yde-goes ### ########.fr */
/* Updated: 2024/05/31 23:03:33 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -33,6 +33,7 @@ Channel::Channel()
/*
** ---------------------------- COPY CONSTRUCTOR ------------------------------
*/

Channel::Channel(std::string name)
{
_limit = -1;
Expand Down
Loading

0 comments on commit 447d214

Please sign in to comment.