THANKS: Ken Hornstein
[s-mailx.git] / sendout.c
blob5594ba3b45e3cec125a8412dad7a7001f18391e7
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Message sending lifecycle, header composing, etc.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE sendout
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #undef SEND_LINESIZE
43 #define SEND_LINESIZE \
44 ((1024 / B64_ENCODE_INPUT_PER_LINE) * B64_ENCODE_INPUT_PER_LINE)
46 enum fmt_flags {
47 FMT_DOMIME = 1<<0,
48 FMT_COMMA = GCOMMA,
49 FMT_FILES = GFILES,
50 _FMT_GMASK = FMT_COMMA | FMT_FILES
52 n_CTA(!(_FMT_GMASK & FMT_DOMIME),
53 "Code-required condition not satisfied but actual bit carrier value");
55 static char const *__sendout_ident; /* TODO temporary hack; rewrite puthead() */
56 static char * _sendout_boundary;
57 static si8_t _sendout_error;
59 /* */
60 static enum okay _putname(char const *line, enum gfield w,
61 enum sendaction action, size_t *gotcha,
62 char const *prefix, FILE *fo, struct name **xp,
63 enum gfield addflags);
65 /* Place Content-Type:, Content-Transfer-Encoding:, Content-Disposition:
66 * headers, respectively */
67 static int _put_ct(FILE *fo, char const *contenttype,
68 char const *charset);
69 SINLINE int _put_cte(FILE *fo, enum conversion conv);
70 static int _put_cd(FILE *fo, char const *cd, char const *filename);
72 /* Put all entries of the given header list */
73 static bool_t _sendout_header_list(FILE *fo, struct n_header_field *hfp,
74 bool_t nodisp);
76 /* Write an attachment to the file buffer, converting to MIME */
77 static int a_sendout_attach_file(struct header *hp, struct attachment *ap,
78 FILE *fo);
79 static int a_sendout__attach_file(struct header *hp, struct attachment *ap,
80 FILE *fo);
82 /* There are non-local receivers, collect credentials etc. */
83 static bool_t _sendbundle_setup_creds(struct sendbundle *sbpm,
84 bool_t signing_caps);
86 /* Attach a message to the file buffer */
87 static int a_sendout_attach_msg(struct header *hp, struct attachment *ap,
88 FILE *fo);
90 /* Generate the body of a MIME multipart message */
91 static int make_multipart(struct header *hp, int convert, FILE *fi,
92 FILE *fo, char const *contenttype, char const *charset);
94 /* Prepend a header in front of the collected stuff and return the new file */
95 static FILE * infix(struct header *hp, FILE *fi);
97 /* Check whether Disposition-Notification-To: is desired */
98 static bool_t _check_dispo_notif(struct name *mdn, struct header *hp,
99 FILE *fo);
101 /* Send mail to a bunch of user names. The interface is through mail() */
102 static int sendmail_internal(void *v, int recipient_record);
104 /* Deal with file and pipe addressees */
105 static struct name * _outof(struct name *names, FILE *fo, bool_t *senderror);
107 /* Record outgoing mail if instructed to do so; in *record* unless to is set */
108 static bool_t mightrecord(FILE *fp, struct name *to, bool_t resend);
110 static bool_t a_sendout__savemail(char const *name, FILE *fp, bool_t resend);
112 /* */
113 static bool_t _transfer(struct sendbundle *sbp);
115 static bool_t __mta_start(struct sendbundle *sbp);
116 static char const ** __mta_prepare_args(struct name *to, struct header *hp);
117 static void __mta_debug(struct sendbundle *sbp, char const *mta,
118 char const **args);
120 /* Create a Message-ID: header field. Use either host name or from address */
121 static char const *a_sendout_random_id(struct header *hp, bool_t msgid);
123 /* Format the given header line to not exceed 72 characters */
124 static int fmt(char const *str, struct name *np, FILE *fo,
125 enum fmt_flags ff);
127 /* Rewrite a message for resending, adding the Resent-Headers */
128 static int infix_resend(FILE *fi, FILE *fo, struct message *mp,
129 struct name *to, int add_resent);
131 static enum okay
132 _putname(char const *line, enum gfield w, enum sendaction action,
133 size_t *gotcha, char const *prefix, FILE *fo, struct name **xp,
134 enum gfield addflags)
136 struct name *np;
137 enum okay rv = STOP;
138 NYD_ENTER;
140 np = lextract(line, GEXTRA | GFULL | addflags);
141 if (xp != NULL)
142 *xp = np;
143 if (np == NULL)
145 else if (fmt(prefix, np, fo, ((w & GCOMMA) |
146 ((action != SEND_TODISP) ? FMT_DOMIME : 0))))
147 rv = OKAY;
148 else if (gotcha != NULL)
149 ++(*gotcha);
150 NYD_LEAVE;
151 return rv;
154 static int
155 _put_ct(FILE *fo, char const *contenttype, char const *charset)
157 int rv, i;
158 NYD2_ENTER;
160 if ((rv = fprintf(fo, "Content-Type: %s", contenttype)) < 0)
161 goto jerr;
163 if (charset == NULL)
164 goto jend;
166 if (putc(';', fo) == EOF)
167 goto jerr;
168 ++rv;
170 if (strlen(contenttype) + sizeof("Content-Type: ;")-1 > 50) {
171 if (putc('\n', fo) == EOF)
172 goto jerr;
173 ++rv;
176 /* C99 */{
177 size_t l = strlen(charset);
178 char *lcs = salloc(l +1);
180 for(l = 0; *charset != '\0'; ++l, ++charset)
181 lcs[l] = lowerconv(*charset);
182 lcs[l] = '\0';
183 charset = lcs;
185 if ((i = fprintf(fo, " charset=%s", charset)) < 0)
186 goto jerr;
187 rv += i;
189 jend:
190 if (putc('\n', fo) == EOF)
191 goto jerr;
192 ++rv;
193 jleave:
194 NYD2_LEAVE;
195 return rv;
196 jerr:
197 rv = -1;
198 goto jleave;
201 SINLINE int
202 _put_cte(FILE *fo, enum conversion conv)
204 int rv;
205 NYD2_ENTER;
207 /* RFC 2045, 6.1.:
208 * This is the default value -- that is,
209 * "Content-Transfer-Encoding: 7BIT" is assumed if the
210 * Content-Transfer-Encoding header field is not present.
212 rv = (conv == CONV_7BIT) ? 0
213 : fprintf(fo, "Content-Transfer-Encoding: %s\n",
214 mime_enc_from_conversion(conv));
215 NYD2_LEAVE;
216 return rv;
219 static int
220 _put_cd(FILE *fo, char const *cd, char const *filename)
222 struct str f;
223 si8_t mpc;
224 int rv;
225 NYD2_ENTER;
227 f.s = NULL;
229 /* xxx Ugly with the trailing space in case of wrap! */
230 if ((rv = fprintf(fo, "Content-Disposition: %s; ", cd)) < 0)
231 goto jerr;
233 if (!(mpc = mime_param_create(&f, "filename", filename)))
234 goto jerr;
235 /* Always fold if result contains newlines */
236 if (mpc < 0 || f.l + rv > MIME_LINELEN) { /* FIXME MIME_LINELEN_MAX */
237 if (putc('\n', fo) == EOF || putc(' ', fo) == EOF)
238 goto jerr;
239 rv += 2;
241 if (fputs(f.s, fo) == EOF || putc('\n', fo) == EOF)
242 goto jerr;
243 rv += (int)++f.l;
245 jleave:
246 NYD2_LEAVE;
247 return rv;
248 jerr:
249 rv = -1;
250 goto jleave;
254 static bool_t
255 _sendout_header_list(FILE *fo, struct n_header_field *hfp, bool_t nodisp){
256 bool_t rv;
257 NYD2_ENTER;
259 for(rv = TRU1; hfp != NULL; hfp = hfp->hf_next)
260 if(fwrite(hfp->hf_dat, sizeof(char), hfp->hf_nl, fo) != hfp->hf_nl ||
261 putc(':', fo) == EOF || putc(' ', fo) == EOF ||
262 xmime_write(hfp->hf_dat + hfp->hf_nl +1, hfp->hf_bl, fo,
263 (!nodisp ? CONV_NONE : CONV_TOHDR),
264 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV)) < 0 ||
265 putc('\n', fo) == EOF){
266 rv = FAL0;
267 break;
269 NYD_LEAVE;
270 return rv;
273 static int
274 a_sendout_attach_file(struct header *hp, struct attachment *ap, FILE *fo)
276 /* TODO of course, the MIME classification needs to performed once
277 * TODO only, not for each and every charset anew ... ;-// */
278 char *charset_iter_orig[2];
279 long offs;
280 int err = 0;
281 NYD_ENTER;
283 /* Is this already in target charset? Simply copy over */
284 if (ap->a_conv == AC_TMPFILE) {
285 err = a_sendout__attach_file(hp, ap, fo);
286 Fclose(ap->a_tmpf);
287 DBG( ap->a_tmpf = NULL; )
288 goto jleave;
291 /* If we don't apply charset conversion at all (fixed input=ouput charset)
292 * we also simply copy over, since it's the users desire */
293 if (ap->a_conv == AC_FIX_INCS) {
294 ap->a_charset = ap->a_input_charset;
295 err = a_sendout__attach_file(hp, ap, fo);
296 goto jleave;
297 } else
298 assert(ap->a_input_charset != NULL);
300 /* Otherwise we need to iterate over all possible output charsets */
301 if ((offs = ftell(fo)) == -1) {
302 err = EIO;
303 goto jleave;
305 charset_iter_recurse(charset_iter_orig);
306 for (charset_iter_reset(NULL);; charset_iter_next()) {
307 if (!charset_iter_is_valid()) {
308 err = EILSEQ;
309 break;
311 err = a_sendout__attach_file(hp, ap, fo);
312 if (err == 0 || (err != EILSEQ && err != EINVAL))
313 break;
314 clearerr(fo);
315 if (fseek(fo, offs, SEEK_SET) == -1) {
316 err = EIO;
317 break;
319 if (ap->a_conv != AC_DEFAULT) {
320 err = EILSEQ;
321 break;
323 ap->a_charset = NULL;
325 charset_iter_restore(charset_iter_orig);
326 jleave:
327 NYD_LEAVE;
328 return err;
331 static int
332 a_sendout__attach_file(struct header *hp, struct attachment *ap, FILE *fo)
334 int err = 0, do_iconv;
335 FILE *fi;
336 char const *charset;
337 enum conversion convert;
338 char *buf;
339 size_t bufsize, lncnt, inlen;
340 NYD_ENTER;
342 /* Either charset-converted temporary file, or plain path */
343 if (ap->a_conv == AC_TMPFILE) {
344 fi = ap->a_tmpf;
345 assert(ftell(fi) == 0);
346 } else if ((fi = Fopen(ap->a_path, "r")) == NULL) {
347 err = errno;
348 n_err(_("%s: %s\n"), n_shexp_quote_cp(ap->a_path, FAL0), strerror(err));
349 goto jleave;
352 /* MIME part header for attachment */
353 { char const *ct, *cp;
355 ct = ap->a_content_type;
356 charset = ap->a_charset;
357 convert = mime_type_classify_file(fi, (char const**)&ct,
358 &charset, &do_iconv);
359 if (charset == NULL || ap->a_conv == AC_FIX_INCS ||
360 ap->a_conv == AC_TMPFILE)
361 do_iconv = 0;
363 if (fprintf(fo, "\n--%s\n", _sendout_boundary) < 0 ||
364 _put_ct(fo, ct, charset) < 0 || _put_cte(fo, convert) < 0 ||
365 _put_cd(fo, ap->a_content_disposition, ap->a_name) < 0)
366 goto jerr_header;
368 if((cp = ok_vlook(stealthmua)) == NULL || !strcmp(cp, "noagent")){
369 struct name *np;
371 if((np = ap->a_content_id) != NULL)
372 cp = np->n_name;
373 else
374 cp = a_sendout_random_id(hp, FAL0);
376 if(cp != NULL && fprintf(fo, "Content-ID: <%s>\n", cp) < 0)
377 goto jerr_header;
380 if ((cp = ap->a_content_description) != NULL &&
381 (fputs("Content-Description: ", fo) == EOF ||
382 xmime_write(cp, strlen(cp), fo, CONV_TOHDR, (TD_ISPR | TD_ICONV)
383 ) < 0 || putc('\n', fo) == EOF))
384 goto jerr_header;
386 if (putc('\n', fo) == EOF) {
387 jerr_header:
388 err = errno;
389 goto jerr_fclose;
393 #ifdef HAVE_ICONV
394 if (iconvd != (iconv_t)-1)
395 n_iconv_close(iconvd);
396 if (do_iconv) {
397 if (asccasecmp(charset, ap->a_input_charset) &&
398 (iconvd = n_iconv_open(charset, ap->a_input_charset)
399 ) == (iconv_t)-1 && (err = errno) != 0) {
400 if (err == EINVAL)
401 n_err(_("Cannot convert from %s to %s\n"), ap->a_input_charset,
402 charset);
403 else
404 n_err(_("iconv_open: %s\n"), strerror(err));
405 goto jerr_fclose;
408 #endif
410 bufsize = SEND_LINESIZE;
411 buf = smalloc(bufsize);
412 if (convert == CONV_TOQP
413 #ifdef HAVE_ICONV
414 || iconvd != (iconv_t)-1
415 #endif
417 lncnt = fsize(fi);
418 for (;;) {
419 if (convert == CONV_TOQP
420 #ifdef HAVE_ICONV
421 || iconvd != (iconv_t)-1
422 #endif
424 if (fgetline(&buf, &bufsize, &lncnt, &inlen, fi, 0) == NULL)
425 break;
426 } else if ((inlen = fread(buf, sizeof *buf, bufsize, fi)) == 0)
427 break;
428 if (xmime_write(buf, inlen, fo, convert, TD_ICONV) < 0) {
429 err = errno;
430 goto jerr;
433 if (ferror(fi))
434 err = EDOM;
435 jerr:
436 free(buf);
437 jerr_fclose:
438 if (ap->a_conv != AC_TMPFILE)
439 Fclose(fi);
440 jleave:
441 NYD_LEAVE;
442 return err;
445 static bool_t
446 _sendbundle_setup_creds(struct sendbundle *sbp, bool_t signing_caps)
448 bool_t v15, rv = FAL0;
449 char *shost, *from;
450 #ifdef HAVE_SMTP
451 char const *smtp;
452 #endif
453 NYD_ENTER;
455 v15 = ok_blook(v15_compat);
456 shost = (v15 ? ok_vlook(smtp_hostname) : NULL);
457 from = ((signing_caps || !v15 || shost == NULL)
458 ? skin(myorigin(sbp->sb_hp)) : NULL);
460 if (signing_caps) {
461 if (from == NULL) {
462 #ifdef HAVE_SSL
463 n_err(_("No *from* address for signing specified\n"));
464 goto jleave;
465 #endif
466 } else
467 sbp->sb_signer.l = strlen(sbp->sb_signer.s = from);
470 #ifdef HAVE_SMTP
471 if ((smtp = ok_vlook(smtp)) == NULL) { /* TODO v15 url_creat(,ok_vlook(mta)*/
472 char const *proto;
474 /* *smtp* OBSOLETE message in mta_start() */
475 if ((smtp = ok_vlook(mta)) == NULL ||
476 (proto = n_servbyname(smtp, NULL)) == NULL || *proto == '\0') {
477 rv = TRU1;
478 goto jleave;
482 if (!url_parse(&sbp->sb_url, CPROTO_SMTP, smtp))
483 goto jleave;
485 if (v15) {
486 if (shost == NULL) {
487 if (from == NULL)
488 goto jenofrom;
489 sbp->sb_url.url_u_h.l = strlen(sbp->sb_url.url_u_h.s = from);
490 } else
491 __sendout_ident = sbp->sb_url.url_u_h.s;
492 if (!ccred_lookup(&sbp->sb_ccred, &sbp->sb_url))
493 goto jleave;
494 } else {
495 if (sbp->sb_url.url_had_user || sbp->sb_url.url_pass.s != NULL) {
496 n_err(_("New-style URL used without *v15-compat* being set\n"));
497 goto jleave;
499 /* TODO part of the entire myorigin() disaster, get rid of this! */
500 if (from == NULL) {
501 jenofrom:
502 n_err(_("Your configuration requires a *from* address, "
503 "but none was given\n"));
504 goto jleave;
506 if (!ccred_lookup_old(&sbp->sb_ccred, CPROTO_SMTP, from))
507 goto jleave;
508 sbp->sb_url.url_u_h.l = strlen(sbp->sb_url.url_u_h.s = from);
511 rv = TRU1;
512 #endif /* HAVE_SMTP */
513 #if defined HAVE_SSL || defined HAVE_SMTP
514 jleave:
515 #endif
516 NYD_LEAVE;
517 return rv;
520 static int
521 a_sendout_attach_msg(struct header *hp, struct attachment *ap, FILE *fo)
523 struct message *mp;
524 char const *ccp;
525 int rv;
526 NYD_ENTER;
527 n_UNUSED(hp);
529 fprintf(fo, "\n--%s\nContent-Type: message/rfc822\n"
530 "Content-Disposition: inline\n", _sendout_boundary);
531 if ((ccp = ap->a_content_description) != NULL)
532 fprintf(fo, "Content-Description: %s\n", ccp);/* TODO MIME! */
533 putc('\n', fo);
535 mp = message + ap->a_msgno - 1;
536 touch(mp);
537 rv = (sendmp(mp, fo, 0, NULL, SEND_RFC822, NULL) < 0) ? -1 : 0;
538 NYD_LEAVE;
539 return rv;
542 static int
543 make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
544 char const *contenttype, char const *charset)
546 struct attachment *att;
547 int rv = -1;
548 NYD_ENTER;
550 fputs("This is a multi-part message in MIME format.\n", fo);
551 if (fsize(fi) != 0) {
552 char const *cp;
553 char *buf;
554 size_t sz, bufsize, cnt;
556 if (fprintf(fo, "\n--%s\n", _sendout_boundary) < 0 ||
557 _put_ct(fo, contenttype, charset) < 0 ||
558 _put_cte(fo, convert) < 0 ||
559 fprintf(fo, "Content-Disposition: inline\n") < 0)
560 goto jleave;
561 if (((cp = ok_vlook(stealthmua)) == NULL || !strcmp(cp, "noagent")) &&
562 (cp = a_sendout_random_id(hp, FAL0)) != NULL &&
563 fprintf(fo, "Content-ID: <%s>\n", cp) < 0)
564 goto jleave;
565 if(putc('\n', fo) == EOF)
566 goto jleave;
568 buf = smalloc(bufsize = SEND_LINESIZE);
569 if (convert == CONV_TOQP
570 #ifdef HAVE_ICONV
571 || iconvd != (iconv_t)-1
572 #endif
574 fflush(fi);
575 cnt = fsize(fi);
577 for (;;) {
578 if (convert == CONV_TOQP
579 #ifdef HAVE_ICONV
580 || iconvd != (iconv_t)-1
581 #endif
583 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
584 break;
585 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
586 break;
588 if (xmime_write(buf, sz, fo, convert, TD_ICONV) < 0) {
589 free(buf);
590 goto jleave;
593 free(buf);
595 if (ferror(fi))
596 goto jleave;
599 for (att = hp->h_attach; att != NULL; att = att->a_flink) {
600 if (att->a_msgno) {
601 if (a_sendout_attach_msg(hp, att, fo) != 0)
602 goto jleave;
603 } else if (a_sendout_attach_file(hp, 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 == NULL) {
630 n_perr(_("temporary mail file"), 0);
631 goto jleave;
633 if ((nfi = Fopen(tempMail, "r")) == NULL) {
634 n_perr(tempMail, 0);
635 Fclose(nfo);
637 Ftmp_release(&tempMail);
638 if (nfi == NULL)
639 goto jleave;
641 pstate &= ~PS_HEADER_NEEDED_MIME; /* TODO a hack should be carrier tracked */
643 contenttype = "text/plain"; /* XXX mail body - always text/plain, want XX? */
644 if((options & OPT_Mm_FLAG) && option_Mm_arg != NULL)
645 contenttype = option_Mm_arg;
646 convert = mime_type_classify_file(fi, &contenttype, &charset, &do_iconv);
648 #ifdef HAVE_ICONV
649 tcs = ok_vlook(ttycharset);
650 if ((convhdr = need_hdrconv(hp))) {
651 if (iconvd != (iconv_t)-1) /* XXX */
652 n_iconv_close(iconvd);
653 if (asccasecmp(convhdr, tcs) != 0 &&
654 (iconvd = n_iconv_open(convhdr, tcs)) == (iconv_t)-1 &&
655 (err = errno) != 0)
656 goto jiconv_err;
658 #endif
659 if (puthead(FAL0, hp, nfo,
660 (GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA | GUA | GMIME | GMSGID |
661 GIDENT | GREF | GDATE), SEND_MBOX, convert, contenttype, charset))
662 goto jerr;
663 #ifdef HAVE_ICONV
664 if (iconvd != (iconv_t)-1)
665 n_iconv_close(iconvd);
666 #endif
668 #ifdef HAVE_ICONV
669 if (do_iconv && charset != NULL) { /*TODO charset->mime_type_classify_file*/
670 if (asccasecmp(charset, tcs) != 0 &&
671 (iconvd = n_iconv_open(charset, tcs)) == (iconv_t)-1 &&
672 (err = errno) != 0) {
673 jiconv_err:
674 if (err == EINVAL)
675 n_err(_("Cannot convert from %s to %s\n"), tcs, charset);
676 else
677 n_perr("iconv_open", 0);
678 goto jerr;
681 #endif
683 if (hp->h_attach != NULL) {
684 if (make_multipart(hp, convert, fi, nfo, contenttype, charset) != 0)
685 goto jerr;
686 } else {
687 size_t sz, bufsize, cnt;
688 char *buf;
690 if (convert == CONV_TOQP
691 #ifdef HAVE_ICONV
692 || iconvd != (iconv_t)-1
693 #endif
695 fflush(fi);
696 cnt = fsize(fi);
698 buf = smalloc(bufsize = SEND_LINESIZE);
699 for (err = 0;;) {
700 if (convert == CONV_TOQP
701 #ifdef HAVE_ICONV
702 || iconvd != (iconv_t)-1
703 #endif
705 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
706 break;
707 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
708 break;
709 if (xmime_write(buf, sz, nfo, convert, TD_ICONV) < 0) {
710 err = 1;
711 break;
714 free(buf);
716 if (err || ferror(fi)) {
717 jerr:
718 Fclose(nfo);
719 Fclose(nfi);
720 #ifdef HAVE_ICONV
721 if (iconvd != (iconv_t)-1)
722 n_iconv_close(iconvd);
723 #endif
724 nfi = NULL;
725 goto jleave;
729 #ifdef HAVE_ICONV
730 if (iconvd != (iconv_t)-1)
731 n_iconv_close(iconvd);
732 #endif
734 fflush(nfo);
735 if ((err = ferror(nfo)))
736 n_perr(_("temporary mail file"), 0);
737 Fclose(nfo);
738 if (!err) {
739 fflush_rewind(nfi);
740 Fclose(fi);
741 } else {
742 Fclose(nfi);
743 nfi = NULL;
745 jleave:
746 NYD_LEAVE;
747 return nfi;
750 static bool_t
751 _check_dispo_notif(struct name *mdn, struct header *hp, FILE *fo)
753 char const *from;
754 bool_t rv = TRU1;
755 NYD_ENTER;
757 /* TODO smtp_disposition_notification (RFC 3798): relation to return-path
758 * TODO not yet checked */
759 if (!ok_blook(disposition_notification_send))
760 goto jleave;
762 if (mdn != NULL && mdn != (struct name*)0x1)
763 from = mdn->n_name;
764 else if ((from = myorigin(hp)) == NULL) {
765 if (options & OPT_D_V)
766 n_err(_("*disposition-notification-send*: no *from* set\n"));
767 goto jleave;
770 if (fmt("Disposition-Notification-To:", nalloc(n_UNCONST(from), 0), fo, 0))
771 rv = FAL0;
772 jleave:
773 NYD_LEAVE;
774 return rv;
777 static int
778 sendmail_internal(void *v, int recipient_record)
780 struct header head;
781 char *str = v;
782 int rv;
783 NYD_ENTER;
785 memset(&head, 0, sizeof head);
786 head.h_to = lextract(str, GTO | GFULL);
787 rv = mail1(&head, 0, NULL, NULL, recipient_record, 0);
788 NYD_LEAVE;
789 return (rv == 0);
792 static struct name *
793 _outof(struct name *names, FILE *fo, bool_t *senderror)
795 ui32_t pipecnt, xcnt, i;
796 int *fda;
797 char const *sh;
798 struct name *np;
799 FILE *fin = NULL, *fout;
800 NYD_ENTER;
802 /* Look through all recipients and do a quick return if no file or pipe
803 * addressee is found */
804 fda = NULL; /* Silence cc */
805 for (pipecnt = xcnt = 0, np = names; np != NULL; np = np->n_flink) {
806 if (np->n_type & GDEL)
807 continue;
808 switch (np->n_flags & NAME_ADDRSPEC_ISFILEORPIPE) {
809 case NAME_ADDRSPEC_ISFILE:
810 ++xcnt;
811 break;
812 case NAME_ADDRSPEC_ISPIPE:
813 ++pipecnt;
814 break;
817 if (pipecnt == 0 && xcnt == 0)
818 goto jleave;
820 /* Otherwise create an array of file descriptors for each found pipe
821 * addressee to get around the dup(2)-shared-file-offset problem, i.e.,
822 * each pipe subprocess needs its very own file descriptor, and we need
823 * to deal with that.
824 * To make our life a bit easier let's just use the auto-reclaimed
825 * string storage */
826 if (pipecnt == 0 || (options & OPT_DEBUG)) {
827 pipecnt = 0;
828 fda = NULL;
829 sh = NULL;
830 } else {
831 fda = salloc(sizeof(int) * pipecnt);
832 for (i = 0; i < pipecnt; ++i)
833 fda[i] = -1;
834 sh = ok_vlook(SHELL);
837 for (np = names; np != NULL; np = np->n_flink) {
838 if (!(np->n_flags & NAME_ADDRSPEC_ISFILEORPIPE))
839 continue;
841 /* In days of old we removed the entry from the the list; now for sake of
842 * header expansion we leave it in and mark it as deleted */
843 np->n_type |= GDEL;
845 if(options & OPT_D_VV)
846 n_err(_(">>> Writing message via %s\n"),
847 n_shexp_quote_cp(np->n_name, FAL0));
848 /* We _do_ write to STDOUT, anyway! */
849 if((options & OPT_DEBUG) && ((np->n_flags & NAME_ADDRSPEC_ISPIPE) ||
850 np->n_name[0] != '-' || np->n_name[1] != '\0'))
851 continue;
853 /* See if we have copied the complete message out yet. If not, do so */
854 if (image < 0) {
855 int c;
856 char *tempEdit;
858 if ((fout = Ftmp(&tempEdit, "outof",
859 OF_WRONLY | OF_HOLDSIGS | OF_REGISTER)) == NULL) {
860 n_perr(_("Creation of temporary image"), 0);
861 *senderror = TRU1;
862 goto jcant;
864 if ((image = open(tempEdit, O_RDWR | _O_CLOEXEC)) >= 0) {
865 _CLOEXEC_SET(image);
866 for (i = 0; i < pipecnt; ++i) {
867 int fd = open(tempEdit, O_RDONLY | _O_CLOEXEC);
868 if (fd < 0) {
869 close(image);
870 image = -1;
871 pipecnt = i;
872 break;
874 fda[i] = fd;
875 _CLOEXEC_SET(fd);
878 Ftmp_release(&tempEdit);
880 if (image < 0) {
881 n_perr(_("Creating descriptor duplicate of temporary image"), 0);
882 *senderror = TRU1;
883 Fclose(fout);
884 goto jcant;
887 fprintf(fout, "From %s %s", ok_vlook(LOGNAME), time_current.tc_ctime);
888 c = EOF;
889 while (i = c, (c = getc(fo)) != EOF)
890 putc(c, fout);
891 rewind(fo);
892 if ((int)i != '\n')
893 putc('\n', fout);
894 putc('\n', fout);
895 fflush(fout);
896 if (ferror(fout)) {
897 n_perr(_("Finalizing write of temporary image"), 0);
898 Fclose(fout);
899 goto jcantfout;
901 Fclose(fout);
903 /* If we have to serve file addressees, open reader */
904 if (xcnt != 0 && (fin = Fdopen(image, "r", FAL0)) == NULL) {
905 n_perr(_("Failed to open a temporary image duplicate"), 0);
906 jcantfout:
907 *senderror = TRU1;
908 close(image);
909 image = -1;
910 goto jcant;
913 /* From now on use xcnt as a counter for pipecnt */
914 xcnt = 0;
917 /* Now either copy "image" to the desired file or give it as the standard
918 * input to the desired program as appropriate */
919 if (np->n_flags & NAME_ADDRSPEC_ISPIPE) {
920 int pid;
921 sigset_t nset;
923 sigemptyset(&nset);
924 sigaddset(&nset, SIGHUP);
925 sigaddset(&nset, SIGINT);
926 sigaddset(&nset, SIGQUIT);
927 pid = start_command(sh, &nset, fda[xcnt++], COMMAND_FD_NULL, "-c",
928 np->n_name + 1, NULL, NULL);
929 if (pid < 0) {
930 n_err(_("Piping message to %s failed\n"),
931 n_shexp_quote_cp(np->n_name, FAL0));
932 *senderror = TRU1;
933 goto jcant;
935 free_child(pid);
936 } else {
937 int c;
938 char const *fname, *fnameq;
940 if ((fname = fexpand(np->n_name, FEXP_LOCAL | FEXP_NOPROTO)) == NULL) {
941 *senderror = TRU1;
942 goto jcant;
944 fnameq = n_shexp_quote_cp(fname, FAL0);
946 if(fname[0] == '-' && fname[1] == '\0')
947 fout = stdout;
948 else if ((fout = Zopen(fname, "a")) == NULL) {
949 n_err(_("Writing message to %s failed: %s\n"),
950 fnameq, strerror(errno));
951 *senderror = TRU1;
952 goto jcant;
954 rewind(fin);
955 while ((c = getc(fin)) != EOF)
956 putc(c, fout);
957 if (ferror(fout)) {
958 n_err(_("Writing message to %s failed: %s\n"),
959 fnameq, _("write error"));
960 *senderror = TRU1;
962 if(fout != stdout)
963 Fclose(fout);
966 jcant:
967 if (image < 0)
968 goto jdelall;
971 jleave:
972 if (fin != NULL)
973 Fclose(fin);
974 for (i = 0; i < pipecnt; ++i)
975 close(fda[i]);
976 if (image >= 0) {
977 close(image);
978 image = -1;
980 NYD_LEAVE;
981 return names;
983 jdelall:
984 while (np != NULL) {
985 if (np->n_flags & NAME_ADDRSPEC_ISFILEORPIPE)
986 np->n_type |= GDEL;
987 np = np->n_flink;
989 goto jleave;
992 static bool_t
993 mightrecord(FILE *fp, struct name *to, bool_t resend)
995 char *cp, *cq;
996 char const *ep;
997 bool_t rv = TRU1;
998 NYD_ENTER;
1000 if (options & OPT_DEBUG)
1001 cp = NULL;
1002 else if (to != NULL) {
1003 cp = savestr(skinned_name(to));
1004 for (cq = cp; *cq != '\0' && *cq != '@'; ++cq)
1006 *cq = '\0';
1007 } else
1008 cp = ok_vlook(record);
1010 if (cp != NULL) {
1011 if ((ep = expand(cp)) == NULL) {
1012 ep = "NULL";
1013 goto jbail;
1016 if (*ep != '/' && *ep != '+' && ok_blook(outfolder) &&
1017 which_protocol(ep) == PROTO_FILE) {
1018 size_t i = strlen(cp);
1019 cq = salloc(i + 1 +1);
1020 cq[0] = '+';
1021 memcpy(cq + 1, cp, i +1);
1022 cp = cq;
1023 if ((ep = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL) {
1024 ep = "NULL";
1025 goto jbail;
1029 if (!a_sendout__savemail(ep, fp, resend)) {
1030 jbail:
1031 n_err(_("Failed to save message in %s - message not sent\n"),
1032 n_shexp_quote_cp(ep, FAL0));
1033 exit_status |= EXIT_ERR;
1034 savedeadletter(fp, 1);
1035 rv = FAL0;
1038 NYD_LEAVE;
1039 return rv;
1042 static bool_t
1043 a_sendout__savemail(char const *name, FILE *fp, bool_t resend){
1044 FILE *fo;
1045 size_t bufsize, buflen, cnt;
1046 bool_t rv, emptyline;
1047 char *buf;
1048 NYD_ENTER;
1050 buf = smalloc(bufsize = LINESIZE);
1051 rv = emptyline = FAL0;
1053 if((fo = Zopen(name, "a+")) == NULL){
1054 if((fo = Zopen(name, "wx")) == NULL){
1055 n_perr(name, 0);
1056 goto j_leave;
1058 emptyline = TRU1;
1061 /* TODO RETURN check, but be aware of protocols: v15: Mailbox->lock()! */
1062 n_file_lock(fileno(fo), FLT_WRITE, 0,0, UIZ_MAX);
1064 rv = TRU1;
1066 if(!emptyline){
1067 if(fseek(fo, -2L, SEEK_END) == 0){ /* TODO Should be Mailbox::->append */
1068 switch(fread(buf, sizeof *buf, 2, fo)){
1069 case 2:
1070 if(buf[1] != '\n')
1071 emptyline = TRU1;
1072 break;
1073 case 1:
1074 if(buf[0] != '\n')
1075 emptyline = TRU1;
1076 break;
1077 default:
1078 if(ferror(fo)){
1079 n_perr(name, 0);
1080 rv = FAL0;
1081 goto jleave;
1084 if(emptyline){
1085 putc('\n', fo);
1086 fflush(fo);
1091 fflush_rewind(fp);
1093 fprintf(fo, "From %s %s", ok_vlook(LOGNAME), time_current.tc_ctime);
1094 for(emptyline = FAL0, buflen = 0, cnt = fsize(fp);
1095 fgetline(&buf, &bufsize, &cnt, &buflen, fp, 0) != NULL;){
1096 /* Only if we are resending it can happen that we have to quote From_
1097 * lines here; we don't generate messages which are ambiguous ourselves */
1098 if(resend){
1099 if(emptyline && is_head(buf, buflen, FAL0))
1100 putc('>', fo);
1101 }DBG(else assert(!is_head(buf, buflen, FAL0)); )
1103 emptyline = (buflen > 0 && *buf == '\n');
1104 fwrite(buf, sizeof *buf, buflen, fo);
1106 if(buflen > 0 && buf[buflen - 1] != '\n')
1107 putc('\n', fo);
1108 putc('\n', fo);
1109 fflush(fo);
1110 if(ferror(fo)){
1111 n_perr(name, 0);
1112 rv = FAL0;
1115 jleave:
1116 really_rewind(fp);
1117 if(Fclose(fo) != 0)
1118 rv = FAL0;
1119 j_leave:
1120 free(buf);
1121 NYD_LEAVE;
1122 return rv;
1125 static bool_t
1126 _transfer(struct sendbundle *sbp)
1128 struct name *np;
1129 ui32_t cnt;
1130 bool_t rv = TRU1;
1131 NYD_ENTER;
1133 for (cnt = 0, np = sbp->sb_to; np != NULL;) {
1134 char const k[] = "smime-encrypt-";
1135 size_t nl = strlen(np->n_name);
1136 char *cp, *vs = ac_alloc(sizeof(k)-1 + nl +1);
1137 memcpy(vs, k, sizeof(k) -1);
1138 memcpy(vs + sizeof(k) -1, np->n_name, nl +1);
1140 if ((cp = vok_vlook(vs)) != NULL) {
1141 #ifdef HAVE_SSL
1142 FILE *ef;
1144 if ((ef = smime_encrypt(sbp->sb_input, cp, np->n_name)) != NULL) {
1145 FILE *fisave = sbp->sb_input;
1146 struct name *nsave = sbp->sb_to;
1148 sbp->sb_to = ndup(np, np->n_type & ~(GFULL | GSKIN));
1149 sbp->sb_input = ef;
1150 if (!__mta_start(sbp))
1151 rv = FAL0;
1152 sbp->sb_to = nsave;
1153 sbp->sb_input = fisave;
1155 Fclose(ef);
1156 } else {
1157 #else
1158 n_err(_("No SSL support compiled in\n"));
1159 rv = FAL0;
1160 #endif
1161 n_err(_("Message not sent to: %s\n"), np->n_name);
1162 _sendout_error = TRU1;
1163 #ifdef HAVE_SSL
1165 #endif
1166 rewind(sbp->sb_input);
1168 if (np->n_flink != NULL)
1169 np->n_flink->n_blink = np->n_blink;
1170 if (np->n_blink != NULL)
1171 np->n_blink->n_flink = np->n_flink;
1172 if (np == sbp->sb_to)
1173 sbp->sb_to = np->n_flink;
1174 np = np->n_flink;
1175 } else {
1176 ++cnt;
1177 np = np->n_flink;
1179 ac_free(vs);
1182 if (cnt > 0 && (ok_blook(smime_force_encryption) || !__mta_start(sbp)))
1183 rv = FAL0;
1184 NYD_LEAVE;
1185 return rv;
1188 static bool_t
1189 __mta_start(struct sendbundle *sbp)
1191 pid_t pid;
1192 sigset_t nset;
1193 char const **args, *mta;
1194 bool_t rv;
1195 NYD_ENTER;
1197 if((mta = ok_vlook(smtp)) != NULL){
1198 OBSOLETE(_("please don't use *smtp*, but assign an SMTP URL to *mta*"));
1199 /* For *smtp* the smtp:// protocol was optional; be simple: don't check
1200 * that *smtp* is misused with file:// or so */
1201 if(n_servbyname(mta, NULL) == NULL)
1202 mta = savecat("smtp://", mta);
1203 rv = TRU1;
1204 }else{
1205 char const *proto;
1207 if((proto = ok_vlook(sendmail)) != NULL)
1208 OBSOLETE(_("please use *mta* instead of *sendmail*"));
1209 if((mta = ok_vlook(mta)) == NULL){ /* TODO v15: mta = ok_vlook(mta); */
1210 if(proto == NULL){
1211 mta = VAL_MTA;
1212 rv = FAL0;
1213 }else
1214 rv = TRU1;
1216 /* TODO for now this is pretty hacky: in v15 we should simply create
1217 * TODO an URL object; i.e., be able to do so, and it does it right
1218 * TODO I.e.,: url_creat(&url, ok_vlook(mta)); */
1219 else if((proto = n_servbyname(mta, NULL)) != NULL){
1220 if(*proto == '\0'){
1221 mta += sizeof("file://") -1;
1222 rv = FAL0;
1223 }else
1224 rv = TRU1;
1225 }else
1226 rv = FAL0;
1229 if(!rv){
1230 char const *mta_base;
1232 if((mta = fexpand(mta_base = mta, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1233 n_err(_("*mta* variable expansion failure: %s\n"),
1234 n_shexp_quote_cp(mta_base, FAL0));
1235 goto jstop;
1238 args = __mta_prepare_args(sbp->sb_to, sbp->sb_hp);
1239 if (options & OPT_DEBUG) {
1240 __mta_debug(sbp, mta, args);
1241 rv = TRU1;
1242 goto jleave;
1244 } else {
1245 n_UNINIT(args, NULL);
1246 #ifndef HAVE_SMTP
1247 n_err(_("No SMTP support compiled in\n"));
1248 goto jstop;
1249 #else
1250 if (options & OPT_DEBUG) {
1251 (void)smtp_mta(sbp);
1252 rv = TRU1;
1253 goto jleave;
1255 #endif
1258 /* Fork, set up the temporary mail file as standard input for "mail", and
1259 * exec with the user list we generated far above */
1260 if ((pid = fork_child()) == -1) {
1261 n_perr("fork", 0);
1262 jstop:
1263 savedeadletter(sbp->sb_input, 0);
1264 _sendout_error = TRU1;
1265 goto jleave;
1267 if (pid == 0) {
1268 sigemptyset(&nset);
1269 sigaddset(&nset, SIGHUP);
1270 sigaddset(&nset, SIGINT);
1271 sigaddset(&nset, SIGQUIT);
1272 sigaddset(&nset, SIGTSTP);
1273 sigaddset(&nset, SIGTTIN);
1274 sigaddset(&nset, SIGTTOU);
1275 freopen("/dev/null", "r", stdin);
1276 #ifdef HAVE_SMTP
1277 if (rv) {
1278 prepare_child(&nset, 0, 1);
1279 if (smtp_mta(sbp))
1280 _exit(EXIT_OK);
1281 } else
1282 #endif
1284 char const *ecp;
1285 int e;
1287 prepare_child(&nset, fileno(sbp->sb_input), -1);
1288 execv(mta, n_UNCONST(args));
1289 e = errno;
1290 ecp = (e != ENOENT) ? strerror(e)
1291 : _("executable not found (adjust *mta* variable)");
1292 n_err(_("Cannot start %s: %s\n"), n_shexp_quote_cp(mta, FAL0), ecp);
1294 savedeadletter(sbp->sb_input, 1);
1295 n_err(_("... message not sent\n"));
1296 _exit(EXIT_ERR);
1299 if ((options & (OPT_DEBUG | OPT_VERB)) || ok_blook(sendwait)) {
1300 if (!(rv = wait_child(pid, NULL)))
1301 _sendout_error = TRU1;
1302 } else {
1303 free_child(pid);
1304 rv = TRU1;
1306 jleave:
1307 NYD_LEAVE;
1308 return rv;
1311 static char const **
1312 __mta_prepare_args(struct name *to, struct header *hp)
1314 size_t vas_cnt, i, j;
1315 char **vas;
1316 char const **args, *cp, *cp_v15compat;
1317 bool_t snda;
1318 NYD_ENTER;
1320 if((cp_v15compat = ok_vlook(sendmail_arguments)) != NULL)
1321 OBSOLETE(_("please use *mta-arguments*, not *sendmail-arguments*"));
1322 if((cp = ok_vlook(mta_arguments)) == NULL)
1323 cp = cp_v15compat;
1324 if ((cp /* TODO v15: = ok_vlook(mta_arguments)*/) == NULL) {
1325 vas_cnt = 0;
1326 vas = NULL;
1327 } else {
1328 /* Don't assume anything on the content but do allocate exactly j slots;
1329 * like this getrawlist will never overflow (and return -1) */
1330 j = strlen(cp);
1331 vas = n_lofi_alloc(sizeof(*vas) * j);
1332 vas_cnt = (size_t)getrawlist(FAL0, vas, j, cp, j);
1335 i = 4 + smopts_cnt + vas_cnt + 4 + 1 + count(to) + 1;
1336 args = salloc(i * sizeof(char*));
1338 if((cp_v15compat = ok_vlook(sendmail_progname)) != NULL)
1339 OBSOLETE(_("please use *mta-argv0*, not *sendmail-progname*"));
1340 if((cp = ok_vlook(mta_argv0)) == NULL)
1341 cp = cp_v15compat;
1342 if(cp == NULL)
1343 cp = VAL_MTA_ARGV0;
1344 args[0] = cp/* TODO v15: = ok_vlook(mta_argv0) */;
1346 if ((snda = ok_blook(sendmail_no_default_arguments)))
1347 OBSOLETE(_("please use *mta-no-default-arguments*, "
1348 "not *sendmail-no-default-arguments*"));
1349 snda |= ok_blook(mta_no_default_arguments);
1350 if ((snda /* TODO v15: = ok_blook(mta_no_default_arguments)*/))
1351 i = 1;
1352 else {
1353 args[1] = "-i";
1354 i = 2;
1355 if (ok_blook(metoo))
1356 args[i++] = "-m";
1357 if (options & OPT_VERB)
1358 args[i++] = "-v";
1361 for (j = 0; j < smopts_cnt; ++j, ++i)
1362 args[i] = smopts[j];
1364 for (j = 0; j < vas_cnt; ++j, ++i)
1365 args[i] = vas[j];
1367 /* -r option? In conjunction with -t we act compatible to postfix(1) and
1368 * ignore it (it is -f / -F there) if the message specified From:/Sender:.
1369 * The interdependency with -t has been resolved in puthead() */
1370 if (!snda && ((options & OPT_r_FLAG) || ok_blook(r_option_implicit))) {
1371 struct name const *np;
1373 if (hp != NULL && (np = hp->h_from) != NULL) {
1374 /* However, what wasn't resolved there was the case that the message
1375 * specified multiple From: addresses and a Sender: */
1376 if ((pstate & PS_t_FLAG) && hp->h_sender != NULL)
1377 np = hp->h_sender;
1379 if (np->n_fullextra != NULL) {
1380 args[i++] = "-F";
1381 args[i++] = np->n_fullextra;
1383 cp = np->n_name;
1384 } else {
1385 assert(option_r_arg == NULL);
1386 cp = skin(myorigin(NULL));
1389 if (cp != NULL) {
1390 args[i++] = "-f";
1391 args[i++] = cp;
1395 /* Terminate option list to avoid false interpretation of system-wide
1396 * aliases that start with hyphen */
1397 if (!snda)
1398 args[i++] = "--";
1400 /* Receivers follow */
1401 for (; to != NULL; to = to->n_flink)
1402 if (!(to->n_type & GDEL))
1403 args[i++] = to->n_name;
1404 args[i] = NULL;
1406 if(vas != NULL)
1407 n_lofi_free(vas);
1408 NYD_LEAVE;
1409 return args;
1412 static void
1413 __mta_debug(struct sendbundle *sbp, char const *mta, char const **args)
1415 size_t cnt, bufsize, llen;
1416 char *buf;
1417 NYD_ENTER;
1419 n_err(_(">>> MTA: %s, arguments:"), n_shexp_quote_cp(mta, FAL0));
1420 for (; *args != NULL; ++args)
1421 n_err(" %s", n_shexp_quote_cp(*args, FAL0));
1422 n_err("\n");
1424 fflush_rewind(sbp->sb_input);
1426 cnt = fsize(sbp->sb_input);
1427 buf = NULL;
1428 bufsize = 0;
1429 while (fgetline(&buf, &bufsize, &cnt, &llen, sbp->sb_input, TRU1) != NULL) {
1430 buf[--llen] = '\0';
1431 n_err(">>> %s\n", buf);
1433 if (buf != NULL)
1434 free(buf);
1435 NYD_LEAVE;
1438 static char const *
1439 a_sendout_random_id(struct header *hp, bool_t msgid)
1441 struct tm *tmp;
1442 char const *h;
1443 size_t rl, i;
1444 char *rv, sep;
1445 NYD_ENTER;
1447 rv = NULL;
1449 if(msgid && hp != NULL && hp->h_message_id != NULL){
1450 rv = hp->h_message_id->n_name;
1451 goto jleave;
1454 if(ok_blook(message_id_disable))
1455 goto jleave;
1457 sep = '%';
1458 rl = 9;
1459 if((h = __sendout_ident) != NULL)
1460 goto jgen;
1461 if(ok_vlook(hostname) != NULL){
1462 h = nodename(1);
1463 sep = '@';
1464 rl = 15;
1465 goto jgen;
1467 if(hp != NULL && (h = skin(myorigin(hp))) != NULL && strchr(h, '@') != NULL)
1468 goto jgen;
1469 goto jleave;
1471 jgen:
1472 tmp = &time_current.tc_gm;
1473 i = sizeof("%04d%02d%02d%02d%02d%02d.%s%c%s") -1 + rl + strlen(h);
1474 rv = salloc(i +1);
1475 snprintf(rv, i, "%04d%02d%02d%02d%02d%02d.%s%c%s",
1476 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
1477 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
1478 getrandstring(rl), sep, h);
1479 rv[i] = '\0'; /* Because we don't test snprintf(3) return */
1480 jleave:
1481 NYD_LEAVE;
1482 return rv;
1485 static int
1486 fmt(char const *str, struct name *np, FILE *fo, enum fmt_flags ff)
1488 enum {
1489 m_INIT = 1<<0,
1490 m_COMMA = 1<<1,
1491 m_NOPF = 1<<2,
1492 m_NONAME = 1<<3,
1493 m_CSEEN = 1<<4
1494 } m = (ff & GCOMMA) ? m_COMMA : 0;
1495 ssize_t col, len;
1496 int rv = 1;
1497 NYD_ENTER;
1499 col = strlen(str);
1500 if (col) {
1501 fwrite(str, sizeof *str, col, fo);
1502 #undef _X
1503 #define _X(S) (col == sizeof(S) -1 && !asccasecmp(str, S))
1504 if (ff & GFILES) {
1506 } else if (_X("reply-to:") || _X("mail-followup-to:") ||
1507 _X("references:") || _X("disposition-notification-to:"))
1508 m |= m_NOPF | m_NONAME;
1509 else if (ok_blook(add_file_recipients)) {
1511 } else if (_X("to:") || _X("cc:") || _X("bcc:") || _X("resent-to:"))
1512 m |= m_NOPF;
1513 #undef _X
1516 for (; np != NULL; np = np->n_flink) {
1517 if (is_addr_invalid(np,
1518 EACM_NOLOG | (m & m_NONAME ? EACM_NONAME : EACM_NONE)))
1519 continue;
1520 /* File and pipe addresses only printed with set *add-file-recipients* */
1521 if ((m & m_NOPF) && is_fileorpipe_addr(np))
1522 continue;
1524 if ((m & (m_INIT | m_COMMA)) == (m_INIT | m_COMMA)) {
1525 if (putc(',', fo) == EOF)
1526 goto jleave;
1527 m |= m_CSEEN;
1528 ++col;
1531 len = strlen(np->n_fullname);
1532 if (np->n_type & GREF)
1533 len += 2;
1534 ++col; /* The separating space */
1535 if ((m & m_INIT) && /*col > 1 &&*/
1536 UICMP(z, col + len, >,
1537 (np->n_type & GREF ? MIME_LINELEN : 72))) {
1538 if (fputs("\n ", fo) == EOF)
1539 goto jleave;
1540 col = 1;
1541 m &= ~m_CSEEN;
1542 } else
1543 putc(' ', fo);
1544 m = (m & ~m_CSEEN) | m_INIT;
1546 /* C99 */{
1547 char *hb;
1549 /* GREF needs to be placed in angle brackets, but which are missing */
1550 hb = np->n_fullname;
1551 if(np->n_type & GREF){
1552 assert(len == strlen(np->n_fullname) + 2);
1553 hb = n_lofi_alloc(len +1);
1554 len -= 2;
1555 hb[0] = '<';
1556 hb[len + 1] = '>';
1557 hb[len + 2] = '\0';
1558 memcpy(&hb[1], np->n_fullname, len);
1559 len += 2;
1561 len = xmime_write(hb, len, fo,
1562 ((ff & FMT_DOMIME) ? CONV_TOHDR_A : CONV_NONE), TD_ICONV);
1563 if(np->n_type & GREF)
1564 n_lofi_free(hb);
1566 if (len < 0)
1567 goto jleave;
1568 col += len;
1571 if (putc('\n', fo) != EOF)
1572 rv = 0;
1573 jleave:
1574 NYD_LEAVE;
1575 return rv;
1578 static int
1579 infix_resend(FILE *fi, FILE *fo, struct message *mp, struct name *to,
1580 int add_resent)
1582 size_t cnt, c, bufsize = 0;
1583 char *buf = NULL;
1584 char const *cp;
1585 struct name *fromfield = NULL, *senderfield = NULL, *mdn;
1586 int rv = 1;
1587 NYD_ENTER;
1589 cnt = mp->m_size;
1591 /* Write the Resent-Fields */
1592 if (add_resent) {
1593 fputs("Resent-", fo);
1594 mkdate(fo, "Date");
1595 if ((cp = myaddrs(NULL)) != NULL) {
1596 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-From:", fo,
1597 &fromfield, 0))
1598 goto jleave;
1600 /* TODO RFC 5322: Resent-Sender SHOULD NOT be used if it's EQ -From: */
1601 if ((cp = ok_vlook(sender)) != NULL) {
1602 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-Sender:", fo,
1603 &senderfield, 0))
1604 goto jleave;
1606 if (fmt("Resent-To:", to, fo, FMT_COMMA))
1607 goto jleave;
1608 if (((cp = ok_vlook(stealthmua)) == NULL || !strcmp(cp, "noagent")) &&
1609 (cp = a_sendout_random_id(NULL, TRU1)) != NULL &&
1610 fprintf(fo, "Resent-Message-ID: <%s>\n", cp) < 0)
1611 goto jleave;
1614 if ((mdn = n_UNCONST(check_from_and_sender(fromfield, senderfield))) == NULL)
1615 goto jleave;
1616 if (!_check_dispo_notif(mdn, NULL, fo))
1617 goto jleave;
1619 /* Write the original headers */
1620 while (cnt > 0) {
1621 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1622 break;
1623 if (ascncasecmp("status:", buf, 7) &&
1624 ascncasecmp("disposition-notification-to:", buf, 28) &&
1625 !is_head(buf, c, FAL0))
1626 fwrite(buf, sizeof *buf, c, fo);
1627 if (cnt > 0 && *buf == '\n')
1628 break;
1631 /* Write the message body */
1632 while (cnt > 0) {
1633 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1634 break;
1635 if (cnt == 0 && *buf == '\n')
1636 break;
1637 fwrite(buf, sizeof *buf, c, fo);
1639 if (buf != NULL)
1640 free(buf);
1641 if (ferror(fo)) {
1642 n_perr(_("temporary mail file"), 0);
1643 goto jleave;
1645 rv = 0;
1646 jleave:
1647 NYD_LEAVE;
1648 return rv;
1651 FL int
1652 mail(struct name *to, struct name *cc, struct name *bcc, char *subject,
1653 struct attachment *attach, char *quotefile, int recipient_record)
1655 struct header head;
1656 struct str in, out;
1657 NYD_ENTER;
1659 memset(&head, 0, sizeof head);
1661 /* The given subject may be in RFC1522 format. */
1662 if (subject != NULL) {
1663 in.s = subject;
1664 in.l = strlen(subject);
1665 mime_fromhdr(&in, &out, /* TODO ??? TD_ISPR |*/ TD_ICONV);
1666 head.h_subject = out.s;
1668 head.h_to = to;
1669 head.h_cc = cc;
1670 head.h_bcc = bcc;
1671 head.h_attach = attach;
1673 mail1(&head, 0, NULL, quotefile, recipient_record, 0);
1675 if (subject != NULL)
1676 free(out.s);
1677 NYD_LEAVE;
1678 return 0;
1681 FL int
1682 c_sendmail(void *v)
1684 int rv;
1685 NYD_ENTER;
1687 rv = sendmail_internal(v, 0);
1688 NYD_LEAVE;
1689 return rv;
1692 FL int
1693 c_Sendmail(void *v)
1695 int rv;
1696 NYD_ENTER;
1698 rv = sendmail_internal(v, 1);
1699 NYD_LEAVE;
1700 return rv;
1703 FL enum okay
1704 mail1(struct header *hp, int printheaders, struct message *quote,
1705 char *quotefile, int recipient_record, int doprefix)
1707 struct n_sigman sm;
1708 struct sendbundle sb;
1709 struct name *to;
1710 bool_t dosign;
1711 FILE *mtf, *nmtf;
1712 enum okay rv;
1713 NYD_ENTER;
1715 _sendout_error = FAL0;
1716 __sendout_ident = NULL;
1717 rv = STOP;
1718 mtf = NULL;
1720 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL) {
1721 case 0:
1722 break;
1723 default:
1724 goto jleave;
1727 /* Update some globals we likely need first */
1728 time_current_update(&time_current, TRU1);
1730 /* Collect user's mail from standard input. Get the result as mtf */
1731 mtf = collect(hp, printheaders, quote, quotefile, doprefix, &_sendout_error);
1732 if (mtf == NULL)
1733 goto jleave;
1735 dosign = TRUM1;
1737 /* */
1738 if (options & OPT_INTERACTIVE) {
1739 if (ok_blook(asksign))
1740 dosign = getapproval(_("Sign this message"), TRU1);
1743 if (fsize(mtf) == 0) {
1744 if (options & OPT_E_FLAG)
1745 goto jleave;
1746 if (hp->h_subject == NULL)
1747 printf(_("No message, no subject; hope that's ok\n"));
1748 else if (ok_blook(bsdcompat) || ok_blook(bsdmsgs))
1749 printf(_("Null message body; hope that's ok\n"));
1752 if (dosign == TRUM1)
1753 dosign = ok_blook(smime_sign); /* TODO USER@HOST <-> *from* +++!!! */
1754 #ifndef HAVE_SSL
1755 if (dosign) {
1756 n_err(_("No SSL support compiled in\n"));
1757 goto jleave;
1759 #endif
1761 /* XXX Update time_current again; once collect() offers editing of more
1762 * XXX headers, including Date:, this must only happen if Date: is the
1763 * XXX same that it was before collect() (e.g., postponing etc.).
1764 * XXX But *do* update otherwise because the mail seems to be backdated
1765 * XXX if the user edited some time, which looks odd and it happened
1766 * XXX to me that i got mis-dated response mails due to that... */
1767 time_current_update(&time_current, TRU1);
1769 /* TODO hrmpf; the MIME/send layer rewrite MUST address the init crap:
1770 * TODO setup the header ONCE; note this affects edit.c, collect.c ...,
1771 * TODO but: offer a hook that rebuilds/expands/checks/fixates all
1772 * TODO header fields ONCE, call that ONCE after user editing etc. has
1773 * TODO completed (one edit cycle) */
1775 /* Take the user names from the combined to and cc lists and do all the
1776 * alias processing. The POSIX standard says:
1777 * The names shall be substituted when alias is used as a recipient
1778 * address specified by the user in an outgoing message (that is,
1779 * other recipients addressed indirectly through the reply command
1780 * shall not be substituted in this manner).
1781 * S-nail thus violates POSIX, as has been pointed out correctly by
1782 * Martin Neitzel, but logic and usability of POSIX standards is not seldom
1783 * disputable anyway. Go for user friendliness */
1785 to = namelist_vaporise_head(hp,
1786 (EACM_NORMAL |
1787 (!(expandaddr_to_eaf() & EAF_NAME) ? EACM_NONAME : EACM_NONE)),
1788 TRU1, &_sendout_error);
1789 if (to == NULL) {
1790 n_err(_("No recipients specified\n"));
1791 goto jfail_dead;
1793 if (_sendout_error < 0) {
1794 n_err(_("Some addressees were classified as \"hard error\"\n"));
1795 goto jfail_dead;
1798 /* */
1799 memset(&sb, 0, sizeof sb);
1800 sb.sb_hp = hp;
1801 sb.sb_to = to;
1802 sb.sb_input = mtf;
1803 if ((dosign || count_nonlocal(to) > 0) &&
1804 !_sendbundle_setup_creds(&sb, (dosign > 0)))
1805 /* TODO saving $DEAD and recovering etc is not yet well defined */
1806 goto jfail_dead;
1808 /* 'Bit ugly kind of control flow until we find a charset that does it */
1809 for (charset_iter_reset(hp->h_charset);; charset_iter_next()) {
1810 int err;
1812 if (!charset_iter_is_valid())
1814 else if ((nmtf = infix(hp, mtf)) != NULL)
1815 break;
1816 else if ((err = errno) == EILSEQ || err == EINVAL) {
1817 rewind(mtf);
1818 continue;
1821 n_perr(_("Failed to create encoded message"), 0);
1822 goto jfail_dead;
1824 mtf = nmtf;
1826 /* */
1827 #ifdef HAVE_SSL
1828 if (dosign) {
1829 if ((nmtf = smime_sign(mtf, sb.sb_signer.s)) == NULL)
1830 goto jfail_dead;
1831 Fclose(mtf);
1832 mtf = nmtf;
1834 #endif
1836 /* TODO truly - i still don't get what follows: (1) we deliver file
1837 * TODO and pipe addressees, (2) we mightrecord() and (3) we transfer
1838 * TODO even if (1) savedeadletter() etc. To me this doesn't make sense? */
1840 /* Deliver pipe and file addressees */
1841 to = _outof(to, mtf, &_sendout_error);
1842 if (_sendout_error)
1843 savedeadletter(mtf, FAL0);
1845 to = elide(to); /* XXX needed only to drop GDELs due to _outof()! */
1847 { ui32_t cnt = count(to);
1848 if ((!recipient_record || cnt > 0) &&
1849 !mightrecord(mtf, (recipient_record ? to : NULL), FAL0))
1850 goto jleave;
1851 if (cnt > 0) {
1852 sb.sb_hp = hp;
1853 sb.sb_to = to;
1854 sb.sb_input = mtf;
1855 if (_transfer(&sb))
1856 rv = OKAY;
1857 } else if (!_sendout_error)
1858 rv = OKAY;
1861 n_sigman_cleanup_ping(&sm);
1862 jleave:
1863 if(mtf != NULL){
1864 Fclose(mtf);
1865 temporary_unroll_compose_mode();
1867 if (_sendout_error)
1868 exit_status |= EXIT_SEND_ERROR;
1869 NYD_LEAVE;
1870 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
1871 return rv;
1873 jfail_dead:
1874 _sendout_error = TRU1;
1875 savedeadletter(mtf, TRU1);
1876 n_err(_("... message not sent\n"));
1877 goto jleave;
1880 FL int
1881 mkdate(FILE *fo, char const *field)
1883 struct tm tmpgm, *tmptr;
1884 int tzdiff, tzdiff_hour, tzdiff_min, rv;
1885 NYD_ENTER;
1887 memcpy(&tmpgm, &time_current.tc_gm, sizeof tmpgm);
1888 tzdiff = time_current.tc_time - mktime(&tmpgm);
1889 tzdiff_hour = (int)(tzdiff / 60);
1890 tzdiff_min = tzdiff_hour % 60;
1891 tzdiff_hour /= 60;
1892 tmptr = &time_current.tc_local;
1893 if (tmptr->tm_isdst > 0)
1894 ++tzdiff_hour;
1895 rv = fprintf(fo, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1896 field,
1897 weekday_names[tmptr->tm_wday],
1898 tmptr->tm_mday, month_names[tmptr->tm_mon],
1899 tmptr->tm_year + 1900, tmptr->tm_hour,
1900 tmptr->tm_min, tmptr->tm_sec,
1901 tzdiff_hour * 100 + tzdiff_min);
1902 NYD_LEAVE;
1903 return rv;
1906 FL int
1907 puthead(bool_t nosend_msg, struct header *hp, FILE *fo, enum gfield w,
1908 enum sendaction action, enum conversion convert, char const *contenttype,
1909 char const *charset)
1911 #define FMT_CC_AND_BCC() \
1912 do {\
1913 if ((w & GCC) && (hp->h_cc != NULL || nosend_msg == TRUM1)) {\
1914 if (fmt("Cc:", hp->h_cc, fo, ff))\
1915 goto jleave;\
1916 ++gotcha;\
1918 if ((w & GBCC) && (hp->h_bcc != NULL || nosend_msg == TRUM1)) {\
1919 if (fmt("Bcc:", hp->h_bcc, fo, ff))\
1920 goto jleave;\
1921 ++gotcha;\
1923 } while (0)
1925 char const *addr;
1926 size_t gotcha;
1927 struct name *np, *fromasender = NULL;
1928 int stealthmua, rv = 1;
1929 bool_t nodisp;
1930 enum fmt_flags ff;
1931 NYD_ENTER;
1933 if ((addr = ok_vlook(stealthmua)) != NULL)
1934 stealthmua = !strcmp(addr, "noagent") ? -1 : 1;
1935 else
1936 stealthmua = 0;
1937 gotcha = 0;
1938 nodisp = (action != SEND_TODISP);
1939 ff = (w & (GCOMMA | GFILES)) | (nodisp ? FMT_DOMIME : 0);
1941 if (w & GDATE)
1942 mkdate(fo, "Date"), ++gotcha;
1943 if (w & GIDENT) {
1944 if (hp->h_from == NULL || hp->h_sender == NULL)
1945 setup_from_and_sender(hp);
1947 if (hp->h_from != NULL) {
1948 if (fmt("From:", hp->h_from, fo, ff))
1949 goto jleave;
1950 ++gotcha;
1953 if (hp->h_sender != NULL) {
1954 if (fmt("Sender:", hp->h_sender, fo, ff))
1955 goto jleave;
1956 ++gotcha;
1959 fromasender = n_UNCONST(check_from_and_sender(hp->h_from, hp->h_sender));
1960 if (fromasender == NULL)
1961 goto jleave;
1962 /* Note that fromasender is (NULL,) 0x1 or real sender here */
1965 #if 1
1966 if ((w & GTO) && (hp->h_to != NULL || nosend_msg == TRUM1)) {
1967 if (fmt("To:", hp->h_to, fo, ff))
1968 goto jleave;
1969 ++gotcha;
1971 #else
1972 /* TODO Thought about undisclosed recipients:;, but would be such a fake
1973 * TODO given that we cannot handle group addresses. Ridiculous */
1974 if (w & GTO) {
1975 struct name *xto;
1977 if ((xto = hp->h_to) != NULL) {
1978 char const ud[] = "To: Undisclosed recipients:;\n" /* TODO groups */;
1980 if (count_nonlocal(xto) != 0 || ok_blook(add_file_recipients) ||
1981 (hp->h_cc != NULL && count_nonlocal(hp->h_cc) > 0))
1982 goto jto_fmt;
1983 if (fwrite(ud, 1, sizeof(ud) -1, fo) != sizeof(ud) -1)
1984 goto jleave;
1985 ++gotcha;
1986 } else if (nosend_msg == TRUM1) {
1987 jto_fmt:
1988 if (fmt("To:", hp->h_to, fo, ff))
1989 goto jleave;
1990 ++gotcha;
1993 #endif
1995 if (!ok_blook(bsdcompat) && !ok_blook(bsdorder))
1996 FMT_CC_AND_BCC();
1998 if ((w & GSUBJECT) && (hp->h_subject != NULL || nosend_msg == TRUM1)) {
1999 if (fwrite("Subject: ", sizeof(char), 9, fo) != 9)
2000 goto jleave;
2001 if (hp->h_subject != NULL) {
2002 char *sub = subject_re_trim(hp->h_subject);
2003 size_t sublen = strlen(sub);
2005 /* Trimmed something, (re-)add Re: */
2006 if (sub != hp->h_subject) {
2007 if (fwrite("Re: ", 1, 4, fo) != 4) /* RFC mandates eng. "Re: " */
2008 goto jleave;
2009 if (sublen > 0 &&
2010 xmime_write(sub, sublen, fo,
2011 (!nodisp ? CONV_NONE : CONV_TOHDR),
2012 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV)) < 0)
2013 goto jleave;
2015 /* This may be, e.g., a Fwd: XXX yes, unfortunately we do like that */
2016 else if (*sub != '\0') {
2017 if (xmime_write(sub, sublen, fo, (!nodisp ? CONV_NONE : CONV_TOHDR),
2018 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV)) < 0)
2019 goto jleave;
2022 ++gotcha;
2023 putc('\n', fo);
2026 if (ok_blook(bsdcompat) || ok_blook(bsdorder))
2027 FMT_CC_AND_BCC();
2029 if ((w & GMSGID) && stealthmua <= 0 &&
2030 (addr = a_sendout_random_id(hp, TRU1)) != NULL) {
2031 if (fprintf(fo, "Message-ID: <%s>\n", addr) < 0)
2032 goto jleave;
2033 ++gotcha;
2036 if ((np = hp->h_ref) != NULL && (w & GREF)) {
2037 if (fmt("References:", np, fo, 0))
2038 goto jleave;
2039 if (hp->h_in_reply_to == NULL && np->n_name != NULL) {
2040 while (np->n_flink != NULL)
2041 np = np->n_flink;
2042 if (!is_addr_invalid(np, /* TODO check that on parser side! */
2043 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME)) {
2044 if (fprintf(fo,
2045 "In-Reply-To: <%s>\n", np->n_name /*TODO RFC5322 3.6.4*/) < 0)
2046 goto jleave;
2047 ++gotcha;
2048 } else {
2049 n_err(_("Invalid address in mail header: %s\n"), np->n_name);
2050 goto jleave;
2054 if ((np = hp->h_in_reply_to) != NULL && (w & GREF)) {
2055 if (fprintf(fo,
2056 "In-Reply-To: <%s>\n", np->n_name /*TODO RFC 5322 3.6.4*/) < 0)
2057 goto jleave;
2058 ++gotcha;
2061 if (w & GIDENT) {
2062 /* Reply-To:. Be careful not to destroy a possible user input, duplicate
2063 * the list first.. TODO it is a terrible codebase.. */
2064 if ((np = hp->h_replyto) != NULL)
2065 np = namelist_dup(np, np->n_type);
2066 else if ((addr = ok_vlook(replyto)) != NULL)
2067 np = lextract(addr, GEXTRA | GFULL);
2068 if (np != NULL &&
2069 (np = elide(
2070 checkaddrs(usermap(np, TRU1), EACM_STRICT | EACM_NOLOG,
2071 NULL))) != NULL) {
2072 if (fmt("Reply-To:", np, fo, ff))
2073 goto jleave;
2074 ++gotcha;
2078 if ((w & GIDENT) && !nosend_msg) {
2079 /* Mail-Followup-To: TODO factor out this huge block of code */
2080 /* Place ourselfs in there if any non-subscribed list is an addressee */
2081 if ((hp->h_flags & HF_LIST_REPLY) || hp->h_mft != NULL ||
2082 ok_blook(followup_to)) {
2083 enum {_ANYLIST=1<<(HF__NEXT_SHIFT+0), _HADMFT=1<<(HF__NEXT_SHIFT+1)};
2085 ui32_t f = hp->h_flags | (hp->h_mft != NULL ? _HADMFT : 0);
2086 struct name *mft, *x;
2088 /* But for that, we have to remove all incarnations of ourselfs first.
2089 * TODO It is total crap that we have delete_alternates(), is_myname()
2090 * TODO or whatever; these work only with variables, not with data
2091 * TODO that is _currently_ in some header fields!!! v15.0: complete
2092 * TODO rewrite, object based, lazy evaluated, on-the-fly marked.
2093 * TODO then this should be a really cheap thing in here... */
2094 np = elide(delete_alternates(cat(
2095 namelist_dup(hp->h_to, GEXTRA | GFULL),
2096 namelist_dup(hp->h_cc, GEXTRA | GFULL))));
2097 addr = hp->h_list_post;
2099 for (mft = NULL; (x = np) != NULL;) {
2100 si8_t ml;
2101 np = np->n_flink;
2103 if ((ml = is_mlist(x->n_name, FAL0)) == MLIST_OTHER &&
2104 addr != NULL && !asccasecmp(addr, x->n_name))
2105 ml = MLIST_KNOWN;
2107 /* Any non-subscribed list? Add ourselves */
2108 switch (ml) {
2109 case MLIST_KNOWN:
2110 f |= HF_MFT_SENDER;
2111 /* FALLTHRU */
2112 case MLIST_SUBSCRIBED:
2113 f |= _ANYLIST;
2114 goto j_mft_add;
2115 case MLIST_OTHER:
2116 if (!(f & HF_LIST_REPLY)) {
2117 j_mft_add:
2118 if (!is_addr_invalid(x,
2119 EACM_STRICT | EACM_NOLOG | EACM_NONAME)) {
2120 x->n_flink = mft;
2121 mft = x;
2122 } /* XXX write some warning? if verbose?? */
2123 continue;
2125 /* And if this is a reply that honoured a MFT: header then we'll
2126 * also add all members of the original MFT: that are still
2127 * addressed by us, regardless of all other circumstances */
2128 else if (f & _HADMFT) {
2129 struct name *ox;
2130 for (ox = hp->h_mft; ox != NULL; ox = ox->n_flink)
2131 if (!asccasecmp(ox->n_name, x->n_name))
2132 goto j_mft_add;
2134 break;
2138 if (f & (_ANYLIST | _HADMFT) && mft != NULL) {
2139 if (((f & HF_MFT_SENDER) ||
2140 ((f & (_ANYLIST | _HADMFT)) == _HADMFT)) &&
2141 (np = fromasender) != NULL && np != (struct name*)0x1) {
2142 np = ndup(np, (np->n_type & ~GMASK) | GEXTRA | GFULL);
2143 np->n_flink = mft;
2144 mft = np;
2147 if (fmt("Mail-Followup-To:", mft, fo, ff))
2148 goto jleave;
2149 ++gotcha;
2153 if (!_check_dispo_notif(fromasender, hp, fo))
2154 goto jleave;
2157 if ((w & GUA) && stealthmua == 0) {
2158 if (fprintf(fo, "User-Agent: %s %s\n", uagent, ok_vlook(version)) < 0)
2159 goto jleave;
2160 ++gotcha;
2163 /* The user may have placed headers when editing */
2164 if(1){
2165 struct n_header_field *hfp;
2167 if((hfp = hp->h_user_headers) != NULL){
2168 if(!_sendout_header_list(fo, hfp, nodisp))
2169 goto jleave;
2170 ++gotcha;
2174 /* Custom headers, as via *customhdr* */
2175 if(!nosend_msg){
2176 struct n_header_field *hfp;
2178 /* With iconv support we likely have a cached result */
2179 if((hfp = hp->h_custom_headers) == NULL)
2180 hp->h_custom_headers = hfp = n_customhdr_query();
2181 if(hfp != NULL){
2182 if(!_sendout_header_list(fo, hfp, nodisp))
2183 goto jleave;
2184 ++gotcha;
2188 /* We don't need MIME unless.. we need MIME?! */
2189 if ((w & GMIME) && ((pstate & PS_HEADER_NEEDED_MIME) ||
2190 hp->h_attach != NULL ||
2191 ((options & OPT_Mm_FLAG) && option_Mm_arg != NULL) ||
2192 convert != CONV_7BIT || asccasecmp(charset, "US-ASCII"))) {
2193 ++gotcha;
2194 if (fputs("MIME-Version: 1.0\n", fo) == EOF)
2195 goto jleave;
2196 if (hp->h_attach != NULL) {
2197 _sendout_boundary = mime_param_boundary_create();/*TODO carrier*/
2198 if (fprintf(fo,
2199 "Content-Type: multipart/mixed;\n boundary=\"%s\"\n",
2200 _sendout_boundary) < 0)
2201 goto jleave;
2202 } else {
2203 if (_put_ct(fo, contenttype, charset) < 0 || _put_cte(fo, convert) < 0)
2204 goto jleave;
2208 if (gotcha && (w & GNL))
2209 if (putc('\n', fo) == EOF)
2210 goto jleave;
2211 rv = 0;
2212 jleave:
2213 NYD_LEAVE;
2214 return rv;
2215 #undef FMT_CC_AND_BCC
2218 FL enum okay
2219 resend_msg(struct message *mp, struct name *to, int add_resent) /* TODO check */
2221 struct sendbundle sb;
2222 FILE *ibuf, *nfo, *nfi;
2223 char *tempMail;
2224 enum okay rv = STOP;
2225 NYD_ENTER;
2227 _sendout_error = FAL0;
2228 __sendout_ident = NULL;
2230 /* Update some globals we likely need first */
2231 time_current_update(&time_current, TRU1);
2233 if ((to = checkaddrs(to,
2234 (EACM_NORMAL |
2235 (!(expandaddr_to_eaf() & EAF_NAME) ? EACM_NONAME : EACM_NONE)),
2236 &_sendout_error)) == NULL)
2237 goto jleave;
2238 /* For the _sendout_error<0 case we want to wait until we can write DEAD! */
2239 if (_sendout_error < 0)
2240 n_err(_("Some addressees were classified as \"hard error\"\n"));
2242 if ((nfo = Ftmp(&tempMail, "resend", OF_WRONLY | OF_HOLDSIGS | OF_REGISTER))
2243 == NULL) {
2244 _sendout_error = TRU1;
2245 n_perr(_("temporary mail file"), 0);
2246 goto jleave;
2248 if ((nfi = Fopen(tempMail, "r")) == NULL)
2249 n_perr(tempMail, 0);
2250 Ftmp_release(&tempMail);
2251 if (nfi == NULL)
2252 goto jerr_o;
2254 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
2255 goto jerr_io;
2257 memset(&sb, 0, sizeof sb);
2258 sb.sb_to = to;
2259 sb.sb_input = nfi;
2260 if (count_nonlocal(to) > 0 && !_sendbundle_setup_creds(&sb, FAL0))
2261 /* ..wait until we can write DEAD */
2262 _sendout_error = -1;
2264 if (infix_resend(ibuf, nfo, mp, to, add_resent) != 0) {
2265 jfail_dead:
2266 savedeadletter(nfi, TRU1);
2267 n_err(_("... message not sent\n"));
2268 jerr_io:
2269 Fclose(nfi);
2270 jerr_o:
2271 Fclose(nfo);
2272 _sendout_error = TRU1;
2273 goto jleave;
2276 if (_sendout_error < 0)
2277 goto jfail_dead;
2279 Fclose(nfo);
2280 rewind(nfi);
2282 to = _outof(to, nfi, &_sendout_error);
2284 if (_sendout_error)
2285 savedeadletter(nfi, FAL0);
2287 if (count(to = elide(to)) != 0) {
2288 if (!ok_blook(record_resent) || mightrecord(nfi, NULL, TRU1)) {
2289 sb.sb_to = to;
2290 /*sb.sb_input = nfi;*/
2291 if (_transfer(&sb))
2292 rv = OKAY;
2294 } else if (!_sendout_error)
2295 rv = OKAY;
2297 Fclose(nfi);
2298 jleave:
2299 if (_sendout_error)
2300 exit_status |= EXIT_SEND_ERROR;
2301 NYD_LEAVE;
2302 return rv;
2305 FL void
2306 savedeadletter(FILE *fp, bool_t fflush_rewind_first){
2307 struct n_string line;
2308 int c;
2309 enum {a_NONE, a_INIT = 1<<0, a_BODY = 1<<1, a_NL = 1<<2} flags;
2310 ul_i bytes, lines;
2311 FILE *dbuf;
2312 char const *cp, *cpq;
2313 NYD_ENTER;
2315 if(!ok_blook(save))
2316 goto jleave;
2318 if(fflush_rewind_first){
2319 fflush(fp);
2320 rewind(fp);
2322 if(fsize(fp) == 0)
2323 goto jleave;
2325 cp = n_getdeadletter();
2326 cpq = n_shexp_quote_cp(cp, FAL0);
2328 if(options & OPT_DEBUG){
2329 n_err(_(">>> Would (try to) write $DEAD %s\n"), cpq);
2330 goto jleave;
2333 if((dbuf = Fopen(cp, "w")) == NULL){
2334 n_perr(_("Cannot save to $DEAD"), 0);
2335 goto jleave;
2337 n_file_lock(fileno(dbuf), FLT_WRITE, 0,0, UIZ_MAX); /* XXX Natomic */
2339 printf("%s ", cpq);
2340 fflush(stdout);
2342 /* TODO savedeadletter() non-conforming: should check whether we have any
2343 * TODO headers, if not we need to place "something", anything will do.
2344 * TODO MIME is completely missing, we use MBOXO quoting!! Yuck.
2345 * TODO I/O error handling missing. Yuck! */
2346 n_string_reserve(n_string_creat_auto(&line), 2 * SEND_LINESIZE);
2347 bytes = (ul_i)fprintf(dbuf, "From %s %s",
2348 ok_vlook(LOGNAME), time_current.tc_ctime);
2349 lines = 1;
2350 for(flags = a_NONE, c = '\0'; c != EOF; bytes += line.s_len, ++lines){
2351 n_string_trunc(&line, 0);
2352 while((c = getc(fp)) != EOF && c != '\n')
2353 n_string_push_c(&line, c);
2355 /* TODO It may be that we have only some plain text. It may be that we
2356 * TODO have a complete MIME encoded message. We don't know, and we
2357 * TODO have no usable mechanism to dig it!! tWe need v15! */
2358 if(!(flags & a_INIT)){
2359 size_t i;
2361 /* Throw away leading empty lines! */
2362 if(line.s_len == 0)
2363 continue;
2364 for(i = 0; i < line.s_len; ++i){
2365 if(fieldnamechar(line.s_dat[i]))
2366 continue;
2367 if(line.s_dat[i] == ':'){
2368 flags |= a_INIT;
2369 break;
2370 }else{
2371 /* We have no headers, this is already a body line! */
2372 flags |= a_INIT | a_BODY;
2373 break;
2376 /* Well, i had to check whether the RFC allows this. Assume we've
2377 * passed the headers, too, then! */
2378 if(i == line.s_len)
2379 flags |= a_INIT | a_BODY;
2381 if(flags & a_BODY){
2382 if(line.s_len >= 5 && !memcmp(line.s_dat, "From ", 5))
2383 n_string_unshift_c(&line, '>');
2385 if(line.s_len == 0)
2386 flags |= a_BODY | a_NL;
2387 else
2388 flags &= ~a_NL;
2390 n_string_push_c(&line, '\n');
2391 fwrite(line.s_dat, sizeof *line.s_dat, line.s_len, dbuf);
2393 if(!(flags & a_NL)){
2394 putc('\n', dbuf);
2395 ++bytes;
2396 ++lines;
2398 n_string_gut(&line);
2400 Fclose(dbuf);
2401 printf("%lu/%lu\n", lines, bytes);
2402 fflush(stdout);
2404 rewind(fp);
2405 jleave:
2406 NYD_LEAVE;
2409 #undef SEND_LINESIZE
2411 /* s-it-mode */