README: tweaks
[s-mailx.git] / sendout.c
blob407481978cb8305f68c9777057cc9995e1abc85e
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Mail to others.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 #include <fcntl.h>
46 #undef SEND_LINESIZE
47 #define SEND_LINESIZE \
48 ((1024 / B64_ENCODE_INPUT_PER_LINE) * B64_ENCODE_INPUT_PER_LINE)
50 static char const *__sendout_ident; /* TODO temporary hack; rewrite puthead() */
51 static char * _sendout_boundary;
52 static bool_t _sendout_error;
54 static enum okay _putname(char const *line, enum gfield w,
55 enum sendaction action, size_t *gotcha,
56 char const *prefix, FILE *fo, struct name **xp);
58 /* Get an encoding flag based on the given string */
59 static char const * _get_encoding(const enum conversion convert);
61 /* Write an attachment to the file buffer, converting to MIME */
62 static int _attach_file(struct attachment *ap, FILE *fo);
63 static int __attach_file(struct attachment *ap, FILE *fo);
65 /* There are non-local receivers, collect credentials etc. */
66 static bool_t _sendbundle_setup_creds(struct sendbundle *sbpm,
67 bool_t signing_caps);
69 /* Prepare arguments for the MTA (non *smtp* mode) */
70 static char const ** _prepare_mta_args(struct name *to, struct header *hp);
72 /* Fix the header by glopping all of the expanded names from the distribution
73 * list into the appropriate fields */
74 static struct name * fixhead(struct header *hp, struct name *tolist);
76 /* Put the signature file at fo. TODO layer rewrite: *integrate in body*!! */
77 static int put_signature(FILE *fo, int convert);
79 /* Attach a message to the file buffer */
80 static int attach_message(struct attachment *ap, FILE *fo);
82 /* Generate the body of a MIME multipart message */
83 static int make_multipart(struct header *hp, int convert, FILE *fi,
84 FILE *fo, char const *contenttype, char const *charset);
86 /* Prepend a header in front of the collected stuff and return the new file */
87 static FILE * infix(struct header *hp, FILE *fi);
89 /* Check wether Disposition-Notification-To: is desired */
90 static bool_t _check_dispo_notif(struct name *mdn, struct header *hp,
91 FILE *fo);
93 /* Save the outgoing mail on the passed file */
94 static int savemail(char const *name, FILE *fi);
96 /* Send mail to a bunch of user names. The interface is through mail() */
97 static int sendmail_internal(void *v, int recipient_record);
99 /* */
100 static bool_t _transfer(struct sendbundle *sbp);
102 /* Start the MTA mailing */
103 static bool_t start_mta(struct sendbundle *sbp);
105 /* Record outgoing mail if instructed to do so; in *record* unless to is set */
106 static bool_t mightrecord(FILE *fp, struct name *to);
108 /* Create a Message-Id: header field. Use either host name or from address */
109 static void _message_id(FILE *fo, struct header *hp);
111 /* Format the given header line to not exceed 72 characters */
112 static int fmt(char const *str, struct name *np, FILE *fo, int comma,
113 int dropinvalid, int domime);
115 /* Rewrite a message for resending, adding the Resent-Headers */
116 static int infix_resend(FILE *fi, FILE *fo, struct message *mp,
117 struct name *to, int add_resent);
119 static enum okay
120 _putname(char const *line, enum gfield w, enum sendaction action,
121 size_t *gotcha, char const *prefix, FILE *fo, struct name **xp)
123 struct name *np;
124 enum okay rv = STOP;
125 NYD_ENTER;
127 np = lextract(line, GEXTRA | GFULL);
128 if (xp != NULL)
129 *xp = np;
130 if (np == NULL)
132 else if (fmt(prefix, np, fo, w & GCOMMA, 0, (action != SEND_TODISP)))
133 rv = OKAY;
134 else if (gotcha != NULL)
135 ++(*gotcha);
136 NYD_LEAVE;
137 return rv;
140 static char const *
141 _get_encoding(enum conversion const convert)
143 char const *rv;
144 NYD_ENTER;
146 switch (convert) {
147 case CONV_7BIT: rv = "7bit"; break;
148 case CONV_8BIT: rv = "8bit"; break;
149 case CONV_TOQP: rv = "quoted-printable"; break;
150 case CONV_TOB64: rv = "base64"; break;
151 default: rv = NULL; break;
153 NYD_LEAVE;
154 return rv;
157 static int
158 _attach_file(struct attachment *ap, FILE *fo)
160 /* TODO of course, the MIME classification needs to performed once
161 * TODO only, not for each and every charset anew ... ;-// */
162 char *charset_iter_orig[2];
163 long offs;
164 int err = 0;
165 NYD_ENTER;
167 /* Is this already in target charset? Simply copy over */
168 if (ap->a_conv == AC_TMPFILE) {
169 err = __attach_file(ap, fo);
170 Fclose(ap->a_tmpf);
171 DBG( ap->a_tmpf = NULL; )
172 goto jleave;
175 /* If we don't apply charset conversion at all (fixed input=ouput charset)
176 * we also simply copy over, since it's the users desire */
177 if (ap->a_conv == AC_FIX_INCS) {
178 ap->a_charset = ap->a_input_charset;
179 err = __attach_file(ap, fo);
180 goto jleave;
183 /* Otherwise we need to iterate over all possible output charsets */
184 if ((offs = ftell(fo)) == -1) {
185 err = EIO;
186 goto jleave;
188 charset_iter_recurse(charset_iter_orig);
189 for (charset_iter_reset(NULL);; charset_iter_next()) {
190 if (!charset_iter_is_valid()) {
191 err = EILSEQ;
192 break;
194 err = __attach_file(ap, fo);
195 if (err == 0 || (err != EILSEQ && err != EINVAL))
196 break;
197 clearerr(fo);
198 if (fseek(fo, offs, SEEK_SET) == -1) {
199 err = EIO;
200 break;
202 if (ap->a_conv != AC_DEFAULT) {
203 err = EILSEQ;
204 break;
206 ap->a_charset = NULL;
208 charset_iter_restore(charset_iter_orig);
209 jleave:
210 NYD_LEAVE;
211 return err;
214 static int
215 __attach_file(struct attachment *ap, FILE *fo) /* XXX linelength */
217 int err = 0, do_iconv;
218 FILE *fi;
219 char const *charset;
220 enum conversion convert;
221 char *buf;
222 size_t bufsize, lncnt, inlen;
223 NYD_ENTER;
225 /* Either charset-converted temporary file, or plain path */
226 if (ap->a_conv == AC_TMPFILE) {
227 fi = ap->a_tmpf;
228 assert(ftell(fi) == 0);
229 } else if ((fi = Fopen(ap->a_name, "r")) == NULL) {
230 err = errno;
231 perror(ap->a_name);
232 goto jleave;
235 /* MIME part header for attachment */
236 { char const *bn = ap->a_name, *ct;
238 if ((ct = strrchr(bn, '/')) != NULL)
239 bn = ++ct;
240 ct = ap->a_content_type;
241 charset = ap->a_charset;
242 convert = mime_classify_file(fi, (char const**)&ct, &charset, &do_iconv);
243 if (charset == NULL || ap->a_conv == AC_FIX_INCS ||
244 ap->a_conv == AC_TMPFILE)
245 do_iconv = 0;
247 if (fprintf(fo, "\n--%s\nContent-Type: %s", _sendout_boundary, ct) == -1)
248 goto jerr_header;
250 if (charset == NULL) {
251 if (putc('\n', fo) == EOF)
252 goto jerr_header;
253 } else if (fprintf(fo, "; charset=%s\n", charset) == -1)
254 goto jerr_header;
256 if (fprintf(fo, "Content-Transfer-Encoding: %s\n"
257 "Content-Disposition: %s;\n filename=\"",
258 _get_encoding(convert), ap->a_content_disposition) == -1)
259 goto jerr_header;
260 if (xmime_write(bn, strlen(bn), fo, CONV_TOHDR, TD_NONE, NULL) < 0)
261 goto jerr_header;
262 if (fwrite("\"\n", sizeof(char), 2, fo) != 2 * sizeof(char))
263 goto jerr_header;
265 if ((bn = ap->a_content_id) != NULL &&
266 fprintf(fo, "Content-ID: %s\n", bn) == -1)
267 goto jerr_header;
269 if ((bn = ap->a_content_description) != NULL &&
270 fprintf(fo, "Content-Description: %s\n", bn) == -1)
271 goto jerr_header;
273 if (putc('\n', fo) == EOF) {
274 jerr_header:
275 err = errno;
276 goto jerr_fclose;
280 #ifdef HAVE_ICONV
281 if (iconvd != (iconv_t)-1)
282 n_iconv_close(iconvd);
283 if (do_iconv) {
284 char const *tcs = charset_get_lc();
285 if (asccasecmp(charset, tcs) &&
286 (iconvd = n_iconv_open(charset, tcs)) == (iconv_t)-1 &&
287 (err = errno) != 0) {
288 if (err == EINVAL)
289 fprintf(stderr, _("Cannot convert from %s to %s\n"), tcs, charset);
290 else
291 perror("iconv_open");
292 goto jerr_fclose;
295 #endif
297 bufsize = SEND_LINESIZE;
298 buf = smalloc(bufsize);
299 if (convert == CONV_TOQP
300 #ifdef HAVE_ICONV
301 || iconvd != (iconv_t)-1
302 #endif
304 lncnt = fsize(fi);
305 for (;;) {
306 if (convert == CONV_TOQP
307 #ifdef HAVE_ICONV
308 || iconvd != (iconv_t)-1
309 #endif
311 if (fgetline(&buf, &bufsize, &lncnt, &inlen, fi, 0) == NULL)
312 break;
313 } else if ((inlen = fread(buf, sizeof *buf, bufsize, fi)) == 0)
314 break;
315 if (xmime_write(buf, inlen, fo, convert, TD_ICONV, NULL) < 0) {
316 err = errno;
317 goto jerr;
320 if (ferror(fi))
321 err = EDOM;
322 jerr:
323 free(buf);
324 jerr_fclose:
325 if (ap->a_conv != AC_TMPFILE)
326 Fclose(fi);
327 jleave:
328 NYD_LEAVE;
329 return err;
332 static bool_t
333 _sendbundle_setup_creds(struct sendbundle *sbp, bool_t signing_caps)
335 bool_t v15, rv = FAL0;
336 char *shost, *from;
337 #ifdef HAVE_SMTP
338 char *smtp;
339 #endif
340 NYD_ENTER;
342 v15 = ok_blook(v15_compat);
343 shost = (v15 ? ok_vlook(smtp_hostname) : NULL);
344 from = ((signing_caps || !v15 || shost == NULL)
345 ? skin(myorigin(sbp->sb_hp)) : NULL);
347 if (signing_caps) {
348 if (from == NULL) {
349 #ifdef HAVE_SSL
350 fprintf(stderr, _("No *from* address for signing specified\n"));
351 goto jleave;
352 #endif
353 } else
354 sbp->sb_signer.l = strlen(sbp->sb_signer.s = from);
357 #ifdef HAVE_SMTP
358 if ((smtp = ok_vlook(smtp)) == NULL) {
359 rv = TRU1;
360 goto jleave;
363 if (!url_parse(&sbp->sb_url, CPROTO_SMTP, smtp))
364 goto jleave;
366 if (v15) {
367 if (shost == NULL) {
368 assert(from != NULL);
369 sbp->sb_url.url_u_h.l = strlen(sbp->sb_url.url_u_h.s = from);
371 else
372 __sendout_ident = sbp->sb_url.url_u_h.s;
373 if (!ccred_lookup(&sbp->sb_ccred, &sbp->sb_url))
374 goto jleave;
375 } else {
376 if (sbp->sb_url.url_had_user || sbp->sb_url.url_pass.s != NULL) {
377 fprintf(stderr, "New-style URL used without *v15-compat* being set\n");
378 goto jleave;
380 assert(from != NULL);
381 if (!ccred_lookup_old(&sbp->sb_ccred, CPROTO_SMTP, from))
382 goto jleave;
383 sbp->sb_url.url_u_h.l = strlen(sbp->sb_url.url_u_h.s = from);
386 rv = TRU1;
387 #endif /* HAVE_SMTP */
388 #if defined HAVE_SSL || defined HAVE_SMTP
389 jleave:
390 #endif
391 NYD_LEAVE;
392 return rv;
395 static char const **
396 _prepare_mta_args(struct name *to, struct header *hp)
398 size_t vas_count, i, j;
399 char **vas, *cp;
400 char const **args;
401 NYD_ENTER;
403 if ((cp = ok_vlook(sendmail_arguments)) == NULL) {
404 vas_count = 0;
405 vas = NULL;
406 } else {
407 j = strlen(cp);
408 vas = ac_alloc(sizeof(*vas) * (j >> 1));
409 vas_count = (size_t)getrawlist(cp, j, vas, (int)(j >> 1), TRU1);
412 i = 4 + smopts_count + vas_count + 2 + count(to) + 1;
413 args = salloc(i * sizeof(char*));
415 args[0] = ok_vlook(sendmail_progname);
416 if (args[0] == NULL || *args[0] == '\0')
417 args[0] = SENDMAIL_PROGNAME;
419 args[1] = "-i";
420 i = 2;
421 if (ok_blook(metoo))
422 args[i++] = "-m";
423 if (options & OPT_VERB)
424 args[i++] = "-v";
426 for (j = 0; j < smopts_count; ++j, ++i)
427 args[i] = smopts[j];
429 for (j = 0; j < vas_count; ++j, ++i)
430 args[i] = vas[j];
432 /* -r option? We may only pass skinned addresses */
433 if (options & OPT_r_FLAG) {
434 if (option_r_arg[0] != '\0')
435 cp = option_r_arg;
436 else if (hp != NULL) {
437 /* puthead() did it, then */
438 assert(hp->h_from != NULL);
439 cp = hp->h_from->n_name;
440 } else
441 cp = skin(myorigin(NULL)); /* XXX ugh! ugh!! */
442 if (cp != NULL) { /* XXX ugh! */
443 args[i++] = "-f";
444 args[i++] = cp;
448 /* Receivers follow */
449 for (; to != NULL; to = to->n_flink)
450 if (!(to->n_type & GDEL))
451 args[i++] = to->n_name;
452 args[i] = NULL;
454 if (vas != NULL)
455 ac_free(vas);
456 NYD_LEAVE;
457 return args;
460 static struct name *
461 fixhead(struct header *hp, struct name *tolist)
463 struct name **npp, *np;
464 NYD_ENTER;
466 tolist = elide(tolist);
468 hp->h_to = hp->h_cc = hp->h_bcc = NULL;
469 for (np = tolist; np != NULL; np = np->n_flink) {
470 switch (np->n_type & (GDEL | GMASK)) {
471 case GTO: npp = &hp->h_to; break;
472 case GCC: npp = &hp->h_cc; break;
473 case GBCC: npp = &hp->h_bcc; break;
474 default: continue;
476 *npp = cat(*npp, ndup(np, np->n_type | GFULL));
478 NYD_LEAVE;
479 return tolist;
482 static int
483 put_signature(FILE *fo, int convert)
485 char buf[SEND_LINESIZE], *sig, c = '\n';
486 FILE *fsig;
487 size_t sz;
488 int rv;
489 NYD_ENTER;
491 if ((sig = ok_vlook(signature)) == NULL || *sig == '\0') {
492 rv = 0;
493 goto jleave;
495 rv = -1;
497 if ((sig = file_expand(sig)) == NULL)
498 goto jleave;
500 if ((fsig = Fopen(sig, "r")) == NULL) {
501 perror(sig);
502 goto jleave;
504 while ((sz = fread(buf, sizeof *buf, SEND_LINESIZE, fsig)) != 0) {
505 c = buf[sz - 1];
506 if (xmime_write(buf, sz, fo, convert, TD_NONE, NULL) < 0)
507 goto jerr;
509 if (ferror(fsig)) {
510 jerr:
511 perror(sig);
512 Fclose(fsig);
513 goto jleave;
515 Fclose(fsig);
516 if (c != '\n')
517 putc('\n', fo);
519 rv = 0;
520 jleave:
521 NYD_LEAVE;
522 return rv;
525 static int
526 attach_message(struct attachment *ap, FILE *fo)
528 struct message *mp;
529 char const *ccp;
530 int rv;
531 NYD_ENTER;
533 fprintf(fo, "\n--%s\nContent-Type: message/rfc822\n"
534 "Content-Disposition: inline\n", _sendout_boundary);
535 if ((ccp = ap->a_content_description) != NULL)
536 fprintf(fo, "Content-Description: %s\n", ccp);
537 fputc('\n', fo);
539 mp = message + ap->a_msgno - 1;
540 touch(mp);
541 rv = (sendmp(mp, fo, 0, NULL, SEND_RFC822, NULL) < 0) ? -1 : 0;
542 NYD_LEAVE;
543 return rv;
546 static int
547 make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
548 char const *contenttype, char const *charset)
550 struct attachment *att;
551 int rv = -1;
552 NYD_ENTER;
554 fputs("This is a multi-part message in MIME format.\n", fo);
555 if (fsize(fi) != 0) {
556 char *buf;
557 size_t sz, bufsize, cnt;
559 fprintf(fo, "\n--%s\n", _sendout_boundary);
560 fprintf(fo, "Content-Type: %s", contenttype);
561 if (charset != NULL)
562 fprintf(fo, "; charset=%s", charset);
563 fprintf(fo, "\nContent-Transfer-Encoding: %s\n"
564 "Content-Disposition: inline\n\n", _get_encoding(convert));
566 buf = smalloc(bufsize = SEND_LINESIZE);
567 if (convert == CONV_TOQP
568 #ifdef HAVE_ICONV
569 || iconvd != (iconv_t)-1
570 #endif
572 fflush(fi);
573 cnt = fsize(fi);
575 for (;;) {
576 if (convert == CONV_TOQP
577 #ifdef HAVE_ICONV
578 || iconvd != (iconv_t)-1
579 #endif
581 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
582 break;
583 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
584 break;
586 if (xmime_write(buf, sz, fo, convert, TD_ICONV, NULL) < 0) {
587 free(buf);
588 goto jleave;
591 free(buf);
593 if (ferror(fi))
594 goto jleave;
595 if (charset != NULL)
596 put_signature(fo, convert);
599 for (att = hp->h_attach; att != NULL; att = att->a_flink) {
600 if (att->a_msgno) {
601 if (attach_message(att, fo) != 0)
602 goto jleave;
603 } else if (_attach_file(att, fo) != 0)
604 goto jleave;
607 /* the final boundary with two attached dashes */
608 fprintf(fo, "\n--%s--\n", _sendout_boundary);
609 rv = 0;
610 jleave:
611 NYD_LEAVE;
612 return rv;
615 static FILE *
616 infix(struct header *hp, FILE *fi) /* TODO check */
618 FILE *nfo, *nfi = NULL;
619 char *tempMail;
620 char const *contenttype, *charset = NULL;
621 enum conversion convert;
622 int do_iconv = 0, err;
623 #ifdef HAVE_ICONV
624 char const *tcs, *convhdr = NULL;
625 #endif
626 NYD_ENTER;
628 if ((nfo = Ftmp(&tempMail, "infix", OF_WRONLY | OF_HOLDSIGS | OF_REGISTER,
629 0600)) == NULL) {
630 perror(_("temporary mail file"));
631 goto jleave;
633 if ((nfi = Fopen(tempMail, "r")) == NULL) {
634 perror(tempMail);
635 Fclose(nfo);
637 Ftmp_release(&tempMail);
638 if (nfi == NULL)
639 goto jleave;
641 contenttype = "text/plain"; /* XXX mail body - always text/plain, want XX? */
642 convert = mime_classify_file(fi, &contenttype, &charset, &do_iconv);
644 #ifdef HAVE_ICONV
645 tcs = charset_get_lc();
646 if ((convhdr = need_hdrconv(hp, GTO | GSUBJECT | GCC | GBCC | GIDENT))) {
647 if (iconvd != (iconv_t)-1) /* XXX */
648 n_iconv_close(iconvd);
649 if (asccasecmp(convhdr, tcs) != 0 &&
650 (iconvd = n_iconv_open(convhdr, tcs)) == (iconv_t)-1 &&
651 (err = errno) != 0)
652 goto jiconv_err;
654 #endif
655 if (puthead(hp, nfo,
656 (GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA | GUA | GMIME | GMSGID |
657 GIDENT | GREF | GDATE), SEND_MBOX, convert, contenttype, charset))
658 goto jerr;
659 #ifdef HAVE_ICONV
660 if (iconvd != (iconv_t)-1)
661 n_iconv_close(iconvd);
662 #endif
664 #ifdef HAVE_ICONV
665 if (do_iconv && charset != NULL) { /*TODO charset->mime_classify_file*/
666 if (asccasecmp(charset, tcs) != 0 &&
667 (iconvd = n_iconv_open(charset, tcs)) == (iconv_t)-1 &&
668 (err = errno) != 0) {
669 jiconv_err:
670 if (err == EINVAL)
671 fprintf(stderr, _("Cannot convert from %s to %s\n"), tcs, charset);
672 else
673 perror("iconv_open");
674 goto jerr;
677 #endif
679 if (hp->h_attach != NULL) {
680 if (make_multipart(hp, convert, fi, nfo, contenttype, charset) != 0)
681 goto jerr;
682 } else {
683 size_t sz, bufsize, cnt;
684 char *buf;
686 if (convert == CONV_TOQP
687 #ifdef HAVE_ICONV
688 || iconvd != (iconv_t)-1
689 #endif
691 fflush(fi);
692 cnt = fsize(fi);
694 buf = smalloc(bufsize = SEND_LINESIZE);
695 for (err = 0;;) {
696 if (convert == CONV_TOQP
697 #ifdef HAVE_ICONV
698 || iconvd != (iconv_t)-1
699 #endif
701 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
702 break;
703 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
704 break;
705 if (xmime_write(buf, sz, nfo, convert, TD_ICONV, NULL) < 0) {
706 err = 1;
707 break;
710 free(buf);
712 if (err || ferror(fi)) {
713 jerr:
714 Fclose(nfo);
715 Fclose(nfi);
716 #ifdef HAVE_ICONV
717 if (iconvd != (iconv_t)-1)
718 n_iconv_close(iconvd);
719 #endif
720 nfi = NULL;
721 goto jleave;
723 if (charset != NULL)
724 put_signature(nfo, convert); /* XXX if (text/) !! */
727 #ifdef HAVE_ICONV
728 if (iconvd != (iconv_t)-1)
729 n_iconv_close(iconvd);
730 #endif
732 fflush(nfo);
733 if ((err = ferror(nfo)))
734 perror(_("temporary mail file"));
735 Fclose(nfo);
736 if (!err) {
737 fflush_rewind(nfi);
738 Fclose(fi);
739 } else {
740 Fclose(nfi);
741 nfi = NULL;
743 jleave:
744 NYD_LEAVE;
745 return nfi;
748 static bool_t
749 _check_dispo_notif(struct name *mdn, struct header *hp, FILE *fo)
751 char const *from;
752 bool_t rv = TRU1;
753 NYD_ENTER;
755 /* TODO smtp_disposition_notification (RFC 3798): relation to return-path
756 * TODO not yet checked */
757 if (!ok_blook(disposition_notification_send))
758 goto jleave;
760 if (mdn != NULL && mdn != (struct name*)0x1)
761 from = mdn->n_name;
762 else if ((from = myorigin(hp)) == NULL) {
763 if (options & OPT_D_V)
764 fprintf(stderr, _("*disposition-notification-send*: no *from* set\n"));
765 goto jleave;
768 if (fmt("Disposition-Notification-To:", nalloc(UNCONST(from), 0), fo,
769 GFILES, TRU1, 0))
770 rv = FAL0;
771 jleave:
772 NYD_LEAVE;
773 return rv;
776 static int
777 savemail(char const *name, FILE *fi)
779 FILE *fo;
780 char *buf;
781 size_t bufsize, buflen, cnt;
782 int prependnl = 0, rv = -1;
783 NYD_ENTER;
785 buf = smalloc(bufsize = LINESIZE);
787 if ((fo = Zopen(name, "a+", NULL)) == NULL) {
788 if ((fo = Zopen(name, "wx", NULL)) == NULL) {
789 perror(name);
790 goto jleave;
792 } else {
793 if (fseek(fo, -2L, SEEK_END) == 0) {
794 switch (fread(buf, sizeof *buf, 2, fo)) {
795 case 2:
796 if (buf[1] != '\n') {
797 prependnl = 1;
798 break;
800 /* FALLTHRU */
801 case 1:
802 if (buf[0] != '\n')
803 prependnl = 1;
804 break;
805 default:
806 if (ferror(fo)) {
807 perror(name);
808 goto jleave;
811 if (prependnl) {
812 putc('\n', fo);
814 fflush(fo);
818 fprintf(fo, "From %s %s", myname, time_current.tc_ctime);
819 fflush_rewind(fi);
820 cnt = fsize(fi);
821 buflen = 0;
822 while (fgetline(&buf, &bufsize, &cnt, &buflen, fi, 0) != NULL) {
823 #ifdef HAVE_DEBUG /* TODO assert legacy */
824 assert(!is_head(buf, buflen));
825 #else
826 if (is_head(buf, buflen))
827 putc('>', fo);
828 #endif
829 fwrite(buf, sizeof *buf, buflen, fo);
831 if (buflen && *(buf + buflen - 1) != '\n')
832 putc('\n', fo);
833 putc('\n', fo);
834 fflush(fo);
836 rv = 0;
837 if (ferror(fo)) {
838 perror(name);
839 rv = -1;
841 if (Fclose(fo) != 0)
842 rv = -1;
843 fflush_rewind(fi);
844 jleave:
845 free(buf);
846 NYD_LEAVE;
847 return rv;
850 static int
851 sendmail_internal(void *v, int recipient_record)
853 struct header head;
854 char *str = v;
855 int rv;
856 NYD_ENTER;
858 memset(&head, 0, sizeof head);
859 head.h_to = lextract(str, GTO | GFULL);
860 rv = mail1(&head, 0, NULL, NULL, recipient_record, 0);
861 NYD_LEAVE;
862 return rv;
865 static bool_t
866 _transfer(struct sendbundle *sbp)
868 struct name *np;
869 ui32_t cnt;
870 bool_t rv = TRU1;
871 NYD_ENTER;
873 for (cnt = 0, np = sbp->sb_to; np != NULL;) {
874 char const k[] = "smime-encrypt-";
875 size_t nl = strlen(np->n_name);
876 char *cp, *vs = ac_alloc(sizeof(k)-1 + nl +1);
877 memcpy(vs, k, sizeof(k) -1);
878 memcpy(vs + sizeof(k) -1, np->n_name, nl +1);
880 if ((cp = vok_vlook(vs)) != NULL) {
881 #ifdef HAVE_SSL
882 FILE *ef;
884 if ((ef = smime_encrypt(sbp->sb_input, cp, np->n_name)) != NULL) {
885 FILE *fisave = sbp->sb_input;
886 struct name *nsave = sbp->sb_to;
888 sbp->sb_to = ndup(np, np->n_type & ~(GFULL | GSKIN));
889 sbp->sb_input = ef;
890 if (!start_mta(sbp))
891 rv = FAL0;
892 sbp->sb_to = nsave;
893 sbp->sb_input = fisave;
895 Fclose(ef);
896 } else {
897 #else
898 fprintf(stderr, _("No SSL support compiled in.\n"));
899 rv = FAL0;
900 #endif
901 fprintf(stderr, _("Message not sent to <%s>\n"), np->n_name);
902 _sendout_error = TRU1;
903 #ifdef HAVE_SSL
905 #endif
906 rewind(sbp->sb_input);
908 if (np->n_flink != NULL)
909 np->n_flink->n_blink = np->n_blink;
910 if (np->n_blink != NULL)
911 np->n_blink->n_flink = np->n_flink;
912 if (np == sbp->sb_to)
913 sbp->sb_to = np->n_flink;
914 np = np->n_flink;
915 } else {
916 ++cnt;
917 np = np->n_flink;
919 ac_free(vs);
922 if (cnt > 0 && (ok_blook(smime_force_encryption) || !start_mta(sbp)))
923 rv = FAL0;
924 NYD_LEAVE;
925 return rv;
928 static bool_t
929 start_mta(struct sendbundle *sbp)
931 char const **args = NULL, **t, *mta;
932 char *smtp;
933 pid_t pid;
934 sigset_t nset;
935 bool_t rv = FAL0;
936 NYD_ENTER;
938 if ((smtp = ok_vlook(smtp)) == NULL) {
939 if ((mta = ok_vlook(sendmail)) != NULL) {
940 if ((mta = file_expand(mta)) == NULL)
941 goto jstop;
942 } else
943 mta = SENDMAIL;
945 args = _prepare_mta_args(sbp->sb_to, sbp->sb_hp);
946 if (options & OPT_DEBUG) {
947 printf(_("Sendmail arguments:"));
948 for (t = args; *t != NULL; ++t)
949 printf(" \"%s\"", *t);
950 printf("\n");
951 rv = TRU1;
952 goto jleave;
954 } else {
955 mta = NULL; /* Silence cc */
956 #ifndef HAVE_SMTP
957 fputs(_("No SMTP support compiled in.\n"), stderr);
958 goto jstop;
959 #else
960 /* XXX assert that sendbundle is setup? */
961 #endif
964 /* Fork, set up the temporary mail file as standard input for "mail", and
965 * exec with the user list we generated far above */
966 if ((pid = fork()) == -1) {
967 perror("fork");
968 jstop:
969 savedeadletter(sbp->sb_input, 0);
970 _sendout_error = TRU1;
971 goto jleave;
973 if (pid == 0) {
974 sigemptyset(&nset);
975 sigaddset(&nset, SIGHUP);
976 sigaddset(&nset, SIGINT);
977 sigaddset(&nset, SIGQUIT);
978 sigaddset(&nset, SIGTSTP);
979 sigaddset(&nset, SIGTTIN);
980 sigaddset(&nset, SIGTTOU);
981 freopen("/dev/null", "r", stdin);
982 #ifdef HAVE_SMTP
983 if (smtp != NULL) {
984 prepare_child(&nset, 0, 1);
985 if (smtp_mta(sbp))
986 _exit(0);
987 } else {
988 #endif
989 prepare_child(&nset, fileno(sbp->sb_input), -1);
990 /* If *record* is set then savemail() will move the file position;
991 * it'll call rewind(), but that may optimize away the systemcall if
992 * possible, and since dup2() shares the position with the original FD
993 * the MTA may end up reading nothing */
994 lseek(0, 0, SEEK_SET);
995 execv(mta, UNCONST(args));
996 perror(mta);
997 #ifdef HAVE_SMTP
999 #endif
1000 savedeadletter(sbp->sb_input, 1);
1001 fputs(_("... message not sent.\n"), stderr);
1002 _exit(1);
1004 if ((options & (OPT_DEBUG | OPT_VERB | OPT_BATCH_FLAG)) ||
1005 ok_blook(sendwait)) {
1006 if (wait_child(pid, NULL))
1007 rv = TRU1;
1008 else
1009 _sendout_error = TRU1;
1010 } else {
1011 rv = TRU1;
1012 free_child(pid);
1014 jleave:
1015 NYD_LEAVE;
1016 return rv;
1019 static bool_t
1020 mightrecord(FILE *fp, struct name *to)
1022 char *cp, *cq;
1023 char const *ep;
1024 bool_t rv = TRU1;
1025 NYD_ENTER;
1027 if (to != NULL) {
1028 cp = savestr(skinned_name(to));
1029 for (cq = cp; *cq != '\0' && *cq != '@'; ++cq)
1031 *cq = '\0';
1032 } else
1033 cp = ok_vlook(record);
1035 if (cp != NULL) {
1036 if ((ep = expand(cp)) == NULL) {
1037 ep = "NULL";
1038 goto jbail;
1041 if (*ep != '/' && *ep != '+' && ok_blook(outfolder) &&
1042 which_protocol(ep) == PROTO_FILE) {
1043 size_t i = strlen(cp);
1044 cq = salloc(i + 1 +1);
1045 cq[0] = '+';
1046 memcpy(cq + 1, cp, i +1);
1047 cp = cq;
1048 if ((ep = file_expand(cp)) == NULL) {
1049 ep = "NULL";
1050 goto jbail;
1054 if (savemail(ep, fp) != 0) {
1055 jbail:
1056 fprintf(stderr, _("Failed to save message in %s - message not sent\n"),
1057 ep);
1058 exit_status |= EXIT_ERR;
1059 savedeadletter(fp, 1);
1060 rv = FAL0;
1063 NYD_LEAVE;
1064 return rv;
1067 static void
1068 _message_id(FILE *fo, struct header *hp)
1070 char const *h;
1071 size_t rl;
1072 struct tm *tmp;
1073 NYD_ENTER;
1075 if (ok_blook(message_id_disable))
1076 goto jleave;
1078 if ((h = __sendout_ident) != NULL)
1079 rl = 8;
1080 else if ((h = ok_vlook(hostname)) != NULL)
1081 rl = 16;
1082 else if ((h = skin(myorigin(hp))) != NULL && strchr(h, '@') != NULL)
1083 rl = 8;
1084 else
1085 /* Up to MTA */
1086 goto jleave;
1088 tmp = &time_current.tc_gm;
1089 fprintf(fo, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
1090 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
1091 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
1092 getrandstring(rl), (rl == 8 ? '%' : '@'), h);
1093 jleave:
1094 __sendout_ident = NULL;
1095 NYD_LEAVE;
1098 static int
1099 fmt(char const *str, struct name *np, FILE *fo, int flags, int dropinvalid,
1100 int domime)
1102 enum {
1103 m_INIT = 1<<0,
1104 m_COMMA = 1<<1,
1105 m_NOPF = 1<<2,
1106 m_CSEEN = 1<<3
1107 } m = (flags & GCOMMA) ? m_COMMA : 0;
1108 ssize_t col, len;
1109 int rv = 1;
1110 NYD_ENTER;
1112 col = strlen(str);
1113 if (col) {
1114 fwrite(str, sizeof *str, col, fo);
1115 if (flags & GFILES)
1116 goto jstep;
1117 if (col == 9 && !asccasecmp(str, "reply-to:")) {
1118 m |= m_NOPF;
1119 goto jstep;
1121 if (ok_blook(add_file_recipients))
1122 goto jstep;
1123 if ((col == 3 && (!asccasecmp(str, "to:") || !asccasecmp(str, "cc:"))) ||
1124 (col == 4 && !asccasecmp(str, "bcc:")) ||
1125 (col == 10 && !asccasecmp(str, "Resent-To:")))
1126 m |= m_NOPF;
1128 jstep:
1129 for (; np != NULL; np = np->n_flink) {
1130 if ((m & m_NOPF) && is_fileorpipe_addr(np))
1131 continue;
1132 if (is_addr_invalid(np, !dropinvalid)) {
1133 if (dropinvalid)
1134 continue;
1135 else
1136 goto jleave;
1138 if ((m & (m_INIT | m_COMMA)) == (m_INIT | m_COMMA)) {
1139 putc(',', fo);
1140 m |= m_CSEEN;
1141 ++col;
1143 len = strlen(np->n_fullname);
1144 ++col; /* The separating space */
1145 if ((m & m_INIT) && /*col > 1 &&*/ UICMP(z, col + len, >, 72)) {
1146 fputs("\n ", fo);
1147 col = 1;
1148 m &= ~m_CSEEN;
1149 } else
1150 putc(' ', fo);
1151 m = (m & ~m_CSEEN) | m_INIT;
1152 len = xmime_write(np->n_fullname, len, fo,
1153 (domime ? CONV_TOHDR_A : CONV_NONE), TD_ICONV, NULL);
1154 if (len < 0)
1155 goto jleave;
1156 col += len;
1158 putc('\n', fo);
1159 rv = 0;
1160 jleave:
1161 NYD_LEAVE;
1162 return rv;
1165 static int
1166 infix_resend(FILE *fi, FILE *fo, struct message *mp, struct name *to,
1167 int add_resent)
1169 size_t cnt, c, bufsize = 0;
1170 char *buf = NULL;
1171 char const *cp;
1172 struct name *fromfield = NULL, *senderfield = NULL, *mdn;
1173 int rv = 1;
1174 NYD_ENTER;
1176 cnt = mp->m_size;
1178 /* Write the Resent-Fields */
1179 if (add_resent) {
1180 fputs("Resent-", fo);
1181 mkdate(fo, "Date");
1182 if ((cp = myaddrs(NULL)) != NULL) {
1183 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-From:", fo,
1184 &fromfield))
1185 goto jleave;
1187 if ((cp = ok_vlook(sender)) != NULL) {
1188 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-Sender:", fo,
1189 &senderfield))
1190 goto jleave;
1192 if (fmt("Resent-To:", to, fo, 1, 1, 0))
1193 goto jleave;
1194 if ((cp = ok_vlook(stealthmua)) == NULL || !strcmp(cp, "noagent")) {
1195 fputs("Resent-", fo);
1196 _message_id(fo, NULL);
1200 if ((mdn = UNCONST(check_from_and_sender(fromfield, senderfield))) == NULL)
1201 goto jleave;
1202 if (!_check_dispo_notif(mdn, NULL, fo))
1203 goto jleave;
1205 /* Write the original headers */
1206 while (cnt > 0) {
1207 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1208 break;
1209 /* XXX more checks: The From_ line may be seen when resending */
1210 /* During headers is_head() is actually overkill, so ^From_ is sufficient
1211 * && !is_head(buf, c) */
1212 if (ascncasecmp("status:", buf, 7) && strncmp("From ", buf, 5) &&
1213 ascncasecmp("disposition-notification-to:", buf, 28))
1214 fwrite(buf, sizeof *buf, c, fo);
1215 if (cnt > 0 && *buf == '\n')
1216 break;
1219 /* Write the message body */
1220 while (cnt > 0) {
1221 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1222 break;
1223 if (cnt == 0 && *buf == '\n')
1224 break;
1225 fwrite(buf, sizeof *buf, c, fo);
1227 if (buf != NULL)
1228 free(buf);
1229 if (ferror(fo)) {
1230 perror(_("temporary mail file"));
1231 goto jleave;
1233 rv = 0;
1234 jleave:
1235 NYD_LEAVE;
1236 return rv;
1239 FL int
1240 mail(struct name *to, struct name *cc, struct name *bcc, char *subject,
1241 struct attachment *attach, char *quotefile, int recipient_record)
1243 struct header head;
1244 struct str in, out;
1245 NYD_ENTER;
1247 memset(&head, 0, sizeof head);
1249 /* The given subject may be in RFC1522 format. */
1250 if (subject != NULL) {
1251 in.s = subject;
1252 in.l = strlen(subject);
1253 mime_fromhdr(&in, &out, /* TODO ??? TD_ISPR |*/ TD_ICONV);
1254 head.h_subject = out.s;
1256 if (!(options & OPT_t_FLAG)) {
1257 head.h_to = to;
1258 head.h_cc = cc;
1259 head.h_bcc = bcc;
1261 head.h_attach = attach;
1263 mail1(&head, 0, NULL, quotefile, recipient_record, 0);
1265 if (subject != NULL)
1266 free(out.s);
1267 NYD_LEAVE;
1268 return 0;
1271 FL int
1272 c_sendmail(void *v)
1274 int rv;
1275 NYD_ENTER;
1277 rv = sendmail_internal(v, 0);
1278 NYD_LEAVE;
1279 return rv;
1282 FL int
1283 c_Sendmail(void *v)
1285 int rv;
1286 NYD_ENTER;
1288 rv = sendmail_internal(v, 1);
1289 NYD_LEAVE;
1290 return rv;
1293 FL enum okay
1294 mail1(struct header *hp, int printheaders, struct message *quote,
1295 char *quotefile, int recipient_record, int doprefix)
1297 struct sendbundle sb;
1298 struct name *to;
1299 FILE *mtf, *nmtf;
1300 int dosign = -1, err;
1301 char const *cp;
1302 enum okay rv = STOP;
1303 NYD_ENTER;
1305 _sendout_error = FAL0;
1307 /* Update some globals we likely need first */
1308 time_current_update(&time_current, TRU1);
1310 /* */
1311 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1312 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL)));
1313 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1314 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL)));
1316 /* Collect user's mail from standard input. Get the result as mtf */
1317 mtf = collect(hp, printheaders, quote, quotefile, doprefix);
1318 if (mtf == NULL)
1319 goto j_leave;
1321 if (options & OPT_INTERACTIVE) {
1322 err = (ok_blook(bsdcompat) || ok_blook(askatend));
1323 if (err == 0)
1324 goto jaskeot;
1325 if (ok_blook(askcc))
1326 ++err, grab_headers(hp, GCC, 1);
1327 if (ok_blook(askbcc))
1328 ++err, grab_headers(hp, GBCC, 1);
1329 if (ok_blook(askattach))
1330 ++err, edit_attachments(&hp->h_attach);
1331 if (ok_blook(asksign))
1332 ++err, dosign = getapproval(_("Sign this message (y/n)? "), TRU1);
1333 if (err == 1) {
1334 jaskeot:
1335 printf(_("EOT\n"));
1336 fflush(stdout);
1340 if (fsize(mtf) == 0) {
1341 if (options & OPT_E_FLAG)
1342 goto jleave;
1343 if (hp->h_subject == NULL)
1344 printf(_("No message, no subject; hope that's ok\n"));
1345 else if (ok_blook(bsdcompat) || ok_blook(bsdmsgs))
1346 printf(_("Null message body; hope that's ok\n"));
1349 if (dosign < 0)
1350 dosign = ok_blook(smime_sign);
1351 #ifndef HAVE_SSL
1352 if (dosign) {
1353 fprintf(stderr, _("No SSL support compiled in.\n"));
1354 goto jleave;
1356 #endif
1358 /* XXX Update time_current again; once collect() offers editing of more
1359 * XXX headers, including Date:, this must only happen if Date: is the
1360 * XXX same that it was before collect() (e.g., postponing etc.).
1361 * XXX But *do* update otherwise because the mail seems to be backdated
1362 * XXX if the user edited some time, which looks odd and it happened
1363 * XXX to me that i got mis-dated response mails due to that... */
1364 time_current_update(&time_current, TRU1);
1366 /* TODO hrmpf; the MIME/send layer rewrite MUST address the init crap:
1367 * TODO setup the header ONCE; note this affects edit.c, collect.c ...,
1368 * TODO but: offer a hook that rebuilds/expands/checks/fixates all
1369 * TODO header fields ONCE, call that ONCE after user editing etc. has
1370 * TODO completed (one edit cycle) */
1372 /* Take the user names from the combined to and cc lists and do all the
1373 * alias processing. The POSIX standard says:
1374 * The names shall be substituted when alias is used as a recipient
1375 * address specified by the user in an outgoing message (that is,
1376 * other recipients addressed indirectly through the reply command
1377 * shall not be substituted in this manner).
1378 * S-nail thus violates POSIX, as has been pointed out correctly by
1379 * Martin Neitzel, but logic and usability of POSIX standards is not seldom
1380 * disputable anyway. Go for user friendliness */
1382 /* Do alias expansion on Reply-To: members, too */
1383 /* TODO puthead() YET (!!! see ONCE note above) expands the value, but
1384 * TODO doesn't perform alias expansion; encapsulate in the ONCE-o */
1385 if (hp->h_replyto == NULL && (cp = ok_vlook(replyto)) != NULL)
1386 hp->h_replyto = checkaddrs(lextract(cp, GEXTRA | GFULL));
1387 if (hp->h_replyto != NULL)
1388 hp->h_replyto = elide(usermap(hp->h_replyto, TRU1));
1390 /* TODO what happens now is that all recipients are merged into
1391 * TODO a duplicated list with expanded aliases, then this list is
1392 * TODO splitted again into the three individual recipient lists (with
1393 * TODO duplicates removed).
1394 * TODO later on we use the merged list for outof() pipe/file saving,
1395 * TODO then we eliminate duplicates (again) and then we use that one
1396 * TODO for mightrecord() and _transfer(), and again. ... Please ... */
1398 /* NOTE: Due to elide() in fixhead(), ENSURE to,cc,bcc order of to!,
1399 * because otherwise the recipients will be "degraded" if they occur
1400 * multiple times */
1401 to = usermap(cat(hp->h_to, cat(hp->h_cc, hp->h_bcc)), FAL0);
1402 if (to == NULL) {
1403 fprintf(stderr, _("No recipients specified\n"));
1404 _sendout_error = TRU1;
1406 to = fixhead(hp, to);
1408 /* */
1409 memset(&sb, 0, sizeof sb);
1410 sb.sb_hp = hp;
1411 sb.sb_to = to;
1412 sb.sb_input = mtf;
1413 if ((dosign || count_nonlocal(to) > 0) &&
1414 !_sendbundle_setup_creds(&sb, (dosign > 0)))
1415 /* TODO saving $DEAD and recovering etc is not yet well defined */
1416 goto jfail_dead;
1418 /* 'Bit ugly kind of control flow until we find a charset that does it */
1419 for (charset_iter_reset(hp->h_charset);; charset_iter_next()) {
1420 if (!charset_iter_is_valid())
1422 else if ((nmtf = infix(hp, mtf)) != NULL)
1423 break;
1424 else if ((err = errno) == EILSEQ || err == EINVAL) {
1425 rewind(mtf);
1426 continue;
1429 perror("");
1430 jfail_dead:
1431 _sendout_error = TRU1;
1432 savedeadletter(mtf, TRU1);
1433 fputs(_("... message not sent.\n"), stderr);
1434 goto jleave;
1436 mtf = nmtf;
1438 /* */
1439 #ifdef HAVE_SSL
1440 if (dosign) {
1441 if ((nmtf = smime_sign(mtf, sb.sb_signer.s)) == NULL)
1442 goto jfail_dead;
1443 Fclose(mtf);
1444 mtf = nmtf;
1446 #endif
1448 /* TODO truly - i still don't get what follows: (1) we deliver file
1449 * TODO and pipe addressees, (2) we mightrecord() and (3) we transfer
1450 * TODO even if (1) savedeadletter() etc. To me this doesn't make sense? */
1452 /* Deliver pipe and file addressees */
1453 to = outof(to, mtf, &_sendout_error);
1454 if (_sendout_error)
1455 savedeadletter(mtf, FAL0);
1457 to = elide(to); /* XXX needed only to drop GDELs due to outof()! */
1458 { ui32_t cnt = count(to);
1459 if ((!recipient_record || cnt > 0) &&
1460 !mightrecord(mtf, (recipient_record ? to : NULL)))
1461 goto jleave;
1462 if (cnt > 0) {
1463 sb.sb_hp = hp;
1464 sb.sb_to = to;
1465 sb.sb_input = mtf;
1466 if (_transfer(&sb))
1467 rv = OKAY;
1468 } else if (!_sendout_error)
1469 rv = OKAY;
1471 jleave:
1472 Fclose(mtf);
1473 j_leave:
1474 if (_sendout_error)
1475 exit_status |= EXIT_SEND_ERROR;
1476 NYD_LEAVE;
1477 return rv;
1480 FL int
1481 mkdate(FILE *fo, char const *field)
1483 struct tm *tmptr;
1484 int tzdiff, tzdiff_hour, tzdiff_min, rv;
1485 NYD_ENTER;
1487 tzdiff = time_current.tc_time - mktime(&time_current.tc_gm);
1488 tzdiff_hour = (int)(tzdiff / 60);
1489 tzdiff_min = tzdiff_hour % 60;
1490 tzdiff_hour /= 60;
1491 tmptr = &time_current.tc_local;
1492 if (tmptr->tm_isdst > 0)
1493 ++tzdiff_hour;
1494 rv = fprintf(fo, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1495 field,
1496 weekday_names[tmptr->tm_wday],
1497 tmptr->tm_mday, month_names[tmptr->tm_mon],
1498 tmptr->tm_year + 1900, tmptr->tm_hour,
1499 tmptr->tm_min, tmptr->tm_sec,
1500 tzdiff_hour * 100 + tzdiff_min);
1501 NYD_LEAVE;
1502 return rv;
1505 FL int
1506 puthead(struct header *hp, FILE *fo, enum gfield w, enum sendaction action,
1507 enum conversion convert, char const *contenttype, char const *charset)
1509 #define FMT_CC_AND_BCC() \
1510 do {\
1511 if (hp->h_cc != NULL && (w & GCC)) {\
1512 if (fmt("Cc:", hp->h_cc, fo, (w & (GCOMMA | GFILES)), 0,\
1513 (action != SEND_TODISP)))\
1514 goto jleave;\
1515 ++gotcha;\
1517 if (hp->h_bcc != NULL && (w & GBCC)) {\
1518 if (fmt("Bcc:", hp->h_bcc, fo, (w & (GCOMMA | GFILES)), 0,\
1519 (action != SEND_TODISP)))\
1520 goto jleave;\
1521 ++gotcha;\
1523 } while (0)
1525 char const *addr;
1526 size_t gotcha, l;
1527 struct name *np, *fromfield = NULL, *senderfield = NULL;
1528 int stealthmua, rv = 1;
1529 bool_t nodisp;
1530 NYD_ENTER;
1532 if ((addr = ok_vlook(stealthmua)) != NULL)
1533 stealthmua = !strcmp(addr, "noagent") ? -1 : 1;
1534 else
1535 stealthmua = 0;
1536 gotcha = 0;
1537 nodisp = (action != SEND_TODISP);
1539 if (w & GDATE)
1540 mkdate(fo, "Date"), ++gotcha;
1541 if (w & GIDENT) {
1542 if (hp->h_from != NULL) {
1543 if (fmt("From:", hp->h_from, fo, (w & (GCOMMA | GFILES)), 0, nodisp))
1544 goto jleave;
1545 ++gotcha;
1546 fromfield = hp->h_from;
1547 } else if ((addr = myaddrs(hp)) != NULL) {
1548 if (_putname(addr, w, action, &gotcha, "From:", fo, &fromfield))
1549 goto jleave;
1550 hp->h_from = fromfield;
1553 if (((addr = hp->h_organization) != NULL ||
1554 (addr = ok_vlook(ORGANIZATION)) != NULL) &&
1555 (l = strlen(addr)) > 0) {
1556 fwrite("Organization: ", sizeof(char), 14, fo);
1557 if (xmime_write(addr, l, fo, (!nodisp ? CONV_NONE : CONV_TOHDR),
1558 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1559 goto jleave;
1560 ++gotcha;
1561 putc('\n', fo);
1564 /* TODO see the ONCE TODO note somewhere around this file;
1565 * TODO but anyway, do NOT perform alias expansion UNLESS
1566 * TODO we are actually sending out! */
1567 if (hp->h_replyto != NULL) {
1568 if (fmt("Reply-To:", hp->h_replyto, fo, w & GCOMMA, 0, nodisp))
1569 goto jleave;
1570 ++gotcha;
1571 } else if ((addr = ok_vlook(replyto)) != NULL)
1572 if (_putname(addr, w, action, &gotcha, "Reply-To:", fo, NULL))
1573 goto jleave;
1575 if (hp->h_sender != NULL) {
1576 if (fmt("Sender:", hp->h_sender, fo, w & GCOMMA, 0, nodisp))
1577 goto jleave;
1578 ++gotcha;
1579 senderfield = hp->h_sender;
1580 } else if ((addr = ok_vlook(sender)) != NULL)
1581 if (_putname(addr, w, action, &gotcha, "Sender:", fo, &senderfield))
1582 goto jleave;
1584 if ((np = UNCONST(check_from_and_sender(fromfield, senderfield))) == NULL)
1585 goto jleave;
1586 if (!_check_dispo_notif(np, hp, fo))
1587 goto jleave;
1590 if (hp->h_to != NULL && w & GTO) {
1591 if (fmt("To:", hp->h_to, fo, (w & (GCOMMA | GFILES)), 0, nodisp))
1592 goto jleave;
1593 ++gotcha;
1596 if (!ok_blook(bsdcompat) && !ok_blook(bsdorder))
1597 FMT_CC_AND_BCC();
1599 if (hp->h_subject != NULL && (w & GSUBJECT)) {
1600 fwrite("Subject: ", sizeof (char), 9, fo);
1601 if (!ascncasecmp(hp->h_subject, "re: ", 4)) {/* TODO localizable */
1602 fwrite("Re: ", sizeof(char), 4, fo);
1603 if (strlen(hp->h_subject + 4) > 0 &&
1604 xmime_write(hp->h_subject + 4, strlen(hp->h_subject + 4), fo,
1605 (!nodisp ? CONV_NONE : CONV_TOHDR),
1606 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1607 goto jleave;
1608 } else if (*hp->h_subject != '\0') {
1609 if (xmime_write(hp->h_subject, strlen(hp->h_subject), fo,
1610 (!nodisp ? CONV_NONE : CONV_TOHDR),
1611 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1612 goto jleave;
1614 ++gotcha;
1615 fwrite("\n", sizeof (char), 1, fo);
1618 if (ok_blook(bsdcompat) || ok_blook(bsdorder))
1619 FMT_CC_AND_BCC();
1621 if ((w & GMSGID) && stealthmua <= 0)
1622 _message_id(fo, hp), ++gotcha;
1624 if ((np = hp->h_ref) != NULL && (w & GREF)) {
1625 fmt("References:", np, fo, 0, 1, 0);
1626 if (np->n_name != NULL) {
1627 while (np->n_flink != NULL)
1628 np = np->n_flink;
1629 if (!is_addr_invalid(np, 0)) {
1630 fprintf(fo, "In-Reply-To: %s\n", np->n_name);
1631 ++gotcha;
1636 if ((w & GUA) && stealthmua == 0)
1637 fprintf(fo, "User-Agent: %s %s\n", uagent, version), ++gotcha;
1639 if (w & GMIME) {
1640 fputs("MIME-Version: 1.0\n", fo), ++gotcha;
1641 if (hp->h_attach != NULL) {
1642 _sendout_boundary = mime_create_boundary();/*TODO carrier*/
1643 fprintf(fo, "Content-Type: multipart/mixed;\n boundary=\"%s\"\n",
1644 _sendout_boundary);
1645 } else {
1646 fprintf(fo, "Content-Type: %s", contenttype);
1647 if (charset != NULL)
1648 fprintf(fo, "; charset=%s", charset);
1649 fprintf(fo, "\nContent-Transfer-Encoding: %s\n",
1650 _get_encoding(convert));
1654 if (gotcha && (w & GNL))
1655 putc('\n', fo);
1656 rv = 0;
1657 jleave:
1658 NYD_LEAVE;
1659 return rv;
1660 #undef FMT_CC_AND_BCC
1663 FL enum okay
1664 resend_msg(struct message *mp, struct name *to, int add_resent) /* TODO check */
1666 struct sendbundle sb;
1667 FILE *ibuf, *nfo, *nfi;
1668 char *tempMail;
1669 enum okay rv = STOP;
1670 NYD_ENTER;
1672 _sendout_error = FAL0;
1674 /* Update some globals we likely need first */
1675 time_current_update(&time_current, TRU1);
1677 if ((to = checkaddrs(to)) == NULL) {
1678 _sendout_error = TRU1;
1679 goto jleave;
1682 if ((nfo = Ftmp(&tempMail, "resend", OF_WRONLY | OF_HOLDSIGS | OF_REGISTER,
1683 0600)) == NULL) {
1684 _sendout_error = TRU1;
1685 perror(_("temporary mail file"));
1686 goto jleave;
1688 if ((nfi = Fopen(tempMail, "r")) == NULL) {
1689 _sendout_error = TRU1;
1690 perror(tempMail);
1692 Ftmp_release(&tempMail);
1693 if (nfi == NULL)
1694 goto jerr_o;
1696 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1697 goto jerr_all;
1699 memset(&sb, 0, sizeof sb);
1700 sb.sb_to = to;
1701 sb.sb_input = nfi;
1702 if (count_nonlocal(to) > 0 && !_sendbundle_setup_creds(&sb, FAL0))
1703 /* TODO saving $DEAD and recovering etc is not yet well defined */
1704 goto jerr_all;
1706 if (infix_resend(ibuf, nfo, mp, to, add_resent) != 0) {
1707 savedeadletter(nfi, TRU1);
1708 fputs(_("... message not sent.\n"), stderr);
1709 jerr_all:
1710 Fclose(nfi);
1711 jerr_o:
1712 Fclose(nfo);
1713 _sendout_error = TRU1;
1714 goto jleave;
1716 Fclose(nfo);
1717 rewind(nfi);
1719 to = outof(to, nfi, &_sendout_error);
1720 if (_sendout_error)
1721 savedeadletter(nfi, FAL0);
1723 to = elide(to); /* TODO should have been done in fixhead()? */
1724 if (count(to) != 0) {
1725 if (!ok_blook(record_resent) || mightrecord(nfi, to)) {
1726 sb.sb_to = to;
1727 /*sb.sb_input = nfi;*/
1728 if (_transfer(&sb))
1729 rv = OKAY;
1731 } else if (!_sendout_error)
1732 rv = OKAY;
1734 Fclose(nfi);
1735 jleave:
1736 if (_sendout_error)
1737 exit_status |= EXIT_SEND_ERROR;
1738 NYD_LEAVE;
1739 return rv;
1742 #undef SEND_LINESIZE
1744 /* s-it-mode */