ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
UserCommand.cpp
[詳解]
1
8#include "UserCommand.hpp"
9#include "Client.hpp"
10#include "Replies.hpp"
11#include "Server.hpp"
12#include <iostream>
13
15
16void UserCommand::registerClient(Client *client) {
17 client->setRegistered(true);
18 std::vector<std::string> welcome_params;
19 welcome_params.push_back(client->getNickname());
20 welcome_params.push_back(client->getPrefix());
21 client->sendMessage(
22 formatReply(this->_server->getServerName(), client->getNickname(), RPL_WELCOME, welcome_params));
23
24 std::vector<std::string> host_params;
25 host_params.push_back(client->getNickname());
26 host_params.push_back(this->_server->getServerName());
27 client->sendMessage(formatReply(this->_server->getServerName(), client->getNickname(), RPL_YOURHOST, host_params));
28
29 std::cout << "Client " << client->getNickname() << " registered." << std::endl;
30}
31
33 if (!client->isAuthenticated()) {
34 return;
35 }
36
37 if (client->isRegistered()) {
39 client->sendMessage(
41 return;
42 }
43
44 if (args.size() < 4) {
46 params.push_back("USER");
47 client->sendMessage(
48 formatReply(this->_server->getServerName(), client->getNickname(), ERR_NEEDMOREPARAMS, params));
49 return;
50 }
51
52 client->setUsername(args[0]);
53
54 // The 4th argument (realname) can contain spaces.
55 // It starts with a colon.
56 std::string realname;
57 for (size_t i = 3; i < args.size(); ++i) {
58 if (!realname.empty()) {
59 realname += " ";
60 }
61 realname += args[i];
62 }
63 if (!realname.empty() && realname[0] == ':') {
64 realname.erase(0, 1);
65 }
66 client->setRealname(realname);
67
68 if (!client->getNickname().empty()) {
69 registerClient(client);
70 }
71}
Manages client connection and state.
std::string formatReply(const std::string &serverName, const std::string &clientNickname, const std::string &replyCodeAndText)
Formats an IRC reply message without extra parameters.
Definition Replies.cpp:14
Defines IRC numeric replies and error messages.
#define RPL_WELCOME
Definition Replies.hpp:46
#define RPL_YOURHOST
Definition Replies.hpp:47
#define ERR_ALREADYREGISTRED
Definition Replies.hpp:77
#define ERR_NEEDMOREPARAMS
Definition Replies.hpp:76
Core IRC server implementation.
Handles the USER command.
Represents an IRC client connected to the server.
Definition Client.hpp:25
void setUsername(const std::string &username)
Sets the client's username.
Definition Client.cpp:60
bool isRegistered() const
Checks if the client is registered.
Definition Client.cpp:33
std::string getPrefix() const
Generates the client's IRC prefix (e.g., :nick!user@host).
Definition Client.cpp:50
void setRealname(const std::string &realname)
Sets the client's real name.
Definition Client.cpp:62
bool isAuthenticated() const
Checks if the client is authenticated.
Definition Client.cpp:31
virtual void sendMessage(const std::string &message) const
Sends a message to the client by appending it to the send buffer.
Definition Client.cpp:135
const std::string & getNickname() const
Gets the client's nickname.
Definition Client.cpp:25
void setRegistered(bool value)
Sets the client's registration status.
Definition Client.cpp:66
Abstract base class (interface) for all IRC commands.
Definition ICommand.hpp:26
Server * _server
Pointer to the IRC server instance.
Definition ICommand.hpp:28
Implements the core IRC server functionality as a Singleton.
Definition Server.hpp:49
const std::string & getServerName() const
Gets the server's name.
Definition Server.cpp:381
UserCommand(Server *server)
Constructs a UserCommand object.
void execute(Client *client, const std::vector< std::string > &args)
Executes the USER command.
T empty(T... args)
T endl(T... args)
T erase(T... args)
T push_back(T... args)
T size(T... args)