-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode.cpp
More file actions
304 lines (278 loc) · 10.9 KB
/
Copy pathmode.cpp
File metadata and controls
304 lines (278 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mode.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cnascime <cnascime@student.42.rio> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/23 19:48:31 by cnascime #+# #+# */
/* Updated: 2025/02/11 11:37:37 by cnascime ### ########.fr */
/* */
/* ************************************************************************** */
#include "server.hpp"
#include "channel.hpp"
#include <string>
void Server::mode_utils(std::string &command, int fd) {
Client *client = get_client_by_fd(fd);
Channel *channel;
std::string name;
std::string params;
std::string new_mode;
std::string arguments = ":";
std::stringstream mode_list;
char binary = '\0';
arguments.clear();
mode_list.clear();
// Searches the command for the first character that is not MODE \t\v.
size_t position = command.find_first_not_of("MODE \t\v");
if (position != std::string::npos)
command = command.substr(position);
else { // If it's not found, sends error 461.
respond(ERR_NEEDMOREPARAMS(client->get_displayname()), fd);
return ;
};
// Looks at the arguments and splits them into individual parameters.
get_arguments(command, name, new_mode, params);
std::vector<std::string> shards = get_params(params);
// If the channel doesn't exist, or its name's invalid, sends error 403.
if (name[0] != '#' || !(channel = get_channel_by_name(name.substr(1)))) {
respond(ERR_NOSUCHCHANNEL(client->get_displayname(), name), fd);
return ; // If the client is not a member, sends error 442.
} else if (!channel->get_client_by_fd(fd) && !channel->get_admin_by_fd(fd)) {
respond(ERR_NOTONCHANNEL(client->get_displayname(), name), fd);
return ; // If no mode is set, displays the current modes.
} else if (new_mode.empty()) {
respond(RPL_CHANNELMODEIS(client->get_displayname(), name, channel->get_modes()) +
RPL_CREATIONTIME(client->get_displayname(), channel->get_name(), channel->get_creation_auto()), fd);
return ; // If the client trying to set modes is not an operator, sends error 482.
} else if (!channel->get_admin_by_fd(fd)) {
respond(ERR_CHANOPRIVSNEEDED(client->get_displayname(), name), fd);
return ;
} else if (channel) {
size_t position = 0;
// Iterates through the parameters and sets the modes accordingly.
for (size_t i = 0; i < new_mode.size(); i++) {
if (new_mode[i] == '+' || new_mode[i] == '-')
binary = new_mode[i]; // Updates the binary operator.
else if (new_mode[i] == 'l')
mode_list << channel_limit(shards, channel, position, binary, fd, mode_list.str(), arguments);
else if (new_mode[i] == 'o')
mode_list << operator_privilege(shards, channel, position, binary, fd, mode_list.str(), arguments);
else if (new_mode[i] == 'k')
mode_list << password_mode(shards, channel, position, binary, fd, mode_list.str(), arguments);
else if (new_mode[i] == 'i')
mode_list << invite_only(channel, binary, mode_list.str());
else if (new_mode[i] == 't')
mode_list << topic_restriction(channel, binary, mode_list.str());
else
respond(ERR_UNKNOWNMODE(client->get_displayname(), channel->get_name(), new_mode[i]), fd);
};
};
std::string final_mode = mode_list.str();
if (final_mode.empty())
return ;
channel->relay(RPL_CHANGEMODE(client->get_hostname(), channel->get_name(), final_mode, arguments));
};
// Points at the first character that is not name, mode or whitespace.
void Server::get_arguments(std::string command, std::string &name, std::string &mode, std::string ¶ms) {
std::istringstream stream(command);
stream >> name;
stream >> mode;
size_t position = command.find_first_not_of(name + mode + " \t\v");
if (position != std::string::npos)
params = command.substr(position);
};
// Returns a list of all the parameters split by commas.
std::vector<std::string> Server::get_params(std::string params) {
if (!params.empty() && params[0] == ':')
params.erase(params.begin());
std::string param;
std::vector<std::string> shards;
std::istringstream stream(params);
while (std::getline(stream, param, ',')) {
shards.push_back(param);
param.clear();
};
return (shards);
};
/* Sets the maximum population of a channel. If there are more users than
the maximum amount, nobody is kicked from the channel, but nobody can join.
Usage: MODE #Channel +l 42 */
std::string Server::channel_limit(std::vector<std::string> shards, Channel *channel, size_t &position, char binary, int fd, std::string mode_list, std::string &arguments) {
std::string param;
std::string limit;
param.clear();
limit.clear();
if (binary == '+') {
if (shards.size() > position) {
limit = shards[position++];
// If the limit is invalid, sends error.
if (is_positive(limit) == false)
respond(ERR_INVALIDMODEPARAM(channel->get_name(), "(l)"), fd);
else {
channel->set_mode_at_index(0, true);
channel->set_max_population(std::atoi(limit.c_str()));
// Adds "limit" to the list of arguments.
if (!arguments.empty())
arguments += " ";
arguments += limit;
// Appends the 'l' mode to the list of modes.
param = append_mode(mode_list, binary, 'l');
};
} else
respond(ERR_NEEDMODEPARAM(channel->get_name(), "(l)"), fd);
} else if (binary == '-' && channel->get_mode_at_index(0)) {
// If the operator is minus, removes the channel limit mode.
channel->set_mode_at_index(0, false);
channel->set_max_population(0);
param = append_mode(mode_list, binary, 'l');
};
return (param);
};
/* Promotes a member to operator, or demotes an operator to member,
depending on the binary operator before the letter 'o'.
Usage: MODE #Channel +o new_operator || MODE #Channel -o bad_operator */
std::string Server::operator_privilege(std::vector<std::string> shards, Channel *channel, size_t &position, char binary, int fd, std::string mode_list, std::string &arguments) {
std::string param;
std::string user;
param.clear();
user.clear();
// Gets the user.
if (shards.size() > position)
user = shards[position++];
else {
respond(ERR_NEEDMODEPARAM(channel->get_name(), "(o)"), fd);
return (param);
};
// If the user is not a member, sends error 441.
if (!channel->get_client_by_name(user)) {
respond(ERR_USERNOTINCHANNEL(user, channel->get_name()), fd);
return (param);
};
if (binary == '+') {
channel->set_mode_at_index(1, true);
if (channel->promote(user)) {
param = append_mode(mode_list, binary, 'o');
if (!arguments.empty())
arguments += " ";
arguments += user;
};
} else if (binary == '-') {
channel->set_mode_at_index(1, false);
if (channel->demote(user)) {
param = append_mode(mode_list, binary, 'o');
if (!arguments.empty())
arguments += " ";
arguments += user;
};
};
return (param);
};
/* New members of the channel can only join if they add the right password
as second argument of the JOIN command.
Usage: MODE #Channel +k password */
std::string Server::password_mode(std::vector<std::string> shards, Channel *channel, size_t &position, char binary, int fd, std::string mode_list, std::string &arguments) {
std::string param;
std::string password;
param.clear();
password.clear();
// Gets the password.
if (shards.size() > position)
password = shards[position++];
else {
respond(ERR_NEEDMODEPARAM(channel->get_name(), "(k)"), fd);
return (param);
};
std::cout << "APAGAR (TESTE) Password 1: " << password << std::endl;
// If the password is not up to the standards, sends error.
if (good_password(password) == false) {
respond(ERR_INVALIDMODEPARAM(channel->get_name(), "(k)"), fd);
return (param);
};
std::cout << "APAGAR (TESTE) Password 2: " << password << std::endl;
if (binary == '+') {
channel->set_mode_at_index(2, true);
channel->set_password(password);
std::cout << "APAGAR (TESTE) Password 3: " << password << std::endl;
if (!arguments.empty())
arguments += " ";
arguments += password;
std::cout << "APAGAR (TESTE) Password 4: " << password << std::endl;
param = append_mode(mode_list, binary, 'k');
} else if (binary == '-' && channel->get_mode_at_index(2)) {
if (password == channel->get_password()) {
channel->set_mode_at_index(2, false);
channel->set_password("");
param = append_mode(mode_list, binary, 'k');
} else
respond(ERR_KEYSET(channel->get_name()), fd);
};
return (param);
};
/* This channel is now only accessible if a member uses this command with
another client's display name. Only then they can successfully join.
Usage: MODE #Channel +i -> INVITE name #Channel */
std::string Server::invite_only(Channel *channel, char binary, std::string mode_list) {
std::string param;
param.clear();
if (binary == '+' && !channel->get_mode_at_index(3)) {
channel->set_mode_at_index(3, true);
channel->lock(true);
param = append_mode(mode_list, binary, 'i');
} else if (binary == '-' && channel->get_mode_at_index(3)) {
channel->set_mode_at_index(3, false);
channel->lock(false);
param = append_mode(mode_list, binary, 'i');
};
return (param);
};
std::string Server::topic_restriction(Channel *channel, char binary, std::string mode_list) {
std::string param;
param.clear();
if (binary == '+' && channel->get_mode_at_index(4) == false) {
channel->set_mode_at_index(4, true);
channel->set_topic_restriction(true);
param = append_mode(mode_list, binary, 't');
} else if (binary == '-' && channel->get_mode_at_index(4)) {
channel->set_mode_at_index(4, false);
channel->set_topic_restriction(false);
param = append_mode(mode_list, binary, 't');
};
return (param);
};
std::string Server::append_mode(std::string mode_list, char binary, char mode) {
std::stringstream stream;
stream.clear();
// Gets the last operator.
char last = '\0';
for (size_t i = 0; i < mode_list.size(); i++) {
if (mode_list[i] == '+' || mode_list[i] == '-')
last = mode_list[i];
};
//If the last operator differs from the current one, adds operator and mode.
if (last != binary)
stream << binary << mode;
else
stream << mode;
return (stream.str());
};
bool Server::is_positive(std::string &limit) {
// Checks if the passed limit contains only digits.
bool is_numeric = (limit.find_first_not_of("0123456789") == std::string::npos);
// Converts it to an integer.
int limit_value = std::atoi(limit.c_str());
// Checks if it's greater than 0.
bool is_positive = (limit_value > 0);
if (is_numeric && is_positive)
return (true);
return (false);
};
bool Server::good_password(std::string password) {
if (password.empty())
return (false);
for (size_t i = 0; i < password.size(); i++) {
if (std::isalnum(password[i] == false) && password[i] != '_')
return (false);
};
return (true);
};