ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
NickCommand.cpp
[詳解]
1
8#include "NickCommand.hpp"
9#include "Client.hpp"
10#include "Replies.hpp"
11#include "Server.hpp"
12#include <cctype> // For std::isdigit, std::isalnum
13#include <iostream>
14
16
17bool NickCommand::isValidNickname(const std::string &nick) const {
18 if (nick.empty() || nick.length() > 9) {
19 return false;
20 }
21 // Allowed characters: a-z, A-Z, 0-9, and special characters []{}^`_|-
22 // Must not start with a number or a hyphen
23 if (std::isdigit(static_cast<unsigned char>(nick[0])) || nick[0] == '-') {
24 return false;
25 }
26 for (size_t i = 0; i < nick.length(); ++i) {
27 char c = nick[i];
28 if (!std::isalnum(static_cast<unsigned char>(c)) && c != '[' && c != ']' && c != '{' && c != '}' && c != '^' &&
29 c != '`' && c != '_' && c != '|' && c != '-') {
30 return false;
31 }
32 }
33 return true;
34}
35
36void NickCommand::registerClient(Client *client) {
37 client->setRegistered(true);
38 std::vector<std::string> welcome_params;
39 welcome_params.push_back(client->getNickname());
40 welcome_params.push_back(client->getPrefix());
41 client->sendMessage(
42 formatReply(this->_server->getServerName(), client->getNickname(), RPL_WELCOME, welcome_params));
43
44 std::vector<std::string> host_params;
45 host_params.push_back(client->getNickname());
46 host_params.push_back(this->_server->getServerName());
47 client->sendMessage(formatReply(this->_server->getServerName(), client->getNickname(), RPL_YOURHOST, host_params));
48
49 std::cout << "Client " << client->getNickname() << " registered." << std::endl;
50}
51
53 // std::cout << "here?" << std::endl; // debug
54
55 // Remove this ??
56 if (!client->isAuthenticated()) {
57 // Not sending a reply is a common behavior for commands sent before authentication
58 return;
59 }
60
61 if (args.empty()) {
64 return;
65 }
66
67 const std::string &newNick = args[0];
68
69 if (!isValidNickname(newNick)) {
71 params.push_back(newNick);
72 std::string clientIdentifier = client->isRegistered() ? client->getNickname() : "*";
73 client->sendMessage(
74 formatReply(this->_server->getServerName(), clientIdentifier, ERR_ERRONEUSNICKNAME, params));
75 return;
76 }
77
78 if (this->_server->getClientByNickname(newNick) != NULL) {
79 std::string clientIdentifier = client->isRegistered() ? client->getNickname() : "*";
81 params.push_back(newNick);
82 client->sendMessage(formatReply(this->_server->getServerName(), clientIdentifier, ERR_NICKNAMEINUSE, params));
83 return;
84 }
85
86 std::string oldPrefix = client->getPrefix();
87 client->setNickname(newNick);
88
89 if (client->isRegistered()) {
90 // If already registered, just announce the nick change
91 client->sendMessage(oldPrefix + " NICK :" + newNick);
92 // Also notify channels the user is in
93 } else if (!client->getUsername().empty()) {
94 // If USER was already set, we can now complete the registration
95 registerClient(client);
96 }
97}
Manages client connection and state.
Handles the NICK command.
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 ERR_ERRONEUSNICKNAME
Definition Replies.hpp:72
#define RPL_WELCOME
Definition Replies.hpp:46
#define RPL_YOURHOST
Definition Replies.hpp:47
#define ERR_NICKNAMEINUSE
Definition Replies.hpp:73
#define ERR_NONICKNAMEGIVEN
Definition Replies.hpp:71
Core IRC server implementation.
Represents an IRC client connected to the server.
Definition Client.hpp:25
void setNickname(const std::string &nickname)
Sets the client's nickname.
Definition Client.cpp:58
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
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
const std::string & getUsername() const
Gets the client's username.
Definition Client.cpp:27
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
NickCommand(Server *server)
Constructs a NickCommand object.
void execute(Client *client, const std::vector< std::string > &args)
Executes the NICK command.
Implements the core IRC server functionality as a Singleton.
Definition Server.hpp:49
Client * getClientByNickname(const std::string &nickname)
Retrieves a Client object by its nickname.
Definition Server.cpp:391
const std::string & getServerName() const
Gets the server's name.
Definition Server.cpp:381
T empty(T... args)
T endl(T... args)
T push_back(T... args)
T length(T... args)