-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.js
1464 lines (1380 loc) · 36.3 KB
/
irc.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
// irc.js
// Deuce's IRC client module for Synchronet
// With the "Manny Mods". :-)
// $Id: irc.js,v 1.58 2019/08/07 16:13:50 deuce Exp $
// disable auto-termination.
var old_auto_terminate=js.auto_terminate;
js.on_exit("js.auto_terminate=old_auto_terminate");
js.auto_terminate=false;
const REVISION = "$Revision: 1.58 $".split(' ')[1];
const SPACEx80 = " ";
const MAX_HIST = 50;
load("sbbsdefs.js");
load("nodedefs.js");
load("sockdefs.js"); // SOCK_DGRAM
load("irclib.js");
// Global vars
var irc_server="irc.synchro.net";
var irc_port=6667;
var default_channel="#synchronet";
var connect_timeout=15; // Seconds
var connected=0;
var quit=0;
var nick=user.handle;
var nicks=new Array();
var loading=true;
var real_names=true;
js.on_exit("console.ctrlkey_passthru = " + console.ctrlkey_passthru);
console.ctrlkey_passthru=~(134217728);
// Commands to send...
var client_cmds = {
'PASS':{minparam:1,maxparam:1}, // Must be sent before NICK/USER
'NICK':{minparam:1,maxparam:1},
'USER':{minparam:4,maxparam:4},
'OPER':{minparam:2,maxparam:2},
'QUIT':{minparam:0,maxparam:1},
'JOIN':{minparam:1,maxparam:2},
'PART':{minparam:1,maxparam:2},
'TOPIC':{minparam:1,maxparam:2},
'NAMES':{minparam:1,maxparam:1},
'LIST':{minparam:1,maxparam:2},
'MOTD':{minparam:0,maxparam:1},
'VERSION':{minparam:0,maxparam:1},
'ADMIN':{minparam:0,maxparam:1},
'CONNECT':{minparam:1,maxparam:3},
'TIME':{minparam:0,maxparam:1},
'STATS':{minparam:0,maxparam:2},
'INFO':{minparam:0,maxparam:1},
'MODE':{minparam:1,maxparam:Infinity},
'PRIVMSG':{minparam:2,maxparam:2},
'NOTICE':{minparam:2,maxparam:2},
'USERHOST':{minparam:1,maxparam:Infinity},
'KILL':{minparam:1,maxparam:2},
'SQUIT':{minparam:2,maxparam:2},
'INVITE':{minparam:2,maxparam:2},
'KICK':{minparam:2,maxparam:3},
'LINKS':{minparam:0,maxparam:2},
'TRACE':{minparam:0,maxparam:1},
'WHO':{minparam:0,maxparam:2},
'WHOIS':{minparam:1,maxparam:2},
'WHOWAS':{minparam:1,maxparam:3},
'PING':{minparam:1,maxparam:2},
'PONG':{minparam:1,maxparam:2},
'AWAY':{minparam:0,maxparam:1},
'REHASH':{minparam:0,maxparam:0},
'RESTART':{minparam:0,maxparam:0},
'SUMMON':{minparam:1,maxparam:3},
'USERS':{minparam:0,maxparam:1},
'WALLOPS':{minparam:1,maxparam:1},
'ISON':{minparam:1,maxparam:Infinity},
'LUSERS':{minparam:0,maxparam:2},
'SERVLIST':{minparam:0,maxparam:2},
'SQUERY':{minparam:2,maxparam:2},
'DIE':{minparam:0,maxparam:0},
};
/* Command-line options go BEFORE command-line args */
var irc_theme = "irc-default.js";
ARGPARSE: for (cmdarg=0;cmdarg<argc;cmdarg++) {
switch(argv[cmdarg]) {
case "-A":
case "-a":
real_names=false;
break;
case "-T":
case "-t":
irc_theme=argv[++cmdarg];
break;
default:
break ARGPARSE;
}
}
/* Load the defined theme. -- Cyan */
load(irc_theme);
/* Command-line args can override default server values */
if(argv[cmdarg]!=undefined)
irc_server=argv[cmdarg++];
if(argv[cmdarg]!=undefined)
irc_port=Number(argv[cmdarg++]);
if(argv[cmdarg]!=undefined)
default_channel=argv[cmdarg++];
default_channel=default_channel.replace(/\s+/g,"_");
sock=new Socket();
history=new History();
screen=new Screen();
// Connect
if(!sock.connect(irc_server,irc_port)) {
log(format("!IRC connection to %s FAILED with error %d"
,irc_server,sock.last_error));
alert(irc_server+" not available");
clean_exit();
}
send_cmd("PASS", user.security.password); // for use with JS IRC server
if (nick=="")
nick=user.alias;
nick=nick.replace(/\s+/g,"_");
send_cmd("NICK", nick);
username=user.alias;
username=username.replace(/\s+/g,"_");
send_cmd("USER", username+" 0 * :"+(real_names?user.name:user.alias)+" ("+client.ip_address+")");
channels=new Channels();
// Wait for welcome message...
while(!connected) {
if(sock.poll(connect_timeout)) {
wait_for(["433","422","376"]);
connected=1;
}
else {
alert("Response timeout");
sock.close();
clean_exit();
}
}
// Main loop
while(!quit) {
if(!sock.is_connected || !connected) {
alert("Lost connection");
sock.close();
clean_exit();
}
if(!client.socket.is_connected) {
send_cmd("QUIT",":Dropped Carrier");
quit=1;
sock.close();
bbs.hangup();
clean_exit();
}
if(js.terminated) {
send_cmd("QUIT",":Client terminated.");
quit=1;
sock.close();
bbs.hangup();
clean_exit();
}
if(bbs.get_time_left && !bbs.get_time_left()) {
send_cmd("QUIT",":Out of time.");
quit=1;
sock.close();
clean_exit();
}
if(sock.poll(.01)) {
receive_command();
screen.update(0);
}
else
screen.update(100);
}
sock.close();
clean_exit();
function send_cmd(command, params)
{
var snd;
var plist;
var pcnt = 0;
var cmd;
if (params === undefined)
plist = [];
else {
if (params[0] == ':')
params = params.substr(1);
plist = params.split(/ \:?/);
}
command = command.toUpperCase();
cmd = client_cmds[command];
if (cmd === undefined) {
screen.print_line("\x01H\x01R!! \x01N\x01R"+command+" not supported.\x01N\x01W");
return;
}
snd = command;
while (plist.length) {
snd += ' ';
pcnt++;
if (pcnt == cmd.maxparam && plist.length > 1)
snd += ':';
snd += plist.shift();
}
if (pcnt < cmd.minparam) {
screen.print_line("\x01H\x01R!! \x01N\x01R"+command+" requires at least "+cmd.minparam+" parameters\x01N\x01W");
return;
}
sock.send(snd+"\r\n");
}
function handle_command(tag, prefix, command, message) {
var from_nick=null;
var full_message=null;
var tmp_str=null;
var tmp_str2=null;
var i=0;
switch(command) {
case "PING":
send_cmd("PONG",message.join(' '));
break;
case "NOTICE":
message.shift(); // Target
from_nick=get_highlighted_nick(prefix,message);
full_message=message.join(" ");
full_message=full_message.replace(/\x01/g,"");
screen.print_line(format(NOTICE_FORMAT,from_nick,full_message));
break;
case "KICK":
tmp_str=message.shift(); // Channel
tmp_str2=message.shift(); // User
from_nick=get_nick(prefix);
full_message=message.join(" ");
if(tmp_str2.toUpperCase()==nick.toUpperCase()) {
channels.part(tmp_str,"");
}
screen.print_line(format(KICK_FORMAT,tmp_str2,tmp_str,full_message));
break;
case "PRIVMSG":
if(message[1][0]=="\x01") {
// CTCP
handle_ctcp(prefix,message);
}
else {
if(prefix=="")
from_nick="[-]";
else {
from_nick=get_highlighted_nick(prefix,message);
message[0]=message[0].toUpperCase();
if(channels.current != undefined) {
if(message[0]==channels.current.name) {
from_nick=format(FROM_NICK_CURCHAN,from_nick);
}
else {
from_nick=format(MSG_FORMAT,from_nick);
if(message[0][0]=="#" || message[0][0]=="&") {
from_nick=from_nick+"\x01N\x01C"+message[0]+":\x01N\x01W ";
}
else
console.beep();
}
}
else {
from_nick=format(FROM_NICK_CURCHAN,from_nick);
}
}
message.shift(); // Receiver
screen.print_line(from_nick+" "+message.join(" "));
}
break;
case "JOIN":
from_nick=get_highlighted_nick(prefix,message);
tmp_str=get_nick(prefix);
if(tmp_str.toUpperCase()==nick.toUpperCase()) {
channels.joined(message[0]);
}
else {
channels.nick_add(tmp_str,message[0]);
}
prefix=prefix.split("!")[1];
screen.print_line(format(JOIN_FORMAT,from_nick,prefix,message[0]));
break;
case "QUIT":
from_nick=get_highlighted_nick(prefix,message);
tmp_str=get_nick(prefix);
prefix=prefix.split("!")[1];
full_message=message.shift();
screen.print_line(format(QUIT_FORMAT,from_nick,channels.current.display,full_message+" "+message.join(" ")));
channels.nick_quit(tmp_str);
break;
case "NICK":
from_nick=get_highlighted_nick(prefix,message);
tmp_str2=get_nick(prefix);
prefix=prefix.split("!")[1];
tmp_str=message.shift();
tmp_str=tmp_str.split("!",1)[0]
screen.print_line(format(NICK_FORMAT,from_nick,tmp_str));
if(tmp_str2.toUpperCase()==nick.toUpperCase()) {
nick=tmp_str;
screen.update_statline();
}
channels.nick_change(tmp_str2,tmp_str);
break;
case "SQUIT":
if(prefix.length > 0) {
from_nick=get_nick(prefix);
tmp_str=message.shift();
tmp_str2=message.shift();
screen.print_line(format(SQUIT_FROM_NICK,from_nick,tmp_str,tmp_str2+message.join(" ")));
}
else {
tmp_str=message.shift();
tmp_str2=message.shift();
screen.print_line(SQUIT_FROM_SERVER,tmp_str,tmp_str2+message.join(" "));
}
case "PART":
from_nick=get_highlighted_nick(prefix,message);
tmp_str=get_nick(prefix);
prefix=prefix.split("!")[1];
screen.print_line(format(PART_FORMAT,from_nick,prefix,message[0]));
channels.nick_part(tmp_str,message[0]);
break;
case "MODE":
from_nick=get_highlighted_nick(prefix,message);
prefix=prefix.split("!")[1];
modestr="";
for (modeidx=1;modeidx<message.length;modeidx++) {
if (modeidx > 1)
modestr += " ";
modestr += message[modeidx];
}
screen.print_line(format(MODE_FORMAT,from_nick,message[0],modestr));
break;
case "TOPIC":
from_nick=get_highlighted_nick(prefix,message);
tmp_str=message.shift();
tmp_str2=message.join(" ");
for(i=0;i<channels.length;i++) {
if(tmp_str.toUpperCase()==channels.channel[i].name) {
channels.channel[i].topic=tmp_str2;
screen.update_statline();
screen.print_line(format(TOPIC_FORMAT,from_nick,tmp_str,tmp_str2));
}
}
break;
// Numeric reply codes.
case "211": // Trace Server
case "352": // WHO reply
case "206": // Trace Server
case "213": // Stats CLINE
case "214": // Stats NLINE
case "215": // Stats ILINE
case "216": // Stats KLINE
case "218": // Stats YLINE
case "241": // Stats LLINE
case "311": // WHOIS reply
case "314": // WHOWAS reply
case "367": // Ban List
case "200": // Trace Link
case "243": // Stats OLINE
case "244": // Stats HLINE
case "317": // WHOISIDLE Reply
case "324": // Channel Modes
case "201": // Trace Connecting
case "202": // Trace Handshake
case "203": // Trace Unknown
case "204": // Trace Operator
case "205": // Trace User
case "208": // New type of trace
case "261": // Trace LOG
case "312": // WHOISSERVER Reply
case "322": // LIST data
case "341": // Invite being sent
case "351": // (server) VERSION reply
case "364": // Links
case "212": // Stats Command
case "313": // WHOISOPERATOR Reply
case "319": // WHOISCHANNELS Reply
case "301": // AWAY Reply
case "318": // End of WHOIS Reply
case "369": // End of WHOWAS Reply
case "321": // LIST Start
case "342": // Is Summoning.
case "315": // End of WHO
case "365": // End of LINKS
case "368": // End of ban list
case "382": // Rehashing
case "391": // Time reply
case "219": // End if stats
case "221": // UMODE reply
case "252": // # Operators online
case "253": // # unknown connections
case "254": // # channels
case "256": // Admin info
case "001": // Sent on successful registration
case "002": // Sent on successful registration
case "003": // Sent on successful registration
case "004": // Sent on successful registration
case "005": // Sent on successful registration
case "265": // Local Users
case "266": // Global Users
case "381": // You're OPER
case "302": // USERHOST Reply
case "303": // ISON Reply
case "305": // You are no longer away
case "306": // You are now marked as away
case "323": // LIST end
case "371": // INFO
case "374": // End if info list
case "392": // USERS Start
case "393": // USERS
case "394": // USERS End
case "395": // No users
case "242": // Stats Uptime
case "251": // LUSER Client
case "255": // # Clients / # Servers
case "257": // Admin LOC 1
case "258": // Admin LOC 2
case "259": // Admin e-mail
case "375": // MOTD Start
case "372": // MOTD
case "333": // Extended TOPIC info (apparently)
message.shift(); // Client
screen.print_line("\x01H\x01C!! \x01N\x01C"+message.join(" ")+"\x01N\x01W");
break;
// Things that actually need dealing with...
case "376": // MOTD End
if (loading) {
loading = false;
channels.join(default_channel);
}
message.shift(); // Client
screen.print_line("\x01H\x01C!! \x01N\x01C"+message.join(" ")+"\x01N\x01W");
break;
case "331": // No Topic
case "332": // Topic
for(i=0;i<channels.length;i++) {
if(message[1].toUpperCase()==channels.channel[i].name) {
channels.channel[i].topic=message[2];
screen.update_statline();
}
}
break;
case "353": // Name reply
message.shift(); // Client
message.shift(); // Symbol
tmp_str=message.shift().toUpperCase(); // Channel
for(i=0;i<message.length;i++) {
switch(message[i][0]) {
case '~': // Founder
case '&': // Protected
case '@': // Op
case '%': // Half-op
case '+': // Voice
message[i]=message[i].substr(1);
break;
}
}
for(i=0;i<channels.length;i++) {
if(tmp_str == channels.channel[i].name) {
channels.channel[i].nick=message;
}
}
screen.print_line("\x01N\x01B\1hPeople in "+tmp_str+" right now: "+message.join(" "));
break;
case "366": // End of Names
break;
// Error Codes
case "433": // Nickname already in use
message.shift(); // Client
nick=message.shift()+"_";
send_cmd("NICK", nick);
break;
// <word1> <word2> :Message errors
case "441": // Nick not on channel
case "443": // User already on channel (invite)
case "401": // No such nick
case "402": // No such server
case "403": // No such channel
case "404": // Cannot send to channel
case "405": // Too Many Channels
case "406": // Was no suck nickname
case "407": // Too many targets
case "413": // No toplevel domain specified
case "414": // Wildcard in TLD
case "421": // Unknown Command
case "423": // No admin info available
case "432": // Erroneous Nick... bad chars
case "436": // Nick collision KILL
case "442": // You aren't on that channel
case "444": // User not logged in (From SUMMON command)
case "461": // Not enough params
case "467": // Channel key already set
case "471": // Channel is full (+l)
case "472": // Unknown mode
case "473": // Invide only channel (And YOU aren't invided)
case "474": // Banned from channel
case "475": // Bad channel key (+k)
case "482": // Not ChanOP so can't do that.
case "422": // MOTD is missing
case "409": // No origin
case "411": // No recipient
case "412": // No text to send
case "424": // File Error
case "431": // No nickname given
case "445": // SUMMON disabled
case "446": // USERS disabled
case "451": // You aren't registered
case "462": // You're ALREADY registered
case "463": // You aren't allowed to connect (HOST based)
case "464": // Incorrect password
case "465": // You are banned
case "481": // You aren't an IRCOP so you can't do that.
case "483": // You can't kill a server.
case "491": // No O-lines for your host
case "501": // Unknown MODE flag
case "502": // Can't change other users mode
message.shift(); // Client
if (message.length > 1) {
tmp_str=message.shift();
screen.print_line("\x01H\x01R!! \x01N\x01R"+tmp_str+" - "+message.join(" ")+"\x01N\x01W");
}
else {
screen.print_line("\x01H\x01R!! \x01N\x01R"+message.join(" ")+"\x01N\x01W");
}
break;
default:
screen.print_line("\x01N\x01R"+prefix+" "+command+" "+message.join(" ")+"\x01N\x01W");
}
}
function get_command()
{
var tag = "";
var prefix="";
var command=null;
var line;
var message = [];
var i;
if(sock.poll(0)) {
line=sock.recvline();
if(!line)
return;
if (line[0] == '@') {
tag = line.slice(1, line.indexOf(" "));
line = line.substr(tag.length + 2);
}
if (line[0] == ':') {
prefix = line.slice(1, line.indexOf(" "));
line = line.substr(prefix.length + 2);
}
message.push(tag);
message.push(prefix);
while (line.length > 0) {
if (line[0] == ':') {
message.push(line.substr(1));
line = '';
}
else {
i = line.indexOf(' ');
if (i == -1) {
message.push(line);
line = '';
}
else {
message.push(line.slice(0, i));
line = line.substr(i + 1);
}
}
}
return message;
}
}
function receive_command() {
var tag = "";
var prefix="";
var command=null;
var message;
var message = get_command();
if (message == undefined)
return;
tag = message.shift();
prefix = message.shift();
command = message.shift();
handle_command(tag, prefix, command, message);
}
function wait_for(commands) {
var tag = '';
var prefix="";
var command=null;
var message="";
var i=0;
while(1) {
if(!sock.is_connected) {
alert("Lost connection");
sock.close();
clean_exit();
}
if(!client.socket.is_connected) {
send_cmd("QUIT", ":Dropped Carrier.");
quit=1;
sock.close();
bbs.hangup();
clean_exit();
}
message = get_command();
if (message != undefined) {
tag = message.shift();
prefix = message.shift();
command = message.shift();
handle_command(tag,prefix,command,message);
for(i=0;i<commands.length;i++) {
if(command==commands[i]) {
return command;
}
}
}
// Don't handle user input at this point!
// screen.update();
}
}
function send_command(command,param) {
var params=[null];
var send_to=null;
var full_params;
var i=0;
var got="";
switch(command) {
case "MSG":
params=param.split(" ");
send_to=params.shift();
send_cmd("PRIVMSG", send_to+" :"+params.join(" "));
screen.print_line(send_to+"\x01H\x01C<\x01N\x01C-\x01N\x01W "+params.join(" "));
break;
case "X":
case "Q":
case "QUIT":
send_cmd("QUIT", param);
quit=1;
sock.close();
clean_exit();
break;
case "J":
case "JOIN":
channels.join(param);
break;
case "ME":
if(channels.current==undefined) {
screen.print_line("\x01H\x01RYou are not in a channel!\x01N\x01W");
}
else {
channels.current.send("\x01ACTION "+param+"\x01");
screen.print_line("\x01N\x01B*\x01W "+nick+" "+param);
}
break;
case "CTCP":
params=param.split(" ");
send_to=params.shift();
full_params=params.join(" ");
full_params=full_params.toUpperCase();
send_cmd("PRIVMSG", send_to+" :\x01"+full_params+"\x01");
break;
case "PART":
// If the user specifies a channel, this SHOULD part that channel,
// not the current one.
channels.part(channels.current.name,param);
break;
case "N":
case "NEXT":
channels.index+=1;
if(channels.index>=channels.length) {
channels.index=0;
}
screen.update_statline();
break;
case "P":
case "PREVIOUS":
case "PREV":
channels.index-=1;
if(channels.index<0) {
channels.index=channels.length-1;
}
screen.update_statline();
break;
case "TOPIC":
if (param.substr(0,1) == '#' || param.substr(0,1) == '&') {
send_cmd(command, param);
}
else {
send_cmd(command, channels.current.name+" "+param);
}
break;
case "KICK":
if (param.substr(0,1) == '#' || param.substr(0,1) == '&') {
send_cmd(command, param);
}
else {
send_cmd(command, channels.current.name+" "+param);
}
break;
case "QUOTE":
if (param.length)
sock.send(param+"\r\n");
break;
default:
if(command[0]=="#" || command[0]=="&") {
for(i=0;i<channels.length;i++) {
if(command.toUpperCase()==channels.channel[i].name) {
channels.index=i;
screen.update_statline();
}
}
}
else {
send_cmd(command, param);
}
}
}
function handle_ctcp(prefix,message) {
var from_nick=null;
var to_nick=null;
var full_message=null;
ctcp_command=message[1].substr(2);
ctcp_command=ctcp_command.replace(/\x01/g,"");
switch(ctcp_command) {
case "ACTION":
message.shift();
message.shift();
from_nick=get_highlighted_nick(prefix,message);
full_message=message.join(" ");
full_message=full_message.replace(/\x01/g,"");
screen.print_line("\x01N\x01B*\x01W "+from_nick+" "+full_message);
break;
case "FINGER":
from_nick=get_highlighted_nick(prefix,message);
to_nick=get_nick(prefix);
send_cmd("NOTICE", to_nick+" :\x01FINGER :"+user.name+" ("+user.alias+") Idle: "+user.timeout+"\x01");
screen.print_line(">"+from_nick+"<"+" CTCP FINGER Reply: "+user.name+" ("+user.alias+") Idle: "+user.timeout);
break;
case "VERSION":
from_nick=get_highlighted_nick(prefix,message);
to_nick=get_nick(prefix);
send_cmd("NOTICE", to_nick+" :\x01VERSION Synchronet IRC Module:"+REVISION+":Synchronet"+"\x01");
screen.print_line(">"+from_nick+"<"+" CTCP VERSION Reply: VERSION Synchronet IRC Module:"+REVISION+":Synchronet");
break;
case "PING":
message.shift();
message.shift();
from_nick=get_highlighted_nick(prefix,message);
to_nick=get_nick(prefix);
send_cmd("NOTICE", to_nick+" :\x01PING "+message.join(" ")+"\x01");
screen.print_line(">"+from_nick+"<"+" CTCP PING Reply.");
break;
case "TIME":
from_nick=get_highlighted_nick(prefix,message);
to_nick=get_nick(prefix);
send_cmd("NOTICE", to_nick+" :\x01TIME "+strftime("%A, %B %d, %I:%M:%S%p, %Y %Z",time())+"\x01");
screen.print_line(">"+from_nick+"<"+" CTCP TIME Reply: "+strftime("%A, %B %d, %I:%M:%S%p, %Y %Z",time() ));
break;
}
}
function get_highlighted_nick(prefix,message) {
var nick_mentioned=0;
var from_nick=null;
// Check if your nick is in the text...
from_nick=prefix;
from_nick=from_nick.split("!",1)[0];
re=new RegExp("\\b"+nick+"\\b","i");
for(j=0;j<message.length;j++) {
if(message[j].search(re)>=0) {
nick_mentioned=1;
}
}
if(nick_mentioned==1) {
from_nick="\x01N\x01Y"+from_nick+"\x01N\x01W";
}
// Not sure if I have to do this... but it makes me feel better.
delete re;
return from_nick;
}
function get_nick(prefix) {
// Check if your nick is in the text...
var to_nick;
to_nick=prefix.split("!",1)[0];
return to_nick;
}
function clean_exit() {
exit();
}
// channel object
function Channel(cname) {
var got="";
this.topic="No topic set";
this.name=cname.toUpperCase();
this.display=cname;
this.topic="";
this.part=Channel_part;
this.send=Channel_send;
this.nick=new Array();
this.matchnick=Channel_matchnick;
}
function Channel_part(message) {
send_cmd("PART", this.name+" :"+message);
screen.print_line("PART "+this.name+" "+message);
this.name=null;
this.display=null;
this.topic=null;
this.part=null;
}
function Channel_send(message) {
send_cmd("PRIVMSG", this.name+" :"+message);
}
function Channel_matchnick(nickpart) {
var i=0;
var j=0;
var count=0;
var tmp_str="\x01N\x01BMatching Nicks:";
var nick_var="";
var partial=nickpart.toUpperCase();
var matched="";
var start="";
if(partial=="") {
screen.print_line("\x01H\x01BNothing to match.\x01N\x01W");
return null;
}
for(i=0;i<this.nick.length;i++) {
if(partial==this.nick[i].substr(0,partial.length).toUpperCase()) {
tmp_str=tmp_str+" "+this.nick[i];
nick_var=this.nick[i];
count++;
if(matched=="") {
matched=nick_var.toUpperCase();
}
else {
nick_var=nick_var.substr(0,matched.length);
for(j=nick_var.length;matched.substr(0,j) != nick_var.substr(0,j).toUpperCase();j--) {}
nick_var=nick_var.substr(0,j);
matched=nick_var.toUpperCase();
}
}
}
if(count<1) {
screen.print_line("\x01H\x01BNo matching nicks.\x01N\x01W");
return null;
}
if(count>1 && matched.length==partial.length) {
screen.print_line(tmp_str+"\x01N\x01W");
return null;
}
return nick_var;
}
// channels object
function Channels() {
this.length=0;
this.index=0;
this.channel=new Array();
this.join=Channels_join;
this.part=Channels_part;
this.joined=Channels_joined;
this.__defineGetter__("current", function() {return this.channel[this.index];});
// Joining is not attempted until numeric 376 (End of MOTD)
// this.join(default_channel);
this.nick_change=Channels_nick_change;
this.nick_quit=Channels_nick_quit;
this.nick_part=Channels_nick_part;
this.nick_add=Channels_nick_add;
}
function Channels_nick_change(from,to) {
var i=0;
var j=0;
for(i=0;i<this.channel.length;i++) {
for(j=0;j<this.channel[i].nick.length;j++) {
if(this.channel[i].nick[j].toUpperCase()==from.toUpperCase()) {
this.channel[i].nick[j]=to;
}
}
}
}
function Channels_nick_quit(nick) {
var i=0;
var j=0;
for(i=0;i<this.channel.length;i++) {
for(j=0;j<this.channel[i].nick.length;j++) {
if(this.channel[i].nick[j].toUpperCase()==nick.toUpperCase()) {
this.channel[i].nick.splice(j,1);
}
}
}
}
function Channels_nick_part(nick,cname) {
var i=0;
var j=0;
for(i=0;i<this.channel.length;i++) {
if(cname.toUpperCase()==this.channel[i].name) {
for(j=0;j<this.channel[i].nick.length;j++) {
if(this.channel[i].nick[j].toUpperCase()==nick.toUpperCase()) {
this.channel[i].nick.splice(j,1);
}
}
}
}
}
function Channels_nick_add(nick,cname) {
var i=0;
var j=0;
for(i=0;i<this.channel.length;i++) {
if(cname.toUpperCase()==this.channel[i].name) {
this.channel[i].nick.push(nick);
}
}
}
function Channels_join(cname) {
send_cmd("JOIN", cname);
}
function Channels_joined(cname) {
this.index=this.channel.length;
this.channel[this.channel.length]=new Channel(cname);
this.length++;
}
function Channels_part(cname,message) {
var i;
if(this.current==undefined) {
return;
}
cname=this.current.name;
for(i=0;i<this.channel.length;i++) {
if(cname.toUpperCase()==this.channel[i].name) {
this.channel[i].part(message);
this.channel.splice(i,1);
this.length -= 1;
}
}
if(this.index>=(this.channel.length-1)) {
this.index=0;
}
}
// Screen object
function Screen() {
console.clear();
this.line=new Array(console.screen_rows-3);
this.rows=console.screen_rows-3; // Title, Status, and input rows are not counted.