ADD commands NICK, USER, PASS, CAP
This commit is contained in:
parent
8a77409158
commit
57aa57d9eb
14 changed files with 224 additions and 50 deletions
31
sources/commands/cap.cpp
Normal file
31
sources/commands/cap.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cap.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/08 22:10:24 by sben-tay #+# #+# */
|
||||
/* Updated: 2025/06/08 22:16:23 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cap.hpp"
|
||||
#include "logs.hpp"
|
||||
|
||||
using namespace cmd;
|
||||
|
||||
cmd::Cap::Cap(User *user, Channel *channel, Server *server, const std::string &line)
|
||||
: ACommand(user, channel, server, line)
|
||||
{
|
||||
_args = split(line);
|
||||
_command = "CAP";
|
||||
}
|
||||
|
||||
void cmd::Cap::execute() {
|
||||
if (_args.size() >= 2 && _args[1] == "LS") {
|
||||
std::string reply = "CAP * LS :\r\n";
|
||||
_sender->appendToWriteBuffer(reply);
|
||||
DEBUG_MSG("Replied to CAP LS");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
/* ************************************************************************** */
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* commands.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 16:11:56 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/03 16:46:58 by rparodi ### ########.fr */
|
||||
/* Updated: 2025/06/08 20:20:11 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,6 +14,9 @@
|
|||
#include "logs.hpp"
|
||||
#include "pass.hpp"
|
||||
#include "ping.hpp"
|
||||
#include "nick.hpp"
|
||||
#include "userCmd.hpp"
|
||||
|
||||
|
||||
/**
|
||||
* @brief To send the line where a command is invoqued to execute
|
||||
|
|
@ -68,6 +71,11 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
|
|||
return;
|
||||
}
|
||||
switch (command_name[0]) {
|
||||
case 'c':
|
||||
if (command_name == "CAP") {
|
||||
Cap(user, channel, server, line).execute();
|
||||
}
|
||||
break;
|
||||
case 'i':
|
||||
// if (command_name == "invite") {
|
||||
// Invite(user, channel, server, line).execute();
|
||||
|
|
@ -94,23 +102,28 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
|
|||
// }
|
||||
break;
|
||||
case 'n':
|
||||
// if (command_name == "nick") {
|
||||
// Nick(user, channel, server, line).execute();
|
||||
if (command_name == "NICK") {
|
||||
Nick(user, channel, server, line).execute();
|
||||
// } else if (command_name == "notice") {
|
||||
// Notice(user, channel, server, line).execute();
|
||||
// }
|
||||
}
|
||||
break;
|
||||
case 'p':
|
||||
if (command_name == "pass") {
|
||||
if (command_name == "PASS") {
|
||||
Pass(user, channel, server, line).execute();
|
||||
}
|
||||
if (command_name == "ping") {
|
||||
if (command_name == "PING") {
|
||||
Ping(user, channel, server, line).execute();
|
||||
}
|
||||
// if (command_name == "part") {
|
||||
// Part(user, channel, server, line).execute();
|
||||
// }
|
||||
break;
|
||||
case 'u':
|
||||
if (command_name == "USER") {
|
||||
userCmd(user, channel, server, line).execute();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
WARNING_MSG("Unknown command: " << command_name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,32 +3,30 @@
|
|||
/* ::: :::::::: */
|
||||
/* nick.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/05 22:48:17 by rparodi ### ########.fr */
|
||||
/* Updated: 2025/06/08 22:18:49 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "nick.hpp"
|
||||
#include "commands.hpp"
|
||||
#include "logs.hpp"
|
||||
|
||||
using namespace cmd;
|
||||
|
||||
e_code Nick::checkArgs() {
|
||||
if (this->_uTarget->isRegistered() == false) {
|
||||
WARNING_MSG("User is not registered for Nick command");
|
||||
INFO_MSG("You can only Nick registered users");
|
||||
return ERR_NOSUCHNICK;
|
||||
e_code cmd::Nick::checkArgs() {
|
||||
if (_args.size() < 2 || _args[1].empty()) {
|
||||
WARNING_MSG("Nick: Not enough arguments");
|
||||
return ERR_NONICKNAMEGIVEN;
|
||||
}
|
||||
if (_args.size() < 2) {
|
||||
WARNING_MSG("Not enough arguments for Nick command");
|
||||
return ERR_NEEDMOREPARAMS;
|
||||
if (_sender->isRegistered()) {
|
||||
WARNING_MSG(_sender->getName() << " is already registered");
|
||||
return ERR_ALREADYREGISTERED;
|
||||
}
|
||||
_uTarget = searchList(this->_users, _args.at(1));
|
||||
if (this->_uTarget != NULL) {
|
||||
WARNING_MSG(_uTarget->getName() << " is already taken")
|
||||
User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin
|
||||
if (existing != NULL) {
|
||||
WARNING_MSG("Nick already in use: " << _args[1]);
|
||||
return ERR_NICKNAMEINUSE;
|
||||
}
|
||||
return _PARSING_OK;
|
||||
|
|
@ -38,10 +36,13 @@ e_code Nick::checkArgs() {
|
|||
* @brief Execute the Nick command
|
||||
* @note To change the nickname of the user
|
||||
*/
|
||||
void Nick::execute() {
|
||||
void cmd::Nick::execute() {
|
||||
if (checkArgs() == _PARSING_OK) {
|
||||
ERROR_MSG("Invalid arguments for Nick command (see warning message)");
|
||||
return;
|
||||
}
|
||||
// check how the com
|
||||
DEBUG_MSG("Setting nickname to " << _args[1]);
|
||||
_sender->setNickname(_args[1]);
|
||||
_sender->setHasNick(true);
|
||||
_sender->checkRegistration();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* pass.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/04 23:59:57 by rparodi ### ########.fr */
|
||||
/* Updated: 2025/06/08 20:10:47 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -33,6 +33,7 @@ e_code Pass::checkArgs() {
|
|||
* @brief Execute the Pass command
|
||||
* @note To connect a user with the password
|
||||
*/
|
||||
|
||||
void Pass::execute() {
|
||||
if (checkArgs() == _PARSING_OK) {
|
||||
ERROR_MSG("Invalid arguments for Pass command (see warning message)");
|
||||
|
|
@ -44,5 +45,5 @@ void Pass::execute() {
|
|||
ERROR_MSG("The password is incorrect");
|
||||
return;
|
||||
}
|
||||
_sender->setRegistered();
|
||||
_sender->setHasPass(true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* ping.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/04 23:54:56 by rparodi ### ########.fr */
|
||||
/* Updated: 2025/06/08 20:13:52 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -30,6 +30,7 @@ e_code Ping::checkArgs() {
|
|||
* @brief Execute the Ping
|
||||
* @note To send a private message to a user / a channel
|
||||
*/
|
||||
|
||||
void Ping::execute() {
|
||||
clock_t start = clock() / CLOCKS_PER_SEC;
|
||||
if (checkArgs() == _PARSING_OK) {
|
||||
|
|
@ -38,5 +39,4 @@ void Ping::execute() {
|
|||
}
|
||||
clock_t diff = Pong().answer(start);
|
||||
INFO_MSG(diff);
|
||||
// check how the com
|
||||
}
|
||||
|
|
|
|||
40
sources/commands/userCmd.cpp
Normal file
40
sources/commands/userCmd.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* userCmd.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/08 19:16:10 by sben-tay #+# #+# */
|
||||
/* Updated: 2025/06/08 22:19:02 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "userCmd.hpp"
|
||||
#include "logs.hpp"
|
||||
|
||||
using namespace cmd;
|
||||
|
||||
e_code cmd::userCmd::checkArgs() {
|
||||
if (_args.size() < 5) {
|
||||
WARNING_MSG("USER: Not enough parameters");
|
||||
return ERR_NEEDMOREPARAMS;
|
||||
}
|
||||
if (_sender->isRegistered()) {
|
||||
WARNING_MSG(_sender->getName() << " is already registered");
|
||||
return ERR_ALREADYREGISTERED;
|
||||
}
|
||||
return _PARSING_OK;
|
||||
}
|
||||
|
||||
void cmd::userCmd::execute() {
|
||||
if (checkArgs() != _PARSING_OK) {
|
||||
ERROR_MSG("USER: bad args");
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_MSG("Setting username to " << _args[1]);
|
||||
_sender->setUsername(_args[1]);
|
||||
_sender->setHasUser(true);
|
||||
_sender->checkRegistration();
|
||||
}
|
||||
|
|
@ -1,19 +1,20 @@
|
|||
/******************************************************************************/
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* user.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
|
||||
/* Updated: 2025/05/29 13:30:51 by rparodi ### ########.fr */
|
||||
/* Updated: 2025/06/08 21:41:26 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "core.hpp"
|
||||
|
||||
// Constructor
|
||||
User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false) {}
|
||||
User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \
|
||||
_hasPass(false) {}
|
||||
|
||||
/**
|
||||
* @brief Getter for the fd
|
||||
|
|
@ -82,9 +83,13 @@ void User::appendToWriteBuffer(const std::string &data)
|
|||
// Check registration
|
||||
void User::checkRegistration()
|
||||
{
|
||||
if (!_registered && _hasNick && _hasUser)
|
||||
if (!_registered && _hasNick && _hasUser && _hasPass)
|
||||
{
|
||||
_registered = true;
|
||||
std::string welcome = ":localhost 001 " + _nickname +
|
||||
" :Welcome to the IRC server " + getPrefix() + "\r\n";
|
||||
|
||||
appendToWriteBuffer(welcome);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -109,3 +114,23 @@ std::string User::extractFullCommand() {
|
|||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
void User::setHasNick(bool value) { _hasNick = value; }
|
||||
|
||||
void User::setHasUser(bool value) { _hasUser = value; }
|
||||
|
||||
void User::setHasPass(bool value) { _hasPass = value; }
|
||||
|
||||
bool User::getHasPass() const { return _hasPass; }
|
||||
|
||||
std::string User::getNickname() const { return _nickname; }
|
||||
|
||||
bool User::hasDataToSend() const { return !_write_buffer.empty(); }
|
||||
|
||||
std::string User::getWriteBuffer() const { return _write_buffer; }
|
||||
|
||||
void User::clearWriteBuffer() { _write_buffer.clear(); }
|
||||
|
||||
std::string User::getPrefix() const {
|
||||
return _nickname + "!" + _username + "@localhost";
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue