strings.c: use NYD2 where applicable
[s-mailx.git] / sendout.c
blob4bd4359cb2a6551ab33e02f87e51c3b1eb23ad11
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, *smtp;
337 NYD_ENTER;
339 v15 = ok_blook(v15_compat);
340 shost = (v15 ? ok_vlook(smtp_hostname) : NULL);
341 from = ((signing_caps || !v15 || shost == NULL)
342 ? skin(myorigin(sbp->sb_hp)) : NULL);
344 if (signing_caps) {
345 if (from == NULL) {
346 #ifdef HAVE_SSL
347 fprintf(stderr, _("No *from* address for signing specified\n"));
348 goto jleave;
349 #endif
350 } else
351 sbp->sb_signer.l = strlen(sbp->sb_signer.s = from);
354 if ((smtp = ok_vlook(smtp)) == NULL) {
355 rv = TRU1;
356 goto jleave;
359 if (!url_parse(&sbp->sb_url, CPROTO_SMTP, smtp))
360 goto jleave;
362 if (v15) {
363 if (shost == NULL) {
364 assert(from != NULL);
365 sbp->sb_url.url_u_h.l = strlen(sbp->sb_url.url_u_h.s = from);
367 else
368 __sendout_ident = sbp->sb_url.url_u_h.s;
369 if (!ccred_lookup(&sbp->sb_ccred, &sbp->sb_url))
370 goto jleave;
371 } else {
372 if (sbp->sb_url.url_had_user || sbp->sb_url.url_pass.s != NULL) {
373 fprintf(stderr, "New-style URL used without *v15-compat* being set\n");
374 goto jleave;
376 assert(from != NULL);
377 if (!ccred_lookup_old(&sbp->sb_ccred, CPROTO_SMTP, from))
378 goto jleave;
379 sbp->sb_url.url_u_h.l = strlen(sbp->sb_url.url_u_h.s = from);
382 rv = TRU1;
383 jleave:
384 NYD_LEAVE;
385 return rv;
388 static char const **
389 _prepare_mta_args(struct name *to, struct header *hp)
391 size_t vas_count, i, j;
392 char **vas, *cp;
393 char const **args;
394 NYD_ENTER;
396 if ((cp = ok_vlook(sendmail_arguments)) == NULL) {
397 vas_count = 0;
398 vas = NULL;
399 } else {
400 j = strlen(cp);
401 vas = ac_alloc(sizeof(*vas) * (j >> 1));
402 vas_count = (size_t)getrawlist(cp, j, vas, (int)(j >> 1), TRU1);
405 i = 4 + smopts_count + vas_count + 2 + count(to) + 1;
406 args = salloc(i * sizeof(char*));
408 args[0] = ok_vlook(sendmail_progname);
409 if (args[0] == NULL || *args[0] == '\0')
410 args[0] = SENDMAIL_PROGNAME;
412 args[1] = "-i";
413 i = 2;
414 if (ok_blook(metoo))
415 args[i++] = "-m";
416 if (options & OPT_VERB)
417 args[i++] = "-v";
419 for (j = 0; j < smopts_count; ++j, ++i)
420 args[i] = smopts[j];
422 for (j = 0; j < vas_count; ++j, ++i)
423 args[i] = vas[j];
425 /* -r option? We may only pass skinned addresses */
426 if (options & OPT_r_FLAG) {
427 if (option_r_arg[0] != '\0')
428 cp = option_r_arg;
429 else if (hp != NULL) {
430 /* puthead() did it, then */
431 assert(hp->h_from != NULL);
432 cp = hp->h_from->n_name;
433 } else
434 cp = skin(myorigin(NULL)); /* XXX ugh! ugh!! */
435 if (cp != NULL) { /* XXX ugh! */
436 args[i++] = "-f";
437 args[i++] = cp;
441 /* Receivers follow */
442 for (; to != NULL; to = to->n_flink)
443 if (!(to->n_type & GDEL))
444 args[i++] = to->n_name;
445 args[i] = NULL;
447 if (vas != NULL)
448 ac_free(vas);
449 NYD_LEAVE;
450 return args;
453 static struct name *
454 fixhead(struct header *hp, struct name *tolist)
456 struct name **npp, *np;
457 NYD_ENTER;
459 tolist = elide(tolist);
461 hp->h_to = hp->h_cc = hp->h_bcc = NULL;
462 for (np = tolist; np != NULL; np = np->n_flink) {
463 switch (np->n_type & (GDEL | GMASK)) {
464 case GTO: npp = &hp->h_to; break;
465 case GCC: npp = &hp->h_cc; break;
466 case GBCC: npp = &hp->h_bcc; break;
467 default: continue;
469 *npp = cat(*npp, ndup(np, np->n_type | GFULL));
471 NYD_LEAVE;
472 return tolist;
475 static int
476 put_signature(FILE *fo, int convert)
478 char buf[SEND_LINESIZE], *sig, c = '\n';
479 FILE *fsig;
480 size_t sz;
481 int rv;
482 NYD_ENTER;
484 if ((sig = ok_vlook(signature)) == NULL || *sig == '\0') {
485 rv = 0;
486 goto jleave;
488 rv = -1;
490 if ((sig = file_expand(sig)) == NULL)
491 goto jleave;
493 if ((fsig = Fopen(sig, "r")) == NULL) {
494 perror(sig);
495 goto jleave;
497 while ((sz = fread(buf, sizeof *buf, SEND_LINESIZE, fsig)) != 0) {
498 c = buf[sz - 1];
499 if (xmime_write(buf, sz, fo, convert, TD_NONE, NULL) < 0)
500 goto jerr;
502 if (ferror(fsig)) {
503 jerr:
504 perror(sig);
505 Fclose(fsig);
506 goto jleave;
508 Fclose(fsig);
509 if (c != '\n')
510 putc('\n', fo);
512 rv = 0;
513 jleave:
514 NYD_LEAVE;
515 return rv;
518 static int
519 attach_message(struct attachment *ap, FILE *fo)
521 struct message *mp;
522 char const *ccp;
523 int rv;
524 NYD_ENTER;
526 fprintf(fo, "\n--%s\nContent-Type: message/rfc822\n"
527 "Content-Disposition: inline\n", _sendout_boundary);
528 if ((ccp = ap->a_content_description) != NULL)
529 fprintf(fo, "Content-Description: %s\n", ccp);
530 fputc('\n', fo);
532 mp = message + ap->a_msgno - 1;
533 touch(mp);
534 rv = (sendmp(mp, fo, 0, NULL, SEND_RFC822, NULL) < 0) ? -1 : 0;
535 NYD_LEAVE;
536 return rv;
539 static int
540 make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
541 char const *contenttype, char const *charset)
543 struct attachment *att;
544 int rv = -1;
545 NYD_ENTER;
547 fputs("This is a multi-part message in MIME format.\n", fo);
548 if (fsize(fi) != 0) {
549 char *buf;
550 size_t sz, bufsize, cnt;
552 fprintf(fo, "\n--%s\n", _sendout_boundary);
553 fprintf(fo, "Content-Type: %s", contenttype);
554 if (charset != NULL)
555 fprintf(fo, "; charset=%s", charset);
556 fprintf(fo, "\nContent-Transfer-Encoding: %s\n"
557 "Content-Disposition: inline\n\n", _get_encoding(convert));
559 buf = smalloc(bufsize = SEND_LINESIZE);
560 if (convert == CONV_TOQP
561 #ifdef HAVE_ICONV
562 || iconvd != (iconv_t)-1
563 #endif
565 fflush(fi);
566 cnt = fsize(fi);
568 for (;;) {
569 if (convert == CONV_TOQP
570 #ifdef HAVE_ICONV
571 || iconvd != (iconv_t)-1
572 #endif
574 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
575 break;
576 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
577 break;
579 if (xmime_write(buf, sz, fo, convert, TD_ICONV, NULL) < 0) {
580 free(buf);
581 goto jleave;
584 free(buf);
586 if (ferror(fi))
587 goto jleave;
588 if (charset != NULL)
589 put_signature(fo, convert);
592 for (att = hp->h_attach; att != NULL; att = att->a_flink) {
593 if (att->a_msgno) {
594 if (attach_message(att, fo) != 0)
595 goto jleave;
596 } else if (_attach_file(att, fo) != 0)
597 goto jleave;
600 /* the final boundary with two attached dashes */
601 fprintf(fo, "\n--%s--\n", _sendout_boundary);
602 rv = 0;
603 jleave:
604 NYD_LEAVE;
605 return rv;
608 static FILE *
609 infix(struct header *hp, FILE *fi) /* TODO check */
611 FILE *nfo, *nfi = NULL;
612 char *tempMail;
613 char const *contenttype, *charset = NULL;
614 enum conversion convert;
615 int do_iconv = 0, err;
616 #ifdef HAVE_ICONV
617 char const *tcs, *convhdr = NULL;
618 #endif
619 NYD_ENTER;
621 if ((nfo = Ftmp(&tempMail, "infix", OF_WRONLY | OF_HOLDSIGS | OF_REGISTER,
622 0600)) == NULL) {
623 perror(_("temporary mail file"));
624 goto jleave;
626 if ((nfi = Fopen(tempMail, "r")) == NULL) {
627 perror(tempMail);
628 Fclose(nfo);
630 Ftmp_release(&tempMail);
631 if (nfi == NULL)
632 goto jleave;
634 contenttype = "text/plain"; /* XXX mail body - always text/plain, want XX? */
635 convert = mime_classify_file(fi, &contenttype, &charset, &do_iconv);
637 #ifdef HAVE_ICONV
638 tcs = charset_get_lc();
639 if ((convhdr = need_hdrconv(hp, GTO | GSUBJECT | GCC | GBCC | GIDENT))) {
640 if (iconvd != (iconv_t)-1) /* XXX */
641 n_iconv_close(iconvd);
642 if (asccasecmp(convhdr, tcs) != 0 &&
643 (iconvd = n_iconv_open(convhdr, tcs)) == (iconv_t)-1 &&
644 (err = errno) != 0)
645 goto jiconv_err;
647 #endif
648 if (puthead(hp, nfo,
649 (GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA | GUA | GMIME | GMSGID |
650 GIDENT | GREF | GDATE), SEND_MBOX, convert, contenttype, charset))
651 goto jerr;
652 #ifdef HAVE_ICONV
653 if (iconvd != (iconv_t)-1)
654 n_iconv_close(iconvd);
655 #endif
657 #ifdef HAVE_ICONV
658 if (do_iconv && charset != NULL) { /*TODO charset->mime_classify_file*/
659 if (asccasecmp(charset, tcs) != 0 &&
660 (iconvd = n_iconv_open(charset, tcs)) == (iconv_t)-1 &&
661 (err = errno) != 0) {
662 jiconv_err:
663 if (err == EINVAL)
664 fprintf(stderr, _("Cannot convert from %s to %s\n"), tcs, charset);
665 else
666 perror("iconv_open");
667 goto jerr;
670 #endif
672 if (hp->h_attach != NULL) {
673 if (make_multipart(hp, convert, fi, nfo, contenttype, charset) != 0)
674 goto jerr;
675 } else {
676 size_t sz, bufsize, cnt;
677 char *buf;
679 if (convert == CONV_TOQP
680 #ifdef HAVE_ICONV
681 || iconvd != (iconv_t)-1
682 #endif
684 fflush(fi);
685 cnt = fsize(fi);
687 buf = smalloc(bufsize = SEND_LINESIZE);
688 for (err = 0;;) {
689 if (convert == CONV_TOQP
690 #ifdef HAVE_ICONV
691 || iconvd != (iconv_t)-1
692 #endif
694 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
695 break;
696 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
697 break;
698 if (xmime_write(buf, sz, nfo, convert, TD_ICONV, NULL) < 0) {
699 err = 1;
700 break;
703 free(buf);
705 if (err || ferror(fi)) {
706 jerr:
707 Fclose(nfo);
708 Fclose(nfi);
709 #ifdef HAVE_ICONV
710 if (iconvd != (iconv_t)-1)
711 n_iconv_close(iconvd);
712 #endif
713 nfi = NULL;
714 goto jleave;
716 if (charset != NULL)
717 put_signature(nfo, convert); /* XXX if (text/) !! */
720 #ifdef HAVE_ICONV
721 if (iconvd != (iconv_t)-1)
722 n_iconv_close(iconvd);
723 #endif
725 fflush(nfo);
726 if ((err = ferror(nfo)))
727 perror(_("temporary mail file"));
728 Fclose(nfo);
729 if (!err) {
730 fflush_rewind(nfi);
731 Fclose(fi);
732 } else {
733 Fclose(nfi);
734 nfi = NULL;
736 jleave:
737 NYD_LEAVE;
738 return nfi;
741 static bool_t
742 _check_dispo_notif(struct name *mdn, struct header *hp, FILE *fo)
744 char const *from;
745 bool_t rv = TRU1;
746 NYD_ENTER;
748 /* TODO smtp_disposition_notification (RFC 3798): relation to return-path
749 * TODO not yet checked */
750 if (!ok_blook(disposition_notification_send))
751 goto jleave;
753 if (mdn != NULL && mdn != (struct name*)0x1)
754 from = mdn->n_name;
755 else if ((from = myorigin(hp)) == NULL) {
756 if (options & OPT_D_V)
757 fprintf(stderr, _("*disposition-notification-send*: no *from* set\n"));
758 goto jleave;
761 if (fmt("Disposition-Notification-To:", nalloc(UNCONST(from), 0), fo,
762 GFILES, TRU1, 0))
763 rv = FAL0;
764 jleave:
765 NYD_LEAVE;
766 return rv;
769 static int
770 savemail(char const *name, FILE *fi)
772 FILE *fo;
773 char *buf;
774 size_t bufsize, buflen, cnt;
775 int prependnl = 0, rv = -1;
776 NYD_ENTER;
778 buf = smalloc(bufsize = LINESIZE);
780 if ((fo = Zopen(name, "a+", NULL)) == NULL) {
781 if ((fo = Zopen(name, "wx", NULL)) == NULL) {
782 perror(name);
783 goto jleave;
785 } else {
786 if (fseek(fo, -2L, SEEK_END) == 0) {
787 switch (fread(buf, sizeof *buf, 2, fo)) {
788 case 2:
789 if (buf[1] != '\n') {
790 prependnl = 1;
791 break;
793 /* FALLTHRU */
794 case 1:
795 if (buf[0] != '\n')
796 prependnl = 1;
797 break;
798 default:
799 if (ferror(fo)) {
800 perror(name);
801 goto jleave;
804 if (prependnl) {
805 putc('\n', fo);
807 fflush(fo);
811 fprintf(fo, "From %s %s", myname, time_current.tc_ctime);
812 fflush_rewind(fi);
813 cnt = fsize(fi);
814 buflen = 0;
815 while (fgetline(&buf, &bufsize, &cnt, &buflen, fi, 0) != NULL) {
816 #ifdef HAVE_DEBUG /* TODO assert legacy */
817 assert(!is_head(buf, buflen));
818 #else
819 if (is_head(buf, buflen))
820 putc('>', fo);
821 #endif
822 fwrite(buf, sizeof *buf, buflen, fo);
824 if (buflen && *(buf + buflen - 1) != '\n')
825 putc('\n', fo);
826 putc('\n', fo);
827 fflush(fo);
829 rv = 0;
830 if (ferror(fo)) {
831 perror(name);
832 rv = -1;
834 if (Fclose(fo) != 0)
835 rv = -1;
836 fflush_rewind(fi);
837 jleave:
838 free(buf);
839 NYD_LEAVE;
840 return rv;
843 static int
844 sendmail_internal(void *v, int recipient_record)
846 struct header head;
847 char *str = v;
848 int rv;
849 NYD_ENTER;
851 memset(&head, 0, sizeof head);
852 head.h_to = lextract(str, GTO | GFULL);
853 rv = mail1(&head, 0, NULL, NULL, recipient_record, 0);
854 NYD_LEAVE;
855 return rv;
858 static bool_t
859 _transfer(struct sendbundle *sbp)
861 struct name *np;
862 ui32_t cnt;
863 bool_t rv = TRU1;
864 NYD_ENTER;
866 for (cnt = 0, np = sbp->sb_to; np != NULL;) {
867 char const k[] = "smime-encrypt-";
868 size_t nl = strlen(np->n_name);
869 char *cp, *vs = ac_alloc(sizeof(k)-1 + nl +1);
870 memcpy(vs, k, sizeof(k) -1);
871 memcpy(vs + sizeof(k) -1, np->n_name, nl +1);
873 if ((cp = vok_vlook(vs)) != NULL) {
874 #ifdef HAVE_SSL
875 FILE *ef;
877 if ((ef = smime_encrypt(sbp->sb_input, cp, np->n_name)) != NULL) {
878 FILE *fisave = sbp->sb_input;
879 struct name *nsave = sbp->sb_to;
881 sbp->sb_to = ndup(np, np->n_type & ~(GFULL | GSKIN));
882 sbp->sb_input = ef;
883 if (!start_mta(sbp))
884 rv = FAL0;
885 sbp->sb_to = nsave;
886 sbp->sb_input = fisave;
888 Fclose(ef);
889 } else {
890 #else
891 fprintf(stderr, _("No SSL support compiled in.\n"));
892 rv = FAL0;
893 #endif
894 fprintf(stderr, _("Message not sent to <%s>\n"), np->n_name);
895 _sendout_error = TRU1;
896 #ifdef HAVE_SSL
898 #endif
899 rewind(sbp->sb_input);
901 if (np->n_flink != NULL)
902 np->n_flink->n_blink = np->n_blink;
903 if (np->n_blink != NULL)
904 np->n_blink->n_flink = np->n_flink;
905 if (np == sbp->sb_to)
906 sbp->sb_to = np->n_flink;
907 np = np->n_flink;
908 } else {
909 ++cnt;
910 np = np->n_flink;
912 ac_free(vs);
915 if (cnt > 0 && (ok_blook(smime_force_encryption) || !start_mta(sbp)))
916 rv = FAL0;
917 NYD_LEAVE;
918 return rv;
921 static bool_t
922 start_mta(struct sendbundle *sbp)
924 char const **args = NULL, **t, *mta;
925 char *smtp;
926 pid_t pid;
927 sigset_t nset;
928 bool_t rv = FAL0;
929 NYD_ENTER;
931 if ((smtp = ok_vlook(smtp)) == NULL) {
932 if ((mta = ok_vlook(sendmail)) != NULL) {
933 if ((mta = file_expand(mta)) == NULL)
934 goto jstop;
935 } else
936 mta = SENDMAIL;
938 args = _prepare_mta_args(sbp->sb_to, sbp->sb_hp);
939 if (options & OPT_DEBUG) {
940 printf(_("Sendmail arguments:"));
941 for (t = args; *t != NULL; ++t)
942 printf(" \"%s\"", *t);
943 printf("\n");
944 rv = TRU1;
945 goto jleave;
947 } else {
948 mta = NULL; /* Silence cc */
949 #ifndef HAVE_SMTP
950 fputs(_("No SMTP support compiled in.\n"), stderr);
951 goto jstop;
952 #else
953 /* XXX assert that sendbundle is setup? */
954 #endif
957 /* Fork, set up the temporary mail file as standard input for "mail", and
958 * exec with the user list we generated far above */
959 if ((pid = fork()) == -1) {
960 perror("fork");
961 jstop:
962 savedeadletter(sbp->sb_input, 0);
963 _sendout_error = TRU1;
964 goto jleave;
966 if (pid == 0) {
967 sigemptyset(&nset);
968 sigaddset(&nset, SIGHUP);
969 sigaddset(&nset, SIGINT);
970 sigaddset(&nset, SIGQUIT);
971 sigaddset(&nset, SIGTSTP);
972 sigaddset(&nset, SIGTTIN);
973 sigaddset(&nset, SIGTTOU);
974 freopen("/dev/null", "r", stdin);
975 #ifdef HAVE_SMTP
976 if (smtp != NULL) {
977 prepare_child(&nset, 0, 1);
978 if (smtp_mta(sbp))
979 _exit(0);
980 } else {
981 #endif
982 prepare_child(&nset, fileno(sbp->sb_input), -1);
983 /* If *record* is set then savemail() will move the file position;
984 * it'll call rewind(), but that may optimize away the systemcall if
985 * possible, and since dup2() shares the position with the original FD
986 * the MTA may end up reading nothing */
987 lseek(0, 0, SEEK_SET);
988 execv(mta, UNCONST(args));
989 perror(mta);
990 #ifdef HAVE_SMTP
992 #endif
993 savedeadletter(sbp->sb_input, 1);
994 fputs(_("... message not sent.\n"), stderr);
995 _exit(1);
997 if ((options & (OPT_DEBUG | OPT_VERB | OPT_BATCH_FLAG)) ||
998 ok_blook(sendwait)) {
999 if (wait_child(pid, NULL))
1000 rv = TRU1;
1001 else
1002 _sendout_error = TRU1;
1003 } else {
1004 rv = TRU1;
1005 free_child(pid);
1007 jleave:
1008 NYD_LEAVE;
1009 return rv;
1012 static bool_t
1013 mightrecord(FILE *fp, struct name *to)
1015 char *cp, *cq;
1016 char const *ep;
1017 bool_t rv = TRU1;
1018 NYD_ENTER;
1020 if (to != NULL) {
1021 cp = savestr(skinned_name(to));
1022 for (cq = cp; *cq != '\0' && *cq != '@'; ++cq)
1024 *cq = '\0';
1025 } else
1026 cp = ok_vlook(record);
1028 if (cp != NULL) {
1029 if ((ep = expand(cp)) == NULL) {
1030 ep = "NULL";
1031 goto jbail;
1034 if (*ep != '/' && *ep != '+' && ok_blook(outfolder) &&
1035 which_protocol(ep) == PROTO_FILE) {
1036 size_t i = strlen(cp);
1037 cq = salloc(i + 1 +1);
1038 cq[0] = '+';
1039 memcpy(cq + 1, cp, i +1);
1040 cp = cq;
1041 if ((ep = file_expand(cp)) == NULL) {
1042 ep = "NULL";
1043 goto jbail;
1047 if (savemail(ep, fp) != 0) {
1048 jbail:
1049 fprintf(stderr, _("Failed to save message in %s - message not sent\n"),
1050 ep);
1051 exit_status |= EXIT_ERR;
1052 savedeadletter(fp, 1);
1053 rv = FAL0;
1056 NYD_LEAVE;
1057 return rv;
1060 static void
1061 _message_id(FILE *fo, struct header *hp)
1063 char const *h;
1064 size_t rl;
1065 struct tm *tmp;
1066 NYD_ENTER;
1068 if (ok_blook(message_id_disable))
1069 goto jleave;
1071 if ((h = __sendout_ident) != NULL)
1072 rl = 8;
1073 else if ((h = ok_vlook(hostname)) != NULL)
1074 rl = 16;
1075 else if ((h = skin(myorigin(hp))) != NULL && strchr(h, '@') != NULL)
1076 rl = 8;
1077 else
1078 /* Up to MTA */
1079 goto jleave;
1081 tmp = &time_current.tc_gm;
1082 fprintf(fo, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
1083 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
1084 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
1085 getrandstring(rl), (rl == 8 ? '%' : '@'), h);
1086 jleave:
1087 __sendout_ident = NULL;
1088 NYD_LEAVE;
1091 static int
1092 fmt(char const *str, struct name *np, FILE *fo, int flags, int dropinvalid,
1093 int domime)
1095 enum {
1096 m_INIT = 1<<0,
1097 m_COMMA = 1<<1,
1098 m_NOPF = 1<<2,
1099 m_CSEEN = 1<<3
1100 } m = (flags & GCOMMA) ? m_COMMA : 0;
1101 ssize_t col, len;
1102 int rv = 1;
1103 NYD_ENTER;
1105 col = strlen(str);
1106 if (col) {
1107 fwrite(str, sizeof *str, col, fo);
1108 if (flags & GFILES)
1109 goto jstep;
1110 if (col == 9 && !asccasecmp(str, "reply-to:")) {
1111 m |= m_NOPF;
1112 goto jstep;
1114 if (ok_blook(add_file_recipients))
1115 goto jstep;
1116 if ((col == 3 && (!asccasecmp(str, "to:") || !asccasecmp(str, "cc:"))) ||
1117 (col == 4 && !asccasecmp(str, "bcc:")) ||
1118 (col == 10 && !asccasecmp(str, "Resent-To:")))
1119 m |= m_NOPF;
1121 jstep:
1122 for (; np != NULL; np = np->n_flink) {
1123 if ((m & m_NOPF) && is_fileorpipe_addr(np))
1124 continue;
1125 if (is_addr_invalid(np, !dropinvalid)) {
1126 if (dropinvalid)
1127 continue;
1128 else
1129 goto jleave;
1131 if ((m & (m_INIT | m_COMMA)) == (m_INIT | m_COMMA)) {
1132 putc(',', fo);
1133 m |= m_CSEEN;
1134 ++col;
1136 len = strlen(np->n_fullname);
1137 ++col; /* The separating space */
1138 if ((m & m_INIT) && /*col > 1 &&*/ UICMP(z, col + len, >, 72)) {
1139 fputs("\n ", fo);
1140 col = 1;
1141 m &= ~m_CSEEN;
1142 } else
1143 putc(' ', fo);
1144 m = (m & ~m_CSEEN) | m_INIT;
1145 len = xmime_write(np->n_fullname, len, fo,
1146 (domime ? CONV_TOHDR_A : CONV_NONE), TD_ICONV, NULL);
1147 if (len < 0)
1148 goto jleave;
1149 col += len;
1151 putc('\n', fo);
1152 rv = 0;
1153 jleave:
1154 NYD_LEAVE;
1155 return rv;
1158 static int
1159 infix_resend(FILE *fi, FILE *fo, struct message *mp, struct name *to,
1160 int add_resent)
1162 size_t cnt, c, bufsize = 0;
1163 char *buf = NULL;
1164 char const *cp;
1165 struct name *fromfield = NULL, *senderfield = NULL, *mdn;
1166 int rv = 1;
1167 NYD_ENTER;
1169 cnt = mp->m_size;
1171 /* Write the Resent-Fields */
1172 if (add_resent) {
1173 fputs("Resent-", fo);
1174 mkdate(fo, "Date");
1175 if ((cp = myaddrs(NULL)) != NULL) {
1176 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-From:", fo,
1177 &fromfield))
1178 goto jleave;
1180 if ((cp = ok_vlook(sender)) != NULL) {
1181 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-Sender:", fo,
1182 &senderfield))
1183 goto jleave;
1185 if (fmt("Resent-To:", to, fo, 1, 1, 0))
1186 goto jleave;
1187 if ((cp = ok_vlook(stealthmua)) == NULL || !strcmp(cp, "noagent")) {
1188 fputs("Resent-", fo);
1189 _message_id(fo, NULL);
1193 if ((mdn = UNCONST(check_from_and_sender(fromfield, senderfield))) == NULL)
1194 goto jleave;
1195 if (!_check_dispo_notif(mdn, NULL, fo))
1196 goto jleave;
1198 /* Write the original headers */
1199 while (cnt > 0) {
1200 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1201 break;
1202 /* XXX more checks: The From_ line may be seen when resending */
1203 /* During headers is_head() is actually overkill, so ^From_ is sufficient
1204 * && !is_head(buf, c) */
1205 if (ascncasecmp("status:", buf, 7) && strncmp("From ", buf, 5) &&
1206 ascncasecmp("disposition-notification-to:", buf, 28))
1207 fwrite(buf, sizeof *buf, c, fo);
1208 if (cnt > 0 && *buf == '\n')
1209 break;
1212 /* Write the message body */
1213 while (cnt > 0) {
1214 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1215 break;
1216 if (cnt == 0 && *buf == '\n')
1217 break;
1218 fwrite(buf, sizeof *buf, c, fo);
1220 if (buf != NULL)
1221 free(buf);
1222 if (ferror(fo)) {
1223 perror(_("temporary mail file"));
1224 goto jleave;
1226 rv = 0;
1227 jleave:
1228 NYD_LEAVE;
1229 return rv;
1232 FL int
1233 mail(struct name *to, struct name *cc, struct name *bcc, char *subject,
1234 struct attachment *attach, char *quotefile, int recipient_record)
1236 struct header head;
1237 struct str in, out;
1238 NYD_ENTER;
1240 memset(&head, 0, sizeof head);
1242 /* The given subject may be in RFC1522 format. */
1243 if (subject != NULL) {
1244 in.s = subject;
1245 in.l = strlen(subject);
1246 mime_fromhdr(&in, &out, /* TODO ??? TD_ISPR |*/ TD_ICONV);
1247 head.h_subject = out.s;
1249 if (!(options & OPT_t_FLAG)) {
1250 head.h_to = to;
1251 head.h_cc = cc;
1252 head.h_bcc = bcc;
1254 head.h_attach = attach;
1256 mail1(&head, 0, NULL, quotefile, recipient_record, 0);
1258 if (subject != NULL)
1259 free(out.s);
1260 NYD_LEAVE;
1261 return 0;
1264 FL int
1265 c_sendmail(void *v)
1267 int rv;
1268 NYD_ENTER;
1270 rv = sendmail_internal(v, 0);
1271 NYD_LEAVE;
1272 return rv;
1275 FL int
1276 c_Sendmail(void *v)
1278 int rv;
1279 NYD_ENTER;
1281 rv = sendmail_internal(v, 1);
1282 NYD_LEAVE;
1283 return rv;
1286 FL enum okay
1287 mail1(struct header *hp, int printheaders, struct message *quote,
1288 char *quotefile, int recipient_record, int doprefix)
1290 struct sendbundle sb;
1291 struct name *to;
1292 FILE *mtf, *nmtf;
1293 int dosign = -1, err;
1294 char const *cp;
1295 enum okay rv = STOP;
1296 NYD_ENTER;
1298 _sendout_error = FAL0;
1300 /* Update some globals we likely need first */
1301 time_current_update(&time_current, TRU1);
1303 /* */
1304 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1305 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL)));
1306 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1307 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL)));
1309 /* Collect user's mail from standard input. Get the result as mtf */
1310 mtf = collect(hp, printheaders, quote, quotefile, doprefix);
1311 if (mtf == NULL)
1312 goto j_leave;
1314 if (options & OPT_INTERACTIVE) {
1315 err = (ok_blook(bsdcompat) || ok_blook(askatend));
1316 if (err == 0)
1317 goto jaskeot;
1318 if (ok_blook(askcc))
1319 ++err, grab_headers(hp, GCC, 1);
1320 if (ok_blook(askbcc))
1321 ++err, grab_headers(hp, GBCC, 1);
1322 if (ok_blook(askattach))
1323 ++err, edit_attachments(&hp->h_attach);
1324 if (ok_blook(asksign))
1325 ++err, dosign = getapproval(_("Sign this message (y/n)? "), TRU1);
1326 if (err == 1) {
1327 jaskeot:
1328 printf(_("EOT\n"));
1329 fflush(stdout);
1333 if (fsize(mtf) == 0) {
1334 if (options & OPT_E_FLAG)
1335 goto jleave;
1336 if (hp->h_subject == NULL)
1337 printf(_("No message, no subject; hope that's ok\n"));
1338 else if (ok_blook(bsdcompat) || ok_blook(bsdmsgs))
1339 printf(_("Null message body; hope that's ok\n"));
1342 if (dosign < 0)
1343 dosign = ok_blook(smime_sign);
1344 #ifndef HAVE_SSL
1345 if (dosign) {
1346 fprintf(stderr, _("No SSL support compiled in.\n"));
1347 goto jleave;
1349 #endif
1351 /* XXX Update time_current again; once collect() offers editing of more
1352 * XXX headers, including Date:, this must only happen if Date: is the
1353 * XXX same that it was before collect() (e.g., postponing etc.).
1354 * XXX But *do* update otherwise because the mail seems to be backdated
1355 * XXX if the user edited some time, which looks odd and it happened
1356 * XXX to me that i got mis-dated response mails due to that... */
1357 time_current_update(&time_current, TRU1);
1359 /* TODO hrmpf; the MIME/send layer rewrite MUST address the init crap:
1360 * TODO setup the header ONCE; note this affects edit.c, collect.c ...,
1361 * TODO but: offer a hook that rebuilds/expands/checks/fixates all
1362 * TODO header fields ONCE, call that ONCE after user editing etc. has
1363 * TODO completed (one edit cycle) */
1365 /* Take the user names from the combined to and cc lists and do all the
1366 * alias processing. The POSIX standard says:
1367 * The names shall be substituted when alias is used as a recipient
1368 * address specified by the user in an outgoing message (that is,
1369 * other recipients addressed indirectly through the reply command
1370 * shall not be substituted in this manner).
1371 * S-nail thus violates POSIX, as has been pointed out correctly by
1372 * Martin Neitzel, but logic and usability of POSIX standards is not seldom
1373 * disputable anyway. Go for user friendliness */
1375 /* Do alias expansion on Reply-To: members, too */
1376 /* TODO puthead() YET (!!! see ONCE note above) expands the value, but
1377 * TODO doesn't perform alias expansion; encapsulate in the ONCE-o */
1378 if (hp->h_replyto == NULL && (cp = ok_vlook(replyto)) != NULL)
1379 hp->h_replyto = checkaddrs(lextract(cp, GEXTRA | GFULL));
1380 if (hp->h_replyto != NULL)
1381 hp->h_replyto = elide(usermap(hp->h_replyto, TRU1));
1383 /* TODO what happens now is that all recipients are merged into
1384 * TODO a duplicated list with expanded aliases, then this list is
1385 * TODO splitted again into the three individual recipient lists (with
1386 * TODO duplicates removed).
1387 * TODO later on we use the merged list for outof() pipe/file saving,
1388 * TODO then we eliminate duplicates (again) and then we use that one
1389 * TODO for mightrecord() and _transfer(), and again. ... Please ... */
1391 /* NOTE: Due to elide() in fixhead(), ENSURE to,cc,bcc order of to!,
1392 * because otherwise the recipients will be "degraded" if they occur
1393 * multiple times */
1394 to = usermap(cat(hp->h_to, cat(hp->h_cc, hp->h_bcc)), FAL0);
1395 if (to == NULL) {
1396 fprintf(stderr, _("No recipients specified\n"));
1397 _sendout_error = TRU1;
1399 to = fixhead(hp, to);
1401 /* */
1402 memset(&sb, 0, sizeof sb);
1403 sb.sb_hp = hp;
1404 sb.sb_to = to;
1405 sb.sb_input = mtf;
1406 if ((dosign || count_nonlocal(to) > 0) &&
1407 !_sendbundle_setup_creds(&sb, (dosign > 0)))
1408 /* TODO saving $DEAD and recovering etc is not yet well defined */
1409 goto jfail_dead;
1411 /* 'Bit ugly kind of control flow until we find a charset that does it */
1412 for (charset_iter_reset(hp->h_charset);; charset_iter_next()) {
1413 if (!charset_iter_is_valid())
1415 else if ((nmtf = infix(hp, mtf)) != NULL)
1416 break;
1417 else if ((err = errno) == EILSEQ || err == EINVAL) {
1418 rewind(mtf);
1419 continue;
1422 perror("");
1423 jfail_dead:
1424 _sendout_error = TRU1;
1425 savedeadletter(mtf, TRU1);
1426 fputs(_("... message not sent.\n"), stderr);
1427 goto jleave;
1429 mtf = nmtf;
1431 /* */
1432 #ifdef HAVE_SSL
1433 if (dosign) {
1434 if ((nmtf = smime_sign(mtf, sb.sb_signer.s)) == NULL)
1435 goto jfail_dead;
1436 Fclose(mtf);
1437 mtf = nmtf;
1439 #endif
1441 /* TODO truly - i still don't get what follows: (1) we deliver file
1442 * TODO and pipe addressees, (2) we mightrecord() and (3) we transfer
1443 * TODO even if (1) savedeadletter() etc. To me this doesn't make sense? */
1445 /* Deliver pipe and file addressees */
1446 to = outof(to, mtf, &_sendout_error);
1447 if (_sendout_error)
1448 savedeadletter(mtf, FAL0);
1450 to = elide(to); /* XXX needed only to drop GDELs due to outof()! */
1451 { ui32_t cnt = count(to);
1452 if ((!recipient_record || cnt > 0) &&
1453 !mightrecord(mtf, (recipient_record ? to : NULL)))
1454 goto jleave;
1455 if (cnt > 0) {
1456 sb.sb_hp = hp;
1457 sb.sb_to = to;
1458 sb.sb_input = mtf;
1459 if (_transfer(&sb))
1460 rv = OKAY;
1461 } else if (!_sendout_error)
1462 rv = OKAY;
1464 jleave:
1465 Fclose(mtf);
1466 j_leave:
1467 if (_sendout_error)
1468 exit_status |= EXIT_SEND_ERROR;
1469 NYD_LEAVE;
1470 return rv;
1473 FL int
1474 mkdate(FILE *fo, char const *field)
1476 struct tm *tmptr;
1477 int tzdiff, tzdiff_hour, tzdiff_min, rv;
1478 NYD_ENTER;
1480 tzdiff = time_current.tc_time - mktime(&time_current.tc_gm);
1481 tzdiff_hour = (int)(tzdiff / 60);
1482 tzdiff_min = tzdiff_hour % 60;
1483 tzdiff_hour /= 60;
1484 tmptr = &time_current.tc_local;
1485 if (tmptr->tm_isdst > 0)
1486 ++tzdiff_hour;
1487 rv = fprintf(fo, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1488 field,
1489 weekday_names[tmptr->tm_wday],
1490 tmptr->tm_mday, month_names[tmptr->tm_mon],
1491 tmptr->tm_year + 1900, tmptr->tm_hour,
1492 tmptr->tm_min, tmptr->tm_sec,
1493 tzdiff_hour * 100 + tzdiff_min);
1494 NYD_LEAVE;
1495 return rv;
1498 FL int
1499 puthead(struct header *hp, FILE *fo, enum gfield w, enum sendaction action,
1500 enum conversion convert, char const *contenttype, char const *charset)
1502 #define FMT_CC_AND_BCC() \
1503 do {\
1504 if (hp->h_cc != NULL && (w & GCC)) {\
1505 if (fmt("Cc:", hp->h_cc, fo, (w & (GCOMMA | GFILES)), 0,\
1506 (action != SEND_TODISP)))\
1507 goto jleave;\
1508 ++gotcha;\
1510 if (hp->h_bcc != NULL && (w & GBCC)) {\
1511 if (fmt("Bcc:", hp->h_bcc, fo, (w & (GCOMMA | GFILES)), 0,\
1512 (action != SEND_TODISP)))\
1513 goto jleave;\
1514 ++gotcha;\
1516 } while (0)
1518 char const *addr;
1519 size_t gotcha, l;
1520 struct name *np, *fromfield = NULL, *senderfield = NULL;
1521 int stealthmua, rv = 1;
1522 bool_t nodisp;
1523 NYD_ENTER;
1525 if ((addr = ok_vlook(stealthmua)) != NULL)
1526 stealthmua = !strcmp(addr, "noagent") ? -1 : 1;
1527 else
1528 stealthmua = 0;
1529 gotcha = 0;
1530 nodisp = (action != SEND_TODISP);
1532 if (w & GDATE)
1533 mkdate(fo, "Date"), ++gotcha;
1534 if (w & GIDENT) {
1535 if (hp->h_from != NULL) {
1536 if (fmt("From:", hp->h_from, fo, (w & (GCOMMA | GFILES)), 0, nodisp))
1537 goto jleave;
1538 ++gotcha;
1539 fromfield = hp->h_from;
1540 } else if ((addr = myaddrs(hp)) != NULL) {
1541 if (_putname(addr, w, action, &gotcha, "From:", fo, &fromfield))
1542 goto jleave;
1543 hp->h_from = fromfield;
1546 if (((addr = hp->h_organization) != NULL ||
1547 (addr = ok_vlook(ORGANIZATION)) != NULL) &&
1548 (l = strlen(addr)) > 0) {
1549 fwrite("Organization: ", sizeof(char), 14, fo);
1550 if (xmime_write(addr, l, fo, (!nodisp ? CONV_NONE : CONV_TOHDR),
1551 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1552 goto jleave;
1553 ++gotcha;
1554 putc('\n', fo);
1557 /* TODO see the ONCE TODO note somewhere around this file;
1558 * TODO but anyway, do NOT perform alias expansion UNLESS
1559 * TODO we are actually sending out! */
1560 if (hp->h_replyto != NULL) {
1561 if (fmt("Reply-To:", hp->h_replyto, fo, w & GCOMMA, 0, nodisp))
1562 goto jleave;
1563 ++gotcha;
1564 } else if ((addr = ok_vlook(replyto)) != NULL)
1565 if (_putname(addr, w, action, &gotcha, "Reply-To:", fo, NULL))
1566 goto jleave;
1568 if (hp->h_sender != NULL) {
1569 if (fmt("Sender:", hp->h_sender, fo, w & GCOMMA, 0, nodisp))
1570 goto jleave;
1571 ++gotcha;
1572 senderfield = hp->h_sender;
1573 } else if ((addr = ok_vlook(sender)) != NULL)
1574 if (_putname(addr, w, action, &gotcha, "Sender:", fo, &senderfield))
1575 goto jleave;
1577 if ((np = UNCONST(check_from_and_sender(fromfield, senderfield))) == NULL)
1578 goto jleave;
1579 if (!_check_dispo_notif(np, hp, fo))
1580 goto jleave;
1583 if (hp->h_to != NULL && w & GTO) {
1584 if (fmt("To:", hp->h_to, fo, (w & (GCOMMA | GFILES)), 0, nodisp))
1585 goto jleave;
1586 ++gotcha;
1589 if (!ok_blook(bsdcompat) && !ok_blook(bsdorder))
1590 FMT_CC_AND_BCC();
1592 if (hp->h_subject != NULL && (w & GSUBJECT)) {
1593 fwrite("Subject: ", sizeof (char), 9, fo);
1594 if (!ascncasecmp(hp->h_subject, "re: ", 4)) {/* TODO localizable */
1595 fwrite("Re: ", sizeof(char), 4, fo);
1596 if (strlen(hp->h_subject + 4) > 0 &&
1597 xmime_write(hp->h_subject + 4, strlen(hp->h_subject + 4), fo,
1598 (!nodisp ? CONV_NONE : CONV_TOHDR),
1599 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1600 goto jleave;
1601 } else if (*hp->h_subject != '\0') {
1602 if (xmime_write(hp->h_subject, strlen(hp->h_subject), fo,
1603 (!nodisp ? CONV_NONE : CONV_TOHDR),
1604 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1605 goto jleave;
1607 ++gotcha;
1608 fwrite("\n", sizeof (char), 1, fo);
1611 if (ok_blook(bsdcompat) || ok_blook(bsdorder))
1612 FMT_CC_AND_BCC();
1614 if ((w & GMSGID) && stealthmua <= 0)
1615 _message_id(fo, hp), ++gotcha;
1617 if ((np = hp->h_ref) != NULL && (w & GREF)) {
1618 fmt("References:", np, fo, 0, 1, 0);
1619 if (np->n_name != NULL) {
1620 while (np->n_flink != NULL)
1621 np = np->n_flink;
1622 if (!is_addr_invalid(np, 0)) {
1623 fprintf(fo, "In-Reply-To: %s\n", np->n_name);
1624 ++gotcha;
1629 if ((w & GUA) && stealthmua == 0)
1630 fprintf(fo, "User-Agent: %s %s\n", uagent, version), ++gotcha;
1632 if (w & GMIME) {
1633 fputs("MIME-Version: 1.0\n", fo), ++gotcha;
1634 if (hp->h_attach != NULL) {
1635 _sendout_boundary = mime_create_boundary();/*TODO carrier*/
1636 fprintf(fo, "Content-Type: multipart/mixed;\n boundary=\"%s\"\n",
1637 _sendout_boundary);
1638 } else {
1639 fprintf(fo, "Content-Type: %s", contenttype);
1640 if (charset != NULL)
1641 fprintf(fo, "; charset=%s", charset);
1642 fprintf(fo, "\nContent-Transfer-Encoding: %s\n",
1643 _get_encoding(convert));
1647 if (gotcha && (w & GNL))
1648 putc('\n', fo);
1649 rv = 0;
1650 jleave:
1651 NYD_LEAVE;
1652 return rv;
1653 #undef FMT_CC_AND_BCC
1656 FL enum okay
1657 resend_msg(struct message *mp, struct name *to, int add_resent) /* TODO check */
1659 struct sendbundle sb;
1660 FILE *ibuf, *nfo, *nfi;
1661 char *tempMail;
1662 enum okay rv = STOP;
1663 NYD_ENTER;
1665 _sendout_error = FAL0;
1667 /* Update some globals we likely need first */
1668 time_current_update(&time_current, TRU1);
1670 if ((to = checkaddrs(to)) == NULL) {
1671 _sendout_error = TRU1;
1672 goto jleave;
1675 if ((nfo = Ftmp(&tempMail, "resend", OF_WRONLY | OF_HOLDSIGS | OF_REGISTER,
1676 0600)) == NULL) {
1677 _sendout_error = TRU1;
1678 perror(_("temporary mail file"));
1679 goto jleave;
1681 if ((nfi = Fopen(tempMail, "r")) == NULL) {
1682 _sendout_error = TRU1;
1683 perror(tempMail);
1685 Ftmp_release(&tempMail);
1686 if (nfi == NULL)
1687 goto jerr_o;
1689 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1690 goto jerr_all;
1692 memset(&sb, 0, sizeof sb);
1693 sb.sb_to = to;
1694 sb.sb_input = nfi;
1695 if (count_nonlocal(to) > 0 && !_sendbundle_setup_creds(&sb, FAL0))
1696 /* TODO saving $DEAD and recovering etc is not yet well defined */
1697 goto jerr_all;
1699 if (infix_resend(ibuf, nfo, mp, to, add_resent) != 0) {
1700 savedeadletter(nfi, TRU1);
1701 fputs(_("... message not sent.\n"), stderr);
1702 jerr_all:
1703 Fclose(nfi);
1704 jerr_o:
1705 Fclose(nfo);
1706 _sendout_error = TRU1;
1707 goto jleave;
1709 Fclose(nfo);
1710 rewind(nfi);
1712 to = outof(to, nfi, &_sendout_error);
1713 if (_sendout_error)
1714 savedeadletter(nfi, FAL0);
1716 to = elide(to); /* TODO should have been done in fixhead()? */
1717 if (count(to) != 0) {
1718 if (!ok_blook(record_resent) || mightrecord(nfi, to)) {
1719 sb.sb_to = to;
1720 /*sb.sb_input = nfi;*/
1721 if (_transfer(&sb))
1722 rv = OKAY;
1724 } else if (!_sendout_error)
1725 rv = OKAY;
1727 Fclose(nfi);
1728 jleave:
1729 if (_sendout_error)
1730 exit_status |= EXIT_SEND_ERROR;
1731 NYD_LEAVE;
1732 return rv;
1735 #undef SEND_LINESIZE
1737 /* vim:set fenc=utf-8:s-it-mode */