-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnntpservice.js
994 lines (904 loc) · 27.2 KB
/
nntpservice.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
// nntpservice.js
// Synchronet Service for the Network News Transfer Protocol (RFC 977)
// $Id: nntpservice.js,v 1.131 2020/04/16 20:47:25 rswindell Exp $
// Example configuration (in ctrl/services.ini):
// [NNTP]
// Port = 119
// MaxClients = 10
// Options = 0
// Command = nntpservice.js -f
// Available Command-line options:
//
// -d debug output
// -f filter bogus client IP addresses
// -na no anonymous logins (requires user authentication)
// -mail expose entire mail database as newsgroup to Sysops
// -auto automatic login based on IP address (no password necessary)
// -nolimit unlimited message lengths
// -notag do not append tear/tagline to local messages for Q-rest accounts
// -ascii convert ex-ASCII to ASCII
// Tested clients:
// Microsoft Outlook Express 6
// Netscape Communicator 4.77
// Xnews 5.04.25
// Mozilla 1.1 (Requires -auto, and a prior login via other method)
const REVISION = "$Revision: 1.131 $".split(' ')[1];
var tearline = format("--- Synchronet %s%s-%s NNTP Service %s\r\n"
,system.version,system.revision,system.platform,REVISION);
var tagline = format(" * %s - %s - telnet://%s\r\n"
,system.name,system.location,system.inetaddr);
load("sbbsdefs.js");
load("newsutil.js");
var debug = false;
var no_anonymous = false;
var auto_login = false;
var msgs_read = 0;
var msgs_posted = 0;
var slave = false;
var bogus_cmd_counter = 0;
var max_bogus_cmds = 10;
var filter_bogus_clients = false;
var include_mail = false;
var impose_limit = true;
var sysop_login = false;
var add_tag = true;
var ex_ascii = true;
var include_votes_in_list = true;
// Parse arguments
for(i=0;i<argc;i++) {
if(argv[i].toLowerCase()=="-d")
debug = true;
else if(argv[i].toLowerCase()=="-f")
filter_bogus_clients = true;
else if(argv[i].toLowerCase()=="-na")
no_anonymous = true;
else if(argv[i].toLowerCase()=="-mail")
include_mail = true;
else if(argv[i].toLowerCase()=="-nolimit")
impose_limit = false;
else if(argv[i].toLowerCase()=="-notag")
add_tag = false;
else if(argv[i].toLowerCase()=="-novotes")
include_votes_in_list = false;
else if(argv[i].toLowerCase()=="-ascii")
ex_ascii = false;
else if(argv[i].toLowerCase()=="-auto") {
no_anonymous = true;
auto_login = true;
}
}
// Write a string to the client socket
function write(str)
{
client.socket.send(str);
}
function writeln(str)
{
if(debug)
log("rsp: " + str);
write(str + "\r\n");
}
// Courtesy of Michael J. Ryan <[email protected]>
// updated 2004-04-16 to lookup by reference id on posting newsgroup.
function getReferenceTo(hdr) {
//Default Response
var to = "All";
var newsgroups = hdr.newsgroups.split(',');
for(n in newsgroups)
for(g in msg_area.grp_list)
for(s in msg_area.grp_list[g].sub_list)
if (msg_area.grp_list[g].sub_list[s].newsgroup.toLowerCase() == newsgroups[n].toLowerCase()) {
var mb=new MsgBase(msg_area.grp_list[g].sub_list[s].code);
if (mb.open() != true) continue;
var hdr2 = mb.get_msg_header(hdr.reply_id);
if (hdr2 != null) to = hdr2.from;
mb.close();
if (to != "All") return to;
}
return to; //no match
}
// Generate an Xref header
function xref(hdr)
{
return(format("%s %s:%u"
,system.local_host_name // "name of the host (with domains omitted)" per RFC1036 sec 2.2.13
,selected.newsgroup
,hdr.number));
}
// This excludes vote messages, but can be "slow"
function count_msgs(msgbase)
{
var total_msgs = msgbase.total_msgs;
var count = 0;
var last = 0;
var first = 0;
for(var i=0;i<total_msgs;i++) {
var idx=msgbase.get_msg_index(/* by_offset */true,i);
if(idx==null)
continue;
if(idx.attr&MSG_DELETE) /* marked for deletion */
continue;
if(first == 0)
first = idx.number;
last = idx.number;
count++;
}
return { total: count, first: first, last: last };
}
function bogus_cmd(cmdline)
{
log(LOG_DEBUG, "Received bogus command: '" + cmdline + "'");
bogus_cmd_counter++;
}
var username='';
var msgbase=null;
var selected=null;
var current_article=0;
var quit=false;
writeln(format("200 %s News (Synchronet %s%s-%s%s NNTP Service %s)"
,system.name,system.version,system.revision,system.platform,system.beta_version
,REVISION));
if(!no_anonymous)
login("guest"); // Login as guest/anonymous by default
while(client.socket.is_connected && !quit) {
if(bogus_cmd_counter) {
log(LOG_DEBUG, "Throttling bogus command sending client for " + bogus_cmd_counter + " seconds");
sleep(bogus_cmd_counter * 1000); // Throttle
}
if(user.security.restrictions&UFLAG_G /* Only guest/anonymous logins can be "bogus" */
&& bogus_cmd_counter >= max_bogus_cmds) {
log(format("!TOO MANY BOGUS COMMANDS (%u)", bogus_cmd_counter));
if(filter_bogus_clients) {
log(LOG_NOTICE,"!FILTERING CLIENT'S IP ADDRESS: " + client.ip_address);
system.filter_ip("NNTP","- TOO MANY BOGUS COMMANDS (Example: " + cmdline +")"
, client.host_name, client.ip_address, client.user_name);
}
break;
}
// Get Request
cmdline = client.socket.recvline(1024 /*maxlen*/, 300 /*timeout*/);
if(cmdline==null) {
if(client.socket.is_connected) {
if(user.security.exemptions&UFLAG_H)
continue;
log(LOG_WARNING, "!TIMEOUT waiting for request");
} else
log(LOG_WARNING, "!client disconnected");
break;
}
if(cmdline=="") { /* ignore blank commands */
bogus_cmd(cmdline);
continue;
}
log((selected==null ? "":("["+selected.newsgroup+"] ")) +format("cmd: %s",cmdline));
cmd=cmdline.split(' ');
switch(cmd[0].toUpperCase()) {
case "AUTHINFO":
if(cmd[1]==undefined) {
writeln("500 Syntax error or unknown command");
break;
}
switch(cmd[1].toUpperCase()) {
case "USER":
username=cmd.slice(2).join(" ");
writeln("381 More authentication required");
break;
case "PASS":
logout();
if(login(username,cmd.slice(2).join(" "))) {
if(no_anonymous && user.security.restrictions&UFLAG_G) {
writeln("502 Anonymous/Guest logins disallowed");
logout();
} else
writeln("281 Authentication successful");
} else
writeln("502 Authentication failure");
break;
default:
writeln("500 Syntax error or unknown command");
break;
}
continue;
case "MODE":
writeln("200 Hello, you can post");
continue;
case "DATE":
d = new Date();
writeln("111 " +
format("%u%02u%02u%02u%02u%02u"
,d.getUTCFullYear()
,d.getUTCMonth()+1
,d.getUTCDate()
,d.getUTCHours()
,d.getUTCMinutes()
,d.getUTCSeconds()
));
continue;
case "QUIT":
writeln("205 closing connection - goodbye!");
quit=true;
continue;
}
if(!logged_in) {
if (auto_login) {
log(LOG_DEBUG,"Autologin Search: Started");
var oUser = new User;
sUser = false;
sPassword = ""
iLastOn = 0;
for (var i=1; i<=system.stats.total_users; i++) {
oUser.number = i;
if (oUser.ip_address == client.ip_address)
if (!(oUser.security.restrictions&UFLAG_Q)) //don't count qnet users
if (oUser.stats.laston_date > iLastOn) { //more recent than last match
iLastOn = oUser.stats.laston_date;
sUser = oUser.alias;
sPassword = oUser.security.password;
log(LOG_DEBUG,"!Autologin Search: Match Found ("+sUser+")");
}
}
oUser = null;
log(LOG_DEBUG,"Autologin Search: Finished");
if (sUser)
login(sUser,sPassword);
}
if(!logged_in) {
writeln("502 Authentication required");
log(LOG_WARNING,"!Authentication required");
continue;
}
}
/* These commands require login/authentication */
switch(cmd[0].toUpperCase()) {
case "SLAVE":
slave = true;
writeln("202 slave status noted");
break;
case "LIST":
if(cmd[1]==undefined || cmd[1].length==0
|| cmd[1].toUpperCase()=="ACTIVE") { // RFC 2980 2.1.2
pattern=cmd[2];
writeln("215 list of newsgroups follows");
if(include_mail && user.security.level == 99 && wildmatch("mail", pattern)) {
msgbase=new MsgBase("mail");
if(msgbase.open()==true) {
writeln(format("mail %u %u n", msgbase.last_msg, msgbase.first_msg));
msgbase.close();
}
}
for(g in msg_area.grp_list)
for(s in msg_area.grp_list[g].sub_list) {
if(!wildmatch(msg_area.grp_list[g].sub_list[s].newsgroup, pattern))
continue;
msgbase=new MsgBase(msg_area.grp_list[g].sub_list[s].code);
if(msgbase.open!=undefined && msgbase.open()==false)
continue;
var count;
if(include_votes_in_list)
count = { total: msgbase.total_msgs, first: msgbase.first_msg, last: msgbase.last_msg };
else
count = count_msgs(msgbase);
writeln(format("%s %u %u %s"
,msg_area.grp_list[g].sub_list[s].newsgroup
,count.last
,count.first
,msg_area.grp_list[g].sub_list[s].is_moderated ? "m" : (msg_area.grp_list[g].sub_list[s].can_post ? "y" : "n")
));
msgbase.close();
}
writeln("."); // end of list
}
else if(cmd[1].toUpperCase()=="NEWSGROUPS") { // RFC 2980 2.1.6
pattern=cmd[2];
writeln("215 list of newsgroups and descriptions follows");
if(include_mail && user.security.level == 99 && wildmatch("mail", pattern))
writeln("mail complete mail database");
for(g in msg_area.grp_list) {
for(s in msg_area.grp_list[g].sub_list) {
if(!wildmatch(msg_area.grp_list[g].sub_list[s].newsgroup, pattern))
continue;
writeln(format("%s %s"
,msg_area.grp_list[g].sub_list[s].newsgroup
,msg_area.grp_list[g].sub_list[s].description
));
}
}
writeln("."); // end of list
}
else if(cmd[1].toUpperCase()=="OVERVIEW.FMT") { // RFC 2980 2.1.7
writeln("215 Order of fields in overview database.");
writeln("Subject:");
writeln("From:");
writeln("Date:");
writeln("Message-ID:");
writeln("References:");
writeln("Bytes:");
writeln("Lines:");
writeln("Xref:full");
writeln("."); // end of list
}
else if(cmd[1].toUpperCase()=="EXTENSIONS") {
writeln("202 Extensions supported:");
writeln("OVER");
writeln("HDR");
writeln("LISTGROUP");
writeln("XGTITLE");
writeln("."); // end of list
}
else {
writeln("500 Syntax error or unknown command");
log(LOG_NOTICE,"!unsupported LIST argument: " + cmd[1]);
}
break;
case "XGTITLE":
pattern=cmd[2];
writeln("282 list of newsgroups follows");
if(include_mail && user.security.level == 99 && wildmatch("mail", pattern))
writeln("mail complete mail database");
for(g in msg_area.grp_list) {
for(s in msg_area.grp_list[g].sub_list) {
if(!wildmatch(msg_area.grp_list[g].sub_list[s].newsgroup, pattern))
continue;
writeln(format("%s %s"
,msg_area.grp_list[g].sub_list[s].newsgroup
,msg_area.grp_list[g].sub_list[s].description
));
}
}
writeln("."); // end of list
break;
case "NEWGROUPS":
var date = cmd[1];
var time = cmd[2];
if(!date || !time) {
writeln("411 no date or time specified");
break;
}
var zone = cmd[3];
var year, month, day;
if(date.length == 6) {
var ng_year = '' + new Date().getFullYear();
year = parseInt(date.substr(0, 2), 10);
if (parseInt(year, 10) <= parseInt(ng_year.substr(2, 2), 10)) {
year = parseInt(ng_year.substr(0, 2) + year, 10);
} else {
var ng_pc = parseInt(ng_year.substr(0, 2)) - 1;
year = parseInt('' + ng_pc + year, 10);
}
month = parseInt(date.substr(2, 2), 10) - 1;
day = parseInt(date.substr(4, 2), 10);
} else {
year = parseInt(date.substr(0, 4));
month = parseInt(date.substr(4, 2), 10) - 1;
day = parseInt(date.substr(6, 2), 10);
}
var compare;
if(zone == "GMT")
compare = new Date(Date.UTC(year, month, day,
/* hour: */parseInt(time.substr(0, 2)),
/* minute: */parseInt(time.substr(2, 2)),
/* seconds: */parseInt(time.substr(4, 2))
));
else
compare = new Date(year, month, day,
/* hour: */parseInt(time.substr(0, 2)),
/* minute: */parseInt(time.substr(2, 2)),
/* seconds: */parseInt(time.substr(4, 2))
);
writeln("231 list of new newsgroups since " + compare.toISOString() + " follows");
for(g in msg_area.grp_list) {
for(s in msg_area.grp_list[g].sub_list) {
msgbase=new MsgBase(msg_area.grp_list[g].sub_list[s].code);
var ini_file = new File(msgbase.file + ".ini");
if(ini_file.open("r")) {
var created = ini_file.iniGetValue(null, "Created", 0);
ini_file.close();
if(created >= compare.getTime() / 1000
&& msgbase.open()) {
var count = count_msgs(msgbase);
writeln(format("%s %u %u %s"
,msg_area.grp_list[g].sub_list[s].newsgroup
,count.last
,count.first
,msg_area.grp_list[g].sub_list[s].is_moderated ? "m" : (msg_area.grp_list[g].sub_list[s].can_post ? "y" : "n")
));
msgbase.close();
}
}
}
}
writeln("."); // end of list
break;
case "GROUP":
if(cmd[1]==undefined || cmd[1].length==0) {
writeln("411 no group specified");
break;
}
case "LISTGROUP":
found=false;
if(cmd[1]==undefined || cmd[1].length==0) {
if(!selected) {
writeln("412 no newsgroup selected");
break;
}
found=true;
}
else if(include_mail && user.security.level==99 && cmd[1].toLowerCase()=="mail") {
msgbase=new MsgBase("mail");
if(msgbase.open()==true) {
selected = { newsgroup: "mail" };
found=true;
}
}
if(!found) {
for(g in msg_area.grp_list)
for(s in msg_area.grp_list[g].sub_list)
if(msg_area.grp_list[g].sub_list[s].newsgroup.toLowerCase()==cmd[1].toLowerCase()) {
msgbase=new MsgBase(msg_area.grp_list[g].sub_list[s].code);
if(msgbase.open!=undefined && msgbase.open()==false)
continue;
found=true;
selected=msg_area.grp_list[g].sub_list[s];
break;
}
}
if(!found) {
writeln("411 no such newsgroup");
log(LOG_NOTICE,"!no such group");
bogus_cmd(cmdline);
break;
}
if(cmd[0].toUpperCase()=="GROUP") {
var count = count_msgs(msgbase);
writeln(format("211 %u %u %d %s group selected"
,count.total // articles in group
,count.first
,count.last
,selected.newsgroup
));
} else { // LISTGROUP
writeln("211 list of article numbers follow");
var total_msgs = msgbase.total_msgs;
for(i=0;i<total_msgs;i++) {
var idx=msgbase.get_msg_index(/* by_offset */true,i);
if(idx==null)
continue;
if(idx.attr&MSG_DELETE) /* marked for deletion */
continue;
writeln(idx.number);
}
writeln(".");
}
break;
case "OVER":
case "XOVER":
if(!selected) {
writeln("412 no newsgroup selected");
break;
}
if(!selected.can_read) {
writeln("412 read permission to newsgroup denied");
break;
}
var first, last;
if(cmd[1]==undefined || cmd[1].length==0)
first=last=current_article;
else if(cmd[1].indexOf('-')>=0) { /* range */
range=cmd[1].split('-');
first=Number(range[0]);
last=Number(range[1]);
if(last == 0)
last = msgbase.last_msg;
} else
first=last=Number(cmd[1]);
writeln("224 Overview information follows for articles " + first + " through " + last);
for(i=first;i<=last;i++) {
hdr=msgbase.get_msg_header(false,i);
if(hdr==null)
continue;
if(hdr.attr&MSG_DELETE) /* marked for deletion */
continue;
writeln(format("%u\t%s\t%s\t%s\t%s\t%s\t%u\t%u\tXref:%s"
,i
,hdr.subject
,hdr.from
,hdr.date
,hdr.id // message-id
,hdr.reply_id ? hdr.reply_id : '' // references
,hdr.data_length // byte count
,Math.round(hdr.data_length/79)+1 // line count
,xref(hdr)
));
}
writeln("."); // end of list
break;
case "HDR":
case "XHDR":
if(cmd[1]==undefined || cmd[2]==undefined) {
writeln("500 Syntax error or unknown command");
break;
}
if(!selected) {
writeln("412 no newsgroup selected");
break;
}
if(!selected.can_read) {
writeln("412 read permission to newsgroup denied");
break;
}
writeln("221 Header follows");
var first, last;
if(cmd[2].indexOf('-')>=0) { /* range */
range=cmd[2].split('-');
first=Number(range[0]);
last=Number(range[1]);
} else
first=last=Number(cmd[2]);
for(i=first;i<=last;i++) {
hdr=msgbase.get_msg_header(false,i);
if(hdr==null)
continue;
if(hdr.attr&MSG_DELETE) /* marked for deletion */
continue;
var field="";
switch(cmd[1].toLowerCase()) { /* header */
case "to":
field=hdr.to;
break;
case "subject":
field=hdr.subject;
break;
case "from":
field=hdr.from;
break;
case "reply-to":
field=hdr.replyto;
break;
case "date":
field=hdr.date;
break;
case "message-id":
field=hdr.id;
break;
case "references":
field=hdr.reply_id;
break;
case "lines":
field=Math.round(hdr.data_length/79)+1;
break;
case "xref":
field=xref(hdr);
break;
/* FidoNet header fields */
case "x-ftn-pid":
field=hdr.ftn_pid;
break;
case "x-ftn-area":
field=hdr.ftn_area;
break;
case "x-ftn-flags":
field=hdr.ftn_flags;
break;
case "x-ftn-msgid":
field=hdr.ftn_msgid;
break;
case "x-ftn-reply":
field=hdr.ftn_reply;
break;
}
if(field==undefined)
field="";
writeln(format("%u %s",i,field.toString()));
}
writeln("."); // end of list
break;
case "ARTICLE":
case "HEAD":
case "BODY":
case "STAT":
if(!selected) {
writeln("412 no newsgroup selected");
bogus_cmd(cmdline);
break;
}
if(!selected.can_read) {
writeln("412 read permission to newsgroup denied");
break;
}
if(cmd[1]==undefined || cmd[1].length==0) {
writeln("420 no current article has been selected");
bogus_cmd(cmdline);
break;
}
if(cmd[1]!='') {
if(cmd[1].indexOf('<')>=0) /* message-id */
current_article=cmd[1];
else
current_article=Number(cmd[1]);
}
if(typeof(current_article)=="number"
&& (current_article<1 || isNaN(current_article))) {
writeln("420 no current article has been selected");
break;
}
hdr=null;
body=null;
hdr=msgbase.get_msg_header(false,current_article);
if(hdr==null) {
writeln("430 no such article found: " + current_article);
break;
}
current_article=hdr.number;
if(cmd[0].toUpperCase()!="HEAD") {
body=msgbase.get_msg_body(false,current_article
,true /* remove ctrl-a codes */
,true /* rfc822 formatted text */);
if(!body) {
writeln("430 error getting message body: " + msgbase.last_error);
break;
}
// force taglines for QNET Users on local messages
if(add_tag && user.security.restrictions&UFLAG_Q && !hdr.from_net_type)
body += "\r\n" + tearline + tagline;
if(!ex_ascii || (msgbase.cfg && msgbase.cfg.settings&SUB_ASCII)) {
/* Convert Ex-ASCII chars to approximate ASCII equivalents */
body = ascii_str(body);
hdr.subject = ascii_str(hdr.subject);
}
}
/* Eliminate dupe loops
if(user.security.restrictions&UFLAG_Q && hdr!=null)
*/
if(hdr.attr&MSG_MODERATED && !(hdr.attr&MSG_VALIDATED)) {
writeln("430 unvalidated message");
break;
}
if(hdr.attr&MSG_DELETE) {
writeln("430 deleted message");
break;
}
if(hdr.attr&MSG_PRIVATE
&& hdr.to.toLowerCase()!=user.alias.toLowerCase()
&& hdr.to.toLowerCase()!=user.name.toLowerCase()) {
writeln("430 private message");
break;
}
switch(cmd[0].toUpperCase()) {
case "ARTICLE":
writeln(format("220 %d %s article retrieved - head and body follow",current_article,hdr.id));
break;
case "HEAD":
writeln(format("221 %d %s article retrieved - header follows",current_article,hdr.id));
break;
case "BODY":
writeln(format("222 %d %s article retrieved - body follows",current_article,hdr.id));
break;
case "STAT":
writeln(format("223 %d %s article retrieved",current_article,hdr.id));
break;
}
if(cmd[0].toUpperCase()=="STAT")
break;
if(cmd[0].toUpperCase()!="BODY") {
if(!hdr.from_net_type || !hdr.from_net_addr) /* local message */
writeln(format("From: \"%s\" <%s@%s>"
,hdr.from
,hdr.from.replace(/ /g,".").toLowerCase()
,system.inetaddr));
else if(!hdr.from_net_addr.length)
writeln(format("From: %s",hdr.from));
else if(hdr.from_net_addr.indexOf('@')!=-1)
writeln(format("From: \"%s\" <%s>"
,hdr.from
,hdr.from_net_addr));
else
writeln(format("From: \"%s\" <%s@%s>"
,hdr.from
,hdr.from.replace(/ /g,".").toLowerCase()
,hdr.from_net_addr));
if(hdr.path==undefined)
hdr.path="not-for-mail";
if(hdr.newsgroups==undefined)
hdr.newsgroups = selected.newsgroup;
else { /* Tracker1's mod for adding the correct newsgroup name */
var ng_found = false; /* Requires sbbs v3.13 */
var ng_list = hdr.newsgroups.split(',');
for(n in ng_list)
if(ng_list[n].toLowerCase() == selected.newsgroup.toLowerCase()) {
ng_found = true;
break;
}
if(!ng_found)
hdr.newsgroups = selected.newsgroup + ',' + hdr.newsgroups;
}
if(hdr.from_org==undefined && !hdr.from_net_type)
hdr.from_org=system.name;
write_news_header(hdr,writeln); // from newsutil.js
}
if(hdr!=null && body!=null) /* both, separate with blank line */
writeln("");
if(body!=null) {
writeln(truncsp(body));
msgs_read++;
}
writeln(".");
break;
case "NEXT":
case "LAST":
if(!selected) {
writeln("412 no newsgroup selected");
break;
}
if(current_article<1) {
writeln("420 no current article has been selected");
break;
}
if(cmd[0].toUpperCase()=="NEXT")
current_article++;
else
current_article--;
writeln(format("223 %u %u article retrieved - request text separately"
,current_article
,current_article
));
break;
case "IHAVE":
if(cmd[1]!=undefined && cmd[1].indexOf('@' + system.inetaddr)!=-1) { // avoid dupe loop
writeln("435 that article came from here. Don't send.");
continue;
}
case "POST":
if(user.security.restrictions&UFLAG_P) {
writeln("440 posting not allowed");
break;
}
writeln("340 send article to be posted. End with <CR-LF>.<CR-LF>");
var posted=false;
var header=true;
var hfields=[];
var body="";
var lines=0;
while(client.socket.is_connected) {
line = client.socket.recvline(4096 /*maxlen*/, 300 /*timeout*/);
if(line==null) {
log(LOG_NOTICE,"!TIMEOUT waiting for text line");
break;
}
//log(format("msgtxt: %s",line));
if(line==".") {
log(format("End of message text (%u chars)",body.length));
break;
}
if(line=="" && header) {
header=false;
continue;
}
if(!header) { /* Body text, append to 'body' */
if(line.charAt(0)=='.')
line=line.slice(1); // Skip prepended dots
body += line;
body += "\r\n";
lines++;
continue;
}
log(line);
if((line.charAt(0)==' ' || line.charAt(0)=='\t') && hfields.length) {
while(line.charAt(0)==' ' // trim prepended spaces
|| line.charAt(0)=='\t')
line=line.slice(1);
hfields[hfields.length-1] += line; // folded header field
} else
hfields.push(line);
}
if(impose_limit && user.limits!=undefined
&& lines > user.limits.lines_per_message) {
log(format("!Message of %u lines exceeds user limit (%u lines)"
,lines,user.limits.lines_per_message));
writeln(format("441 posting failed, number of lines (%u) exceeds max (%u)"
,lines,user.limits.lines_per_message));
break;
}
// Parse the message header
var hdr={ from: "", subject: "" };
for(h in hfields)
parse_news_header(hdr,hfields[h]); // from newsutil.js
body=decode_news_body(hdr, body);
var newsgroups=hdr.newsgroups.split(',');
if(hdr.to==undefined && hdr.reply_id!=undefined)
hdr.to=getReferenceTo(hdr);
if(hdr.to==undefined && hdr.newsgroups!=undefined)
hdr.to=hdr.newsgroups;
if(!(user.security.restrictions&(UFLAG_G|UFLAG_Q))) { // !Guest and !Network Node
hdr.from=user.alias;
hdr.from_ext=user.number;
}
if(!(user.security.restrictions&UFLAG_Q)) // Treat this as a local message
hdr.from_net_type=NET_NONE;
if(system.trashcan("subject",hdr.subject)) {
log(format("!BLOCKED subject: %s",hdr.subject));
var reason = format("Blocked subject from %s (%s): %s"
,user.alias,hdr.from,hdr.subject);
system.spamlog("NNTP","BLOCKED"
,reason,client.host_name,client.ip_address,hdr.to);
writeln("441 posting failed");
break;
}
for(n in newsgroups)
for(g in msg_area.grp_list)
for(s in msg_area.grp_list[g].sub_list)
if(msg_area.grp_list[g].sub_list[s].newsgroup.toLowerCase()
==newsgroups[n].toLowerCase()) {
if(!msg_area.grp_list[g].sub_list[s].can_post)
continue;
if(msgbase!=null) {
msgbase.close();
delete msgbase;
}
msgbase=new MsgBase(msg_area.grp_list[g].sub_list[s].code);
if(msgbase.open!=undefined && msgbase.open()==false)
continue;
/* NNTP Control Message? */
if(hdr.control!=undefined) {
var ctrl_msg = hdr.control.split(/\s+/);
var target;
if(ctrl_msg.length) {
switch(ctrl_msg[0].toLowerCase()) {
case "cancel":
target=msgbase.get_msg_header(ctrl_msg[1]);
if(target==null) {
log(LOG_NOTICE,"!Invalid Message-ID: " + ctrl_msg[1]);
break;
}
if(logged_in && ((target.from_ext==user.number
&& msg_area.grp_list[g].sub_list[s].settings&SUB_DEL)
|| msg_area.grp_list[g].sub_list[s].is_operator)) {
if(msgbase.remove_msg(ctrl_msg[1])) {
posted=true;
log(LOG_NOTICE,"Message deleted: " + ctrl_msg[1]);
} else
log(LOG_ERR,"!ERROR " + msgbase.error +
" deleting message: " + ctrl_msg[1]);
continue;
}
break;
}
}
log(LOG_WARNING,"!Invalid control message: " + hdr.control);
break;
}
if(msg_area.grp_list[g].sub_list[s].settings&SUB_NAME
&& !(user.security.restrictions&(UFLAG_G|UFLAG_Q)))
hdr.from=user.name; // Use real names
if(msg_area.grp_list[g].sub_list[s].is_moderated)
hdr.attr|=MSG_MODERATED;
else
hdr.attr&=~MSG_MODERATED;
if(msgbase.save_msg(hdr,client,body)) {
log(format("%s posted a message (%u chars, %u lines) on %s"
,user.alias, body.length, lines, newsgroups[n]));
posted=true;
msgs_posted++;
} else
log(msgbase.status > 0 ? LOG_WARNING:LOG_ERR
,format("!ERROR %d saving mesage: %s"
,msgbase.status, msgbase.last_error));
}
if(posted)
writeln("240 article posted ok");
else {
log(LOG_WARNING,"!post failure");
writeln("441 posting failed");
}
break;
default:
writeln("500 Syntax error or unknown command");
log(LOG_NOTICE,"!unknown command");
break;
}
}
// Log statistics
if(msgs_read)
log(format("%u messages read",msgs_read));
if(msgs_posted)
log(format("%u messages posted",msgs_posted));
/* End of nntpservice.js */