-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircd.js
3146 lines (2905 loc) · 84 KB
/
ircd.js
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: ircd.js,v 1.193 2020/04/04 08:32:04 deuce Exp $
//
// ircd.js
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details:
// http://www.gnu.org/licenses/gpl.txt
//
// Synchronet IRC Daemon as per RFC 1459, link compatible with Bahamut 1.4
//
// Copyright 2003-2009 Randolph Erwin Sommerfeld <[email protected]>
//
//load("synchronet-json.js");
load("sbbsdefs.js");
load("sockdefs.js");
load("nodedefs.js");
load("irclib.js");
load("ircd_unreg.js");
load("ircd_user.js");
load("ircd_channel.js");
load("ircd_server.js");
// CVS revision
const MAIN_REVISION = "$Revision: 1.193 $".split(' ')[1];
// Please don't play with this, unless you're making custom hacks.
// IF you're making a custom version, it'd be appreciated if you left the
// version number alone, and add a token in the form of +hack (i.e. 1.0+cyan)
// This is so everyone knows your revision base, AND type of hack used.
const VERSION = "SynchronetIRCd-1.3a(" + MAIN_REVISION + ")";
const VERSION_STR = "Synchronet "
+ system.version + system.revision + "-" + system.platform
+ system.beta_version + " (IRCd by Randy Sommerfeld)";
// This will dump all I/O to and from the server to your Synchronet console.
// It also enables some more verbose WALLOPS, especially as they pertain to
// blocking functions.
// The special "DEBUG" oper command also switches this value.
var debug = false;
// The number of seconds to block before giving up on outbound CONNECT
// attempts (when connecting to another IRC server -- i.e. a hub) This value
// is important because connecing is a BLOCKING operation, so your IRC *will*
// freeze for the amount of time it takes to connect.
const ob_sock_timeout = 3;
// Should we enable the USERS and SUMMON commands? These allow IRC users to
// view users on the local BBS and summon them to IRC via a Synchronet telegram
// message respectively. Some people might be running the ircd standalone, or
// otherwise don't want anonymous IRC users to have access to these commands.
// We enable this by default because there's typically nothing wrong with
// seeing who's on an arbitrary BBS or summoning them to IRC.
const enable_users_summon = true;
// what our server is capable of from a server point of view.
// TS3 = Version 3 of accepted interserver timestamp protocol.
// NOQUIT = QUIT clients on behalf of a SQUIT server? (no netsplit spam)
// SSJOIN = SJOIN interserver command without dual TS, single TS only.
// BURST = Sending of network synch data is done in a 3-stage burst (BURST cmd)
// UNCONNECT = Server SQUIT is routable.
// NICKIP = 9th parameter of interserver NICK command is an integer IP.
// TSMODE = 2nd arg to standard MODE is the channel's TS.
const server_capab = "TS3 NOQUIT SSJOIN BURST UNCONNECT NICKIP TSMODE";
// EVERY server on the network MUST have the same values in ALL of these
// categories. If you change these, you WILL NOT be able to link to the
// Synchronet IRC network. Linking servers with different values here WILL
// cause your network to desynchronize (and possibly crash the IRCD)
// Remember, this is Synchronet, not Desynchronet ;)
const max_chanlen = 100; // Maximum channel name length.
const max_nicklen = 30; // Maximum nickname length.
const max_modes = 6; // Maximum modes on single MODE command
const max_user_chans = 10; // Maximum channels users can join
const max_bans = 25; // Maximum bans (+b) per channel
const max_topiclen = 307; // Maximum length of topic per channel
const max_kicklen = 307; // Maximum length of kick reasons
const max_who = 100; // Maximum replies to WHO for non-oper users
const max_silence = 10; // Maximum entries on a user's SILENCE list
/* Server types */
const BAHAMUT = 1;
const DREAMFORGE = 2;
var default_port = 6667;
log(VERSION + " started.");
// Our primary arrays.
Unregistered = new Object;
Users = new Object;
Servers = new Object;
Channels = new Object;
Local_Sockets = new Object;
Local_Sockets_Map = new Object;
Selectable_Sockets = new Object;
Selectable_Sockets_Map = new Object;
Global_CommandLine = ""; // We use this to track if a cmdline causes a crash.
hcc_total = 0;
hcc_users = 0;
hcc_counter = 0;
server_uptime = time();
WhoWas = new Object; /* Stores uppercase nicks */
WhoWasMap = new Array; /* A true push/pop array pointing to WhoWas entries */
WhoWas_Buffer = 1000; /* Maximum number of WhoWas entries to keep track of */
NickHistory = new Array; /* A true array using push and pop */
nick_buffer = 1000;
nick_pointer = 0;
/* Keep track of commands and how long they take to execute. */
Profile = new Object;
// This is where our unique ID for each client comes from for unreg'd clients.
next_client_id = 0;
// An array containing all the objects containing local sockets that we need
// to poll.
Local_Users = new Object;
Local_Servers = new Object;
rebuild_socksel_array = true;
network_debug = false;
last_recvq_check = 0;
/*
* A tri-state variable indicating if socket.send is "old" (ie: returns bool)
* or "new" (ie: returns count of bytes sent).
*/
var new_socket_send;
// Parse command-line arguments.
config_filename="";
var cmdline_port;
for (cmdarg=0;cmdarg<argc;cmdarg++) {
switch(argv[cmdarg].toLowerCase()) {
case "-f":
config_filename = argv[++cmdarg];
break;
case "-p":
cmdline_port = parseInt(argv[++cmdarg]);
break;
case "-d":
debug=true;
break;
}
}
read_config_file();
if(this.server==undefined) { // Running from JSexec?
if (!jsexec_revision_detail)
jsexec_revision_detail = "JSexec";
if (cmdline_port)
default_port = cmdline_port;
else if (mline_port)
default_port = mline_port;
server = { socket: false, terminated: false,
version_detail: jsexec_revision_detail, interface_ip_addr_list: ["0.0.0.0","::"] };
server.socket = create_new_socket(default_port)
if (!server.socket)
exit();
}
server.socket.nonblocking = true; // REQUIRED!
server.socket.debug = false; // Will spam your log if true :)
// Now open additional listening sockets as defined on the P:Line in ircd.conf
open_plines = new Array(); /* True Array */
// Make our 'server' object the first open P:Line
open_plines[0] = server.socket;
for (pl in PLines) {
var new_pl_sock = create_new_socket(PLines[pl]);
if (new_pl_sock) {
new_pl_sock.nonblocking = true;
new_pl_sock.debug = false;
open_plines.push(new_pl_sock);
}
}
js.branch_limit=0; // we're not an infinite loop.
js.auto_terminate=false; // we handle our own termination requests
///// Main Loop /////
while (!js.terminated) {
if(file_date(system.ctrl_dir + "ircd.rehash") > time_config_read)
read_config_file();
// Setup a new socket if a connection is accepted.
for (pl in open_plines) {
if (open_plines[pl].poll()) {
var client_sock=open_plines[pl].accept();
log(LOG_DEBUG,"Accepting new connection on port "
+ client_sock.local_port);
if(client_sock) {
client_sock.nonblocking = true;
switch(client_sock.local_port) {
case 994:
case 6697:
client_sock.ssl_server=1;
}
if (!client_sock.remote_ip_address) {
log(LOG_DEBUG,"Socket has no IP address. Closing.");
client_sock.close();
} else if (iszlined(client_sock.remote_ip_address)) {
client_sock.send(":" + servername
+ " 465 * :You've been Z:Lined from this server.\r\n");
client_sock.close();
} else {
var new_id = "id" + next_client_id;
next_client_id++;
if(server.client_add != undefined)
server.client_add(client_sock);
if(server.clients != undefined)
log(LOG_DEBUG,format("%d clients", server.clients));
Unregistered[new_id] = new Unregistered_Client(new_id,
client_sock);
}
} else
log(LOG_DEBUG,"!ERROR " + open_plines[pl].error
+ " accepting connection");
}
}
// Check for pending DNS hostname resolutions.
for(this_unreg in Unregistered) {
if (Unregistered[this_unreg] &&
Unregistered[this_unreg].pending_resolve_time)
Unregistered[this_unreg].resolve_check();
}
// Only rebuild our selectable sockets if required.
if (rebuild_socksel_array) {
Selectable_Sockets = new Array;
Selectable_Sockets_Map = new Array;
for (i in Local_Sockets) {
Selectable_Sockets.push(Local_Sockets[i]);
Selectable_Sockets_Map.push(Local_Sockets_Map[i]);
}
rebuild_socksel_array = false;
}
/* Check for ping timeouts and process queues. */
/* FIXME/TODO: These need to be changed to a mapping system ASAP. */
for(this_sock in Selectable_Sockets) {
if (Selectable_Sockets_Map[this_sock]) {
Selectable_Sockets_Map[this_sock].check_timeout();
Selectable_Sockets_Map[this_sock].check_queues();
}
}
// do some work.
if (Selectable_Sockets.length) {
var readme = socket_select(Selectable_Sockets, 1 /*secs*/);
try {
for(thisPolled in readme) {
if (Selectable_Sockets_Map[readme[thisPolled]]) {
var conn = Selectable_Sockets_Map[readme[thisPolled]];
if (!conn.socket.is_connected) {
conn.quit("Connection reset by peer.");
continue;
}
conn.recvq.recv(conn.socket);
}
}
} catch(e) {
gnotice("FATAL ERROR: " + e + " CMDLINE: " + Global_CommandLine);
log(LOG_ERR,"JavaScript exception: " + e + " CMDLINE: "
+ Global_CommandLine);
terminate_everything("A fatal error occured!", /* ERROR? */true);
}
} else {
mswait(100);
}
// Scan C:Lines for servers to connect to automatically.
var my_cline;
for(thisCL in CLines) {
my_cline = CLines[thisCL];
if (my_cline.port && YLines[my_cline.ircclass].connfreq &&
(YLines[my_cline.ircclass].maxlinks > YLines[my_cline.ircclass].active) &&
(search_server_only(my_cline.servername) < 1) &&
((time() - my_cline.lastconnect) >
YLines[my_cline.ircclass].connfreq)
) {
umode_notice(USERMODE_ROUTING,"Routing",
"Auto-connecting to " +
CLines[thisCL].servername + " ("+CLines[thisCL].host+")");
connect_to_server(CLines[thisCL]);
}
}
}
// End of our run, so terminate everything before we go.
terminate_everything("Terminated.");
//////////////////////////////// END OF MAIN ////////////////////////////////
// Okay, welcome to my world.
// str = The string used for the quit reason UNLESS 'is_netsplit' is set to
// true, in which case it becomes the string used to QUIT individual
// clients in a netsplit (i.e. "server.one server.two")
// suppress_bcast = Set to TRUE if you don't want the message to be broadcast
// accross the entire network. Useful for netsplits, global kills, or
// other network messages where the rest of the network is nuking the
// client on their own.
// is_netsplit = Should never be used except in a recursive call from the
// 'this.netsplit()' function. Tells the function that we're recursive
// and to use 'str' as the reason for quiting all the clients
// origin = an object typically only passed in the case of a SQUIT, contains
// the client who originated the message (i.e. for generating netsplit
// messages.)
// FIXME: this function split into three. comments kept for now, but nuke later
////////// Functions not linked to an object //////////
// Sigh, there's no way to tell the length of an associative array in JS, so,
// we have this to help us:
function true_array_len(my_array) {
var counter = 0;
for (i in my_array) {
counter++;
}
return counter;
}
function terminate_everything(terminate_reason, error) {
log(error ? LOG_ERR : LOG_NOTICE, "Terminating: " + terminate_reason);
for(thisClient in Local_Sockets_Map) {
var Client = Local_Sockets_Map[thisClient];
Client.rawout("ERROR :" + terminate_reason);
Client.socket.close();
}
exit(error);
}
function search_server_only(server_name) {
if (!server_name)
return 0;
for(thisServer in Servers) {
var Server=Servers[thisServer];
if (wildmatch(Server.nick,server_name))
return Server;
}
if (wildmatch(servername,server_name))
return -1; // the server passed to us is our own.
// No success.
return 0;
}
function searchbyserver(servnick) {
if (!servnick)
return 0;
var server_try = search_server_only(servnick);
if (server_try) {
return server_try;
} else {
for(thisNick in Users) {
var Nick=Users[thisNick];
if (wildmatch(Nick.nick,servnick))
return search_server_only(Nick.servername);
}
}
return 0; // looks like we failed after all that hard work :(
}
// Only allow letters, numbers and underscore in username to a maximum of
// 9 characters for 'anonymous' users (i.e. not using PASS to authenticate.)
// hostile characters like !,@,: etc would be bad here :)
function parse_username(str) {
str = str.replace(/[^\w]/g,"").toLowerCase();
if (!str)
str = "user"; // nothing? we'll give you something boring.
return str.slice(0,9);
}
function parse_nline_flags(flags) {
var nline_flags = 0;
for(thisflag in flags) {
switch(flags[thisflag]) {
case "q":
nline_flags |= NLINE_CHECK_QWKPASSWD;
break;
case "w":
nline_flags |= NLINE_IS_QWKMASTER;
break;
case "k":
nline_flags |= NLINE_CHECK_WITH_QWKMASTER;
break;
case "d":
nline_flags |= NLINE_IS_DREAMFORGE;
break;
default:
log(LOG_WARNING,"!WARNING Unknown N:Line flag '"
+ flags[thisflag] + "' in config.");
break;
}
}
return nline_flags;
}
function parse_oline_flags(flags) {
var oline_flags = 0;
for(thisflag in flags) {
switch(flags[thisflag]) {
case "r":
oline_flags |= OLINE_CAN_REHASH;
break;
case "R":
oline_flags |= OLINE_CAN_RESTART;
break;
case "D":
oline_flags |= OLINE_CAN_DIE;
break;
case "g":
oline_flags |= OLINE_CAN_GLOBOPS;
break;
case "w":
oline_flags |= OLINE_CAN_WALLOPS;
break;
case "l":
oline_flags |= OLINE_CAN_LOCOPS;
break;
case "c":
oline_flags |= OLINE_CAN_LSQUITCON;
break;
case "C":
oline_flags |= OLINE_CAN_GSQUITCON;
break;
case "k":
oline_flags |= OLINE_CAN_LKILL;
break;
case "K":
oline_flags |= OLINE_CAN_GKILL;
break;
case "b":
oline_flags |= OLINE_CAN_KLINE;
break;
case "B":
oline_flags |= OLINE_CAN_UNKLINE;
break;
case "n":
oline_flags |= OLINE_CAN_LGNOTICE;
break;
case "N":
oline_flags |= OLINE_CAN_GGNOTICE;
break;
case "u":
oline_flags |= OLINE_CAN_UMODEC;
break;
case "A":
oline_flags |= OLINE_IS_ADMIN;
break;
case "a":
case "f":
case "F":
break; // All reserved for future use.
case "s":
oline_flags |= OLINE_CAN_CHATOPS;
break;
case "S":
oline_flags |= OLINE_CHECK_SYSPASSWD;
break;
case "x":
case "X":
oline_flags |= OLINE_CAN_DEBUG;
break;
case "O":
oline_flags |= OLINE_IS_GOPER;
oline_flags |= OLINE_CAN_GSQUITCON;
oline_flags |= OLINE_CAN_GKILL;
oline_flags |= OLINE_CAN_GGNOTICE;
oline_flags |= OLINE_CAN_CHATOPS;
case "o":
oline_flags |= OLINE_CAN_REHASH;
oline_flags |= OLINE_CAN_GLOBOPS;
oline_flags |= OLINE_CAN_WALLOPS;
oline_flags |= OLINE_CAN_LOCOPS;
oline_flags |= OLINE_CAN_LSQUITCON;
oline_flags |= OLINE_CAN_LKILL;
oline_flags |= OLINE_CAN_KLINE;
oline_flags |= OLINE_CAN_UNKLINE;
oline_flags |= OLINE_CAN_LGNOTICE;
oline_flags |= OLINE_CAN_UMODEC;
break;
default:
log(LOG_WARNING,"!WARNING Unknown O:Line flag '"
+ flags[thisflag] + "' in config.");
break;
}
}
return oline_flags;
}
function umode_notice(bit,ntype,nmessage) {
log(ntype + ": " + nmessage);
for (thisuser in Local_Users) {
var user = Local_Users[thisuser];
if (user.mode && ((user.mode&bit)==bit))
user.rawout(":" + servername + " NOTICE " + user.nick
+ " :*** " + ntype + " -- " + nmessage);
}
}
function create_ban_mask(str,kline) {
var tmp_banstr = new Array;
tmp_banstr[0] = "";
tmp_banstr[1] = "";
tmp_banstr[2] = "";
var bchar_counter = 0;
var part_counter = 0; // BAN: 0!1@2 KLINE: 0@1
var regexp="[A-Za-z\{\}\`\^\_\|\\]\\[\\\\0-9\-.*?\~:]";
var finalstr;
for (bchar in str) {
if (str[bchar].match(regexp)) {
tmp_banstr[part_counter] += str[bchar];
bchar_counter++;
} else if ((str[bchar] == "!") && (part_counter == 0) &&
!kline) {
part_counter = 1;
bchar_counter = 0;
} else if ((str[bchar] == "@") && (part_counter == 1) &&
!kline) {
part_counter = 2;
bchar_counter = 0;
} else if ((str[bchar] == "@") && (part_counter == 0)) {
if (kline) {
part_counter = 1;
} else {
tmp_banstr[1] = tmp_banstr[0];
tmp_banstr[0] = "*";
part_counter = 2;
}
bchar_counter = 0;
}
}
if (!tmp_banstr[0] && !tmp_banstr[1] && !tmp_banstr[2])
return 0;
if (tmp_banstr[0].match(/[.]/) && !tmp_banstr[1] && !tmp_banstr[2]) {
if (kline)
tmp_banstr[1] = tmp_banstr[0];
else
tmp_banstr[2] = tmp_banstr[0];
tmp_banstr[0] = "";
}
if (!tmp_banstr[0])
tmp_banstr[0] = "*";
if (!tmp_banstr[1])
tmp_banstr[1] = "*";
if (!tmp_banstr[2] && !kline)
tmp_banstr[2] = "*";
if (kline)
finalstr = tmp_banstr[0].slice(0,10) + "@" + tmp_banstr[1].slice(0,80);
else
finalstr = tmp_banstr[0].slice(0,max_nicklen) + "!"
+ tmp_banstr[1].slice(0,10) + "@" + tmp_banstr[2].slice(0,80);
while (finalstr.match(/[*][*]/)) {
finalstr=finalstr.replace(/[*][*]/g,"*");
}
return finalstr;
}
function isklined(kl_str) {
for(the_kl in KLines) {
if (KLines[the_kl].hostmask &&
wildmatch(kl_str,KLines[the_kl].hostmask))
return KLines[the_kl];
}
return 0;
}
function iszlined(zl_ip) {
for(the_zl in ZLines) {
if (ZLines[the_zl].ipmask &&
wildmatch(zl_ip,ZLines[the_zl].ipmask))
return 1;
}
return 0;
}
function scan_for_klined_clients() {
for(thisUser in Local_Users) {
var theuser=Local_Users[thisUser];
var kline=isklined(theuser.uprefix + "@" + theuser.hostname);
if (kline)
theuser.quit("User has been K:Lined (" + kline.reason + ")");
if (iszlined(theuser.ip))
theuser.quit("User has been Z:Lined");
}
}
function remove_kline(kl_hm) {
for(the_kl in KLines) {
if (KLines[the_kl].hostmask &&
wildmatch(kl_hm,KLines[the_kl].hostmask)) {
KLines[the_kl].hostmask = "";
KLines[the_kl].reason = "";
KLines[the_kl].type = "";
return 1;
}
}
return 0; // failure.
}
function connect_to_server(this_cline,the_port) {
var connect_sock;
var new_id;
if (!the_port && this_cline.port)
the_port = this_cline.port;
else if (!the_port)
the_port = default_port; // try a safe default.
if (js.global.ConnectedSocket != undefined) {
try {
connect_sock = new ConnectedSocket(this_cline.host, the_port, {timeout:ob_sock_timeout, bindaddrs:server.interface_ip_addr_list});
}
catch(e) {
connect_sock = new Socket();
}
}
else {
connect_sock = new Socket();
connect_sock.bind(0,server.interface_ip_address);
connect_sock.connect(this_cline.host,the_port,ob_sock_timeout);
}
var sendts = true; /* Assume Bahamut */
for (nl in NLines) {
var mynl = NLines[nl];
if ((mynl.flags&NLINE_IS_DREAMFORGE) &&
(mynl.servername == this_cline.servername)) {
sendts = false;
break;
}
}
if (connect_sock.is_connected) {
umode_notice(USERMODE_ROUTING,"Routing",
"Connected! Sending info...");
var sendstr = "PASS " + this_cline.password;
if (sendts)
sendstr += " :TS";
connect_sock.send(sendstr + "\r\n");
connect_sock.send("CAPAB " + server_capab + "\r\n");
connect_sock.send("SERVER " + servername + " 1 :" + serverdesc +"\r\n");
new_id = "id" + next_client_id;
next_client_id++;
Unregistered[new_id]=new Unregistered_Client(new_id,connect_sock);
Unregistered[new_id].sendps = false; // Don't do P/S pair again
Unregistered[new_id].outgoing = true; /* Outgoing Connection */
Unregistered[new_id].ircclass = this_cline.ircclass;
YLines[this_cline.ircclass].active++;
log(LOG_DEBUG, "Class "+this_cline.ircclass+" up to "+YLines[this_cline.ircclass].active+" active out of "+YLines[this_cline.ircclass].maxlinks);
}
else {
umode_notice(USERMODE_ROUTING,"Routing",
"Failed to connect to " +
this_cline.servername + " ("+this_cline.host+")");
connect_sock.close();
}
this_cline.lastconnect = time();
}
function wallopers(str) {
for(thisoper in Local_Users) {
var oper=Local_Users[thisoper];
if (oper.mode&USERMODE_OPER)
oper.rawout(str);
}
}
function push_nickbuf(oldnick,newnick) {
NickHistory.unshift(new NickBuf(oldnick,newnick));
if(NickHistory.length >= nick_buffer)
NickHistory.pop();
}
function search_nickbuf(bufnick) {
for (nb=NickHistory.length-1;nb>-1;nb--) {
if (bufnick.toUpperCase() == NickHistory[nb].oldnick.toUpperCase()) {
if (!Users[NickHistory[nb].newnick.toUpperCase()])
bufnick = NickHistory[nb].newnick;
else
return Users[NickHistory[nb].newnick.toUpperCase()];
}
}
return 0;
}
var time_config_read;
function read_config_file() {
/* All of these variables are global. */
Admin1 = "";
Admin2 = "";
Admin3 = "";
CLines = new Array;
HLines = new Array;
ILines = new Array;
KLines = new Array;
NLines = new Array;
OLines = new Array;
PLines = new Array;
QLines = new Array;
ULines = new Array;
diepass = "";
restartpass = "";
YLines = new Array;
ZLines = new Array;
/* End of global variables */
var fname="";
if (config_filename && config_filename.length) {
if(config_filename.indexOf('/')>=0 || config_filename.indexOf('\\')>=0)
fname=config_filename;
else
fname=system.ctrl_dir + config_filename;
} else {
fname=system.ctrl_dir + "ircd." + system.local_host_name + ".conf";
if(!file_exists(fname))
fname=system.ctrl_dir + "ircd." + system.host_name + ".conf";
if(!file_exists(fname))
fname=system.ctrl_dir + "ircd.conf";
}
log(LOG_INFO,"Reading Config: " + fname);
if (fname.substr(fname.length-3,3) == "ini")
read_ini_config(fname);
else
read_conf_config(fname);
time_config_read = time();
}
function read_ini_config(fname) {
var conf = new File(fname);
if (conf.open("r")) {
/* Global Variables */
}
conf.close();
}
function read_conf_config(fname) {
var conf = new File(fname);
function fancy_split(line) {
var ret = [];
var i;
var s = 0;
var inb = false;
var str;
for (i = 0; i < line.length; i++) {
if (line[i] == ':') {
if (inb)
continue;
if (i > 0 && line[i-1] == ']')
str = line.slice(s, i-1);
else
str = line.slice(s, i);
ret.push(str);
s = i + 1;
}
else if (!inb && line[i] == '[' && s == i) {
inb = true;
s = i + 1;
}
else if (line[i] == ']' && (i+1 == line.length || line[i+1] == ':')) {
inb = false;
}
}
if (s < line.length) {
if (i > 0 && line[i-1] == ']')
str = line.slice(s, i-1);
else
str = line.slice(s, i);
ret.push(str);
}
return ret;
}
if (conf.open("r")) {
while (!conf.eof) {
var conf_line = conf.readln();
if ((conf_line != null) && conf_line.match("[:]")) {
var arg = fancy_split(conf_line);
for(argument in arg) {
arg[argument]=arg[argument].replace(
/SYSTEM_HOST_NAME/g,system.host_name);
arg[argument]=arg[argument].replace(
/SYSTEM_NAME/g,system.name);
arg[argument]=arg[argument].replace(
/SYSTEM_QWKID/g,system.qwk_id.toLowerCase());
arg[argument]=arg[argument].replace(
/VERSION_NOTICE/g,system.version_notice);
}
switch (conf_line[0].toUpperCase()) {
case "A":
if (!arg[3])
break;
Admin1 = arg[1];
Admin2 = arg[2];
Admin3 = arg[3];
break;
case "C":
if (!arg[5])
break;
CLines.push(new CLine(arg[1],arg[2],arg[3],arg[4],
parseInt(arg[5]) ));
break;
case "H":
if (!arg[3])
break;
HLines.push(new HLine(arg[1],arg[3]));
break;
case "I":
if (!arg[5])
break;
ILines.push(new ILine(arg[1],arg[2],arg[3],arg[4],
arg[5]));
break;
case "K":
if (!arg[2])
break;
var kline_mask = create_ban_mask(arg[1],true);
if (!kline_mask) {
log(LOG_WARNING,"!WARNING Invalid K:Line ("
+ arg[1] + ")");
break;
}
KLines.push(new KLine(kline_mask,arg[2],"K"));
break;
case "M":
if (!arg[3])
break;
servername = arg[1];
serverdesc = arg[3];
mline_port = parseInt(arg[4]);
break;
case "N":
if (!arg[5])
break;
NLines.push(new NLine(arg[1],arg[2],arg[3],
parse_nline_flags(arg[4]),arg[5]) );
break;
case "O":
if (!arg[5])
break;
OLines.push(new OLine(arg[1],arg[2],arg[3],
parse_oline_flags(arg[4]),parseInt(arg[5]) ));
break;
case "P":
PLines.push(parseInt(arg[4]));
break;
case "Q":
if (!arg[3])
break;
QLines.push(new QLine(arg[3],arg[2]));
break;
case "U":
if (!arg[1])
break;
ULines.push(arg[1]);
break;
case "X":
diepass = arg[1];
restartpass = arg[2];
break;
case "Y":
if (!arg[5])
break;
YLines[parseInt(arg[1])] = new YLine(parseInt(arg[2]),
parseInt(arg[3]),parseInt(arg[4]),parseInt(arg[5]));
break;
case "Z":
if (!arg[2])
break;
ZLines.push(new ZLine(arg[1],arg[2]));
break;
case "#":
case ";":
default:
break;
}
}
}
conf.close();
} else {
log ("WARNING! No config file found or unable to open."
+ " Proceeding with defaults.");
}
scan_for_klined_clients();
YLines[0] = new YLine(120,600,1,5050000); // default irc class
}
function create_new_socket(port) {
var newsock;
log(LOG_DEBUG,"Creating new socket object on port " + port);
if (js.global.ListeningSocket != undefined) {
try {
newsock = new ListeningSocket(server.interface_ip_addr_list, port, "IRCd");
log(format("IRC server socket bound to TCP port " + port));
}
catch(e) {
log(LOG_ERR,"!Error " + e + " creating listening socket on port "
+ port);
return 0;
}
}
else {
newsock = new Socket();
if(!newsock.bind(port,server.interface_ip_address)) {
log(LOG_ERR,"!Error " + newsock.error + " binding socket to TCP port "
+ port);
return 0;
}
log(format("%04u ",newsock.descriptor)
+ "IRC server socket bound to TCP port " + port);
if(!newsock.listen(5 /* backlog */)) {
log(LOG_ERR,"!Error " + newsock.error
+ " setting up socket for listening");
return 0;
}
}
return newsock;
}
function check_qwk_passwd(qwkid,password) {
if (!password || !qwkid)
return 0;
qwkid = qwkid.toUpperCase();
var usernum = system.matchuser(qwkid);
var bbsuser = new User(usernum);
if ((password.toUpperCase() ==
bbsuser.security.password.toUpperCase()) &&
(bbsuser.security.restrictions&UFLAG_Q) )
return 1;
return 0;
}
function IRCClient_netsplit(ns_reason) {
if (!ns_reason)
ns_reason = "net.split.net.split net.split.net.split";
for (sqclient in Users) {
if (Users[sqclient] &&
(Users[sqclient].servername == this.nick)
)
Users[sqclient].quit(ns_reason,true,true);
}
for (sqserver in Servers) {
if (Servers[sqserver] &&
(Servers[sqserver].linkparent == this.nick)
)
Servers[sqserver].quit(ns_reason,true,true);
}
}
function IRCClient_RMChan(rmchan_obj) {
if (!rmchan_obj)
return 0;
if (rmchan_obj.users[this.id])
delete rmchan_obj.users[this.id];
if (this.channels[rmchan_obj.nam.toUpperCase()])
delete this.channels[rmchan_obj.nam.toUpperCase()];
delete rmchan_obj.modelist[CHANMODE_OP][this.id];
delete rmchan_obj.modelist[CHANMODE_VOICE][this.id];
if (!true_array_len(rmchan_obj.users))
delete Channels[rmchan_obj.nam.toUpperCase()];
}
//////////////////// Output Helper Functions ////////////////////
function rawout(str) {
var sendconn;
var str_end;
var str_beg;
if (debug)
log(format("[RAW->%s]: %s",this.nick,str));