Merge pull request #12 from EniumRaphael/samy
No compile, split in dispatch() fixed
This commit is contained in:
commit
5cd18d313e
22 changed files with 266 additions and 81 deletions
17
Makefile
17
Makefile
|
|
@ -1,14 +1,14 @@
|
||||||
#******************************************************************************#
|
# **************************************************************************** #
|
||||||
# #
|
# #
|
||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ +:+ +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ #
|
# By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
||||||
# Updated: 2025/06/05 22:54:26 by rparodi ### ########.fr #
|
# Updated: 2025/06/08 23:06:16 by rparodi ### ########.fr #
|
||||||
# #
|
# #
|
||||||
#******************************************************************************#
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
||||||
# Name
|
# Name
|
||||||
|
|
@ -25,6 +25,7 @@ SESSION = test-irc
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
SRC = sources/channel/channel.cpp \
|
SRC = sources/channel/channel.cpp \
|
||||||
|
sources/commands/cap.cpp \
|
||||||
sources/commands/commands.cpp \
|
sources/commands/commands.cpp \
|
||||||
sources/commands/invite.cpp \
|
sources/commands/invite.cpp \
|
||||||
sources/commands/join.cpp \
|
sources/commands/join.cpp \
|
||||||
|
|
@ -35,6 +36,7 @@ SRC = sources/channel/channel.cpp \
|
||||||
sources/commands/ping.cpp \
|
sources/commands/ping.cpp \
|
||||||
sources/commands/pong.cpp \
|
sources/commands/pong.cpp \
|
||||||
sources/commands/privmsg.cpp \
|
sources/commands/privmsg.cpp \
|
||||||
|
sources/commands/userCmd.cpp \
|
||||||
sources/core/PollManager.cpp \
|
sources/core/PollManager.cpp \
|
||||||
sources/core/Server.cpp \
|
sources/core/Server.cpp \
|
||||||
sources/core/check.cpp \
|
sources/core/check.cpp \
|
||||||
|
|
@ -114,8 +116,6 @@ test: debug
|
||||||
'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""'
|
'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""'
|
||||||
@tmux attach -t $(SESSION)
|
@tmux attach -t $(SESSION)
|
||||||
|
|
||||||
# @tmux split-window -h -p 70 -t $(SESSION):0 \
|
|
||||||
# 'bash -lc "irssi -c localhost -p $(PORT) -w irc || exec yes \"irssi exit code: $?\""'
|
|
||||||
run: all
|
run: all
|
||||||
@printf '$(GREY) now running with\n\t- Port:\t\t$(GREEN)$(PORT)$(GREY)\n\t- Password:\t$(GREEN)irc$(END)\n'
|
@printf '$(GREY) now running with\n\t- Port:\t\t$(GREEN)$(PORT)$(GREY)\n\t- Password:\t$(GREEN)irc$(END)\n'
|
||||||
@if tmux has-session -t $(SESSION) 2>/dev/null; then \
|
@if tmux has-session -t $(SESSION) 2>/dev/null; then \
|
||||||
|
|
@ -123,10 +123,13 @@ run: all
|
||||||
fi
|
fi
|
||||||
@tmux new-session -d -s $(SESSION) \
|
@tmux new-session -d -s $(SESSION) \
|
||||||
'bash -lc "./$(NAME) $(PORT) irc; exec bash"'
|
'bash -lc "./$(NAME) $(PORT) irc; exec bash"'
|
||||||
@tmux split-window -v -p 50 -t $(SESSION):0 \
|
@tmux split-window -h -p 70 -t $(SESSION):0 \
|
||||||
|
'bash -lc "irssi -c localhost -p $(PORT) -w irc || exec yes \"irssi exit code: $?\""'
|
||||||
|
@tmux split-window -v -p 50 -t $(SESSION):0.1 \
|
||||||
'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""'
|
'bash -lc "nc localhost $(PORT) || exec yes \"netcat exit code: $?\""'
|
||||||
@tmux attach -t $(SESSION)
|
@tmux attach -t $(SESSION)
|
||||||
|
|
||||||
|
|
||||||
# Header
|
# Header
|
||||||
header:
|
header:
|
||||||
@clear
|
@clear
|
||||||
|
|
|
||||||
|
|
@ -149,6 +149,9 @@ package "Command" <<namespace>> {
|
||||||
class UNKNOWN {
|
class UNKNOWN {
|
||||||
+ execute(user : User, args : vector<string>) : void
|
+ execute(user : User, args : vector<string>) : void
|
||||||
}
|
}
|
||||||
|
class CAP {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
tmux
|
tmux
|
||||||
lld
|
lld
|
||||||
fd
|
fd
|
||||||
|
gh
|
||||||
] ++ (
|
] ++ (
|
||||||
if pkgs.stdenv.isLinux then [
|
if pkgs.stdenv.isLinux then [
|
||||||
valgrind
|
valgrind
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* commands.hpp :+: :+: :+: */
|
/* commands.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */
|
/* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/06/05 22:55:54 by rparodi ### ########.fr */
|
/* Updated: 2025/06/08 22:57:21 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -22,8 +22,8 @@
|
||||||
|
|
||||||
namespace cmd
|
namespace cmd
|
||||||
{
|
{
|
||||||
void dispatch(User *user, Channel *channel, Server *server, const std::string &line);
|
void dispatch(User *user, Channel *channel, Server *server, std::string &line);
|
||||||
std::vector<std::string> split(const std::string &line);
|
std::vector<std::string> split(std::string &line);
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T searchList(const std::list<T> &list, const std::string &name);
|
T searchList(const std::list<T> &list, const std::string &name);
|
||||||
|
|
||||||
|
|
@ -42,9 +42,10 @@ namespace cmd
|
||||||
virtual void execute() = 0;
|
virtual void execute() = 0;
|
||||||
virtual e_code checkArgs() = 0;
|
virtual e_code checkArgs() = 0;
|
||||||
~ACommand();
|
~ACommand();
|
||||||
ACommand(User *user, Channel *channel, Server *server, const std::string &line);
|
ACommand(User *user, Channel *channel, Server *server, std::string &line);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Cap;
|
||||||
class Invite;
|
class Invite;
|
||||||
class Join;
|
class Join;
|
||||||
class Kick;
|
class Kick;
|
||||||
|
|
@ -54,13 +55,14 @@ namespace cmd
|
||||||
class Nick;
|
class Nick;
|
||||||
class Notice;
|
class Notice;
|
||||||
class Part;
|
class Part;
|
||||||
|
class Pass;
|
||||||
class Ping;
|
class Ping;
|
||||||
class Pong;
|
class Pong;
|
||||||
class PrivMsg;
|
class PrivMsg;
|
||||||
class Quit;
|
class Quit;
|
||||||
class Topic;
|
class Topic;
|
||||||
class Unknown;
|
class Unknown;
|
||||||
class cmdUser;
|
class userCmd;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "./commands/commands.tpp"
|
#include "./commands/commands.tpp"
|
||||||
|
|
|
||||||
23
include/commands/cap.hpp
Normal file
23
include/commands/cap.hpp
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cap.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/06/08 22:08:44 by sben-tay #+# #+# */
|
||||||
|
/* Updated: 2025/06/08 22:42:04 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "commands.hpp"
|
||||||
|
#include "core.hpp"
|
||||||
|
|
||||||
|
class cmd::Cap : public ACommand {
|
||||||
|
public:
|
||||||
|
Cap(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
|
virtual void execute();
|
||||||
|
virtual e_code checkArgs();
|
||||||
|
};
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
class cmd::Invite : public ACommand {
|
class cmd::Invite : public ACommand {
|
||||||
public:
|
public:
|
||||||
Invite(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
Invite(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
class cmd::Join : public ACommand {
|
class cmd::Join : public ACommand {
|
||||||
public:
|
public:
|
||||||
Join(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
Join(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* nick.hpp :+: :+: :+: */
|
/* nick.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */
|
/* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/06/02 00:40:18 by rparodi ### ########.fr */
|
/* Updated: 2025/06/08 22:13:30 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
class cmd::Nick : public ACommand {
|
class cmd::Nick : public ACommand {
|
||||||
public:
|
public:
|
||||||
Nick(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
Nick(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs(void);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
class cmd::Notice : public ACommand {
|
class cmd::Notice : public ACommand {
|
||||||
public:
|
public:
|
||||||
Notice(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
Notice(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
class cmd::Part : public ACommand {
|
class cmd::Part : public ACommand {
|
||||||
public:
|
public:
|
||||||
Part(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
Part(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
namespace cmd {
|
namespace cmd {
|
||||||
class Pass : public ACommand {
|
class Pass : public ACommand {
|
||||||
public:
|
public:
|
||||||
Pass(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
Pass(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
class cmd::Ping : public ACommand {
|
class cmd::Ping : public ACommand {
|
||||||
public:
|
public:
|
||||||
Ping(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
Ping(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
class cmd::PrivMsg : public ACommand {
|
class cmd::PrivMsg : public ACommand {
|
||||||
public:
|
public:
|
||||||
PrivMsg(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {}
|
PrivMsg(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
virtual void execute(void);
|
virtual void execute(void);
|
||||||
virtual e_code checkArgs();
|
virtual e_code checkArgs();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
22
include/commands/userCmd.hpp
Normal file
22
include/commands/userCmd.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* userCmd.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/06/08 19:19:47 by sben-tay #+# #+# */
|
||||||
|
/* Updated: 2025/06/08 22:02:24 by sben-tay ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "commands.hpp"
|
||||||
|
|
||||||
|
class cmd::userCmd : public ACommand {
|
||||||
|
public:
|
||||||
|
userCmd(User *user, Channel *channel, Server *server, std::string &line) : ACommand(user, channel, server, line) {}
|
||||||
|
virtual void execute();
|
||||||
|
virtual e_code checkArgs();
|
||||||
|
};
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
/******************************************************************************/
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* user.hpp :+: :+: :+: */
|
/* user.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */
|
/* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/29 12:37:11 by rparodi ### ########.fr */
|
/* Updated: 2025/06/08 21:59:53 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/******************************************************************************/
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
@ -26,6 +26,7 @@ class User
|
||||||
std::string _username;
|
std::string _username;
|
||||||
bool _hasNick;
|
bool _hasNick;
|
||||||
bool _hasUser;
|
bool _hasUser;
|
||||||
|
bool _hasPass; // to check if the user has sent a PASS command
|
||||||
|
|
||||||
public:
|
public:
|
||||||
User(short unsigned fd);
|
User(short unsigned fd);
|
||||||
|
|
@ -40,4 +41,17 @@ class User
|
||||||
void setNickname(const std::string &nickname);
|
void setNickname(const std::string &nickname);
|
||||||
void setUsername(const std::string &username);
|
void setUsername(const std::string &username);
|
||||||
void checkRegistration();
|
void checkRegistration();
|
||||||
|
|
||||||
|
// setters and getters
|
||||||
|
void setHasNick(bool value);
|
||||||
|
void setHasUser(bool value);
|
||||||
|
void setHasPass(bool value);
|
||||||
|
bool getHasPass() const;
|
||||||
|
std::string getNickname() const;
|
||||||
|
|
||||||
|
bool hasDataToSend() const;
|
||||||
|
std::string getWriteBuffer() const;
|
||||||
|
void clearWriteBuffer();
|
||||||
|
|
||||||
|
std::string getPrefix() const;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
33
sources/commands/cap.cpp
Normal file
33
sources/commands/cap.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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:45:35 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "cap.hpp"
|
||||||
|
#include "logs.hpp"
|
||||||
|
|
||||||
|
using namespace cmd;
|
||||||
|
|
||||||
|
e_code Cap::checkArgs() {
|
||||||
|
return _PARSING_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cmd::Cap::execute() {
|
||||||
|
DEBUG_MSG("coucou");
|
||||||
|
if (this->checkArgs() != _PARSING_OK)
|
||||||
|
return;
|
||||||
|
DEBUG_MSG("coucou");
|
||||||
|
if (_args.size() >= 2 && _args[1] == "LS") {
|
||||||
|
std::string reply = "CAP * LS :\r\n";
|
||||||
|
_sender->appendToWriteBuffer(reply);
|
||||||
|
DEBUG_MSG("Replied to CAP LS");
|
||||||
|
}
|
||||||
|
DEBUG_MSG("coucou");
|
||||||
|
}
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* commands.cpp :+: :+: :+: */
|
/* 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 #+# #+# */
|
/* Created: 2025/05/24 16:11:56 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/06/03 16:46:58 by rparodi ### ########.fr */
|
/* Updated: 2025/06/08 23:26:07 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,6 +14,11 @@
|
||||||
#include "logs.hpp"
|
#include "logs.hpp"
|
||||||
#include "pass.hpp"
|
#include "pass.hpp"
|
||||||
#include "ping.hpp"
|
#include "ping.hpp"
|
||||||
|
#include "nick.hpp"
|
||||||
|
#include "userCmd.hpp"
|
||||||
|
#include "cap.hpp"
|
||||||
|
#include <cctype>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief To send the line where a command is invoqued to execute
|
* @brief To send the line where a command is invoqued to execute
|
||||||
|
|
@ -22,21 +27,26 @@
|
||||||
* @param channel channel where the command is sent
|
* @param channel channel where the command is sent
|
||||||
* @param line line send by the user
|
* @param line line send by the user
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> cmd::split(const std::string &line) {
|
|
||||||
|
std::vector<std::string> cmd::split(std::string &line) {
|
||||||
std::vector<std::string> args;
|
std::vector<std::string> args;
|
||||||
std::string arg;
|
size_t start = 0;
|
||||||
if (line.empty())
|
size_t end;
|
||||||
return args;
|
|
||||||
size_t pos = line.find(' ');
|
while ((end = line.find(' ', start)) != std::string::npos) {
|
||||||
while (pos != std::string::npos) {
|
std::string arg = line.substr(start, end - start);
|
||||||
arg = line.substr(0, pos);
|
|
||||||
if (!arg.empty()) {
|
if (!arg.empty()) {
|
||||||
|
for (size_t i = 0; i < arg.length(); ++i)
|
||||||
|
arg[i] = std::tolower(arg[i]);
|
||||||
args.push_back(arg);
|
args.push_back(arg);
|
||||||
}
|
}
|
||||||
pos = line.find(' ');
|
start = end + 1;
|
||||||
}
|
}
|
||||||
if (!line.empty()) {
|
if (start < line.length()) {
|
||||||
args.push_back(line);
|
std::string arg = line.substr(start);
|
||||||
|
for (size_t i = 0; i < arg.length(); ++i)
|
||||||
|
arg[i] = std::tolower(arg[i]);
|
||||||
|
args.push_back(arg);
|
||||||
}
|
}
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
@ -49,11 +59,12 @@ std::vector<std::string> cmd::split(const std::string &line) {
|
||||||
* @param server Server where the command is sent
|
* @param server Server where the command is sent
|
||||||
* @param line input line from the user
|
* @param line input line from the user
|
||||||
*/
|
*/
|
||||||
void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::string &line) {
|
void cmd::dispatch(::User *user, Channel *channel, Server *server, std::string &line) {
|
||||||
DEBUG_MSG("in dispatch");
|
DEBUG_MSG("in dispatch");
|
||||||
std::vector<std::string> args = cmd::split(line);
|
std::vector<std::string> args = cmd::split(line);
|
||||||
|
DEBUG_MSG("in dispatch");
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
ERROR_MSG("Empty line");
|
DEBUG_MSG("Empty line");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string command_name = args[0];
|
std::string command_name = args[0];
|
||||||
|
|
@ -61,13 +72,14 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
|
||||||
WARNING_MSG("No command found in line: " << line);
|
WARNING_MSG("No command found in line: " << line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (command_name[0] == '/') {
|
std::cout << command_name << std::endl;
|
||||||
command_name.erase(0, 1);
|
DEBUG_MSG(command_name);
|
||||||
} else {
|
|
||||||
WARNING_MSG("Command does not start with '/': " << command_name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (command_name[0]) {
|
switch (command_name[0]) {
|
||||||
|
case 'c':
|
||||||
|
if (command_name == "cap") {
|
||||||
|
Cap(user, channel, server, line).execute();
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
// if (command_name == "invite") {
|
// if (command_name == "invite") {
|
||||||
// Invite(user, channel, server, line).execute();
|
// Invite(user, channel, server, line).execute();
|
||||||
|
|
@ -94,11 +106,11 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
|
||||||
// }
|
// }
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
// if (command_name == "nick") {
|
if (command_name == "NICK") {
|
||||||
// Nick(user, channel, server, line).execute();
|
Nick(user, channel, server, line).execute();
|
||||||
// } else if (command_name == "notice") {
|
// } else if (command_name == "notice") {
|
||||||
// Notice(user, channel, server, line).execute();
|
// Notice(user, channel, server, line).execute();
|
||||||
// }
|
}
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
if (command_name == "pass") {
|
if (command_name == "pass") {
|
||||||
|
|
@ -111,6 +123,11 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
|
||||||
// Part(user, channel, server, line).execute();
|
// Part(user, channel, server, line).execute();
|
||||||
// }
|
// }
|
||||||
break;
|
break;
|
||||||
|
case 'u':
|
||||||
|
if (command_name == "user") {
|
||||||
|
userCmd(user, channel, server, line).execute();
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
WARNING_MSG("Unknown command: " << command_name);
|
WARNING_MSG("Unknown command: " << command_name);
|
||||||
}
|
}
|
||||||
|
|
@ -120,7 +137,7 @@ void cmd::dispatch(::User *user, Channel *channel, Server *server, const std::st
|
||||||
(void)line;
|
(void)line;
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd::ACommand::ACommand(::User *user, ::Channel *channel, ::Server *server, const std::string &line) : _sender(user), _channel(channel), _server(server) {
|
cmd::ACommand::ACommand(::User *user, ::Channel *channel, ::Server *server, std::string &line) : _sender(user), _channel(channel), _server(server) {
|
||||||
DEBUG_MSG("ACommand constructor called");
|
DEBUG_MSG("ACommand constructor called");
|
||||||
_args = split(line);
|
_args = split(line);
|
||||||
_command = _args.at(0);
|
_command = _args.at(0);
|
||||||
|
|
|
||||||
|
|
@ -3,32 +3,30 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* nick.cpp :+: :+: :+: */
|
/* 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 #+# #+# */
|
/* 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 "nick.hpp"
|
||||||
#include "commands.hpp"
|
|
||||||
#include "logs.hpp"
|
#include "logs.hpp"
|
||||||
|
|
||||||
using namespace cmd;
|
using namespace cmd;
|
||||||
|
|
||||||
e_code Nick::checkArgs() {
|
e_code cmd::Nick::checkArgs() {
|
||||||
if (this->_uTarget->isRegistered() == false) {
|
if (_args.size() < 2 || _args[1].empty()) {
|
||||||
WARNING_MSG("User is not registered for Nick command");
|
WARNING_MSG("Nick: Not enough arguments");
|
||||||
INFO_MSG("You can only Nick registered users");
|
return ERR_NONICKNAMEGIVEN;
|
||||||
return ERR_NOSUCHNICK;
|
|
||||||
}
|
}
|
||||||
if (_args.size() < 2) {
|
if (_sender->isRegistered()) {
|
||||||
WARNING_MSG("Not enough arguments for Nick command");
|
WARNING_MSG(_sender->getName() << " is already registered");
|
||||||
return ERR_NEEDMOREPARAMS;
|
return ERR_ALREADYREGISTERED;
|
||||||
}
|
}
|
||||||
_uTarget = searchList(this->_users, _args.at(1));
|
User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin
|
||||||
if (this->_uTarget != NULL) {
|
if (existing != NULL) {
|
||||||
WARNING_MSG(_uTarget->getName() << " is already taken")
|
WARNING_MSG("Nick already in use: " << _args[1]);
|
||||||
return ERR_NICKNAMEINUSE;
|
return ERR_NICKNAMEINUSE;
|
||||||
}
|
}
|
||||||
return _PARSING_OK;
|
return _PARSING_OK;
|
||||||
|
|
@ -38,10 +36,13 @@ e_code Nick::checkArgs() {
|
||||||
* @brief Execute the Nick command
|
* @brief Execute the Nick command
|
||||||
* @note To change the nickname of the user
|
* @note To change the nickname of the user
|
||||||
*/
|
*/
|
||||||
void Nick::execute() {
|
void cmd::Nick::execute() {
|
||||||
if (checkArgs() == _PARSING_OK) {
|
if (checkArgs() == _PARSING_OK) {
|
||||||
ERROR_MSG("Invalid arguments for Nick command (see warning message)");
|
ERROR_MSG("Invalid arguments for Nick command (see warning message)");
|
||||||
return;
|
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 :+: :+: :+: */
|
/* 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 #+# #+# */
|
/* 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
|
* @brief Execute the Pass command
|
||||||
* @note To connect a user with the password
|
* @note To connect a user with the password
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void Pass::execute() {
|
void Pass::execute() {
|
||||||
if (checkArgs() == _PARSING_OK) {
|
if (checkArgs() == _PARSING_OK) {
|
||||||
ERROR_MSG("Invalid arguments for Pass command (see warning message)");
|
ERROR_MSG("Invalid arguments for Pass command (see warning message)");
|
||||||
|
|
@ -44,5 +45,5 @@ void Pass::execute() {
|
||||||
ERROR_MSG("The password is incorrect");
|
ERROR_MSG("The password is incorrect");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_sender->setRegistered();
|
_sender->setHasPass(true);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ping.cpp :+: :+: :+: */
|
/* 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 #+# #+# */
|
/* 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
|
* @brief Execute the Ping
|
||||||
* @note To send a private message to a user / a channel
|
* @note To send a private message to a user / a channel
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void Ping::execute() {
|
void Ping::execute() {
|
||||||
clock_t start = clock() / CLOCKS_PER_SEC;
|
clock_t start = clock() / CLOCKS_PER_SEC;
|
||||||
if (checkArgs() == _PARSING_OK) {
|
if (checkArgs() == _PARSING_OK) {
|
||||||
|
|
@ -38,5 +39,4 @@ void Ping::execute() {
|
||||||
}
|
}
|
||||||
clock_t diff = Pong().answer(start);
|
clock_t diff = Pong().answer(start);
|
||||||
INFO_MSG(diff);
|
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 :+: :+: :+: */
|
/* 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 #+# #+# */
|
/* 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"
|
#include "core.hpp"
|
||||||
|
|
||||||
// Constructor
|
// 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
|
* @brief Getter for the fd
|
||||||
|
|
@ -82,9 +83,13 @@ void User::appendToWriteBuffer(const std::string &data)
|
||||||
// Check registration
|
// Check registration
|
||||||
void User::checkRegistration()
|
void User::checkRegistration()
|
||||||
{
|
{
|
||||||
if (!_registered && _hasNick && _hasUser)
|
if (!_registered && _hasNick && _hasUser && _hasPass)
|
||||||
{
|
{
|
||||||
_registered = true;
|
_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;
|
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