start coding command pass/nick/user and add client/channel struct

This commit is contained in:
kinou-p 2023-02-07 20:28:19 +01:00
parent 4bc54b29d7
commit fb1b7b10cf
11 changed files with 276 additions and 144 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/12/01 17:27:29 by apommier #+# #+# #
# Updated: 2022/12/20 23:31:36 by apommier ### ########.fr #
# Updated: 2023/02/07 14:47:20 by apommier ### ########.fr #
# #
# **************************************************************************** #
@ -18,7 +18,11 @@ SRCS := main.cpp\
client_request.cpp\
new_connection.cpp\
server_loop.cpp\
commands/parse_commands.cpp
commands/parse_commands.cpp\
commands/auth/nick.cpp\
commands/auth/pass.cpp\
commands/auth/user.cpp\
commands/auth/utils.cpp
CC = c++
FLAGS = -g

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 17:27:37 by apommier #+# #+# */
/* Updated: 2023/02/06 15:10:08 by apommier ### ########.fr */
/* Updated: 2023/02/07 20:25:26 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,58 +20,61 @@
#include <netinet/in.h> //struct socket
#include <sys/epoll.h> //epoll ensemble
#include <unistd.h> //close()
#include <vector>
#define MAX_EVENTS 5
#define READ_SIZE 10
#define CMD_NBR 10
//struct functionTab;
void ft_test();
struct fdList;
//std::string ft_test();
/* ************************************************************************** */
/* *********************************COMMANDS********************************* */
/* ************************************************************************** */
std::string PASS();
std::string NICK();
std::string USER();
std::string SERVER();
std::string OPER();
std::string QUIT();
std::string SQUIT();
std::string JOIN();
std::string PART();
std::string MODE();
std::string TOPIC();
std::string NAMES();
std::string LIST();
std::string INVITE();
std::string KICK();
std::string VERSION();
std::string STATS();
std::string LINKS();
std::string TIME();
std::string CONNECT();
std::string TRACE();
std::string ADMIN();
std::string INFO();
std::string PRIVMSG();
std::string NOTICE();
std::string WHO();
std::string WHOIS();
std::string WHOWAS();
std::string KILL();
std::string PING();
std::string PONG();
std::string ERROR();
std::string AWAY();
std::string REHASH();
std::string RESTART();
std::string SUMMON();
std::string USER();
std::string OPERWALL();
std::string USERHOST();
std::string ISON();
void PASS(std::string buffer, fdList allFds, int user);
void NICK(std::string buffer, fdList allFds, int user);
void USER(std::string buffer, fdList allFds, int user);
void SERVER(std::string buffer, fdList allFds, int user);
void OPER(std::string buffer, fdList allFds, int user);
void QUIT(std::string buffer, fdList allFds, int user);
void SQUIT(std::string buffer, fdList allFds, int user);
void JOIN(std::string buffer, fdList allFds, int user);
void PART(std::string buffer, fdList allFds, int user);
void MODE(std::string buffer, fdList allFds, int user);
void TOPIC(std::string buffer, fdList allFds, int user);
void NAMES(std::string buffer, fdList allFds, int user);
void LIST(std::string buffer, fdList allFds, int user);
void INVITE(std::string buffer, fdList allFds, int user);
void KICK(std::string buffer, fdList allFds, int user);
void VERSION(std::string buffer, fdList allFds, int user);
void STATS(std::string buffer, fdList allFds, int user);
void LINKS(std::string buffer, fdList allFds, int user);
void TIME(std::string buffer, fdList allFds, int user);
void CONNECT(std::string buffer, fdList allFds, int user);
void TRACE(std::string buffer, fdList allFds, int user);
void ADMIN(std::string buffer, fdList allFds, int user);
void INFO(std::string buffer, fdList allFds, int user);
void PRIVMSG(std::string buffer, fdList allFds, int user);
void NOTICE(std::string buffer, fdList allFds, int user);
void WHO(std::string buffer, fdList allFds, int user);
void WHOIS(std::string buffer, fdList allFds, int user);
void WHOWAS(std::string buffer, fdList allFds, int user);
void KILL(std::string buffer, fdList allFds, int user);
void PING(std::string buffer, fdList allFds, int user);
void PONG(std::string buffer, fdList allFds, int user);
void ERROR(std::string buffer, fdList allFds, int user);
void AWAY(std::string buffer, fdList allFds, int user);
void REHASH(std::string buffer, fdList allFds, int user);
void RESTART(std::string buffer, fdList allFds, int user);
void SUMMON(std::string buffer, fdList allFds, int user);
void USER(std::string buffer, fdList allFds, int user);
void OPERWALL(std::string buffer, fdList allFds, int user);
void USERHOST(std::string buffer, fdList allFds, int user);
void ISON(std::string buffer, fdList allFds, int user);
/* ************************************************************************** */
@ -80,7 +83,7 @@ void ft_test();
struct functionTab
{
static const int cmdNbr = 5;
static const int cmdNbr = 40;
std::string cmdName[cmdNbr] =
{
//4. Détails des messages
@ -142,64 +145,67 @@ struct functionTab
};
void (*cmdPtr[cmdNbr])(void) =
//std::string (*cmdPtr[cmdNbr])(void) =
void (*cmdPtr[cmdNbr])(std::string buffer, fdList allFds, int user) =
{
//ft_test
//4. Détails des messages
//4.1 Etablissement de connexion
PASS,
NICK,
USER,
SERVER,
OPER,
QUIT,
SQUIT,
// //4.1 Etablissement de connexion
// PASS,
NICK
// USER,
// SERVER,
// OPER,
// QUIT,
// SQUIT,
//4.2 Opérations sur les canaux
JOIN,
PART,
MODE,
TOPIC,
NAMES,
LIST,
INVITE,
KICK,
// //4.2 Opérations sur les canaux
// JOIN,
// PART,
// MODE,
// TOPIC,
// NAMES,
// LIST,
// INVITE,
// KICK,
//4.3 Requêtes et commandes des serveurs
VERSION,
STATS,
LINKS,
TIME,
CONNECT,
TRACE,
ADMIN,
INFO,
// //4.3 Requêtes et commandes des serveurs
// VERSION,
// STATS,
// LINKS,
// TIME,
// CONNECT,
// TRACE,
// ADMIN,
// INFO,
//4.4 Envoi de messages
PRIVMSG,
NOTICE,
// //4.4 Envoi de messages
// PRIVMSG,
// NOTICE,
//4.5 Requêtes basées sur les utilisateurs
// //4.5 Requêtes basées sur les utilisateurs
WHO,
WHOIS,
WHOWAS,
//4.6 Messages divers
// WHO,
// WHOIS,
// WHOWAS,
// //4.6 Messages divers
KILL,
PING,
PONG,
ERROR,
// KILL,
// PING,
// PONG,
// ERROR,
//5. Messages optionnels
AWAY,
REHASH,
RESTART,
SUMMON,
USER,
OPERWALL,
USERHOST,
ISON
// //5. Messages optionnels
// AWAY,
// REHASH,
// RESTART,
// SUMMON,
// USER,
// OPERWALL,
// USERHOST,
// ISON
};
};
@ -207,22 +213,49 @@ struct functionTab
/* *********************************STRUCT*********************************** */
/* ************************************************************************** */
struct channelData;
struct clientData
{
bool registered = 0;
std::string buf[1024];
std::string nickname;
std::string password;
std::string userName;
std::string realName;
std::string hostName;
std::string serverName;
//std::string joinedChan[20];
channelData *joinedChan; //is there a limit?
int nbrChan;
int perm;
int fd;
};
struct channelData
{
//std::string userList[MAX_EVENTS];
clientData userList[MAX_EVENTS];
int op;
int nbrUser;
};
struct fdList //allFds in code
{
struct epoll_event events[MAX_EVENTS];
int epollFd;
int serverFd;
int userList[MAX_EVENTS];
int userList[MAX_EVENTS]; //list of user's fd
clientData userData[MAX_EVENTS];
int nbrUser;
functionTab parsingTab;
};
struct userClient
{
// int epollFd;
// int serverFd;
// int userList[MAX_EVENTS];
// int nbrUser;
};
@ -237,6 +270,8 @@ void close_fd(int fd);
/* ******************************START SERVER******************************** */
/* ************************************************************************** */
void cmd_error(fdList allFds, int user, std::string error);
void split(std::string const &str, const char delim, std::vector<std::string> &out);
void initialize(char **av); //1st
void start_loop(fdList allFds); //3rd
@ -252,11 +287,11 @@ int epoll_start(); //2nd
/* ************************************************************************** */
void new_connection(fdList allFds, struct epoll_event newClient);
bool clientRequest(fdList allFds, int newFd);
bool clientRequest(fdList allFds, int user);
/* ************************************************************************** */
/* ***************************COMMANDS PARSING******************************* */
/* ************************************************************************** */
void parse_commands(std::string buffer, fdList allFds);
void parse_commands(std::string buffer, fdList allFds, int user);

