This repository was archived by the owner on Aug 3, 2025. It is now read-only.
forked from Jaapet/ft_irc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
624 lines (545 loc) · 19.1 KB
/
Copy pathServer.cpp
File metadata and controls
624 lines (545 loc) · 19.1 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#include "Server.hpp"
#include "Session.hpp"
#include "Message.hpp"
#include "error.hpp"
#include "reply.hpp"
#include <fcntl.h>
#include <cstring>
#include <ctime>
#include <cerrno>
void handle_signal_ctrld(int signal) {
if (signal == SIGINT) {
Debug::Warning("CTRL-C Pressed, end of the server");
// Set the flag to indicate Ctrl+C was pressed
ctrlc_pressed++;
}
}
//CONSTRUCTOR//////////////////////////////////////////////////
Server::Server(std::string hostname, std::string pwd, uint16_t port): _hostname(hostname), _password(pwd), _port(port)
{
//Init
this->_select_timeout.tv_sec = 1;
this->_select_timeout.tv_usec = 0;
this->_servername = hostname;
this->_networkname = "Peppa network";
this->_version = "ft_irc-1.0";
this->_version_comments = "Released for ft_irc a 42school project MAY/JUNE 2024";
this->_available_user_modes = "0"; // no available user mode
this->_available_channel_modes = "itkol"; //user mode specified by the subject
this->_creation_date = Utils::getCurrentDate();
this->_creation_ts = std::time(NULL);
this->_info = "This server run on ft_irc a 42school project"; //, it was made by edfirmin ggualerzi and ndesprez for a 42 project. This server app doesnt handle SASL, SSL, MultiServer, server wide operator or idle";
this->_hoster_location = "Hosted in Antibes, France";
this->_hoster_organization = "Hosted by GMCG (985211358), personnal company of ggualerz";
this->_hoster_contact = "gregory.gualerzi@gmcg.fr";
this->_session_max = 0;
Debug::Info("Persue the server to not commit suicide");
this->_should_i_end_this_suffering = false;
Debug::Info("Attempt to initialize " + this->getHostName() + " IRC server");
try
{
signal(SIGINT, handle_signal_ctrld);
this->createSocket();
this->bindSocket();
this->launchListen();
this->mapCommands();
this->initSessionsFds();
Debug::Header();
Debug::Success("Launching of " + this->getHostName() + " IRC server was sucessfull " + Utils::getCurrentDate());
//g_sev = this;
this->handleConnections();//loop until ctrl-c
}
catch(const std::exception& e)
{
if(!ctrlc_pressed)
{
Debug::Error(e.what());
Server::cleanExit(1);
}
}
this->cleanExit(0);
}
Server::~Server()
{
}
//PRIVATE METHODS///////////////////////////////////////////////////////
// Private method, should only be launched by the constructor
void Server::createSocket(void)
{
this->_fd_socket = socket(AF_INET, SOCK_STREAM, 0); // TO COMMENT JPTA
if (this->_fd_socket < 0){
throw(Server::SocketCreationError());
}
}
void Server::bindSocket(void)
{
Debug::Info("Prepare structure for binding");
std::memset(&this->_address_socket, 0, sizeof(this->_address_socket)); //Full this->_address_socket with 0 bytes (bzero it, bzero is not part of c++ std lib)
this->_address_socket.sin_family = AF_INET; //Specify its a tcp ipv4
this->_address_socket.sin_addr.s_addr = INADDR_ANY; // Listen on every interfaces like a 0.0.0.0
this->_address_socket.sin_port = htons(this->getPort()); //htons is used to convert a 16-bit quantity from host byte order to network byte order
Debug::Info("Attempt to bind the socket");
if (bind(this->getFdSocket(), (struct sockaddr *)&this->getAddressSocket(), this->getLenSocket()) < 0){
throw(Server::SocketBindingError());
}
}
void Server::launchListen(void)
{
Debug::Info("Attempt to listening ");
if (listen(this->getFdSocket(), this->getBackLog()) < 0){
throw(Server::SocketListenError());
}
}
void Server::initSessionsFds(void)
{
Debug::Info("Attempt to create fd set for the clients sessions ");
FD_ZERO(&this->_read_sessions_fd); // Put the fd set at zero
FD_SET(this->getFdSocket(), &this->_read_sessions_fd); // put the read server fd in the set
FD_ZERO(&this->_write_sessions_fd); // Put the fd set at zero
FD_SET(this->getFdSocket(), &this->_write_sessions_fd); // put the write server fd in the set
}
void Server::mapCommands(void)
{
Debug::Info("Attempt to map commands ");
//Connection Messages
this->_commands["CAP"] = &(Command::cap);
this->_commands["AUTHENTICATE"] = &(Command::notimplanted);
this->_commands["PASS"] = &(Command::pass);
this->_commands["NICK"] = &(Command::nick);
this->_commands["USER"] = &(Command::user);
this->_commands["PING"] = &(Command::ping);
this->_commands["PONG"] = &(Command::pong);
this->_commands["OPER"] = &(Command::notimplanted);
this->_commands["QUIT"] = &(Command::quit);
this->_commands["ERROR"] = &(Command::notimplanted);
//Channel operations
this->_commands["JOIN"] = &(Command::join);
this->_commands["PART"] = &(Command::part);
this->_commands["TOPIC"] = &(Command::topic);
this->_commands["NAMES"] = &(Command::names);
this->_commands["LIST"] = &(Command::list);
this->_commands["INVITE"] = &(Command::invite);
this->_commands["KICK"] = &(Command::kick);
//Server Queries and Commands
this->_commands["MOTD"] = &(Command::motd);
this->_commands["VERSION"] = &(Command::version);
this->_commands["VERSIO"] = &(Command::version); //Add a version alias for hexchat who want to send privmsg instead
this->_commands["ADMIN"] = &(Command::admin);
this->_commands["CONNECT"] = &(Command::notimplanted);
this->_commands["LUSERS"] = &(Command::lusers);
this->_commands["TIME"] = &(Command::time);
this->_commands["STATS"] = &(Command::stats);
this->_commands["HELP"] = &(Command::help);
this->_commands["SRVHELP"] = &(Command::help);
this->_commands["INFO"] = &(Command::info);
this->_commands["MODE"] = &(Command::mode);
//Sending Messages
this->_commands["PRIVMSG"] = &(Command::privmsg);
this->_commands["NOTICE"] = &(Command::privmsg); //Pourquoi pas launch la cmd PRIVMSG
//User-Based Queries
this->_commands["WHO"] = &(Command::who);
this->_commands["WHOIS"] = &(Command::whois);
this->_commands["WHOWAS"] = &(Command::notimplanted);
//Operator Messages
this->_commands["KILL"] = &(Command::notimplanted);
this->_commands["REHASH"] = &(Command::notimplanted);
this->_commands["RESTART"] = &(Command::notimplanted);
this->_commands["SQUIT"] = &(Command::notimplanted);
//Optional Messages
this->_commands["AWAY"] = &(Command::away);
this->_commands["LINKS"] = &(Command::notimplanted);
this->_commands["USERHOST"] = &(Command::notimplanted);
this->_commands["WALLOPS"] = &(Command::wallops);
}
void Server::handleConnections(void)
{
int fd_max = this->getFdSocket(); //fd server fd, usually will be fd 3, will be the last fd to iterate until new cnx
fd_set read_fd_cpy, write_fd_cpy; //store a copy of session_fd, because macro will modify it
while(!(this->getKill()))
{
read_fd_cpy = this->_read_sessions_fd; //work on copy fdp set because macro will modify it, each while itteration it take back the master set
write_fd_cpy = this->_write_sessions_fd;
if(select(fd_max + 1, &read_fd_cpy, &write_fd_cpy, NULL, &this->_select_timeout) == -1) //select between read & write fd with a specified timeout
throw(Server::SelectCallError());
for(int i = 0; i <= fd_max; i++)
{
if(FD_ISSET(i, &read_fd_cpy)) //Check if FD is in the SET, first itteration trigger the server fd, then all fd put with FD_SET
{
if (i == this->getFdSocket()) // New client found lets make a session, basically the read would be on the fd of the server
this->handleNewSession(fd_max);
else // if its not a new client
this->handleReadEvents(i);
}
if(FD_ISSET(i, &write_fd_cpy)) //check if current fd is in the write fd_set
this->handleWriteEvents(i);
}
if(ctrlc_pressed)
this->_should_i_end_this_suffering = true;
}
}
void Server::handleNewSession(int &fd_max)
{
Session *tmp_sess = new Session(this);
socklen_t tmp_size = tmp_sess->getLenSocket();
tmp_sess->_fd_socket = accept(this->getFdSocket(), (struct sockaddr*)&tmp_sess->_address_socket, &tmp_size);
this->setNonBlockingFd(tmp_sess->_fd_socket);
if (tmp_sess->getFdSocket() == 0)
{
delete tmp_sess;
throw(Server::SessionCreationError());
}
else
{
FD_SET(tmp_sess->_fd_socket, &this->_read_sessions_fd); //add client fd to the read fd set
FD_SET(tmp_sess->_fd_socket, &this->_write_sessions_fd);//add client fd to the write fd set
if(tmp_sess->_fd_socket > fd_max)
fd_max = tmp_sess->_fd_socket; //fd max take the last fd to iterate on it
if(this->_sessions[tmp_sess->_fd_socket] != NULL)
throw(Server::DuplicatedSessionFd());
this->_sessions[tmp_sess->_fd_socket] = tmp_sess;
if(this->getActiveUsersNb() + this->getUnknownUsersNb() > this->_session_max)
this->_session_max = this->getActiveUsersNb() + this->getUnknownUsersNb();
Debug::Info("New connection from " + std::string(inet_ntoa(tmp_sess->_address_socket.sin_addr)));
}
}
void Server::handleReadEvents(int cur_fd)
{
char buffer[BUFFER_SIZE];
memset(buffer,'\0',BUFFER_SIZE);
int nbytes = recv(cur_fd, buffer, sizeof(buffer), 0);
if (nbytes == 0)
{
Debug::Warning("Connection closed by: " + std::string(inet_ntoa(this->_sessions[cur_fd]->_address_socket.sin_addr)) + " Attempting to close the session server side");
Session *sess_tmp = this->_sessions[cur_fd];
if(sess_tmp)
{
Message mess_tmp;
mess_tmp.payload = "Connection closed without QUIT";
Command::quit(this, sess_tmp, mess_tmp);
}
// this->killSession(cur_fd);
}
else if (nbytes < 0)
{
Debug::Warning("Error with a packet from: " + std::string(inet_ntoa(this->_sessions[cur_fd]->_address_socket.sin_addr)) + " closing the connection...");
Session *sess_tmp = this->_sessions[cur_fd];
if(sess_tmp)
{
Message mess_tmp;
mess_tmp.payload = "Connection closed without QUIT";
Command::quit(this, sess_tmp, mess_tmp);
}
// this->killSession(cur_fd);
}
else
{
if(nbytes >= BUFFER_SIZE) //IN Packet too long, not clearly explain what to do in rfc, so we choosed to truncate it, client should handle this shit
{
Debug::Warning("Packet too long from: " + std::string(inet_ntoa(this->_sessions[cur_fd]->_address_socket.sin_addr)) + " truncate it");
buffer[BUFFER_SIZE - 2] = '\r';//ADD CR
buffer[BUFFER_SIZE - 1] = '\n';//ADD LF
}
buffer[nbytes] = '\0'; //Terminate buffer with \0, good old char *
Debug::Message(std::string(buffer), cur_fd);
this->_sessions[cur_fd]->addRecvBuffer(buffer);
if(Utils::containsCRLF(this->_sessions[cur_fd]->getRecvBuffer()))
{
this->executeCommands(this->splitBuffer(this->_sessions[cur_fd]->getRecvBuffer()), cur_fd);
if(this->_sessions[cur_fd] != NULL)
this->_sessions[cur_fd]->clearRecvBuffer();
}
}
}
void Server::executeCommands(std::vector<std::string> command_to_execute, int cur_fd)
{
for(size_t i = 0; i < command_to_execute.size() ; i++)
{
Message tmp_msg;
// std::string inBuffer(buffer);
// inBuffer = this->removeCRLF(inBuffer);
this->parseMessage(command_to_execute[i], tmp_msg);
if(!(tmp_msg.command.empty()))
{
tmp_msg.command = Utils::strToUpper(tmp_msg.command);
if(tmp_msg.command == "QUIT") // In case of using QUIT, the session will be deleted before the getting of _sendBuffer, avoid a segfault
this->_commands[tmp_msg.command](this, this->_sessions[cur_fd], tmp_msg);
else if(this->_commands[tmp_msg.command] != NULL)
this->_sessions[cur_fd]->getSendBuffer() += this->_commands[tmp_msg.command](this, this->_sessions[cur_fd], tmp_msg);
else
this->_sessions[cur_fd]->getSendBuffer() += Error::ERR_UNKNOWNCOMMAND_421(this, this->_sessions[cur_fd], tmp_msg);
}
}
}
void Server::handleWriteEvents(int cur_fd)
{
if(this->_sessions[cur_fd] && !this->_sessions[cur_fd]->getSendBuffer().empty()) //if sendBuffer of the session is not empty
{
Debug::Message("[SEND]" + this->_sessions[cur_fd]->getSendBuffer(), cur_fd);
send(cur_fd, this->_sessions[cur_fd]->getSendBuffer().c_str(), this->_sessions[cur_fd]->getSendBuffer().length() ,MSG_NOSIGNAL);
this->_sessions[cur_fd]->getSendBuffer().clear();
}
}
//PUBLIC METHODS//////////////////////////////////////////////////////
void Server::killSession(int const session_fd)
{
this->killSession(session_fd, true);
}
void Server::killSession(int const session_fd, bool erase_it)
{
//send waiting buffer
Utils::sendBufferNow(this->_sessions[session_fd]);
FD_CLR(session_fd, &this->_read_sessions_fd);
FD_CLR(session_fd, &this->_write_sessions_fd);
if(shutdown(session_fd, SHUT_RDWR) == -1)
Debug::Error("Cannot properly shutdown session: " + Utils::itoa(session_fd));
delete (this->_sessions[session_fd]);
if(erase_it)
this->_sessions.erase(session_fd);
close(session_fd);
Debug::Info("Session closed");
}
void Server::parseMessage(const std::string &message, Message &outmessage)
{
if(message[0] == '\r' && message[1] == '\n')
return;
outmessage.clear();
size_t pos = 0;
size_t end = message.size();
// Check for sender (prefix)
if (message[pos] == ':') {
size_t prefixEnd = message.find(' ', pos);
if (prefixEnd != std::string::npos)
{
outmessage.sender = message.substr(pos + 1, prefixEnd - pos - 1);
pos = prefixEnd + 1;
}
else
{
return; // Invalid message format
}
}
else
{
outmessage.sender = "";
}
// Get command
size_t commandEnd = message.find(' ', pos);
if (commandEnd != std::string::npos)
{
outmessage.command = message.substr(pos, commandEnd - pos);
pos = commandEnd + 1;
}
else
{
outmessage.command = message.substr(pos);
return;
}
// Get params and payload
while (pos < end) {
if (message[pos] == ':')
{
// Trailing param (payload)
outmessage.payload = message.substr(pos + 1);
break;
}
size_t paramEnd = message.find(' ', pos);
if (paramEnd != std::string::npos)
{
outmessage.params.push_back(message.substr(pos, paramEnd - pos));
pos = paramEnd + 1;
}
else
{
outmessage.params.push_back(message.substr(pos));
break;
}
}
}
void Server::setNonBlockingFd(int fd)
{
if (fcntl(fd, F_SETFL,O_NONBLOCK) == -1) {
throw std::runtime_error("fcntl F_SETFL failed");
}
}
Session *Server::getSession(std::string const &nickname)
{
std::map<int, Session*>::iterator it;
Session* foundSession = NULL;
for (it = this->_sessions.begin(); it != this->_sessions.end(); ++it)
{
if (it->second != NULL && Utils::strToUpper(it->second->getNickName()) == Utils::strToUpper(nickname))
{
foundSession = it->second;
break;
}
}
return(foundSession);
}
std::vector<std::string> Server::splitBuffer(std::string const &str)
{
std::string del = "\r\n";
std::string tmp2;
std::string tmp = str;
size_t f = 4;
std::vector<std::string> com;
while (f != std::string::npos)
{
f = tmp.find(del);
tmp2 = tmp.substr(0, f);
if (!tmp2.empty())
com.push_back(tmp2);
if (f == std::string::npos)
break;
tmp = tmp.substr(f + del.size(), tmp.size() - 1);
}
return (com);
}
void Server::cleanExit(int exitcode)
{
//Clean Channels
std::map<std::string, Channel*>::iterator it1;
if (this->_channels.size() > 0)
{
it1 = this->_channels.begin();
while (it1 != this->_channels.end())
{
// Save the next iterator before erasing the current one
std::map<std::string, Channel*>::iterator toErase = it1;
++it1; // Move to the next element
// Perform the operation that removes the current element
if(toErase->second != NULL)
{
Debug::Info("cleanExit() Killing channels: " + toErase->second->get_name());
this->removeChannel(toErase->first);
}
// this->_channels.erase(toErase); // Erase the element after moving the iterator
}
}
//Clean Essions
std::map<int, Session*>::iterator it2;
if (this->_sessions.size() > 0)
{
it2 = this->_sessions.begin();
while (it2 != this->_sessions.end())
{
// Save the next iterator before erasing the current one
std::map<int, Session*>::iterator toErase = it2;
++it2; // Move to the next element
// Perform the operation that removes the current element
if(toErase->second != NULL)
{
Debug::Info("cleanExit() Killing session: " + toErase->second->getNickName());
this->killSession(toErase->first, false);
}
this->_sessions.erase(toErase->first); // Erase the element after moving the iterator
}
}
this->_should_i_end_this_suffering = true;
try
{
Debug::Info("Attempt to close server Fd");
close(this->getFdSocket());
Debug::Info("Attempt to shutdown server socket");
shutdown(this->getFdSocket(), SHUT_RDWR);
}
catch(const std::exception& e)
{
Debug::Error(e.what());
std::exit(1);
}
Debug::Success("Server is successfully closed");
std::exit(exitcode);
}
Channel *Server::getChannel(std::string channel_name)
{
std::map<std::string,Channel *> channels = this->getChannels();
std::map<std::string,Channel *>::iterator it = channels.begin();
while(it != channels.end())
{
if(!(it->first.empty()) && Utils::strToUpper(it->first) == Utils::strToUpper(channel_name))
return(it->second);
it++;
}
return(NULL);
}
void Server::removeChannel(std::string chan_name)
{
std::vector<std::string> users = this->_channels.at(chan_name)->get_users();
for(size_t i = 0; i < users.size(); i++)
{
this->getSession(users[i])->setChannel(NULL);
}
delete this->_channels.at(chan_name);
this->_channels.erase(chan_name);
}
size_t Server::getActiveUsersNb(void)
{
std::map<int, Session*> sessions = this->getSessions();
std::map<int, Session*>::iterator it = sessions.begin();
size_t users = 0;
while(it != sessions.end())
{
if(it->second != NULL && it->second->getAuthenticated() == true)
users++;
it++;
}
return(users);
}
size_t Server::getOperatorUsersNb(void)
{
std::map<int, Session*> sessions = this->getSessions();
std::map<int, Session*>::iterator it = sessions.begin();
size_t users = 0;
while(it != sessions.end())
{
if( it->second != NULL && \
it->second->getAuthenticated() == true &&\
it->second->getChannel() != NULL && \
it->second->getChannel()->is_op(it->second->getNickName()) == true)
users++;
it++;
}
return(users);
}
size_t Server::getUnknownUsersNb(void)
{
std::map<int, Session*> sessions = this->getSessions();
std::map<int, Session*>::iterator it = sessions.begin();
size_t users = 0;
while(it != sessions.end())
{
if(it->second != NULL && it->second->getAuthenticated() == false)
users++;
it++;
}
return(users);
}
size_t Server::getChannelsNb(void)
{
std::map<std::string, Channel*> channels = this->getChannels();
std::map<std::string, Channel*>::iterator it = channels.begin();
size_t chan_nb = 0;
while(it != channels.end())
{
if(it->second != NULL)
chan_nb++;
it++;
}
return(chan_nb);
}
void Server::getUptime(size_t &day, size_t &hour, size_t &minute, size_t &second)
{
// Parse the Unix timestamp from the server creation date
std::time_t creationTime = this->getCreationTs();
// Get the current time as a Unix timestamp
std::time_t currentTime = std::time(0);
// Calculate the difference in seconds
std::time_t diff = currentTime - creationTime;
// Convert the difference into days, hours, minutes, and seconds
day = diff / (24 * 3600);
diff %= (24 * 3600);
hour = diff / 3600;
diff %= 3600;
minute = diff / 60;
second = diff % 60;
}