Annotation of libwww/Library/src/HTNews.c, revision 2.49
2.39 frystyk 1: /* NEWS ACCESS HTNews.c
2: ** ===========
1.1 timbl 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.
2.39 frystyk 13: ** 30 Aug 95 FTLO Added POST functionality and updated state machine
14: ** 30 Aug 95 HFN Cleaned up a whole lot and made a state machine
1.1 timbl 15: */
2.27 roeber 16:
2.39 frystyk 17: /* Library Include files */
2.28 frystyk 18: #include "tcp.h"
2.39 frystyk 19: #include "HTUtils.h"
2.28 frystyk 20: #include "HTString.h"
21: #include "HTParse.h"
2.39 frystyk 22: #include "HTFWrite.h"
23: #include "HTWriter.h"
24: #include "HTConLen.h"
2.30 frystyk 25: #include "HTSocket.h"
2.39 frystyk 26: #include "HTTCP.h"
2.28 frystyk 27: #include "HTError.h"
2.39 frystyk 28: #include "HTChunk.h"
29: #include "HTEscape.h"
2.43 frystyk 30: #include "HTReqMan.h" /* @@@ */
31: #include "HTNetMan.h" /* @@@ */
2.39 frystyk 32: #include "HTNewsRq.h"
33: #include "HTNews.h" /* Implements */
34:
35: /* Macros and other defines */
36: #ifndef NEWS_PORT
37: #define NEWS_PORT 119 /* See rfc977 */
38: #endif
1.3 timbl 39:
2.39 frystyk 40: #ifndef NEWS_LIST_FILE
41: #define NEWS_LIST_FILE ".www_news" /* Name of news list file */
42: #endif
1.1 timbl 43:
44: #ifndef DEFAULT_NEWS_HOST
2.39 frystyk 45: #define DEFAULT_NEWS_HOST "news"
1.1 timbl 46: #endif
2.39 frystyk 47:
1.1 timbl 48: #ifndef SERVER_FILE
2.39 frystyk 49: #define SERVER_FILE "/usr/local/lib/rn/server"
1.1 timbl 50: #endif
51:
2.45 frystyk 52: #define MAX_NEWS_ARTICLES 0 /* No default max number of articles */
2.40 frystyk 53:
2.39 frystyk 54: #define PUTBLOCK(b, l) (*me->target->isa->put_block) (me->target, b, l)
55: #define ABORT_TARGET (*me->target->isa->abort) (me->target, e)
2.8 timbl 56:
2.39 frystyk 57: /* Local context structure used in the HTNet object */
58: typedef enum _HTNewsState {
59: NEWS_BEGIN,
60: NEWS_SEEK_CACHE,
61: NEWS_NEED_CONNECTION,
62: NEWS_NEED_GREETING,
63: NEWS_NEED_SWITCH,
64: NEWS_NEED_ARTICLE,
2.45 frystyk 65: #if HT_LISTGROUP
2.39 frystyk 66: NEWS_NEED_LGRP,
67: #endif
68: NEWS_NEED_LIST,
69: NEWS_NEED_GROUP,
70: NEWS_NEED_XOVER,
71: NEWS_NEED_HEAD,
72: NEWS_NEED_POST,
73: NEWS_NEED_BODY,
74: NEWS_ERROR,
75: NEWS_SUCCESS
76: } HTNewsState;
77:
78: typedef struct _news_info {
79: HTChunk * cmd;
80: int repcode;
81: char * reply;
82: HTNewsState state; /* State of the connection */
83: HTFormat format;
84: char * name; /* Name of article or newsgroup */
85: BOOL sent; /* Read or write command */
86: int first; /* First article in the group list */
87: int last; /* Last article in the group list */
88: int total; /* Estimated number of articles */
89: int current; /* To count # of HEADS sent */
90: } news_info;
91:
92: /* This version of a stream is used for NEWS LIST conversion to HTML */
93: struct _HTStream {
94: CONST HTStreamClass * isa;
95: HTStream * target;
96: HTRequest * request;
97: news_info * news;
98: HTSocketEOL EOLstate;
99: BOOL semi_trans;
100: BOOL junk;
101: char buffer[MAX_NEWS_LINE+1];
102: int buflen;
1.2 timbl 103: };
104:
2.39 frystyk 105: PRIVATE char *HTNewsHost = NULL;
2.45 frystyk 106: PRIVATE int MaxArt = MAX_NEWS_ARTICLES;
1.1 timbl 107:
2.39 frystyk 108: /* ------------------------------------------------------------------------- */
109: /* NEWS INPUT STREAM */
110: /* ------------------------------------------------------------------------- */
111:
112: /* ScanResponse
113: ** ------------
114: ** Analyzes the response from the NNTP server.
115: ** We only expect one line response codes.
116: ** Returns HT_LOADED if OK, HT_ERROR if error
117: */
118: PRIVATE int ScanResponse (HTStream * me)
119: {
120: news_info *news = me->news;
121: *(me->buffer+me->buflen) = '\0';
122: if (isdigit(*(me->buffer))) sscanf(me->buffer, "%d", &news->repcode);
123: me->buflen = 0;
124: news->reply = me->buffer+4;
2.42 frystyk 125: if (PROT_TRACE) TTYPrint(TDEST, "News Rx..... `%s\'\n", news->reply);
2.39 frystyk 126:
127: /* If 2xx code and we expect data then go into semi-transparent mode */
128: if (me->news->format && news->repcode/100 == 2) {
129: HTRequest *req = me->request;
130: me->target = HTStreamStack(me->news->format, req->output_format,
131: req->output_stream, req, NO);
132: me->semi_trans = YES;
133: if (!me->target) return HT_ERROR;
134: }
135: return HT_LOADED;
1.2 timbl 136: }
1.1 timbl 137:
2.39 frystyk 138: /*
139: ** Searches for NNTP header line until buffer fills up or a CRLF or LF
140: ** is found
1.1 timbl 141: */
2.39 frystyk 142: PRIVATE int HTNewsStatus_put_block (HTStream * me, CONST char * b, int l)
1.1 timbl 143: {
2.39 frystyk 144: while (!me->semi_trans && l-- > 0) {
145: int status;
146: if (me->EOLstate == EOL_FCR) {
147: if (*b == LF) {
148: if (me->junk) me->junk = NO;
149: me->EOLstate = EOL_BEGIN;
150: if ((status = ScanResponse(me)) != HT_LOADED) return status;
151: }
152: } else if (*b == CR) {
153: me->EOLstate = EOL_FCR;
154: } else if (*b == LF) {
155: if (me->junk) me->junk = NO;
156: me->EOLstate = EOL_BEGIN;
157: if ((status = ScanResponse(me)) != HT_LOADED) return status;
158: } else {
159: *(me->buffer+me->buflen++) = *b;
160: if (me->buflen >= MAX_NEWS_LINE) {
161: if (PROT_TRACE)
2.42 frystyk 162: TTYPrint(TDEST, "News Status. Line too long - chopped\n");
2.39 frystyk 163: me->junk = YES;
164: if ((status = ScanResponse(me)) != HT_LOADED) return status;
1.1 timbl 165: }
166: }
2.39 frystyk 167: b++;
168: }
1.1 timbl 169:
2.39 frystyk 170: /*
171: ** Now see if we have parts of the body to put down the stream pipe.
172: ** At this point we are looking for CRLF.CRLF. We are guaranteed a stream
173: */
174: if (l > 0) {
175: int rest = l;
176: CONST char *ptr = b;
177: while (rest-- > 0) {
178: if (*ptr == CR) {
179: me->EOLstate = me->EOLstate==EOL_DOT ? EOL_SCR : EOL_FCR;
180: } else if (*ptr == '.') {
181: me->EOLstate = me->EOLstate==EOL_FLF ? EOL_DOT : EOL_BEGIN;
182: } else if (*ptr == LF) {
183: me->EOLstate = me->EOLstate>EOL_DOT ? EOL_SLF : EOL_FLF;
184: } else
185: me->EOLstate = EOL_BEGIN;
186: ptr++;
187: }
188: if (me->EOLstate == EOL_SLF) {
189: int status = PUTBLOCK(b, l-5);
190: return status != HT_OK ? status : HT_LOADED;
191: } else {
192: int status = PUTBLOCK(b, l);
193: return status;
1.1 timbl 194: }
195: }
2.39 frystyk 196: return HT_LOADED;
1.1 timbl 197: }
198:
2.39 frystyk 199: PRIVATE int HTNewsStatus_put_character (HTStream * me, char ch)
1.1 timbl 200: {
2.39 frystyk 201: return HTNewsStatus_put_block(me, &ch, 1);
1.1 timbl 202: }
203:
2.39 frystyk 204: PRIVATE int HTNewsStatus_put_string (HTStream * me, CONST char * str)
1.1 timbl 205: {
2.39 frystyk 206: return HTNewsStatus_put_block(me, str, (int) strlen(str));
1.1 timbl 207: }
208:
2.39 frystyk 209: PRIVATE int HTNewsStatus_flush (HTStream * me)
1.1 timbl 210: {
2.39 frystyk 211: return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
1.1 timbl 212: }
213:
2.39 frystyk 214: PRIVATE int HTNewsStatus_free (HTStream * me)
1.2 timbl 215: {
2.39 frystyk 216: int status = HT_OK;
217: if (me->target) {
218: if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
219: return HT_WOULD_BLOCK;
1.2 timbl 220: }
2.39 frystyk 221: free(me);
222: return status;
1.2 timbl 223: }
1.1 timbl 224:
2.44 frystyk 225: PRIVATE int HTNewsStatus_abort (HTStream * me, HTList * e)
2.16 luotonen 226: {
2.39 frystyk 227: if (me->target)
228: ABORT_TARGET;
229: free(me);
2.42 frystyk 230: if (PROT_TRACE) TTYPrint(TDEST, "NewsStatus.. ABORTING...\n");
2.39 frystyk 231: return HT_ERROR;
2.16 luotonen 232: }
233:
2.39 frystyk 234: PRIVATE CONST HTStreamClass HTNewsStatusClass =
235: {
236: "NewsStatus",
237: HTNewsStatus_flush,
238: HTNewsStatus_free,
239: HTNewsStatus_abort,
240: HTNewsStatus_put_character,
241: HTNewsStatus_put_string,
242: HTNewsStatus_put_block
243: };
2.16 luotonen 244:
2.39 frystyk 245: PUBLIC HTStream *HTNewsStatus_new (HTRequest * request, news_info * news)
1.1 timbl 246: {
2.39 frystyk 247: HTStream *me = (HTStream *) calloc(1, sizeof(HTStream));
248: if (!me) outofmem(__FILE__, "HTNewsStatus_new");
249: me->isa = &HTNewsStatusClass;
250: me->request = request;
251: me->news = news;
252: me->EOLstate = EOL_BEGIN;
253: return me;
1.1 timbl 254: }
255:
2.39 frystyk 256: /* ------------------------------------------------------------------------- */
257: /* PROTOCOL FUNCTIONS */
258: /* ------------------------------------------------------------------------- */
1.1 timbl 259:
2.39 frystyk 260: /*
261: ** Set the max number of articles at the same time.
262: ** Default is MAX_NEWS_ARTICLES
1.1 timbl 263: */
2.39 frystyk 264: PUBLIC BOOL HTNews_setMaxArticles (int new_max)
265: {
2.45 frystyk 266: if (new_max >= 0) {
267: MaxArt = new_max;
2.39 frystyk 268: return YES;
1.1 timbl 269: }
2.39 frystyk 270: return NO;
1.1 timbl 271: }
272:
2.39 frystyk 273: /*
274: ** Get current max number of articles at the same time.
1.1 timbl 275: */
2.39 frystyk 276: PUBLIC int HTNews_maxArticles (void)
277: {
2.45 frystyk 278: return MaxArt;
1.1 timbl 279: }
280:
2.39 frystyk 281: /*
282: ** Sets the current NEWS server.
283: */
284: PUBLIC BOOL HTNews_setHost (CONST char * newshost)
1.1 timbl 285: {
2.39 frystyk 286: if (newshost && *newshost) {
287: StrAllocCopy(HTNewsHost, newshost);
288: {
289: char *strptr = HTNewsHost;
290: while (*strptr) {
291: *strptr = TOLOWER(*strptr);
292: strptr++;
1.2 timbl 293: }
294:
2.39 frystyk 295: /* Remove final dot or paste in domain name */
296: if (strchr(HTNewsHost, '.')) {
297: if (*(HTNewsHost+strlen(HTNewsHost)-1) == '.')
298: *(HTNewsHost+strlen(HTNewsHost)-1) = '\0';
299: } else {
300: CONST char *domain = HTGetDomainName();
301: if (domain) {
302: StrAllocCat(HTNewsHost, ".");
303: StrAllocCat(HTNewsHost, domain);
304: }
305: }
1.1 timbl 306: }
2.39 frystyk 307: if (PROT_TRACE)
2.42 frystyk 308: TTYPrint(TDEST, "SetNewsHost. Host name is `%s\'\n", HTNewsHost);
2.39 frystyk 309: return YES;
310: } else {
311: if (PROT_TRACE)
2.42 frystyk 312: TTYPrint(TDEST, "SetNewsHost. Bad argument ignored\n");
2.39 frystyk 313: return NO;
1.1 timbl 314: }
315: }
316:
317: /*
2.39 frystyk 318: ** Except on the NeXT, we pick up the NewsHost name from
319: **
320: ** 1. Environment variable NNTPSERVER
321: ** 2. File SERVER_FILE
322: ** 3. Compilation time macro DEFAULT_NEWS_HOST
323: **
324: ** On the NeXT, we pick up the NewsHost name from, in order:
1.1 timbl 325: **
2.39 frystyk 326: ** 1. WorldWideWeb default "NewsHost"
327: ** 2. News default "NewsHost"
328: ** 3. Compilation time macro DEFAULT_NEWS_HOST
1.1 timbl 329: **
2.39 frystyk 330: ** Return: HTNewsHost if success else NULL
1.1 timbl 331: */
2.39 frystyk 332: PUBLIC CONST char *HTNews_host (void)
1.1 timbl 333: {
2.39 frystyk 334: if (HTNewsHost) {
335: if (*HTNewsHost) {
336: if (PROT_TRACE)
2.42 frystyk 337: TTYPrint(TDEST, "GetNewsHost. found as `%s\'\n", HTNewsHost);
2.39 frystyk 338: return HTNewsHost;
339: } else
340: return NULL; /* We couldn't get it the last time */
1.1 timbl 341: }
2.39 frystyk 342: {
343: char *newshost = NULL;
344: char buffer[80];
1.1 timbl 345:
2.39 frystyk 346: #ifdef NeXTStep
347: if ((newshost = NXGetDefaultValue("WorldWideWeb","NewsHost")) == 0)
348: if ((newshost = NXGetDefaultValue("News","NewsHost")) == 0)
349: newshost = DEFAULT_NEWS_HOST;
350: #else
351: if ((newshost = (char *) getenv("NNTPSERVER")) == NULL) {
352: FILE *fp = fopen(SERVER_FILE, "r");
353: *(buffer+79) = '\0';
354: if (fp) {
355: if (fgets(buffer, 79, fp)) {
356: char *end;
357: newshost = buffer;
358: while (*newshost == ' ' || *newshost == '\t')
359: newshost++;
360: end = newshost;
361: while (*end && !isspace(*end))
362: end++;
363: *end = '\0';
364: }
365: fclose(fp);
366: }
367: }
368: #endif /* NestStep */
1.1 timbl 369:
2.39 frystyk 370: if (!newshost || !*newshost)
371: newshost = DEFAULT_NEWS_HOST;
372: if (HTNews_setHost(newshost))
373: return HTNewsHost;
374: StrAllocCopy(HTNewsHost, "");
375: return NULL;
1.1 timbl 376: }
2.39 frystyk 377: }
1.1 timbl 378:
2.39 frystyk 379: /*
380: ** Free Newshostname
1.2 timbl 381: */
2.39 frystyk 382: PUBLIC void HTFreeNewsHost (void)
383: {
384: FREE(HTNewsHost);
385: }
1.2 timbl 386:
2.39 frystyk 387: /* HTNewsCleanup
388: ** -------------
389: ** This function closes the connection and frees memory.
390: ** Returns YES on OK, else NO
1.1 timbl 391: */
2.39 frystyk 392: PRIVATE int HTNewsCleanup (HTRequest * req, int status)
393: {
394: HTNet *net = req->net;
395: news_info *news = (news_info *) net->context;
1.1 timbl 396:
2.39 frystyk 397: /* Free stream with data TO network */
398: if (!HTRequest_isDestination(req) && req->input_stream) {
399: if (status == HT_INTERRUPTED)
400: (*req->input_stream->isa->abort)(req->input_stream, NULL);
401: else
402: (*req->input_stream->isa->_free)(req->input_stream);
1.1 timbl 403: }
2.16 luotonen 404:
2.39 frystyk 405: /* Remove the request object and our own context structure for nntp */
406: HTNet_delete(net, status);
2.49 ! frystyk 407: if (news) {
! 408: FREE(news->name);
! 409: HTChunk_delete(news->cmd);
! 410: FREE(news);
! 411: }
2.39 frystyk 412: return YES;
1.1 timbl 413: }
414:
2.39 frystyk 415: PRIVATE int SendCommand (HTRequest *request, news_info *news,
416: char *token, char *pars)
1.1 timbl 417: {
2.39 frystyk 418: int len = strlen(token) + (pars ? strlen(pars)+1:0) + 2;
2.48 frystyk 419: HTChunk_clear(news->cmd);
420: HTChunk_ensure(news->cmd, len);
2.39 frystyk 421: if (pars && *pars)
2.48 frystyk 422: sprintf(HTChunk_data(news->cmd), "%s %s%c%c", token, pars, CR, LF);
2.39 frystyk 423: else
2.48 frystyk 424: sprintf(HTChunk_data(news->cmd), "%s%c%c", token, CR, LF);
425: if (PROT_TRACE) TTYPrint(TDEST, "News Tx..... %s", HTChunk_data(news->cmd));
2.39 frystyk 426: return (*request->input_stream->isa->put_block)
2.48 frystyk 427: (request->input_stream, HTChunk_data(news->cmd), len);
2.39 frystyk 428: }
429:
430: /* Load data object from NNTP Server HTLoadNews
431: ** =================================
432: **
433: ** Given a hypertext addres, this routine loads a document
434: **
435: ** On Entry,
436: ** request The request structure
437: **
438: ** returns HT_ERROR Error has occured or interrupted
439: ** HT_WOULD_BLOCK if operation would have blocked
440: ** HT_LOADED if 200 OK
441: ** HT_NO_DATA if No Response
442: ** HT_RETRY if Service Unavail.
443: */
444: PUBLIC int HTLoadNews (SOCKET soc, HTRequest * request, SockOps ops)
445: {
446: int status = HT_ERROR;
447: HTNet *net = request->net;
448: HTParentAnchor *anchor = HTRequest_anchor(request);
449: char *url = HTAnchor_physical(anchor);
450: news_info *news;
451:
452: /*
453: ** Initiate a new nntp structure and bind to request structure
454: ** This is actually state NNTP_BEGIN, but it can't be in the state
455: ** machine as we need the structure first.
456: */
2.36 frystyk 457: if (ops == FD_NONE) {
2.39 frystyk 458: if (PROT_TRACE)
2.42 frystyk 459: TTYPrint(TDEST, "NNTP........ Looking for `%s\'\n", url);
2.36 frystyk 460: if ((news = (news_info *) calloc(1, sizeof(news_info))) == NULL)
461: outofmem(__FILE__, "HTLoadNews");
2.48 frystyk 462: news->cmd = HTChunk_new(128);
2.36 frystyk 463: news->state = NEWS_BEGIN;
464: net->context = news;
2.39 frystyk 465: } else if (ops == FD_CLOSE) { /* Interrupted */
466: if(HTRequest_isPostWeb(request)&&!HTRequest_isMainDestination(request))
467: HTNewsCleanup(request, HT_IGNORE);
468: else
469: HTNewsCleanup(request, HT_INTERRUPTED);
2.36 frystyk 470: return HT_OK;
471: } else
2.39 frystyk 472: news = (news_info *) net->context; /* Get existing copy */
473:
474: /* Now start the state machine */
475: while (1) {
476: switch (news->state) {
477: case NEWS_BEGIN:
478: news->state = (!strchr(url, '@') && strchr(url, '*')) ?
479: NEWS_SEEK_CACHE : NEWS_NEED_CONNECTION;
480: break;
481:
482: case NEWS_SEEK_CACHE: /* @@@ DO THIS @@@@@@ */
483: news->state = NEWS_NEED_CONNECTION;
484: break;
485:
486: case NEWS_NEED_CONNECTION: /* Let's set up a connection */
487: if (!strncasecomp(url, "news:", 5)) {
488: CONST char *newshost = HTNews_host();
489: StrAllocCopy(news->name, url+5);
490: if (newshost) {
491: char *newshack = NULL; /* Then we can use HTParse :-) */
492: StrAllocCopy(newshack, "news://");
493: StrAllocCat(newshack, newshost);
494: status = HTDoConnect(net, (char *) newshack, NEWS_PORT);
495: free(newshack);
496: } else
497: news->state = NEWS_ERROR;
498: } else if (!strncasecomp(url, "nntp:", 5)) {
499: news->name = HTParse(url, "", PARSE_PATH);
500: status = HTDoConnect(net, url, NEWS_PORT);
501: } else {
2.42 frystyk 502: if (PROT_TRACE) TTYPrint(TDEST, "News........ Huh?");
2.39 frystyk 503: news->state = NEWS_ERROR;
504: }
505: if (status == HT_OK) {
506: BOOL greeting = NO;
507: char *s_class = HTDNS_serverClass(net->dns);
508: if (s_class && strcasecomp(s_class, "nntp")) {
2.46 frystyk 509: HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
510: NULL, 0, "HTLoadNews");
2.39 frystyk 511: news->state = NEWS_ERROR;
512: break;
513: }
514: HTDNS_setServerClass(net->dns, "nntp");
515: if (HTDNS_socket(net->dns) == INVSOC) {
516: HTDNS_setSocket(net->dns, net->sockfd);
517: greeting = YES;
518: }
519: if (PROT_TRACE)
2.42 frystyk 520: TTYPrint(TDEST, "News........ Connected, socket %d\n",
2.39 frystyk 521: net->sockfd);
2.36 frystyk 522:
2.39 frystyk 523: /* Set up stream TO network */
524: request->input_stream = HTWriter_new(net, YES);
525:
526: /*
527: ** Set up concurrent read/write if this request isn't the
528: ** source for a PUT or POST. As source we don't start reading
529: ** before all destinations are ready. If destination then
530: ** register the input stream and get ready for read
531: */
532: if (HTRequest_isPostWeb(request)) {
533: HTEvent_Register(net->sockfd, request, (SockOps) FD_READ,
534: HTLoadNews, net->priority);
535: HTRequest_linkDestination(request);
536: }
537:
538: /* Set up stream FROM network and corresponding read buffer */
539: net->isoc = HTInputSocket_new(net->sockfd);
540: net->target = HTNewsStatus_new(request, news);
541: news->state = greeting ? NEWS_NEED_GREETING : NEWS_NEED_SWITCH;
542: } else if (status == HT_WOULD_BLOCK || status == HT_PERSISTENT)
543: return HT_OK;
544: else
545: news->state = NEWS_ERROR;
546: break;
547:
548: case NEWS_NEED_GREETING:
549: status = HTSocketRead(request, net);
550: if (status == HT_WOULD_BLOCK)
551: return HT_OK;
552: else if (status == HT_LOADED) {
553: if (news->repcode/100 == 2)
554: news->state = NEWS_NEED_SWITCH;
555: else
556: news->state = NEWS_ERROR;
557: } else
558: news->state = NEWS_ERROR;
559: break;
560:
561: case NEWS_NEED_SWITCH:
562: /*
563: ** Find out what to ask the news server. Syntax of address is
564: ** xxx@yyy Article
565: ** <xxx@yyy> Same article
566: ** xxxxx News group (no "@")
567: */
568: if (request->method == METHOD_GET) {
569: if (strchr(url, '@')) { /* ARTICLE */
570: if (*(news->name) != '<') { /* Add '<' and '>' */
571: char *newart = (char *) malloc(strlen(news->name)+3);
572: if (!newart) outofmem(__FILE__, "HTLoadNews");
573: sprintf(newart, "<%s>", news->name);
574: free(news->name);
575: news->name = newart;
576: }
577: news->state = NEWS_NEED_ARTICLE;
578: } else if (strchr(url, '*'))
579: news->state = NEWS_NEED_LIST;
580: else
581: news->state = NEWS_NEED_GROUP;
582: } else if (request->method == METHOD_POST)
583: news->state = NEWS_NEED_POST;
584: else {
2.46 frystyk 585: HTRequest_addError(request, ERR_FATAL, NO,
586: HTERR_NOT_IMPLEMENTED,NULL, 0,"HTLoadNews");
2.39 frystyk 587: news->state = NEWS_ERROR;
588: }
589: HTUnEscape(news->name);
590: HTCleanTelnetString(news->name);
591: break;
592:
593: case NEWS_NEED_ARTICLE:
594: if (!news->sent) {
595: status = SendCommand(request, news, "ARTICLE", news->name);
596: if (status == HT_WOULD_BLOCK)
597: return HT_OK;
598: else if (status == HT_ERROR)
599: news->state = NEWS_ERROR;
600: news->format = WWW_MIME;
601: news->sent = YES;
602: } else {
603: status = HTSocketRead(request, net);
604: if (status == HT_WOULD_BLOCK)
605: return HT_OK;
606: else if (status == HT_OK)
607: news->state = NEWS_NEED_BODY;
608: else if (status == HT_LOADED) {
609: news->state = (news->repcode/100 == 2) ?
610: NEWS_SUCCESS : NEWS_ERROR;
611: } else
612: news->state = NEWS_ERROR;
613: news->sent = NO;
614: }
615: break;
616:
2.45 frystyk 617: #if HT_LISTGROUP
2.39 frystyk 618: case NEWS_NEED_LGRP:
619: if (!news->sent) {
620: status = SendCommand(request, news, "LIST", "NEWSGROUPS");
621: if (status == HT_WOULD_BLOCK)
622: return HT_OK;
623: else if (status == HT_ERROR)
624: news->state = NEWS_ERROR;
625: news->format = WWW_NNTP_LIST;
626: news->sent = YES;
627: } else {
628: status = HTSocketRead(request, net);
629: if (status == HT_WOULD_BLOCK)
630: return HT_OK;
631: else if (status == HT_OK)
632: news->state = NEWS_NEED_BODY;
633: else if (status == HT_LOADED) {
634: news->state = (news->repcode/100 == 2) ?
635: NEWS_SUCCESS : NEWS_NEED_LIST;
636: } else
637: news->state = NEWS_ERROR;
638: news->sent = NO;
639: }
640: break;
2.45 frystyk 641: #endif /* HT_LISTGROUP */
2.39 frystyk 642:
643: case NEWS_NEED_LIST:
644: if (!news->sent) {
645: status = SendCommand(request, news, "LIST", NULL);
646: if (status == HT_WOULD_BLOCK)
647: return HT_OK;
648: else if (status == HT_ERROR)
649: news->state = NEWS_ERROR;
650: news->format = WWW_NNTP_LIST;
651: news->sent = YES;
652: } else {
653: status = HTSocketRead(request, net);
654: if (status == HT_WOULD_BLOCK)
655: return HT_OK;
656: else if (status == HT_OK)
657: news->state = NEWS_NEED_BODY;
658: else if (status == HT_LOADED) {
659: news->state = (news->repcode/100 == 2) ?
660: NEWS_SUCCESS : NEWS_ERROR;
661: } else
662: news->state = NEWS_ERROR;
663: news->sent = NO;
664: }
665: break;
1.1 timbl 666:
2.39 frystyk 667: case NEWS_NEED_GROUP:
668: if (!news->sent) {
669: status = SendCommand(request, news, "GROUP", news->name);
670: if (status == HT_WOULD_BLOCK)
671: return HT_OK;
672: else if (status == HT_ERROR)
673: news->state = NEWS_ERROR;
674: news->sent = YES;
1.1 timbl 675: } else {
2.39 frystyk 676: status = HTSocketRead(request, net);
677: if (status == HT_WOULD_BLOCK)
678: return HT_OK;
679: else if (status == HT_LOADED) {
680: if (news->repcode/100 == 2) {
681: if (sscanf(news->reply, "%d%d%d", &news->total,
682: &news->first, &news->last) == 3) {
2.45 frystyk 683: if (MaxArt && news->total>MaxArt)
684: news->last = news->first-MaxArt;
2.39 frystyk 685: news->current = news->first;
686: news->state = NEWS_NEED_XOVER;
687: } else
688: news->state = NEWS_ERROR;
689: } else
690: news->state = NEWS_ERROR;
691: } else
692: news->state = NEWS_ERROR;
693: news->sent = NO;
1.1 timbl 694: }
2.39 frystyk 695: break;
1.1 timbl 696:
2.39 frystyk 697: case NEWS_NEED_XOVER:
698: if (!news->sent) {
699: char buf[20];
700: sprintf(buf, "%d-%d", news->first, news->last);
701: status = SendCommand(request, news, "XOVER", buf);
702: if (status == HT_WOULD_BLOCK)
703: return HT_OK;
704: else if (status == HT_ERROR)
705: news->state = NEWS_ERROR;
706: news->format = WWW_NNTP_OVER;
707: news->sent = YES;
708: } else {
709: status = HTSocketRead(request, net);
710: if (status == HT_WOULD_BLOCK)
711: return HT_OK;
712: else if (status == HT_OK)
713: news->state = NEWS_NEED_BODY;
714: else if (status == HT_LOADED) {
715: if (news->repcode/100 == 2)
716: news->state = NEWS_SUCCESS;
717: else {
718: news->format = WWW_NNTP_HEAD;
719: news->state = NEWS_NEED_HEAD;
720: }
721: } else
722: news->state = NEWS_ERROR;
723: news->sent = NO;
724: }
725: break;
1.1 timbl 726:
2.39 frystyk 727: case NEWS_NEED_HEAD:
728: if (!news->sent) {
729: char buf[10];
730: sprintf(buf, "%d", news->current++);
731: status = SendCommand(request, news, "HEAD", buf);
732: if (status == HT_WOULD_BLOCK)
2.36 frystyk 733: return HT_OK;
2.39 frystyk 734: else if (status == HT_ERROR)
735: news->state = NEWS_ERROR;
736: news->sent = YES;
1.1 timbl 737: } else {
2.39 frystyk 738: status = HTSocketRead(request, net);
739: if (status == HT_WOULD_BLOCK)
740: return HT_OK;
741: else if (status == HT_LOADED) {
742: if (news->repcode/100 == 2) {
743: if (news->current > news->last)
744: news->state = NEWS_SUCCESS;
745: } else
746: news->state = NEWS_ERROR;
747: } else
748: news->state = NEWS_ERROR;
749: news->sent = NO;
750: }
751: break;
752:
753: case NEWS_NEED_POST:
754: request->input_stream =
755: HTNewsPost_new(request, HTBuffer_new(request->input_stream,
756: request, 512));
757:
758: /* Remember to convert to CRLF */
759:
760: news->state = NEWS_NEED_BODY;
761: break;
762:
763: case NEWS_NEED_BODY:
764: if (ops == FD_WRITE || ops == FD_NONE) {
765: if (HTRequest_isDestination(request)) {
766: HTNet *srcnet = request->source->net;
767: HTEvent_Register(srcnet->sockfd, request->source,
768: (SockOps) FD_READ,
769: HTLoadNews, srcnet->priority);
2.36 frystyk 770: return HT_OK;
2.21 frystyk 771: }
2.41 frystyk 772: status = request->PostCallback ?
773: request->PostCallback(request, request->input_stream) :
2.39 frystyk 774: (*request->input_stream->isa->flush)(request->input_stream);
775: if (status == HT_WOULD_BLOCK)
776: return HT_OK;
777: else
778: ops = FD_READ; /* Trick to ensure that we do READ */
779: } else if (ops == FD_READ) {
780: status = HTSocketRead(request, net);
781: if (status == HT_WOULD_BLOCK)
782: return HT_OK;
783: else if (status == HT_LOADED)
784: news->state = NEWS_SUCCESS;
785: else
786: news->state = NEWS_ERROR;
787: } else {
788: news->state = NEWS_ERROR;
1.1 timbl 789: }
2.39 frystyk 790: break;
791:
792: case NEWS_SUCCESS:
793: if (HTRequest_isPostWeb(request)) {
794: BOOL main = HTRequest_isMainDestination(request);
795: if (HTRequest_isDestination(request)) {
796: HTLink *link =
797: HTAnchor_findLink((HTAnchor *) request->source->anchor,
798: (HTAnchor *) request->anchor);
2.47 frystyk 799: HTLink_setResult(link, HT_LINK_OK);
2.39 frystyk 800: }
801: HTRequest_removeDestination(request);
802: HTNewsCleanup(request, main ? HT_LOADED : HT_IGNORE);
803: } else
804: HTNewsCleanup(request, HT_LOADED);
805: return HT_OK;
806: break;
807:
808: case NEWS_ERROR:
809: /* Clean up the other connections or just this one */
810: if (HTRequest_isPostWeb(request)) {
811: BOOL main = HTRequest_isMainDestination(request);
812: HTRequest_killPostWeb(request);
813: if (HTRequest_isDestination(request)) {
814: HTLink *link =
815: HTAnchor_findLink((HTAnchor *) request->source->anchor,
816: (HTAnchor *) request->anchor);
2.47 frystyk 817: HTLink_setResult(link, HT_LINK_ERROR);
2.39 frystyk 818: }
819: HTRequest_removeDestination(request);
820: HTNewsCleanup(request, main ? HT_ERROR : HT_IGNORE);
821: } else
822: HTNewsCleanup(request, HT_ERROR);
823: return HT_OK;
824: break;
1.1 timbl 825: }
2.39 frystyk 826: } /* End of while(1) */
1.1 timbl 827: }
Webmaster