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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
|
/*
* Network news transport protocol (NNTP) file server.
*
* Unfortunately, the file system differs from that expected
* by Charles Forsyth's rin news reader. This is partially out
* of my own laziness, but it makes the bookkeeping here
* a lot easier.
*/
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
typedef struct Netbuf Netbuf;
typedef struct Group Group;
struct Netbuf {
Biobuf br;
Biobuf bw;
int lineno;
int fd;
int code; /* last response code */
int auth; /* Authorization required? */
char response[128]; /* last response */
Group *currentgroup;
char *addr;
char *user;
char *pass;
ulong extended; /* supported extensions */
};
struct Group {
char *name;
Group *parent;
Group **kid;
int num;
int nkid;
int lo, hi;
int canpost;
int isgroup; /* might just be piece of hierarchy */
ulong mtime;
ulong atime;
};
/*
* First eight fields are, in order:
* article number, subject, author, date, message-ID,
* references, byte count, line count
* We don't support OVERVIEW.FMT; when I see a server with more
* interesting fields, I'll implement support then. In the meantime,
* the standard defines the first eight fields.
*/
/* Extensions */
enum {
Nxover = (1<<0),
Nxhdr = (1<<1),
Nxpat = (1<<2),
Nxlistgp = (1<<3),
};
Group *root;
Netbuf *net;
ulong now;
int netdebug;
int readonly;
void*
erealloc(void *v, ulong n)
{
v = realloc(v, n);
if(v == nil)
sysfatal("out of memory reallocating %lud", n);
setmalloctag(v, getcallerpc(&v));
return v;
}
void*
emalloc(ulong n)
{
void *v;
v = malloc(n);
if(v == nil)
sysfatal("out of memory allocating %lud", n);
memset(v, 0, n);
setmalloctag(v, getcallerpc(&n));
return v;
}
char*
estrdup(char *s)
{
int l;
char *t;
if (s == nil)
return nil;
l = strlen(s)+1;
t = emalloc(l);
memcpy(t, s, l);
setmalloctag(t, getcallerpc(&s));
return t;
}
char*
estrdupn(char *s, int n)
{
int l;
char *t;
l = strlen(s);
if(l > n)
l = n;
t = emalloc(l+1);
memmove(t, s, l);
t[l] = '\0';
setmalloctag(t, getcallerpc(&s));
return t;
}
char*
Nrdline(Netbuf *n)
{
char *p;
int l;
n->lineno++;
Bflush(&n->bw);
if((p = Brdline(&n->br, '\n')) == nil){
werrstr("nntp eof");
return nil;
}
p[l=Blinelen(&n->br)-1] = '\0';
if(l > 0 && p[l-1] == '\r')
p[l-1] = '\0';
if(netdebug)
fprint(2, "-> %s\n", p);
return p;
}
int
nntpresponse(Netbuf *n, int e, char *cmd)
{
int r;
char *p;
for(;;){
p = Nrdline(n);
if(p==nil){
strcpy(n->response, "early nntp eof");
return -1;
}
r = atoi(p);
if(r/100 == 1){ /* BUG? */
fprint(2, "%s\n", p);
continue;
}
break;
}
strecpy(n->response, n->response+sizeof(n->response), p);
if((r=atoi(p)) == 0){
close(n->fd);
n->fd = -1;
fprint(2, "bad nntp response: %s\n", p);
werrstr("bad nntp response");
return -1;
}
n->code = r;
if(0 < e && e<10 && r/100 != e){
fprint(2, "%s: expected %dxx: got %s\n", cmd, e, n->response);
return -1;
}
if(10 <= e && e<100 && r/10 != e){
fprint(2, "%s: expected %dx: got %s\n", cmd, e, n->response);
return -1;
}
if(100 <= e && r != e){
fprint(2, "%s: expected %d: got %s\n", cmd, e, n->response);
return -1;
}
return r;
}
int nntpauth(Netbuf*);
int nntpxcmdprobe(Netbuf*);
int nntpcurrentgroup(Netbuf*, Group*);
/* XXX: bug OVER/XOVER et al. */
static struct {
ulong n;
char *s;
} extensions [] = {
{ Nxover, "OVER" },
{ Nxhdr, "HDR" },
{ Nxpat, "PAT" },
{ Nxlistgp, "LISTGROUP" },
{ 0, nil }
};
static int indial;
int
nntpconnect(Netbuf *n)
{
n->currentgroup = nil;
close(n->fd);
if((n->fd = dial(n->addr, nil, nil, nil)) < 0){
snprint(n->response, sizeof n->response, "dial: %r");
return -1;
}
Binit(&n->br, n->fd, OREAD);
Binit(&n->bw, n->fd, OWRITE);
if(nntpresponse(n, 20, "greeting") < 0)
return -1;
readonly = (n->code == 201);
indial = 1;
if(n->auth != 0)
nntpauth(n);
// nntpxcmdprobe(n);
indial = 0;
return 0;
}
int
nntpcmd(Netbuf *n, char *cmd, int e)
{
int tried;
tried = 0;
for(;;){
if(netdebug)
fprint(2, "<- %s\n", cmd);
Bprint(&n->bw, "%s\r\n", cmd);
if(nntpresponse(n, e, cmd)>=0 && (e < 0 || n->code/100 != 5))
return 0;
/* redial */
if(indial || tried++ || nntpconnect(n) < 0)
return -1;
}
}
int
nntpauth(Netbuf *n)
{
char cmd[256];
snprint(cmd, sizeof cmd, "AUTHINFO USER %s", n->user);
if (nntpcmd(n, cmd, -1) < 0 || n->code != 381) {
fprint(2, "Authentication failed: %s\n", n->response);
return -1;
}
snprint(cmd, sizeof cmd, "AUTHINFO PASS %s", n->pass);
if (nntpcmd(n, cmd, -1) < 0 || n->code != 281) {
fprint(2, "Authentication failed: %s\n", n->response);
return -1;
}
return 0;
}
int
nntpxcmdprobe(Netbuf *n)
{
int i;
char *p;
n->extended = 0;
if (nntpcmd(n, "LIST EXTENSIONS", 0) < 0 || n->code != 202)
return 0;
while((p = Nrdline(n)) != nil) {
if (strcmp(p, ".") == 0)
break;
for(i=0; extensions[i].s != nil; i++)
if (cistrcmp(extensions[i].s, p) == 0) {
n->extended |= extensions[i].n;
break;
}
}
return 0;
}
/* XXX: searching, lazy evaluation */
static int
overcmp(void *v1, void *v2)
{
int a, b;
a = atoi(*(char**)v1);
b = atoi(*(char**)v2);
if(a < b)
return -1;
else if(a > b)
return 1;
return 0;
}
enum {
XoverChunk = 100,
};
char *xover[XoverChunk];
int xoverlo;
int xoverhi;
int xovercount;
Group *xovergroup;
char*
nntpover(Netbuf *n, Group *g, int m)
{
int i, lo, hi, mid, msg;
char *p;
char cmd[64];
if (g->isgroup == 0) /* BUG: should check extension capabilities */
return nil;
if(g != xovergroup || m < xoverlo || m >= xoverhi){
lo = (m/XoverChunk)*XoverChunk;
hi = lo+XoverChunk;
if(lo < g->lo)
lo = g->lo;
else if (lo > g->hi)
lo = g->hi;
if(hi < lo || hi > g->hi)
hi = g->hi;
if(nntpcurrentgroup(n, g) < 0)
return nil;
if(lo == hi)
snprint(cmd, sizeof cmd, "XOVER %d", hi);
else
snprint(cmd, sizeof cmd, "XOVER %d-%d", lo, hi-1);
if(nntpcmd(n, cmd, 224) < 0)
return nil;
for(i=0; (p = Nrdline(n)) != nil; i++) {
if(strcmp(p, ".") == 0)
break;
if(i >= XoverChunk)
sysfatal("news server doesn't play by the rules");
free(xover[i]);
xover[i] = emalloc(strlen(p)+2);
strcpy(xover[i], p);
strcat(xover[i], "\n");
}
qsort(xover, i, sizeof(xover[0]), overcmp);
xovercount = i;
xovergroup = g;
xoverlo = lo;
xoverhi = hi;
}
lo = 0;
hi = xovercount;
/* search for message */
while(lo < hi){
mid = (lo+hi)/2;
msg = atoi(xover[mid]);
if(m == msg)
return xover[mid];
else if(m < msg)
hi = mid;
else
lo = mid+1;
}
return nil;
}
/*
* Return the new Group structure for the group name.
* Destroys name.
*/
static int printgroup(char*,Group*);
Group*
findgroup(Group *g, char *name, int mk)
{
int lo, hi, m;
char *p, *q;
static int ngroup;
for(p=name; *p; p=q){
if(q = strchr(p, '.'))
*q++ = '\0';
else
q = p+strlen(p);
lo = 0;
hi = g->nkid;
while(hi-lo > 1){
m = (lo+hi)/2;
if(strcmp(p, g->kid[m]->name) < 0)
hi = m;
else
lo = m;
}
assert(lo==hi || lo==hi-1);
if(lo==hi || strcmp(p, g->kid[lo]->name) != 0){
if(mk==0)
return nil;
if(g->nkid%16 == 0)
g->kid = erealloc(g->kid, (g->nkid+16)*sizeof(g->kid[0]));
/*
* if we're down to a single place 'twixt lo and hi, the insertion might need
* to go at lo or at hi. strcmp to find out. the list needs to stay sorted.
*/
if(lo==hi-1 && strcmp(p, g->kid[lo]->name) < 0)
hi = lo;
if(hi < g->nkid)
memmove(g->kid+hi+1, g->kid+hi, sizeof(g->kid[0])*(g->nkid-hi));
g->nkid++;
g->kid[hi] = emalloc(sizeof(*g));
g->kid[hi]->parent = g;
g = g->kid[hi];
g->name = estrdup(p);
g->num = ++ngroup;
g->mtime = time(0);
}else
g = g->kid[lo];
}
if(mk)
g->isgroup = 1;
return g;
}
static int
printgroup(char *s, Group *g)
{
if(g->parent == g)
return 0;
if(printgroup(s, g->parent))
strcat(s, ".");
strcat(s, g->name);
return 1;
}
static char*
Nreaddata(Netbuf *n)
{
char *p, *q;
int l;
p = nil;
l = 0;
for(;;){
q = Nrdline(n);
if(q==nil){
free(p);
return nil;
}
if(strcmp(q, ".")==0)
return p;
if(q[0]=='.')
q++;
p = erealloc(p, l+strlen(q)+1+1);
strcpy(p+l, q);
strcat(p+l, "\n");
l += strlen(p+l);
}
}
/*
* Return the output of a HEAD, BODY, or ARTICLE command.
*/
char*
nntpget(Netbuf *n, Group *g, int msg, char *retr)
{
char *s;
char cmd[1024];
if(g->isgroup == 0){
werrstr("not a group");
return nil;
}
if(strcmp(retr, "XOVER") == 0){
s = nntpover(n, g, msg);
if(s == nil)
s = "";
return estrdup(s);
}
if(nntpcurrentgroup(n, g) < 0)
return nil;
sprint(cmd, "%s %d", retr, msg);
nntpcmd(n, cmd, 0);
if(n->code/10 != 22)
return nil;
return Nreaddata(n);
}
int
nntpcurrentgroup(Netbuf *n, Group *g)
{
char cmd[1024];
if(n->currentgroup != g){
strcpy(cmd, "GROUP ");
printgroup(cmd, g);
if(nntpcmd(n, cmd, 21) < 0)
return -1;
n->currentgroup = g;
}
return 0;
}
void
nntprefreshall(Netbuf *n)
{
char *f[10], *p;
int hi, lo, nf;
Group *g;
if(nntpcmd(n, "LIST", 21) < 0)
return;
while(p = Nrdline(n)){
if(strcmp(p, ".")==0)
break;
nf = getfields(p, f, nelem(f), 1, "\t\r\n ");
if(nf != 4){
int i;
for(i=0; i<nf; i++)
fprint(2, "%s%s", i?" ":"", f[i]);
fprint(2, "\n");
fprint(2, "syntax error in group list, line %d", n->lineno);
return;
}
g = findgroup(root, f[0], 1);
hi = strtol(f[1], 0, 10)+1;
lo = strtol(f[2], 0, 10);
if(g->hi != hi){
g->hi = hi;
if(g->lo==0)
g->lo = lo;
g->canpost = f[3][0] == 'y';
g->mtime = time(0);
}
}
}
void
nntprefresh(Netbuf *n, Group *g)
{
char cmd[1024];
char *f[5];
int lo, hi;
if(g->isgroup==0)
return;
if(time(0) - g->atime < 30)
return;
strcpy(cmd, "GROUP ");
printgroup(cmd, g);
if(nntpcmd(n, cmd, 21) < 0){
n->currentgroup = nil;
return;
}
n->currentgroup = g;
if(tokenize(n->response, f, nelem(f)) < 4){
fprint(2, "error reading GROUP response");
return;
}
/* backwards from LIST! */
hi = strtol(f[3], 0, 10)+1;
lo = strtol(f[2], 0, 10);
if(g->hi != hi){
g->mtime = time(0);
if(g->lo==0)
g->lo = lo;
g->hi = hi;
}
g->atime = time(0);
}
char*
nntppost(Netbuf *n, char *msg)
{
char *p, *q;
if(nntpcmd(n, "POST", 34) < 0)
return n->response;
for(p=msg; *p; p=q){
if(q = strchr(p, '\n'))
*q++ = '\0';
else
q = p+strlen(p);
if(p[0]=='.')
Bputc(&n->bw, '.');
Bwrite(&n->bw, p, strlen(p));
Bputc(&n->bw, '\r');
Bputc(&n->bw, '\n');
}
Bprint(&n->bw, ".\r\n");
if(nntpresponse(n, 0, nil) < 0)
return n->response;
if(n->code/100 != 2)
return n->response;
return nil;
}
/*
* Because an expanded QID space makes thngs much easier,
* we sleazily use the version part of the QID as more path bits.
* Since we make sure not to mount ourselves cached, this
* doesn't break anything (unless you want to bind on top of
* things in this file system). In the next version of 9P, we'll
* have more QID bits to play with.
*
* The newsgroup is encoded in the top 15 bits
* of the path. The message number is the bottom 17 bits.
* The file within the message directory is in the version [sic].
*/
enum { /* file qids */
Qhead,
Qbody,
Qarticle,
Qxover,
Nfile,
};
char *filename[] = {
"header",
"body",
"article",
"xover",
};
char *nntpname[] = {
"HEAD",
"BODY",
"ARTICLE",
"XOVER",
};
#define GROUP(p) (((p)>>17)&0x3FFF)
#define MESSAGE(p) ((p)&0x1FFFF)
#define FILE(v) ((v)&0x3)
#define PATH(g,m) ((((g)&0x3FFF)<<17)|((m)&0x1FFFF))
#define POST(g) PATH(0,g,0)
#define VERS(f) ((f)&0x3)
typedef struct Aux Aux;
struct Aux {
Group *g;
int n;
int ispost;
int file;
char *s;
int ns;
int offset;
};
static void
fsattach(Req *r)
{
Aux *a;
char *spec;
spec = r->ifcall.aname;
if(spec && spec[0]){
respond(r, "invalid attach specifier");
return;
}
a = emalloc(sizeof *a);
a->g = root;
a->n = -1;
r->fid->aux = a;
r->ofcall.qid = (Qid){0, 0, QTDIR};
r->fid->qid = r->ofcall.qid;
respond(r, nil);
}
static char*
fsclone(Fid *ofid, Fid *fid)
{
Aux *a;
a = emalloc(sizeof(*a));
*a = *(Aux*)ofid->aux;
fid->aux = a;
return nil;
}
static char*
fswalk1(Fid *fid, char *name, Qid *qid)
{
char *p;
int i, isdotdot, n;
Aux *a;
Group *ng;
isdotdot = strcmp(name, "..")==0;
a = fid->aux;
if(a->s) /* file */
return "protocol botch";
if(a->n != -1){
if(isdotdot){
*qid = (Qid){PATH(a->g->num, 0), 0, QTDIR};
fid->qid = *qid;
a->n = -1;
return nil;
}
for(i=0; i<Nfile; i++){
if(strcmp(name, filename[i])==0){
if(a->s = nntpget(net, a->g, a->n, nntpname[i])){
*qid = (Qid){PATH(a->g->num, a->n), Qbody, 0};
fid->qid = *qid;
a->file = i;
return nil;
}else
return "file does not exist";
}
}
return "file does not exist";
}
if(isdotdot){
a->g = a->g->parent;
*qid = (Qid){PATH(a->g->num, 0), 0, QTDIR};
fid->qid = *qid;
return nil;
}
if(a->g->isgroup && !readonly && a->g->canpost
&& strcmp(name, "post")==0){
a->ispost = 1;
*qid = (Qid){PATH(a->g->num, 0), 0, 0};
fid->qid = *qid;
return nil;
}
if(ng = findgroup(a->g, name, 0)){
a->g = ng;
*qid = (Qid){PATH(a->g->num, 0), 0, QTDIR};
fid->qid = *qid;
return nil;
}
n = strtoul(name, &p, 0);
if('0'<=name[0] && name[0]<='9' && *p=='\0' && a->g->lo<=n && n<a->g->hi){
a->n = n;
*qid = (Qid){PATH(a->g->num, n+1-a->g->lo), 0, QTDIR};
fid->qid = *qid;
return nil;
}
return "file does not exist";
}
static void
fsopen(Req *r)
{
Aux *a;
a = r->fid->aux;
if((a->ispost && (r->ifcall.mode&~OTRUNC) != OWRITE)
|| (!a->ispost && r->ifcall.mode != OREAD))
respond(r, "permission denied");
else
respond(r, nil);
}
static void
fillstat(Dir *d, Aux *a)
{
char buf[32];
Group *g;
memset(d, 0, sizeof *d);
d->uid = estrdup("nntp");
d->gid = estrdup("nntp");
g = a->g;
d->atime = d->mtime = g->mtime;
if(a->ispost){
d->name = estrdup("post");
d->mode = 0222;
d->qid = (Qid){PATH(g->num, 0), 0, 0};
d->length = a->ns;
return;
}
if(a->s){ /* article file */
d->name = estrdup(filename[a->file]);
d->mode = 0444;
d->qid = (Qid){PATH(g->num, a->n+1-g->lo), a->file, 0};
return;
}
if(a->n != -1){ /* article directory */
sprint(buf, "%d", a->n);
d->name = estrdup(buf);
d->mode = DMDIR|0555;
d->qid = (Qid){PATH(g->num, a->n+1-g->lo), 0, QTDIR};
return;
}
/* group directory */
if(g->name[0])
d->name = estrdup(g->name);
else
d->name = estrdup("/");
d->mode = DMDIR|0555;
d->qid = (Qid){PATH(g->num, 0), g->hi-1, QTDIR};
}
static int
dirfillstat(Dir *d, Aux *a, int i)
{
int ndir;
Group *g;
char buf[32];
memset(d, 0, sizeof *d);
d->uid = estrdup("nntp");
d->gid = estrdup("nntp");
g = a->g;
d->atime = d->mtime = g->mtime;
if(a->n != -1){ /* article directory */
if(i >= Nfile)
return -1;
d->name = estrdup(filename[i]);
d->mode = 0444;
d->qid = (Qid){PATH(g->num, a->n), i, 0};
return 0;
}
/* hierarchy directory: child groups */
if(i < g->nkid){
d->name = estrdup(g->kid[i]->name);
d->mode = DMDIR|0555;
d->qid = (Qid){PATH(g->kid[i]->num, 0), g->kid[i]->hi-1, QTDIR};
return 0;
}
i -= g->nkid;
/* group directory: post file */
if(g->isgroup && !readonly && g->canpost){
if(i < 1){
d->name = estrdup("post");
d->mode = 0222;
d->qid = (Qid){PATH(g->num, 0), 0, 0};
return 0;
}
i--;
}
/* group directory: child articles */
ndir = g->hi - g->lo;
if(i < ndir){
sprint(buf, "%d", g->lo+i);
d->name = estrdup(buf);
d->mode = DMDIR|0555;
d->qid = (Qid){PATH(g->num, i+1), 0, QTDIR};
return 0;
}
return -1;
}
static void
fsstat(Req *r)
{
Aux *a;
a = r->fid->aux;
if(r->fid->qid.path == 0 && (r->fid->qid.type & QTDIR))
nntprefreshall(net);
else if(a->g->isgroup)
nntprefresh(net, a->g);
fillstat(&r->d, a);
respond(r, nil);
}
static void
fsread(Req *r)
{
int offset, n;
Aux *a;
char *p, *ep;
Dir d;
a = r->fid->aux;
if(a->s){
readstr(r, a->s);
respond(r, nil);
return;
}
if(r->ifcall.offset == 0)
offset = 0;
else
offset = a->offset;
p = r->ofcall.data;
ep = r->ofcall.data+r->ifcall.count;
for(; p+2 < ep; p += n){
if(dirfillstat(&d, a, offset) < 0)
break;
n=convD2M(&d, (uchar*)p, ep-p);
free(d.name);
free(d.uid);
free(d.gid);
free(d.muid);
if(n <= BIT16SZ)
break;
offset++;
}
a->offset = offset;
r->ofcall.count = p - r->ofcall.data;
respond(r, nil);
}
static void
fswrite(Req *r)
{
Aux *a;
long count;
vlong offset;
a = r->fid->aux;
if(r->ifcall.count == 0){ /* commit */
respond(r, nntppost(net, a->s));
free(a->s);
a->ns = 0;
a->s = nil;
return;
}
count = r->ifcall.count;
offset = r->ifcall.offset;
if(a->ns < count+offset+1){
a->s = erealloc(a->s, count+offset+1);
a->ns = count+offset;
a->s[a->ns] = '\0';
}
memmove(a->s+offset, r->ifcall.data, count);
r->ofcall.count = count;
respond(r, nil);
}
static void
fsdestroyfid(Fid *fid)
{
Aux *a;
a = fid->aux;
if(a==nil)
return;
if(a->ispost && a->s)
nntppost(net, a->s);
free(a->s);
free(a);
}
Srv nntpsrv = {
.destroyfid= fsdestroyfid,
.attach= fsattach,
.clone= fsclone,
.walk1= fswalk1,
.open= fsopen,
.read= fsread,
.write= fswrite,
.stat= fsstat,
};
void
usage(void)
{
fprint(2, "usage: nntpsrv [-a] [-s service] [-m mtpt] [nntp.server]\n");
exits("usage");
}
void
dumpgroups(Group *g, int ind)
{
int i;
print("%*s%s\n", ind*4, "", g->name);
for(i=0; i<g->nkid; i++)
dumpgroups(g->kid[i], ind+1);
}
void
main(int argc, char **argv)
{
int auth, x;
char *mtpt, *service, *where, *user;
Netbuf n;
UserPasswd *up;
mtpt = "/mnt/news";
service = nil;
memset(&n, 0, sizeof n);
user = nil;
auth = 0;
ARGBEGIN{
case 'D':
chatty9p++;
break;
case 'N':
netdebug = 1;
break;
case 'a':
auth = 1;
break;
case 'u':
user = EARGF(usage());
break;
case 's':
service = EARGF(usage());
break;
case 'm':
mtpt = EARGF(usage());
break;
default:
usage();
}ARGEND
if(argc > 1)
usage();
if(argc==0)
where = "$nntp";
else
where = argv[0];
now = time(0);
net = &n;
if(auth) {
n.auth = 1;
if(user)
up = auth_getuserpasswd(auth_getkey, "proto=pass service=nntp server=%q user=%q", where, user);
else
up = auth_getuserpasswd(auth_getkey, "proto=pass service=nntp server=%q", where);
if(up == nil)
sysfatal("no password: %r");
n.user = up->user;
n.pass = up->passwd;
}
n.addr = netmkaddr(where, "tcp", "nntp");
root = emalloc(sizeof *root);
root->name = estrdup("");
root->parent = root;
n.fd = -1;
if(nntpconnect(&n) < 0)
sysfatal("nntpconnect: %s", n.response);
x=netdebug;
netdebug=0;
nntprefreshall(&n);
netdebug=x;
// dumpgroups(root, 0);
postmountsrv(&nntpsrv, service, mtpt, MREPL);
exits(nil);
}
|