This commit is contained in:
Ladebeze66 2024-05-28 15:27:30 +02:00
parent 0fa9a1b3ea
commit 79a4688520
24 changed files with 604 additions and 469 deletions

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 18:09:05 by fgras-ca #+# #+# */
/* Updated: 2024/05/21 19:46:40 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:16:57 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,15 +34,15 @@ class CommandHandler;
class AdditionalCommands
{
private:
Server *_server;
Server *_server;
CommandHandler *_commandhandler;
public:
AdditionalCommands(Server *server);
void processCommand(Client *client, const std::string &command);
void broadcastChannelList(Client *client, Server *server);
void handlePartCommand(Server *server, Client *client, const std::string &command);
void handleNickCommand(Server *server, Client *client, const std::string &command);
void handlePrivmsgCommand(Server *server, Client *client, const std::string &command);
AdditionalCommands(Server *server);
void processCommand(Client *client, const std::string &command);
void broadcastChannelList(Client *client, Server *server);
void handlePartCommand(Server *server, Client *client, const std::string &command);
void handleNickCommand(Server *server, Client *client, const std::string &command);
void handlePrivmsgCommand(Server *server, Client *client, const std::string &command);
};
#endif // ADDITIONALCOMMANDS_HPP

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:41:35 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 11:13:12 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:16:55 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,42 +29,42 @@ class Client;
class Channel
{
public:
Channel(const std::string &name);
~Channel();
Channel(const std::string &name);
~Channel();
const std::string &getName() const;
void addClient(Client *client);
void removeClient(Client *client);
bool isEmpty() const;
const std::vector<Client *> &getClients() const;
void addOperator(Client *client);
bool isOperator(Client *client) const;
bool hasClient(Client *client) const;
void broadcast(const std::string &message, Client *_client, Server *_server);
const std::string &getName() const;
void addClient(Client *client);
void removeClient(Client *client);
bool isEmpty() const;
const std::vector<Client *> &getClients() const;
void addOperator(Client *client);
bool isOperator(Client *client) const;
bool hasClient(Client *client) const;
void broadcast(const std::string &message, Client *_client, Server *_server);
// Ajouts
bool isBanned(Client *client) const;
bool isFull() const;
bool isInviteOnly() const;
bool isInvited(Client *client) const;
bool checkKey(const std::string &key) const;
const std::string &getTopic() const;
const std::string &getTopicSetter() const;
time_t getTopicTime() const;
void setTopic(const std::string &topic, const std::string &setter);
// Ajouts
bool isBanned(Client *client) const;
bool isFull() const;
bool isInviteOnly() const;
bool isInvited(Client *client) const;
bool checkKey(const std::string &key) const;
const std::string &getTopic() const;
const std::string &getTopicSetter() const;
time_t getTopicTime() const;
void setTopic(const std::string &topic, const std::string &setter);
private:
std::string _name;
std::vector<Client *> _clients;
std::vector<Client *> _operators;
std::set<Client *> _bannedClients;
std::set<Client *> _invitedClients;
std::string _key;
std::string _topic;
std::string _topicSetter;
time_t _topicTime;
size_t _clientLimit;
bool _inviteOnly;
std::string _name;
std::vector<Client *> _clients;
std::vector<Client *> _operators;
std::set<Client *> _bannedClients;
std::set<Client *> _invitedClients;
std::string _key;
std::string _topic;
std::string _topicSetter;
time_t _topicTime;
size_t _clientLimit;
bool _inviteOnly;
};
#endif // CHANNEL_HPP

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:15:42 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 14:09:24 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:17:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,41 +18,41 @@
class Client
{
public:
Client(int fd, const std::string &nickname, const std::string &user, const std::string &host, const std::string &password, const std::string &realname);
Client(int fd, const std::string &nickname, const std::string &user, const std::string &host, const std::string &password, const std::string &realname);
int getFd() const;
const std::string &getNickname() const;
void setNickname(const std::string &nickname);
const std::string &getUser() const;
void setUser(const std::string &user);
const std::string &getHost() const;
void setHost(const std::string &host);
const std::string &getPassword() const;
void setPassword(const std::string &password);
const std::string &getRealName() const;
void setRealName(const std::string &realname);
bool isAuthenticated() const;
void authenticate();
bool isOperator() const;
void setOperator(bool isOperator);
// Ajout des méthodes pour la gestion du statut "away"
bool isAway() const;
const std::string &getAwayMessage() const;
void setAwayMessage(const std::string &message);
void setAway(bool away);
int getFd() const;
const std::string &getNickname() const;
void setNickname(const std::string &nickname);
const std::string &getUser() const;
void setUser(const std::string &user);
const std::string &getHost() const;
void setHost(const std::string &host);
const std::string &getPassword() const;
void setPassword(const std::string &password);
const std::string &getRealName() const;
void setRealName(const std::string &realname);
bool isAuthenticated() const;
void authenticate();
bool isOperator() const;
void setOperator(bool isOperator);
// Ajout des méthodes pour la gestion du statut "away"
bool isAway() const;
const std::string &getAwayMessage() const;
void setAwayMessage(const std::string &message);
void setAway(bool away);
private:
int _fd;
std::string _nickname;
std::string _user;
std::string _host;
std::string _password;
std::string _realname;
bool _authenticated;
bool _operator;
bool _away;
std::string _awayMessage;
int _fd;
std::string _nickname;
std::string _user;
std::string _host;
std::string _password;
std::string _realname;
bool _authenticated;
bool _operator;
bool _away;
std::string _awayMessage;
};
#endif // CLIENT_HPP

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:30:07 by fgras-ca #+# #+# */
/* Updated: 2024/05/21 18:01:47 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:17:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,14 +40,14 @@ class Client;
class ClientManager
{
public:
ClientManager(Server *server);
void acceptClient();
void handleClient(int client_fd);
void removeClient(int client_fd);
ClientManager(Server *server);
void acceptClient();
void handleClient(int client_fd);
void removeClient(int client_fd);
private:
Server *_server;
Server *_server;
};
#endif

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:14:12 by fgras-ca #+# #+# */
/* Updated: 2024/05/21 20:15:58 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:17:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,22 +36,22 @@ class AdditionalCommands;
class CommandHandler
{
private:
Server *_server;
Server *_server;
AdditionalCommands *_additionalCommands;
public:
CommandHandler(Server *server);
bool isValidNickname(const std::string& nickname);
bool isNicknameInUse(const std::string& nickname);
void handleNick(Client* client, const std::vector<std::string>& tokens);
CommandHandler(Server *server);
bool isValidNickname(const std::string& nickname);
bool isNicknameInUse(const std::string& nickname);
void handleNick(Client* client, const std::vector<std::string>& tokens);
void handleUser(Client* client, const std::vector<std::string>& tokens);
void handlePingCommand(Client* client, const std::vector<std::string>& tokens);
void handleCommand(Client *client, const std::string &command);
void handleCapCommand(Client* client, const std::vector<std::string>& tokens);
void handlePassCommand(Client* client, const std::vector<std::string>& tokens);
void handleQuitCommand(Client* client, const std::vector<std::string>& tokens);
void handleErrorCommand(Client* client, const std::string &message);
void handleUser(Client* client, const std::vector<std::string>& tokens);
void handlePingCommand(Client* client, const std::vector<std::string>& tokens);
void handleCommand(Client *client, const std::string &command);
void handleCapCommand(Client* client, const std::vector<std::string>& tokens);
void handlePassCommand(Client* client, const std::vector<std::string>& tokens);
void handleQuitCommand(Client* client, const std::vector<std::string>& tokens);
void handleErrorCommand(Client* client, const std::string &message);
};

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 19:51:08 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 11:09:22 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:17:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,11 +22,11 @@
class JoinHandler
{
public:
void handleJoinCommand(Client *client, const std::string &channelName, Server *server);
void handleJoinCommand(Client *client, const std::string &channelName, Server *server);
private:
void sendJoinSuccess(Client *client, Channel *channel, Server *server);
std::string getUsersList(Channel *channel);
void sendJoinSuccess(Client *client, Channel *channel, Server *server);
std::string getUsersList(Channel *channel);
};
#endif // JOIN_HPP

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:47 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 13:56:19 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 15:26:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,78 +34,84 @@
// Fonctions pour générer les réponses RPL
inline std::string RPL_WELCOME(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 001 " << CLIENT_NICK(client)
<< " :Welcome to the Internet Relay Network " << CLIENT_NICK(client)
<< "!" << CLIENT_USER(client) << "@" << CLIENT_HOST(client) << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 001 " << CLIENT_NICK(client)
<< " :Welcome to the Internet Relay Network " << CLIENT_NICK(client)
<< "!" << CLIENT_USER(client) << "@" << CLIENT_HOST(client) << "\r\n";
return oss.str();
}
inline std::string RPL_YOURHOST(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 002 " << CLIENT_FD(client)
<< " :Your host is " << SERVER_NAME << ", running version " << SERVER_VERSION << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 002 " << CLIENT_FD(client)
<< " :Your host is " << SERVER_NAME << ", running version " << SERVER_VERSION << "\r\n";
return oss.str();
}
inline std::string RPL_CREATED(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 003 " << CLIENT_FD(client)
<< " :This server was created " << __DATE__ << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 003 " << CLIENT_FD(client)
<< " :This server was created " << __DATE__ << "\r\n";
return oss.str();
}
inline std::string RPL_MYINFO(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 004 " << CLIENT_FD(client) << " "
<< SERVER_NAME << " " << SERVER_VERSION << " "
<< USER_MODES << " " << CHANNEL_MODES << " "
<< CHANNEL_MODES_WITH_PARAMS << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 004 " << CLIENT_FD(client) << " "
<< SERVER_NAME << " " << SERVER_VERSION << " "
<< USER_MODES << " " << CHANNEL_MODES << " "
<< CHANNEL_MODES_WITH_PARAMS << "\r\n";
return oss.str();
}
inline std::string RPL_ISUPPORT(Client* client, const std::string& tokens) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 005 " << CLIENT_FD(client)
<< " " << tokens << " :are supported by this server\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 005 " << CLIENT_FD(client)
<< " " << tokens << " :are supported by this server\r\n";
return oss.str();
}
inline std::string RPL_UMODEIS(int clientFd, const std::string& modes) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 221 " << clientFd << " :" << modes << "\r\n";
return oss.str();
}
inline std::string RPL_AWAY(int clientFd, const std::string& target, const std::string& message) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 301 " << clientFd << " " << target << " :" << message << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 301 " << clientFd << " " << target << " :" << message << "\r\n";
return oss.str();
}
// WHOIS Command RPLs
inline std::string RPL_WHOISUSER(int clientFd, Client* target)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 311 " << clientFd << " " << CLIENT_NICK(target) << " "
<< CLIENT_USER(target) << " " << CLIENT_HOST(target) << " * :" << CLIENT_REALNAME(target) << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 311 " << clientFd << " " << CLIENT_NICK(target) << " "
<< CLIENT_USER(target) << " " << CLIENT_HOST(target) << " * :" << CLIENT_REALNAME(target) << "\r\n";
return oss.str();
}
inline std::string RPL_WHOISSERVER(int clientFd, const std::string& targetNick, const std::string& serverInfo)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 312 " << clientFd << " " << targetNick << " " << SERVER_NAME
<< " :" << serverInfo << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 312 " << clientFd << " " << targetNick << " " << SERVER_NAME
<< " :" << serverInfo << "\r\n";
return oss.str();
}
inline std::string RPL_ENDOFWHO(int clientFd, const std::string& channel)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 315 " << clientFd << " " << channel << " :End of /WHO list.\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 315 " << clientFd << " " << channel << " :End of /WHO list.\r\n";
return oss.str();
}
inline std::string RPL_ENDOFWHOIS(int clientFd, const std::string& targetNick)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 318 " << clientFd << " " << targetNick << " :End of /WHOIS list.\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 318 " << clientFd << " " << targetNick << " :End of /WHOIS list.\r\n";
return oss.str();
}
// RPL Channel List Messages
@ -126,255 +132,338 @@ inline std::string RPL_LISTEND(int clientFd)
// RPL Mode Messages
inline std::string RPL_CHANNELMODEIS(int clientFd, const std::string& channel, const std::string& mode)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 324 " << clientFd << " " << channel << " " << mode << "\r\n";
return oss.str();
}
inline std::string RPL_CREATIONTIME(int clientFd, const std::string& channel, time_t creationTime) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 329 " << clientFd << " " << channel << " " << creationTime << "\r\n";
return oss.str();
}
inline std::string RPL_NOTOPIC(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 324 " << clientFd << " " << channel << " " << mode << "\r\n";
oss << ":" << SERVER_NAME << " 331 " << CLIENT_NICK(client) << " " << channel
<< " :No topic is set\r\n";
return oss.str();
}
inline std::string RPL_TOPIC(Client* client, const std::string& channel, const std::string& topic) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 332 " << CLIENT_NICK(client) << " " << channel << " :" << topic << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 332 " << CLIENT_NICK(client) << " " << channel << " :" << topic << "\r\n";
return oss.str();
}
inline std::string RPL_TOPICWHOTIME(Client* client, const std::string& channel, const std::string& setter, time_t setTime) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 333 " << CLIENT_NICK(client) << " " << channel << " " << setter << " " << setTime << "\r\n";
return oss.str();
}
inline std::string RPL_INVITELIST(int clientFd, const std::string& channel, const std::string& inviteMask) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 336 " << clientFd << " " << channel << " " << inviteMask << "\r\n";
return oss.str();
}
inline std::string RPL_ENDOFINVITELIST(int clientFd, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 337 " << clientFd << " " << channel << " :End of channel invite list\r\n";
return oss.str();
}
// Ajoutez cette fonction pour RPL_INVITING
inline std::string RPL_INVITING(Client* client, const std::string& channel, const std::string& nick) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 333 " << CLIENT_NICK(client) << " " << channel << " " << setter << " " << setTime << "\r\n";
oss << ":" << SERVER_NAME << " 341 " << CLIENT_NICK(client) << " " << nick << " " << channel << "\r\n";
return oss.str();
}
inline std::string RPL_EXCEPTLIST(int clientFd, const std::string& channel, const std::string& exceptionMask) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 348 " << clientFd << " " << channel << " " << exceptionMask << "\r\n";
return oss.str();
}
inline std::string RPL_ENDOFEXCEPTLIST(int clientFd, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 349 " << clientFd << " " << channel << " :End of channel exception list\r\n";
return oss.str();
}
inline std::string RPL_NAMREPLY(Client* client, const std::string& channel, const std::string& users) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 353 " << CLIENT_NICK(client) << " = " << channel << " :" << users << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 353 " << CLIENT_NICK(client) << " = " << channel << " :" << users << "\r\n";
return oss.str();
}
// WHO Command RPLs
inline std::string RPL_WHOREPLY(const std::string& channel, Client* target)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 352 " << CLIENT_NICK(target) << " " << channel << " "
<< CLIENT_USER(target) << " " << CLIENT_HOST(target) << " " << SERVER_NAME << " "
<< CLIENT_NICK(target) << " H :0 " << CLIENT_REALNAME(target) << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 352 " << CLIENT_NICK(target) << " " << channel << " "
<< CLIENT_USER(target) << " " << CLIENT_HOST(target) << " " << SERVER_NAME << " "
<< CLIENT_NICK(target) << " H :0 " << CLIENT_REALNAME(target) << "\r\n";
return oss.str();
}
inline std::string RPL_ENDOFNAMES(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 366 " << CLIENT_NICK(client) << " " << channel << " :End of /NAMES list\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 366 " << CLIENT_NICK(client) << " " << channel << " :End of /NAMES list\r\n";
return oss.str();
}
inline std::string RPL_BANLIST(int clientFd, const std::string& channel, const std::string& banMask) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 367 " << clientFd << " " << channel << " " << banMask << "\r\n";
return oss.str();
}
inline std::string RPL_ENDOFBANLIST(int clientFd, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 368 " << clientFd << " " << channel << " :End of channel ban list\r\n";
return oss.str();
}
inline std::string RPL_MOTD(Client* client, const std::string& line) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 372 " << CLIENT_FD(client)
<< " :- " << line << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 372 " << CLIENT_FD(client)
<< " :- " << line << "\r\n";
return oss.str();
}
inline std::string RPL_MOTDSTART(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 375 " << CLIENT_FD(client)
<< " :- " << SERVER_NAME << " Message of the day - \r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 375 " << CLIENT_FD(client)
<< " :- " << SERVER_NAME << " Message of the day - \r\n";
return oss.str();
}
inline std::string RPL_ENDOFMOTD(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 376 " << CLIENT_FD(client)
<< " :End of /MOTD command.\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 376 " << CLIENT_FD(client)
<< " :End of /MOTD command.\r\n";
return oss.str();
}
inline std::string ERR_NOSUCHNICK(int clientFd, const std::string& nick)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 401 " << clientFd << " " << nick << " :No such nick/channel\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 401 " << clientFd << " " << nick << " :No such nick/channel\r\n";
return oss.str();
}
inline std::string ERR_NOSUCHCHANNEL(int clientFd, const std::string& channel)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 403 " << clientFd << " " << channel << " :No such channel\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 403 " << clientFd << " " << channel << " :No such channel\r\n";
return oss.str();
}
inline std::string ERR_CANNOTSENDTOCHAN(int clientFd, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 404 " << clientFd << " " << channel << " :Cannot send to channel\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 404 " << clientFd << " " << channel << " :Cannot send to channel\r\n";
return oss.str();
}
inline std::string ERR_TOOMANYCHANNELS(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 405 " << CLIENT_NICK(client) << " " << channel << " :You have joined too many channels\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 405 " << CLIENT_NICK(client) << " " << channel << " :You have joined too many channels\r\n";
return oss.str();
}
// RPL Error Messages
inline std::string ERR_NOORIGIN(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 409 " << CLIENT_FD(client) << " :No origin specified\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 409 " << CLIENT_FD(client) << " :No origin specified\r\n";
return oss.str();
}
inline std::string ERR_NORECIPIENT(int clientFd, const std::string& command) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 411 " << clientFd << " :No recipient given (" << command << ")\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 411 " << clientFd << " :No recipient given (" << command << ")\r\n";
return oss.str();
}
inline std::string ERR_NOTEXTTOSEND(int clientFd) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 412 " << clientFd << " :No text to send\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 412 " << clientFd << " :No text to send\r\n";
return oss.str();
}
inline std::string ERR_UNKNOWNCOMMAND(Client* client, const std::string& command) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 421 " << CLIENT_NICK(client) << " " << command << " :Unknown command\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 421 " << CLIENT_NICK(client) << " " << command << " :Unknown command\r\n";
return oss.str();
}
inline std::string ERR_NOMOTD(Client* client) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 422 " << CLIENT_FD(client)
<< " :MOTD File is missing\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 422 " << CLIENT_FD(client)
<< " :MOTD File is missing\r\n";
return oss.str();
}
inline std::string ERR_NONICKNAMEGIVEN(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 431 " << CLIENT_FD(client) << " :No nickname given\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 431 " << CLIENT_FD(client) << " :No nickname given\r\n";
return oss.str();
}
inline std::string ERR_ERRONEUSNICKNAME(Client* client, const std::string& nickname)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 432 " << CLIENT_FD(client) << " " << nickname << " :Erroneous nickname\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 432 " << CLIENT_FD(client) << " " << nickname << " :Erroneous nickname\r\n";
return oss.str();
}
inline std::string ERR_NICKNAMEINUSE(Client* client, const std::string& nickname)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 433 " << CLIENT_FD(client) << " " << nickname << " :Nickname is already in use\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 433 " << CLIENT_FD(client) << " " << nickname << " :Nickname is already in use\r\n";
return oss.str();
}
inline std::string ERR_USERNOTINCHANNEL(int clientFd, const std::string& nick, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 441 " << clientFd << " " << nick << " " << channel
<< " :They aren't on that channel\r\n";
return oss.str();
}
inline std::string ERR_NOTONCHANNEL(int clientFd, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 442 " << clientFd << " " << channel << " :You're not on that channel\r\n";
return oss.str();
}
// Ajoutez cette fonction pour ERR_USERONCHANNEL
inline std::string ERR_USERONCHANNEL(Client* client, const std::string& nick, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 442 " << clientFd << " " << channel << " :You're not on that channel\r\n";
oss << ":" << SERVER_NAME << " 443 " << CLIENT_NICK(client) << " " << nick << " " << channel
<< " :is already on channel\r\n";
return oss.str();
}
inline std::string ERR_NOTREGISTERED(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 451 " << CLIENT_FD(client) << " :You have not registered\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 451 " << CLIENT_FD(client) << " :You have not registered\r\n";
return oss.str();
}
//RPL Password
// RPL Error Messages
inline std::string ERR_NEEDMOREPARAMS(Client* client, const std::string& command)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 461 " << CLIENT_FD(client) << " " << command << " :Not enough parameters\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 461 " << CLIENT_FD(client) << " " << command << " :Not enough parameters\r\n";
return oss.str();
}
inline std::string ERR_ALREADYREGISTERED(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 462 " << CLIENT_FD(client) << " :You may not reregister\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 462 " << CLIENT_FD(client) << " :You may not reregister\r\n";
return oss.str();
}
inline std::string ERR_PASSWDMISMATCH(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 464 " << CLIENT_FD(client) << " :Password incorrect\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 464 " << CLIENT_FD(client) << " :Password incorrect\r\n";
return oss.str();
}
inline std::string ERR_CHANNELISFULL(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 471 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (channel is full)\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 471 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (channel is full)\r\n";
return oss.str();
}
inline std::string ERR_INVITEONLYCHAN(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 473 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (invite only)\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 473 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (invite only)\r\n";
return oss.str();
}
inline std::string ERR_BANNEDFROMCHAN(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 474 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (banned)\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 474 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (banned)\r\n";
return oss.str();
}
inline std::string ERR_BADCHANNELKEY(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 475 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (incorrect key)\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 475 " << CLIENT_NICK(client) << " " << channel << " :Cannot join channel (incorrect key)\r\n";
return oss.str();
}
inline std::string ERR_BADCHANMASK(Client* client, const std::string& channel) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 476 " << CLIENT_NICK(client) << " " << channel << " :Bad channel mask\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 476 " << CLIENT_NICK(client) << " " << channel << " :Bad channel mask\r\n";
return oss.str();
}
inline std::string RPL_NOCHANMODES(int clientFd, const std::string& channel)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 477 " << clientFd << " " << channel << " :Channel doesn't support modes\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 477 " << clientFd << " " << channel << " :Channel doesn't support modes\r\n";
return oss.str();
}
inline std::string ERR_CHANOPRIVSNEEDED(int clientFd, const std::string& channel)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 482 " << clientFd << " " << channel << " :You're not channel operator\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 482 " << clientFd << " " << channel << " :You're not channel operator\r\n";
return oss.str();
}
inline std::string ERR_UMODEUNKNOWNFLAG(int clientFd) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 501 " << clientFd << " :Unknown MODE flag\r\n";
return oss.str();
}
inline std::string ERR_USERSDONTMATCH(int clientFd) {
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 502 " << clientFd << " :Cannot change mode for other users\r\n";
return oss.str();
}
// PONG Reply
inline std::string RPL_PONG(const std::string& token)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " PONG " << SERVER_NAME << " " << token << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " PONG " << SERVER_NAME << " " << token << "\r\n";
return oss.str();
}
// CAP Command RPLs
inline std::string RPL_CAP(int clientFd, const std::string& subcommand, const std::string& capabilities)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " CAP " << clientFd << " " << subcommand << " :" << capabilities << "\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " CAP " << clientFd << " " << subcommand << " :" << capabilities << "\r\n";
return oss.str();
}
inline std::string RPL_PASSACCEPTED(Client* client)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " NOTICE " << CLIENT_FD(client)
<< " :Password accepted!\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " NOTICE " << CLIENT_FD(client)
<< " :Password accepted!\r\n";
return oss.str();
}
// Add this function to handle the CAP END response
inline std::string RPL_CAPEND(int clientFd)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " CAP " << clientFd << " END\r\n";
return oss.str();
std::ostringstream oss;
oss << ":" << SERVER_NAME << " CAP " << clientFd << " END\r\n";
return oss.str();
}
#endif // RPL_HPP

BIN
ft_irc3/ircserv Executable file

Binary file not shown.

View File

Binary file not shown.

BIN
ft_irc3/obj/Channel.o Normal file

Binary file not shown.

BIN
ft_irc3/obj/Client.o Normal file

Binary file not shown.

BIN
ft_irc3/obj/ClientManager.o Normal file

Binary file not shown.

Binary file not shown.

BIN
ft_irc3/obj/Join.o Normal file

Binary file not shown.

BIN
ft_irc3/obj/Server.o Normal file

Binary file not shown.

BIN
ft_irc3/obj/Utils.o Normal file

Binary file not shown.

BIN
ft_irc3/obj/Welcome.o Normal file

Binary file not shown.

BIN
ft_irc3/obj/Who.o Normal file

Binary file not shown.

BIN
ft_irc3/obj/main.o Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:17:42 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 14:10:21 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:32:11 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,51 +15,97 @@
Client::Client(int fd, const std::string &nickname, const std::string &user, const std::string &host, const std::string &password, const std::string &realname)
: _fd(fd), _nickname(nickname), _user(user), _host(host), _password(password), _realname(realname), _authenticated(false), _operator(false), _away(false) {}
int Client::getFd() const { return _fd; }
const std::string &Client::getNickname() const { return _nickname; }
void Client::setNickname(const std::string &nickname) { _nickname = nickname; }
const std::string &Client::getUser() const { return _user; }
void Client::setUser(const std::string &user) { _user = user; }
const std::string &Client::getHost() const { return _host; }
void Client::setHost(const std::string &host) { _host = host; }
const std::string &Client::getPassword() const { return _password; }
void Client::setPassword(const std::string &password) { _password = password; }
const std::string &Client::getRealName() const { return _realname; }
void Client::setRealName(const std::string &realname) { _realname = realname; }
bool Client::isAuthenticated() const { return _authenticated; }
void Client::authenticate() { _authenticated = true; }
bool Client::isOperator() const { return _operator; }
void Client::setOperator(bool isOperator) { _operator = isOperator; }
bool Client::isAway() const { return _away; }
const std::string &Client::getAwayMessage() const { return _awayMessage; }
void Client::setAwayMessage(const std::string &message) { _awayMessage = message; }
void Client::setAway(bool away) { _away = away; }
/*Client::Client(int fd)
Description: Constructeur de la classe Client. Initialise le client avec le descripteur de fichier fourni.
Paramètres:
int fd: Le descripteur de fichier du client.
Client::~Client()
Description: Destructeur de la classe Client. Gère la destruction de l'objet.
int Client::getFd() const
{
return _fd;
}
const std::string &Client::getNickname() const
{
return _nickname;
}
void Client::setNickname(const std::string &nickname)
{
_nickname = nickname;
}
const std::string &Client::getUser() const
{
return _user;
}
void Client::setUser(const std::string &user)
{
_user = user;
}
const std::string &Client::getHost() const
{
return _host;
}
void Client::setHost(const std::string &host)
{
_host = host;
}
const std::string &Client::getPassword() const
{
return _password;
}
Description: Renvoie le descripteur de fichier du client.
Retourne: Le descripteur de fichier du client.
void Client::setPassword(const std::string &password)
{
_password = password;
}
const std::string &Client::getRealName() const
{
return _realname;
}
void Client::setRealName(const std::string &realname)
{
_realname = realname;
}
Description: Définit le mot de passe du client et le marque comme authentifié.
Paramètres:
const std::string &password: Le mot de passe à définir.
bool Client::isAuthenticated() const
{
return _authenticated;
}
Description: Vérifie si le client est authentifié.
Retourne: true si le client est authentifié, false sinon.
void logMessage(const std::string &message, const std::string &color)
void Client::authenticate()
{
_authenticated = true;
}
Description: Enregistre un message dans les logs et l'affiche avec des couleurs dans la console.
Paramètres:
const std::string &message: Le message à enregistrer.
const std::string &color: La couleur du message.*/
bool Client::isOperator() const
{
return _operator;
}
void Client::setOperator(bool isOperator)
{
_operator = isOperator;
}
bool Client::isAway() const
{
return _away;
}
const std::string &Client::getAwayMessage() const
{
return _awayMessage;
}
void Client::setAwayMessage(const std::string &message)
{
_awayMessage = message;
}
void Client::setAway(bool away)
{
_away = away;
}

