ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
TopicCommand.cpp
[詳解]
1
8#include "TopicCommand.hpp"
9#include "Channel.hpp"
10#include "Client.hpp"
11#include "Replies.hpp"
12#include "Server.hpp"
13
15
17 if (args.empty()) {
19 params.push_back("TOPIC");
21 return;
22 }
23
24 const std::string &channelName = args[0];
25 Channel *channel = _server->getChannel(channelName);
26
27 if (channel == NULL) {
29 params.push_back(channelName);
31 return;
32 }
33
34 if (!channel->isMember(client)) {
36 params.push_back(channelName);
38 return;
39 }
40
41 if (args.size() == 1) {
42 // View topic
43 if (channel->getTopic().empty()) {
45 params.push_back(channelName);
46 client->sendMessage(formatReply(_server->getServerName(), client->getNickname(), RPL_NOTOPIC, params));
47 } else {
49 params.push_back(channelName);
50 params.push_back(channel->getTopic());
51 client->sendMessage(formatReply(_server->getServerName(), client->getNickname(), RPL_TOPIC, params));
52 }
53 } else {
54 // Set topic
55 if (channel->hasMode('t') && !channel->isOperator(client)) {
57 params.push_back(channelName);
58 client->sendMessage(
60 return;
61 }
62
63 std::string newTopic;
64 // The topic can be a single word or a sentence prefixed with a colon.
65 // We need to concatenate args from index 1 to the end.
66 if (args.size() > 1) {
67 newTopic = args[1];
68 if (!newTopic.empty() && newTopic.at(0) == ':') {
69 newTopic.erase(0, 1);
70 }
71 for (size_t i = 2; i < args.size(); ++i) {
72 newTopic += " " + args[i];
73 }
74 }
75
76 channel->setTopic(newTopic);
77 std::string topicMessage = client->getPrefix() + " TOPIC " + channelName + " :" + newTopic + "\r\n";
78 channel->broadcast(topicMessage, NULL);
79 }
80}
Manages channel members and states.
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_NOTOPIC
Definition Replies.hpp:49
#define RPL_TOPIC
Definition Replies.hpp:48
#define ERR_NOTONCHANNEL
Definition Replies.hpp:75
#define ERR_CHANOPRIVSNEEDED
Definition Replies.hpp:80
#define ERR_NOSUCHCHANNEL
Definition Replies.hpp:65
#define ERR_NEEDMOREPARAMS
Definition Replies.hpp:76
Core IRC server implementation.
Handles the TOPIC command.
T at(T... args)
チャンネルのメンバーと状態を管理するクラス。
Definition Channel.hpp:25
bool hasMode(char mode) const
Definition Channel.cpp:59
bool isMember(Client *client) const
Definition Channel.cpp:37
void setTopic(const std::string &topic)
Definition Channel.cpp:28
void broadcast(const std::string &message, Client *excludeClient)
Definition Channel.cpp:39
bool isOperator(Client *client) const
Definition Channel.cpp:51
const std::string & getTopic() const
Definition Channel.cpp:26
Represents an IRC client connected to the server.
Definition Client.hpp:25
std::string getPrefix() const
Generates the client's IRC prefix (e.g., :nick!user@host).
Definition Client.cpp:50
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
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::string & getServerName() const
Gets the server's name.
Definition Server.cpp:381
TopicCommand(Server *server)
Constructs a TopicCommand object.
void execute(Client *client, const std::vector< std::string > &args)
Executes the TOPIC command.
T empty(T... args)
T erase(T... args)
T push_back(T... args)
T size(T... args)