/
server.cpp
562 lines (440 loc) · 15.7 KB
/
server.cpp
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
/*NEXUS IRC session BNC, by Subsentient. This software is public domain.*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#ifdef WIN
#include <winsock2.h>
#else
#include <fcntl.h>
#endif
#include <list>
#include <string>
#include "server.h"
#include "netcore.h"
#include "config.h"
#include "nexus.h"
#include "state.h"
#include "scrollback.h"
#include "irc.h"
#include "substrings/substrings.h"
/**This file has our IRC pseudo-server that we run ourselves and its interaction with clients.**/
//Globals
std::list<struct ClientListStruct> ClientListCore;
struct ClientListStruct *CurrentClient, *PreviousClient;
//Prototypes
namespace Server
{
static void SendChannelNamesList(const class ChannelList *const Channel, struct ClientListStruct *Client);
static void SendChannelRejoin(const class ChannelList *const Channel, const int ClientDescriptor);
static void SendIRCWelcome(const int ClientDescriptor);
}
//Functions
struct ClientListStruct *Server::ClientList::Lookup(const int Descriptor)
{
std::list<struct ClientListStruct>::iterator Iter = ClientListCore.begin();
for (; Iter != ClientListCore.end(); ++Iter)
{
if (Iter->Descriptor == Descriptor)
{
return &*Iter; //Not used to doing this kind of weird shit. That'd seem really stupid and redundant in C, where Iter would be a pointer.
}
}
return NULL;
}
struct ClientListStruct *Server::ClientList::Add(const struct ClientListStruct *const InStruct)
{
ClientListCore.push_front(*InStruct);
return &*ClientListCore.begin();
}
void Server::ClientList::Shutdown(void)
{
ClientListCore.clear();
PreviousClient = NULL;
CurrentClient = NULL;
}
bool Server::ClientList::Del(const int Descriptor)
{
std::list<struct ClientListStruct>::iterator Iter = ClientListCore.begin();
for (; Iter != ClientListCore.end(); ++Iter)
{
if (Iter->Descriptor == Descriptor)
{
if (&*Iter == CurrentClient) CurrentClient = reinterpret_cast<struct ClientListStruct*>(-1);
if (&*Iter == PreviousClient) PreviousClient = reinterpret_cast<struct ClientListStruct*>(-1);
ClientListCore.erase(Iter);
return true;
}
}
return false;
}
bool Server::ForwardToAll(const char *const InStream)
{ //This function sends the provided text stream to all clients. Very simple.
std::list<struct ClientListStruct>::iterator Iter = ClientListCore.begin();
for (; Iter != ClientListCore.end(); ++Iter)
{
Iter->SendLine(InStream);
}
return true;
}
bool Server::NukeClient(const int Descriptor)
{ //Close the descriptor, remove from select() tracking, and purge him from our minds.
struct ClientListStruct *Client = Server::ClientList::Lookup(Descriptor);
if (!Client) return false;
Net::Close(Client->Descriptor);
NEXUS::DescriptorSet_Del(Client->Descriptor);
Server::ClientList::Del(Client->Descriptor);
return true;
}
void Server::SendQuit(const int Descriptor, const char *const Reason)
{ //Tells all clients or just one client to quit
std::list<struct ClientListStruct>::iterator Iter = ClientListCore.begin();
char OutBuf[2048];
for (; Iter != ClientListCore.end(); ++Iter)
{
//If not on "everyone" mode we check if the descriptor matches.
if (Descriptor != -1 && Descriptor != Iter->Descriptor) continue;
if (Reason)
{
snprintf(OutBuf, sizeof OutBuf, ":%s!%s@%s QUIT :%s\r\n", IRCConfig.Nick, Iter->Ident, Iter->IP, Reason);
}
else
{
snprintf(OutBuf, sizeof OutBuf, ":%s!%s@%s QUIT :Disconnected from NEXUS.\r\n", IRCConfig.Nick, Iter->Ident, Iter->IP);
}
Iter->SendLine(OutBuf);
}
}
static void Server::SendIRCWelcome(const int ClientDescriptor)
{
char OutBuf[2048];
struct ClientListStruct *Client = Server::ClientList::Lookup(ClientDescriptor);
std::map<std::string, ChannelList>::iterator Iter = ChannelListCore.begin();
const int ClientCount = ClientListCore.size();
if (!Client) return;
//First thing we send is our greeting, telling them they're connected OK.
snprintf(OutBuf, sizeof OutBuf, ":" NEXUS_FAKEHOST " 001 %s :NEXUS is forwarding you to server %s:%hu\r\n",
IRCConfig.Nick, IRCConfig.Server, IRCConfig.PortNum); //Putting IRCConfig.Nick here is the same as sending a NICK command.
Client->SendLine(OutBuf);
//Tell them to join all channels we are already in.
for (; Iter != ChannelListCore.end(); ++Iter)
{
Server::SendChannelRejoin(&Iter->second, Client->Descriptor);
}
//Count clients for our next cool little trick.
/**Send a MOTD.**/
//Send the beginning.
snprintf(OutBuf, sizeof OutBuf, ":" NEXUS_FAKEHOST " 375 %s :Welcome to NEXUS " NEXUS_VERSION
". You are being forwarded to \"%s:%hu\".\r\n", IRCConfig.Nick, IRCConfig.Server, IRCConfig.PortNum);
Client->SendLine(OutBuf);
//For dumb bots that connect, make it abundantly clear what their nick should be.
snprintf(OutBuf, sizeof OutBuf, ":%s!%s@%s NICK :%s\r\n", Client->OriginalNick, Client->Ident, Client->IP, IRCConfig.Nick);
Client->SendLine(OutBuf);
//Send the middle.
snprintf(OutBuf, sizeof OutBuf,
":" NEXUS_FAKEHOST " 372 %s :There are currently %d other instances connected to this NEXUS server.\r\n",
IRCConfig.Nick, ClientCount - 1);
Client->SendLine(OutBuf);
//Send the end.
snprintf(OutBuf, sizeof OutBuf, ":" NEXUS_FAKEHOST " 376 %s :End of MOTD.\r\n", IRCConfig.Nick);
Client->SendLine(OutBuf);
//Send all scrollback.
struct ScrollbackList *SWorker = ScrollbackCore;
char ScrollBuf[2048], SelfOrigin[256];
snprintf(SelfOrigin, sizeof SelfOrigin, "%s!%s@%s", IRCConfig.Nick, Client->Ident, Client->IP);
for (; SWorker; SWorker = SWorker->Next)
{ //Loop through sending the scrollback.
struct tm *TimeStruct = localtime(&SWorker->Time);
char TimeBuf[128];
const char *Origin = SWorker->Origin;
strftime(TimeBuf, sizeof TimeBuf, "%I:%M:%S %p %Y-%m-%d", TimeStruct);
if (!Origin)
{ //We want to use ourselves as the origin if none specified.
Origin = SelfOrigin;
}
if (SubStrings.StartsWith("\1ACTION ", SWorker->Msg))
{
char *NewMsg = (char*)calloc(strlen(SWorker->Msg) + 1, 1);
//Don't bother trying to understand the arithmetic here.
SubStrings.Copy(NewMsg, SWorker->Msg + (sizeof "\1ACTION " - 1), (strlen(SWorker->Msg) + 1) - (sizeof "\1ACTION " - 1));
//Chop off the \x01 at the end.
SubStrings.StripTrailingChars(NewMsg, "\001");
//Build the message.
char Nick[64], Ident[256], Mask[512];
IRC::BreakdownNick(Origin, Nick, Ident, Mask);
snprintf(ScrollBuf, sizeof ScrollBuf, ":%s PRIVMSG %s :\0034[%s]\3 ** %s %s **\r\n",
Origin, (SWorker->Target ? SWorker->Target : IRCConfig.Nick), TimeBuf, Nick, NewMsg);
free(NewMsg);
}
else
{
snprintf(ScrollBuf, sizeof ScrollBuf, ":%s PRIVMSG %s :\0034[%s]\3 %s\r\n",
Origin, (SWorker->Target ? SWorker->Target : IRCConfig.Nick), TimeBuf, SWorker->Msg);
}
//Now send it to the client.
Client->SendLine(ScrollBuf);
}
}
static void Server::SendChannelNamesList(const class ChannelList *const Channel, struct ClientListStruct *Client)
{
char OutBuf[2048];
unsigned Ticker = 1;
std::map<std::string, struct UserStruct> &UserListRef = Channel->GetUserList();
std::map<std::string, struct UserStruct>::iterator Iter = UserListRef.begin();
SendBegin:
std::string OutString = std::string(":" NEXUS_FAKEHOST " 353 ") + IRCConfig.Nick + " = " + Channel->GetChannelName() + " :";
for (Ticker = 1; Iter != UserListRef.end(); ++Iter, ++Ticker)
{
const struct UserStruct *Worker = &Iter->second;
//Reconstitute the mode flag for this user.
const char Sym = State::UserModes_Get_Mode2Symbol(Worker->Modes);
if (Sym)
{
OutString += Sym;
}
OutString += Worker->Nick;
std::map<std::string, struct UserStruct>::iterator TempIter = Iter;
++TempIter;
if (Ticker == 20 || TempIter == UserListRef.end())
{
OutString += "\r\n";
Client->SendLine(OutString.c_str());
if (TempIter != UserListRef.end())
{
goto SendBegin;
}
}
else
{
OutString += " ";
}
}
snprintf(OutBuf, sizeof OutBuf, ":" NEXUS_FAKEHOST " 366 %s %s :End of /NAMES list.\r\n", IRCConfig.Nick, Channel->GetChannelName());
Client->SendLine(OutBuf);
}
static void Server::SendChannelRejoin(const class ChannelList *const Channel, const int ClientDescriptor)
{ //Sends the list of channels we are in to the client specified.
char OutBuf[2048];
struct ClientListStruct *Client = Server::ClientList::Lookup(ClientDescriptor);
if (!Client) return;
//Send the join command.
snprintf(OutBuf, sizeof OutBuf, ":%s!%s@%s JOIN %s\r\n", IRCConfig.Nick, Client->Ident, Client->IP, Channel->GetChannelName());
Client->SendLine(OutBuf);
//Send the topic and the setter of the topic.
if (*Channel->GetTopic() && *Channel->GetWhoSetTopic() && Channel->GetWhenSetTopic() != 0)
{
snprintf(OutBuf, sizeof OutBuf, ":" NEXUS_FAKEHOST " 332 %s %s :%s\r\n", IRCConfig.Nick, Channel->GetChannelName(), Channel->GetTopic());
Client->SendLine(OutBuf);
snprintf(OutBuf, sizeof OutBuf, ":" NEXUS_FAKEHOST " 333 %s %s %s %u\r\n", IRCConfig.Nick,
Channel->GetChannelName(), Channel->GetWhoSetTopic(), Channel->GetWhenSetTopic());
Client->SendLine(OutBuf);
}
//Send list of users.
Server::SendChannelNamesList(Channel, Client);
}
struct ClientListStruct *Server::AcceptLoop(void)
{
struct ClientListStruct TempClient;
char InBuf[2048];
struct ClientListStruct *Client = NULL;
bool UserProvided = false, NickProvided = false;
bool PasswordProvided = false;
if (!Net::AcceptClient(&TempClient.Descriptor, TempClient.IP, sizeof TempClient.IP))
{ //No client.
return NULL;
}
///Apparently there is a client.
Client = Server::ClientList::Add(&TempClient); //Store their information.
/**Continuously try to read their replies until we get them.**/
while (!UserProvided || !NickProvided)
{ //Wait for their greeting.
const bool NetReadReturn = Net::Read(Client->Descriptor, InBuf, sizeof InBuf, true);
if (!NetReadReturn)
{
Server::NukeClient(Client->Descriptor);
return NULL;
}
//Does it start with USER?
if (!strncmp(InBuf, "USER", sizeof "USER" - 1) || !strncmp(InBuf, "user", sizeof "user" - 1))
{ //This information is needed to fool the IRC clients.
const char *TWorker = InBuf + sizeof "USER";
unsigned Inc = 0;
//If we want a password, WE WANT A PASSWORD. You're supposed to send PASS first, dunce!
if (*NEXUSConfig.ServerPassword && !PasswordProvided)
{
Server::NukeClient(Client->Descriptor);
return NULL;
}
while (*TWorker == ' ' || *TWorker == ':') ++TWorker;
for (; TWorker[Inc] != ' ' && TWorker[Inc] != '\0' && Inc < sizeof Client->Ident - 1; ++Inc)
{ //Copy in the ident they sent us.
Client->Ident[Inc] = TWorker[Inc];
}
Client->Ident[Inc] = '\0';
UserProvided = true;
}
else if (!strncmp(InBuf, "NICK", sizeof "NICK" - 1) || !strncmp(InBuf, "nick", sizeof "nick" - 1))
{
const char *TWorker = InBuf + sizeof "nick";
//If we want a password, WE WANT A PASSWORD.
if (*NEXUSConfig.ServerPassword && !PasswordProvided)
{
Net::Close(Client->Descriptor);
return NULL;
}
while (*TWorker == ' ' || *TWorker == ':') ++TWorker;
strncpy(Client->OriginalNick, TWorker, sizeof Client->OriginalNick - 1); //Copy in their chosen nick.
Client->OriginalNick[sizeof Client->OriginalNick - 1] = '\0';
NickProvided = true;
}
else if (!strncmp(InBuf, "PASS", sizeof "PASS" - 1))
{
const char *TW = InBuf + sizeof "PASS";
if (!*NEXUSConfig.ServerPassword)
{ //We don't NEED a password. Just ignore this.
continue;
}
while (*TW == ' ') ++TW;
if (!strcmp(TW, NEXUSConfig.ServerPassword)) PasswordProvided = true;
else
{ //Wrong password.
Net::Close(Client->Descriptor);
return NULL;
}
}
continue;
}
//Get rid of expired scrollback before we send it to someone.
Scrollback::Reap();
//Time to welcome them.
Server::SendIRCWelcome(Client->Descriptor);
//Return the client we found.
return Client;
}
enum ServerMessageType Server::GetMessageType(const char *InStream_)
{ //Another function torn out of aqu4bot.
const char *InStream = InStream_;
char Command[32];
unsigned Inc = 0;
for (; InStream[Inc] != ' ' && InStream[Inc] != '\0' && Inc < sizeof Command - 1; ++Inc)
{ /*Copy in the command.*/
Command[Inc] = toupper(InStream[Inc]);
}
Command[Inc] = '\0';
/*Time for the comparison.*/
if (!strcmp(Command, "PRIVMSG")) return SERVERMSG_PRIVMSG;
else if (!strcmp(Command, "NOTICE")) return SERVERMSG_NOTICE;
else if (!strcmp(Command, "MODE")) return SERVERMSG_MODE;
else if (!strcmp(Command, "JOIN")) return SERVERMSG_JOIN;
else if (!strcmp(Command, "PART")) return SERVERMSG_PART;
else if (!strcmp(Command, "PING")) return SERVERMSG_PING;
else if (!strcmp(Command, "PONG")) return SERVERMSG_PONG;
else if (!strcmp(Command, "NICK")) return SERVERMSG_NICK;
else if (!strcmp(Command, "QUIT")) return SERVERMSG_QUIT;
else if (!strcmp(Command, "KICK")) return SERVERMSG_KICK;
else if (!strcmp(Command, "KILL")) return SERVERMSG_KILL;
else if (!strcmp(Command, "INVITE")) return SERVERMSG_INVITE;
else if (!strcmp(Command, "TOPIC")) return SERVERMSG_TOPIC;
else if (!strcmp(Command, "NAMES")) return SERVERMSG_NAMES;
else if (!strcmp(Command, "WHO")) return SERVERMSG_WHO;
else return SERVERMSG_UNKNOWN;
}
bool ClientListStruct::Ping()
{
std::string Out = "PING :" NEXUS_FAKEHOST "\r\n";
bool RetVal = false;
#ifdef WIN
u_long Value = 1;
ioctlsocket(this->Descriptor, FIONBIO, &Value);
#else
fcntl(this->Descriptor, F_SETFL, O_NONBLOCK); //Set nonblocking. Necessary for our single-threaded model.
#endif //WIN
try
{
Net::Write(this->Descriptor, Out.c_str(), Out.length());
}
catch (Net::Errors::Any)
{
goto End;
}
RetVal = true;
this->PingSentTime = time(NULL);
this->WaitingForPing = true;
End:
#ifdef WIN
Value = 0;
ioctlsocket(this->Descriptor, FIONBIO, &Value);
#else
fcntl(this->Descriptor, F_SETFL, 0);
#endif //WIN
return RetVal;
}
bool ClientListStruct::CompletePing(void)
{
if (!this->WaitingForPing) return false;
this->WaitingForPing = false;
this->PingRecvTime = time(NULL);
return true;
}
ClientListStruct::ClientListStruct(const int InDescriptor, const char *IP, const char *OriginalNick, const char *Ident)
: WaitingForPing(false), PingSentTime(0), PingRecvTime(time(NULL)), Descriptor(InDescriptor)
{ //PingRecvTime is initialized with a real time so that the server doesn't send pings instantly.
SubStrings.Copy(this->IP, IP, sizeof this->IP);
SubStrings.Copy(this->OriginalNick, OriginalNick, sizeof this->OriginalNick);
SubStrings.Copy(this->Ident, Ident, sizeof this->Ident);
}
ClientListStruct::ClientListStruct(void)
: WaitingForPing(false), PingSentTime(0), PingRecvTime(time(NULL)), Descriptor(0)
{
}
void ClientListStruct::SendLine(const char *const String)
{
std::string Out = String;
if (!SubStrings.EndsWith("\r\n", Out.c_str()))
{
Out += "\r\n";
}
this->WriteQueue.push(Out);
}
bool ClientListStruct::FlushSendBuffer(void)
{
if (this->WriteQueue.empty()) return false;
while (!this->WriteQueue.empty())
{
if (!this->WriteQueue_Pop()) return false;
}
return true;
}
bool ClientListStruct::WriteQueue_Pop(void)
{
if (this->WriteQueue.empty()) return false;
bool RetVal = false;
std::string Out = this->WriteQueue.front();
#ifdef WIN
u_long Value = 1;
ioctlsocket(this->Descriptor, FIONBIO, &Value);
#else
fcntl(this->Descriptor, F_SETFL, O_NONBLOCK); //Set nonblocking. Necessary for our single-threaded model.
#endif //WIN
try
{
Net::Write(this->Descriptor, Out.c_str(), Out.length());
}
catch (Net::Errors::Any)
{ //We will probably get Net::Errors::BlockingError.
goto End;
}
RetVal = true;
this->WriteQueue.pop();
End:
#ifdef WIN
Value = 0;
ioctlsocket(this->Descriptor, FIONBIO, &Value);
#else
fcntl(this->Descriptor, F_SETFL, 0);
#endif //WIN
return RetVal;
}