cmd1.c: expand() may fail
[s-mailx.git] / sendout.c
blob00588cf3d9792705efeb19a3367f52832a384bd3
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #include "rcv.h"
41 #include "extern.h"
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <time.h>
49 * Mail -- a mail program
51 * Mail to others.
54 static char *send_boundary;
56 static char *getencoding(enum conversion convert);
57 static struct name *fixhead(struct header *hp, struct name *tolist);
58 static int put_signature(FILE *fo, int convert);
59 static int attach_file1(struct attachment *ap, FILE *fo, int dosign);
60 static int attach_file(struct attachment *ap, FILE *fo, int dosign);
61 static int attach_message(struct attachment *ap, FILE *fo, int dosign);
62 static int make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
63 const char *contenttype, const char *charset, int dosign);
64 static FILE *infix(struct header *hp, FILE *fi, int dosign);
65 static int savemail(char *name, FILE *fi);
66 static int sendmail_internal(void *v, int recipient_record);
67 static enum okay transfer(struct name *to, FILE *input, struct header *hp);
68 static char ** prepare_mta_args(struct name *to);
69 static enum okay start_mta(struct name *to, FILE *input, struct header *hp);
70 static void message_id(FILE *fo, struct header *hp);
71 static int fmt(char *str, struct name *np, FILE *fo, int comma,
72 int dropinvalid, int domime);
73 static int infix_resend(FILE *fi, FILE *fo, struct message *mp,
74 struct name *to, int add_resent);
77 * Generate a boundary for MIME multipart messages.
79 char *
80 makeboundary(void)
82 static char bound[70];
83 time_t now;
85 time(&now);
86 snprintf(bound, sizeof bound, "=_%lx.%s", (long)now, getrandstring(48));
87 send_boundary = bound;
88 return send_boundary;
92 * Get an encoding flag based on the given string.
94 static char *
95 getencoding(enum conversion convert)
97 switch (convert) {
98 case CONV_7BIT:
99 return "7bit";
100 case CONV_8BIT:
101 return "8bit";
102 case CONV_TOQP:
103 return "quoted-printable";
104 case CONV_TOB64:
105 return "base64";
106 default:
107 break;
109 /*NOTREACHED*/
110 return NULL;
114 * Fix the header by glopping all of the expanded names from
115 * the distribution list into the appropriate fields.
117 static struct name *
118 fixhead(struct header *hp, struct name *tolist) /* TODO !HAVE_ASSERTS legacy*/
120 struct name *np;
122 hp->h_to = hp->h_cc = hp->h_bcc = NULL;
123 for (np = tolist; np != NULL; np = np->n_flink)
124 if (np->n_type & GDEL) {
125 #ifdef HAVE_ASSERTS
126 assert(0); /* Shouldn't happen here, but later on :)) */
127 #else
128 continue;
129 #endif
130 } else switch (np->n_type & GMASK) {
131 case (GTO):
132 hp->h_to = cat(hp->h_to, ndup(np, np->n_type|GFULL));
133 break;
134 case (GCC):
135 hp->h_cc = cat(hp->h_cc, ndup(np, np->n_type|GFULL));
136 break;
137 case (GBCC):
138 hp->h_bcc = cat(hp->h_bcc, ndup(np, np->n_type|GFULL));
139 break;
140 default:
141 break;
143 return (tolist);
148 * Do not change, you get incorrect base64 encodings else!
150 #define INFIX_BUF 972
153 * Put the signature file at fo.
155 static int
156 put_signature(FILE *fo, int convert)
158 char *sig, buf[INFIX_BUF], c = '\n';
159 FILE *fsig;
160 size_t sz;
162 sig = value("signature");
163 if (sig == NULL || *sig == '\0')
164 return 0;
165 else
166 sig = file_expand(sig);
167 if ((fsig = Fopen(sig, "r")) == NULL) {
168 perror(sig);
169 return -1;
171 while ((sz = fread(buf, sizeof *buf, INFIX_BUF, fsig)) != 0) {
172 c = buf[sz - 1];
173 if (mime_write(buf, sz, fo, convert, TD_NONE,
174 NULL, (size_t)0, NULL, NULL)
175 == 0) {
176 perror(sig);
177 Fclose(fsig);
178 return -1;
181 if (ferror(fsig)) {
182 perror(sig);
183 Fclose(fsig);
184 return -1;
186 Fclose(fsig);
187 if (c != '\n')
188 putc('\n', fo);
189 return 0;
193 * Write an attachment to the file buffer, converting to MIME.
195 static int
196 attach_file1(struct attachment *ap, FILE *fo, int dosign)
198 FILE *fi;
199 char *charset = NULL, *contenttype = NULL, *basename;
200 enum conversion convert = CONV_TOB64;
201 int err = 0;
202 enum mimeclean isclean;
203 size_t sz;
204 char *buf;
205 size_t bufsize, count;
206 int lastc = EOF;
207 #ifdef HAVE_ICONV
208 char *tcs;
209 #endif
211 if ((fi = Fopen(ap->a_name, "r")) == NULL) {
212 perror(ap->a_name);
213 return -1;
215 if ((basename = strrchr(ap->a_name, '/')) == NULL)
216 basename = ap->a_name;
217 else
218 basename++;
219 if (ap->a_content_type)
220 contenttype = ap->a_content_type;
221 else
222 contenttype = mime_filecontent(basename);
223 if (ap->a_charset)
224 charset = ap->a_charset;
225 convert = get_mime_convert(fi, &contenttype, &charset, &isclean,
226 dosign);
227 fprintf(fo,
228 "\n--%s\n"
229 "Content-Type: %s",
230 send_boundary, contenttype);
231 if (charset == NULL)
232 putc('\n', fo);
233 else
234 fprintf(fo, ";\n charset=%s\n", charset);
235 if (ap->a_content_disposition == NULL)
236 ap->a_content_disposition = "attachment";
237 fprintf(fo, "Content-Transfer-Encoding: %s\n"
238 "Content-Disposition: %s;\n"
239 " filename=\"",
240 getencoding(convert),
241 ap->a_content_disposition);
242 mime_write(basename, strlen(basename), fo,
243 CONV_TOHDR, TD_NONE, NULL, (size_t)0, NULL, NULL);
244 fwrite("\"\n", sizeof (char), 2, fo);
245 if (ap->a_content_id)
246 fprintf(fo, "Content-ID: %s\n", ap->a_content_id);
247 if (ap->a_content_description)
248 fprintf(fo, "Content-Description: %s\n",
249 ap->a_content_description);
250 putc('\n', fo);
251 #ifdef HAVE_ICONV
252 if (iconvd != (iconv_t)-1) {
253 iconv_close(iconvd);
254 iconvd = (iconv_t)-1;
256 tcs = gettcharset();
257 if ((isclean & (MIME_HASNUL|MIME_CTRLCHAR)) == 0 &&
258 ascncasecmp(contenttype, "text/", 5) == 0 &&
259 isclean & MIME_HIGHBIT &&
260 charset != NULL) {
261 if ((iconvd = iconv_open_ft(charset, tcs)) == (iconv_t)-1 &&
262 errno != 0) {
263 if (errno == EINVAL)
264 fprintf(stderr, catgets(catd, CATSET, 179,
265 "Cannot convert from %s to %s\n"), tcs, charset);
266 else
267 perror("iconv_open");
268 Fclose(fi);
269 return -1;
272 #endif /* HAVE_ICONV */
273 buf = smalloc(bufsize = INFIX_BUF);
274 if (convert == CONV_TOQP
275 #ifdef HAVE_ICONV
276 || iconvd != (iconv_t)-1
277 #endif
279 fflush(fi);
280 count = fsize(fi);
282 for (;;) {
283 if (convert == CONV_TOQP
284 #ifdef HAVE_ICONV
285 || iconvd != (iconv_t)-1
286 #endif
288 if (fgetline(&buf, &bufsize, &count, &sz, fi, 0)
289 == NULL)
290 break;
291 } else {
292 if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
293 break;
295 lastc = buf[sz-1];
296 if (mime_write(buf, sz, fo, convert, TD_ICONV,
297 NULL, (size_t)0, NULL, NULL) == 0)
298 err = -1;
300 if (convert == CONV_TOQP && lastc != '\n')
301 fwrite("=\n", 1, 2, fo);
302 if (ferror(fi))
303 err = -1;
304 Fclose(fi);
305 free(buf);
306 return err;
310 * Try out different character set conversions to attach a file.
312 static int
313 attach_file(struct attachment *ap, FILE *fo, int dosign)
315 char *_wantcharset, *charsets, *ncs;
316 size_t offs = ftell(fo);
318 if (ap->a_charset || (charsets = value("sendcharsets")) == NULL)
319 return attach_file1(ap, fo, dosign);
320 _wantcharset = wantcharset;
321 wantcharset = savestr(charsets);
322 loop: if ((ncs = strchr(wantcharset, ',')) != NULL)
323 *ncs++ = '\0';
324 try: if (attach_file1(ap, fo, dosign) != 0) {
325 if (errno == EILSEQ || errno == EINVAL) {
326 if (ncs && *ncs) {
327 wantcharset = ncs;
328 clearerr(fo);
329 fseek(fo, offs, SEEK_SET);
330 goto loop;
332 if (wantcharset) {
333 if (wantcharset == (char *)-1)
334 wantcharset = NULL;
335 else {
336 wantcharset = (char *)-1;
337 clearerr(fo);
338 fseek(fo, offs, SEEK_SET);
339 goto try;
344 wantcharset = _wantcharset;
345 return 0;
349 * Attach a message to the file buffer.
351 static int
352 attach_message(struct attachment *ap, FILE *fo, int dosign)
354 struct message *mp;
355 (void)dosign;
357 fprintf(fo, "\n--%s\n"
358 "Content-Type: message/rfc822\n"
359 "Content-Disposition: inline\n\n", send_boundary);
360 mp = &message[ap->a_msgno - 1];
361 touch(mp);
362 if (send(mp, fo, 0, NULL, SEND_RFC822, NULL) < 0)
363 return -1;
364 return 0;
368 * Generate the body of a MIME multipart message.
370 static int
371 make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
372 const char *contenttype, const char *charset, int dosign)
374 struct attachment *att;
376 fputs("This is a multi-part message in MIME format.\n", fo);
377 if (fsize(fi) != 0) {
378 char *buf, c = '\n';
379 size_t sz, bufsize, count;
381 fprintf(fo, "\n--%s\n", send_boundary);
382 fprintf(fo, "Content-Type: %s", contenttype);
383 if (charset)
384 fprintf(fo, "; charset=%s", charset);
385 fprintf(fo, "\nContent-Transfer-Encoding: %s\n"
386 "Content-Disposition: inline\n\n",
387 getencoding(convert));
388 buf = smalloc(bufsize = INFIX_BUF);
389 if (convert == CONV_TOQP
390 #ifdef HAVE_ICONV
391 || iconvd != (iconv_t)-1
392 #endif /* HAVE_ICONV */
394 fflush(fi);
395 count = fsize(fi);
397 for (;;) {
398 if (convert == CONV_TOQP
399 #ifdef HAVE_ICONV
400 || iconvd != (iconv_t)-1
401 #endif /* HAVE_ICONV */
403 if (fgetline(&buf, &bufsize, &count, &sz, fi, 0)
404 == NULL)
405 break;
406 } else {
407 sz = fread(buf, sizeof *buf, bufsize, fi);
408 if (sz == 0)
409 break;
411 c = buf[sz - 1];
412 if (mime_write(buf, sz, fo, convert,
413 TD_ICONV, NULL, (size_t)0,
414 NULL, NULL) == 0) {
415 free(buf);
416 return -1;
419 free(buf);
420 if (ferror(fi))
421 return -1;
422 if (c != '\n')
423 putc('\n', fo);
424 if (charset != NULL)
425 put_signature(fo, convert);
427 for (att = hp->h_attach; att != NULL; att = att->a_flink) {
428 if (att->a_msgno) {
429 if (attach_message(att, fo, dosign) != 0)
430 return -1;
431 } else {
432 if (attach_file(att, fo, dosign) != 0)
433 return -1;
436 /* the final boundary with two attached dashes */
437 fprintf(fo, "\n--%s--\n", send_boundary);
438 return 0;
442 * Prepend a header in front of the collected stuff
443 * and return the new file.
445 static FILE *
446 infix(struct header *hp, FILE *fi, int dosign)
448 FILE *nfo, *nfi;
449 char *tempMail;
450 #ifdef HAVE_ICONV
451 char *tcs, *convhdr = NULL;
452 #endif
453 enum mimeclean isclean;
454 enum conversion convert;
455 char *charset = NULL, *contenttype = NULL;
456 int lastc = EOF;
458 if ((nfo = Ftemp(&tempMail, "Rs", "w", 0600, 1)) == NULL) {
459 perror(catgets(catd, CATSET, 178, "temporary mail file"));
460 return(NULL);
462 if ((nfi = Fopen(tempMail, "r")) == NULL) {
463 perror(tempMail);
464 Fclose(nfo);
465 return(NULL);
467 rm(tempMail);
468 Ftfree(&tempMail);
469 convert = get_mime_convert(fi, &contenttype, &charset,
470 &isclean, dosign);
471 #ifdef HAVE_ICONV
472 tcs = gettcharset();
473 if ((convhdr = need_hdrconv(hp, GTO|GSUBJECT|GCC|GBCC|GIDENT)) != 0) {
474 if (iconvd != (iconv_t)-1)
475 iconv_close(iconvd);
476 if ((iconvd = iconv_open_ft(convhdr, tcs)) == (iconv_t)-1
477 && errno != 0) {
478 if (errno == EINVAL)
479 fprintf(stderr, catgets(catd, CATSET, 179,
480 "Cannot convert from %s to %s\n"), tcs, convhdr);
481 else
482 perror("iconv_open");
483 Fclose(nfo);
484 return NULL;
487 #endif /* HAVE_ICONV */
488 if (puthead(hp, nfo,
489 GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA|GUA|GMIME
490 |GMSGID|GIDENT|GREF|GDATE,
491 SEND_MBOX, convert, contenttype, charset)) {
492 Fclose(nfo);
493 Fclose(nfi);
494 #ifdef HAVE_ICONV
495 if (iconvd != (iconv_t)-1) {
496 iconv_close(iconvd);
497 iconvd = (iconv_t)-1;
499 #endif
500 return NULL;
502 #ifdef HAVE_ICONV
503 if (convhdr && iconvd != (iconv_t)-1) {
504 iconv_close(iconvd);
505 iconvd = (iconv_t)-1;
507 if ((isclean & (MIME_HASNUL|MIME_CTRLCHAR)) == 0 &&
508 ascncasecmp(contenttype, "text/", 5) == 0 &&
509 isclean & MIME_HIGHBIT &&
510 charset != NULL) {
511 if (iconvd != (iconv_t)-1)
512 iconv_close(iconvd);
513 if ((iconvd = iconv_open_ft(charset, tcs)) == (iconv_t)-1
514 && errno != 0) {
515 if (errno == EINVAL)
516 fprintf(stderr, catgets(catd, CATSET, 179,
517 "Cannot convert from %s to %s\n"), tcs, charset);
518 else
519 perror("iconv_open");
520 Fclose(nfo);
521 return NULL;
524 #endif
525 if (hp->h_attach != NULL) {
526 if (make_multipart(hp, convert, fi, nfo,
527 contenttype, charset, dosign) != 0) {
528 Fclose(nfo);
529 Fclose(nfi);
530 #ifdef HAVE_ICONV
531 if (iconvd != (iconv_t)-1) {
532 iconv_close(iconvd);
533 iconvd = (iconv_t)-1;
535 #endif
536 return NULL;
538 } else {
539 size_t sz, bufsize, count;
540 char *buf;
542 if (convert == CONV_TOQP
543 #ifdef HAVE_ICONV
544 || iconvd != (iconv_t)-1
545 #endif /* HAVE_ICONV */
547 fflush(fi);
548 count = fsize(fi);
550 buf = smalloc(bufsize = INFIX_BUF);
551 for (;;) {
552 if (convert == CONV_TOQP
553 #ifdef HAVE_ICONV
554 || iconvd != (iconv_t)-1
555 #endif /* HAVE_ICONV */
557 if (fgetline(&buf, &bufsize, &count, &sz, fi, 0)
558 == NULL)
559 break;
560 } else {
561 sz = fread(buf, sizeof *buf, bufsize, fi);
562 if (sz == 0)
563 break;
565 lastc = buf[sz - 1];
566 if (mime_write(buf, sz, nfo, convert,
567 TD_ICONV, NULL, (size_t)0,
568 NULL, NULL) == 0) {
569 Fclose(nfo);
570 Fclose(nfi);
571 #ifdef HAVE_ICONV
572 if (iconvd != (iconv_t)-1) {
573 iconv_close(iconvd);
574 iconvd = (iconv_t)-1;
576 #endif
577 free(buf);
578 return NULL;
581 if (convert == CONV_TOQP && lastc != '\n')
582 fwrite("=\n", 1, 2, nfo);
583 free(buf);
584 if (ferror(fi)) {
585 Fclose(nfo);
586 Fclose(nfi);
587 #ifdef HAVE_ICONV
588 if (iconvd != (iconv_t)-1) {
589 iconv_close(iconvd);
590 iconvd = (iconv_t)-1;
592 #endif
593 return NULL;
595 if (charset != NULL)
596 put_signature(nfo, convert);
598 #ifdef HAVE_ICONV
599 if (iconvd != (iconv_t)-1) {
600 iconv_close(iconvd);
601 iconvd = (iconv_t)-1;
603 #endif
604 fflush(nfo);
605 if (ferror(nfo)) {
606 perror(catgets(catd, CATSET, 180, "temporary mail file"));
607 Fclose(nfo);
608 Fclose(nfi);
609 return NULL;
611 Fclose(nfo);
612 Fclose(fi);
613 fflush(nfi);
614 rewind(nfi);
615 return(nfi);
619 * Save the outgoing mail on the passed file.
622 /*ARGSUSED*/
623 static int
624 savemail(char *name, FILE *fi)
626 FILE *fo;
627 char *buf;
628 size_t bufsize, buflen, count;
629 char *p;
630 time_t now;
631 int prependnl = 0;
632 int error = 0;
634 buf = smalloc(bufsize = LINESIZE);
635 time(&now);
636 if ((fo = Zopen(name, "a+", NULL)) == NULL) {
637 if ((fo = Zopen(name, "wx", NULL)) == NULL) {
638 perror(name);
639 free(buf);
640 return (-1);
642 } else {
643 if (fseek(fo, -2L, SEEK_END) == 0) {
644 switch (fread(buf, sizeof *buf, 2, fo)) {
645 case 2:
646 if (buf[1] != '\n') {
647 prependnl = 1;
648 break;
650 /*FALLTHRU*/
651 case 1:
652 if (buf[0] != '\n')
653 prependnl = 1;
654 break;
655 default:
656 if (ferror(fo)) {
657 perror(name);
658 free(buf);
659 return -1;
662 fflush(fo);
663 if (prependnl) {
664 putc('\n', fo);
665 fflush(fo);
669 fprintf(fo, "From %s %s", myname, ctime(&now));
670 buflen = 0;
671 fflush(fi);
672 rewind(fi);
673 count = fsize(fi);
674 while (fgetline(&buf, &bufsize, &count, &buflen, fi, 0) != NULL) {
675 if (*buf == '>') {
676 p = buf + 1;
677 while (*p == '>')
678 p++;
679 if (strncmp(p, "From ", 5) == 0)
680 /* we got a masked From line */
681 putc('>', fo);
682 } else if (strncmp(buf, "From ", 5) == 0)
683 putc('>', fo);
684 fwrite(buf, sizeof *buf, buflen, fo);
686 if (buflen && *(buf + buflen - 1) != '\n')
687 putc('\n', fo);
688 putc('\n', fo);
689 fflush(fo);
690 if (ferror(fo)) {
691 perror(name);
692 error = -1;
694 if (Fclose(fo) != 0)
695 error = -1;
696 fflush(fi);
697 rewind(fi);
698 free(buf);
699 return error;
703 * Interface between the argument list and the mail1 routine
704 * which does all the dirty work.
706 int
707 mail(struct name *to, struct name *cc, struct name *bcc,
708 char *subject, struct attachment *attach,
709 char *quotefile, int recipient_record, int tflag, int Eflag)
711 struct header head;
712 struct str in, out;
714 memset(&head, 0, sizeof head);
715 /* The given subject may be in RFC1522 format. */
716 if (subject != NULL) {
717 in.s = subject;
718 in.l = strlen(subject);
719 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
720 head.h_subject = out.s;
722 if (tflag == 0) {
723 head.h_to = to;
724 head.h_cc = cc;
725 head.h_bcc = bcc;
727 head.h_attach = attach;
728 mail1(&head, 0, NULL, quotefile, recipient_record, 0, tflag, Eflag);
729 if (subject != NULL)
730 free(out.s);
731 return(0);
735 * Send mail to a bunch of user names. The interface is through
736 * the mail routine below.
738 static int
739 sendmail_internal(void *v, int recipient_record)
741 int Eflag;
742 char *str = v;
743 struct header head;
745 memset(&head, 0, sizeof head);
746 head.h_to = lextract(str, GTO|GFULL);
747 Eflag = value("skipemptybody") != NULL;
748 mail1(&head, 0, NULL, NULL, recipient_record, 0, 0, Eflag);
749 return(0);
752 int
753 sendmail(void *v)
755 return sendmail_internal(v, 0);
758 int
759 Sendmail(void *v)
761 return sendmail_internal(v, 1);
764 static enum okay
765 transfer(struct name *to, FILE *input, struct header *hp)
767 char o[LINESIZE], *cp;
768 struct name *np, *nt;
769 int cnt = 0;
770 FILE *ef;
771 enum okay ok = OKAY;
773 np = to;
774 while (np) {
775 snprintf(o, sizeof o, "smime-encrypt-%s", np->n_name);
776 if ((cp = value(o)) != NULL) {
777 if ((ef = smime_encrypt(input, cp, np->n_name)) != 0) {
778 nt = ndup(np, np->n_type & ~(GFULL|GSKIN));
779 if (start_mta(nt, ef, hp) != OKAY)
780 ok = STOP;
781 Fclose(ef);
782 } else {
783 fprintf(stderr, "Message not sent to <%s>\n",
784 np->n_name);
785 senderr++;
787 rewind(input);
788 if (np->n_flink)
789 np->n_flink->n_blink = np->n_blink;
790 if (np->n_blink)
791 np->n_blink->n_flink = np->n_flink;
792 if (np == to)
793 to = np->n_flink;
794 np = np->n_flink;
795 } else {
796 cnt++;
797 np = np->n_flink;
800 if (cnt) {
801 if (value("smime-force-encryption") ||
802 start_mta(to, input, hp) != OKAY)
803 ok = STOP;
805 return ok;
808 static char **
809 prepare_mta_args(struct name *to)
811 size_t j, i = 4 + smopts_count + count(to) + 1;
812 char **args = salloc(i * sizeof(char*));
814 args[0] = "send-mail";
815 args[1] = "-i";
816 i = 2;
817 if (value("metoo"))
818 args[i++] = "-m";
819 if (value("verbose"))
820 args[i++] = "-v";
821 for (j = 0; j < smopts_count; ++j, ++i)
822 args[i] = smopts[j];
823 for (; to != NULL; to = to->n_flink)
824 if ((to->n_type & GDEL) == 0)
825 args[i++] = to->n_name;
826 args[i] = NULL;
827 return (args);
831 * Start the Mail Transfer Agent
832 * mailing to namelist and stdin redirected to input.
834 static enum okay
835 start_mta(struct name *to, FILE *input, struct header *hp)
837 #ifdef USE_SMTP
838 struct termios otio;
839 int reset_tio;
840 #endif
841 char **args = NULL, *user = NULL, *password = NULL, *skinned = NULL,
842 **t, *cp, *smtp;
843 enum okay ok = STOP;
844 pid_t pid;
845 sigset_t nset;
847 if ((smtp = value("smtp")) == NULL) {
848 args = prepare_mta_args(to);
849 if (debug || value("debug")) {
850 printf(tr(181, "Sendmail arguments:"));
851 for (t = args; *t != NULL; t++)
852 printf(" \"%s\"", *t);
853 printf("\n");
854 return (OKAY);
857 #ifdef USE_SMTP
858 if (smtp != NULL) {
859 skinned = skin(myorigin(hp));
860 if ((user = smtp_auth_var("-user", skinned)) != NULL &&
861 (password = smtp_auth_var("-password",
862 skinned)) == NULL)
863 password = getpassword(&otio, &reset_tio, NULL);
865 #endif
867 * Fork, set up the temporary mail file as standard
868 * input for "mail", and exec with the user list we generated
869 * far above.
871 if ((pid = fork()) == -1) {
872 perror("fork");
873 savedeadletter(input);
874 senderr++;
875 return STOP;
877 if (pid == 0) {
878 sigemptyset(&nset);
879 sigaddset(&nset, SIGHUP);
880 sigaddset(&nset, SIGINT);
881 sigaddset(&nset, SIGQUIT);
882 sigaddset(&nset, SIGTSTP);
883 sigaddset(&nset, SIGTTIN);
884 sigaddset(&nset, SIGTTOU);
885 freopen("/dev/null", "r", stdin);
886 if (smtp != NULL) {
887 prepare_child(&nset, 0, 1);
888 if (smtp_mta(smtp, to, input, hp,
889 user, password, skinned) == 0)
890 _exit(0);
891 } else {
892 prepare_child(&nset, fileno(input), -1);
893 if ((cp = value("sendmail")) != NULL)
894 cp = file_expand(cp);
895 else
896 cp = SENDMAIL;
897 execv(cp, args);
898 perror(cp);
900 savedeadletter(input);
901 fputs(catgets(catd, CATSET, 182,
902 ". . . message not sent.\n"), stderr);
903 _exit(1);
905 if (value("verbose") != NULL || value("sendwait") || debug
906 || value("debug")) {
907 if (wait_child(pid) == 0)
908 ok = OKAY;
909 else
910 senderr++;
911 } else {
912 ok = OKAY;
913 free_child(pid);
915 return ok;
919 * Record outgoing mail if instructed to do so.
921 static enum okay
922 mightrecord(FILE *fp, struct name *to, int recipient_record)
924 char *cp, *cq, *ep;
926 if (recipient_record) {
927 cq = skinned_name(to);
928 cp = salloc(strlen(cq) + 1);
929 strcpy(cp, cq);
930 for (cq = cp; *cq && *cq != '@'; cq++);
931 *cq = '\0';
932 } else
933 cp = value("record");
934 if (cp != NULL) {
935 ep = expand(cp);
936 if (value("outfolder") && *ep != '/' && *ep != '+' &&
937 which_protocol(ep) == PROTO_FILE) {
938 cq = salloc(strlen(cp) + 2);
939 cq[0] = '+';
940 strcpy(&cq[1], cp);
941 cp = cq;
942 ep = expand(cp);
944 if (savemail(ep, fp) != 0) {
945 fprintf(stderr,
946 "Error while saving message to %s - "
947 "message not sent\n", ep);
948 rewind(fp);
949 exit_status |= 1;
950 savedeadletter(fp);
951 return STOP;
954 return OKAY;
958 * Mail a message on standard input to the people indicated
959 * in the passed header. (Internal interface).
961 enum okay
962 mail1(struct header *hp, int printheaders, struct message *quote,
963 char *quotefile, int recipient_record, int doprefix, int tflag,
964 int Eflag)
966 struct name *to;
967 FILE *mtf, *nmtf;
968 enum okay ok = STOP;
969 int dosign = -1;
970 char *charsets, *ncs = NULL, *cp;
972 #ifdef notdef
973 if ((hp->h_to = checkaddrs(hp->h_to)) == NULL) {
974 senderr++;
975 return STOP;
977 #endif
978 if ((cp = value("autocc")) != NULL && *cp)
979 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC|GFULL)));
980 if ((cp = value("autobcc")) != NULL && *cp)
981 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp,GBCC|GFULL)));
983 * Collect user's mail from standard input.
984 * Get the result as mtf.
986 if ((mtf = collect(hp, printheaders, quote, quotefile, doprefix,
987 tflag)) == NULL)
988 return STOP;
989 if (value("interactive") != NULL) {
990 if (((value("bsdcompat") || value("askatend"))
991 && (value("askcc") != NULL ||
992 value("askbcc") != NULL)) ||
993 value("askattach") != NULL ||
994 value("asksign") != NULL) {
995 if (value("askcc") != NULL)
996 grabh(hp, GCC, 1);
997 if (value("askbcc") != NULL)
998 grabh(hp, GBCC, 1);
999 if (value("askattach") != NULL)
1000 hp->h_attach = edit_attachments(hp->h_attach);
1001 if (value("asksign") != NULL)
1002 dosign = yorn("Sign this message (y/n)? ");
1003 } else {
1004 printf(catgets(catd, CATSET, 183, "EOT\n"));
1005 fflush(stdout);
1008 if (fsize(mtf) == 0) {
1009 if (Eflag)
1010 goto out;
1011 if (hp->h_subject == NULL)
1012 printf(catgets(catd, CATSET, 184,
1013 "No message, no subject; hope that's ok\n"));
1014 else if (value("bsdcompat") || value("bsdmsgs"))
1015 printf(catgets(catd, CATSET, 185,
1016 "Null message body; hope that's ok\n"));
1018 if (dosign < 0) {
1019 if (value("smime-sign") != NULL)
1020 dosign = 1;
1021 else
1022 dosign = 0;
1025 * Now, take the user names from the combined
1026 * to and cc lists and do all the alias
1027 * processing.
1029 senderr = 0;
1030 if ((to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)))) == NULL) {
1031 printf(catgets(catd, CATSET, 186, "No recipients specified\n"));
1032 senderr++;
1034 to = fixhead(hp, to);
1035 if (hp->h_charset) {
1036 wantcharset = hp->h_charset;
1037 goto try;
1039 hloop: wantcharset = NULL;
1040 if ((charsets = value("sendcharsets")) != NULL) {
1041 wantcharset = savestr(charsets);
1042 loop: if ((ncs = strchr(wantcharset, ',')) != NULL)
1043 *ncs++ = '\0';
1045 try: if ((nmtf = infix(hp, mtf, dosign)) == NULL) {
1046 if (hp->h_charset && (errno == EILSEQ || errno == EINVAL)) {
1047 rewind(mtf);
1048 hp->h_charset = NULL;
1049 goto hloop;
1051 if (errno == EILSEQ || errno == EINVAL) {
1052 if (ncs && *ncs) {
1053 rewind(mtf);
1054 wantcharset = ncs;
1055 goto loop;
1057 if (wantcharset && value("interactive") == NULL) {
1058 if (wantcharset == (char *)-1)
1059 wantcharset = NULL;
1060 else {
1061 rewind(mtf);
1062 wantcharset = (char *)-1;
1063 goto try;
1067 /* fprintf(stderr, ". . . message lost, sorry.\n"); */
1068 perror("");
1069 fail: senderr++;
1070 rewind(mtf);
1071 savedeadletter(mtf);
1072 fputs(catgets(catd, CATSET, 187,
1073 ". . . message not sent.\n"), stderr);
1074 return STOP;
1076 mtf = nmtf;
1077 if (dosign) {
1078 if ((nmtf = smime_sign(mtf, hp)) == NULL)
1079 goto fail;
1080 Fclose(mtf);
1081 mtf = nmtf;
1084 * Look through the recipient list for names with /'s
1085 * in them which we write to as files directly.
1087 to = outof(to, mtf, hp);
1088 if (senderr)
1089 savedeadletter(mtf);
1090 to = elide(to);
1091 if (count(to) == 0) {
1092 if (senderr == 0)
1093 ok = OKAY;
1094 goto out;
1096 if (mightrecord(mtf, to, recipient_record) != OKAY)
1097 goto out;
1098 ok = transfer(to, mtf, hp);
1099 out:
1100 Fclose(mtf);
1101 return ok;
1105 * Create a Message-Id: header field.
1106 * Use either the host name or the from address.
1108 static void
1109 message_id(FILE *fo, struct header *hp)
1111 time_t now;
1112 struct tm *tmp;
1113 char *h;
1114 size_t rl;
1116 time(&now);
1117 tmp = gmtime(&now);
1118 if ((h = value("hostname")) != NULL)
1119 rl = 24;
1120 else if ((h = skin(myorigin(hp))) != NULL && strchr(h, '@') != NULL)
1121 rl = 16;
1122 else
1123 /* Delivery seems to dependent on a MTA -- it's up to it */
1124 return;
1126 fprintf(fo, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
1127 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
1128 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
1129 getrandstring(rl), (rl == 16 ? '%' : '@'), h);
1132 static const char *weekday_names[] = {
1133 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1136 const char *month_names[] = {
1137 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1138 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
1142 * Create a Date: header field.
1143 * We compare the localtime() and gmtime() results to get the timezone,
1144 * because numeric timezones are easier to read and because $TZ is
1145 * not set on most GNU systems.
1148 mkdate(FILE *fo, const char *field)
1150 time_t t;
1151 struct tm *tmptr;
1152 int tzdiff, tzdiff_hour, tzdiff_min;
1154 time(&t);
1155 tzdiff = t - mktime(gmtime(&t));
1156 tzdiff_hour = (int)(tzdiff / 60);
1157 tzdiff_min = tzdiff_hour % 60;
1158 tzdiff_hour /= 60;
1159 tmptr = localtime(&t);
1160 if (tmptr->tm_isdst > 0)
1161 tzdiff_hour++;
1162 return fprintf(fo, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1163 field,
1164 weekday_names[tmptr->tm_wday],
1165 tmptr->tm_mday, month_names[tmptr->tm_mon],
1166 tmptr->tm_year + 1900, tmptr->tm_hour,
1167 tmptr->tm_min, tmptr->tm_sec,
1168 tzdiff_hour * 100 + tzdiff_min);
1171 static enum okay
1172 putname(char *line, enum gfield w, enum sendaction action, int *gotcha,
1173 char *prefix, FILE *fo, struct name **xp)
1175 struct name *np;
1177 np = lextract(line, GEXTRA|GFULL);
1178 if (xp)
1179 *xp = np;
1180 if (np == NULL)
1181 return 0;
1182 if (fmt(prefix, np, fo, w&(GCOMMA|GFILES), 0, action != SEND_TODISP))
1183 return 1;
1184 if (gotcha)
1185 (*gotcha)++;
1186 return 0;
1189 #define FMT_CC_AND_BCC { \
1190 if (hp->h_cc != NULL && w & GCC) { \
1191 if (fmt("Cc:", hp->h_cc, fo, \
1192 w&(GCOMMA|GFILES), 0, \
1193 action!=SEND_TODISP)) \
1194 return 1; \
1195 gotcha++; \
1197 if (hp->h_bcc != NULL && w & GBCC) { \
1198 if (fmt("Bcc:", hp->h_bcc, fo, \
1199 w&(GCOMMA|GFILES), 0, \
1200 action!=SEND_TODISP)) \
1201 return 1; \
1202 gotcha++; \
1206 * Dump the to, subject, cc header on the
1207 * passed file buffer.
1210 puthead(struct header *hp, FILE *fo, enum gfield w,
1211 enum sendaction action, enum conversion convert,
1212 char *contenttype, char *charset)
1214 int gotcha;
1215 char *addr/*, *cp*/;
1216 int stealthmua;
1217 struct name *np, *fromfield = NULL, *senderfield = NULL;
1220 if ((addr = value("stealthmua")) != NULL) {
1221 stealthmua = (strcmp(addr, "noagent") == 0) ? -1 : 1;
1222 } else
1223 stealthmua = 0;
1224 gotcha = 0;
1225 if (w & GDATE) {
1226 mkdate(fo, "Date"), gotcha++;
1228 if (w & GIDENT) {
1229 if (hp->h_from != NULL) {
1230 if (fmt("From:", hp->h_from, fo, w&(GCOMMA|GFILES), 0,
1231 action!=SEND_TODISP))
1232 return 1;
1233 gotcha++;
1234 fromfield = hp->h_from;
1235 } else if ((addr = myaddrs(hp)) != NULL)
1236 if (putname(addr, w, action, &gotcha, "From:", fo,
1237 &fromfield))
1238 return 1;
1239 if (((addr = hp->h_organization) != NULL ||
1240 (addr = value("ORGANIZATION")) != NULL)
1241 && strlen(addr) > 0) {
1242 fwrite("Organization: ", sizeof (char), 14, fo);
1243 if (mime_write(addr, strlen(addr), fo,
1244 action == SEND_TODISP ?
1245 CONV_NONE:CONV_TOHDR,
1246 action == SEND_TODISP ?
1247 TD_ISPR|TD_ICONV:TD_ICONV,
1248 NULL, (size_t)0,
1249 NULL, NULL) == 0)
1250 return 1;
1251 gotcha++;
1252 putc('\n', fo);
1254 if (hp->h_replyto != NULL) {
1255 if (fmt("Reply-To:", hp->h_replyto, fo,
1256 w&(GCOMMA|GFILES), 0,
1257 action!=SEND_TODISP))
1258 return 1;
1259 gotcha++;
1260 } else if ((addr = value("replyto")) != NULL)
1261 if (putname(addr, w, action, &gotcha, "Reply-To:", fo,
1262 NULL))
1263 return 1;
1264 if (hp->h_sender != NULL) {
1265 if (fmt("Sender:", hp->h_sender, fo,
1266 w&(GCOMMA|GFILES), 0,
1267 action!=SEND_TODISP))
1268 return 1;
1269 gotcha++;
1270 senderfield = hp->h_sender;
1271 } else if ((addr = value("sender")) != NULL)
1272 if (putname(addr, w, action, &gotcha, "Sender:", fo,
1273 &senderfield))
1274 return 1;
1275 if (check_from_and_sender(fromfield, senderfield))
1276 return 1;
1278 if (hp->h_to != NULL && w & GTO) {
1279 if (fmt("To:", hp->h_to, fo, w&(GCOMMA|GFILES), 0,
1280 action!=SEND_TODISP))
1281 return 1;
1282 gotcha++;
1284 if (value("bsdcompat") == NULL && value("bsdorder") == NULL)
1285 FMT_CC_AND_BCC
1286 if (hp->h_subject != NULL && w & GSUBJECT) {
1287 fwrite("Subject: ", sizeof (char), 9, fo);
1288 if (ascncasecmp(hp->h_subject, "re: ", 4) == 0) {
1289 fwrite("Re: ", sizeof (char), 4, fo);
1290 if (strlen(hp->h_subject + 4) > 0 &&
1291 mime_write(hp->h_subject + 4,
1292 strlen(hp->h_subject + 4),
1293 fo, action == SEND_TODISP ?
1294 CONV_NONE:CONV_TOHDR,
1295 action == SEND_TODISP ?
1296 TD_ISPR|TD_ICONV:TD_ICONV,
1297 NULL, (size_t)0,
1298 NULL, NULL) == 0)
1299 return 1;
1300 } else if (*hp->h_subject) {
1301 if (mime_write(hp->h_subject,
1302 strlen(hp->h_subject),
1303 fo, action == SEND_TODISP ?
1304 CONV_NONE:CONV_TOHDR,
1305 action == SEND_TODISP ?
1306 TD_ISPR|TD_ICONV:TD_ICONV,
1307 NULL, (size_t)0,
1308 NULL, NULL) == 0)
1309 return 1;
1311 gotcha++;
1312 fwrite("\n", sizeof (char), 1, fo);
1314 if (value("bsdcompat") || value("bsdorder"))
1315 FMT_CC_AND_BCC
1316 if (w & GMSGID && stealthmua <= 0)
1317 message_id(fo, hp), gotcha++;
1318 if ((np = hp->h_ref) != NULL && w & GREF) {
1319 fmt("References:", np, fo, 0, 1, 0);
1320 if (np->n_name) {
1321 while (np->n_flink)
1322 np = np->n_flink;
1323 if (is_addr_invalid(np, 0) == 0) {
1324 fprintf(fo, "In-Reply-To: %s\n", np->n_name);
1325 gotcha++;
1329 if (w & GUA && stealthmua == 0)
1330 fprintf(fo, "User-Agent: %s %s\n", uagent, version), gotcha++;
1331 if (w & GMIME) {
1332 fputs("MIME-Version: 1.0\n", fo), gotcha++;
1333 if (hp->h_attach != NULL) {
1334 makeboundary();
1335 fprintf(fo, "Content-Type: multipart/mixed;\n"
1336 " boundary=\"%s\"\n", send_boundary);
1337 } else {
1338 fprintf(fo, "Content-Type: %s", contenttype);
1339 if (charset)
1340 fprintf(fo, "; charset=%s", charset);
1341 fprintf(fo, "\nContent-Transfer-Encoding: %s\n",
1342 getencoding(convert));
1345 if (gotcha && w & GNL)
1346 putc('\n', fo);
1347 return(0);
1351 * Format the given header line to not exceed 72 characters.
1353 static int
1354 fmt(char *str, struct name *np, FILE *fo, int flags, int dropinvalid,
1355 int domime)
1357 enum {
1358 m_INIT = 1<<0,
1359 m_COMMA = 1<<1,
1360 m_NOPF = 1<<2,
1361 m_CSEEN = 1<<3
1362 } m = (flags & GCOMMA) ? m_COMMA : 0;
1363 int col, len;
1365 col = strlen(str);
1366 if (col) {
1367 fwrite(str, sizeof *str, strlen(str), fo);
1368 if ((flags&GFILES) == 0 && ! value("add-file-recipients") &&
1369 ((col == 3 && ((asccasecmp(str, "to:") == 0) ||
1370 asccasecmp(str, "cc:") == 0)) ||
1371 (col == 4 && asccasecmp(str, "bcc:") == 0) ||
1372 (col == 10 &&
1373 asccasecmp(str, "Resent-To:") == 0)))
1374 m |= m_NOPF;
1376 for (; np != NULL; np = np->n_flink) {
1377 if ((m & m_NOPF) && is_fileorpipe_addr(np))
1378 continue;
1379 if (is_addr_invalid(np, ! dropinvalid)) {
1380 if (dropinvalid)
1381 continue;
1382 else
1383 return (1);
1385 if ((m & (m_INIT | m_COMMA)) == (m_INIT | m_COMMA)) {
1386 putc(',', fo);
1387 m |= m_CSEEN;
1388 ++col;
1390 len = strlen(np->n_fullname);
1391 ++col; /* The separating space */
1392 if ((m & m_INIT) && col > 1 && col + len > 72) {
1393 fputs("\n ", fo);
1394 col = 1;
1395 m &= ~m_CSEEN;
1396 } else
1397 putc(' ', fo);
1398 m = (m & ~m_CSEEN) | m_INIT;
1399 len = mime_write(np->n_fullname,
1400 len, fo,
1401 domime?CONV_TOHDR_A:CONV_NONE,
1402 TD_ICONV, NULL, (size_t)0,
1403 NULL, NULL);
1404 col += len;
1406 putc('\n', fo);
1407 return (0);
1411 * Rewrite a message for resending, adding the Resent-Headers.
1413 static int
1414 infix_resend(FILE *fi, FILE *fo, struct message *mp, struct name *to,
1415 int add_resent)
1417 size_t count;
1418 char *buf = NULL, *cp/*, *cp2*/;
1419 size_t c, bufsize = 0;
1420 struct name *fromfield = NULL, *senderfield = NULL;
1422 count = mp->m_size;
1424 * Write the Resent-Fields.
1426 if (add_resent) {
1427 fputs("Resent-", fo);
1428 mkdate(fo, "Date");
1429 if ((cp = myaddrs(NULL)) != NULL) {
1430 if (putname(cp, GCOMMA, SEND_MBOX, NULL,
1431 "Resent-From:", fo, &fromfield))
1432 return 1;
1434 if ((cp = value("sender")) != NULL) {
1435 if (putname(cp, GCOMMA, SEND_MBOX, NULL,
1436 "Resent-Sender:", fo, &senderfield))
1437 return 1;
1439 if (fmt("Resent-To:", to, fo, 1, 1, 0)) {
1440 if (buf)
1441 free(buf);
1442 return 1;
1444 if ((cp = value("stealthmua")) == NULL ||
1445 strcmp(cp, "noagent") == 0) {
1446 fputs("Resent-", fo);
1447 message_id(fo, NULL);
1450 if (check_from_and_sender(fromfield, senderfield))
1451 return 1;
1453 * Write the original headers.
1455 while (count > 0) {
1456 if ((cp = foldergets(&buf, &bufsize, &count, &c, fi)) == NULL)
1457 break;
1458 if (ascncasecmp("status: ", buf, 8) != 0
1459 && strncmp("From ", buf, 5) != 0) {
1460 fwrite(buf, sizeof *buf, c, fo);
1462 if (count > 0 && *buf == '\n')
1463 break;
1466 * Write the message body.
1468 while (count > 0) {
1469 if (foldergets(&buf, &bufsize, &count, &c, fi) == NULL)
1470 break;
1471 if (count == 0 && *buf == '\n')
1472 break;
1473 fwrite(buf, sizeof *buf, c, fo);
1475 if (buf)
1476 free(buf);
1477 if (ferror(fo)) {
1478 perror(catgets(catd, CATSET, 188, "temporary mail file"));
1479 return 1;
1481 return 0;
1484 enum okay
1485 resend_msg(struct message *mp, struct name *to, int add_resent)
1487 FILE *ibuf, *nfo, *nfi;
1488 char *tempMail;
1489 struct header head;
1490 enum okay ok = STOP;
1492 memset(&head, 0, sizeof head);
1493 if ((to = checkaddrs(to)) == NULL) {
1494 senderr++;
1495 return STOP;
1497 if ((nfo = Ftemp(&tempMail, "Rs", "w", 0600, 1)) == NULL) {
1498 senderr++;
1499 perror(catgets(catd, CATSET, 189, "temporary mail file"));
1500 return STOP;
1502 if ((nfi = Fopen(tempMail, "r")) == NULL) {
1503 senderr++;
1504 perror(tempMail);
1505 return STOP;
1507 rm(tempMail);
1508 Ftfree(&tempMail);
1509 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1510 return STOP;
1511 head.h_to = to;
1512 to = fixhead(&head, to);
1513 if (infix_resend(ibuf, nfo, mp, head.h_to, add_resent) != 0) {
1514 senderr++;
1515 rewind(nfo);
1516 savedeadletter(nfi);
1517 fputs(catgets(catd, CATSET, 190,
1518 ". . . message not sent.\n"), stderr);
1519 Fclose(nfo);
1520 Fclose(nfi);
1521 return STOP;
1523 fflush(nfo);
1524 rewind(nfo);
1525 Fclose(nfo);
1526 to = outof(to, nfi, &head);
1527 if (senderr)
1528 savedeadletter(nfi);
1529 to = elide(to);
1530 if (count(to) != 0) {
1531 if (value("record-resent") == NULL ||
1532 mightrecord(nfi, to, 0) == OKAY)
1533 ok = transfer(to, nfi, NULL);
1534 } else if (senderr == 0)
1535 ok = OKAY;
1536 Fclose(nfi);
1537 return ok;