View File

@ -6,126 +6,126 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:32:23 by fgras-ca #+# #+# */
/* Updated: 2024/05/21 20:28:41 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:32:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClientManager.hpp"
ClientManager::ClientManager(Server *server)
: _server(server)
: _server(server)
{
}
void ClientManager::acceptClient()
{
int client_fd = accept(_server->_server_fd, NULL, NULL);
if (client_fd < 0)
{
_server->log("Failed to accept client.", RED);
return;
}
int client_fd = accept(_server->_server_fd, NULL, NULL);
if (client_fd < 0)
{
_server->log("Failed to accept client.", RED);
return;
}
Client *newClient = new Client(client_fd, "", "", "", "", ""); // Fournir six arguments
_server->_clients[client_fd] = newClient;
struct pollfd client_pollfd;
client_pollfd.fd = client_fd;
client_pollfd.events = POLLIN;
_server->_poll_fds.push_back(client_pollfd);
Client *newClient = new Client(client_fd, "", "", "", "", ""); // Fournir six arguments
_server->_clients[client_fd] = newClient;
struct pollfd client_pollfd;
client_pollfd.fd = client_fd;
client_pollfd.events = POLLIN;
_server->_poll_fds.push_back(client_pollfd);
std::stringstream ss;
ss << "Client attempting connection: " << client_fd;
_server->log(ss.str(), YELLOW);
std::stringstream ss;
ss << "Client attempting connection: " << client_fd;
_server->log(ss.str(), YELLOW);
}
void ClientManager::handleClient(int client_fd)
{
char buffer[1024];
std::memset(buffer, 0, sizeof(buffer));
int bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
char buffer[1024];
std::memset(buffer, 0, sizeof(buffer));
int bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
if (bytes_received <= 0)
{
std::ostringstream oss;
oss << "Client disconnected: " << client_fd;
_server->log(oss.str(), RED);
removeClient(client_fd);
return;
}
if (bytes_received <= 0)
{
std::ostringstream oss;
oss << "Client disconnected: " << client_fd;
_server->log(oss.str(), RED);
removeClient(client_fd);
return;
}
std::string message(buffer, bytes_received);
std::ostringstream oss;
oss << "Received from client " << client_fd << ": " << message;
_server->log(oss.str(), BLUE);
std::string message(buffer, bytes_received);
std::ostringstream oss;
oss << "Received from client " << client_fd << ": " << message;
_server->log(oss.str(), BLUE);
Client* client = _server->getClients()[client_fd];
if (!client)
{
std::ostringstream oss;
oss << "Client not found for fd: " << client_fd;
_server->log(oss.str(), RED);
return;
}
Client* client = _server->getClients()[client_fd];
if (!client)
{
std::ostringstream oss;
oss << "Client not found for fd: " << client_fd;
_server->log(oss.str(), RED);
return;
}
std::istringstream message_stream(message);
std::string line;
std::istringstream message_stream(message);
std::string line;
while (std::getline(message_stream, line))
{
// Remove trailing '\r' and '\n'
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
while (std::getline(message_stream, line))
{
// Remove trailing '\r' and '\n'
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
if (!line.empty())
{
std::ostringstream oss;
oss << "Processing command from client " << client_fd << ": " << line;
_server->log(oss.str(), BLUE);
_server->_commandHandler->handleCommand(client, line);
}
}
if (!line.empty())
{
std::ostringstream oss;
oss << "Processing command from client " << client_fd << ": " << line;
_server->log(oss.str(), BLUE);
_server->_commandHandler->handleCommand(client, line);
}
}
}
void ClientManager::removeClient(int clientFd)
{
Client* client = _server->_clients[clientFd];
if (client)
{
// Log the nickname being freed
_server->log("Removing client: " + client->getNickname(), YELLOW);
Client* client = _server->_clients[clientFd];
if (client)
{
// Log the nickname being freed
_server->log("Removing client: " + client->getNickname(), YELLOW);
std::map<std::string, Channel*>::iterator it = _server->_channels.begin();
while (it != _server->_channels.end())
{
it->second->removeClient(client);
if (it->second->isEmpty())
{
delete it->second;
std::map<std::string, Channel*>::iterator it_to_delete = it++;
_server->_channels.erase(it_to_delete);
}
else
{
++it;
}
}
delete client;
_server->_clients.erase(clientFd);
}
std::map<std::string, Channel*>::iterator it = _server->_channels.begin();
while (it != _server->_channels.end())
{
it->second->removeClient(client);
if (it->second->isEmpty())
{
delete it->second;
std::map<std::string, Channel*>::iterator it_to_delete = it++;
_server->_channels.erase(it_to_delete);
}
else
{
++it;
}
}
delete client;
_server->_clients.erase(clientFd);
}
std::vector<struct pollfd>::iterator it_poll = _server->_poll_fds.begin();
while (it_poll != _server->_poll_fds.end())
{
if (it_poll->fd == clientFd)
{
_server->_poll_fds.erase(it_poll);
break;
}
++it_poll;
}
std::vector<struct pollfd>::iterator it_poll = _server->_poll_fds.begin();
while (it_poll != _server->_poll_fds.end())
{
if (it_poll->fd == clientFd)
{
_server->_poll_fds.erase(it_poll);
break;
}
++it_poll;
}
std::stringstream ss;
ss << "Client disconnected: " << clientFd;
_server->log(ss.str(), YELLOW);
std::stringstream ss;
ss << "Client disconnected: " << clientFd;
_server->log(ss.str(), YELLOW);
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 19:51:31 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 13:07:53 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:33:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,66 +14,66 @@
void JoinHandler::handleJoinCommand(Client *client, const std::string &channelName, Server *server)
{
if (channelName.empty()) {
server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "JOIN"));
return;
}
if (channelName.empty()) {
server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "JOIN"));
return;
}
std::map<std::string, Channel *> &channels = server->getChannels();
Channel *channel;
std::map<std::string, Channel *> &channels = server->getChannels();
Channel *channel;
if (channels.find(channelName) == channels.end()) {
channel = new Channel(channelName);
channels[channelName] = channel;
} else {
channel = channels[channelName];
}
if (channels.find(channelName) == channels.end()) {
channel = new Channel(channelName);
channels[channelName] = channel;
} else {
channel = channels[channelName];
}
if (channel->isBanned(client)) {
server->sendToClient(client->getFd(), ERR_BANNEDFROMCHAN(client, channelName));
return;
}
if (channel->isBanned(client)) {
server->sendToClient(client->getFd(), ERR_BANNEDFROMCHAN(client, channelName));
return;
}
if (channel->isFull()) {
server->sendToClient(client->getFd(), ERR_CHANNELISFULL(client, channelName));
return;
}
if (channel->isFull()) {
server->sendToClient(client->getFd(), ERR_CHANNELISFULL(client, channelName));
return;
}
if (channel->isInviteOnly() && !channel->isInvited(client)) {
server->sendToClient(client->getFd(), ERR_INVITEONLYCHAN(client, channelName));
return;
}
if (channel->isInviteOnly() && !channel->isInvited(client)) {
server->sendToClient(client->getFd(), ERR_INVITEONLYCHAN(client, channelName));
return;
}
channel->addClient(client);
sendJoinSuccess(client, channel, server);
channel->addClient(client);
sendJoinSuccess(client, channel, server);
}
void JoinHandler::sendJoinSuccess(Client *client, Channel *channel, Server *server)
{
std::string joinMessage = ":" + client->getNickname() + " JOIN " + channel->getName() + "\r\n";
server->sendToClient(client->getFd(), joinMessage);
std::string joinMessage = ":" + client->getNickname() + " JOIN " + channel->getName() + "\r\n";
server->sendToClient(client->getFd(), joinMessage);
if (!channel->getTopic().empty()) {
server->sendToClient(client->getFd(), RPL_TOPIC(client, channel->getName(), channel->getTopic()));
server->sendToClient(client->getFd(), RPL_TOPICWHOTIME(client, channel->getName(), channel->getTopicSetter(), channel->getTopicTime()));
}
if (!channel->getTopic().empty()) {
server->sendToClient(client->getFd(), RPL_TOPIC(client, channel->getName(), channel->getTopic()));
server->sendToClient(client->getFd(), RPL_TOPICWHOTIME(client, channel->getName(), channel->getTopicSetter(), channel->getTopicTime()));
}
std::string usersList = getUsersList(channel);
server->sendToClient(client->getFd(), usersList);
std::string usersList = getUsersList(channel);
server->sendToClient(client->getFd(), usersList);
channel->broadcast(joinMessage, client, server);
channel->broadcast(joinMessage, client, server);
}
std::string JoinHandler::getUsersList(Channel *channel)
{
std::vector<Client *> clients = channel->getClients();
std::string users;
for (std::vector<Client *>::iterator it = clients.begin(); it != clients.end(); ++it) {
users += (*it)->getNickname() + " ";
}
std::vector<Client *> clients = channel->getClients();
std::string users;
for (std::vector<Client *>::iterator it = clients.begin(); it != clients.end(); ++it) {
users += (*it)->getNickname() + " ";
}
std::ostringstream oss;
oss << RPL_NAMREPLY(clients[0], channel->getName(), users);
oss << RPL_ENDOFNAMES(clients[0], channel->getName());
return oss.str();
std::ostringstream oss;
oss << RPL_NAMREPLY(clients[0], channel->getName(), users);
oss << RPL_ENDOFNAMES(clients[0], channel->getName());
return oss.str();
}

View File

@ -6,66 +6,66 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/17 16:09:20 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 13:08:30 by fgras-ca ### ########.fr */
/* Updated: 2024/05/28 14:33:29 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Who.hpp"
WhoHandler::WhoHandler(Server *server)
: _server(server)
: _server(server)
{
}
void WhoHandler::handleWhoCommand(Client *client, const std::string &command)
{
std::istringstream iss(command);
std::string cmd, mask;
iss >> cmd >> mask;
std::istringstream iss(command);
std::string cmd, mask;
iss >> cmd >> mask;
std::map<std::string, Channel *> &channels = _server->getChannels();
if (channels.find(mask) != channels.end())
{
Channel *channel = channels[mask];
std::vector<Client *> channelClients = channel->getClients();
std::map<std::string, Channel *> &channels = _server->getChannels();
if (channels.find(mask) != channels.end())
{
Channel *channel = channels[mask];
std::vector<Client *> channelClients = channel->getClients();
for (size_t i = 0; i < channelClients.size(); ++i)
{
_server->sendToClient(client->getFd(), RPL_WHOREPLY(mask, channelClients[i]));
}
}
else
{
Client *targetClient = _server->getClientByName(mask);
if (targetClient)
{
_server->sendToClient(client->getFd(), RPL_WHOREPLY("*", targetClient));
}
}
for (size_t i = 0; i < channelClients.size(); ++i)
{
_server->sendToClient(client->getFd(), RPL_WHOREPLY(mask, channelClients[i]));
}
}
else
{
Client *targetClient = _server->getClientByName(mask);
if (targetClient)
{
_server->sendToClient(client->getFd(), RPL_WHOREPLY("*", targetClient));
}
}
_server->sendToClient(client->getFd(), RPL_ENDOFWHO(client->getFd(), mask));
_server->sendToClient(client->getFd(), RPL_ENDOFWHO(client->getFd(), mask));
}
void WhoHandler::handleWhoisCommand(Client *client, const std::string &command)
{
std::istringstream iss(command);
std::string cmd, target;
iss >> cmd >> target;
std::istringstream iss(command);
std::string cmd, target;
iss >> cmd >> target;
if (target.empty())
{
_server->sendToClient(client->getFd(), ERR_NONICKNAMEGIVEN(client));
return;
}
if (target.empty())
{
_server->sendToClient(client->getFd(), ERR_NONICKNAMEGIVEN(client));
return;
}
Client *targetClient = _server->getClientByName(target);
if (!targetClient)
{
_server->sendToClient(client->getFd(), ERR_NOSUCHNICK(client->getFd(), target));
return;
}
Client *targetClient = _server->getClientByName(target);
if (!targetClient)
{
_server->sendToClient(client->getFd(), ERR_NOSUCHNICK(client->getFd(), target));
return;
}
_server->sendToClient(client->getFd(), RPL_WHOISUSER(client->getFd(), targetClient));
_server->sendToClient(client->getFd(), RPL_WHOISSERVER(client->getFd(), target, "IRC server info"));
_server->sendToClient(client->getFd(), RPL_ENDOFWHOIS(client->getFd(), target));
_server->sendToClient(client->getFd(), RPL_WHOISUSER(client->getFd(), targetClient));
_server->sendToClient(client->getFd(), RPL_WHOISSERVER(client->getFd(), target, "IRC server info"));
_server->sendToClient(client->getFd(), RPL_ENDOFWHOIS(client->getFd(), target));
}