From 3b09910eadcc56240137fd0d809cc73d6aff9218 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 20 May 2025 22:17:06 +0200 Subject: [PATCH] feat(user): starting the class user --- include/user.hpp | 36 ++++++++++++++++++++ sources/user/user.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 include/user.hpp create mode 100644 sources/user/user.cpp diff --git a/include/user.hpp b/include/user.hpp new file mode 100644 index 0000000..b40bfee --- /dev/null +++ b/include/user.hpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* user.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */ +/* Updated: 2025/05/20 22:13:41 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include "color.hpp" + +class User { + private: + int _fd; + bool _registered; + std::string _nickname; + std::string _hostname; + std::string _read_buffer; + std::string _write_buffer; + public: + int getFd() const; + bool isReadyToSend() const; + bool isRegistered() const; + std::string getNickname() const; + std::string extractFullCommand(); + void appendToReadBuffer(const std::string &data); + void appendToWriteBuffer(const std::string &data); + void setNickname(const std::string &nickname); +}; diff --git a/sources/user/user.cpp b/sources/user/user.cpp new file mode 100644 index 0000000..80e1adb --- /dev/null +++ b/sources/user/user.cpp @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* user.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/20 22:03:36 by rparodi #+# #+# */ +/* Updated: 2025/05/20 22:15:14 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "user.hpp" + +/** + * @brief Getter for the fd of the user + * + * @return the actual fd of the user + */ +int User::getFd() const { + return this->_fd; +} + +/** + * @brief Getter for the nickname of the user + * + * @return the actual nickname of the user + */ +std::string User::getNickname() const { + return this->_nickname; +} + +/** + * @brief Setter for the nickname of the user (also checker) + * + * @param nickname the nickname given to set to the user + */ +void User::setNickname(const std::string &nickname) { + if (nickname.empty()) { + throw std::invalid_argument("Nickname cannot be empty"); + } else if (nickname == "anonymous") { + throw std::invalid_argument("Nickname cannot be 'anonymous'"); + } else if (nickname.length() > 9) { + throw std::length_error("Nickname is too long"); + } else if (nickname == this->_nickname) { + throw std::invalid_argument("The nickname is the same"); + } else { + this->_nickname = nickname; + } +} + +/** + * @brief Getter for the state of the user + * + * @return true if the user is registered, false otherwise + */ +bool User::isRegistered() const { + return this->_registered; +} +void User::appendToReadBuffer(const std::string &data) { + std::cerr << CLR_RED << "Error: Method not found (" << __FILE_NAME__ ":" << __LINE__ << ")" << CLR_RESET << std::endl; + (void)data; +} +void User::appendToWriteBuffer(const std::string &data) { + std::cerr << CLR_RED << "Error: Method not found (" << __FILE_NAME__ ":" << __LINE__ << ")" << CLR_RESET << std::endl; + (void)data; +} +std::string User::extractFullCommand() { + std::cerr << CLR_RED << "Error: Method not found (" << __FILE_NAME__ ":" << __LINE__ << ")" << CLR_RESET << std::endl; + return nullptr; +} + +bool User::isReadyToSend() const { + std::cerr << CLR_RED << "Error: Method not found (" << __FILE_NAME__ ":" << __LINE__ << ")" << CLR_RESET << std::endl; + return (false); +}