One file per command

This commit is contained in:
2026-04-17 12:01:16 +02:00
parent 00a08b4f30
commit bb66fcc097
8 changed files with 169 additions and 4 deletions

View File

@@ -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)
@@ -126,4 +139,22 @@ 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";
}

View File

@@ -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);