Annotation of libwww/Library/src/HTNews.c, revision 2.23
1.1 timbl 1: /* NEWS ACCESS HTNews.c
2: ** ===========
3: **
4: ** History:
5: ** 26 Sep 90 Written TBL
6: ** 29 Nov 91 Downgraded to C, for portable implementation.
2.19 luotonen 7: ** 16 Feb 94 AL Added Lou Montulli's Lynx & LIST NEWSGROUPS diffs.
8: ** 2 May 94 AL Added HTUnEscape() to HTLoadNews(), and
9: ** fixed a possible security hole when the URL contains
10: ** a newline, that could cause multiple commands to be
11: ** sent to an NNTP server.
2.23 ! duns 12: ** 8 Jul 94 FM Insulate free() from _free structure element.
1.1 timbl 13: */
1.2 timbl 14: /* Implements:
15: */
16: #include "HTNews.h"
1.1 timbl 17:
1.3 timbl 18: #define CR FROMASCII('\015') /* Must be converted to ^M for transmission */
19: #define LF FROMASCII('\012') /* Must be converted to ^J for transmission */
20:
1.1 timbl 21: #define NEWS_PORT 119 /* See rfc977 */
22: #define APPEND /* Use append methods */
23: #define MAX_CHUNK 40 /* Largest number of articles in one window */
24: #define CHUNK_SIZE 20 /* Number of articles for quick display */
25:
26: #ifndef DEFAULT_NEWS_HOST
27: #define DEFAULT_NEWS_HOST "news"
28: #endif
29: #ifndef SERVER_FILE
30: #define SERVER_FILE "/usr/local/lib/rn/server"
31: #endif
32:
33: #include <ctype.h>
34: #include "HTUtils.h" /* Coding convention macros */
35: #include "tcp.h"
36:
1.2 timbl 37: #include "HTML.h"
1.1 timbl 38: #include "HTParse.h"
39: #include "HTFormat.h"
2.8 timbl 40: #include "HTAlert.h"
2.21 frystyk 41: #include "HTError.h"
1.1 timbl 42:
2.8 timbl 43: #define BIG 1024 /* @@@ */
44:
1.2 timbl 45: struct _HTStructured {
46: CONST HTStructuredClass * isa;
47: /* ... */
48: };
49:
2.7 timbl 50: #define NEWS_PROGRESS(foo) HTProgress(foo)
1.1 timbl 51:
52:
2.12 timbl 53: #define NEXT_CHAR HTInputSocket_getCharacter(isoc)
1.1 timbl 54: #define LINE_LENGTH 512 /* Maximum length of line of ARTICLE etc */
55: #define GROUP_NAME_LENGTH 256 /* Maximum length of group name */
56:
57:
58: /* Module-wide variables
59: */
1.2 timbl 60: PUBLIC char * HTNewsHost;
1.1 timbl 61: PRIVATE struct sockaddr_in soc_address; /* Binary network address */
62: PRIVATE int s; /* Socket for NewsHost */
63: PRIVATE char response_text[LINE_LENGTH+1]; /* Last response */
1.2 timbl 64: /* PRIVATE HText * HT; */ /* the new hypertext */
65: PRIVATE HTStructured * target; /* The output sink */
66: PRIVATE HTStructuredClass targetClass; /* Copy of fn addresses */
1.1 timbl 67: PRIVATE HTParentAnchor *node_anchor; /* Its anchor */
68: PRIVATE int diagnostic; /* level: 0=none 2=source */
69:
1.2 timbl 70:
71: #define PUTC(c) (*targetClass.put_character)(target, c)
72: #define PUTS(s) (*targetClass.put_string)(target, s)
73: #define START(e) (*targetClass.start_element)(target, e, 0, 0)
74: #define END(e) (*targetClass.end_element)(target, e)
75:
2.11 timbl 76: PUBLIC HTInputSocket *isoc; /* @@@ non-reentrant */
77:
1.2 timbl 78: PUBLIC CONST char * HTGetNewsHost NOARGS
79: {
80: return HTNewsHost;
81: }
1.1 timbl 82:
1.2 timbl 83: PUBLIC void HTSetNewsHost ARGS1(CONST char *, value)
84: {
85: StrAllocCopy(HTNewsHost, value);
86: }
1.1 timbl 87:
88: /* Initialisation for this module
89: ** ------------------------------
90: **
91: ** Except on the NeXT, we pick up the NewsHost name from
92: **
93: ** 1. Environment variable NNTPSERVER
94: ** 2. File SERVER_FILE
95: ** 3. Compilation time macro DEFAULT_NEWS_HOST
96: **
97: ** On the NeXT, we pick up the NewsHost name from, in order:
98: **
99: ** 1. WorldWideWeb default "NewsHost"
100: ** 2. Global default "NewsHost"
101: ** 3. News default "NewsHost"
102: ** 4. Compilation time macro DEFAULT_NEWS_HOST
103: */
104: PRIVATE BOOL initialized = NO;
105: PRIVATE BOOL initialize NOARGS
106: {
107: CONST struct hostent *phost; /* Pointer to host - See netdb.h */
108: struct sockaddr_in* sin = &soc_address;
109:
110:
111: /* Set up defaults:
112: */
113: sin->sin_family = AF_INET; /* Family = internet, host order */
114: sin->sin_port = htons(NEWS_PORT); /* Default: new port, */
115:
116: /* Get name of Host
117: */
118: #ifdef NeXTStep
1.2 timbl 119: if ((HTNewsHost = NXGetDefaultValue("WorldWideWeb","NewsHost"))==0)
120: if ((HTNewsHost = NXGetDefaultValue("News","NewsHost")) == 0)
121: HTNewsHost = DEFAULT_NEWS_HOST;
1.1 timbl 122: #else
123: if (getenv("NNTPSERVER")) {
1.2 timbl 124: StrAllocCopy(HTNewsHost, (char *)getenv("NNTPSERVER"));
1.1 timbl 125: if (TRACE) fprintf(stderr, "HTNews: NNTPSERVER defined as `%s'\n",
1.2 timbl 126: HTNewsHost);
1.1 timbl 127: } else {
128: char server_name[256];
129: FILE* fp = fopen(SERVER_FILE, "r");
130: if (fp) {
131: if (fscanf(fp, "%s", server_name)==1) {
1.2 timbl 132: StrAllocCopy(HTNewsHost, server_name);
1.1 timbl 133: if (TRACE) fprintf(stderr,
134: "HTNews: File %s defines news host as `%s'\n",
1.2 timbl 135: SERVER_FILE, HTNewsHost);
1.1 timbl 136: }
137: fclose(fp);
138: }
139: }
1.2 timbl 140: if (!HTNewsHost) HTNewsHost = DEFAULT_NEWS_HOST;
1.1 timbl 141: #endif
142:
1.2 timbl 143: if (*HTNewsHost>='0' && *HTNewsHost<='9') { /* Numeric node address: */
144: sin->sin_addr.s_addr = inet_addr((char *)HTNewsHost); /* See arpa/inet.h */
1.1 timbl 145:
146: } else { /* Alphanumeric node name: */
1.2 timbl 147: phost=gethostbyname((char*)HTNewsHost); /* See netdb.h */
1.1 timbl 148: if (!phost) {
2.7 timbl 149: char message[150]; /* @@@ */
150: sprintf(message,
151: "HTNews: Can't find news host `%s'.\n%s",HTNewsHost,
152: "Please define your NNTP server");
153: HTAlert(message);
1.1 timbl 154: CTRACE(tfp,
1.2 timbl 155: "HTNews: Can't find news host `%s'.\n",HTNewsHost);
1.1 timbl 156: return NO; /* Fail */
157: }
158: memcpy(&sin->sin_addr, phost->h_addr, phost->h_length);
159: }
160:
161: if (TRACE) fprintf(stderr,
162: "HTNews: Parsed address as port %4x, inet %d.%d.%d.%d\n",
163: (unsigned int)ntohs(sin->sin_port),
164: (int)*((unsigned char *)(&sin->sin_addr)+0),
165: (int)*((unsigned char *)(&sin->sin_addr)+1),
166: (int)*((unsigned char *)(&sin->sin_addr)+2),
167: (int)*((unsigned char *)(&sin->sin_addr)+3));
168:
169: s = -1; /* Disconnected */
170:
171: return YES;
172: }
173:
174:
175:
176: /* Send NNTP Command line to remote host & Check Response
177: ** ------------------------------------------------------
178: **
179: ** On entry,
180: ** command points to the command to be sent, including CRLF, or is null
181: ** pointer if no command to be sent.
182: ** On exit,
183: ** Negative status indicates transmission error, socket closed.
184: ** Positive status is an NNTP status.
185: */
186:
187:
188: PRIVATE int response ARGS1(CONST char *,command)
189: {
190: int result;
191: char * p = response_text;
192: if (command) {
193: int status;
194: int length = strlen(command);
195: if (TRACE) fprintf(stderr, "NNTP command to be sent: %s", command);
196: #ifdef NOT_ASCII
197: {
198: CONST char * p;
199: char * q;
200: char ascii[LINE_LENGTH+1];
201: for(p = command, q=ascii; *p; p++, q++) {
202: *q = TOASCII(*p);
203: }
204: status = NETWRITE(s, ascii, length);
205: }
206: #else
207: status = NETWRITE(s, command, length);
208: #endif
209: if (status<0){
210: if (TRACE) fprintf(stderr,
211: "HTNews: Unable to send command. Disconnecting.\n");
212: NETCLOSE(s);
2.11 timbl 213: HTInputSocket_free(isoc);
1.1 timbl 214: s = -1;
215: return status;
216: } /* if bad status */
217: } /* if command to be sent */
218:
219: for(;;) {
1.3 timbl 220: if (((*p++=NEXT_CHAR) == LF)
221: || (p == &response_text[LINE_LENGTH])) {
1.1 timbl 222: *p++=0; /* Terminate the string */
223: if (TRACE) fprintf(stderr, "NNTP Response: %s\n", response_text);
224: sscanf(response_text, "%d", &result);
2.19 luotonen 225: if (result >= 411 && result <= 430) { /* no such article/group */
226: char * msg = strchr(response_text,' ');
227: if (!msg) msg = response_text;
228: PUTS("<H1>News error</H1>\n");
229: PUTS(msg);
230: CTRACE(stderr, "News error.. %s", response_text);
231: }
1.1 timbl 232: return result;
233: } /* if end of line */
234:
235: if (*(p-1) < 0) {
236: if (TRACE) fprintf(stderr,
237: "HTNews: EOF on read, closing socket %d\n", s);
238: NETCLOSE(s); /* End of file, close socket */
2.11 timbl 239: HTInputSocket_free(isoc);
1.1 timbl 240: return s = -1; /* End of file on response */
241: }
242: } /* Loop over characters */
243: }
244:
245:
246: /* Case insensitive string comparisons
247: ** -----------------------------------
248: **
249: ** On entry,
250: ** template must be already un upper case.
251: ** unknown may be in upper or lower or mixed case to match.
252: */
253: PRIVATE BOOL match ARGS2 (CONST char *,unknown, CONST char *,template)
254: {
255: CONST char * u = unknown;
256: CONST char * t = template;
257: for (;*u && *t && (TOUPPER(*u)==*t); u++, t++) /* Find mismatch or end */ ;
258: return (BOOL)(*t==0); /* OK if end of template */
259: }
260:
261: /* Find Author's name in mail address
262: ** ----------------------------------
263: **
264: ** On exit,
265: ** THE EMAIL ADDRESS IS CORRUPTED
266: **
267: ** For example, returns "Tim Berners-Lee" if given any of
268: ** " Tim Berners-Lee <tim@online.cern.ch> "
269: ** or " tim@online.cern.ch ( Tim Berners-Lee ) "
270: */
271: PRIVATE char * author_name ARGS1 (char *,email)
272: {
273: char *s, *e;
274:
275: if ((s=strchr(email,'(')) && (e=strchr(email, ')')))
276: if (e>s) {
277: *e=0; /* Chop off everything after the ')' */
278: return HTStrip(s+1); /* Remove leading and trailing spaces */
279: }
280:
281: if ((s=strchr(email,'<')) && (e=strchr(email, '>')))
282: if (e>s) {
283: strcpy(s, e+1); /* Remove <...> */
284: return HTStrip(email); /* Remove leading and trailing spaces */
285: }
286:
287: return HTStrip(email); /* Default to the whole thing */
288:
289: }
290:
1.2 timbl 291: /* Start anchor element
292: ** --------------------
293: */
294: PRIVATE void start_anchor ARGS1(CONST char *, href)
295: {
296: BOOL present[HTML_A_ATTRIBUTES];
297: CONST char* value[HTML_A_ATTRIBUTES];
298:
299: {
300: int i;
301: for(i=0; i<HTML_A_ATTRIBUTES; i++)
302: present[i] = (i==HTML_A_HREF);
303: }
304: value[HTML_A_HREF] = href;
305: (*targetClass.start_element)(target, HTML_A , present, value);
306:
307: }
1.1 timbl 308:
2.16 luotonen 309:
310: /* Start link element
311: ** --------------------
312: */
313: PRIVATE void start_link ARGS2(CONST char *, href, CONST char *, rev)
314: {
315: #ifdef WHEN_WE_HAVE_HTMLPLUS
316:
317: BOOL present[HTML_LINK_ATTRIBUTES];
318: CONST char* value[HTML_LINK_ATTRIBUTES];
319:
320: {
321: int i;
322: for(i=0; i<HTML_LINK_ATTRIBUTES; i++)
323: present[i] = (i==HTML_LINK_HREF || i==HTML_LINK_REV);
324: }
325: value[HTML_LINK_HREF] = href;
326: value[HTML_LINK_REV] = rev;
327: (*targetClass.start_element)(target, HTML_LINK , present, value);
328:
329: #endif
330: }
331:
332:
333:
334:
1.1 timbl 335: /* Paste in an Anchor
336: ** ------------------
337: **
338: **
339: ** On entry,
340: ** HT has a selection of zero length at the end.
341: ** text points to the text to be put into the file, 0 terminated.
342: ** addr points to the hypertext refernce address,
343: ** terminated by white space, comma, NULL or '>'
344: */
345: PRIVATE void write_anchor ARGS2(CONST char *,text, CONST char *,addr)
346: {
347: char href[LINE_LENGTH+1];
348:
349: {
350: CONST char * p;
351: strcpy(href,"news:");
352: for(p=addr; *p && (*p!='>') && !WHITE(*p) && (*p!=','); p++);
353: strncat(href, addr, p-addr); /* Make complete hypertext reference */
354: }
355:
1.2 timbl 356: start_anchor(href);
357: PUTS(text);
358: END(HTML_A);
1.1 timbl 359: }
360:
361:
362: /* Write list of anchors
363: ** ---------------------
364: **
365: ** We take a pointer to a list of objects, and write out each,
366: ** generating an anchor for each.
367: **
368: ** On entry,
369: ** HT has a selection of zero length at the end.
370: ** text points to a comma or space separated list of addresses.
371: ** On exit,
372: ** *text is NOT any more chopped up into substrings.
373: */
374: PRIVATE void write_anchors ARGS1 (char *,text)
375: {
376: char * start = text;
377: char * end;
378: char c;
379: for (;;) {
380: for(;*start && (WHITE(*start)); start++); /* Find start */
381: if (!*start) return; /* (Done) */
382: for(end=start; *end && (*end!=' ') && (*end!=','); end++);/* Find end */
383: if (*end) end++; /* Include comma or space but not NULL */
384: c = *end;
385: *end = 0;
386: write_anchor(start, start);
2.16 luotonen 387: START(HTML_BR);
1.1 timbl 388: *end = c;
389: start = end; /* Point to next one */
390: }
391: }
392:
393: /* Abort the connection abort_socket
394: ** --------------------
395: */
396: PRIVATE void abort_socket NOARGS
397: {
398: if (TRACE) fprintf(stderr,
399: "HTNews: EOF on read, closing socket %d\n", s);
400: NETCLOSE(s); /* End of file, close socket */
2.11 timbl 401: HTInputSocket_free(isoc);
1.2 timbl 402: PUTS("Network Error: connection lost");
403: PUTC('\n');
1.1 timbl 404: s = -1; /* End of file on response */
405: return;
406: }
407:
408: /* Read in an Article read_article
409: ** ------------------
410: **
411: **
412: ** Note the termination condition of a single dot on a line by itself.
413: ** RFC 977 specifies that the line "folding" of RFC850 is not used, so we
414: ** do not handle it here.
415: **
416: ** On entry,
417: ** s Global socket number is OK
418: ** HT Global hypertext object is ready for appending text
419: */
420: PRIVATE void read_article NOARGS
421: {
422:
423: char line[LINE_LENGTH+1];
424: char *references=NULL; /* Hrefs for other articles */
425: char *newsgroups=NULL; /* Newsgroups list */
426: char *p = line;
427: BOOL done = NO;
428:
429: /* Read in the HEADer of the article:
430: **
431: ** The header fields are either ignored, or formatted and put into the
432: ** Text.
433: */
434: if (!diagnostic) {
1.2 timbl 435: (*targetClass.start_element)(target, HTML_ADDRESS, 0, 0);
1.1 timbl 436: while(!done){
437: char ch = *p++ = NEXT_CHAR;
438: if (ch==(char)EOF) {
439: abort_socket(); /* End of file, close socket */
440: return; /* End of file on response */
441: }
1.3 timbl 442: if ((ch == LF) || (p == &line[LINE_LENGTH])) {
1.1 timbl 443: *--p=0; /* Terminate the string */
444: if (TRACE) fprintf(stderr, "H %s\n", line);
445:
446: if (line[0]=='.') {
447: if (line[1]<' ') { /* End of article? */
448: done = YES;
449: break;
450: }
451:
452: } else if (line[0]<' ') {
453: break; /* End of Header? */
2.16 luotonen 454:
1.1 timbl 455: } else if (match(line, "SUBJECT:")) {
1.2 timbl 456: END(HTML_ADDRESS);
457: START(HTML_TITLE); /** Uuugh! @@@ */
2.16 luotonen 458: PUTS(line+9);
459: END(HTML_TITLE);
460: START(HTML_H1);
1.2 timbl 461: PUTS(line+8);
2.16 luotonen 462: END(HTML_H1);
1.2 timbl 463: START(HTML_ADDRESS);
2.16 luotonen 464:
1.1 timbl 465: } else if (match(line, "DATE:")
466: || match(line, "ORGANIZATION:")) {
2.16 luotonen 467: PUTS(strchr(line,':')+2);
468: START(HTML_BR);
469:
470: } else if(match(line, "FROM:")) {
471: char * temp=0;
472: char * href=0;
473: char *cp1, *cp2;
474:
475: /* copy into temporary storage */
476: StrAllocCopy(temp, strchr(line,':')+1);
477:
478: cp1=temp;
479: while(isspace(*cp1)) cp1++;
480: /* remove space and stuff after */
481: if((cp2 = strchr(cp1,' ')) != NULL)
482: *cp2 = '\0';
483:
484: StrAllocCopy(href,"mailto:");
485: StrAllocCat(href,cp1);
486:
487: start_anchor(href);
488: PUTS("Reply to ");
489: PUTS(strchr(line,':')+1);
490: END(HTML_A);
491: START(HTML_BR);
492:
493: /* put in the owner as a link rel. as well */
494: start_link(href, "made");
495:
496: /* free of temp vars */
497: free(temp);
498: free(href);
499:
1.1 timbl 500: } else if (match(line, "NEWSGROUPS:")) {
501: StrAllocCopy(newsgroups, HTStrip(strchr(line,':')+1));
502:
503: } else if (match(line, "REFERENCES:")) {
504: StrAllocCopy(references, HTStrip(strchr(line,':')+1));
505:
506: } /* end if match */
507: p = line; /* Restart at beginning */
508: } /* if end of line */
509: } /* Loop over characters */
2.16 luotonen 510: END(HTML_ADDRESS);
1.1 timbl 511:
1.2 timbl 512: if (newsgroups || references) {
2.16 luotonen 513: START(HTML_DL);
1.2 timbl 514: if (newsgroups) {
2.16 luotonen 515: #ifdef POSTING
516: char *href=0;
517: #endif
518:
1.2 timbl 519: (*targetClass.start_element)(target, HTML_DT , 0, 0);
520: PUTS("Newsgroups:");
521: (*targetClass.start_element)(target, HTML_DD , 0, 0);
522: write_anchors(newsgroups);
2.16 luotonen 523:
524: #ifdef POSTING
525: /* make posting possible */
526: StrAllocCopy(href,"newspost:");
527: StrAllocCat(href,newsgroups);
528: START(HTML_DT);
529: start_anchor(href);
530: PUTS("Reply to newsgroup(s)");
531: END(HTML_A);
532: #endif
533:
1.2 timbl 534: free(newsgroups);
535: }
536:
537: if (references) {
538: (*targetClass.start_element)(target, HTML_DT , 0, 0);
539: PUTS("References:");
540: (*targetClass.start_element)(target, HTML_DD , 0, 0);
541: write_anchors(references);
542: free(references);
543: }
2.16 luotonen 544: #ifdef WHEN_WE_HAVE_HTMLPLUS
545: (*targetClass.end_element)(target, HTML_DLC);
546: #else
2.10 timbl 547: (*targetClass.end_element)(target, HTML_DL);
2.16 luotonen 548: #endif
1.1 timbl 549: }
1.2 timbl 550: PUTS("\n\n\n");
1.1 timbl 551:
552: }
553:
554: /* Read in the BODY of the Article:
555: */
1.2 timbl 556: (*targetClass.start_element)(target, HTML_PRE , 0, 0);
557:
1.1 timbl 558: p = line;
559: while(!done){
560: char ch = *p++ = NEXT_CHAR;
561: if (ch==(char)EOF) {
562: abort_socket(); /* End of file, close socket */
563: return; /* End of file on response */
564: }
1.3 timbl 565: if ((ch == LF) || (p == &line[LINE_LENGTH])) {
1.1 timbl 566: *p++=0; /* Terminate the string */
567: if (TRACE) fprintf(stderr, "B %s", line);
568: if (line[0]=='.') {
569: if (line[1]<' ') { /* End of article? */
570: done = YES;
571: break;
572: } else { /* Line starts with dot */
1.2 timbl 573: PUTS(&line[1]); /* Ignore first dot */
1.1 timbl 574: }
575: } else {
576:
577: /* Normal lines are scanned for buried references to other articles.
578: ** Unfortunately, it will pick up mail addresses as well!
579: */
580: char *l = line;
581: char * p;
2.14 luotonen 582: while ((p=strchr(l, '<'))) {
1.1 timbl 583: char *q = strchr(p,'>');
584: char *at = strchr(p, '@');
585: if (q && at && at<q) {
586: char c = q[1];
587: q[1] = 0; /* chop up */
588: *p = 0;
1.2 timbl 589: PUTS(l);
1.1 timbl 590: *p = '<'; /* again */
591: *q = 0;
1.2 timbl 592: start_anchor(p+1);
1.1 timbl 593: *q = '>'; /* again */
1.2 timbl 594: PUTS(p);
595: (*targetClass.end_element)(target, HTML_A);
1.1 timbl 596: q[1] = c; /* again */
597: l=q+1;
598: } else break; /* line has unmatched <> */
599: }
1.2 timbl 600: PUTS( l); /* Last bit of the line */
1.1 timbl 601: } /* if not dot */
602: p = line; /* Restart at beginning */
603: } /* if end of line */
604: } /* Loop over characters */
1.2 timbl 605:
606: (*targetClass.end_element)(target, HTML_PRE);
1.1 timbl 607: }
608:
609:
610: /* Read in a List of Newsgroups
611: ** ----------------------------
612: */
613: /*
614: ** Note the termination condition of a single dot on a line by itself.
615: ** RFC 977 specifies that the line "folding" of RFC850 is not used, so we
616: ** do not handle it here.
617: */
618: PRIVATE void read_list NOARGS
619: {
620:
621: char line[LINE_LENGTH+1];
622: char *p;
623: BOOL done = NO;
624:
625: /* Read in the HEADer of the article:
626: **
627: ** The header fields are either ignored, or formatted and put into the
628: ** Text.
629: */
1.2 timbl 630: (*targetClass.start_element)(target, HTML_H1 , 0, 0);
631: PUTS( "Newsgroups");
632: (*targetClass.end_element)(target, HTML_PRE);
1.1 timbl 633: p = line;
2.16 luotonen 634: (*targetClass.start_element)(target, HTML_DL, 0, 0);
1.1 timbl 635: while(!done){
636: char ch = *p++ = NEXT_CHAR;
637: if (ch==(char)EOF) {
638: abort_socket(); /* End of file, close socket */
639: return; /* End of file on response */
640: }
1.3 timbl 641: if ((ch == LF) || (p == &line[LINE_LENGTH])) {
1.1 timbl 642: *p++=0; /* Terminate the string */
643: if (TRACE) fprintf(stderr, "B %s", line);
2.16 luotonen 644: (*targetClass.start_element)(target, HTML_DT , 0, 0);
1.1 timbl 645: if (line[0]=='.') {
646: if (line[1]<' ') { /* End of article? */
647: done = YES;
648: break;
649: } else { /* Line starts with dot */
1.2 timbl 650: PUTS( &line[1]);
1.1 timbl 651: }
652: } else {
653:
654: /* Normal lines are scanned for references to newsgroups.
655: */
2.16 luotonen 656: int i=0;
657:
658: /* find whitespace if it exits */
659: for(; line[i] != '\0' && !WHITE(line[i]); i++)
660: ; /* null body */
661:
662: if(line[i] != '\0') {
663: line[i] = '\0';
664: write_anchor(line, line);
665: (*targetClass.start_element)(target, HTML_DD , 0, 0);
666: PUTS(&line[i+1]); /* put description */
667: } else {
668: write_anchor(line, line);
669: }
670:
671: #ifdef OLD_CODE
1.1 timbl 672: char group[LINE_LENGTH];
673: int first, last;
674: char postable;
675: if (sscanf(line, "%s %d %d %c", group, &first, &last, &postable)==4)
676: write_anchor(line, group);
677: else
1.2 timbl 678: PUTS(line);
2.16 luotonen 679: #endif /*OLD_CODE*/
680:
1.1 timbl 681: } /* if not dot */
682: p = line; /* Restart at beginning */
683: } /* if end of line */
684: } /* Loop over characters */
2.16 luotonen 685: (*targetClass.end_element)(target, HTML_DL);
1.1 timbl 686: }
687:
688:
689: /* Read in a Newsgroup
690: ** -------------------
691: ** Unfortunately, we have to ask for each article one by one if we
692: ** want more than one field.
693: **
694: */
695: PRIVATE void read_group ARGS3(
696: CONST char *,groupName,
697: int,first_required,
698: int,last_required
699: )
700: {
701: char line[LINE_LENGTH+1];
702: char author[LINE_LENGTH+1];
703: char subject[LINE_LENGTH+1];
704: char *p;
705: BOOL done;
706:
707: char buffer[LINE_LENGTH];
708: char *reference=0; /* Href for article */
709: int art; /* Article number WITHIN GROUP */
710: int status, count, first, last; /* Response fields */
711: /* count is only an upper limit */
712:
713: sscanf(response_text, " %d %d %d %d", &status, &count, &first, &last);
2.17 frystyk 714: if(TRACE)
715: fprintf(stderr,
716: "Newsgroup status=%d, count=%d, (%d-%d) required:(%d-%d)\n",
717: status, count, first, last, first_required, last_required);
1.1 timbl 718: if (last==0) {
1.2 timbl 719: PUTS( "\nNo articles in this group.\n");
2.16 luotonen 720: #ifdef POSTING
721: goto add_post;
722: #endif
1.1 timbl 723: return;
724: }
725:
726: #define FAST_THRESHOLD 100 /* Above this, read IDs fast */
727: #define CHOP_THRESHOLD 50 /* Above this, chop off the rest */
728:
729: if (first_required<first) first_required = first; /* clip */
730: if ((last_required==0) || (last_required > last)) last_required = last;
731:
732: if (last_required<=first_required) {
1.2 timbl 733: PUTS( "\nNo articles in this range.\n");
2.16 luotonen 734: #ifdef POSTING
735: goto add_post;
736: #endif
1.1 timbl 737: return;
738: }
739:
740: if (last_required-first_required+1 > MAX_CHUNK) { /* Trim this block */
741: first_required = last_required-CHUNK_SIZE+1;
742: }
2.18 frystyk 743: if (TRACE) fprintf (stderr, " Chunk will be (%d-%d)\n",
2.16 luotonen 744: first_required, last_required);
1.1 timbl 745:
1.2 timbl 746: /* Set window title
747: */
748: sprintf(buffer, "Newsgroup %s, Articles %d-%d",
749: groupName, first_required, last_required);
750: START(HTML_TITLE);
751: PUTS(buffer);
752: END(HTML_TITLE);
753:
1.1 timbl 754: /* Link to earlier articles
755: */
756: if (first_required>first) {
757: int before; /* Start of one before */
758: if (first_required-MAX_CHUNK <= first) before = first;
759: else before = first_required-CHUNK_SIZE;
760: sprintf(buffer, "%s/%d-%d", groupName, before, first_required-1);
761: if (TRACE) fprintf(stderr, " Block before is %s\n", buffer);
1.2 timbl 762: PUTS( " (");
763: start_anchor(buffer);
764: PUTS("Earlier articles");
765: END(HTML_A);
766: PUTS( "...)\n");
1.1 timbl 767: }
768:
769: done = NO;
770:
771: /*#define USE_XHDR*/
772: #ifdef USE_XHDR
773: if (count>FAST_THRESHOLD) {
774: sprintf(buffer,
775: "\nThere are about %d articles currently available in %s, IDs as follows:\n\n",
776: count, groupName);
1.2 timbl 777: PUTS(buffer);
1.3 timbl 778: sprintf(buffer, "XHDR Message-ID %d-%d%c%c", first, last, CR, LF);
1.1 timbl 779: status = response(buffer);
780: if (status==221) {
781:
782: p = line;
783: while(!done){
784: char ch = *p++ = NEXT_CHAR;
785: if (ch==(char)EOF) {
786: abort_socket(); /* End of file, close socket */
787: return; /* End of file on response */
788: }
789: if ((ch == '\n') || (p == &line[LINE_LENGTH])) {
790: *p++=0; /* Terminate the string */
791: if (TRACE) fprintf(stderr, "X %s", line);
792: if (line[0]=='.') {
793: if (line[1]<' ') { /* End of article? */
794: done = YES;
795: break;
796: } else { /* Line starts with dot */
797: /* Ignore strange line */
798: }
799: } else {
800:
801: /* Normal lines are scanned for references to articles.
802: */
803: char * space = strchr(line, ' ');
804: if (space++)
805: write_anchor(space, space);
806: } /* if not dot */
807: p = line; /* Restart at beginning */
808: } /* if end of line */
809: } /* Loop over characters */
810:
811: /* leaving loop with "done" set */
812: } /* Good status */
813: };
814: #endif
815:
816: /* Read newsgroup using individual fields:
817: */
818: if (!done) {
819: if (first==first_required && last==last_required)
1.2 timbl 820: PUTS("\nAll available articles in ");
821: else PUTS( "\nArticles in ");
822: PUTS(groupName);
823: START(HTML_MENU);
1.1 timbl 824: for(art=first_required; art<=last_required; art++) {
825:
826: /*#define OVERLAP*/
827: #ifdef OVERLAP
828: /* With this code we try to keep the server running flat out by queuing just
829: ** one extra command ahead of time. We assume (1) that the server won't abort
830: ** if it gets input during output, and (2) that TCP buffering is enough for the
831: ** two commands. Both these assumptions seem very reasonable. However, we HAVE
832: ** had a hangup with a loaded server.
833: */
834: if (art==first_required) {
835: if (art==last_required) {
1.3 timbl 836: sprintf(buffer, "HEAD %d%c%c", art, CR, LF); /* Only one */
1.1 timbl 837: status = response(buffer);
838: } else { /* First of many */
1.3 timbl 839: sprintf(buffer, "HEAD %d%c%cHEAD %d%c%c",
840: art, CR, LF, art+1, CR, LF);
1.1 timbl 841: status = response(buffer);
842: }
843: } else if (art==last_required) { /* Last of many */
844: status = response(NULL);
845: } else { /* Middle of many */
1.3 timbl 846: sprintf(buffer, "HEAD %d%c%c", art+1, CR, LF);
1.1 timbl 847: status = response(buffer);
848: }
849:
850: #else /* NOT OVERLAP */
1.3 timbl 851: sprintf(buffer, "HEAD %d%c%c", art, CR, LF);
1.1 timbl 852: status = response(buffer);
853: #endif /* NOT OVERLAP */
854:
855: if (status == 221) { /* Head follows - parse it:*/
2.20 frystyk 856: int ch;
1.1 timbl 857: p = line; /* Write pointer */
858: done = NO;
859: while(!done){
2.20 frystyk 860: if ((ch = HTInputSocket_getCharacter(isoc)) < 0) {
1.1 timbl 861: abort_socket(); /* End of file, close socket */
862: return; /* End of file on response */
863: }
2.20 frystyk 864: *p++ = (unsigned char) ch;
1.3 timbl 865: if ((ch == LF)
1.1 timbl 866: || (p == &line[LINE_LENGTH]) ) {
867:
868: *--p=0; /* Terminate & chop LF*/
869: p = line; /* Restart at beginning */
870: if (TRACE) fprintf(stderr, "G %s\n", line);
871: switch(line[0]) {
872:
873: case '.':
874: done = (line[1]<' '); /* End of article? */
875: break;
876:
877: case 'S':
878: case 's':
879: if (match(line, "SUBJECT:"))
880: strcpy(subject, line+9);/* Save subject */
881: break;
882:
883: case 'M':
884: case 'm':
885: if (match(line, "MESSAGE-ID:")) {
886: char * addr = HTStrip(line+11) +1; /* Chop < */
887: addr[strlen(addr)-1]=0; /* Chop > */
888: StrAllocCopy(reference, addr);
889: }
890: break;
891:
892: case 'f':
893: case 'F':
894: if (match(line, "FROM:")) {
895: char * p;
896: strcpy(author,
897: author_name(strchr(line,':')+1));
2.17 frystyk 898: if (*author) { /* Not always there! */
899: p = author + strlen(author) - 1;
900: if (*p==LF) *p = 0; /* Chop off newline */
901: }
1.1 timbl 902: }
903: break;
904:
905: } /* end switch on first character */
906: } /* if end of line */
907: } /* Loop over characters */
908:
1.2 timbl 909: START(HTML_LI);
1.1 timbl 910: sprintf(buffer, "\"%s\" - %s", subject, author);
911: if (reference) {
912: write_anchor(buffer, reference);
913: free(reference);
914: reference=0;
915: } else {
1.2 timbl 916: PUTS(buffer);
1.1 timbl 917: }
918:
919:
1.2 timbl 920: /* indicate progress! @@@@@@
1.1 timbl 921: */
922:
923: } /* If good response */
924: } /* Loop over article */
925: } /* If read headers */
1.2 timbl 926: END(HTML_MENU);
927: START(HTML_P);
1.1 timbl 928:
929: /* Link to later articles
930: */
931: if (last_required<last) {
932: int after; /* End of article after */
933: after = last_required+CHUNK_SIZE;
934: if (after==last) sprintf(buffer, "news:%s", groupName); /* original group */
935: else sprintf(buffer, "news:%s/%d-%d", groupName, last_required+1, after);
936: if (TRACE) fprintf(stderr, " Block after is %s\n", buffer);
1.2 timbl 937: PUTS( "(");
938: start_anchor(buffer);
939: PUTS( "Later articles");
940: END(HTML_A);
941: PUTS( "...)\n");
1.1 timbl 942: }
2.16 luotonen 943:
2.19 luotonen 944: #ifdef POSTING
945: add_post:
946: #endif
2.16 luotonen 947: {
948: char *href=0;
949: START(HTML_HR);
950:
951: StrAllocCopy(href,"newspost:");
952: StrAllocCat(href,groupName);
953: start_anchor(href);
954: PUTS("Post to ");
955: PUTS(groupName);
956: END(HTML_A);
957:
958: free(href);
959: }
1.1 timbl 960:
961:
962: }
963:
964:
965: /* Load by name HTLoadNews
966: ** ============
967: */
2.13 timbl 968: PUBLIC int HTLoadNews ARGS1(HTRequest *, request)
1.1 timbl 969: {
2.19 luotonen 970: char * arg = HTAnchor_physical(request->anchor);
1.1 timbl 971: char command[257]; /* The whole command */
972: char groupName[GROUP_NAME_LENGTH]; /* Just the group name */
973: int status; /* tcp return */
974: int retries; /* A count of how hard we have tried */
975: BOOL group_wanted; /* Flag: group was asked for, not article */
976: BOOL list_wanted; /* Flag: group was asked for, not article */
977: int first, last; /* First and last articles asked for */
978:
2.10 timbl 979: diagnostic = (request->output_format == WWW_SOURCE); /* set global flag */
1.1 timbl 980:
981: if (TRACE) fprintf(stderr, "HTNews: Looking for %s\n", arg);
982:
983: if (!initialized) initialized = initialize();
984: if (!initialized) return -1; /* FAIL */
985:
986: {
2.19 luotonen 987: char * p1=arg;
1.1 timbl 988:
989: /* We will ask for the document, omitting the host name & anchor.
990: **
991: ** Syntax of address is
992: ** xxx@yyy Article
993: ** <xxx@yyy> Same article
994: ** xxxxx News group (no "@")
995: ** group/n1-n2 Articles n1 to n2 in group
996: */
997: group_wanted = (strchr(arg, '@')==0) && (strchr(arg, '*')==0);
998: list_wanted = (strchr(arg, '@')==0) && (strchr(arg, '*')!=0);
999:
1000: /* p1 = HTParse(arg, "", PARSE_PATH | PARSE_PUNCTUATION); */
1001: /* Don't use HTParse because news: access doesn't follow traditional
1002: rules. For instance, if the article reference contains a '#',
1003: the rest of it is lost -- JFG 10/7/92, from a bug report */
1004: if (!strncasecomp (arg, "news:", 5))
1005: p1 = arg + 5; /* Skip "news:" prefix */
2.19 luotonen 1006: HTUnEscape(p1); /* AL May 2, 1994 */
1007: HTCleanTelnetString(p1); /* Prevent security holes */
1.1 timbl 1008: if (list_wanted) {
2.16 luotonen 1009: strcpy(command, "LIST NEWSGROUPS");
1.1 timbl 1010: } else if (group_wanted) {
1011: char * slash = strchr(p1, '/');
1012: strcpy(command, "GROUP ");
1013: first = 0;
1014: last = 0;
1015: if (slash) {
1016: *slash = 0;
1017: strcpy(groupName, p1);
1018: *slash = '/';
1019: (void) sscanf(slash+1, "%d-%d", &first, &last);
1020: } else {
1021: strcpy(groupName, p1);
1022: }
1023: strcat(command, groupName);
1024: } else {
1025: strcpy(command, "ARTICLE ");
1026: if (strchr(p1, '<')==0) strcat(command,"<");
1027: strcat(command, p1);
1028: if (strchr(p1, '>')==0) strcat(command,">");
1029: }
1030:
1.3 timbl 1031: {
1032: char * p = command + strlen(command);
1033: *p++ = CR; /* Macros to be correct on Mac */
1034: *p++ = LF;
1035: *p++ = 0;
1036: /* strcat(command, "\r\n"); */ /* CR LF, as in rfc 977 */
1037: }
1.1 timbl 1038: } /* scope of p1 */
1039:
1040: if (!*arg) return NO; /* Ignore if no name */
1041:
1042:
1043: /* Make a hypertext object with an anchor list.
1044: */
2.10 timbl 1045: node_anchor = request->anchor;
2.11 timbl 1046: target = HTML_new(request, NULL, WWW_HTML,
1047: request->output_format, request->output_stream);
1.2 timbl 1048: targetClass = *target->isa; /* Copy routine entry points */
1049:
1.1 timbl 1050:
1051: /* Now, let's get a stream setup up from the NewsHost:
1052: */
1053: for(retries=0;retries<2; retries++){
1054:
1055: if (s<0) {
1056: NEWS_PROGRESS("Connecting to NewsHost ...");
1057: s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1058: status = connect(s, (struct sockaddr*)&soc_address, sizeof(soc_address));
1059: if (status<0){
1060: NETCLOSE(s);
1061: s = -1;
2.21 frystyk 1062: #ifdef OLD_CODE
1063: char message[256];
1.1 timbl 1064: if (TRACE) fprintf(stderr, "HTNews: Unable to connect to news host.\n");
1065: /* if (retries<=1) continue; WHY TRY AGAIN ? */
1066: sprintf(message,
1067: "\nCould not access %s.\n\n (Check default WorldWideWeb NewsHost ?)\n",
1.2 timbl 1068: HTNewsHost);
2.15 luotonen 1069: return HTLoadError(request, 500, message);
2.21 frystyk 1070: #endif /* OLD_CODE */
1071: {
1072: char *unescaped = NULL;
1073: StrAllocCopy(unescaped, arg);
1074: HTUnEscape(unescaped);
1075: HTErrorAdd(request, ERR_FATAL, NO, HTERR_INTERNAL,
1076: (void *) unescaped,
1077: (int) strlen(unescaped), "HTLoadNews");
1078: free(unescaped);
1079: return -1;
1080: }
1.1 timbl 1081: } else {
1082: if (TRACE) fprintf(stderr, "HTNews: Connected to news host %s.\n",
1.2 timbl 1083: HTNewsHost);
2.11 timbl 1084: isoc = HTInputSocket_new(s); /* set up buffering */
1.1 timbl 1085: if ((response(NULL) / 100) !=2) {
2.21 frystyk 1086: int length = strlen(response_text);
1087: NETCLOSE(s);
1088: HTInputSocket_free(isoc);
1089: s = -1;
1090: HTErrorAdd(request, ERR_FATAL, NO, HTERR_NEWS_SERVER,
1091: (void *) response_text, length < 50 ?
1092: length : 50, "HTLoadNews");
1093: HTErrorAdd(request, ERR_FATAL, NO, HTERR_INTERNAL,
1094: (void *) HTNewsHost,
1095: (int) strlen(HTNewsHost), "HTLoadNews");
1096: return -1;
1097: }
1098: #ifdef OLD_CODE
2.8 timbl 1099: char message[BIG];
1100: sprintf(message,
1101: "Can't read news info. News host %.20s responded: %.200s",
1102: HTNewsHost, response_text);
2.15 luotonen 1103: return HTLoadError(request, 500, message);
2.21 frystyk 1104: }
1105: #endif /* OLD_CODE */
1.1 timbl 1106: }
1107: } /* If needed opening */
1108:
1.2 timbl 1109: /* @@@@@@@@@@@@@@Tell user something's happening */
1110:
1.1 timbl 1111: status = response(command);
1112: if (status<0) break;
2.19 luotonen 1113: if (status >= 411 && status <= 430) break; /* no such article/group */
1.1 timbl 1114: if ((status/ 100) !=2) {
2.8 timbl 1115: HTProgress(response_text);
1.1 timbl 1116: /* NXRunAlertPanel("News access", response_text,
1117: NULL,NULL,NULL);
1118: */
1119: NETCLOSE(s);
2.11 timbl 1120: HTInputSocket_free(isoc);
1.1 timbl 1121: s = -1;
1122: /* return HT; -- no:the message might be "Timeout-disconnected" left over */
1123: continue; /* Try again */
1124: }
1125:
1126: /* Load a group, article, etc
1127: */
1.2 timbl 1128:
1.1 timbl 1129:
1130: if (list_wanted) read_list();
1131: else if (group_wanted) read_group(groupName, first, last);
1132: else read_article();
1133:
2.23 ! duns 1134: (*targetClass._free)(target);
1.2 timbl 1135: return HT_LOADED;
1.1 timbl 1136:
1137: } /* Retry loop */
1138:
1.2 timbl 1139:
2.8 timbl 1140: /* HTAlert("Sorry, could not load requested news.\n"); */
1141:
1.1 timbl 1142: /* NXRunAlertPanel(NULL, "Sorry, could not load `%s'.",
1143: NULL,NULL,NULL, arg);No -- message earlier wil have covered it */
1144:
2.23 ! duns 1145: (*targetClass._free)(target); /* AL May 2, 1994 */
1.2 timbl 1146: return HT_LOADED;
1.1 timbl 1147: }
1148:
2.10 timbl 1149: GLOBALDEF PUBLIC HTProtocol HTNews = { "news", HTLoadNews, NULL, NULL};
Webmaster