1 /*
2  * $LynxId: LYMail.c,v 1.94 2013/05/05 21:41:23 tom Exp $
3  */
4 #include <HTUtils.h>
5 #include <HTParse.h>
6 #include <LYGlobalDefs.h>
7 #include <HTAlert.h>
8 #include <LYCurses.h>
9 #include <LYSignal.h>
10 #include <LYUtils.h>
11 #include <LYClean.h>
12 #include <LYStrings.h>
13 #include <GridText.h>
14 #include <LYMail.h>
15 #include <LYEdit.h>
16 #include <LYCharSets.h>		/* to get current charset for mail header */
17 
18 #include <LYLeaks.h>
19 
20 #define MAX_SUBJECT 70
21 
22 BOOLEAN term_letter;		/* Global variable for async i/o. */
23 
terminate_letter(int sig GCC_UNUSED)24 static void terminate_letter(int sig GCC_UNUSED)
25 {
26     term_letter = TRUE;
27     /* Reassert the AST */
28     signal(SIGINT, terminate_letter);
29 #if USE_VMS_MAILER || defined(PDCURSES)
30     /*
31      * Refresh the screen to get rid of the "interrupt" message.
32      */
33     if (!dump_output_immediately) {
34 	lynx_force_repaint();
35 	LYrefresh();
36     }
37 #endif /* VMS */
38 }
39 
40 /* HTUnEscape with control-code nuking */
SafeHTUnEscape(char * string)41 static void SafeHTUnEscape(char *string)
42 {
43     int i;
44     int flg = FALSE;
45 
46     HTUnEscape(string);
47     for (i = 0; string[i] != '\0'; i++) {
48 	/* FIXME: this is no longer explicitly 7-bit ASCII,
49 	   but are there portability problems? */
50 	if ((!LYIsASCII(string[i])) || !isprint(UCH(string[i]))) {
51 	    string[i] = '?';
52 	    flg = TRUE;
53 	}
54     }
55     if (flg)
56 	HTAlert(MAILTO_SQUASH_CTL);
57 }
58 
remove_tildes(char * string)59 static void remove_tildes(char *string)
60 {
61     /*
62      * Change the first character to a space if it is a '~'.
63      */
64     if (*string == '~')
65 	*string = ' ';
66 }
67 
comma_append(char ** dst,char * src)68 static void comma_append(char **dst,
69 			 char *src)
70 {
71     if (*src) {
72 	while (*src == ',' || isspace(UCH(*src)))
73 	    src++;
74 	if (*src) {
75 	    if (isEmpty(*dst)) {
76 		StrAllocCopy(*dst, src);
77 	    } else {
78 		StrAllocCat(*dst, ",");
79 		StrAllocCat(*dst, src);
80 	    }
81 	}
82     }
83 }
84 
extract_field(char ** dst,char * src,const char * keyword)85 static void extract_field(char **dst,
86 			  char *src,
87 			  const char *keyword)
88 {
89     int len = (int) strlen(keyword);
90     char *cp, *cp1;
91 
92     cp = (src + 1);
93     while (*cp != '\0') {
94 	if ((*(cp - 1) == '?' || *(cp - 1) == '&') &&
95 	    !strncasecomp(cp, keyword, len)) {
96 	    cp += len;
97 	    if ((cp1 = strchr(cp, '&')) != NULL) {
98 		*cp1 = '\0';
99 	    }
100 	    comma_append(dst, cp);
101 	    if (cp1) {
102 		*cp1 = '&';
103 		cp = cp1;
104 		cp1 = NULL;
105 	    } else {
106 		break;
107 	    }
108 	}
109 	cp++;
110     }
111     CTRACE((tfp, "extract_field(%s) = '%s'\n", keyword, *dst));
112 }
113 
114 /*
115  * Seek and handle a subject=foo.  - FM
116  */
extract_subject(char * dst,char * src)117 static void extract_subject(char *dst,
118 			    char *src)
119 {
120     const char *keyword = "subject=";
121     int len = (int) strlen(keyword);
122     char *cp, *cp1;
123 
124     cp = (src + 1);
125     while (*cp != '\0') {
126 	if ((*(cp - 1) == '?' || *(cp - 1) == '&') &&
127 	    !strncasecomp(cp, keyword, len))
128 	    break;
129 	cp++;
130     }
131     if (*cp) {
132 	cp += len;
133 	if ((cp1 = strchr(cp, '&')) != NULL) {
134 	    *cp1 = '\0';
135 	}
136 	if (*cp) {
137 	    LYStrNCpy(dst, cp, MAX_SUBJECT);
138 	    SafeHTUnEscape(dst);
139 	}
140 	if (cp1) {
141 	    *cp1 = '&';
142 	    cp1 = NULL;
143 	}
144     }
145     CTRACE((tfp, "extract_subject(%s) = '%s'\n", keyword, NONNULL(dst)));
146 }
147 
148 /*
149  * Seek and handle body=foo fields.  - FM
150  */
extract_body(char ** dst,char * src)151 static void extract_body(char **dst,
152 			 char *src)
153 {
154     const char *keyword = "body=";
155     int len = (int) strlen(keyword);
156     int i;
157     char *cp, *cp0, *cp1, *temp = 0;
158 
159     cp = (src + 1);
160     while (*cp != '\0') {
161 	if ((*(cp - 1) == '?' || *(cp - 1) == '&') &&
162 	    !strncasecomp(cp, keyword, len)) {
163 	    cp += len;
164 	    if ((cp1 = strchr(cp, '&')) != NULL) {
165 		*cp1 = '\0';
166 	    }
167 	    if (*cp) {
168 		/*
169 		 * Break up the value into lines with a maximum length of 78.
170 		 * - FM
171 		 */
172 		StrAllocCopy(temp, cp);
173 		HTUnEscape(temp);
174 		cp0 = temp;
175 		while ((cp = strchr(cp0, '\n')) != NULL) {
176 		    *cp = '\0';
177 		    if (cp > cp0) {
178 			if (*(cp - 1) == '\r') {
179 			    *(cp - 1) = '\0';
180 			}
181 		    }
182 		    i = 0;
183 		    len = (int) strlen(cp0);
184 		    while (len > 78) {
185 			HTSprintf(dst, "%.78s\n", &cp0[i]);
186 			i += 78;
187 			len = (int) strlen(&cp0[i]);
188 		    }
189 		    HTSprintf(dst, "%s\n", &cp0[i]);
190 		    cp0 = (cp + 1);
191 		}
192 		i = 0;
193 		len = (int) strlen(cp0);
194 		while (len > 78) {
195 		    HTSprintf(dst, "%.78s\n", &cp0[i]);
196 		    i += 78;
197 		    len = (int) strlen(&cp0[i]);
198 		}
199 		if (len) {
200 		    HTSprintf(dst, "%s\n", &cp0[i]);
201 		}
202 		FREE(temp);
203 	    }
204 	    if (cp1) {
205 		*cp1 = '&';
206 		cp = cp1;
207 		cp1 = NULL;
208 	    } else {
209 		break;
210 	    }
211 	}
212 	cp++;
213     }
214     CTRACE((tfp, "extract_body(%s) = '%s'\n", keyword, NONNULL(*dst)));
215 }
216 
217 /*
218  * Convert any Explorer semi-colon Internet address separators to commas - FM
219  */
trim_comma(char * address)220 static BOOLEAN trim_comma(char *address)
221 {
222     if (address[(strlen(address) - 1)] == ',')
223 	address[(strlen(address) - 1)] = '\0';
224     return (BOOL) (*address == '\0');
225 }
226 
227 /*
228  * Convert any Explorer semi-colon Internet address separators to commas - FM
229  */
convert_explorer(char * address)230 static BOOLEAN convert_explorer(char *address)
231 {
232     char *cp = address;
233     char *cp0;
234     char *cp1;
235 
236     while ((cp1 = strchr(cp, '@')) != NULL) {
237 	cp1++;
238 	if ((cp0 = strchr(cp1, ';')) != NULL) {
239 	    *cp0 = ',';
240 	    cp1 = cp0 + 1;
241 	}
242 	cp = cp1;
243     }
244     return trim_comma(address);
245 }
246 
247 /*
248  * reply_by_mail() prompts line-by-line for header information, allowing
249  * scrolling of the screen.
250  */
header_prompt(const char * label,char ** result,unsigned limit)251 static int header_prompt(const char *label,
252 			 char **result,
253 			 unsigned limit)
254 {
255     char buffer[LINESIZE];
256     int ok;
257 
258     if (*result != 0) {
259 	LYaddstr(CTRL_U_TO_ERASE);
260 	LYStrNCpy(buffer, *result, sizeof(buffer) - 1);
261     } else
262 	*buffer = 0;
263 
264     if (limit > sizeof(buffer))
265 	limit = sizeof(buffer);
266 
267     LYaddstr(gettext(label));
268     LYaddstr(": ");
269     ok = (LYGetStr(buffer, VISIBLE, limit, NORECALL) >= 0
270 	  && !term_letter);
271     LYaddstr("\n");
272 
273     if (ok) {
274 	remove_tildes(buffer);
275 	StrAllocCopy(*result, buffer);
276     }
277     term_letter = FALSE;
278     return ok;
279 }
280 
show_addresses(char * addresses)281 static void show_addresses(char *addresses)
282 {
283     char *cp = addresses;
284     char *cp1;
285 
286     while ((cp1 = strchr(cp, ',')) != NULL) {
287 	*cp1 = '\0';
288 	while (*cp == ' ')
289 	    cp++;
290 	if (*cp) {
291 	    LYaddstr(cp);
292 	    LYaddstr(",\n  ");
293 	}
294 	*cp1 = ',';
295 	cp = (cp1 + 1);
296     }
297     if (*cp) {
298 	LYaddstr(cp);
299     }
300 }
301 
302 #if USE_BLAT_MAILER
303 
304 /*
305  * blat's options-file parser (see makeargv.cpp) treats backslash and double
306  * quote characters specially.  lynx doesn't.  Do a conversion as we write the
307  * option.
308  *
309  * Other quirks (reading blat 3.06):
310  * + Whitespace not in quotes terminates a line.
311  * + Blat allows a comment-character to terminate a line.  By default, that
312  *   is a semicolon.
313  *
314  * Given that, the simplest thing to do is to always quote the string, using
315  * escaping to handle special cases.
316  */
blat_option(FILE * fp,const char * option,const char * value)317 static void blat_option(FILE *fp, const char *option, const char *value)
318 {
319     if (non_empty(value)) {
320 	char *result = malloc(strlen(option) + 4 + 4 * strlen(value));
321 	char *working = result;
322 
323 	CTRACE((tfp, "blat_option(opt=\"%s\", value=\"%s\")\n", option, value));
324 	sprintf(working, "%s \"", option);
325 	working += strlen(working);
326 
327 	while (*value != '\0') {
328 	    unsigned ch = UCH(*value);
329 
330 	    switch (ch) {
331 	    case '\\':
332 		*working++ = '\\';
333 		*working++ = '\\';
334 		break;
335 	    case '"':
336 		*working++ = '\\';
337 		*working++ = '"';
338 		break;
339 	    default:
340 		if (ch < ' ' || ch > '~') {
341 		    sprintf(working, "\\%03o", ch);
342 		} else {
343 		    *working++ = ch;
344 		}
345 		break;
346 	    }
347 	    ++value;
348 	}
349 	*working++ = '"';
350 	*working++ = '\n';
351 	*working = 0;
352 
353 	CTRACE((tfp, "->%s", result));
354 	fputs(result, fp);
355 	FREE(result);
356     }
357 }
358 
359 /*
360 syntax for blat 2.6.2:
361 Blat <filename> -t <recipient> [optional switches (see below)]
362 
363 -bodyF <filename> : file with the message body
364 -t <recipient>    : recipient list (comma separated)
365 -s <subj>         : subject line
366 -f <sender>       : overrides the default sender address (must be known to server)
367 -i <addr>         : a 'From:' address, not necessarily known to the SMTP server.
368 -c <recipient>    : carbon copy recipient list (comma separated)
369 -b <recipient>    : blind carbon copy recipient list (comma separated)
370 -help             : displays the help message.
371 -mime             : MIME Quoted-Printable Content-Transfer-Encoding.
372 -q                : supresses *all* output.
373 -server <addr>    : overrides the default SMTP server to be used.
374 
375 */
376 
blat_cmd(char * filename,char * address,char * subject,char * ccaddr,char * mail_addr)377 static char *blat_cmd(char *filename,
378 		      char *address,
379 		      char *subject,
380 		      char *ccaddr,
381 		      char *mail_addr)
382 {
383     char *b_cmd = NULL;
384 
385     if (mail_is_altblat) {
386 	const char *format = "%s %s -t %s -s %s %s%s%s";
387 
388 	HTAddParam(&b_cmd, format, 1, ALTBLAT_MAIL);
389 	HTAddParam(&b_cmd, format, 2, filename);
390 	HTAddParam(&b_cmd, format, 3, address);
391 	HTAddParam(&b_cmd, format, 4, subject);
392 	HTAddToCmd(&b_cmd, format, 5, ALTBLAT_MAIL_FLAGS);
393 	if (non_empty(ccaddr)) {
394 	    HTAddToCmd(&b_cmd, format, 6, " -c ");
395 	    HTAddParam(&b_cmd, format, 7, NonNull(ccaddr));
396 	}
397 	HTEndParam(&b_cmd, format, 8);
398 
399     } else {
400 
401 	const char *format = "%s -of %s";
402 	char bl_cmd_file[LY_MAXPATH];
403 	FILE *fp;
404 
405 #ifdef __CYGWIN__
406 	char dosname[LY_MAXPATH];
407 
408 #else
409 	char *dosname;
410 #endif
411 
412 	bl_cmd_file[0] = '\0';
413 	if ((fp = LYOpenTemp(bl_cmd_file, ".blt", "w")) == NULL) {
414 	    HTAlert(FORM_MAILTO_FAILED);
415 	    return NULL;
416 	}
417 
418 	HTAddParam(&b_cmd, format, 1, BLAT_MAIL);
419 
420 	ConvertToWin32Path(filename, dosname);
421 	blat_option(fp, "-bodyF", dosname);
422 	blat_option(fp, "-t", address);
423 	blat_option(fp, "-s", subject);
424 	blat_option(fp, "-f", mail_addr);
425 	blat_option(fp, "-c", ccaddr);
426 	LYCloseOutput(fp);
427 
428 	ConvertToWin32Path(bl_cmd_file, dosname);
429 
430 	HTAddParam(&b_cmd, format, 2, dosname);
431 	HTEndParam(&b_cmd, format, 3);
432 
433     }
434 
435     return b_cmd;
436 }
437 
438 #endif /* USE_BLAT_MAILER */
439 
440 #if USE_VMS_MAILER
LYMailPMDF(void)441 BOOLEAN LYMailPMDF(void)
442 {
443     return (system_mail != 0)
444 	? !strncasecomp(system_mail, "PMDF SEND", 9)
445 	: FALSE;
446 }
447 
448 /*
449  * Add all of the people in the address field to the command
450  */
vms_append_addrs(char ** cmd,char * address,char * option)451 static void vms_append_addrs(char **cmd, char *address, char *option)
452 {
453     BOOLEAN first = TRUE;
454     char *cp;
455     char *address_ptr1;
456     char *address_ptr2;
457 
458     address_ptr1 = address;
459     do {
460 	if ((cp = strchr(address_ptr1, ',')) != NULL) {
461 	    address_ptr2 = (cp + 1);
462 	    *cp = '\0';
463 	} else {
464 	    address_ptr2 = NULL;
465 	}
466 
467 	/*
468 	 * 4 letters is arbitrarily the smallest possible mail address, at
469 	 * least for lynx.  That way extra spaces won't confuse the mailer and
470 	 * give a blank address.
471 	 */
472 	if (strlen(address_ptr1) > 3) {
473 	    if (!first) {
474 		StrAllocCat(*cmd, ",");
475 	    }
476 	    HTSprintf(cmd, mail_adrs, address_ptr1);
477 	    if (*option && LYMailPMDF())
478 		StrAllocCat(*cmd, option);
479 	    first = FALSE;
480 	}
481 	address_ptr1 = address_ptr2;
482     } while (address_ptr1 != NULL);
483 }
484 
remove_quotes(char * string)485 static void remove_quotes(char *string)
486 {
487     while (*string != 0) {
488 	if (strchr("\"&|", *string) != 0)
489 	    *string = ' ';
490 	string++;
491     }
492 }
493 #else
494 #if CAN_PIPE_TO_MAILER
495 
496 /*
497  * Open a pipe to the mailer
498  */
LYPipeToMailer(void)499 FILE *LYPipeToMailer(void)
500 {
501     char *buffer = NULL;
502     FILE *fp = NULL;
503 
504     if (LYSystemMail()) {
505 	HTSprintf0(&buffer, "%s %s", system_mail, system_mail_flags);
506 	fp = popen(buffer, "w");
507 	CTRACE((tfp, "popen(%s) %s\n", buffer, fp != 0 ? "OK" : "FAIL"));
508 	FREE(buffer);
509     }
510     return fp;
511 }
512 #else /* DOS, Win32, etc. */
513 
LYSendMailFile(char * the_address,char * the_filename,char * the_subject GCC_UNUSED,char * the_ccaddr GCC_UNUSED,char * message)514 int LYSendMailFile(char *the_address,
515 		   char *the_filename,
516 		   char *the_subject GCC_UNUSED,
517 		   char *the_ccaddr GCC_UNUSED,
518 		   char *message)
519 {
520     char *cmd = NULL;
521     int code;
522 
523     if (!LYSystemMail())
524 	return 0;
525 
526 #if USE_BLAT_MAILER
527     if (mail_is_blat) {
528 	cmd = blat_cmd(the_filename,
529 		       the_address,
530 		       the_subject,
531 		       the_ccaddr,
532 		       personal_mail_address);
533     } else
534 #endif
535 #ifdef __DJGPP__
536     if (LYGetEnv("SHELL")) {
537 	extern int dj_is_bash;
538 	extern char *shell;
539 	const char *c_option;
540 	const char *format = "%s %s %s -t %s -F %s";
541 
542 	if (dj_is_bash) {
543 	    c_option = "-c";
544 	} else {
545 	    c_option = "/c";
546 	}
547 	HTAddParam(&cmd, format, 1, shell);
548 	HTAddParam(&cmd, format, 2, c_option);
549 	HTAddParam(&cmd, format, 3, system_mail);
550 	HTAddParam(&cmd, format, 4, the_address);
551 	HTAddParam(&cmd, format, 5, the_filename);
552 	HTEndParam(&cmd, format, 6);
553     } else
554 #endif /* __DJGPP__ */
555     {
556 	const char *format = "%s -t %s -F %s";
557 
558 	HTAddParam(&cmd, format, 1, system_mail);
559 	HTAddParam(&cmd, format, 2, the_address);
560 	HTAddParam(&cmd, format, 3, the_filename);
561 	HTEndParam(&cmd, format, 4);
562     }
563 
564     stop_curses();
565     SetOutputMode(O_TEXT);
566     printf("%s\n\n$ %s\n\n%s",
567 	   *message ? message : gettext("Sending"),
568 	   cmd, PLEASE_WAIT);
569     code = LYSystem(cmd);
570     LYSleepMsg();
571     start_curses();
572     SetOutputMode(O_BINARY);
573 
574     FREE(cmd);
575 
576     return code;
577 }
578 #endif /* CAN_PIPE_TO_FILE */
579 #endif /* USE_VMS_MAILER */
580 
581 /*
582  *  mailform() sends form content to the mailto address(es). - FM
583  */
mailform(const char * mailto_address,const char * mailto_subject,const char * mailto_content,const char * mailto_type)584 void mailform(const char *mailto_address,
585 	      const char *mailto_subject,
586 	      const char *mailto_content,
587 	      const char *mailto_type)
588 {
589     FILE *fd;
590     char *address = NULL;
591     char *ccaddr = NULL;
592     char *keywords = NULL;
593     char *cp = NULL;
594     char self[MAX_SUBJECT + 10];
595     char subject[MAX_SUBJECT + 10];
596     char *searchpart = NULL;
597     char buf[512];
598     int len, i;
599 
600 #if USE_VMS_MAILER
601     static char *cmd;
602     char *command = NULL;
603     BOOLEAN isPMDF = LYMailPMDF();
604     char hdrfile[LY_MAXPATH];
605 #endif
606 #if !CAN_PIPE_TO_MAILER
607     char my_tmpfile[LY_MAXPATH];
608 #endif
609 
610     CTRACE((tfp, "mailto_address: \"%s\"\n", NONNULL(mailto_address)));
611     CTRACE((tfp, "mailto_subject: \"%s\"\n", NONNULL(mailto_subject)));
612     CTRACE((tfp, "mailto_content: \"%s\"\n", NONNULL(mailto_content)));
613     CTRACE((tfp, "mailto_type:    \"%s\"\n", NONNULL(mailto_type)));
614 
615     if (!LYSystemMail())
616 	return;
617 
618     if (!mailto_address || !mailto_content) {
619 	HTAlert(BAD_FORM_MAILTO);
620 	return;
621     }
622     subject[0] = '\0';
623     self[0] = '\0';
624 
625     if ((cp = (char *) strchr(mailto_address, '\n')) != NULL)
626 	*cp = '\0';
627     StrAllocCopy(address, mailto_address);
628 
629     /*
630      * Check for a ?searchpart.  - FM
631      */
632     if ((cp = strchr(address, '?')) != NULL) {
633 	StrAllocCopy(searchpart, cp);
634 	*cp = '\0';
635 	cp = (searchpart + 1);
636 	if (*cp != '\0') {
637 	    /*
638 	     * Seek and handle a subject=foo.  - FM
639 	     */
640 	    extract_subject(subject, searchpart);
641 
642 	    /*
643 	     * Seek and handle to=address(es) fields.  Appends to address.  -
644 	     * FM
645 	     */
646 	    extract_field(&address, searchpart, "to=");
647 
648 	    /*
649 	     * Seek and handle cc=address(es) fields.  Excludes Bcc=address(es)
650 	     * as unsafe.  We may append our own cc (below) as a list for the
651 	     * actual mailing.  - FM
652 	     */
653 	    extract_field(&ccaddr, searchpart, "cc=");
654 
655 	    /*
656 	     * Seek and handle keywords=term(s) fields.  - FM
657 	     */
658 	    extract_field(&keywords, searchpart, "keywords=");
659 
660 	    if (keywords != NULL) {
661 		if (*keywords != '\0') {
662 		    SafeHTUnEscape(keywords);
663 		} else {
664 		    FREE(keywords);
665 		}
666 	    }
667 
668 	    FREE(searchpart);
669 	}
670     }
671 
672     if (convert_explorer(address)) {
673 	HTAlert(BAD_FORM_MAILTO);
674 	goto cleanup;
675     }
676     if (ccaddr != NULL) {
677 	if (convert_explorer(ccaddr)) {
678 	    FREE(ccaddr);
679 	}
680     }
681 
682     /*
683      * Unescape the address and ccaddr fields.  - FM
684      */
685     SafeHTUnEscape(address);
686     if (ccaddr != NULL) {
687 	SafeHTUnEscape(ccaddr);
688     }
689 
690     /*
691      * Allow user to edit the default Subject - FM
692      */
693     if (subject[0] == '\0') {
694 	if (non_empty(mailto_subject)) {
695 	    LYStrNCpy(subject, mailto_subject, MAX_SUBJECT);
696 	} else {
697 	    sprintf(subject, "mailto:%.63s", address);
698 	}
699     }
700     _statusline(SUBJECT_PROMPT);
701     if (LYGetStr(subject, VISIBLE, MAX_SUBJECT, NORECALL) < 0) {
702 	/*
703 	 * User cancelled via ^G. - FM
704 	 */
705 	HTInfoMsg(FORM_MAILTO_CANCELLED);
706 	goto cleanup;
707     }
708 
709     /*
710      * Allow user to specify a self copy via a CC:  entry, if permitted.  - FM
711      */
712     if (!LYNoCc) {
713 	sprintf(self, "%.*s", MAX_SUBJECT,
714 		isEmpty(personal_mail_address) ? "" : personal_mail_address);
715 	_statusline("Cc: ");
716 	if (LYGetStr(self, VISIBLE, MAX_SUBJECT, NORECALL) < 0) {
717 	    /*
718 	     * User cancelled via ^G. - FM
719 	     */
720 	    HTInfoMsg(FORM_MAILTO_CANCELLED);
721 	    goto cleanup;
722 	}
723 	remove_tildes(self);
724 	if (ccaddr == NULL) {
725 	    StrAllocCopy(ccaddr, self);
726 	} else {
727 	    StrAllocCat(ccaddr, ",");
728 	    StrAllocCat(ccaddr, self);
729 	}
730     }
731 #if CAN_PIPE_TO_MAILER
732     if ((fd = LYPipeToMailer()) == 0) {
733 	HTAlert(FORM_MAILTO_FAILED);
734 	goto cleanup;
735     }
736 
737     if (non_empty(mailto_type)) {
738 	fprintf(fd, "Mime-Version: 1.0\n");
739 	fprintf(fd, "Content-Type: %s\n", mailto_type);
740     }
741     fprintf(fd, "To: %s\n", address);
742     if (non_empty(personal_mail_address))
743 	fprintf(fd, "From: %s\n", personal_mail_address);
744     if (non_empty(ccaddr))
745 	fprintf(fd, "Cc: %s\n", ccaddr);
746     fprintf(fd, "Subject: %s\n\n", subject);
747     if (non_empty(keywords))
748 	fprintf(fd, "Keywords: %s\n", keywords);
749     _statusline(SENDING_FORM_CONTENT);
750 #else /* e.g., VMS, DOS */
751     if ((fd = LYOpenTemp(my_tmpfile, ".txt", "w")) == NULL) {
752 	HTAlert(FORM_MAILTO_FAILED);
753 	goto cleanup;
754     }
755 #if USE_VMS_MAILER
756     if (isPMDF) {
757 	FILE *hfd;
758 
759 	if ((hfd = LYOpenTemp(hdrfile, ".txt", "w")) == NULL) {
760 	    HTAlert(FORM_MAILTO_FAILED);
761 	    LYCloseTempFP(fd);
762 	    goto cleanup;
763 	}
764 	if (non_empty(mailto_type)) {
765 	    fprintf(hfd, "Mime-Version: 1.0\n");
766 	    fprintf(hfd, "Content-Type: %s\n", mailto_type);
767 	    if (non_empty(personal_mail_address))
768 		fprintf(hfd, "From: %s\n", personal_mail_address);
769 	}
770 	/*
771 	 * For PMDF, put any keywords and the subject in the header file and
772 	 * close it.  - FM
773 	 */
774 	if (non_empty(keywords)) {
775 	    fprintf(hfd, "Keywords: %s\n", keywords);
776 	}
777 	fprintf(hfd, "Subject: %s\n\n", subject);
778 	LYCloseTempFP(hfd);
779     } else if (mailto_type &&
780 	       !strncasecomp(mailto_type, "multipart/form-data", 19)) {
781 	/*
782 	 * Ugh!  There's no good way to include headers while we're still using
783 	 * "generic" VMS MAIL, so we'll put this in the body of the message.  -
784 	 * FM
785 	 */
786 	fprintf(fd, "X-Content-Type: %s\n\n", mailto_type);
787     }
788 #else /* !VMS (DOS) */
789 #if USE_BLAT_MAILER
790     if (mail_is_blat) {
791 	if (strlen(subject) > MAX_SUBJECT)
792 	    subject[MAX_SUBJECT] = '\0';
793     } else
794 #endif
795     {
796 	if (non_empty(mailto_type)) {
797 	    fprintf(fd, "Mime-Version: 1.0\n");
798 	    fprintf(fd, "Content-Type: %s\n", mailto_type);
799 	}
800 	fprintf(fd, "To: %s\n", address);
801 	if (non_empty(personal_mail_address))
802 	    fprintf(fd, "From: %s\n", personal_mail_address);
803 	fprintf(fd, "Subject: %.70s\n\n", subject);
804     }
805 #endif /* VMS */
806 #endif /* CAN_PIPE_TO_MAILER */
807 
808     /*
809      * Break up the content into lines with a maximum length of 78.  If the
810      * ENCTYPE was text/plain, we have physical newlines and should take them
811      * into account.  Otherwise, the actual newline characters in the content
812      * are hex escaped.  - FM
813      */
814     while ((cp = strchr(mailto_content, '\n')) != NULL) {
815 	*cp = '\0';
816 	i = 0;
817 	len = (int) strlen(mailto_content);
818 	while (len > 78) {
819 	    LYStrNCpy(buf, &mailto_content[i], 78);
820 	    fprintf(fd, "%s\n", buf);
821 	    i += 78;
822 	    len = (int) strlen(&mailto_content[i]);
823 	}
824 	fprintf(fd, "%s\n", &mailto_content[i]);
825 	mailto_content = (cp + 1);
826     }
827     i = 0;
828     len = (int) strlen(mailto_content);
829     while (len > 78) {
830 	LYStrNCpy(buf, &mailto_content[i], 78);
831 	fprintf(fd, "%s\n", buf);
832 	i += 78;
833 	len = (int) strlen(&mailto_content[i]);
834     }
835     if (len)
836 	fprintf(fd, "%s\n", &mailto_content[i]);
837 
838 #if CAN_PIPE_TO_MAILER
839     pclose(fd);
840     LYSleepMsg();
841 #else
842     LYCloseTempFP(fd);
843 #if USE_VMS_MAILER
844     /*
845      * Set the mail command.  - FM
846      */
847     if (isPMDF) {
848 	/*
849 	 * Now set up the command.  - FM
850 	 */
851 	HTSprintf0(&cmd,
852 		   "%s %s %s,%s ",
853 		   system_mail,
854 		   system_mail_flags,
855 		   hdrfile,
856 		   my_tmpfile);
857     } else {
858 	/*
859 	 * For "generic" VMS MAIL, include the subject in the command, and
860 	 * ignore any keywords to minimize risk of them making the line too
861 	 * long or having problem characters.  - FM
862 	 */
863 	HTSprintf0(&cmd,
864 		   "%s %s%s/subject=\"%s\" %s ",
865 		   system_mail,
866 		   system_mail_flags,
867 		   (strncasecomp(system_mail, "MAIL", 4) ? "" : "/noself"),
868 		   subject,
869 		   my_tmpfile);
870     }
871     StrAllocCopy(command, cmd);
872 
873     vms_append_addrs(&command, address, "");
874     if (non_empty(ccaddr)) {
875 	vms_append_addrs(&command, ccaddr, "/CC");
876     }
877 
878     stop_curses();
879     printf("%s\n\n$ %s\n\n%s", SENDING_FORM_CONTENT, command, PLEASE_WAIT);
880     LYSystem(command);		/* Mail (VMS) */
881     FREE(command);
882     LYSleepAlert();
883     start_curses();
884     (void) LYRemoveTemp(my_tmpfile);
885     if (isPMDF)
886 	(void) LYRemoveTemp(hdrfile);
887 #else /* DOS */
888     LYSendMailFile(address,
889 		   my_tmpfile,
890 		   subject,
891 		   ccaddr,
892 		   SENDING_FORM_CONTENT);
893     (void) LYRemoveTemp(my_tmpfile);
894 #endif /* USE_VMS_MAILER */
895 #endif /* CAN_PIPE_TO_MAILER */
896 
897   cleanup:
898     FREE(address);
899     FREE(ccaddr);
900     FREE(keywords);
901     return;
902 }
903 
904 /*
905  *  mailmsg() sends a message to the owner of the file, if one is defined,
906  *  telling of errors (i.e., link not available).
907  */
mailmsg(int cur,char * owner_address,char * filename,char * linkname)908 void mailmsg(int cur,
909 	     char *owner_address,
910 	     char *filename,
911 	     char *linkname)
912 {
913     FILE *fd, *fp;
914     char *address = NULL;
915     char *searchpart = NULL;
916     char *cmd = NULL, *cp;
917 
918 #ifdef ALERTMAIL
919     BOOLEAN skip_parsing = FALSE;
920 #endif
921 #if !CAN_PIPE_TO_MAILER
922     char *ccaddr;
923     char subject[128];
924     char my_tmpfile[LY_MAXPATH];
925 #endif
926 #if USE_VMS_MAILER
927     BOOLEAN isPMDF = LYMailPMDF();
928     char hdrfile[LY_MAXPATH];
929     char *command = NULL;
930 
931     CTRACE((tfp, "mailmsg(%d, \"%s\", \"%s\", \"%s\")\n", cur,
932 	    NONNULL(owner_address),
933 	    NONNULL(filename),
934 	    NONNULL(linkname)));
935 
936 #endif /* VMS */
937 
938     if (!LYSystemMail())
939 	return;
940 
941 #ifdef ALERTMAIL
942     if (owner_address == NULL) {
943 	owner_address = ALERTMAIL;
944 	skip_parsing = TRUE;
945     }
946 #endif
947 
948     if (isEmpty(owner_address))
949 	return;
950     if ((cp = (char *) strchr(owner_address, '\n')) != NULL) {
951 #ifdef ALERTMAIL
952 	if (skip_parsing)
953 	    return;		/* invalidly defined - ignore - kw */
954 #else
955 	*cp = '\0';
956 #endif
957     }
958     if (!strncasecomp(owner_address, "lynx-dev@", 9)) {
959 	/*
960 	 * Silently refuse sending bad link messages to lynx-dev.
961 	 */
962 	return;
963     }
964     StrAllocCopy(address, owner_address);
965 
966 #ifdef ALERTMAIL
967     /*
968      * If we are using a fixed address given by ALERTMAIL, it is supposed to
969      * already be in usable form, without URL-isms like ?-searchpart and
970      * URL-escaping.  So skip some code.  - kw
971      */
972     if (!skip_parsing)
973 #endif
974     {
975 	/*
976 	 * Check for a ?searchpart.  - FM
977 	 */
978 	if ((cp = strchr(address, '?')) != NULL) {
979 	    StrAllocCopy(searchpart, cp);
980 	    *cp = '\0';
981 	    cp = (searchpart + 1);
982 	    if (*cp != '\0') {
983 		/*
984 		 * Seek and handle to=address(es) fields.
985 		 * Appends to address.  We ignore any other
986 		 * headers in the ?searchpart.  - FM
987 		 */
988 		extract_field(&address, searchpart, "to=");
989 	    }
990 	}
991 
992 	(void) convert_explorer(address);
993 
994 	/*
995 	 * Unescape the address field.  - FM
996 	 */
997 	SafeHTUnEscape(address);
998     }
999 
1000     if (trim_comma(address)) {
1001 	FREE(address);
1002 	CTRACE((tfp, "mailmsg: No address in '%s'.\n", owner_address));
1003 	return;
1004     }
1005 #if CAN_PIPE_TO_MAILER
1006     if ((fd = LYPipeToMailer()) == 0) {
1007 	FREE(address);
1008 	CTRACE((tfp, "mailmsg: '%s' failed.\n", cmd));
1009 	return;
1010     }
1011 
1012     fprintf(fd, "To: %s\n", address);
1013     fprintf(fd, "Subject: Lynx Error in %s\n", filename);
1014     if (non_empty(personal_mail_address)) {
1015 	fprintf(fd, "Cc: %s\n", personal_mail_address);
1016     }
1017     fprintf(fd, "X-URL: %s\n", filename);
1018     fprintf(fd, "X-Mailer: %s, Version %s\n\n", LYNX_NAME, LYNX_VERSION);
1019 #else
1020     if ((fd = LYOpenTemp(my_tmpfile, ".txt", "w")) == NULL) {
1021 	CTRACE((tfp, "mailmsg: Could not fopen '%s'.\n", my_tmpfile));
1022 	FREE(address);
1023 	return;
1024     }
1025     sprintf(subject, "Lynx Error in %.56s", filename);
1026     ccaddr = personal_mail_address;
1027 #if USE_VMS_MAILER
1028     if (isPMDF) {
1029 	FILE *hfd;
1030 
1031 	if ((hfd = LYOpenTemp(hdrfile, ".txt", "w")) == NULL) {
1032 	    CTRACE((tfp, "mailmsg: Could not fopen '%s'.\n", hdrfile));
1033 	    FREE(address);
1034 	    return;
1035 	}
1036 
1037 	if (non_empty(personal_mail_address)) {
1038 	    fprintf(fd, "Cc: %s\n", personal_mail_address);
1039 	}
1040 	fprintf(fd, "X-URL: %s\n", filename);
1041 	fprintf(fd, "X-Mailer: %s, Version %s\n\n", LYNX_NAME, LYNX_VERSION);
1042 	/*
1043 	 * For PMDF, put the subject in the header file and close it.  - FM
1044 	 */
1045 	fprintf(hfd, "Subject: Lynx Error in %.56s\n\n", filename);
1046 	LYCloseTempFP(hfd);
1047     }
1048 #endif /* USE_VMS_MAILER */
1049 #endif /* CAN_PIPE_TO_MAILER */
1050 
1051     fprintf(fd, gettext("The link   %s :?: %s \n"),
1052 	    links[cur].lname, links[cur].target);
1053     fprintf(fd, gettext("called \"%s\"\n"), LYGetHiliteStr(cur, 0));
1054     fprintf(fd, gettext("in the file \"%s\" called \"%s\"\n"), filename, linkname);
1055     fprintf(fd, "%s\n\n", gettext("was requested but was not available."));
1056     fprintf(fd, "%s\n\n", gettext("Thought you might want to know."));
1057 
1058     fprintf(fd, "%s\n", gettext("This message was automatically generated by"));
1059     fprintf(fd, "%s %s", LYNX_NAME, LYNX_VERSION);
1060     if ((LynxSigFile != NULL) &&
1061 	(fp = fopen(LynxSigFile, TXT_R)) != NULL) {
1062 	fputs("-- \n", fd);
1063 	while (LYSafeGets(&cmd, fp) != NULL)
1064 	    fputs(cmd, fd);
1065 	LYCloseInput(fp);
1066     }
1067 #if CAN_PIPE_TO_MAILER
1068     pclose(fd);
1069 #else
1070     LYCloseTempFP(fd);
1071 #if USE_VMS_MAILER
1072     if (isPMDF) {
1073 	/*
1074 	 * Now set up the command.  - FM
1075 	 */
1076 	HTSprintf0(&command,
1077 		   "%s %s %s,%s ",
1078 		   system_mail,
1079 		   system_mail_flags,
1080 		   hdrfile,
1081 		   my_tmpfile);
1082     } else {
1083 	/*
1084 	 * For "generic" VMS MAIL, include the subject in the command.  - FM
1085 	 */
1086 	HTSprintf0(&command,
1087 		   "%s %s/self/subject=\"Lynx Error in %.56s\" %s ",
1088 		   system_mail,
1089 		   system_mail_flags,
1090 		   filename,
1091 		   my_tmpfile);
1092     }
1093     vms_append_addrs(&command, address, "");
1094 
1095     LYSystem(command);		/* VMS */
1096     FREE(command);
1097     FREE(cmd);
1098     (void) LYRemoveTemp(my_tmpfile);
1099     if (isPMDF) {
1100 	(void) LYRemoveTemp(hdrfile);
1101     }
1102 #else /* DOS */
1103     LYSendMailFile(address,
1104 		   my_tmpfile,
1105 		   subject,
1106 		   ccaddr,
1107 		   "");
1108     (void) LYRemoveTemp(my_tmpfile);
1109 #endif /* USE_VMS_MAILER */
1110 #endif /* CAN_PIPE_TO_MAILER */
1111 
1112     if (traversal) {
1113 	FILE *ofp;
1114 
1115 	if ((ofp = LYAppendToTxtFile(TRAVERSE_ERRORS)) == NULL) {
1116 	    if ((ofp = LYNewTxtFile(TRAVERSE_ERRORS)) == NULL) {
1117 		perror(NOOPEN_TRAV_ERR_FILE);
1118 		exit_immediately(EXIT_FAILURE);
1119 	    }
1120 	}
1121 
1122 	fprintf(ofp, "%s\t%s \tin %s\n",
1123 		links[cur].lname, links[cur].target, filename);
1124 	LYCloseOutput(ofp);
1125     }
1126 
1127     FREE(address);
1128     return;
1129 }
1130 
1131 /*
1132  *  reply_by_mail() invokes sendmail on Unix or mail on VMS to send
1133  *  a comment from the users to the owner
1134  */
reply_by_mail(char * mail_address,char * filename,const char * title,const char * refid)1135 void reply_by_mail(char *mail_address,
1136 		   char *filename,
1137 		   const char *title,
1138 		   const char *refid)
1139 {
1140     char user_input[LINESIZE];
1141     FILE *fd, *fp;
1142     const char *label = NULL;
1143     char *from_address = NULL;
1144     char *cc_address = NULL;
1145     char *to_address = NULL;
1146     char *the_subject = NULL;
1147     char *ccaddr = NULL;
1148     char *keywords = NULL;
1149     char *searchpart = NULL;
1150     char *body = NULL;
1151     char *cp = NULL, *cp1 = NULL;
1152     int i;
1153     int c = 0;			/* user input */
1154     char my_tmpfile[LY_MAXPATH];
1155     char default_subject[MAX_SUBJECT + 10];
1156 
1157 #if USE_VMS_MAILER
1158     char *command = NULL;
1159     BOOLEAN isPMDF = LYMailPMDF();
1160     char hdrfile[LY_MAXPATH];
1161     FILE *hfd = 0;
1162 
1163 #else
1164 #if !CAN_PIPE_TO_MAILER
1165     char tmpfile2[LY_MAXPATH];
1166 #endif
1167     char buf[4096];		/* 512 */
1168     char *header = NULL;
1169     size_t nbytes;
1170 #endif /* USE_VMS_MAILER */
1171 
1172     CTRACE((tfp, "reply_by_mail(\"%s\", \"%s\", \"%s\", \"%s\")\n",
1173 	    NONNULL(mail_address),
1174 	    NONNULL(filename),
1175 	    NONNULL(title),
1176 	    NONNULL(refid)));
1177 
1178     term_letter = FALSE;
1179 
1180     if (!LYSystemMail())
1181 	return;
1182 
1183     if (isEmpty(mail_address)) {
1184 	HTAlert(NO_ADDRESS_IN_MAILTO_URL);
1185 	return;
1186     }
1187     StrAllocCopy(to_address, mail_address);
1188 
1189     if ((fd = LYOpenTemp(my_tmpfile, ".txt", "w")) == NULL) {
1190 	HTAlert(MAILTO_URL_TEMPOPEN_FAILED);
1191 	return;
1192     }
1193 #if USE_VMS_MAILER
1194     if (isPMDF) {
1195 	if ((hfd = LYOpenTemp(hdrfile, ".txt", "w")) == NULL) {
1196 	    HTAlert(MAILTO_URL_TEMPOPEN_FAILED);
1197 	    return;
1198 	}
1199     }
1200 #endif /* VMS */
1201     default_subject[0] = '\0';
1202 
1203     /*
1204      * Check for a ?searchpart.  - FM
1205      */
1206     if ((cp = strchr(to_address, '?')) != NULL) {
1207 	StrAllocCopy(searchpart, cp);
1208 	*cp = '\0';
1209 	cp = (searchpart + 1);
1210 	if (*cp != '\0') {
1211 	    /*
1212 	     * Seek and handle a subject=foo.  - FM
1213 	     */
1214 	    extract_subject(default_subject, searchpart);
1215 
1216 	    /*
1217 	     * Seek and handle to=address(es) fields.  Appends to address.  -
1218 	     * FM
1219 	     */
1220 	    extract_field(&to_address, searchpart, "to=");
1221 
1222 	    /*
1223 	     * Seek and handle cc=address(es) fields.  Excludes Bcc=address(es)
1224 	     * as unsafe.  We may append our own cc (below) as a list for the
1225 	     * actual mailing.  - FM
1226 	     */
1227 	    extract_field(&ccaddr, searchpart, "cc=");
1228 
1229 	    /*
1230 	     * Seek and handle keywords=term(s) fields.  - FM
1231 	     */
1232 	    extract_field(&keywords, searchpart, "keywords=");
1233 
1234 	    if (keywords != NULL) {
1235 		if (*keywords != '\0') {
1236 		    SafeHTUnEscape(keywords);
1237 		} else {
1238 		    FREE(keywords);
1239 		}
1240 	    }
1241 
1242 	    /*
1243 	     * Seek and handle body=foo fields.  - FM
1244 	     */
1245 	    extract_body(&body, searchpart);
1246 
1247 	    FREE(searchpart);
1248 	}
1249     }
1250 
1251     if (convert_explorer(to_address)) {
1252 	HTAlert(NO_ADDRESS_IN_MAILTO_URL);
1253 	goto cancelled;
1254     }
1255     if (ccaddr != NULL) {
1256 	if (convert_explorer(ccaddr)) {
1257 	    FREE(ccaddr);
1258 	}
1259     }
1260 
1261     /*
1262      * Unescape the address and ccaddr fields.  - FM
1263      */
1264     SafeHTUnEscape(to_address);
1265     if (ccaddr != NULL) {
1266 	SafeHTUnEscape(ccaddr);
1267     }
1268 
1269     /*
1270      * Set the default subject.  - FM
1271      */
1272     if ((default_subject[0] == '\0') && non_empty(title)) {
1273 	LYStrNCpy(default_subject, title, MAX_SUBJECT);
1274     }
1275 
1276     /*
1277      * Use ^G to cancel mailing of comment and don't let SIGINTs exit lynx.
1278      */
1279     signal(SIGINT, terminate_letter);
1280 
1281 #if USE_VMS_MAILER
1282     if (isPMDF || !body) {
1283 	/*
1284 	 * Put the X-URL and X-Mailer lines in the hdrfile for PMDF or
1285 	 * my_tmpfile for VMS MAIL.  - FM
1286 	 */
1287 	fprintf((isPMDF ? hfd : fd),
1288 		"X-URL: %s%s\n",
1289 		isEmpty(filename) ? STR_MAILTO_URL : filename,
1290 		isEmpty(filename) ? to_address : "");
1291 	fprintf((isPMDF ? hfd : fd),
1292 		"X-Mailer: %s, Version %s\n", LYNX_NAME, LYNX_VERSION);
1293 #ifdef NO_ANONYMOUS_EMAIL
1294 	if (!isPMDF) {
1295 	    fprintf(fd, "\n");
1296 	}
1297 #endif /* NO_ANONYMOUS_EMAIL */
1298     }
1299 #else /* Unix/DOS/Windows */
1300     /*
1301      * Put the To:  line in the header.
1302      */
1303 #ifndef DOSPATH
1304     HTSprintf(&header, "To: %s\n", to_address);
1305 #endif
1306 
1307     /*
1308      * Put the Mime-Version, Content-Type and Content-Transfer-Encoding in the
1309      * header.  This assumes that the same character set is used for composing
1310      * the mail which is currently selected as display character set...  Don't
1311      * send a charset if we have a CJK character set selected, since it may not
1312      * be appropriate for mail...  Also don't use an unofficial "x-" charset.
1313      * Also if the charset would be "us-ascii" (7-bit replacements selected,
1314      * don't send any MIME headers.  - kw
1315      */
1316     if (strncasecomp(LYCharSet_UC[current_char_set].MIMEname,
1317 		     "us-ascii", 8) != 0) {
1318 	StrAllocCat(header, "Mime-Version: 1.0\n");
1319 	if (!LYHaveCJKCharacterSet &&
1320 	    strncasecomp(LYCharSet_UC[current_char_set].MIMEname, "x-", 2)
1321 	    != 0) {
1322 	    HTSprintf(&header, "Content-Type: text/plain; charset=%s\n",
1323 		      LYCharSet_UC[current_char_set].MIMEname);
1324 	}
1325 	StrAllocCat(header, "Content-Transfer-Encoding: 8bit\n");
1326     }
1327     /*
1328      * Put the X-URL and X-Mailer lines in the header.
1329      */
1330     if (non_empty(filename)) {
1331 	HTSprintf(&header, "X-URL: %s\n", filename);
1332     } else {
1333 	HTSprintf(&header, "X-URL: mailto:%s\n", to_address);
1334     }
1335     HTSprintf(&header, "X-Mailer: %s, Version %s\n", LYNX_NAME, LYNX_VERSION);
1336 
1337     if (non_empty(refid)) {
1338 	HTSprintf(&header, "In-Reply-To: <%s>\n", refid);
1339     }
1340 #endif /* VMS */
1341 
1342     /*
1343      * Clear the screen and inform the user.
1344      */
1345     LYclear();
1346     LYmove(2, 0);
1347     scrollok(LYwin, TRUE);	/* Enable scrolling. */
1348     if (body)
1349 	LYaddstr(SENDING_MESSAGE_WITH_BODY_TO);
1350     else
1351 	LYaddstr(SENDING_COMMENT_TO);
1352     show_addresses(to_address);
1353     if (
1354 #if USE_VMS_MAILER
1355 	   (isPMDF == TRUE) &&
1356 #endif /* VMS */
1357 	   (cp = ccaddr) != NULL) {
1358 	if (strchr(cp, ',') != NULL) {
1359 	    LYaddstr(WITH_COPIES_TO);
1360 	} else {
1361 	    LYaddstr(WITH_COPY_TO);
1362 	}
1363 	show_addresses(ccaddr);
1364     }
1365     LYaddstr(CTRL_G_TO_CANCEL_SEND);
1366 
1367 #if USE_VMS_MAILER
1368     if (isPMDF || !body) {
1369 #endif /* USE_VMS_MAILER */
1370 #ifndef NO_ANONYMOUS_EMAIL
1371 	/*
1372 	 * Get the user's personal name.
1373 	 */
1374 	LYaddstr(ENTER_NAME_OR_BLANK);
1375 #if USE_VMS_MAILER
1376 	if (isPMDF) {
1377 	    label = "Personal_name";
1378 	} else {
1379 	    label = "X-Personal_name";
1380 	}
1381 #else
1382 	label = "X-Personal_Name";
1383 #endif /* USE_VMS_MAILER */
1384 	if (!header_prompt(label, &personal_mail_name, LINESIZE)) {
1385 	    goto cancelled;
1386 	}
1387 	if (*personal_mail_name) {
1388 #if USE_VMS_MAILER
1389 	    fprintf((isPMDF ? hfd : fd), "%s: %s\n", label, personal_mail_name);
1390 #else
1391 	    HTSprintf(&header, "%s: %s\n", label, personal_mail_name);
1392 #endif /* VMS */
1393 	}
1394 
1395 	/*
1396 	 * Get the user's return address.
1397 	 */
1398 	LYaddstr(ENTER_MAIL_ADDRESS_OR_OTHER);
1399 	LYaddstr(MEANS_TO_CONTACT_FOR_RESPONSE);
1400 #if USE_VMS_MAILER
1401 	if (isPMDF) {
1402 	    label = "From";
1403 	} else {
1404 	    label = "X-From";
1405 	}
1406 #else
1407 	label = "From";
1408 #endif /* VMS */
1409 	/* Add the personal mail address if there is one. */
1410 	if (personal_mail_address)
1411 	    StrAllocCopy(from_address, personal_mail_address);
1412 	if (!header_prompt(label, &from_address, LINESIZE)) {
1413 	    goto cancelled;
1414 	}
1415 #if USE_VMS_MAILER
1416 	if (*from_address) {
1417 	    fprintf(isPMDF ? hfd : fd, "%s: %s\n", label, from_address);
1418 	}
1419 	if (!isPMDF) {
1420 	    fprintf(fd, "\n");
1421 	}
1422 #else
1423 	HTSprintf(&header, "%s: %s\n", label, from_address);
1424 #endif /* USE_VMS_MAILER */
1425 #endif /* !NO_ANONYMOUS_EMAIL */
1426 #if USE_VMS_MAILER
1427     }
1428 #endif /* USE_VMS_MAILER */
1429 
1430     /*
1431      * Get the subject line.
1432      */
1433     LYaddstr(ENTER_SUBJECT_LINE);
1434     label = "Subject";
1435     if (*default_subject) {
1436 	StrAllocCopy(the_subject, default_subject);
1437     } else if (non_empty(filename)) {
1438 	HTSprintf(&the_subject, "%s", filename);
1439     } else {
1440 	HTSprintf(&the_subject, "mailto:%s", to_address);
1441     }
1442     if (!header_prompt(label, &the_subject, MAX_SUBJECT)) {
1443 	goto cancelled;
1444     }
1445 
1446     /*
1447      * Offer a CC line, if permitted.  - FM
1448      */
1449     if (!LYNoCc) {
1450 	LYaddstr(ENTER_ADDRESS_FOR_CC);
1451 	LYaddstr(BLANK_FOR_NO_COPY);
1452 	if (personal_mail_address)
1453 	    StrAllocCopy(cc_address, personal_mail_address);
1454 	if (!header_prompt("Cc", &cc_address, LINESIZE)) {
1455 	    goto cancelled;
1456 	}
1457 	comma_append(&ccaddr, cc_address);
1458     }
1459 #if !USE_VMS_MAILER
1460     HTSprintf(&header, "%s: %s\n", label, the_subject);
1461 #if !CAN_PIPE_TO_MAILER
1462     if (*to_address) {
1463 	HTSprintf(&header, "To: %s\n", to_address);
1464     }
1465 #endif
1466 
1467     /*
1468      * Add the Cc:  header.  - FM
1469      */
1470     if (non_empty(ccaddr)) {
1471 	HTSprintf(&header, "Cc: %s\n", ccaddr);
1472     }
1473 
1474     /*
1475      * Add the Keywords:  header.  - FM
1476      */
1477     if (non_empty(keywords)) {
1478 	HTSprintf(&header, "Keywords: %s\n", keywords);
1479     }
1480 
1481     /*
1482      * Terminate the header.
1483      */
1484     StrAllocCat(header, "\n");
1485     CTRACE((tfp, "**header==\n%s", header));
1486 #endif /* !VMS */
1487 
1488     if (!no_editor && non_empty(editor)) {
1489 
1490 	if (body) {
1491 	    cp1 = body;
1492 	    while ((cp = strchr(cp1, '\n')) != NULL) {
1493 		*cp++ = '\0';
1494 		fprintf(fd, "%s\n", cp1);
1495 		cp1 = cp;
1496 	    }
1497 	} else if (strcmp(HTLoadedDocumentURL(), "")) {
1498 	    /*
1499 	     * Ask if the user wants to include the original message.
1500 	     */
1501 	    BOOLEAN is_preparsed = (BOOL) (LYPreparsedSource &&
1502 					   HTisDocumentSource());
1503 
1504 	    if (HTConfirm(is_preparsed
1505 			  ? INC_PREPARSED_MSG_PROMPT
1506 			  : INC_ORIG_MSG_PROMPT) == YES) {
1507 		print_wwwfile_to_fd(fd, TRUE, (BOOL) !is_preparsed);
1508 	    }
1509 	}
1510 	LYCloseTempFP(fd);	/* Close the tmpfile. */
1511 	scrollok(LYwin, FALSE);	/* Stop scrolling.    */
1512 
1513 	if (term_letter || LYCharIsINTERRUPT(c))
1514 	    goto cleanup;
1515 
1516 	/*
1517 	 * Spawn the users editor on the mail file
1518 	 */
1519 	edit_temporary_file(my_tmpfile, "", SPAWNING_EDITOR_FOR_MAIL);
1520 
1521     } else if (body) {
1522 	/*
1523 	 * Let user review the body.  - FM
1524 	 */
1525 	LYclear();
1526 	LYmove(0, 0);
1527 	LYaddstr(REVIEW_MESSAGE_BODY);
1528 	LYrefresh();
1529 	cp1 = body;
1530 	i = (LYlines - 5);
1531 	while ((cp = strchr(cp1, '\n')) != NULL) {
1532 	    if (i <= 0) {
1533 		LYaddstr(RETURN_TO_CONTINUE);
1534 		LYrefresh();
1535 		c = LYgetch();
1536 		LYaddstr("\n");
1537 		if (term_letter || LYCharIsINTERRUPT(c)) {
1538 		    goto cancelled;
1539 		}
1540 		i = (LYlines - 2);
1541 	    }
1542 	    *cp++ = '\0';
1543 	    fprintf(fd, "%s\n", cp1);
1544 	    LYaddstr(cp1);
1545 	    LYaddstr("\n");
1546 	    cp1 = cp;
1547 	    i--;
1548 	}
1549 	while (i >= 0) {
1550 	    LYaddstr("\n");
1551 	    i--;
1552 	}
1553 	LYrefresh();
1554 	LYCloseTempFP(fd);	/* Close the tmpfile.     */
1555 	scrollok(LYwin, FALSE);	/* Stop scrolling.        */
1556 
1557     } else {
1558 	/*
1559 	 * Use the internal line editor for the message.
1560 	 */
1561 	LYaddstr(ENTER_MESSAGE_BELOW);
1562 	LYaddstr(ENTER_PERIOD_WHEN_DONE_A);
1563 	LYaddstr(ENTER_PERIOD_WHEN_DONE_B);
1564 	LYaddstr(CTRL_G_TO_CANCEL_SEND);
1565 	LYaddstr("\n\n");
1566 	LYrefresh();
1567 	*user_input = '\0';
1568 	if (LYGetStr(user_input, VISIBLE, sizeof(user_input), NORECALL) < 0 ||
1569 	    term_letter || STREQ(user_input, ".")) {
1570 	    goto cancelled;
1571 	}
1572 
1573 	while (!STREQ(user_input, ".") && !term_letter) {
1574 	    LYaddstr("\n");
1575 	    remove_tildes(user_input);
1576 	    fprintf(fd, "%s\n", user_input);
1577 	    *user_input = '\0';
1578 	    if (LYGetStr(user_input, VISIBLE,
1579 			 sizeof(user_input), NORECALL) < 0) {
1580 		goto cancelled;
1581 	    }
1582 	}
1583 
1584 	fprintf(fd, "\n");	/* Terminate the message. */
1585 	LYCloseTempFP(fd);	/* Close the tmpfile.     */
1586 	scrollok(LYwin, FALSE);	/* Stop scrolling.        */
1587     }
1588 
1589 #if !USE_VMS_MAILER
1590     /*
1591      * Ignore CTRL-C on this last question.
1592      */
1593     signal(SIGINT, SIG_IGN);
1594 #endif /* !VMS */
1595     LYStatusLine = (LYlines - 1);
1596     c = HTConfirm(body ? SEND_MESSAGE_PROMPT : SEND_COMMENT_PROMPT);
1597     LYStatusLine = -1;
1598     if (c != YES) {
1599 	LYclear();		/* clear the screen */
1600 	goto cleanup;
1601     }
1602     if ((body == NULL && LynxSigFile != NULL) &&
1603 	(fp = fopen(LynxSigFile, TXT_R)) != NULL) {
1604 	LYStatusLine = (LYlines - 1);
1605 	if (term_letter) {
1606 	    _user_message(APPEND_SIG_FILE, LynxSigFile);
1607 	    c = 0;
1608 	} else {
1609 	    char *msg = NULL;
1610 
1611 	    HTSprintf0(&msg, APPEND_SIG_FILE, LynxSigFile);
1612 	    c = HTConfirm(msg);
1613 	    FREE(msg);
1614 	}
1615 	LYStatusLine = -1;
1616 	if (c == YES) {
1617 	    if ((fd = fopen(my_tmpfile, TXT_A)) != NULL) {
1618 		char *buffer = NULL;
1619 
1620 		fputs("-- \n", fd);
1621 		while (LYSafeGets(&buffer, fp) != NULL) {
1622 		    fputs(buffer, fd);
1623 		}
1624 		LYCloseOutput(fd);
1625 		FREE(buffer);
1626 	    }
1627 	}
1628 	LYCloseInput(fp);
1629     }
1630     LYclear();			/* Clear the screen. */
1631 
1632     /*
1633      * Send the message.
1634      */
1635 #if USE_VMS_MAILER
1636     /*
1637      * Set the mail command.  - FM
1638      */
1639     if (isPMDF) {
1640 	/*
1641 	 * For PMDF, put any keywords and the subject in the header file and
1642 	 * close it.  - FM
1643 	 */
1644 	if (non_empty(keywords)) {
1645 	    fprintf(hfd, "Keywords: %s\n", keywords);
1646 	}
1647 	fprintf(hfd, "Subject: %s\n\n", the_subject);
1648 	LYCloseTempFP(hfd);
1649 	/*
1650 	 * Now set up the command.  - FM
1651 	 */
1652 	HTSprintf0(&command, "%s %s %s,%s ",
1653 		   system_mail,
1654 		   system_mail_flags,
1655 		   hdrfile,
1656 		   my_tmpfile);
1657     } else {
1658 	/*
1659 	 * For "generic" VMS MAIL, include the subject in the command, and
1660 	 * ignore any keywords to minimize risk of them making the line too
1661 	 * long or having problem characters.  - FM
1662 	 */
1663 	HTSprintf0(&command, "%s %s%s/subject=\"%s\" %s ",
1664 		   system_mail,
1665 		   system_mail_flags,
1666 		   (strncasecomp(system_mail, "MAIL", 4) ? "" : "/noself"),
1667 		   the_subject,
1668 		   my_tmpfile);
1669     }
1670 
1671     vms_append_addrs(&command, to_address, "");
1672     if (non_empty(ccaddr)) {
1673 	vms_append_addrs(&command, ccaddr, "/CC");
1674     }
1675 
1676     stop_curses();
1677     printf("%s\n\n$ %s\n\n%s", SENDING_COMMENT, command, PLEASE_WAIT);
1678     LYSystem(command);		/* SENDING COMMENT (VMS) */
1679     FREE(command);
1680     LYSleepAlert();
1681     start_curses();
1682 #else /* Unix/DOS/Windows */
1683     /*
1684      * Send the tmpfile into sendmail.
1685      */
1686     _statusline(SENDING_YOUR_MSG);
1687 #if CAN_PIPE_TO_MAILER
1688     signal(SIGINT, SIG_IGN);
1689     if ((fp = LYPipeToMailer()) == 0) {
1690 	HTInfoMsg(CANCELLED);
1691     }
1692 #else
1693     if ((fp = LYOpenTemp(tmpfile2, ".txt", "w")) == NULL) {
1694 	HTAlert(MAILTO_URL_TEMPOPEN_FAILED);
1695     }
1696 #endif /* CAN_PIPE_TO_MAILER */
1697     if (fp != 0) {
1698 	fd = fopen(my_tmpfile, TXT_R);
1699 	if (fd == NULL) {
1700 	    HTInfoMsg(CANCELLED);
1701 #if CAN_PIPE_TO_MAILER
1702 	    pclose(fp);
1703 #else
1704 	    LYCloseTempFP(fp);
1705 #endif /* CAN_PIPE_TO_MAILER */
1706 	} else {
1707 #if USE_BLAT_MAILER
1708 	    if (!mail_is_blat)
1709 		fputs(header, fp);
1710 #else
1711 	    fputs(header, fp);
1712 #endif
1713 	    while ((nbytes = fread(buf, (size_t) 1, sizeof(buf), fd)) != 0) {
1714 		if (fwrite(buf, (size_t) 1, (size_t) nbytes, fp) < nbytes)
1715 		    break;
1716 	    }
1717 #if CAN_PIPE_TO_MAILER
1718 	    pclose(fp);
1719 #else
1720 	    LYCloseTempFP(fp);	/* Close the tmpfile. */
1721 	    LYSendMailFile(to_address,
1722 			   tmpfile2,
1723 			   the_subject,
1724 			   ccaddr,
1725 			   SENDING_COMMENT);
1726 	    (void) LYRemoveTemp(tmpfile2);	/* Delete the tmpfile. */
1727 #endif /* CAN_PIPE_TO_MAILER */
1728 	    LYCloseInput(fd);	/* Close the tmpfile. */
1729 	}
1730     }
1731 #endif /* USE_VMS_MAILER */
1732     goto cleanup;
1733 
1734     /*
1735      * Come here to cleanup and exit.
1736      */
1737   cancelled:
1738     HTInfoMsg(CANCELLED);
1739     LYCloseTempFP(fd);		/* Close the tmpfile.   */
1740     scrollok(LYwin, FALSE);	/* Stop scrolling.      */
1741   cleanup:
1742     signal(SIGINT, cleanup_sig);
1743     term_letter = FALSE;
1744 
1745 #if USE_VMS_MAILER
1746     while (LYRemoveTemp(my_tmpfile) == 0) ;	/* Delete the tmpfile(s). */
1747     if (isPMDF) {
1748 	(void) LYRemoveTemp(hdrfile);	/* Delete the hdrfile. */
1749     }
1750 #else
1751     FREE(header);
1752     (void) LYRemoveTemp(my_tmpfile);	/* Delete the tmpfile. */
1753 #endif /* VMS */
1754 
1755     FREE(from_address);
1756     FREE(the_subject);
1757     FREE(cc_address);
1758     FREE(to_address);
1759     FREE(ccaddr);
1760     FREE(keywords);
1761     FREE(body);
1762     return;
1763 }
1764 
1765 /*
1766  * Check that we have configured values for system mailer.
1767  */
LYSystemMail(void)1768 BOOLEAN LYSystemMail(void)
1769 {
1770     if (system_mail == 0 || !strcmp(system_mail, "unknown")) {
1771 	HTAlert(gettext("No system mailer configured"));
1772 	return FALSE;
1773     }
1774     return TRUE;
1775 }
1776