ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
CommandManager.cpp
[詳解]
1
8#include "CommandManager.hpp"
9#include "Client.hpp"
10#include "Replies.hpp"
11#include "Server.hpp"
12#include <algorithm>
13#include <iostream>
14#include <sstream>
15
16// Include all concrete command headers
17#include "InviteCommand.hpp"
18#include "JoinCommand.hpp"
19#include "KickCommand.hpp"
20#include "ListCommand.hpp"
21#include "ModeCommand.hpp"
22#include "NamesCommand.hpp"
23#include "NickCommand.hpp"
24#include "NoticeCommand.hpp"
25#include "PartCommand.hpp"
26#include "PassCommand.hpp"
27#include "PingCommand.hpp"
28#include "PongCommand.hpp"
29#include "PrivmsgCommand.hpp"
30#include "QuitCommand.hpp"
31#include "TopicCommand.hpp"
32#include "UserCommand.hpp"
33#include "WhoCommand.hpp"
34#include "WhoisCommand.hpp"
35
36CommandManager::CommandManager(Server *server) : _server(server) {
37 _commands["PASS"] = new PassCommand(server);
38 _commands["NICK"] = new NickCommand(server);
39 _commands["USER"] = new UserCommand(server);
40 _commands["JOIN"] = new JoinCommand(server);
41 _commands["PRIVMSG"] = new PrivmsgCommand(server);
42 _commands["PART"] = new PartCommand(server);
43 _commands["QUIT"] = new QuitCommand(server);
44 _commands["PING"] = new PingCommand(server);
45 _commands["PONG"] = new PongCommand(server);
46 _commands["NOTICE"] = new NoticeCommand(server);
47 _commands["NAMES"] = new NamesCommand(server);
48 _commands["TOPIC"] = new TopicCommand(server);
49 _commands["MODE"] = new ModeCommand(server);
50 _commands["KICK"] = new KickCommand(server);
51 _commands["LIST"] = new ListCommand(server);
52 _commands["WHO"] = new WhoCommand(server);
53 _commands["WHOIS"] = new WhoisCommand(server);
54 _commands["INVITE"] = new InviteCommand(server);
55}
56
58 for (std::map<std::string, ICommand *>::iterator it = _commands.begin(); it != _commands.end(); ++it) {
59 delete it->second;
60 }
61}
62
63std::string CommandManager::getCommand(const std::string &rawMessage) {
64 std::string command;
65 size_t start = 0;
66 // Skip prefix if it exists
67 if (rawMessage[0] == ':') {
68 start = rawMessage.find(' ');
69 if (start == std::string::npos)
70 return ""; // Malformed message
71 start++; // Move past the space
72 }
73
74 size_t end = rawMessage.find(' ', start);
75 if (end == std::string::npos) {
76 command = rawMessage.substr(start);
77 } else {
78 command = rawMessage.substr(start, end - start);
79 }
80 // --------- debug ---------
81 // std::cout << "rawMessage[" << rawMessage << "]" << std::endl;
82 // std::cout << "start[" << start << "]" << std::endl;
83 // std::cout << "end[" << end << "]" << std::endl;
84 // std::cout << "command[" << command << "]" << std::endl;
85 // -------------------------
86 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
87 return command;
88}
89
90std::vector<std::string> CommandManager::getArguments(const std::string &rawMessage) {
92 std::stringstream ss(rawMessage);
93 std::string token;
94
95 // Skip the command part
96 ss >> token;
97 if (!token.empty() && token[0] == ':') { // if there's a prefix
98 ss >> token;
99 }
100
101 while (ss >> token) {
102 if (!token.empty() && token[0] == ':') {
103 // This is the last argument, which can contain spaces
104 std::string lastArg;
105 getline(ss, lastArg, '\n'); // Read the rest of the line
106 if (!lastArg.empty() && (lastArg[lastArg.size() - 1] == '\r')) {
107 lastArg.erase(lastArg.size() - 1); // remove \r
108 }
109 args.push_back(token.substr(1) + lastArg);
110 break;
111 }
112 args.push_back(token);
113 }
114 return args;
115}
116
117void CommandManager::parseAndExecute(Client *client, const std::string &rawMessage) {
118 if (rawMessage.empty() || !client) {
119 return;
120 }
121
122 std::string command = getCommand(rawMessage);
123 if (command.empty()) {
124 return;
125 }
126
128 if (it != _commands.end()) {
129 std::vector<std::string> args = getArguments(rawMessage);
130 it->second->execute(client, args);
131 client->updateActivityTime(); // クライアントのアクティビティタイムを更新
132 // --- debug ---
133 // std::cout << "args: ";
134 // for (const std::string &arg : args) {
135 // std::cout << arg << " ";
136 //}
137 // std::cout << std::endl;
138 // std::cout << "first: " << it->first << std::endl;
139 // std::cout << client->getNickname() << std::endl;
140 // -------------
141 } else {
143 params.push_back(command);
144 client->sendMessage(
145 formatReply(this->_server->getServerName(), client->getNickname(), ERR_UNKNOWNCOMMAND, params));
146 }
147}
Manages client connection and state.
Manages IRC commands and their execution.
Handles the INVITE command.
Handles the JOIN command.
Handles the KICK command.
Handles the LIST command.
Handles the MODE command.
Handles the NAMES command.
Handles the NICK command.
Handles the NOTICE command.
Handles the PART command.
Handles the PASS command.
Handles the PING command.
Handles the PONG command.
Handles the PRIVMSG command.
Handles the QUIT 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_UNKNOWNCOMMAND
Definition Replies.hpp:70
Core IRC server implementation.
Handles the TOPIC command.
Handles the USER command.
Handles the WHO command.
Handles the WHOIS command.
T begin(T... args)
Represents an IRC client connected to the server.
Definition Client.hpp:25
void updateActivityTime()
Updates the client's last activity time to the current time.
Definition Client.cpp:87
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
CommandManager(Server *server)
Constructs a new CommandManager object.
void parseAndExecute(Client *client, const std::string &rawMessage)
Parses a raw message and executes the corresponding command.
~CommandManager()
Destroys the CommandManager object and frees all registered command objects.
Implements the INVITE command.
Implements the JOIN command.
Implements the KICK command.
Implements the LIST command.
Implements the MODE command.
Implements the NAMES command.
Implements the NICK command.
Implements the NOTICE command.
Implements the PART command.
Implements the PASS command.
Implements the PING command.
Implements the PONG command.
Implements the PRIVMSG command.
Implements the QUIT command.
Implements the core IRC server functionality as a Singleton.
Definition Server.hpp:49
const std::string & getServerName() const
Gets the server's name.
Definition Server.cpp:381
Implements the TOPIC command.
Implements the USER command.
Implements the WHO command.
Implements the WHOIS command.
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
T getline(T... args)
T push_back(T... args)
T size(T... args)
T substr(T... args)
T transform(T... args)