From bb66fcc0971ddd415faaf1f5fe80e3ac88c2920b Mon Sep 17 00:00:00 2001 From: aortigos Date: Fri, 17 Apr 2026 12:01:16 +0200 Subject: [PATCH] One file per command --- Client/Client.cpp | 4 ++-- Client/Client.hpp | 2 +- Makefile | 4 +++- Server/Server.cpp | 31 +++++++++++++++++++++++++++++++ Server/Server.hpp | 14 ++++++++++++++ commands/nick.cpp | 38 ++++++++++++++++++++++++++++++++++++++ commands/pass.cpp | 33 +++++++++++++++++++++++++++++++++ commands/user.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 commands/nick.cpp create mode 100644 commands/pass.cpp create mode 100644 commands/user.cpp diff --git a/Client/Client.cpp b/Client/Client.cpp index f32d895..8f880c4 100644 --- a/Client/Client.cpp +++ b/Client/Client.cpp @@ -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(); } \ No newline at end of file diff --git a/Client/Client.hpp b/Client/Client.hpp index 7ccfb7c..df381e4 100644 --- a/Client/Client.hpp +++ b/Client/Client.hpp @@ -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; diff --git a/Makefile b/Makefile index a3478ec..85854e0 100644 --- a/Makefile +++ b/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) diff --git a/Server/Server.cpp b/Server/Server.cpp index c0c9534..8976fa7 100644 --- a/Server/Server.cpp +++ b/Server/Server.cpp @@ -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"; } \ No newline at end of file diff --git a/Server/Server.hpp b/Server/Server.hpp index 02f9dbd..5c7d91a 100644 --- a/Server/Server.hpp +++ b/Server/Server.hpp @@ -23,6 +23,14 @@ # include "../Client/Client.hpp" # include +# include + + +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); diff --git a/commands/nick.cpp b/commands/nick.cpp new file mode 100644 index 0000000..0f46fdc --- /dev/null +++ b/commands/nick.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* nick.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos > 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); + } +} \ No newline at end of file diff --git a/commands/pass.cpp b/commands/pass.cpp new file mode 100644 index 0000000..f7dff62 --- /dev/null +++ b/commands/pass.cpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pass.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos > 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); + } +} \ No newline at end of file diff --git a/commands/user.cpp b/commands/user.cpp new file mode 100644 index 0000000..8248e64 --- /dev/null +++ b/commands/user.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* user.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos > 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); +} \ No newline at end of file