-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplies.cpp
More file actions
452 lines (438 loc) · 18.4 KB
/
Replies.cpp
File metadata and controls
452 lines (438 loc) · 18.4 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "Server.hpp"
void Server::LUSERS_handler(msg_parse &command, User &user)
{
write_reply(user, RPL_LUSERCLIENT, command);
write_reply(user, RPL_LUSEROP, command);
write_reply(user, RPL_LUSERUNKNOWN, command);
write_reply(user, RPL_LUSERCHANNELS, command);
write_reply(user, RPL_LUSERME, command);
}
void Server::MOTD_handler(msg_parse &command, User &user)
{
write_reply(user, RPL_MOTDSTART, command);
write_reply(user, RPL_MOTD, command);
write_reply(user, RPL_ENDOFMOTD, command);
}
void Server::NAMES_handler(msg_parse &command, User &user)
{
write_reply(user, RPL_NAMREPLY, command);
write_reply(user, RPL_ENDOFNAMES, command);
}
void Server::LIST_handler(msg_parse &command, User &user)
{
write_reply(user, RPL_LIST, command);
write_reply(user, RPL_LISTEND, command);
}
std::string names_reply(User &user, std::string &serv_name)
{
return ("\n:" + serv_name + " 353 "+ user.get_nickname() + " channel * :");
}
std::string names_reply(User &user, Channel &channel, std::string &serv_name, char c)
{
if (c == '=')
return (":" + serv_name + " 353 "+ user.get_nickname() + " = " + channel.get_name() + " :");
if (c == '*')
return (":" + serv_name + " 353 "+ user.get_nickname() + " * " + channel.get_name() + " :");
if (c == '@')
return (":" + serv_name + " 353 "+ user.get_nickname() + " @ " + channel.get_name() + " :");
return "";
}
int Server::write_reply(User &user, int reply_code, msg_parse &command)
{
if (reply_code == RPL_WELCOME)
{
std::string full_msg = ":" + this->__name + " 001 :Welcome to the Internet Relay Network :" + user.get_nickname() + "!" + user.get_username() + "@" + user.get_hostname() + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_MYINFO)
{
std::string full_msg = ":" + this->__name + " 004 Server_name : this is your host " + this->__name + "\n 005 : * Server version : 0.1.\nAvailable Commands :\n-MODE: <channel> *( ( \"-\" \"+\" ) *<modes> *<modeparams>.\n-AWAY: <text>.\n-LUSERS: <no params>\n-MOTD: <no params>\n-NAMES: <channel> *( \",\" <channel> ).\n-LIST: <channel> *( \",\" <channel> ).\n-PRIVMSG: <msgtarget> <text to be sent>.\n-NOTICE: <msgtarget> <text>.\n-QUIT: <Quit Message>.\n-OPER: <name> <password>.\n-TOPIC: <channel> [ <topic> ].\n-JOIN: <channel> *( \",\" <channel> ) [ <key> *( \",\" <key> ) ] / \"0\".\n-PART: <channel> *( \",\" <channel> ) [ <Part Message> ].\n-INVITE: <nickname> <channel>.\n-KICK: <channel> *( \",\" <channel> ) <user> *( \",\" <user> ) [<comment>].\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NICKNAMEINUSE)
{
std::string full_msg = ":" + this->__name + " 433 NICK :Nickname is already in use\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_NOWAWAY)
{
std::string full_msg = ":" + this->__name + " 306 AWAY :You have been marked as being away\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_UNAWAY)
{
std::string full_msg = ":" + this->__name + " 305 AWAY :You are no longer marked as being away\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_ERRONEUSNICKNAME)
{
std::string full_msg = ":" + this->__name + " 432 " + command.get_cmd_params().front() + " NICK :Erroneous nickname\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NONICKNAMEGIVEN)
{
std::string full_msg = ":" + this->__name + " 431 NICK :No nickname given\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NOTREGISTERED)
{
std::string full_msg = ":" + this->__name + " 451 " + command.get_cmd() + ":You have not registered\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NEEDMOREPARAMS)
{
std::string full_msg = ":" + this->__name + " 461 " + command.get_cmd() + " :Not enough parameters\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_PASSWDMISMATCH)
{
std::string full_msg = ":" + this->__name+ " 464 " + command.get_cmd() + " :Password incorrect\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_ALREADYREGISTRED)
{
std::string full_msg = ":" + this->__name + " 462 " + command.get_cmd() + " :Unauthorized command (already registered)\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_UMODEUNKNOWNFLAG)
{
std::string full_msg = ":" + this->__name + " 501 MODE :Unknown MODE flag\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_USERSDONTMATCH)
{
std::string full_msg = ":" + this->__name + " 502 MODE :Cannot change mode for other users\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_YOUREOPER)
{
std::string full_msg = ":" + this->__name + " 381 OPER :You are now an IRC operator\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_TOPIC)
{
std::string full_msg = ":" + this->__name + " 332 " + command.get_cmd() + command.get_cmd_params()[0] + " :" + command.get_cmd_params()[1] + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_INVITEONLYCHAN)
{
std::string full_msg = ":" + this->__name + " 473 "+ command.get_cmd() + command.get_cmd_params()[0] + " :Cannot join channel (+i)\n" + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_BANNEDFROMCHAN)
{
std::string full_msg = ":" + this->__name + " 474 " + command.get_cmd() + command.get_cmd_params()[0] + " :Cannot join channel (+b)\n" + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_BADCHANNELKEY)
{
std::string full_msg = ":" + this->__name + " 475 " + command.get_cmd() + command.get_cmd_params()[0] + " :Cannot join channel (+k)\n" + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_CHANNELISFULL)
{
std::string full_msg = ":" + this->__name + " 471 " + command.get_cmd() + command.get_cmd_params()[0] + " :Cannot join channel (+l)\n" + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NOTONCHANNEL)
{
std::string full_msg = ":" + this->__name + " 442 " + command.get_cmd() + command.get_cmd_params()[0] + " :You're not on that channel\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NORECIPIENT)
{
std::string full_msg = ":" + this->__name + " 411 :No recipient given (" + command.get_cmd() + ") \n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_CANNOTSENDTOCHAN)
{
std::string full_msg = ":" + this->__name + " " + command.get_cmd() + " 404 :Cannot send to channel" + "\n" ;
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NOTEXTTOSEND)
{
std::string full_msg = ":" + this->__name + " " + command.get_cmd() + " 412 :No text to send\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NOSUCHNICK)
{
std::string full_msg = ":" + this->__name + " 401 * :No such nick/channel\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NOSUCHCHANNEL)
{
std::string full_msg = ":" + this->__name + " 403 " + command.get_cmd_params()[0]+ " :No such channel\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_TOOMANYTARGETS)
{
std::string full_msg = ":" + this->__name + " " + command.get_cmd() + " 407 :Too many recipients\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_AWAY)
{
std::string full_msg = ":" + this->__name + " " + command.get_cmd() + " 301 : " + command.get_cmd_params()[0] + "\n" + user.get_away_msg() + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_UNKNOWNCOMMAND)
{
std::string full_msg = ":" + this->__name + " 421 " + command.get_cmd() + " :Unknown command\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_LUSERCLIENT)
{
std::string full_msg = ":" + this->__name + " 251 " + user.get_nickname() + " :There are " + std::to_string(this->__users.size()) + " users\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_LUSEROP)
{
int nbr_of_OPs = 0;
for (std::list<User>::iterator it = this->__users.begin(); it != this->__users.end(); it++)
{
if ((*it).get_modes().get_o() || (*it).get_modes().get_O())
nbr_of_OPs++;
}
std::string full_msg = ":" + this->__name + " 252 " + user.get_nickname() + " " + std::to_string(nbr_of_OPs) + " :operator(s) online\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_LUSERUNKNOWN)
{
std::string full_msg = ":" + this->__name + " 253 " + user.get_nickname() + " " + std::to_string(this->__nbr_of_unknown_conns) + " :unknown connection(s)\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_LUSERCHANNELS)
{
std::string full_msg = ":" + this->__name + " 254 " + user.get_nickname() + " " + std::to_string(this->__channels.size()) + " :channels formed\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_LUSERME)
{
std::string full_msg = ":" + this->__name + " 255 " + user.get_nickname() + " :I have " + std::to_string(this->__users.size()) + " clients\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_USERONCHANNEL)
{
std::string full_msg = ":" + this->__name + " 443 " + command.get_cmd_params()[0] + " :is already on channel\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_CHANOPRIVSNEEDED)
{
std::string full_msg = ":" + this->__name + " " + command.get_cmd() + " 482 " + command.get_cmd_params()[1] + " :You're not channel operator\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_NOMOTD)
{
std::string full_msg = ":" + this->__name + " 422 MOTD :MOTD file is missing\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_MOTDSTART)
{
std::string full_msg = ":" + this->__name + " 375 MOTD :- " + this->__name + " Message of the day - \n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_MOTD)
{
std::ifstream motd_file;
std::string line;
std::string full_msg;
motd_file.open("motd.txt", std::ifstream::in);
if (!motd_file)
write_reply(user, ERR_NOMOTD, command);
else
{
while (std::getline(motd_file, line))
{
line += "\n";
full_msg = ":" + this->__name + " 372 MOTD :- " + line;
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
full_msg.clear();
}
}
}
else if (reply_code == RPL_ENDOFMOTD)
{
std::string full_msg = ":" + this->__name + " 376 MOTD :End of MOTD command\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_NAMREPLY)
{
std::string full_msg;
int found = 0;
if (command.get_cmd_params().size() == 0) // LIST ALL CHANNELS AND THEIR USERS (IF VISIBLE)
{
for (std::list<Channel>::iterator it = this->__channels.begin(); it != this->__channels.end(); it++)
{
if (!(*it).get_modes().get_p() && !(*it).get_modes().get_s())
{
full_msg = names_reply(user, *it, this->__name, '=');
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (find_user_in_channel(user, *it) == *((*it).get_users().end()) && !user.get_modes().get_o())
continue ;
if ((*it).get_modes().get_p())
{
full_msg = names_reply(user, *it, this->__name, '*');
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if ((*it).get_modes().get_s())
{
full_msg = names_reply(user, *it, this->__name, '@');
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
for(std::list<User *>::iterator it2 = (*it).get_users().begin(); it2 != (*it).get_users().end(); it2++)
{
if (!(*it2)->get_modes().get_i())
{
full_msg = (*it2)->get_nickname();
full_msg = (*it).is_operator((*it2)->get_nickname()) == true ? "@" + full_msg : ((*it).has_voice((*it2)->get_nickname()) == true ? "+" + full_msg : full_msg);
full_msg = (*it2) != (*it).get_users().back() ? full_msg + " " : full_msg + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
found = -1;
}
else
found = 0;
}
}
// LIST USERS THAT ARE VISIBLE BUT NOT ON ANY CHANNEL OR ON A NONE VISIBLE CHANNEL
full_msg = names_reply(user, this->__name);
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
for(std::list<User>::iterator it = this->__users.begin(); it != this->__users.end(); it++)
{
if ((*it).get_channels().size() == 0)
{
found = -1;
if (!(*it).get_modes().get_i())
{
full_msg = (*it).get_nickname();
full_msg = (*it).is_channel_op() == true ? "@" + full_msg : ((*it).has_voice() == true ? "+" + full_msg : full_msg);
full_msg = *it != this->__users.back() ? full_msg + " " : full_msg + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
}
else
found = 0;
}
if (found == 0)
send(user.get_fd(), "\n", 1, 0);
}
else // LIST THE GIVEN CHANNEL'S (OR MULTIPLE CHANNELS) NAME(s) AND USERS
{
std::string ch_name = command.get_cmd_params()[0];
std::string name;
std::string full_name;
std::list<Channel>::iterator it;
while (!(name = split_channel_names(ch_name)).empty())
{
if ((it = find_channel(name[0], name.substr(1, name.length() - 1))) != this->__channels.end())
{
full_name = (*it).get_prefix() + (*it).get_name();
if (!(*it).get_modes().get_p() && !(*it).get_modes().get_s())
{
full_msg = names_reply(user, *it, this->__name, '=');
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (find_user_in_channel(user, *it) == *((*it).get_users().end()) && !user.get_modes().get_o())
continue ;
if ((*it).get_modes().get_p())
{
full_msg = names_reply(user, *it, this->__name, '*');
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if ((*it).get_modes().get_s())
{
full_msg = names_reply(user, *it, this->__name, '@');
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
for(std::list<User *>::iterator it2 = (*it).get_users().begin(); it2 != (*it).get_users().end(); it2++)
{
if (!(*it2)->get_modes().get_i())
{
full_msg = (*it2)->get_nickname();
full_msg = (*it).is_operator((*it2)->get_nickname()) == true ? "@" + full_msg : ((*it).has_voice((*it2)->get_nickname()) == true ? "+" + full_msg : full_msg);
full_msg = (*it2) != (*it).get_users().back() ? full_msg + " " : full_msg + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
found = -1;
}
else
found = 0;
}
}
else
write_reply(user, ERR_NOSUCHNICK, command);
}
}
}
else if (reply_code == RPL_ENDOFNAMES)
{
std::string full_msg = ":" + this->__name + " " + command.get_cmd() + " 366 :End of Names command\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_LIST)
{
std::string full_msg;
std::string ch_name;
if (command.get_cmd_params().size() == 0)
{
for(std::list<Channel>::iterator it = this->__channels.begin(); it != this->__channels.end(); it++)
{
if ((!(*it).get_modes().get_p() && !(*it).get_modes().get_s()) || user.get_modes().get_o() || find_user_in_channel(user, (*it)) != *(*it).get_users().end())
{
full_msg = ":" + this->__name + " 322 " + user.get_nickname() + " " + (*it).get_prefix() + (*it).get_name() + " " + std::to_string((*it).get_users().size()) + " :" + (*it).get_topic() + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
full_msg.clear();
}
}
}
else
{
std::list<Channel>::iterator it;
std::string name;
ch_name = command.get_cmd_params()[0];
while (!(name = split_channel_names(ch_name)).empty())
{
if ((it = find_channel(name[0], name.substr(1, name.size() - 1))) != this->__channels.end())
{
if ((!(*it).get_modes().get_p() && !(*it).get_modes().get_s()) || user.get_modes().get_o() || find_user_in_channel(user, (*it)) != *(*it).get_users().end())
{
full_msg = ":" + this->__name + " 322 " + user.get_nickname() + " " + (*it).get_prefix() + (*it).get_name() + " :" + (*it).get_topic() + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
full_msg.clear();
}
}
else
write_reply(user, ERR_NOSUCHNICK, command);
}
}
}
else if (reply_code == RPL_LISTEND)
{
std::string full_msg = ":" + this->__name + " 323 " + command.get_cmd() + " :End of LIST\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_UNKNOWNMODE)
{
std::string full_msg = command.get_cmd() + " 472 " + command.get_cmd_params()[1][command.get_pos() + 1] + " is unknown mode char to me for " + (command.get_cmd_params()[0] + 1) + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_USERNOTINCHANNEL)
{
std::string full_msg;
if (command.get_cmd_params().size() == 3)
full_msg = command.get_cmd() + " 441 " + command.get_cmd_params()[2] + " " + command.get_cmd_params()[0] + " :they aren't on that channel\n";
else
full_msg = command.get_cmd() + " 441 " + user.get_nickname() + " " + command.get_cmd_params()[0] + " :they aren't on that channel\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == ERR_KEYSET)
{
std::string full_msg = command.get_cmd() + " 467 " + command.get_cmd_params()[0] + " :Channel key already set" + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
else if (reply_code == RPL_CHANNELMODEIS)
{
std::string full_msg = ":" + this->__name + " " + command.get_cmd() + " 324 " + command.get_cmd_params()[0] + " " + command.get_cmd_params()[1][0] + command.get_cmd_params()[1][command.get_pos()];
full_msg = command.get_cmd_params().size() == 3 ? full_msg + " " + command.get_cmd_params()[2] + "\n" : full_msg + "\n";
send(user.get_fd(), full_msg.c_str(), full_msg.size(), 0);
}
return 1;
}