ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
NamesCommand.cpp
[詳解]
1
8#include "NamesCommand.hpp"
9#include "Channel.hpp"
10#include "Client.hpp"
11#include "CommandUtils.hpp"
12#include "Replies.hpp"
13#include "Server.hpp"
14
16
18 if (args.empty()) {
19 // List all channels
21 for (std::map<std::string, Channel *>::const_iterator it = channels.begin(); it != channels.end(); ++it) {
22 sendChannelNames(client, it->second);
23 }
24 } else {
25 // List specified channels
26 std::vector<std::string> channelNames = split(args[0], ',');
27 for (size_t i = 0; i < channelNames.size(); ++i) {
28 Channel *channel = _server->getChannel(channelNames[i]);
29 if (channel) {
30 sendChannelNames(client, channel);
31 } else {
32 // Non-existent channel, just send ENDOFNAMES
34 params.push_back(channelNames[i]);
35 client->sendMessage(
37 }
38 }
39 }
40}
41
42void NamesCommand::sendChannelNames(Client *client, Channel *channel) {
43 if (!channel)
44 return;
45
46 std::string channelName = channel->getName();
47 std::string names;
48 const std::map<int, Client *> &members = channel->getMembers();
49 for (std::map<int, Client *>::const_iterator it = members.begin(); it != members.end(); ++it) {
50 if (channel->isOperator(it->second)) {
51 names += "@";
52 }
53 names += it->second->getNickname() + " ";
54 }
55 if (!names.empty()) {
56 names.erase(names.length() - 1);
57 }
58
59 std::vector<std::string> nam_params;
60 nam_params.push_back(channelName);
61 nam_params.push_back(names);
62 client->sendMessage(formatReply(_server->getServerName(), client->getNickname(), RPL_NAMREPLY, nam_params));
63
64 std::vector<std::string> end_params;
65 end_params.push_back(channelName);
66 client->sendMessage(formatReply(_server->getServerName(), client->getNickname(), RPL_ENDOFNAMES, end_params));
67}
Manages channel members and states.
Manages client connection and state.
std::vector< std::string > split(const std::string &s, char delimiter)
Splits a string into a vector of strings based on a delimiter.
Utility functions for command handling.
Handles the NAMES 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 RPL_ENDOFNAMES
Definition Replies.hpp:56
#define RPL_NAMREPLY
Definition Replies.hpp:55
Core IRC server implementation.
T begin(T... args)
チャンネルのメンバーと状態を管理するクラス。
Definition Channel.hpp:25
const std::map< int, Client * > & getMembers() const
Definition Channel.cpp:24
const std::string & getName() const
Definition Channel.cpp:22
bool isOperator(Client *client) const
Definition Channel.cpp:51
Represents an IRC client connected to the server.
Definition Client.hpp:25
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
Abstract base class (interface) for all IRC commands.
Definition ICommand.hpp:26
Server * _server
Pointer to the IRC server instance.
Definition ICommand.hpp:28
void execute(Client *client, const std::vector< std::string > &args)
Executes the NAMES command.
NamesCommand(Server *server)
Constructs a NamesCommand object.
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< std::string, Channel * > & getChannels() const
Gets a constant reference to the map of channels managed by the server.
Definition Server.cpp:422
const std::string & getServerName() const
Gets the server's name.
Definition Server.cpp:381
T empty(T... args)
T end(T... args)
T erase(T... args)
T push_back(T... args)
T size(T... args)