View File

@ -6,21 +6,25 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/09 22:34:36 by apommier #+# #+# */
/* Updated: 2023/02/06 12:36:52 by apommier ### ########.fr */
/* Updated: 2023/02/07 11:24:51 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/ft_irc.hpp"
bool clientRequest(fdList allFds, int clientFd)
bool clientRequest(fdList allFds, int user)//,
{
int clientFd = allFds.events[user].data.fd;
char buf[1024] = {0};
size_t len = 1024;
//se demerder pour join quand pas \n
std::cout << "client request!" << std::endl;
if (recv(clientFd, buf, len, 0) == -1)
ft_error("recv() error");
std::cout << "BUFFER: ---" << buf << "---" << std::endl;
parse_commands(buf, allFds);
//split with \n and while (tab de split) -> parsing
parse_commands(buf, allFds, user);
return (1);
}

View File

@ -6,13 +6,21 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/06 14:28:38 by apommier #+# #+# */
/* Updated: 2023/02/06 14:28:55 by apommier ### ########.fr */
/* Updated: 2023/02/07 15:13:27 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_irc.hpp"
#include "../../../includes/ft_irc.hpp"
void NICK()
// ERR_NONICKNAMEGIVEN ERR_ERRONEUSNICKNAME
// ERR_NICKNAMEINUSE ERR_NICKCOLLISION
void NICK(std::string buffer, fdList allFds, int user)
{
std::vector<std::string> splitBuff;
split(buffer, ' ', splitBuff);
//if nickname ok then (another user has it? )
allFds.userData[user].nickname = splitBuff[1];
return ;
}

View File

@ -6,13 +6,33 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/06 14:40:42 by apommier #+# #+# */
/* Updated: 2023/02/06 14:41:12 by apommier ### ########.fr */
/* Updated: 2023/02/07 19:40:34 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_irc.hpp"
#include "../../../includes/ft_irc.hpp"
void PASS()
void PASS(std::string buffer, fdList allFds, int user)
{
std::string password;
if (buffer.size() < 6)// ---PASS ---
{
cmd_error(allFds, user, "461 * PASS :Not enough parameters");
return ;
}
// if (splitBuff.size() != 2)
// {
// cmd_error(allFds, user, "461 * PASS :Not enough parameters");
// //write(allFds.userData[user].fd, "ERR_NEEDMOREPARAMS", 18);
// return ;
// }
else if (allFds.userData[user].registered)
{
cmd_error(allFds, user, "462 * PASS :You may not reregister");
return ;
}
password = buffer.substr(5, buffer.npos);
allFds.userData[user].password = buffer;
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* user.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/06 14:40:39 by apommier #+# #+# */
/* Updated: 2023/02/07 20:27:12 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/ft_irc.hpp"
//Commande: USER
//Paramètres: <nom d'utilisateur> <hôte> <nom de serveur> :<nom réel>
//ERR_NEEDMOREPARAMS ERR_ALREADYREGISTRED
void USER(std::string buffer, fdList allFds, int user)
{
std::string realName = buffer.substr(buffer.find_first_of(":") + 1 , buffer.npos);
std::vector<std::string> splitBuff;
buffer.resize(buffer.size() - realName.size());
std::cout << "Buffer in user = ---" << buffer << "---" << std::endl;
std::cout << "realname in user = ---" << realName << "---" << std::endl;
split(buffer, ' ', splitBuff);
if (splitBuff.size() < 4)
{
cmd_error(allFds, user, "461 * PASS :Not enough parameters");
//write(allFds.userData[user].fd, "ERR_NEEDMOREPARAMS", 18);
return ;
}
allFds.userData[user].userName = splitBuff[1];
allFds.userData[user].hostName = splitBuff[2];
allFds.userData[user].serverName = splitBuff[3];
allFds.userData[user].realName = realName;
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/07 14:45:45 by apommier #+# #+# */
/* Updated: 2023/02/07 15:16:10 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/ft_irc.hpp"
void cmd_error(fdList allFds, int user, std::string error)
{
write(allFds.userData[user].fd, ":irc.local ", error.size());
write(allFds.userData[user].fd, error.c_str(), error.size());
}
void split(std::string const &str, const char delim, std::vector<std::string> &out)
{
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
{
end = str.find(delim, start);
out.push_back(str.substr(start, end - start));
}
}

View File

@ -6,28 +6,33 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 22:01:25 by apommier #+# #+# */
/* Updated: 2023/02/06 12:39:13 by apommier ### ########.fr */
/* Updated: 2023/02/07 15:18:41 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_irc.hpp"
void ft_test()
std::string ft_test()
{
std::cout << "hehe je suis la" << std::endl;
return ("prout");
}
void parse_commands(std::string buffer, fdList allFds)
void parse_commands(std::string buffer, fdList allFds, int user)
{
std::string cmd = buffer;
std::vector<std::string> splitBuff;
int a = 0;
cmd.pop_back();
split(cmd, ' ', splitBuff);
std::cout << "BUFFER In PARSING: ---" << cmd << "---" << std::endl;
while (cmd != allFds.parsingTab.cmdName[a] && a < allFds.parsingTab.cmdNbr)
while (splitBuff[0] != allFds.parsingTab.cmdName[a] && a < allFds.parsingTab.cmdNbr)
a++;
if (a == allFds.parsingTab.cmdNbr)
std::cout << "Command not found\n";
else
allFds.parsingTab.cmdPtr[a]();
allFds.parsingTab.cmdPtr[a](buffer, allFds, user);
}

View File

@ -1,18 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* user.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/06 14:40:39 by apommier #+# #+# */
/* Updated: 2023/02/06 14:41:03 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_irc.hpp"
void USER()
{
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/09 22:26:27 by apommier #+# #+# */
/* Updated: 2022/12/18 12:13:28 by apommier ### ########.fr */
/* Updated: 2023/02/07 11:41:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,9 @@ void new_connection(fdList allFds, struct epoll_event newClient)
if (MAX_EVENTS == allFds.nbrUser)
std::cout << "new connection refused, already full" << std::endl;
allFds.userList[allFds.nbrUser] = accept(allFds.serverFd, (struct sockaddr *)&addr, (socklen_t*)&addrLen);
allFds.userData[allFds.nbrUser].fd = allFds.userList[allFds.nbrUser];
epoll_add(allFds.epollFd, allFds.userList[allFds.nbrUser]);
allFds.nbrUser++;
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 21:58:13 by apommier #+# #+# */
/* Updated: 2023/02/06 12:33:21 by apommier ### ########.fr */
/* Updated: 2023/02/07 11:23:01 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,11 +16,12 @@ void start_loop(fdList allFds)
{
bool alive = true;
char buffer[1024] = { 0 };
struct epoll_event events[MAX_EVENTS];
//struct epoll_event events[MAX_EVENTS];
int readChar;
int eventNbr;
std::cout << "serverFd: " << allFds.serverFd << std::endl;
//allFds.events //= allFds.events;
while (alive)
{
// std::cout << "hehe\n";
@ -30,14 +31,14 @@ void start_loop(fdList allFds)
// send(clientFd, "message received", 18, 0);
eventNbr = epoll_wait(allFds.epollFd, events, MAX_EVENTS, 5000);
eventNbr = epoll_wait(allFds.epollFd, allFds.events, MAX_EVENTS, 5000);
std::cout << "eventNbr: " << eventNbr << std::endl;
for (int i = 0; i < eventNbr ; i++)
{
std::cout << "event[i]'s fd: " << events[i].data.fd << std::endl;
if (events[i].data.fd == allFds.serverFd)
new_connection(allFds, events[i]);
else if (!clientRequest(allFds, events[i].data.fd))
std::cout << "event[i]'s fd: " << allFds.events[i].data.fd << std::endl;
if (allFds.events[i].data.fd == allFds.serverFd)
new_connection(allFds, allFds.events[i]);
else if (!clientRequest(allFds, i))//allFds.events[i].data.fd))
{
alive = false;
//else
@ -48,7 +49,6 @@ void start_loop(fdList allFds)
}
if (!strncmp("/quit", buffer, 5))
std::cout << "quit message received\n";
}
std::cout << "dead server\n";