ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
WhoCommand.cpp
[詳解]
1
8#include "WhoCommand.hpp"
9#include "Channel.hpp"
10#include "Client.hpp"
11#include "CommandUtils.hpp" // For split
12#include "Replies.hpp"
13#include "Server.hpp"
14#include <sstream>
15#include <string>
16#include <vector>
17
19
21 std::string query = args.empty() ? "" : args[0];
22
23 // Iterate through all clients on the server
24 const std::map<int, Client *> &all_clients = _server->getClients(); // Assuming getClients() exists
25 for (std::map<int, Client *>::const_iterator it = all_clients.begin(); it != all_clients.end(); ++it) {
26 Client *targetClient = it->second;
27
28 // Skip if client is not registered
29 if (!targetClient->isRegistered()) {
30 continue;
31 }
32
33 bool matches_query = false;
34 if (query.empty()) {
35 // No query, list all registered clients
36 matches_query = true;
37 } else if (query[0] == '#') {
38 // Query is a channel name
39 Channel *channel = _server->getChannel(query);
40 if (channel && channel->isMember(targetClient)) {
41 matches_query = true;
42 }
43 } else {
44 // Query is a nickname or host mask (simplified to nickname for now)
45 if (targetClient->getNickname() == query) {
46 matches_query = true;
47 }
48 }
49
50 if (matches_query) {
51
52 std::string channel_name = "*";
53 std::string status_flags = "H"; // H = Here, G = Gone (Away - not implemented)
54 std::string op_flag = "";
55
56 // Check if the client is in any channel matching the query
57 const std::vector<Channel *> &client_channels = targetClient->getChannels();
58 for (size_t i = 0; i < client_channels.size(); ++i) {
59 Channel *ch = client_channels[i];
60 if (query.empty() || query[0] == '#') { // If listing all or by channel
61 if (query.empty() || ch->getName() == query) {
62 channel_name = ch->getName();
63 if (ch->isOperator(targetClient)) {
64 op_flag = "@";
65 }
66 break; // Found a channel, use its info
67 }
68 } else { // If querying by nickname, show their first channel
69 channel_name = ch->getName();
70 if (ch->isOperator(targetClient)) {
71 op_flag = "@";
72 }
73 break;
74 }
75 }
76
77 // Construct status flags
78 status_flags += op_flag; // Add @ if operator
79
80 std::vector<std::string> whoreply_params;
81 whoreply_params.push_back(channel_name); // <channel>
82 whoreply_params.push_back(targetClient->getNickname()); // <user>
83 whoreply_params.push_back(targetClient->getHostname()); // <host>
84 whoreply_params.push_back(_server->getServerName()); // <server>
85 whoreply_params.push_back(targetClient->getNickname()); // <nick>
86 whoreply_params.push_back(status_flags); // <H|G>[*][@|+] (simplified)
88 ss << 0; // <hopcount> (simplified to 0)
89 whoreply_params.push_back(ss.str());
90 whoreply_params.push_back(targetClient->getNickname()); // <real name> (simplified to nickname)
91
92 client->sendMessage(
93 formatReply(_server->getServerName(), client->getNickname(), RPL_WHOREPLY, whoreply_params));
94 }
95 }
96
97 // RPL_ENDOFWHO
98 std::vector<std::string> endwho_params;
99 if (!query.empty()) {
100 endwho_params.push_back(query);
101 } else {
102 endwho_params.push_back(client->getNickname());
103 }
104 client->sendMessage(formatReply(_server->getServerName(), client->getNickname(), RPL_ENDOFWHO, endwho_params));
105}
Manages channel members and states.
Manages client connection and state.
Utility functions for command handling.
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_WHOREPLY
Definition Replies.hpp:53
#define RPL_ENDOFWHO
Definition Replies.hpp:54
Core IRC server implementation.
Handles the WHO command.
T begin(T... args)
チャンネルのメンバーと状態を管理するクラス。
Definition Channel.hpp:25
const std::string & getName() const
Definition Channel.cpp:22
bool isMember(Client *client) const
Definition Channel.cpp:37
bool isOperator(Client *client) const
Definition Channel.cpp:51
Represents an IRC client connected to the server.
Definition Client.hpp:25
bool isRegistered() const
Checks if the client is registered.
Definition Client.cpp:33
const std::vector< Channel * > & getChannels() const
Gets the list of channels the client is in.
Definition Client.cpp:43
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 & getHostname() const
Gets the client's hostname.
Definition Client.cpp:29
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
Channel * getChannel(const std::string &name)
Retrieves a Channel object by its name.
Definition Server.cpp:406
const std::map< int, Client * > & getClients() const
Gets a constant reference to the map of clients managed by the server.
Definition Server.cpp:424
const std::string & getServerName() const
Gets the server's name.
Definition Server.cpp:381
void execute(Client *client, const std::vector< std::string > &args)
Executes the WHO command.
WhoCommand(Server *server)
Constructs a WhoCommand object.
T empty(T... args)
T end(T... args)
T push_back(T... args)
T size(T... args)
T str(T... args)