ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
ModeCommand.cpp
[詳解]
1
8#include "ModeCommand.hpp"
9#include "Channel.hpp"
10#include "Client.hpp"
11#include "Replies.hpp"
12#include "Server.hpp"
13#include <cstdlib>
14
16
18 if (args.size() < 2) {
20 params.push_back("MODE");
22 return;
23 }
24
25 const std::string &channelName = args[0];
26 const std::string &modeStr = args[1];
27 Channel *channel = _server->getChannel(channelName);
28
29 if (channel == NULL) {
31 params.push_back(channelName);
33 return;
34 }
35
36 if (!channel->isOperator(client)) {
38 params.push_back(channelName);
40 return;
41 }
42
43 if (modeStr.length() != 2 || (modeStr[0] != '+' && modeStr[0] != '-')) {
44 // Invalid mode string format
45 return;
46 }
47
48 bool add = (modeStr[0] == '+');
49 char mode = modeStr[1];
50
51 if (mode == 'o') {
52 if (args.size() < 3) {
54 params.push_back("MODE");
55 client->sendMessage(
57 return;
58 }
59 const std::string &targetNick = args[2];
60 Client *targetClient = _server->getClientByNickname(targetNick);
61
62 if (targetClient == NULL) {
64 params.push_back(targetNick);
66 return;
67 }
68
69 if (!channel->isMember(targetClient)) {
71 params.push_back(targetNick);
72 params.push_back(channelName);
73 client->sendMessage(
75 return;
76 }
77
78 if (add) {
79 channel->addOperator(targetClient);
80 } else {
81 channel->removeOperator(targetClient);
82 }
83 // Broadcast mode change
84 std::string modeMsg = client->getPrefix() + " MODE " + channelName + " " + modeStr + " " + targetNick + "\r\n";
85 channel->broadcast(modeMsg, NULL);
86
87 } else if (mode == 't' || mode == 'n') {
88 channel->setMode(mode, add);
89 // Broadcast mode change
90 std::string modeMsg = client->getPrefix() + " MODE " + channelName + " " + modeStr + "\r\n";
91 channel->broadcast(modeMsg, NULL);
92 } else if (mode == 'k') {
93 if (add) {
94 if (args.size() < 3) {
96 params.push_back("MODE");
97 client->sendMessage(
99 return;
100 }
101 channel->setKey(args[2]);
102 channel->setMode('k', true);
103 } else {
104 if (args.size() < 3) {
106 params.push_back("MODE");
107 client->sendMessage(
109 return;
110 }
111 if (channel->getKey() != args[2]) {
112 // RFCにはERR_KEYSETのようなエラーはないが、不正なキーでの解除は無視するかエラーを返す
113 // ここでは無視する
114 return;
115 }
116 channel->setKey("");
117 channel->setMode('k', false);
118 }
119 std::string modeMsg =
120 client->getPrefix() + " MODE " + channelName + " " + modeStr + (add ? " " + args[2] : "") + "\r\n";
121 channel->broadcast(modeMsg, NULL);
122 } else if (mode == 'l') {
123 if (add) {
124 if (args.size() < 3) {
126 params.push_back("MODE");
127 client->sendMessage(
129 return;
130 }
131 unsigned int limit = static_cast<unsigned int>(std::atoi(args[2].c_str()));
132 channel->setLimit(limit);
133 channel->setMode('l', true);
134 } else {
135 channel->setLimit(0);
136 channel->setMode('l', false);
137 }
138 std::string modeMsg =
139 client->getPrefix() + " MODE " + channelName + " " + modeStr + (add ? " " + args[2] : "") + "\r\n";
140 channel->broadcast(modeMsg, NULL);
141 } else if (mode == 'i') {
142 channel->setMode(mode, add);
143 std::string modeMsg = client->getPrefix() + " MODE " + channelName + " " + modeStr + "\r\n";
144 channel->broadcast(modeMsg, NULL);
145 } else {
147 params.push_back(std::string(1, mode));
148 params.push_back(channelName);
150 }
151}
Manages channel members and states.
Manages client connection and state.
Handles the MODE 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_NOSUCHNICK
Definition Replies.hpp:64
#define ERR_CHANOPRIVSNEEDED
Definition Replies.hpp:80
#define ERR_NOSUCHCHANNEL
Definition Replies.hpp:65
#define ERR_USERNOTINCHANNEL
Definition Replies.hpp:74
#define ERR_NEEDMOREPARAMS
Definition Replies.hpp:76
#define ERR_UNKNOWNMODE
Definition Replies.hpp:79
Core IRC server implementation.
T atoi(T... args)
チャンネルのメンバーと状態を管理するクラス。
Definition Channel.hpp:25
void setMode(char mode, bool value)
Definition Channel.cpp:53
bool isMember(Client *client) const
Definition Channel.cpp:37
void setLimit(unsigned int limit)
Definition Channel.cpp:83
void removeOperator(Client *client)
Definition Channel.cpp:49
void addOperator(Client *client)
Definition Channel.cpp:47
const std::string & getKey() const
Definition Channel.cpp:81
void broadcast(const std::string &message, Client *excludeClient)
Definition Channel.cpp:39
bool isOperator(Client *client) const
Definition Channel.cpp:51
void setKey(const std::string &key)
Definition Channel.cpp:77
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
void execute(Client *client, const std::vector< std::string > &args)
Executes the MODE command.
ModeCommand(Server *server)
Constructs a ModeCommand 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
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 push_back(T... args)
T size(T... args)