ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
CommandUtils.cpp
[詳解]
1
8#include "CommandUtils.hpp"
9#include "Channel.hpp"
10#include "Client.hpp"
11#include "Replies.hpp"
12#include "Server.hpp"
13#include <sstream>
14
15std::vector<std::string> split(const std::string &s, char delimiter) {
17 std::string token;
18 std::istringstream tokenStream(s);
19 while (std::getline(tokenStream, token, delimiter)) {
20 tokens.push_back(token);
21 }
22 return tokens;
23}
24
25void handleMessage(Server *server, Client *sender, const std::string &command, const std::vector<std::string> &args) {
26 bool isNotice = (command == "NOTICE");
27
28 if (args.empty()) {
29 if (!isNotice) {
31 params.push_back(command);
32 sender->sendMessage(formatReply(server->getServerName(), sender->getNickname(), ERR_NORECIPIENT, params));
33 }
34 return;
35 }
36
37 if (args.size() < 2) {
38 if (!isNotice) {
41 }
42 return;
43 }
44
45 const std::string &target = args[0];
46 const std::string &message = args[1];
47
48 if (target[0] == '#') {
49 // Target is a channel
50 Channel *channel = server->getChannel(target);
51 if (channel == NULL) {
52 if (!isNotice) {
54 params.push_back(target);
55 sender->sendMessage(
56 formatReply(server->getServerName(), sender->getNickname(), ERR_NOSUCHCHANNEL, params));
57 }
58 return;
59 }
60
61 // If +n mode is set, only members can send messages to the channel.
62 // If +n is NOT set, external messages are allowed.
63 if (channel->hasMode('n') && !channel->isMember(sender)) {
64 if (!isNotice) {
66 params.push_back(target);
67 sender->sendMessage(
68 formatReply(server->getServerName(), sender->getNickname(), ERR_CANNOTSENDTOCHAN, params));
69 }
70 return;
71 }
72
73 std::string fullMessage = sender->getPrefix() + " " + command + " " + target + " :" + message + "\r\n";
74 channel->broadcast(fullMessage, sender);
75 } else {
76 // Target is a user
77 Client *recipient = server->getClientByNickname(target);
78 if (recipient == NULL) {
79 if (!isNotice) {
81 params.push_back(target);
82 sender->sendMessage(
83 formatReply(server->getServerName(), sender->getNickname(), ERR_NOSUCHNICK, params));
84 }
85 return;
86 }
87
88 std::string fullMessage = sender->getPrefix() + " " + command + " " + target + " :" + message + "\r\n";
89 recipient->sendMessage(fullMessage);
90 }
91}
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.
void handleMessage(Server *server, Client *sender, const std::string &command, const std::vector< std::string > &args)
Handles sending messages to a target (user or channel) for PRIVMSG and NOTICE commands.
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 ERR_NOTEXTTOSEND
Definition Replies.hpp:68
#define ERR_NORECIPIENT
Definition Replies.hpp:67
#define ERR_NOSUCHNICK
Definition Replies.hpp:64
#define ERR_CANNOTSENDTOCHAN
Definition Replies.hpp:66
#define ERR_NOSUCHCHANNEL
Definition Replies.hpp:65
Core IRC server implementation.
チャンネルのメンバーと状態を管理するクラス。
Definition Channel.hpp:25
bool hasMode(char mode) const
Definition Channel.cpp:59
bool isMember(Client *client) const
Definition Channel.cpp:37
void broadcast(const std::string &message, Client *excludeClient)
Definition Channel.cpp:39
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
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 empty(T... args)
T getline(T... args)
T push_back(T... args)
T size(T... args)