Debian Codegraph @

WWW/Library/Implementation/HTNews.c

raw
1 /*
2 * $LynxId: HTNews.c,v 1.87 2026/05/25 16:55:26 tom Exp $
3 *
4 * NEWS ACCESS HTNews.c
5 * ===========
6 *
7 * History:
8 * 26 Sep 90 Written TBL
9 * 29 Nov 91 Downgraded to C, for portable implementation.
10 */
11
12 #include <HTNews.h> /* Coding convention macros */
13
14 #ifndef DISABLE_NEWS
15
16 /* Implements:
17 */
18 #include <HTMIME.h>
19 #include <HTTCP.h>
20 #include <LYUtils.h>
21 #include <LYStrings.h>
22 #include <HTCJK.h>
23
24 #define NEWS_PORT 119 /* See rfc977 */
25 #define SNEWS_PORT 563 /* See Lou Montulli */
26 #define APPEND /* Use append methods */
27 int HTNewsChunkSize = 30; /* Number of articles for quick display */
28 int HTNewsMaxChunk = 40; /* Largest number of articles in one window */
29
30 #ifndef DEFAULT_NEWS_HOST
31 #define DEFAULT_NEWS_HOST "news"
32 #endif /* DEFAULT_NEWS_HOST */
33
34 #ifndef NEWS_SERVER_FILE
35 #define NEWS_SERVER_FILE "/usr/local/lib/rn/server"
36 #endif /* NEWS_SERVER_FILE */
37
38 #ifndef NEWS_AUTH_FILE
39 #define NEWS_AUTH_FILE ".newsauth"
40 #endif /* NEWS_AUTH_FILE */
41
42 #ifdef USE_SSL
43
44 #if defined(LIBRESSL_VERSION_NUMBER)
45 /* OpenSSL and LibreSSL version numbers do not correspond */
46 #elif (OPENSSL_VERSION_NUMBER >= 0x10100000L)
47 #undef SSL_load_error_strings
48 #define SSL_load_error_strings() /* nothing */
49 #endif
50
51 static SSL *Handle = NULL;
52 static int channel_s = 1;
53
54 #define NEWS_NETWRITE(sock, buff, size) \
55 ((Handle != NULL) \
56 ? SSL_write(Handle, buff, size) \
57 : NETWRITE(sock, buff, size))
58 #define NEWS_NETCLOSE(sock) \
59 { \
60 if ((int)(sock) >= 0) { \
61 (void)NETCLOSE(sock); \
62 } \
63 if (Handle != NULL) { \
64 SSL_free(Handle); \
65 Handle = NULL; \
66 } \
67 }
68 static int HTNewsGetCharacter(void);
69
70 #define NEXT_CHAR HTNewsGetCharacter()
71 #else
72 #define NEWS_NETWRITE NETWRITE
73 #define NEWS_NETCLOSE NETCLOSE
74 #define NEXT_CHAR HTGetCharacter()
75 #endif /* USE_SSL */
76
77 #include <HTML.h>
78 #include <HTAccess.h>
79 #include <HTParse.h>
80 #include <HTFormat.h>
81 #include <HTAlert.h>
82
83 #include <LYNews.h>
84 #include <LYGlobalDefs.h>
85 #include <LYLeaks.h>
86
87 #define SnipIn(d,fmt,len,s) sprintf(d, fmt, (int)sizeof(d)-len, s)
88 #define SnipIn2(d,fmt,tag,len,s) sprintf(d, fmt, tag, (int)sizeof(d)-len, s)
89
90 struct _HTStructured {
91 const HTStructuredClass *isa;
92 /* ... */
93 };
94
95 #define LINE_LENGTH 512 /* Maximum length of line of ARTICLE etc */
96 #define GROUP_NAME_LENGTH 256 /* Maximum length of group name */
97
98 /*
99 * Module-wide variables.
100 */
101 char *HTNewsHost = NULL; /* Default host */
102 static char *NewsHost = NULL; /* Current host */
103 static char *NewsHREF = NULL; /* Current HREF prefix */
104 static int s; /* Socket for NewsHost */
105 static int HTCanPost = FALSE; /* Current POST permission */
106 static char response_text[LINE_LENGTH + 1]; /* Last response */
107
108 static HTStructured *target; /* The output sink */
109 static HTStructuredClass targetClass; /* Copy of fn addresses */
110 static HTStream *rawtarget = NULL; /* The output sink for rawtext */
111 static HTStreamClass rawtargetClass; /* Copy of fn addresses */
112 static int diagnostic; /* level: 0=none 2=source */
113 static BOOL rawtext = NO; /* Flag: HEAD or -mime_headers */
114 static HTList *NNTP_AuthInfo = NULL; /* AUTHINFO database */
115 static char *name = NULL;
116 static char *address = NULL;
117 static char *dbuf = NULL; /* dynamic buffer for long messages etc. */
118
119 #define PUTC(c) (*targetClass.put_character)(target, c)
120 #define PUTS(s) (*targetClass.put_string)(target, s)
121 #define RAW_PUTS(s) (*rawtargetClass.put_string)(rawtarget, s)
122 #define START(e) (*targetClass.start_element)(target, e, NULL, NULL, -1, NULL)
123 #define END(e) (*targetClass.end_element)(target, e, NULL)
124 #define MAYBE_END(e) if (HTML_dtd.tags[e].contents != SGML_EMPTY) \
125 (*targetClass.end_element)(target, e, NULL)
126 #define FREE_TARGET if (rawtext) (*rawtargetClass._free)(rawtarget); \
127 else (*targetClass._free)(target)
128 #define ABORT_TARGET if (rawtext) (*rawtargetClass._abort)(rawtarget, NULL); \
129 else (*targetClass._abort)(target, NULL)
130
131 typedef struct _NNTPAuth {
132 char *host;
133 char *user;
134 char *pass;
135 } NNTPAuth;
136
137 #ifdef LY_FIND_LEAKS
138 static void free_news_globals(void)
139 {
140 if (s >= 0) {
141 NEWS_NETCLOSE(s);
142 s = -1;
143 }
144 FREE(HTNewsHost);
145 FREE(NewsHost);
146 FREE(NewsHREF);
147 FREE(name);
148 FREE(address);
149 FREE(dbuf);
150 }
151 #endif /* LY_FIND_LEAKS */
152
153 static void free_NNTP_AuthInfo(void)
154 {
155 HTList *cur = NNTP_AuthInfo;
156 NNTPAuth *auth = NULL;
157
158 if (!cur)
159 return;
160
161 while (NULL != (auth = (NNTPAuth *) HTList_nextObject(cur))) {
162 FREE(auth->host);
163 FREE(auth->user);
164 FREE(auth->pass);
165 FREE(auth);
166 }
167 HTList_delete(NNTP_AuthInfo);
168 NNTP_AuthInfo = NULL;
169 return;
170 }
171
172 /*
173 * Initialize the authentication list by loading the user's $HOME/.newsauth
174 * file. That file is part of tin's configuration and is used by a few other
175 * programs.
176 */
177 static void load_NNTP_AuthInfo(void)
178 {
179 FILE *fp;
180 char fname[LY_MAXPATH];
181 char buffer[LINE_LENGTH + 1];
182
183 LYAddPathToHome(fname, sizeof(fname), NEWS_AUTH_FILE);
184
185 if ((fp = fopen(fname, "r")) != NULL) {
186 while (fgets(buffer, (int) sizeof(buffer), fp) != NULL) {
187 char the_host[LINE_LENGTH + 1];
188 char the_pass[LINE_LENGTH + 1];
189 char the_user[LINE_LENGTH + 1];
190
191 if (sscanf(buffer, "%s%s%s", the_host, the_pass, the_user) == 3
192 && strlen(the_host) != 0
193 && strlen(the_pass) != 0
194 && strlen(the_user) != 0) {
195 NNTPAuth *auth = typecalloc(NNTPAuth);
196
197 if (auth == NULL)
198 break;
199 StrAllocCopy(auth->host, the_host);
200 StrAllocCopy(auth->pass, the_pass);
201 StrAllocCopy(auth->user, the_user);
202
203 HTList_appendObject(NNTP_AuthInfo, auth);
204 }
205 }
206 fclose(fp);
207 }
208 }
209
210 const char *HTGetNewsHost(void)
211 {
212 return HTNewsHost;
213 }
214
215 void HTSetNewsHost(const char *value)
216 {
217 StrAllocCopy(HTNewsHost, value);
218 }
219
220 /* Initialisation for this module
221 * ------------------------------
222 *
223 * Except on the NeXT, we pick up the NewsHost name from
224 *
225 * 1. Environment variable NNTPSERVER
226 * 2. File NEWS_SERVER_FILE
227 * 3. Compilation time macro DEFAULT_NEWS_HOST
228 * 4. Default to "news"
229 *
230 * On the NeXT, we pick up the NewsHost name from, in order:
231 *
232 * 1. WorldWideWeb default "NewsHost"
233 * 2. Global default "NewsHost"
234 * 3. News default "NewsHost"
235 * 4. Compilation time macro DEFAULT_NEWS_HOST
236 * 5. Default to "news"
237 */
238 static BOOL initialized = NO;
239 static BOOL initialize(void)
240 {
241 #ifdef NeXTStep
242 char *cp = NULL;
243 #endif
244
245 /*
246 * Get name of Host.
247 */
248 #ifdef NeXTStep
249 if ((cp = NXGetDefaultValue("WorldWideWeb", "NewsHost")) == 0) {
250 if ((cp = NXGetDefaultValue("News", "NewsHost")) == 0) {
251 StrAllocCopy(HTNewsHost, DEFAULT_NEWS_HOST);
252 }
253 }
254 if (cp) {
255 StrAllocCopy(HTNewsHost, cp);
256 cp = NULL;
257 }
258 #else
259 if (LYGetEnv("NNTPSERVER")) {
260 StrAllocCopy(HTNewsHost, LYGetEnv("NNTPSERVER"));
261 CTRACE((tfp, "HTNews: NNTPSERVER defined as `%s'\n",
262 HTNewsHost));
263 } else {
264 FILE *fp = fopen(NEWS_SERVER_FILE, TXT_R);
265
266 if (fp) {
267 char server_name[MAXHOSTNAMELEN + 1];
268
269 if (fgets(server_name, (int) sizeof server_name, fp) != NULL) {
270 char *p = StrChr(server_name, '\n');
271
272 if (p != NULL)
273 *p = '\0';
274 StrAllocCopy(HTNewsHost, server_name);
275 CTRACE((tfp, "HTNews: File %s defines news host as `%s'\n",
276 NEWS_SERVER_FILE, HTNewsHost));
277 }
278 fclose(fp);
279 }
280 }
281 if (!HTNewsHost)
282 StrAllocCopy(HTNewsHost, DEFAULT_NEWS_HOST);
283 #endif /* NeXTStep */
284
285 s = -1; /* Disconnected */
286 #ifdef LY_FIND_LEAKS
287 atexit(free_news_globals);
288 #endif
289 return YES;
290 }
291
292 /* Send NNTP Command line to remote host & Check Response
293 * ------------------------------------------------------
294 *
295 * On entry,
296 * command points to the command to be sent, including CRLF, or is null
297 * pointer if no command to be sent.
298 * On exit,
299 * Negative status indicates transmission error, socket closed.
300 * Positive status is an NNTP status.
301 */
302 static int response(char *command)
303 {
304 int result;
305 char *p = response_text;
306 int ich;
307
308 if (command) {
309 int status;
310 int length = (int) strlen(command);
311
312 CTRACE((tfp, "NNTP command to be sent: %s", command));
313 #ifdef NOT_ASCII
314 {
315 const char *p2;
316 char *q;
317 char ascii[LINE_LENGTH + 1];
318
319 for (p2 = command, q = ascii; *p2; p2++, q++) {
320 *q = TOASCII(*p2);
321 }
322 status = NEWS_NETWRITE(s, ascii, length);
323 }
324 #else
325 status = (int) NEWS_NETWRITE(s, (char *) command, length);
326 #endif /* NOT_ASCII */
327 if (status < 0) {
328 CTRACE((tfp, "HTNews: Unable to send command. Disconnecting.\n"));
329 NEWS_NETCLOSE(s);
330 s = -1;
331 return status;
332 } /* if bad status */
333 }
334 /* if command to be sent */
335 for (;;) {
336 ich = NEXT_CHAR;
337 if (((*p++ = (char) ich) == LF) ||
338 (p == &response_text[LINE_LENGTH])) {
339 *--p = '\0'; /* Terminate the string */
340 CTRACE((tfp, "NNTP Response: %s\n", response_text));
341 sscanf(response_text, "%d", &result);
342 return result;
343 }
344 /* if end of line */
345 if (ich == EOF) {
346 *(p - 1) = '\0';
347 if (interrupted_in_htgetcharacter) {
348 CTRACE((tfp,
349 "HTNews: Interrupted on read, closing socket %d\n",
350 s));
351 } else {
352 CTRACE((tfp, "HTNews: EOF on read, closing socket %d\n",
353 s));
354 }
355 NEWS_NETCLOSE(s); /* End of file, close socket */
356 s = -1;
357 if (interrupted_in_htgetcharacter) {
358 interrupted_in_htgetcharacter = 0;
359 return (HT_INTERRUPTED);
360 }
361 return ((int) EOF); /* End of file on response */
362 }
363 } /* Loop over characters */
364 }
365
366 /* Case insensitive string comparisons
367 * -----------------------------------
368 *
369 * On entry,
370 * template must be already in upper case.
371 * unknown may be in upper or lower or mixed case to match.
372 */
373 static BOOL match(const char *unknown, const char *ctemplate)
374 {
375 const char *u = unknown;
376 const char *t = ctemplate;
377
378 for (; *u && *t && (TOUPPER(*u) == *t); u++, t++) ; /* Find mismatch or end */
379 return (BOOL) (*t == 0); /* OK if end of template */
380 }
381
382 typedef enum {
383 NNTPAUTH_ERROR = 0, /* general failure */
384 NNTPAUTH_OK = 281, /* authenticated successfully */
385 NNTPAUTH_CLOSE = 502 /* server probably closed connection */
386 } NNTPAuthResult;
387
388 /*
389 * This function handles nntp authentication. - FM
390 */
391 static NNTPAuthResult HTHandleAuthInfo(char *host)
392 {
393 HTList *cur = NULL;
394 NNTPAuth *auth = NULL;
395 char *UserName = NULL;
396 char *PassWord = NULL;
397 char *msg = NULL;
398 char buffer[512];
399 int status, tries;
400
401 /*
402 * Make sure we have a host. - FM
403 */
404 if (isEmpty(host))
405 return NNTPAUTH_ERROR;
406
407 /*
408 * Check for an existing authorization entry. - FM
409 */
410 if (NNTP_AuthInfo == NULL) {
411 NNTP_AuthInfo = HTList_new();
412 load_NNTP_AuthInfo();
413 #ifdef LY_FIND_LEAKS
414 atexit(free_NNTP_AuthInfo);
415 #endif
416 }
417
418 cur = NNTP_AuthInfo;
419 while (NULL != (auth = (NNTPAuth *) HTList_nextObject(cur))) {
420 if (!strcmp(auth->host, host)) {
421 UserName = auth->user;
422 PassWord = auth->pass;
423 break;
424 }
425 }
426
427 /*
428 * Handle the username. - FM
429 */
430 buffer[sizeof(buffer) - 1] = '\0';
431 tries = 3;
432
433 while (tries) {
434 if (UserName == NULL) {
435 HTSprintf0(&msg, LY_MSG("Username for news host '%s':"), host);
436 UserName = HTPrompt(msg, NULL);
437 FREE(msg);
438 if (!(UserName && *UserName)) {
439 FREE(UserName);
440 return NNTPAUTH_ERROR;
441 }
442 }
443 sprintf(buffer, "AUTHINFO USER %.*s%c%c",
444 (int) sizeof(buffer) - 17, UserName, CR, LF);
445 if ((status = response(buffer)) < 0) {
446 if (status == HT_INTERRUPTED)
447 _HTProgress(CONNECTION_INTERRUPTED);
448 else
449 HTAlert(FAILED_CONNECTION_CLOSED);
450 if (auth) {
451 if (auth->user != UserName) {
452 FREE(auth->user);
453 auth->user = UserName;
454 }
455 } else {
456 FREE(UserName);
457 }
458 return NNTPAUTH_CLOSE;
459 }
460 if (status == 281) {
461 /*
462 * Username is accepted and no password is required. - FM
463 */
464 if (auth) {
465 if (auth->user != UserName) {
466 FREE(auth->user);
467 auth->user = UserName;
468 }
469 } else {
470 /*
471 * Store the accepted username and no password. - FM
472 */
473 if ((auth = typecalloc(NNTPAuth)) != NULL) {
474 StrAllocCopy(auth->host, host);
475 auth->user = UserName;
476 HTList_appendObject(NNTP_AuthInfo, auth);
477 }
478 }
479 return NNTPAUTH_OK;
480 }
481 if (status != 381) {
482 /*
483 * Not success, nor a request for the password, so it must be an
484 * error. - FM
485 */
486 HTAlert(response_text);
487 tries--;
488 if ((tries > 0) && HTConfirm(gettext("Change username?"))) {
489 if (!auth || auth->user != UserName) {
490 FREE(UserName);
491 }
492 if ((UserName = HTPrompt(gettext("Username:"), UserName))
493 != NULL &&
494 *UserName) {
495 continue;
496 }
497 }
498 if (auth) {
499 if (auth->user != UserName) {
500 FREE(auth->user);
501 }
502 FREE(auth->pass);
503 }
504 FREE(UserName);
505 return NNTPAUTH_ERROR;
506 }
507 break;
508 }
509
510 if (status == 381) {
511 /*
512 * Handle the password. - FM
513 */
514 tries = 3;
515 while (tries) {
516 if (PassWord == NULL) {
517 HTSprintf0(&msg, LY_MSG("Password for news host '%s':"), host);
518 PassWord = HTPromptPassword(msg, NULL);
519 FREE(msg);
520 if (!(PassWord && *PassWord)) {
521 FREE(PassWord);
522 return NNTPAUTH_ERROR;
523 }
524 }
525 sprintf(buffer, "AUTHINFO PASS %.*s%c%c",
526 (int) sizeof(buffer) - 17, PassWord, CR, LF);
527 if ((status = response(buffer)) < 0) {
528 if (status == HT_INTERRUPTED) {
529 _HTProgress(CONNECTION_INTERRUPTED);
530 } else {
531 HTAlert(FAILED_CONNECTION_CLOSED);
532 }
533 if (auth) {
534 if (auth->user != UserName) {
535 FREE(auth->user);
536 auth->user = UserName;
537 }
538 if (auth->pass != PassWord) {
539 FREE(auth->pass);
540 auth->pass = PassWord;
541 }
542 } else {
543 FREE(UserName);
544 FREE(PassWord);
545 }
546 return NNTPAUTH_CLOSE;
547 }
548 if (status == 502) {
549 /*
550 * That's what INN's nnrpd returns. It closes the connection
551 * after this. - kw
552 */
553 HTAlert(response_text);
554 if (auth) {
555 if (auth->user == UserName)
556 UserName = NULL;
557 FREE(auth->user);
558 if (auth->pass == PassWord)
559 PassWord = NULL;
560 FREE(auth->pass);
561 }
562 FREE(UserName);
563 FREE(PassWord);
564 return NNTPAUTH_CLOSE;
565 }
566 if (status == 281) {
567 /*
568 * Password also is accepted, and everything has been stored.
569 * - FM
570 */
571 if (auth) {
572 if (auth->user != UserName) {
573 FREE(auth->user);
574 auth->user = UserName;
575 }
576 if (auth->pass != PassWord) {
577 FREE(auth->pass);
578 auth->pass = PassWord;
579 }
580 } else {
581 if ((auth = typecalloc(NNTPAuth)) != NULL) {
582 StrAllocCopy(auth->host, host);
583 auth->user = UserName;
584 auth->pass = PassWord;
585 HTList_appendObject(NNTP_AuthInfo, auth);
586 }
587 }
588 return NNTPAUTH_OK;
589 }
590 /*
591 * Not success, so it must be an error. - FM
592 */
593 HTAlert(response_text);
594 if (!auth || auth->pass != PassWord) {
595 FREE(PassWord);
596 } else {
597 PassWord = NULL;
598 }
599 tries--;
600 if ((tries > 0) && HTConfirm(gettext("Change password?"))) {
601 continue;
602 }
603 if (auth) {
604 if (auth->user == UserName)
605 UserName = NULL;
606 FREE(auth->user);
607 FREE(auth->pass);
608 }
609 FREE(UserName);
610 break;
611 }
612 }
613
614 return NNTPAUTH_ERROR;
615 }
616
617 /* Find Author's name in mail address
618 * ----------------------------------
619 *
620 * On exit,
621 * Returns allocated string which cannot be freed by the
622 * calling function, and is reallocated on subsequent calls
623 * to this function.
624 *
625 * For example, returns "Tim Berners-Lee" if given any of
626 * " Tim Berners-Lee <[email protected]> "
627 * or " [email protected] ( Tim Berners-Lee ) "
628 */
629 static char *author_name(char *email)
630 {
631 char *p, *e;
632
633 StrAllocCopy(name, email);
634 CTRACE((tfp, "Trying to find name in: %s\n", name));
635
636 if ((p = strrchr(name, '(')) && (e = strrchr(name, ')'))) {
637 if (e > p) {
638 *e = '\0'; /* Chop off everything after the ')' */
639 return HTStrip(p + 1); /* Remove leading and trailing spaces */
640 }
641 }
642
643 if ((p = strrchr(name, '<')) && (e = strrchr(name, '>'))) {
644 if (e++ > p) {
645 while ((*p++ = *e++) != 0) /* Remove <...> */
646 ;
647 return HTStrip(name); /* Remove leading and trailing spaces */
648 }
649 }
650
651 return HTStrip(name); /* Default to the whole thing */
652 }
653
654 /* Find Author's mail address
655 * --------------------------
656 *
657 * On exit,
658 * Returns allocated string which cannot be freed by the
659 * calling function, and is reallocated on subsequent calls
660 * to this function.
661 *
662 * For example, returns "[email protected]" if given any of
663 * " Lou Montulli <[email protected]> "
664 * or " [email protected] ( Lou "The Stud" Montulli ) "
665 */
666 static char *author_address(char *email)
667 {
668 char *p, *at, *e;
669
670 StrAllocCopy(address, email);
671 CTRACE((tfp, "Trying to find address in: %s\n", address));
672
673 if ((p = strrchr(address, '<'))) {
674 if ((e = strrchr(p, '>')) && (at = strrchr(p, '@'))) {
675 if (at < e) {
676 *e = '\0'; /* Remove > */
677 return HTStrip(p + 1); /* Remove leading and trailing spaces */
678 }
679 }
680 }
681
682 if ((p = strrchr(address, '(')) &&
683 (e = strrchr(address, ')')) && (at = StrChr(address, '@'))) {
684 if (e > p && at < e) {
685 *p = '\0'; /* Chop off everything after the ')' */
686 return HTStrip(address); /* Remove leading and trailing spaces */
687 }
688 }
689
690 if ((at = strrchr(address, '@')) && at > address) {
691 p = (at - 1);
692 e = (at + 1);
693 while (p > address && !isspace(UCH(*p)))
694 p--;
695 while (*e && !isspace(UCH(*e)))
696 e++;
697 *e = 0;
698 return HTStrip(p);
699 }
700
701 /*
702 * Default to the first word.
703 */
704 p = address;
705 while (isspace(UCH(*p)))
706 p++; /* find first non-space */
707 e = p;
708 while (!isspace(UCH(*e)) && *e != '\0')
709 e++; /* find next space or end */
710 *e = '\0'; /* terminate space */
711
712 return (p);
713 }
714
715 /* Start anchor element
716 * --------------------
717 */
718 static void start_anchor(const char *href)
719 {
720 BOOL present[HTML_A_ATTRIBUTES];
721 const char *value[HTML_A_ATTRIBUTES];
722 int i;
723
724 for (i = 0; i < HTML_A_ATTRIBUTES; i++)
725 present[i] = (BOOL) (i == HTML_A_HREF);
726 value[HTML_A_HREF] = href;
727 (*targetClass.start_element) (target, HTML_A, present, value, -1, NULL);
728 }
729
730 /* Start link element
731 * ------------------
732 */
733 static void start_link(const char *href, const char *rev)
734 {
735 BOOL present[HTML_LINK_ATTRIBUTES];
736 const char *value[HTML_LINK_ATTRIBUTES];
737 int i;
738
739 for (i = 0; i < HTML_LINK_ATTRIBUTES; i++)
740 present[i] = (BOOL) (i == HTML_LINK_HREF || i == HTML_LINK_REV);
741 value[HTML_LINK_HREF] = href;
742 value[HTML_LINK_REV] = rev;
743 (*targetClass.start_element) (target, HTML_LINK, present, value, -1, NULL);
744 }
745
746 /* Start list element
747 * ------------------
748 */
749 static void start_list(int seqnum)
750 {
751 BOOL present[HTML_OL_ATTRIBUTES];
752 const char *value[HTML_OL_ATTRIBUTES];
753 char SeqNum[20];
754 int i;
755
756 for (i = 0; i < HTML_OL_ATTRIBUTES; i++)
757 present[i] = (BOOL) (i == HTML_OL_SEQNUM || i == HTML_OL_START);
758 sprintf(SeqNum, "%d", seqnum);
759 value[HTML_OL_SEQNUM] = SeqNum;
760 value[HTML_OL_START] = SeqNum;
761 (*targetClass.start_element) (target, HTML_OL, present, value, -1, NULL);
762 }
763
764 /* Paste in an Anchor
765 * ------------------
766 *
767 *
768 * On entry,
769 * HT has a selection of zero length at the end.
770 * text points to the text to be put into the file, 0 terminated.
771 * addr points to the hypertext reference address,
772 * terminated by white space, comma, NULL or '>'
773 */
774 static void write_anchor(const char *text, const char *addr)
775 {
776 char href[LINE_LENGTH + 1];
777 const char *p;
778 char *q;
779
780 for (p = addr; *p && (*p != '>') && !WHITE(*p) && (*p != ','); p++) {
781 ;
782 }
783 if (strlen(NewsHREF) + (size_t) (p - addr) + 1 < sizeof(href)) {
784 q = href;
785 strcpy(q, NewsHREF);
786 /* Make complete hypertext reference */
787 StrNCat(q, addr, (size_t) (p - addr));
788 } else {
789 q = NULL;
790 HTSprintf0(&q, "%s%.*s", NewsHREF, (int) (p - addr), addr);
791 }
792
793 start_anchor(q);
794 PUTS(text);
795 END(HTML_A);
796
797 if (q != href)
798 FREE(q);
799 }
800
801 /* Write list of anchors
802 * ---------------------
803 *
804 * We take a pointer to a list of objects, and write out each,
805 * generating an anchor for each.
806 *
807 * On entry,
808 * HT has a selection of zero length at the end.
809 * text points to a comma or space separated list of addresses.
810 * On exit,
811 * *text is NOT any more chopped up into substrings.
812 */
813 static void write_anchors(char *text)
814 {
815 char *start = text;
816 char *end;
817 char c;
818
819 for (;;) {
820 for (; *start && (WHITE(*start)); start++) ; /* Find start */
821 if (!*start)
822 return; /* (Done) */
823 for (end = start;
824 *end && (*end != ' ') && (*end != ','); end++) ; /* Find end */
825 if (*end)
826 end++; /* Include comma or space but not NULL */
827 c = *end;
828 *end = '\0';
829 if (*start == '<')
830 write_anchor(start, start + 1);
831 else
832 write_anchor(start, start);
833 START(HTML_BR);
834 *end = c;
835 start = end; /* Point to next one */
836 }
837 }
838
839 /* Abort the connection abort_socket
840 * --------------------
841 */
842 static void abort_socket(void)
843 {
844 CTRACE((tfp, "HTNews: EOF on read, closing socket %d\n", s));
845 NEWS_NETCLOSE(s); /* End of file, close socket */
846 if (rawtext) {
847 RAW_PUTS("Network Error: connection lost\n");
848 } else {
849 PUTS("Network Error: connection lost");
850 PUTC('\n');
851 }
852 s = -1; /* End of file on response */
853 }
854
855 /*
856 * Determine if a line is a valid header line. valid_header
857 * -------------------------------------------
858 */
859 static BOOLEAN valid_header(char *line)
860 {
861 char *colon, *space;
862
863 /*
864 * Blank or tab in first position implies this is a continuation header.
865 */
866 if (line[0] == ' ' || line[0] == '\t')
867 return (TRUE);
868
869 /*
870 * Just check for initial letter, colon, and space to make sure we discard
871 * only invalid headers.
872 */
873 colon = StrChr(line, ':');
874 space = StrChr(line, ' ');
875 if (isalpha(UCH(line[0])) && colon && space == colon + 1)
876 return (TRUE);
877
878 /*
879 * Anything else is a bad header -- it should be ignored.
880 */
881 return (FALSE);
882 }
883
884 /* post in an Article post_article
885 * ------------------
886 * (added by FM, modeled on Lynx's previous mini inews)
887 *
888 * Note the termination condition of a single dot on a line by itself.
889 *
890 * On entry,
891 * s Global socket number is OK
892 * postfile file with header and article to post.
893 */
894 static void post_article(char *postfile)
895 {
896 char line[512];
897 char buf[512];
898 char crlf[3];
899 char *cp;
900 int status;
901 FILE *fd;
902 int in_header = 1, seen_header = 0, seen_fromline = 0;
903 int blen = 0, llen = 0;
904
905 /*
906 * Open the temporary file with the nntp headers and message body. - FM
907 */
908 if ((fd = fopen(NonNull(postfile), TXT_R)) == NULL) {
909 HTAlert(FAILED_CANNOT_OPEN_POST);
910 return;
911 }
912
913 /*
914 * Read the temporary file and post in maximum 512 byte chunks. - FM
915 */
916 buf[0] = '\0';
917 sprintf(crlf, "%c%c", CR, LF);
918 while (fgets(line, (int) sizeof(line) - 2, fd) != NULL) {
919 if ((cp = StrChr(line, '\n')) != NULL)
920 *cp = '\0';
921 if (line[0] == '.') {
922 /*
923 * A single '.' means end of transmission for nntp. Lead dots on
924 * lines normally are trimmed and the EOF is not registered if the
925 * dot was not followed by CRLF. We prepend an extra dot for any
926 * line beginning with one, to retain the one intended, as well as
927 * avoid a false EOF signal. We know we have room for it in the
928 * buffer, because we normally send when it would exceed 510. - FM
929 */
930 strcat(buf, ".");
931 blen++;
932 }
933 llen = (int) strlen(line);
934 if (in_header && !strncasecomp(line, "From:", 5)) {
935 seen_header = 1;
936 seen_fromline = 1;
937 }
938 if (in_header && line[0] == '\0') {
939 if (seen_header) {
940 in_header = 0;
941 if (!seen_fromline) {
942 if (blen >= (int) sizeof(buf) - 35) {
943 IGNORE_RC(NEWS_NETWRITE(s, buf, blen));
944 buf[blen = 0] = 0;
945 }
946 strcat(buf, "From: [email protected]");
947 strcat(buf, crlf);
948 blen += 34;
949 }
950 } else {
951 continue;
952 }
953 } else if (in_header) {
954 if (valid_header(line)) {
955 seen_header = 1;
956 } else {
957 continue;
958 }
959 }
960 strcat(line, crlf);
961 llen += 2;
962 if ((blen + llen) >= (int) sizeof(buf) - 1) {
963 IGNORE_RC(NEWS_NETWRITE(s, buf, blen));
964 buf[blen = 0] = 0;
965 }
966 strcat(buf, line);
967 blen += llen;
968 }
969 fclose(fd);
970 HTSYS_remove(postfile);
971
972 /*
973 * Send the nntp EOF and get the server's response. - FM
974 */
975 if (blen >= (int) sizeof(buf) - 4) {
976 IGNORE_RC(NEWS_NETWRITE(s, buf, blen));
977 buf[blen = 0] = 0;
978 }
979 strcat(buf, ".");
980 strcat(buf, crlf);
981 blen += 3;
982 IGNORE_RC(NEWS_NETWRITE(s, buf, blen));
983
984 status = response(NULL);
985 if (status == 240) {
986 /*
987 * Successful post. - FM
988 */
989 HTProgress(response_text);
990 } else {
991 /*
992 * Shucks, something went wrong. - FM
993 */
994 HTAlert(response_text);
995 }
996 }
997
998 #ifdef NEWS_DEBUG
999 /* for DEBUG 1997/11/07 (Fri) 17:20:16 */
1000 void debug_print(unsigned char *p)
1001 {
1002 while (*p) {
1003 if (*p == '\0')
1004 break;
1005 if (*p == 0x1b)
1006 printf("[ESC]");
1007 else if (*p == '\n')
1008 printf("[NL]");
1009 else if (*p < ' ' || *p >= 0x80)
1010 printf("(%02x)", *p);
1011 else
1012 putchar(*p);
1013 p++;
1014 }
1015 printf("]\n");
1016 }
1017 #endif
1018
1019 static char *decode_mime(char **str)
1020 {
1021 static char empty[] = "";
1022
1023 #ifdef SH_EX
1024 if (HTCJK != JAPANESE)
1025 return *str;
1026 #endif
1027 HTmmdecode(str, *str);
1028 return HTrjis(str, *str) ? *str : empty;
1029 }
1030
1031 /* Read in an Article read_article
1032 * ------------------
1033 *
1034 * Note the termination condition of a single dot on a line by itself.
1035 * RFC 977 specifies that the line "folding" of RFC850 is not used, so we
1036 * do not handle it here.
1037 *
1038 * On entry,
1039 * s Global socket number is OK
1040 * HT Global hypertext object is ready for appending text
1041 */
1042 static int read_article(HTParentAnchor *thisanchor)
1043 {
1044 char line[LINE_LENGTH + 1];
1045 char *full_line = NULL;
1046 char *subject = NULL; /* Subject string */
1047 char *from = NULL; /* From string */
1048 char *replyto = NULL; /* Reply-to string */
1049 char *date = NULL; /* Date string */
1050 char *organization = NULL; /* Organization string */
1051 char *references = NULL; /* Hrefs for other articles */
1052 char *newsgroups = NULL; /* Newsgroups list */
1053 char *followupto = NULL; /* Followup list */
1054 char *href = NULL;
1055 char *p = line;
1056 char *cp;
1057 const char *ccp;
1058 BOOL done = NO;
1059
1060 /*
1061 * Read in the HEADer of the article.
1062 *
1063 * The header fields are either ignored, or formatted and put into the
1064 * text.
1065 */
1066 if (!diagnostic && !rawtext) {
1067 while (!done) {
1068 int ich = NEXT_CHAR;
1069
1070 *p++ = (char) ich;
1071 if (ich == EOF) {
1072 if (interrupted_in_htgetcharacter) {
1073 interrupted_in_htgetcharacter = 0;
1074 CTRACE((tfp,
1075 "HTNews: Interrupted on read, closing socket %d\n",
1076 s));
1077 NEWS_NETCLOSE(s);
1078 s = -1;
1079 return (HT_INTERRUPTED);
1080 }
1081 abort_socket(); /* End of file, close socket */
1082 return (HT_LOADED); /* End of file on response */
1083 }
1084 if (((char) ich == LF) || (p == &line[LINE_LENGTH])) {
1085 *--p = '\0'; /* Terminate the string */
1086 CTRACE((tfp, "H %s\n", line));
1087
1088 if (line[0] == '\t' || line[0] == ' ') {
1089 int i = 0;
1090
1091 while (line[i]) {
1092 if (line[i] == '\t')
1093 line[i] = ' ';
1094 i++;
1095 }
1096 if (full_line == NULL) {
1097 StrAllocCopy(full_line, line);
1098 } else {
1099 StrAllocCat(full_line, line);
1100 }
1101 } else {
1102 StrAllocCopy(full_line, line);
1103 }
1104
1105 if (full_line[0] == '.') {
1106 /*
1107 * End of article?
1108 */
1109 if (UCH(full_line[1]) < ' ') {
1110 done = YES;
1111 break;
1112 }
1113 } else if (UCH(full_line[0]) < ' ') {
1114 break; /* End of Header? */
1115
1116 } else if (match(full_line, "SUBJECT:")) {
1117 StrAllocCopy(subject, HTStrip(StrChr(full_line, ':') + 1));
1118 decode_mime(&subject);
1119 } else if (match(full_line, "DATE:")) {
1120 StrAllocCopy(date, HTStrip(StrChr(full_line, ':') + 1));
1121
1122 } else if (match(full_line, "ORGANIZATION:")) {
1123 StrAllocCopy(organization,
1124 HTStrip(StrChr(full_line, ':') + 1));
1125 decode_mime(&organization);
1126
1127 } else if (match(full_line, "FROM:")) {
1128 StrAllocCopy(from, HTStrip(StrChr(full_line, ':') + 1));
1129 decode_mime(&from);
1130
1131 } else if (match(full_line, "REPLY-TO:")) {
1132 StrAllocCopy(replyto, HTStrip(StrChr(full_line, ':') + 1));
1133 decode_mime(&replyto);
1134
1135 } else if (match(full_line, "NEWSGROUPS:")) {
1136 StrAllocCopy(newsgroups, HTStrip(StrChr(full_line, ':') + 1));
1137
1138 } else if (match(full_line, "REFERENCES:")) {
1139 StrAllocCopy(references, HTStrip(StrChr(full_line, ':') + 1));
1140
1141 } else if (match(full_line, "FOLLOWUP-TO:")) {
1142 StrAllocCopy(followupto, HTStrip(StrChr(full_line, ':') + 1));
1143
1144 } else if (match(full_line, "MESSAGE-ID:")) {
1145 char *msgid = HTStrip(full_line + 11);
1146
1147 if (msgid[0] == '<' && msgid[strlen(msgid) - 1] == '>') {
1148 msgid[strlen(msgid) - 1] = '\0'; /* Chop > */
1149 msgid++; /* Chop < */
1150 HTAnchor_setMessageID(thisanchor, msgid);
1151 }
1152
1153 } /* end if match */
1154 p = line; /* Restart at beginning */
1155 } /* if end of line */
1156 } /* Loop over characters */
1157 FREE(full_line);
1158
1159 START(HTML_HEAD);
1160 PUTC('\n');
1161 START(HTML_TITLE);
1162 if (subject && *subject != '\0')
1163 PUTS(subject);
1164 else
1165 PUTS("No Subject");
1166 END(HTML_TITLE);
1167 PUTC('\n');
1168 /*
1169 * Put in the owner as a link rel.
1170 */
1171 if (from || replyto) {
1172 char *temp = NULL;
1173
1174 StrAllocCopy(temp, author_address(replyto ? replyto : from));
1175 StrAllocCopy(href, STR_MAILTO_URL);
1176 if (StrChr(temp, '%') || StrChr(temp, '?')) {
1177 cp = HTEscape(temp, URL_XPALPHAS);
1178 StrAllocCat(href, cp);
1179 FREE(cp);
1180 } else {
1181 StrAllocCat(href, temp);
1182 }
1183 start_link(href, "made");
1184 PUTC('\n');
1185 FREE(temp);
1186 }
1187 END(HTML_HEAD);
1188 PUTC('\n');
1189
1190 START(HTML_H1);
1191 if (subject && *subject != '\0')
1192 PUTS(subject);
1193 else
1194 PUTS("No Subject");
1195 END(HTML_H1);
1196 PUTC('\n');
1197
1198 if (subject)
1199 FREE(subject);
1200
1201 START(HTML_DLC);
1202 PUTC('\n');
1203
1204 if (from || replyto) {
1205 START(HTML_DT);
1206 START(HTML_B);
1207 PUTS("From:");
1208 END(HTML_B);
1209 PUTC(' ');
1210 if (from)
1211 PUTS(from);
1212 else
1213 PUTS(replyto);
1214 MAYBE_END(HTML_DT);
1215 PUTC('\n');
1216
1217 if (!replyto)
1218 StrAllocCopy(replyto, from);
1219 START(HTML_DT);
1220 START(HTML_B);
1221 PUTS("Reply to:");
1222 END(HTML_B);
1223 PUTC(' ');
1224 start_anchor(href);
1225 if (*replyto != '<')
1226 PUTS(author_name(replyto));
1227 else
1228 PUTS(author_address(replyto));
1229 END(HTML_A);
1230 START(HTML_BR);
1231 MAYBE_END(HTML_DT);
1232 PUTC('\n');
1233
1234 FREE(from);
1235 FREE(replyto);
1236 }
1237
1238 if (date) {
1239 START(HTML_DT);
1240 START(HTML_B);
1241 PUTS("Date:");
1242 END(HTML_B);
1243 PUTC(' ');
1244 PUTS(date);
1245 MAYBE_END(HTML_DT);
1246 PUTC('\n');
1247 FREE(date);
1248 }
1249
1250 if (organization) {
1251 START(HTML_DT);
1252 START(HTML_B);
1253 PUTS("Organization:");
1254 END(HTML_B);
1255 PUTC(' ');
1256 PUTS(organization);
1257 MAYBE_END(HTML_DT);
1258 PUTC('\n');
1259 FREE(organization);
1260 }
1261
1262 /* sanitize some headers - kw */
1263 if (newsgroups &&
1264 ((cp = StrChr(newsgroups, '/')) ||
1265 (cp = StrChr(newsgroups, '(')))) {
1266 *cp = '\0';
1267 }
1268 if (newsgroups && !*newsgroups) {
1269 FREE(newsgroups);
1270 }
1271 if (followupto &&
1272 ((cp = StrChr(followupto, '/')) ||
1273 (cp = StrChr(followupto, '(')))) {
1274 *cp = '\0';
1275 }
1276 if (followupto && !*followupto) {
1277 FREE(followupto);
1278 }
1279
1280 if (newsgroups && HTCanPost) {
1281 START(HTML_DT);
1282 START(HTML_B);
1283 PUTS("Newsgroups:");
1284 END(HTML_B);
1285 PUTC('\n');
1286 MAYBE_END(HTML_DT);
1287 START(HTML_DD);
1288 write_anchors(newsgroups);
1289 MAYBE_END(HTML_DD);
1290 PUTC('\n');
1291 }
1292
1293 if (followupto && !strcasecomp(followupto, "poster")) {
1294 /*
1295 * "Followup-To: poster" has special meaning. Don't use it to
1296 * construct a newsreply link. -kw
1297 */
1298 START(HTML_DT);
1299 START(HTML_B);
1300 PUTS("Followup to:");
1301 END(HTML_B);
1302 PUTC(' ');
1303 if (href) {
1304 start_anchor(href);
1305 PUTS("poster");
1306 END(HTML_A);
1307 } else {
1308 PUTS("poster");
1309 }
1310 MAYBE_END(HTML_DT);
1311 PUTC('\n');
1312 FREE(followupto);
1313 }
1314
1315 if (newsgroups && HTCanPost) {
1316 /*
1317 * We have permission to POST to this host, so add a link for
1318 * posting followups for this article. - FM
1319 */
1320 if (!strncasecomp(NewsHREF, STR_SNEWS_URL, 6))
1321 StrAllocCopy(href, "snewsreply://");
1322 else
1323 StrAllocCopy(href, "newsreply://");
1324 StrAllocCat(href, NewsHost);
1325 StrAllocCat(href, "/");
1326 StrAllocCat(href, (followupto ? followupto : newsgroups));
1327 if (*href == 'n' &&
1328 (ccp = HTAnchor_messageID(thisanchor)) && *ccp) {
1329 StrAllocCat(href, ";ref=");
1330 if (StrChr(ccp, '<') || StrChr(ccp, '&') ||
1331 StrChr(ccp, ' ') || StrChr(ccp, ':') ||
1332 StrChr(ccp, '/') || StrChr(ccp, '%') ||
1333 StrChr(ccp, ';')) {
1334 char *cp1 = HTEscape(ccp, URL_XPALPHAS);
1335
1336 StrAllocCat(href, cp1);
1337 FREE(cp1);
1338 } else {
1339 StrAllocCat(href, ccp);
1340 }
1341 }
1342
1343 START(HTML_DT);
1344 START(HTML_B);
1345 PUTS("Followup to:");
1346 END(HTML_B);
1347 PUTC(' ');
1348 start_anchor(href);
1349 if (StrChr((followupto ? followupto : newsgroups), ',')) {
1350 PUTS("newsgroups");
1351 } else {
1352 PUTS("newsgroup");
1353 }
1354 END(HTML_A);
1355 MAYBE_END(HTML_DT);
1356 PUTC('\n');
1357 }
1358 FREE(newsgroups);
1359 FREE(followupto);
1360
1361 if (references) {
1362 START(HTML_DT);
1363 START(HTML_B);
1364 PUTS("References:");
1365 END(HTML_B);
1366 MAYBE_END(HTML_DT);
1367 PUTC('\n');
1368 START(HTML_DD);
1369 write_anchors(references);
1370 MAYBE_END(HTML_DD);
1371 PUTC('\n');
1372 FREE(references);
1373 }
1374
1375 END(HTML_DLC);
1376 PUTC('\n');
1377 FREE(href);
1378 }
1379
1380 if (rawtext) {
1381 /*
1382 * No tags, and never do a PUTC. - kw
1383 */
1384 ;
1385 } else if (diagnostic) {
1386 /*
1387 * Read in the HEAD and BODY of the Article as XMP formatted text. -
1388 * FM
1389 */
1390 START(HTML_XMP);
1391 PUTC('\n');
1392 } else {
1393 /*
1394 * Read in the BODY of the Article as PRE formatted text. - FM
1395 */
1396 START(HTML_PRE);
1397 PUTC('\n');
1398 }
1399
1400 p = line;
1401 while (!done) {
1402 int ich = NEXT_CHAR;
1403
1404 *p++ = (char) ich;
1405 if (ich == EOF) {
1406 if (interrupted_in_htgetcharacter) {
1407 interrupted_in_htgetcharacter = 0;
1408 CTRACE((tfp,
1409 "HTNews: Interrupted on read, closing socket %d\n",
1410 s));
1411 NEWS_NETCLOSE(s);
1412 s = -1;
1413 return (HT_INTERRUPTED);
1414 }
1415 abort_socket(); /* End of file, close socket */
1416 return (HT_LOADED); /* End of file on response */
1417 }
1418 if (((char) ich == LF) || (p == &line[LINE_LENGTH])) {
1419 *p = '\0'; /* Terminate the string */
1420 CTRACE((tfp, "B %s", line));
1421 #ifdef NEWS_DEBUG /* 1997/11/09 (Sun) 15:56:11 */
1422 debug_print(line); /* @@@ */
1423 #endif
1424 if (line[0] == '.') {
1425 /*
1426 * End of article?
1427 */
1428 if (UCH(line[1]) < ' ') {
1429 break;
1430 } else { /* Line starts with dot */
1431 if (rawtext) {
1432 RAW_PUTS(&line[1]);
1433 } else {
1434 PUTS(&line[1]); /* Ignore first dot */
1435 }
1436 }
1437 } else {
1438 if (rawtext) {
1439 RAW_PUTS(line);
1440 } else if (diagnostic || !scan_for_buried_news_references) {
1441 /*
1442 * All lines are passed as unmodified source. - FM
1443 */
1444 PUTS(line);
1445 } else {
1446 /*
1447 * Normal lines are scanned for buried references to other
1448 * articles. Unfortunately, it could pick up mail
1449 * addresses as well! It also can corrupt uuencoded
1450 * messages! So we don't do this when fetching articles as
1451 * WWW_SOURCE or when downloading (diagnostic is TRUE) or
1452 * if the client has set scan_for_buried_news_references to
1453 * FALSE. Otherwise, we convert all "<...@...>" strings
1454 * preceded by "rticle " to "news:...@..." links, and any
1455 * strings that look like URLs to links. - FM
1456 */
1457 char *l = line;
1458 char *p2;
1459
1460 while ((p2 = strstr(l, "rticle <")) != NULL) {
1461 char *q = strrchr(p2, '>');
1462 char *at = strrchr(p2, '@');
1463
1464 if (q && at && at < q) {
1465 char c = q[1];
1466
1467 q[1] = 0; /* chop up */
1468 p2 += 7;
1469 *p2 = 0;
1470 while (*l) {
1471 if (StrNCmp(l, STR_NEWS_URL, LEN_NEWS_URL) &&
1472 StrNCmp(l, "snews://", 8) &&
1473 StrNCmp(l, "nntp://", 7) &&
1474 StrNCmp(l, "snewspost:", 10) &&
1475 StrNCmp(l, "snewsreply:", 11) &&
1476 StrNCmp(l, "newspost:", 9) &&
1477 StrNCmp(l, "newsreply:", 10) &&
1478 StrNCmp(l, "ftp://", 6) &&
1479 StrNCmp(l, "file:/", 6) &&
1480 StrNCmp(l, "finger://", 9) &&
1481 StrNCmp(l, "http://", 7) &&
1482 StrNCmp(l, "https://", 8) &&
1483 StrNCmp(l, "wais://", 7) &&
1484 StrNCmp(l, STR_MAILTO_URL, LEN_MAILTO_URL) &&
1485 StrNCmp(l, "cso://", 6) &&
1486 StrNCmp(l, "gopher://", 9)) {
1487 PUTC(*l++);
1488 } else {
1489 StrAllocCopy(href, l);
1490 start_anchor(strtok(href, " \r\n\t,>)\""));
1491 while (*l && !StrChr(" \r\n\t,>)\"", *l))
1492 PUTC(*l++);
1493 END(HTML_A);
1494 FREE(href);
1495 }
1496 }
1497 *p2 = '<'; /* again */
1498 *q = 0;
1499 start_anchor(p2 + 1);
1500 *q = '>'; /* again */
1501 PUTS(p2);
1502 END(HTML_A);
1503 q[1] = c; /* again */
1504 l = q + 1;
1505 } else {
1506 break; /* line has unmatched <> */
1507 }
1508 }
1509 while (*l) { /* Last bit of the line */
1510 if (StrNCmp(l, STR_NEWS_URL, LEN_NEWS_URL) &&
1511 StrNCmp(l, "snews://", 8) &&
1512 StrNCmp(l, "nntp://", 7) &&
1513 StrNCmp(l, "snewspost:", 10) &&
1514 StrNCmp(l, "snewsreply:", 11) &&
1515 StrNCmp(l, "newspost:", 9) &&
1516 StrNCmp(l, "newsreply:", 10) &&
1517 StrNCmp(l, "ftp://", 6) &&
1518 StrNCmp(l, "file:/", 6) &&
1519 StrNCmp(l, "finger://", 9) &&
1520 StrNCmp(l, "http://", 7) &&
1521 StrNCmp(l, "https://", 8) &&
1522 StrNCmp(l, "wais://", 7) &&
1523 StrNCmp(l, STR_MAILTO_URL, LEN_MAILTO_URL) &&
1524 StrNCmp(l, "cso://", 6) &&
1525 StrNCmp(l, "gopher://", 9))
1526 PUTC(*l++);
1527 else {
1528 StrAllocCopy(href, l);
1529 start_anchor(strtok(href, " \r\n\t,>)\""));
1530 while (*l && !StrChr(" \r\n\t,>)\"", *l))
1531 PUTC(*l++);
1532 END(HTML_A);
1533 FREE(href);
1534 }
1535 }
1536 } /* if diagnostic or not scan_for_buried_news_references */
1537 } /* if not dot */
1538 p = line; /* Restart at beginning */
1539 } /* if end of line */
1540 } /* Loop over characters */
1541
1542 if (rawtext)
1543 return (HT_LOADED);
1544
1545 if (diagnostic)
1546 END(HTML_XMP);
1547 else
1548 END(HTML_PRE);
1549 PUTC('\n');
1550 return (HT_LOADED);
1551 }
1552
1553 /* Read in a List of Newsgroups
1554 * ----------------------------
1555 *
1556 * Note the termination condition of a single dot on a line by itself.
1557 * RFC 977 specifies that the line "folding" of RFC850 is not used,
1558 * so we do not handle it here.
1559 */
1560 static int read_list(char *arg)
1561 {
1562 char line[LINE_LENGTH + 1];
1563 char *p;
1564 BOOL done = NO;
1565 BOOL head = NO;
1566 BOOL tail = NO;
1567 BOOL skip_this_line = NO;
1568 BOOL skip_rest_of_line = NO;
1569 int listing = 0;
1570 char *pattern = NULL;
1571 int len = 0;
1572
1573 /*
1574 * Support head or tail matches for groups to list. - FM
1575 */
1576 if (arg && strlen(arg) > 1) {
1577 if (*arg == '*') {
1578 tail = YES;
1579 StrAllocCopy(pattern, (arg + 1));
1580 } else if (arg[strlen(arg) - 1] == '*') {
1581 head = YES;
1582 StrAllocCopy(pattern, arg);
1583 pattern[strlen(pattern) - 1] = '\0';
1584 }
1585 if (tail || head) {
1586 len = (int) strlen(pattern);
1587 }
1588
1589 }
1590
1591 /*
1592 * Read the server's reply.
1593 *
1594 * The lines are scanned for newsgroup names and descriptions.
1595 */
1596 START(HTML_HEAD);
1597 PUTC('\n');
1598 START(HTML_TITLE);
1599 PUTS("Newsgroups");
1600 END(HTML_TITLE);
1601 PUTC('\n');
1602 END(HTML_HEAD);
1603 PUTC('\n');
1604 START(HTML_H1);
1605 PUTS("Newsgroups");
1606 END(HTML_H1);
1607 PUTC('\n');
1608 *(p = line) = '\0';
1609 START(HTML_DLC);
1610 PUTC('\n');
1611 while (!done) {
1612 int ich = NEXT_CHAR;
1613 char ch = (char) ich;
1614
1615 if (ich == EOF) {
1616 if (interrupted_in_htgetcharacter) {
1617 interrupted_in_htgetcharacter = 0;
1618 CTRACE((tfp,
1619 "HTNews: Interrupted on read, closing socket %d\n",
1620 s));
1621 NEWS_NETCLOSE(s);
1622 s = -1;
1623 return (HT_INTERRUPTED);
1624 }
1625 abort_socket(); /* End of file, close socket */
1626 FREE(pattern);
1627 return (HT_LOADED); /* End of file on response */
1628 } else if (skip_this_line) {
1629 if (ch == LF) {
1630 skip_this_line = skip_rest_of_line = NO;
1631 p = line;
1632 }
1633 continue;
1634 } else if (skip_rest_of_line) {
1635 if (ch != LF) {
1636 continue;
1637 }
1638 } else if (p == &line[LINE_LENGTH]) {
1639 CTRACE((tfp, "b %.*s%c[...]\n", (LINE_LENGTH), line, ch));
1640 *p = '\0';
1641 if (ch == LF) {
1642 ; /* Will be dealt with below */
1643 } else if (WHITE(ch)) {
1644 ch = LF; /* May treat as line without description */
1645 skip_this_line = YES; /* ...and ignore until LF */
1646 } else if (StrChr(line, ' ') == NULL &&
1647 StrChr(line, '\t') == NULL) {
1648 /* No separator found */
1649 CTRACE((tfp, "HTNews..... group name too long, discarding.\n"));
1650 skip_this_line = YES; /* ignore whole line */
1651 continue;
1652 } else {
1653 skip_rest_of_line = YES; /* skip until ch == LF found */
1654 }
1655 } else {
1656 *p++ = ch;
1657 }
1658 if (ch == LF) {
1659 skip_rest_of_line = NO; /* done, reset flag */
1660 *p = '\0'; /* Terminate the string */
1661 CTRACE((tfp, "B %s", line));
1662 if (line[0] == '.') {
1663 /*
1664 * End of article?
1665 */
1666 if (UCH(line[1]) < ' ') {
1667 break;
1668 } else { /* Line starts with dot */
1669 START(HTML_DT);
1670 PUTS(&line[1]);
1671 MAYBE_END(HTML_DT);
1672 }
1673 } else if (line[0] == '#') { /* Comment? */
1674 p = line; /* Restart at beginning */
1675 continue;
1676 } else {
1677 /*
1678 * Normal lines are scanned for references to newsgroups.
1679 */
1680 int i = 0;
1681
1682 /* find whitespace if it exits */
1683 for (; line[i] != '\0' && !WHITE(line[i]); i++) ; /* null body */
1684
1685 if (line[i] != '\0') {
1686 line[i] = '\0';
1687 if ((head && strncasecomp(line, pattern, len)) ||
1688 (tail && (i < len ||
1689 strcasecomp((line + (i - len)), pattern)))) {
1690 p = line; /* Restart at beginning */
1691 continue;
1692 }
1693 START(HTML_DT);
1694 write_anchor(line, line);
1695 listing++;
1696 MAYBE_END(HTML_DT);
1697 PUTC('\n');
1698 START(HTML_DD);
1699 PUTS(&line[i + 1]); /* put description */
1700 MAYBE_END(HTML_DD);
1701 } else {
1702 if ((head && strncasecomp(line, pattern, len)) ||
1703 (tail && (i < len ||
1704 strcasecomp((line + (i - len)), pattern)))) {
1705 p = line; /* Restart at beginning */
1706 continue;
1707 }
1708 START(HTML_DT);
1709 write_anchor(line, line);
1710 MAYBE_END(HTML_DT);
1711 listing++;
1712 }
1713 } /* if not dot */
1714 p = line; /* Restart at beginning */
1715 } /* if end of line */
1716 } /* Loop over characters */
1717 if (!listing) {
1718 char *msg = NULL;
1719
1720 START(HTML_DT);
1721 HTSprintf0(&msg, LY_MSG("No matches for: %s"), arg);
1722 PUTS(msg);
1723 MAYBE_END(HTML_DT);
1724 FREE(msg);
1725 }
1726 END(HTML_DLC);
1727 PUTC('\n');
1728 FREE(pattern);
1729 return (HT_LOADED);
1730 }
1731
1732 /* Read in a Newsgroup
1733 * -------------------
1734 *
1735 * Unfortunately, we have to ask for each article one by one if we
1736 * want more than one field.
1737 *
1738 */
1739 static int read_group(const char *groupName,
1740 int first_required,
1741 int last_required)
1742 {
1743 char line[LINE_LENGTH + 1];
1744 char *author = NULL;
1745 char *subject = NULL;
1746 char *date = NULL;
1747 int i;
1748 char *p;
1749 BOOL done;
1750
1751 char buffer[LINE_LENGTH + 1];
1752 char *temp = NULL;
1753 char *reference = NULL; /* Href for article */
1754 int art; /* Article number WITHIN GROUP */
1755 int status, count, first, last; /* Response fields */
1756
1757 START(HTML_HEAD);
1758 PUTC('\n');
1759 START(HTML_TITLE);
1760 PUTS("Newsgroup ");
1761 PUTS(groupName);
1762 END(HTML_TITLE);
1763 PUTC('\n');
1764 END(HTML_HEAD);
1765 PUTC('\n');
1766
1767 sscanf(response_text, " %d %d %d %d", &status, &count, &first, &last);
1768 CTRACE((tfp, "Newsgroup status=%d, count=%d, (%d-%d) required:(%d-%d)\n",
1769 status, count, first, last, first_required, last_required));
1770 if (last == 0) {
1771 PUTS(gettext("\nNo articles in this group.\n"));
1772 goto add_post;
1773 }
1774 #define FAST_THRESHOLD 100 /* Above this, read IDs fast */
1775 #define CHOP_THRESHOLD 50 /* Above this, chop off the rest */
1776
1777 if (first_required < first)
1778 first_required = first; /* clip */
1779 if ((last_required == 0) || (last_required > last))
1780 last_required = last;
1781
1782 if (last_required < first_required) {
1783 PUTS(gettext("\nNo articles in this range.\n"));
1784 goto add_post;
1785 }
1786
1787 if (last_required - first_required + 1 > HTNewsMaxChunk) { /* Trim this block */
1788 first_required = last_required - HTNewsChunkSize + 1;
1789 }
1790 CTRACE((tfp, " Chunk will be (%d-%d)\n",
1791 first_required, last_required));
1792
1793 /*
1794 * Set window title.
1795 */
1796 HTSprintf0(&temp, LY_MSG("%s, Articles %d-%d"),
1797 groupName, first_required, last_required);
1798 START(HTML_H1);
1799 PUTS(temp);
1800 FREE(temp);
1801 END(HTML_H1);
1802 PUTC('\n');
1803
1804 /*
1805 * Link to earlier articles.
1806 */
1807 if (first_required > first) {
1808 int before; /* Start of one before */
1809
1810 if (first_required - HTNewsMaxChunk <= first)
1811 before = first;
1812 else
1813 before = first_required - HTNewsChunkSize;
1814 HTSprintf0(&dbuf, "%s%s/%d-%d", NewsHREF, groupName,
1815 before, first_required - 1);
1816 CTRACE((tfp, " Block before is %s\n", dbuf));
1817 PUTC('(');
1818 start_anchor(dbuf);
1819 PUTS(gettext("Earlier articles"));
1820 END(HTML_A);
1821 PUTS("...)\n");
1822 START(HTML_P);
1823 PUTC('\n');
1824 }
1825
1826 done = NO;
1827
1828 /*#define USE_XHDR*/
1829 #ifdef USE_XHDR
1830 if (count > FAST_THRESHOLD) {
1831 HTSprintf0(&temp,
1832 LY_MSG("\nThere are about %d articles currently available in %s, IDs as follows:\n\n"),
1833 count, groupName);
1834 PUTS(temp);
1835 FREE(temp);
1836 sprintf(buffer, "XHDR Message-ID %d-%d%c%c", first, last, CR, LF);
1837 status = response(buffer);
1838 if (status == 221) {
1839 p = line;
1840 while (!done) {
1841 int ich = NEXT_CHAR;
1842
1843 *p++ = ich;
1844 if (ich == EOF) {
1845 if (interrupted_in_htgetcharacter) {
1846 interrupted_in_htgetcharacter = 0;
1847 CTRACE((tfp,
1848 "HTNews: Interrupted on read, closing socket %d\n",
1849 s));
1850 NEWS_NETCLOSE(s);
1851 s = -1;
1852 return (HT_INTERRUPTED);
1853 }
1854 abort_socket(); /* End of file, close socket */
1855 return (HT_LOADED); /* End of file on response */
1856 }
1857 if (((char) ich == '\n') || (p == &line[LINE_LENGTH])) {
1858 *p = '\0'; /* Terminate the string */
1859 CTRACE((tfp, "X %s", line));
1860 if (line[0] == '.') {
1861 /*
1862 * End of article?
1863 */
1864 if (UCH(line[1]) < ' ') {
1865 done = YES;
1866 break;
1867 } else { /* Line starts with dot */
1868 /* Ignore strange line */
1869 }
1870 } else {
1871 /*
1872 * Normal lines are scanned for references to articles.
1873 */
1874 char *space = StrChr(line, ' ');
1875
1876 if (space++)
1877 write_anchor(space, space);
1878 } /* if not dot */
1879 p = line; /* Restart at beginning */
1880 } /* if end of line */
1881 } /* Loop over characters */
1882
1883 /* leaving loop with "done" set */
1884 } /* Good status */
1885 }
1886 #endif /* USE_XHDR */
1887
1888 /*
1889 * Read newsgroup using individual fields.
1890 */
1891 if (!done) {
1892 START(HTML_B);
1893 if (first == first_required && last == last_required)
1894 PUTS(gettext("All available articles in "));
1895 else
1896 PUTS("Articles in ");
1897 PUTS(groupName);
1898 END(HTML_B);
1899 PUTC('\n');
1900 if (LYListNewsNumbers)
1901 start_list(first_required);
1902 else
1903 START(HTML_UL);
1904 for (art = first_required; art <= last_required; art++) {
1905 /*#define OVERLAP*/
1906 #ifdef OVERLAP
1907 /*
1908 * With this code we try to keep the server running flat out by
1909 * queuing just one extra command ahead of time. We assume (1)
1910 * that the server won't abort if it gets input during output, and
1911 * (2) that TCP buffering is enough for the two commands. Both
1912 * these assumptions seem very reasonable. However, we HAVE had a
1913 * hangup with a loaded server.
1914 */
1915 if (art == first_required) {
1916 if (art == last_required) { /* Only one */
1917 sprintf(buffer, "HEAD %d%c%c",
1918 art, CR, LF);
1919 status = response(buffer);
1920 } else { /* First of many */
1921 sprintf(buffer, "HEAD %d%c%cHEAD %d%c%c",
1922 art, CR, LF, art + 1, CR, LF);
1923 status = response(buffer);
1924 }
1925 } else if (art == last_required) { /* Last of many */
1926 status = response(NULL);
1927 } else { /* Middle of many */
1928 sprintf(buffer, "HEAD %d%c%c", art + 1, CR, LF);
1929 status = response(buffer);
1930 }
1931 #else /* Not OVERLAP: */
1932 sprintf(buffer, "HEAD %d%c%c", art, CR, LF);
1933 status = response(buffer);
1934 #endif /* OVERLAP */
1935 /*
1936 * Check for a good response (221) for the HEAD request, and if so,
1937 * parse it. Otherwise, indicate the error so that the number of
1938 * listings corresponds to what's claimed for the range, and if we
1939 * are listing numbers via an ordered list, they stay in synchrony
1940 * with the article numbers. - FM
1941 */
1942 if (status == 221) { /* Head follows - parse it: */
1943 p = line; /* Write pointer */
1944 done = NO;
1945 while (!done) {
1946 int ich = NEXT_CHAR;
1947
1948 *p++ = (char) ich;
1949 if (ich == EOF) {
1950 if (interrupted_in_htgetcharacter) {
1951 interrupted_in_htgetcharacter = 0;
1952 CTRACE((tfp,
1953 "HTNews: Interrupted on read, closing socket %d\n",
1954 s));
1955 NEWS_NETCLOSE(s);
1956 s = -1;
1957 return (HT_INTERRUPTED);
1958 }
1959 abort_socket(); /* End of file, close socket */
1960 return (HT_LOADED); /* End of file on response */
1961 }
1962 if (((char) ich == LF) ||
1963 (p == &line[LINE_LENGTH])) {
1964
1965 *--p = '\0'; /* Terminate & chop LF */
1966 p = line; /* Restart at beginning */
1967 CTRACE((tfp, "G %s\n", line));
1968 switch (line[0]) {
1969
1970 case '.':
1971 /*
1972 * End of article?
1973 */
1974 done = (BOOL) (UCH(line[1]) < ' ');
1975 break;
1976
1977 case 'S':
1978 case 's':
1979 if (match(line, "SUBJECT:")) {
1980 StrAllocCopy(subject, line + 9);
1981 decode_mime(&subject);
1982 }
1983 break;
1984
1985 case 'M':
1986 case 'm':
1987 if (match(line, "MESSAGE-ID:")) {
1988 char *addr = HTStrip(line + 11) + 1; /* Chop < */
1989
1990 addr[strlen(addr) - 1] = '\0'; /* Chop > */
1991 StrAllocCopy(reference, addr);
1992 }
1993 break;
1994
1995 case 'f':
1996 case 'F':
1997 if (match(line, "FROM:")) {
1998 char *p2;
1999
2000 StrAllocCopy(author, StrChr(line, ':') + 1);
2001 decode_mime(&author);
2002 p2 = author + strlen(author) - 1;
2003 if (*p2 == LF)
2004 *p2 = '\0'; /* Chop off newline */
2005 }
2006 break;
2007
2008 case 'd':
2009 case 'D':
2010 if (LYListNewsDates && match(line, "DATE:")) {
2011 StrAllocCopy(date,
2012 HTStrip(StrChr(line, ':') + 1));
2013 }
2014 break;
2015
2016 } /* end switch on first character */
2017 } /* if end of line */
2018 } /* Loop over characters */
2019
2020 PUTC('\n');
2021 START(HTML_LI);
2022 p = decode_mime(&subject);
2023 HTSprintf0(&temp, "\"%s\"", NonNull(p));
2024 if (reference) {
2025 write_anchor(temp, reference);
2026 FREE(reference);
2027 } else {
2028 PUTS(temp);
2029 }
2030 FREE(temp);
2031
2032 if (author != NULL) {
2033 PUTS(" - ");
2034 if (LYListNewsDates)
2035 START(HTML_I);
2036 PUTS(decode_mime(&author));
2037 if (LYListNewsDates)
2038 END(HTML_I);
2039 FREE(author);
2040 }
2041 if (date) {
2042 if (!diagnostic) {
2043 for (i = 0; date[i]; i++) {
2044 if (date[i] == ' ') {
2045 date[i] = HT_NON_BREAK_SPACE;
2046 }
2047 }
2048 }
2049 sprintf(buffer, " [%.*s]", (int) (sizeof(buffer) - 4), date);
2050 PUTS(buffer);
2051 FREE(date);
2052 }
2053 MAYBE_END(HTML_LI);
2054 /*
2055 * Indicate progress! @@@@@@
2056 */
2057 } else if (status == HT_INTERRUPTED) {
2058 interrupted_in_htgetcharacter = 0;
2059 CTRACE((tfp,
2060 "HTNews: Interrupted on read, closing socket %d\n",
2061 s));
2062 NEWS_NETCLOSE(s);
2063 s = -1;
2064 return (HT_INTERRUPTED);
2065 } else {
2066 /*
2067 * Use the response text on error. - FM
2068 */
2069 PUTC('\n');
2070 START(HTML_LI);
2071 START(HTML_I);
2072 if (LYListNewsNumbers)
2073 LYStrNCpy(buffer, "Status:", sizeof(buffer) - 1);
2074 else
2075 sprintf(buffer, "Status (ARTICLE %d):", art);
2076 PUTS(buffer);
2077 END(HTML_I);
2078 PUTC(' ');
2079 PUTS(response_text);
2080 MAYBE_END(HTML_LI);
2081 } /* Handle response to HEAD request */
2082 } /* Loop over article */
2083 FREE(author);
2084 FREE(subject);
2085 } /* If read headers */
2086 PUTC('\n');
2087 if (LYListNewsNumbers)
2088 END(HTML_OL);
2089 else
2090 END(HTML_UL);
2091 PUTC('\n');
2092
2093 /*
2094 * Link to later articles.
2095 */
2096 if (last_required < last) {
2097 int after; /* End of article after */
2098
2099 after = last_required + HTNewsChunkSize;
2100 if (after == last)
2101 HTSprintf0(&dbuf, "%s%s", NewsHREF, groupName); /* original group */
2102 else
2103 HTSprintf0(&dbuf, "%s%s/%d-%d", NewsHREF, groupName,
2104 last_required + 1, after);
2105 CTRACE((tfp, " Block after is %s\n", dbuf));
2106 PUTC('(');
2107 start_anchor(dbuf);
2108 PUTS(gettext("Later articles"));
2109 END(HTML_A);
2110 PUTS("...)\n");
2111 }
2112
2113 add_post:
2114 if (HTCanPost) {
2115 /*
2116 * We have permission to POST to this host, so add a link for posting
2117 * messages to this newsgroup. - FM
2118 */
2119 char *href = NULL;
2120
2121 START(HTML_HR);
2122 PUTC('\n');
2123 if (!strncasecomp(NewsHREF, STR_SNEWS_URL, 6))
2124 StrAllocCopy(href, "snewspost://");
2125 else
2126 StrAllocCopy(href, "newspost://");
2127 StrAllocCat(href, NewsHost);
2128 StrAllocCat(href, "/");
2129 StrAllocCat(href, groupName);
2130 start_anchor(href);
2131 PUTS(gettext("Post to "));
2132 PUTS(groupName);
2133 END(HTML_A);
2134 FREE(href);
2135 } else {
2136 START(HTML_HR);
2137 }
2138 PUTC('\n');
2139 return (HT_LOADED);
2140 }
2141
2142 /* Load by name. HTLoadNews
2143 * =============
2144 */
2145 static int HTLoadNews(const char *arg,
2146 HTParentAnchor *anAnchor,
2147 HTFormat format_out,
2148 HTStream *stream)
2149 {
2150 char command[262]; /* The whole command */
2151 char proxycmd[260]; /* The proxy command */
2152 char groupName[GROUP_NAME_LENGTH]; /* Just the group name */
2153 int status; /* tcp return */
2154 int retries; /* A count of how hard we have tried */
2155 BOOL normal_url; /* Flag: "news:" or "nntp:" (physical) URL */
2156 BOOL group_wanted; /* Flag: group was asked for, not article */
2157 BOOL list_wanted; /* Flag: list was asked for, not article */
2158 BOOL post_wanted; /* Flag: new post to group was asked for */
2159 BOOL reply_wanted; /* Flag: followup post was asked for */
2160 BOOL spost_wanted; /* Flag: new SSL post to group was asked for */
2161 BOOL sreply_wanted; /* Flag: followup SSL post was asked for */
2162 BOOL head_wanted = NO; /* Flag: want HEAD of single article */
2163 int first, last; /* First and last articles asked for */
2164 char *cp = NULL;
2165 char *ListArg = NULL;
2166 char *ProxyHost = NULL;
2167 char *ProxyHREF = NULL;
2168 char *postfile = NULL;
2169
2170 #ifdef USE_SSL
2171 char SSLprogress[256];
2172 #endif /* USE_SSL */
2173
2174 diagnostic = (format_out == WWW_SOURCE || /* set global flag */
2175 format_out == WWW_DOWNLOAD ||
2176 format_out == WWW_DUMP);
2177 rawtext = NO;
2178
2179 CTRACE((tfp, "HTNews: Looking for %s\n", arg));
2180
2181 if (!initialized)
2182 initialized = initialize();
2183 if (!initialized)
2184 return -1; /* FAIL */
2185
2186 FREE(NewsHREF);
2187 command[0] = '\0';
2188 command[sizeof(command) - 1] = '\0';
2189 proxycmd[0] = '\0';
2190 proxycmd[sizeof(proxycmd) - 1] = '\0';
2191
2192 {
2193 const char *p1;
2194
2195 /*
2196 * We will ask for the document, omitting the host name & anchor.
2197 *
2198 * Syntax of address is
2199 * xxx@yyy Article
2200 * <xxx@yyy> Same article
2201 * xxxxx News group (no "@")
2202 * group/n1-n2 Articles n1 to n2 in group
2203 */
2204 normal_url = (BOOL) (!StrNCmp(arg, STR_NEWS_URL, LEN_NEWS_URL) ||
2205 !StrNCmp(arg, "nntp:", 5));
2206 spost_wanted = (BOOL) (!normal_url && strstr(arg, "snewspost:") != NULL);
2207 sreply_wanted = (BOOL) (!(normal_url || spost_wanted) &&
2208 strstr(arg, "snewsreply:") != NULL);
2209 post_wanted = (BOOL) (!(normal_url || spost_wanted || sreply_wanted) &&
2210 strstr(arg, "newspost:") != NULL);
2211 reply_wanted = (BOOL) (!(normal_url || spost_wanted || sreply_wanted ||
2212 post_wanted) &&
2213 strstr(arg, "newsreply:") != NULL);
2214 group_wanted = (BOOL) ((!(spost_wanted || sreply_wanted ||
2215 post_wanted || reply_wanted) &&
2216 StrChr(arg, '@') == NULL) &&
2217 (StrChr(arg, '*') == NULL));
2218 list_wanted = (BOOL) ((!(spost_wanted || sreply_wanted ||
2219 post_wanted || reply_wanted ||
2220 group_wanted) &&
2221 StrChr(arg, '@') == NULL) &&
2222 (StrChr(arg, '*') != NULL));
2223
2224 #ifndef USE_SSL
2225 if (!strncasecomp(arg, "snewspost:", 10) ||
2226 !strncasecomp(arg, "snewsreply:", 11)) {
2227 HTAlert(FAILED_CANNOT_POST_SSL);
2228 return HT_NOT_LOADED;
2229 }
2230 #endif /* !USE_SSL */
2231 if (post_wanted || reply_wanted || spost_wanted || sreply_wanted) {
2232 /*
2233 * Make sure we have a non-zero path for the newsgroup(s). - FM
2234 */
2235 if ((p1 = strrchr(arg, '/')) != NULL) {
2236 p1++;
2237 } else if ((p1 = strrchr(arg, ':')) != NULL) {
2238 p1++;
2239 }
2240 if (!(p1 && *p1)) {
2241 HTAlert(WWW_ILLEGAL_URL_MESSAGE);
2242 return (HT_NO_DATA);
2243 }
2244 if (!(cp = HTParse(arg, "", PARSE_HOST)) || *cp == '\0') {
2245 if (s >= 0 && NewsHost && strcasecomp(NewsHost, HTNewsHost)) {
2246 NEWS_NETCLOSE(s);
2247 s = -1;
2248 }
2249 StrAllocCopy(NewsHost, HTNewsHost);
2250 } else {
2251 if (s >= 0 && NewsHost && strcasecomp(NewsHost, cp)) {
2252 NEWS_NETCLOSE(s);
2253 s = -1;
2254 }
2255 StrAllocCopy(NewsHost, cp);
2256 }
2257 FREE(cp);
2258 HTSprintf0(&NewsHREF, "%s://%.*s/",
2259 (post_wanted ?
2260 "newspost" :
2261 (reply_wanted ?
2262 "newreply" :
2263 (spost_wanted ?
2264 "snewspost" : "snewsreply"))),
2265 (int) sizeof(command) - 15, NewsHost);
2266
2267 /*
2268 * If the SSL daemon is being used as a proxy, reset p1 to the
2269 * start of the proxied URL rather than to the start of the
2270 * newsgroup(s). - FM
2271 */
2272 if (spost_wanted && strncasecomp(arg, "snewspost:", 10))
2273 p1 = strstr(arg, "snewspost:");
2274 if (sreply_wanted && strncasecomp(arg, "snewsreply:", 11))
2275 p1 = strstr(arg, "snewsreply:");
2276
2277 /* p1 = HTParse(arg, "", PARSE_PATH | PARSE_PUNCTUATION); */
2278 /*
2279 * Don't use HTParse because news: access doesn't follow
2280 * traditional rules. For instance, if the article reference
2281 * contains a '#', the rest of it is lost -- JFG 10/7/92, from a
2282 * bug report
2283 */
2284 } else if (isNNTP_URL(arg)) {
2285 if (((*(arg + 5) == '\0') ||
2286 (!strcmp((arg + 5), "/") ||
2287 !strcmp((arg + 5), "//") ||
2288 !strcmp((arg + 5), "///"))) ||
2289 ((!StrNCmp((arg + 5), "//", 2)) &&
2290 (!(cp = StrChr((arg + 7), '/')) || *(cp + 1) == '\0'))) {
2291 p1 = "*";
2292 group_wanted = FALSE;
2293 list_wanted = TRUE;
2294 } else if (*(arg + 5) != '/') {
2295 p1 = (arg + 5);
2296 } else if (*(arg + 5) == '/' && *(arg + 6) != '/') {
2297 p1 = (arg + 6);
2298 } else {
2299 p1 = (cp ? (cp + 1) : (arg + 6));
2300 }
2301 if (!(cp = HTParse(arg, "", PARSE_HOST)) || *cp == '\0') {
2302 if (s >= 0 && NewsHost && strcasecomp(NewsHost, HTNewsHost)) {
2303 NEWS_NETCLOSE(s);
2304 s = -1;
2305 }
2306 StrAllocCopy(NewsHost, HTNewsHost);
2307 } else {
2308 if (s >= 0 && NewsHost && strcasecomp(NewsHost, cp)) {
2309 NEWS_NETCLOSE(s);
2310 s = -1;
2311 }
2312 StrAllocCopy(NewsHost, cp);
2313 }
2314 FREE(cp);
2315 SnipIn2(command, "%s//%.*s/", STR_NNTP_URL, 9, NewsHost);
2316 StrAllocCopy(NewsHREF, command);
2317 } else if (!strncasecomp(arg, STR_SNEWS_URL, 6)) {
2318 #ifdef USE_SSL
2319 if (((*(arg + 6) == '\0') ||
2320 (!strcmp((arg + 6), "/") ||
2321 !strcmp((arg + 6), "//") ||
2322 !strcmp((arg + 6), "///"))) ||
2323 ((!StrNCmp((arg + 6), "//", 2)) &&
2324 (!(cp = StrChr((arg + 8), '/')) || *(cp + 1) == '\0'))) {
2325 p1 = "*";
2326 group_wanted = FALSE;
2327 list_wanted = TRUE;
2328 } else if (*(arg + 6) != '/') {
2329 p1 = (arg + 6);
2330 } else if (*(arg + 6) == '/' && *(arg + 7) != '/') {
2331 p1 = (arg + 7);
2332 } else {
2333 p1 = (cp ? (cp + 1) : (arg + 7));
2334 }
2335 if (!(cp = HTParse(arg, "", PARSE_HOST)) || *cp == '\0') {
2336 if (s >= 0 && NewsHost && strcasecomp(NewsHost, HTNewsHost)) {
2337 NEWS_NETCLOSE(s);
2338 s = -1;
2339 }
2340 StrAllocCopy(NewsHost, HTNewsHost);
2341 } else {
2342 if (s >= 0 && NewsHost && strcasecomp(NewsHost, cp)) {
2343 NEWS_NETCLOSE(s);
2344 s = -1;
2345 }
2346 StrAllocCopy(NewsHost, cp);
2347 }
2348 FREE(cp);
2349 sprintf(command, "%s//%.250s/", STR_SNEWS_URL, NewsHost);
2350 StrAllocCopy(NewsHREF, command);
2351 #else
2352 HTAlert(gettext("This client does not contain support for SNEWS URLs."));
2353 return HT_NOT_LOADED;
2354 #endif /* USE_SSL */
2355 } else if (!strncasecomp(arg, "news:/", 6)) {
2356 if (((*(arg + 6) == '\0') ||
2357 !strcmp((arg + 6), "/") ||
2358 !strcmp((arg + 6), "//")) ||
2359 ((*(arg + 6) == '/') &&
2360 (!(cp = StrChr((arg + 7), '/')) || *(cp + 1) == '\0'))) {
2361 p1 = "*";
2362 group_wanted = FALSE;
2363 list_wanted = TRUE;
2364 } else if (*(arg + 6) != '/') {
2365 p1 = (arg + 6);
2366 } else {
2367 p1 = (cp ? (cp + 1) : (arg + 6));
2368 }
2369 if (!(cp = HTParse(arg, "", PARSE_HOST)) || *cp == '\0') {
2370 if (s >= 0 && NewsHost && strcasecomp(NewsHost, HTNewsHost)) {
2371 NEWS_NETCLOSE(s);
2372 s = -1;
2373 }
2374 StrAllocCopy(NewsHost, HTNewsHost);
2375 } else {
2376 if (s >= 0 && NewsHost && strcasecomp(NewsHost, cp)) {
2377 NEWS_NETCLOSE(s);
2378 s = -1;
2379 }
2380 StrAllocCopy(NewsHost, cp);
2381 }
2382 FREE(cp);
2383 SnipIn(command, "news://%.*s/", 9, NewsHost);
2384 StrAllocCopy(NewsHREF, command);
2385 } else {
2386 p1 = (arg + 5); /* Skip "news:" prefix */
2387 if (*p1 == '\0') {
2388 p1 = "*";
2389 group_wanted = FALSE;
2390 list_wanted = TRUE;
2391 }
2392 if (s >= 0 && NewsHost && strcasecomp(NewsHost, HTNewsHost)) {
2393 NEWS_NETCLOSE(s);
2394 s = -1;
2395 }
2396 StrAllocCopy(NewsHost, HTNewsHost);
2397 StrAllocCopy(NewsHREF, STR_NEWS_URL);
2398 }
2399
2400 /*
2401 * Set up any proxy for snews URLs that returns NNTP responses for Lynx
2402 * to convert to HTML, instead of doing the conversion itself, and for
2403 * handling posts or followups. - TZ & FM
2404 */
2405 if (!strncasecomp(p1, STR_SNEWS_URL, 6) ||
2406 !strncasecomp(p1, "snewspost:", 10) ||
2407 !strncasecomp(p1, "snewsreply:", 11)) {
2408 StrAllocCopy(ProxyHost, NewsHost);
2409 if ((cp = HTParse(p1, "", PARSE_HOST)) != NULL && *cp != '\0') {
2410 SnipIn2(command, "%s//%.*s", STR_SNEWS_URL, 10, cp);
2411 StrAllocCopy(NewsHost, cp);
2412 } else {
2413 SnipIn2(command, "%s//%.*s", STR_SNEWS_URL, 10, NewsHost);
2414 }
2415 command[sizeof(command) - 2] = '\0';
2416 FREE(cp);
2417 sprintf(proxycmd, "GET %.*s%c%c%c%c",
2418 (int) sizeof(proxycmd) - 9, command,
2419 CR, LF, CR, LF);
2420 CTRACE((tfp, "HTNews: Proxy command is '%.*s'\n",
2421 (int) (strlen(proxycmd) - 4), proxycmd));
2422 strcat(command, "/");
2423 StrAllocCopy(ProxyHREF, NewsHREF);
2424 StrAllocCopy(NewsHREF, command);
2425 if (spost_wanted || sreply_wanted) {
2426 /*
2427 * Reset p1 so that it points to the newsgroup(s).
2428 */
2429 if ((p1 = strrchr(arg, '/')) != NULL) {
2430 p1++;
2431 } else {
2432 p1 = (strrchr(arg, ':') + 1);
2433 }
2434 } else {
2435 const char *cp2;
2436
2437 /*
2438 * Reset p1 so that it points to the newsgroup (or a wildcard),
2439 * or the article.
2440 */
2441 if (!(cp2 = strrchr((p1 + 6), '/')) || *(cp2 + 1) == '\0') {
2442 p1 = "*";
2443 group_wanted = FALSE;
2444 list_wanted = TRUE;
2445 } else {
2446 p1 = (cp2 + 1);
2447 }
2448 }
2449 }
2450
2451 /*
2452 * Set up command for a post, listing, or article request. - FM
2453 */
2454 if (post_wanted || reply_wanted || spost_wanted || sreply_wanted) {
2455 strcpy(command, "POST");
2456 } else if (list_wanted) {
2457 if (strlen(p1) > 249) {
2458 FREE(ProxyHost);
2459 FREE(ProxyHREF);
2460 HTAlert(URL_TOO_LONG);
2461 return -400;
2462 }
2463 SnipIn(command, "XGTITLE %.*s", 11, p1);
2464 } else if (group_wanted) {
2465 char *slash = StrChr(p1, '/');
2466
2467 first = 0;
2468 last = 0;
2469 if (slash) {
2470 *slash = '\0';
2471 if (strlen(p1) >= sizeof(groupName)) {
2472 FREE(ProxyHost);
2473 FREE(ProxyHREF);
2474 HTAlert(URL_TOO_LONG);
2475 return -400;
2476 }
2477 LYStrNCpy(groupName, p1, sizeof(groupName) - 1);
2478 *slash = '/';
2479 (void) sscanf(slash + 1, "%d-%d", &first, &last);
2480 if ((first > 0) && (isdigit(UCH(*(slash + 1)))) &&
2481 (StrChr(slash + 1, '-') == NULL || first == last)) {
2482 /*
2483 * We got a number greater than 0, which will be loaded as
2484 * first, and either no range or the range computes to
2485 * zero, so make last negative, as a flag to select the
2486 * group and then fetch an article by number (first)
2487 * instead of by messageID. - FM
2488 */
2489 last = -1;
2490 }
2491 } else {
2492 if (strlen(p1) >= sizeof(groupName)) {
2493 FREE(ProxyHost);
2494 FREE(ProxyHREF);
2495 HTAlert(URL_TOO_LONG);
2496 return -400;
2497 }
2498 LYStrNCpy(groupName, p1, sizeof(groupName) - 1);
2499 }
2500 SnipIn(command, "GROUP %.*s", 9, groupName);
2501 } else {
2502 size_t add_open = (size_t) (StrChr(p1, '<') == NULL);
2503 size_t add_close = (size_t) (StrChr(p1, '>') == NULL);
2504
2505 if (strlen(p1) + add_open + add_close >= 252) {
2506 FREE(ProxyHost);
2507 FREE(ProxyHREF);
2508 HTAlert(URL_TOO_LONG);
2509 return -400;
2510 }
2511 sprintf(command, "ARTICLE %s%.*s%s",
2512 add_open ? "<" : "",
2513 (int) (sizeof(command) - (11 + add_open + add_close)),
2514 p1,
2515 add_close ? ">" : "");
2516 }
2517
2518 {
2519 char *p = command + strlen(command);
2520
2521 /*
2522 * Terminate command with CRLF, as in RFC 977.
2523 */
2524 *p++ = CR; /* Macros to be correct on Mac */
2525 *p++ = LF;
2526 *p = 0;
2527 }
2528 StrAllocCopy(ListArg, p1);
2529 } /* scope of p1 */
2530
2531 if (!*arg) {
2532 FREE(NewsHREF);
2533 FREE(ProxyHost);
2534 FREE(ProxyHREF);
2535 FREE(ListArg);
2536 return NO; /* Ignore if no name */
2537 }
2538
2539 if (!(post_wanted || reply_wanted || spost_wanted || sreply_wanted ||
2540 (group_wanted && last != -1) || list_wanted)) {
2541 head_wanted = anAnchor->isHEAD;
2542 if (head_wanted && !StrNCmp(command, "ARTICLE ", 8)) {
2543 /* overwrite "ARTICLE" - hack... */
2544 strcpy(command, "HEAD ");
2545 for (cp = command + 5;; cp++)
2546 if ((*cp = *(cp + 3)) == '\0')
2547 break;
2548 }
2549 rawtext = (BOOL) (head_wanted || keep_mime_headers);
2550 }
2551 if (rawtext) {
2552 rawtarget = HTStreamStack(WWW_PLAINTEXT,
2553 format_out,
2554 stream, anAnchor);
2555 if (!rawtarget) {
2556 FREE(NewsHost);
2557 FREE(NewsHREF);
2558 FREE(ProxyHost);
2559 FREE(ProxyHREF);
2560 FREE(ListArg);
2561 HTAlert(gettext("No target for raw text!"));
2562 return (HT_NOT_LOADED);
2563 } /* Copy routine entry points */
2564 rawtargetClass = *rawtarget->isa;
2565 } else
2566 /*
2567 * Make a hypertext object with an anchor list.
2568 */
2569 if (!(post_wanted || reply_wanted || spost_wanted || sreply_wanted)) {
2570 target = HTML_new(anAnchor, format_out, stream);
2571 targetClass = *target->isa; /* Copy routine entry points */
2572 }
2573
2574 /*
2575 * Now, let's get a stream setup up from the NewsHost.
2576 */
2577 for (retries = 0; retries < 2; retries++) {
2578 if (s < 0) {
2579 /* CONNECTING to news host */
2580 char url[260];
2581
2582 if (!strcmp(NewsHREF, STR_NEWS_URL)) {
2583 SnipIn(url, "lose://%.*s/", 9, NewsHost);
2584 } else if (ProxyHREF) {
2585 SnipIn(url, "%.*s", 1, ProxyHREF);
2586 } else {
2587 SnipIn(url, "%.*s", 1, NewsHREF);
2588 }
2589 CTRACE((tfp, "News: doing HTDoConnect on '%s'\n", url));
2590
2591 _HTProgress(gettext("Connecting to NewsHost ..."));
2592
2593 #ifdef USE_SSL
2594 if (!using_proxy &&
2595 (!StrNCmp(arg, STR_SNEWS_URL, 6) ||
2596 !StrNCmp(arg, "snewspost:", 10) ||
2597 !StrNCmp(arg, "snewsreply:", 11)))
2598 status = HTDoConnect(url, "NNTPS", SNEWS_PORT, &s);
2599 else
2600 status = HTDoConnect(url, "NNTP", NEWS_PORT, &s);
2601 #else
2602 status = HTDoConnect(url, "NNTP", NEWS_PORT, &s);
2603 #endif /* USE_SSL */
2604
2605 if (status == HT_INTERRUPTED) {
2606 /*
2607 * Interrupt cleanly.
2608 */
2609 CTRACE((tfp,
2610 "HTNews: Interrupted on connect; recovering cleanly.\n"));
2611 _HTProgress(CONNECTION_INTERRUPTED);
2612 if (!(post_wanted || reply_wanted ||
2613 spost_wanted || sreply_wanted)) {
2614 ABORT_TARGET;
2615 }
2616 FREE(NewsHost);
2617 FREE(NewsHREF);
2618 FREE(ProxyHost);
2619 FREE(ProxyHREF);
2620 FREE(ListArg);
2621 #ifdef USE_SSL
2622 if (Handle) {
2623 SSL_free(Handle);
2624 Handle = NULL;
2625 }
2626 #endif /* USE_SSL */
2627 if (postfile) {
2628 HTSYS_remove(postfile);
2629 FREE(postfile);
2630 }
2631 return HT_NOT_LOADED;
2632 }
2633 if (status < 0) {
2634 NEWS_NETCLOSE(s);
2635 s = -1;
2636 CTRACE((tfp, "HTNews: Unable to connect to news host.\n"));
2637 if (retries < 1)
2638 continue;
2639 if (!(post_wanted || reply_wanted ||
2640 spost_wanted || sreply_wanted)) {
2641 ABORT_TARGET;
2642 }
2643 HTSprintf0(&dbuf, LY_MSG("Could not access %s."), NewsHost);
2644 FREE(NewsHost);
2645 FREE(NewsHREF);
2646 FREE(ProxyHost);
2647 FREE(ProxyHREF);
2648 FREE(ListArg);
2649 if (postfile) {
2650 HTSYS_remove(postfile);
2651 FREE(postfile);
2652 }
2653 return HTLoadError(stream, 500, dbuf);
2654 } else {
2655 CTRACE((tfp, "HTNews: Connected to news host %s.\n",
2656 NewsHost));
2657 #ifdef USE_SSL
2658 /*
2659 * If this is an snews url, then do the SSL stuff here
2660 */
2661 if (!using_proxy &&
2662 (!StrNCmp(url, "snews", 5) ||
2663 !StrNCmp(url, "snewspost:", 10) ||
2664 !StrNCmp(url, "snewsreply:", 11))) {
2665 Handle = HTGetSSLHandle();
2666 SSL_set_fd(Handle, s);
2667 HTSSLInitPRNG();
2668 status = SSL_connect(Handle);
2669
2670 if (status <= 0) {
2671 unsigned long SSLerror;
2672
2673 CTRACE((tfp,
2674 "HTNews: Unable to complete SSL handshake for '%s', SSL_connect=%d, SSL error stack dump follows\n",
2675 url, status));
2676 SSL_load_error_strings();
2677 while ((SSLerror = ERR_get_error()) != 0) {
2678 CTRACE((tfp, "HTNews: SSL: %s\n",
2679 ERR_error_string(SSLerror, NULL)));
2680 }
2681 HTAlert("Unable to make secure connection to remote host.");
2682 NEWS_NETCLOSE(s);
2683 s = -1;
2684 if (!(post_wanted || reply_wanted ||
2685 spost_wanted || sreply_wanted))
2686 (*targetClass._abort) (target, NULL);
2687 FREE(NewsHost);
2688 FREE(NewsHREF);
2689 FREE(ProxyHost);
2690 FREE(ProxyHREF);
2691 FREE(ListArg);
2692 if (postfile) {
2693 #ifdef VMS
2694 while (remove(postfile) == 0) ; /* loop through all versions */
2695 #else
2696 remove(postfile);
2697 #endif /* VMS */
2698 FREE(postfile);
2699 }
2700 return HT_NOT_LOADED;
2701 }
2702 sprintf(SSLprogress,
2703 "Secure %d-bit %s (%s) NNTP connection",
2704 SSL_get_cipher_bits(Handle, NULL),
2705 SSL_get_cipher_version(Handle),
2706 SSL_get_cipher(Handle));
2707 _HTProgress(SSLprogress);
2708 }
2709 #endif /* USE_SSL */
2710 HTInitInput(s); /* set up buffering */
2711 if (proxycmd[0]) {
2712 status = (int) NEWS_NETWRITE(s, proxycmd, (int) strlen(proxycmd));
2713 CTRACE((tfp,
2714 "HTNews: Proxy command returned status '%d'.\n",
2715 status));
2716 }
2717 if (((status = response(NULL)) / 100) != 2) {
2718 NEWS_NETCLOSE(s);
2719 s = -1;
2720 if (status == HT_INTERRUPTED) {
2721 _HTProgress(CONNECTION_INTERRUPTED);
2722 if (!(post_wanted || reply_wanted ||
2723 spost_wanted || sreply_wanted)) {
2724 ABORT_TARGET;
2725 }
2726 FREE(NewsHost);
2727 FREE(NewsHREF);
2728 FREE(ProxyHost);
2729 FREE(ProxyHREF);
2730 FREE(ListArg);
2731 if (postfile) {
2732 HTSYS_remove(postfile);
2733 FREE(postfile);
2734 }
2735 return (HT_NOT_LOADED);
2736 }
2737 if (retries < 1)
2738 continue;
2739 FREE(ProxyHost);
2740 FREE(ProxyHREF);
2741 FREE(ListArg);
2742 FREE(postfile);
2743 if (!(post_wanted || reply_wanted ||
2744 spost_wanted || sreply_wanted)) {
2745 ABORT_TARGET;
2746 }
2747 if (response_text[0]) {
2748 HTSprintf0(&dbuf,
2749 LY_MSG("Can't read news info. News host %.20s responded: %.200s"),
2750 NewsHost, response_text);
2751 } else {
2752 HTSprintf0(&dbuf,
2753 LY_MSG("Can't read news info, empty response from host %s"),
2754 NewsHost);
2755 }
2756 return HTLoadError(stream, 500, dbuf);
2757 }
2758 if (status == 200) {
2759 HTCanPost = TRUE;
2760 } else {
2761 HTCanPost = FALSE;
2762 if (post_wanted || reply_wanted ||
2763 spost_wanted || sreply_wanted) {
2764 HTAlert(CANNOT_POST);
2765 FREE(NewsHREF);
2766 if (ProxyHREF) {
2767 StrAllocCopy(NewsHost, ProxyHost);
2768 FREE(ProxyHost);
2769 FREE(ProxyHREF);
2770 }
2771 FREE(ListArg);
2772 if (postfile) {
2773 HTSYS_remove(postfile);
2774 FREE(postfile);
2775 }
2776 return (HT_NOT_LOADED);
2777 }
2778 }
2779 }
2780 }
2781 /* If needed opening */
2782 if (post_wanted || reply_wanted ||
2783 spost_wanted || sreply_wanted) {
2784 if (!HTCanPost) {
2785 HTAlert(CANNOT_POST);
2786 FREE(NewsHREF);
2787 if (ProxyHREF) {
2788 StrAllocCopy(NewsHost, ProxyHost);
2789 FREE(ProxyHost);
2790 FREE(ProxyHREF);
2791 }
2792 FREE(ListArg);
2793 if (postfile) {
2794 HTSYS_remove(postfile);
2795 FREE(postfile);
2796 }
2797 return (HT_NOT_LOADED);
2798 }
2799 if (postfile == NULL) {
2800 postfile = LYNewsPost(ListArg,
2801 (reply_wanted || sreply_wanted));
2802 }
2803 if (postfile == NULL) {
2804 HTProgress(CANCELLED);
2805 FREE(NewsHREF);
2806 if (ProxyHREF) {
2807 StrAllocCopy(NewsHost, ProxyHost);
2808 FREE(ProxyHost);
2809 FREE(ProxyHREF);
2810 }
2811 FREE(ListArg);
2812 return (HT_NOT_LOADED);
2813 }
2814 } else {
2815 /*
2816 * Ensure reader mode, but don't bother checking the status for
2817 * anything but HT_INTERRUPTED or a 480 Authorization request,
2818 * because if the reader mode command is not needed, the server
2819 * probably returned a 500, which is irrelevant at this point. -
2820 * FM
2821 */
2822 char buffer[20];
2823
2824 sprintf(buffer, "mode reader%c%c", CR, LF);
2825 if ((status = response(buffer)) == HT_INTERRUPTED) {
2826 _HTProgress(CONNECTION_INTERRUPTED);
2827 break;
2828 }
2829 if (status == 480) {
2830 NNTPAuthResult auth_result = HTHandleAuthInfo(NewsHost);
2831
2832 if (auth_result == NNTPAUTH_CLOSE) {
2833 if (s != -1 && !(ProxyHost || ProxyHREF)) {
2834 NEWS_NETCLOSE(s);
2835 s = -1;
2836 }
2837 }
2838 if (auth_result != NNTPAUTH_OK) {
2839 break;
2840 }
2841 if (response(buffer) == HT_INTERRUPTED) {
2842 _HTProgress(CONNECTION_INTERRUPTED);
2843 break;
2844 }
2845 }
2846 }
2847
2848 Send_NNTP_command:
2849 #ifdef NEWS_DEB
2850 if (postfile)
2851 printf("postfile = %s, command = %s", postfile, command);
2852 else
2853 printf("command = %s", command);
2854 #endif
2855 if ((status = response(command)) == HT_INTERRUPTED) {
2856 _HTProgress(CONNECTION_INTERRUPTED);
2857 break;
2858 }
2859 if (status < 0) {
2860 if (retries < 1) {
2861 continue;
2862 } else {
2863 break;
2864 }
2865 }
2866 /*
2867 * For some well known error responses which are expected to occur in
2868 * normal use, break from the loop without retrying and without closing
2869 * the connection. It is unlikely that these are leftovers from a
2870 * timed-out connection (but we do some checks to see whether the
2871 * response corresponds to the last command), or that they will give
2872 * anything else when automatically retried. - kw
2873 */
2874 if (status == 411 && group_wanted &&
2875 !StrNCmp(command, "GROUP ", 6) &&
2876 !strncasecomp(response_text + 3, " No such group ", 15) &&
2877 !strcmp(response_text + 18, groupName)) {
2878
2879 HTAlert(response_text);
2880 break;
2881 } else if (status == 430 && !group_wanted && !list_wanted &&
2882 !StrNCmp(command, "ARTICLE <", 9) &&
2883 !strcasecomp(response_text + 3, " No such article")) {
2884
2885 HTAlert(response_text);
2886 break;
2887 }
2888 if ((status / 100) != 2 &&
2889 status != 340 &&
2890 status != 480) {
2891 if (retries) {
2892 if (list_wanted && !StrNCmp(command, "XGTITLE", 7)) {
2893 sprintf(command, "LIST NEWSGROUPS%c%c", CR, LF);
2894 goto Send_NNTP_command;
2895 }
2896 HTAlert(response_text);
2897 } else {
2898 _HTProgress(response_text);
2899 }
2900 NEWS_NETCLOSE(s);
2901 s = -1;
2902 /*
2903 * Message might be a leftover "Timeout-disconnected", so try again
2904 * if the retries maximum has not been reached.
2905 */
2906 continue;
2907 }
2908
2909 /*
2910 * Post or load a group, article, etc
2911 */
2912 if (status == 480) {
2913 NNTPAuthResult auth_result;
2914
2915 /*
2916 * Some servers return 480 for a failed XGTITLE. - FM
2917 */
2918 if (list_wanted && !StrNCmp(command, "XGTITLE", 7) &&
2919 strstr(response_text, "uthenticat") == NULL &&
2920 strstr(response_text, "uthor") == NULL) {
2921 sprintf(command, "LIST NEWSGROUPS%c%c", CR, LF);
2922 goto Send_NNTP_command;
2923 }
2924 /*
2925 * Handle Authorization. - FM
2926 */
2927 if ((auth_result = HTHandleAuthInfo(NewsHost)) == NNTPAUTH_OK) {
2928 goto Send_NNTP_command;
2929 } else if (auth_result == NNTPAUTH_CLOSE) {
2930 if (s != -1 && !(ProxyHost || ProxyHREF)) {
2931 NEWS_NETCLOSE(s);
2932 s = -1;
2933 }
2934 if (retries < 1)
2935 continue;
2936 }
2937 status = HT_NOT_LOADED;
2938 } else if (post_wanted || reply_wanted ||
2939 spost_wanted || sreply_wanted) {
2940 /*
2941 * Handle posting of an article. - FM
2942 */
2943 if (status != 340) {
2944 HTAlert(CANNOT_POST);
2945 if (postfile) {
2946 HTSYS_remove(postfile);
2947 }
2948 } else {
2949 post_article(postfile);
2950 }
2951 FREE(postfile);
2952 status = HT_NOT_LOADED;
2953 } else if (list_wanted) {
2954 /*
2955 * List available newsgroups. - FM
2956 */
2957 _HTProgress(gettext("Reading list of available newsgroups."));
2958 status = read_list(ListArg);
2959 } else if (group_wanted) {
2960 /*
2961 * List articles in a news group. - FM
2962 */
2963 if (last < 0) {
2964 /*
2965 * We got one article number rather than a range following the
2966 * slash which followed the group name, or the range was zero,
2967 * so now that we have selected that group, load ARTICLE and
2968 * the the number (first) as the command and go back to send it
2969 * and check the response. - FM
2970 */
2971 sprintf(command, "%s %d%c%c",
2972 head_wanted ? "HEAD" : "ARTICLE",
2973 first, CR, LF);
2974 group_wanted = FALSE;
2975 retries = 2;
2976 goto Send_NNTP_command;
2977 }
2978 _HTProgress(gettext("Reading list of articles in newsgroup."));
2979 status = read_group(groupName, first, last);
2980 } else {
2981 /*
2982 * Get an article from a news group. - FM
2983 */
2984 _HTProgress(gettext("Reading news article."));
2985 status = read_article(anAnchor);
2986 }
2987 if (status == HT_INTERRUPTED) {
2988 _HTProgress(CONNECTION_INTERRUPTED);
2989 status = HT_LOADED;
2990 }
2991 if (!(post_wanted || reply_wanted ||
2992 spost_wanted || sreply_wanted)) {
2993 if (status == HT_NOT_LOADED) {
2994 ABORT_TARGET;
2995 } else {
2996 FREE_TARGET;
2997 }
2998 }
2999 FREE(NewsHREF);
3000 if (ProxyHREF) {
3001 StrAllocCopy(NewsHost, ProxyHost);
3002 FREE(ProxyHost);
3003 FREE(ProxyHREF);
3004 }
3005 FREE(ListArg);
3006 if (postfile) {
3007 HTSYS_remove(postfile);
3008 FREE(postfile);
3009 }
3010 return status;
3011 } /* Retry loop */
3012
3013 #if 0
3014 HTAlert(gettext("Sorry, could not load requested news."));
3015 NXRunAlertPanel(NULL, "Sorry, could not load `%s'.", NULL, NULL, NULL, arg);
3016 /* No -- message earlier will have covered it */
3017 #endif
3018
3019 if (!(post_wanted || reply_wanted ||
3020 spost_wanted || sreply_wanted)) {
3021 ABORT_TARGET;
3022 }
3023 FREE(NewsHREF);
3024 if (ProxyHREF) {
3025 StrAllocCopy(NewsHost, ProxyHost);
3026 FREE(ProxyHost);
3027 FREE(ProxyHREF);
3028 }
3029 FREE(ListArg);
3030 if (postfile) {
3031 HTSYS_remove(postfile);
3032 FREE(postfile);
3033 }
3034 return HT_NOT_LOADED;
3035 }
3036
3037 /*
3038 * This function clears all authorization information by
3039 * invoking the free_NNTP_AuthInfo() function, which normally
3040 * is invoked at exit. It allows a browser command to do
3041 * this at any time, for example, if the user is leaving
3042 * the terminal for a period of time, but does not want
3043 * to end the current session. - FM
3044 */
3045 void HTClearNNTPAuthInfo(void)
3046 {
3047 /*
3048 * Need code to check cached documents and do something to ensure that any
3049 * protected documents no longer can be accessed without a new retrieval.
3050 * - FM
3051 */
3052
3053 /*
3054 * Now free all of the authorization info. - FM
3055 */
3056 free_NNTP_AuthInfo();
3057 }
3058
3059 #ifdef USE_SSL
3060 static int HTNewsGetCharacter(void)
3061 {
3062 if (!Handle)
3063 return HTGetCharacter();
3064 else
3065 return HTGetSSLCharacter((void *) Handle);
3066 }
3067
3068 int HTNewsProxyConnect(int sock,
3069 const char *url,
3070 HTParentAnchor *anAnchor,
3071 HTFormat format_out,
3072 HTStream *sink)
3073 {
3074 int status;
3075 const char *arg = url;
3076 char SSLprogress[256];
3077
3078 s = channel_s = sock;
3079 Handle = HTGetSSLHandle();
3080 SSL_set_fd(Handle, s);
3081 HTSSLInitPRNG();
3082 status = SSL_connect(Handle);
3083
3084 if (status <= 0) {
3085 unsigned long SSLerror;
3086
3087 channel_s = -1;
3088 CTRACE((tfp,
3089 "HTNews: Unable to complete SSL handshake for '%s', SSL_connect=%d, SSL error stack dump follows\n",
3090 url, status));
3091 SSL_load_error_strings();
3092 while ((SSLerror = ERR_get_error()) != 0) {
3093 CTRACE((tfp, "HTNews: SSL: %s\n", ERR_error_string(SSLerror, NULL)));
3094 }
3095 HTAlert("Unable to make secure connection to remote host.");
3096 NEWS_NETCLOSE(s);
3097 s = -1;
3098 return HT_NOT_LOADED;
3099 }
3100 sprintf(SSLprogress, "Secure %d-bit %s (%s) NNTP connection",
3101 SSL_get_cipher_bits(Handle, NULL),
3102 SSL_get_cipher_version(Handle),
3103 SSL_get_cipher(Handle));
3104 _HTProgress(SSLprogress);
3105 status = HTLoadNews(arg, anAnchor, format_out, sink);
3106 channel_s = -1;
3107 return status;
3108 }
3109 #endif /* USE_SSL */
3110
3111 #ifdef GLOBALDEF_IS_MACRO
3112 #define _HTNEWS_C_1_INIT { "news", HTLoadNews, NULL }
3113 GLOBALDEF(HTProtocol, HTNews, _HTNEWS_C_1_INIT);
3114 #define _HTNEWS_C_2_INIT { "nntp", HTLoadNews, NULL }
3115 GLOBALDEF(HTProtocol, HTNNTP, _HTNEWS_C_2_INIT);
3116 #define _HTNEWS_C_3_INIT { "newspost", HTLoadNews, NULL }
3117 GLOBALDEF(HTProtocol, HTNewsPost, _HTNEWS_C_3_INIT);
3118 #define _HTNEWS_C_4_INIT { "newsreply", HTLoadNews, NULL }
3119 GLOBALDEF(HTProtocol, HTNewsReply, _HTNEWS_C_4_INIT);
3120 #define _HTNEWS_C_5_INIT { "snews", HTLoadNews, NULL }
3121 GLOBALDEF(HTProtocol, HTSNews, _HTNEWS_C_5_INIT);
3122 #define _HTNEWS_C_6_INIT { "snewspost", HTLoadNews, NULL }
3123 GLOBALDEF(HTProtocol, HTSNewsPost, _HTNEWS_C_6_INIT);
3124 #define _HTNEWS_C_7_INIT { "snewsreply", HTLoadNews, NULL }
3125 GLOBALDEF(HTProtocol, HTSNewsReply, _HTNEWS_C_7_INIT);
3126 #else
3127 GLOBALDEF HTProtocol HTNews =
3128 {"news", HTLoadNews, NULL};
3129 GLOBALDEF HTProtocol HTNNTP =
3130 {"nntp", HTLoadNews, NULL};
3131 GLOBALDEF HTProtocol HTNewsPost =
3132 {"newspost", HTLoadNews, NULL};
3133 GLOBALDEF HTProtocol HTNewsReply =
3134 {"newsreply", HTLoadNews, NULL};
3135 GLOBALDEF HTProtocol HTSNews =
3136 {"snews", HTLoadNews, NULL};
3137 GLOBALDEF HTProtocol HTSNewsPost =
3138 {"snewspost", HTLoadNews, NULL};
3139 GLOBALDEF HTProtocol HTSNewsReply =
3140 {"snewsreply", HTLoadNews, NULL};
3141 #endif /* GLOBALDEF_IS_MACRO */
3142
3143 #endif /* not DISABLE_NEWS */