One file per command
This commit is contained in:
@@ -36,7 +36,7 @@ int Client::getFd() const { return (this->fd); }
|
||||
std::string Client::getNick() const { return (this->nick); }
|
||||
std::string Client::getUsername() const { return (this->username); }
|
||||
std::string Client::getRealname() const { return (this->realname); }
|
||||
std::string Client::getBuffer() const { return (this->buffer); }
|
||||
std::string &Client::getBuffer() { return (this->buffer); }
|
||||
bool Client::isAuthenticated() const { return (this->authenticated); }
|
||||
bool Client::isRegistered() const { return (this->registered); }
|
||||
|
||||
@@ -45,6 +45,6 @@ void Client::setNick(std::string nick) { this->nick = nick; }
|
||||
void Client::setUsername(std::string username) { this->username = username; }
|
||||
void Client::setRealname(std::string realname) { this->realname = realname; }
|
||||
void Client::setAuthenticated(bool value) { this->authenticated = value; }
|
||||
void Client::setRegistered(bool value) { this->authenticated = value; }
|
||||
void Client::setRegistered(bool value) { this->registered = value; }
|
||||
void Client::appendBuffer(std::string data) { this->buffer += data; }
|
||||
void Client::clearBuffer() { this->buffer.clear(); }
|
||||
@@ -39,7 +39,7 @@ class Client
|
||||
std::string getNick() const;
|
||||
std::string getUsername() const;
|
||||
std::string getRealname() const;
|
||||
std::string getBuffer() const;
|
||||
std::string &getBuffer();
|
||||
bool isAuthenticated() const;
|
||||
bool isRegistered() const;
|
||||
|
||||
|
||||
4
Makefile
4
Makefile
@@ -1,6 +1,8 @@
|
||||
NAME = ircserver
|
||||
|
||||
SRC = main.cpp Server/Server.cpp Client/Client.cpp
|
||||
SRC = main.cpp Server/Server.cpp Client/Client.cpp \
|
||||
commands/pass.cpp commands/nick.cpp commands/user.cpp \
|
||||
|
||||
|
||||
OBJ = $(SRC:.cpp=.o)
|
||||
|
||||
|
||||
@@ -109,6 +109,19 @@ void Server::readClient(int fd)
|
||||
|
||||
clients[fd].appendBuffer(buf);
|
||||
std::cout << "fd=" << fd << " dice: " << buf;
|
||||
|
||||
std::string &clientBuf = clients[fd].getBuffer();
|
||||
size_t pos;
|
||||
while ((pos = clientBuf.find('\n')) != std::string::npos)
|
||||
{
|
||||
std::string line = clientBuf.substr(0, pos);
|
||||
clients[fd].clearBuffer();
|
||||
|
||||
if (!line.empty() && line[line.size() - 1] == '\r')
|
||||
line.erase(line.size() - 1);
|
||||
if (!line.empty())
|
||||
parseCommand(fd, line);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::removeClient(int fd)
|
||||
@@ -127,3 +140,21 @@ void Server::removeClient(int fd)
|
||||
clients.erase(fd);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void Server::parseCommand(int fd, std::string line)
|
||||
{
|
||||
(void)fd;
|
||||
std::istringstream ss(line);
|
||||
std::string command;
|
||||
|
||||
ss >> command;
|
||||
|
||||
if (command == "PASS")
|
||||
cmd_pass(fd, ss);
|
||||
else if (command == "NICK")
|
||||
cmd_nick(fd, ss);
|
||||
else if (command == "USER")
|
||||
cmd_user(fd, ss);
|
||||
else
|
||||
std::cout << "Comando desconocido: " << command << "\n";
|
||||
}
|
||||
@@ -23,6 +23,14 @@
|
||||
|
||||
# include "../Client/Client.hpp"
|
||||
# include <map>
|
||||
# include <sstream>
|
||||
|
||||
|
||||
const std::string ERR_PASSWORD = "ERROR :Password incorrect\r\n";
|
||||
const std::string ERR_NONICK = "ERROR :No nick given\r\n";
|
||||
const std::string ERR_NOPASS = "ERROR :Authenticate first\r\n";
|
||||
const std::string ERR_REREGISTER = "462 :You may not reregister\r\n";
|
||||
const std::string ERR_NOUSER = "ERROR :No username given\r\n";
|
||||
|
||||
class Server
|
||||
{
|
||||
@@ -37,6 +45,12 @@ class Server
|
||||
void readClient(int fd);
|
||||
void removeClient(int fd);
|
||||
|
||||
void parseCommand(int fd, std::string line);
|
||||
|
||||
void cmd_pass(int fd, std::istringstream &ss);
|
||||
void cmd_nick(int fd, std::istringstream &ss);
|
||||
void cmd_user(int fd, std::istringstream &ss);
|
||||
|
||||
public:
|
||||
Server();
|
||||
Server(int port, std::string password);
|
||||
|
||||
38
commands/nick.cpp
Normal file
38
commands/nick.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* nick.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/04/17 11:56:55 by aortigos #+# #+# */
|
||||
/* Updated: 2026/04/17 11:59:33 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../Server/Server.hpp"
|
||||
|
||||
void Server::cmd_nick(int fd, std::istringstream &ss)
|
||||
{
|
||||
std::string args;
|
||||
|
||||
ss >> args;
|
||||
|
||||
if(!clients[fd].isAuthenticated())
|
||||
{
|
||||
send(fd, ERR_NOPASS.c_str(), ERR_NOPASS.size(), 0);
|
||||
return ;
|
||||
}
|
||||
|
||||
if (!args.empty())
|
||||
{
|
||||
clients[fd].setNick(args);
|
||||
|
||||
std::string msg = "Your nick has been updated!\r\n";
|
||||
send(fd, msg.c_str(), msg.size(), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
send(fd, ERR_NONICK.c_str(), ERR_NONICK.size(), 0);
|
||||
}
|
||||
}
|
||||
33
commands/pass.cpp
Normal file
33
commands/pass.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pass.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/04/17 11:53:56 by aortigos #+# #+# */
|
||||
/* Updated: 2026/04/17 11:56:21 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../Server/Server.hpp"
|
||||
|
||||
void Server::cmd_pass(int fd, std::istringstream &ss)
|
||||
{
|
||||
std::string args;
|
||||
|
||||
ss >> args;
|
||||
|
||||
if (args == this->password)
|
||||
{
|
||||
clients[fd].setAuthenticated(true);
|
||||
|
||||
std::string msg = "You have been authenticated!\r\n";
|
||||
send(fd, msg.c_str(), msg.size(), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
send(fd, ERR_PASSWORD.c_str(), ERR_PASSWORD.size(), 0);
|
||||
removeClient(fd);
|
||||
}
|
||||
}
|
||||
47
commands/user.cpp
Normal file
47
commands/user.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* user.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/04/17 11:58:23 by aortigos #+# #+# */
|
||||
/* Updated: 2026/04/17 11:58:40 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../Server/Server.hpp"
|
||||
|
||||
void Server::cmd_user(int fd, std::istringstream &ss)
|
||||
{
|
||||
std::string username, unused1, unused2, realname;
|
||||
|
||||
ss >> username >> unused1 >> unused2;
|
||||
std::getline(ss, realname);
|
||||
|
||||
if (clients[fd].getNick().empty())
|
||||
{
|
||||
send(fd, ERR_NONICK.c_str(), ERR_NONICK.size(), 0);
|
||||
return ;
|
||||
}
|
||||
|
||||
if(clients[fd].isRegistered())
|
||||
{
|
||||
send(fd, ERR_REREGISTER.c_str(), ERR_REREGISTER.size(), 0);
|
||||
return ;
|
||||
}
|
||||
|
||||
if (username.empty())
|
||||
{
|
||||
send(fd, ERR_NOUSER.c_str(), ERR_NOUSER.size(), 0);
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
clients[fd].setUsername(username);
|
||||
clients[fd].setRealname(realname);
|
||||
clients[fd].setRegistered(true);
|
||||
|
||||
std::string msg = "You has been registered!\r\n";
|
||||
send(fd, msg.c_str(), msg.size(), 0);
|
||||
}
|
||||
Reference in New Issue
Block a user