send.c: GCC 4.9 warnings
[s-mailx.git] / sendout.c
blob9e0383521c44bf0dfffb686437901cd6972c036f
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 /* Save the outgoing mail on the passed file */
90 static int savemail(char const *name, FILE *fi);
92 /* Send mail to a bunch of user names. The interface is through mail() */
93 static int sendmail_internal(void *v, int recipient_record);
95 /* */
96 static bool_t _transfer(struct sendbundle *sbp);
98 /* Start the MTA mailing */
99 static bool_t start_mta(struct sendbundle *sbp);
101 /* Record outgoing mail if instructed to do so; in *record* unless to is set */
102 static bool_t mightrecord(FILE *fp, struct name *to);
104 /* Create a Message-Id: header field. Use either host name or from address */
105 static void _message_id(FILE *fo, struct header *hp);
107 /* Format the given header line to not exceed 72 characters */
108 static int fmt(char const *str, struct name *np, FILE *fo, int comma,
109 int dropinvalid, int domime);
111 /* Rewrite a message for resending, adding the Resent-Headers */
112 static int infix_resend(FILE *fi, FILE *fo, struct message *mp,
113 struct name *to, int add_resent);
115 static enum okay
116 _putname(char const *line, enum gfield w, enum sendaction action,
117 size_t *gotcha, char const *prefix, FILE *fo, struct name **xp)
119 struct name *np;
120 enum okay rv = STOP;
121 NYD_ENTER;
123 np = lextract(line, GEXTRA | GFULL);
124 if (xp != NULL)
125 *xp = np;
126 if (np == NULL)
128 else if (fmt(prefix, np, fo, w & GCOMMA, 0, (action != SEND_TODISP)))
129 rv = OKAY;
130 else if (gotcha != NULL)
131 ++(*gotcha);
132 NYD_LEAVE;
133 return rv;
136 static char const *
137 _get_encoding(enum conversion const convert)
139 char const *rv;
140 NYD_ENTER;
142 switch (convert) {
143 case CONV_7BIT: rv = "7bit"; break;
144 case CONV_8BIT: rv = "8bit"; break;
145 case CONV_TOQP: rv = "quoted-printable"; break;
146 case CONV_TOB64: rv = "base64"; break;
147 default: rv = NULL; break;
149 NYD_LEAVE;
150 return rv;
153 static int
154 _attach_file(struct attachment *ap, FILE *fo)
156 /* TODO of course, the MIME classification needs to performed once
157 * TODO only, not for each and every charset anew ... ;-// */
158 char *charset_iter_orig[2];
159 long offs;
160 int err = 0;
161 NYD_ENTER;
163 /* Is this already in target charset? Simply copy over */
164 if (ap->a_conv == AC_TMPFILE) {
165 err = __attach_file(ap, fo);
166 Fclose(ap->a_tmpf);
167 DBG( ap->a_tmpf = NULL; )
168 goto jleave;
171 /* If we don't apply charset conversion at all (fixed input=ouput charset)
172 * we also simply copy over, since it's the users desire */
173 if (ap->a_conv == AC_FIX_INCS) {
174 ap->a_charset = ap->a_input_charset;
175 err = __attach_file(ap, fo);
176 goto jleave;
179 /* Otherwise we need to iterate over all possible output charsets */
180 if ((offs = ftell(fo)) == -1) {
181 err = EIO;
182 goto jleave;
184 charset_iter_recurse(charset_iter_orig);
185 for (charset_iter_reset(NULL);; charset_iter_next()) {
186 if (!charset_iter_is_valid()) {
187 err = EILSEQ;
188 break;
190 err = __attach_file(ap, fo);
191 if (err == 0 || (err != EILSEQ && err != EINVAL))
192 break;
193 clearerr(fo);
194 if (fseek(fo, offs, SEEK_SET) == -1) {
195 err = EIO;
196 break;
198 if (ap->a_conv != AC_DEFAULT) {
199 err = EILSEQ;
200 break;
202 ap->a_charset = NULL;
204 charset_iter_restore(charset_iter_orig);
205 jleave:
206 NYD_LEAVE;
207 return err;
210 static int
211 __attach_file(struct attachment *ap, FILE *fo) /* XXX linelength */
213 int err = 0, do_iconv;
214 FILE *fi;
215 char const *charset;
216 enum conversion convert;
217 char *buf;
218 size_t bufsize, lncnt, inlen;
219 NYD_ENTER;
221 /* Either charset-converted temporary file, or plain path */
222 if (ap->a_conv == AC_TMPFILE) {
223 fi = ap->a_tmpf;
224 assert(ftell(fi) == 0);
225 } else if ((fi = Fopen(ap->a_name, "r")) == NULL) {
226 err = errno;
227 perror(ap->a_name);
228 goto jleave;
231 /* MIME part header for attachment */
232 { char const *bn = ap->a_name, *ct;
234 if ((ct = strrchr(bn, '/')) != NULL)
235 bn = ++ct;
236 ct = ap->a_content_type;
237 charset = ap->a_charset;
238 convert = mime_classify_file(fi, (char const**)&ct, &charset, &do_iconv);
239 if (charset == NULL || ap->a_conv == AC_FIX_INCS ||
240 ap->a_conv == AC_TMPFILE)
241 do_iconv = 0;
243 if (fprintf(fo, "\n--%s\nContent-Type: %s", _sendout_boundary, ct) == -1)
244 goto jerr_header;
246 if (charset == NULL) {
247 if (putc('\n', fo) == EOF)
248 goto jerr_header;
249 } else if (fprintf(fo, "; charset=%s\n", charset) == -1)
250 goto jerr_header;
252 if (fprintf(fo, "Content-Transfer-Encoding: %s\n"
253 "Content-Disposition: %s;\n filename=\"",
254 _get_encoding(convert), ap->a_content_disposition) == -1)
255 goto jerr_header;
256 if (xmime_write(bn, strlen(bn), fo, CONV_TOHDR, TD_NONE, NULL) < 0)
257 goto jerr_header;
258 if (fwrite("\"\n", sizeof(char), 2, fo) != 2 * sizeof(char))
259 goto jerr_header;
261 if ((bn = ap->a_content_id) != NULL &&
262 fprintf(fo, "Content-ID: %s\n", bn) == -1)
263 goto jerr_header;
265 if ((bn = ap->a_content_description) != NULL &&
266 fprintf(fo, "Content-Description: %s\n", bn) == -1)
267 goto jerr_header;
269 if (putc('\n', fo) == EOF) {
270 jerr_header:
271 err = errno;
272 goto jerr_fclose;
276 #ifdef HAVE_ICONV
277 if (iconvd != (iconv_t)-1)
278 n_iconv_close(iconvd);
279 if (do_iconv) {
280 char const *tcs = charset_get_lc();
281 if (asccasecmp(charset, tcs) &&
282 (iconvd = n_iconv_open(charset, tcs)) == (iconv_t)-1 &&
283 (err = errno) != 0) {
284 if (err == EINVAL)
285 fprintf(stderr, tr(179, "Cannot convert from %s to %s\n"),
286 tcs, charset);
287 else
288 perror("iconv_open");
289 goto jerr_fclose;
292 #endif
294 bufsize = SEND_LINESIZE;
295 buf = smalloc(bufsize);
296 if (convert == CONV_TOQP
297 #ifdef HAVE_ICONV
298 || iconvd != (iconv_t)-1
299 #endif
301 lncnt = fsize(fi);
302 for (;;) {
303 if (convert == CONV_TOQP
304 #ifdef HAVE_ICONV
305 || iconvd != (iconv_t)-1
306 #endif
308 if (fgetline(&buf, &bufsize, &lncnt, &inlen, fi, 0) == NULL)
309 break;
310 } else if ((inlen = fread(buf, sizeof *buf, bufsize, fi)) == 0)
311 break;
312 if (xmime_write(buf, inlen, fo, convert, TD_ICONV, NULL) < 0) {
313 err = errno;
314 goto jerr;
317 if (ferror(fi))
318 err = EDOM;
319 jerr:
320 free(buf);
321 jerr_fclose:
322 if (ap->a_conv != AC_TMPFILE)
323 Fclose(fi);
324 jleave:
325 NYD_LEAVE;
326 return err;
329 static bool_t
330 _sendbundle_setup_creds(struct sendbundle *sbp, bool_t signing_caps)
332 bool_t v15, rv = FAL0;
333 char *shost, *from, *smtp;
334 NYD_ENTER;
336 v15 = ok_blook(v15_compat);
337 shost = (v15 ? ok_vlook(smtp_hostname) : NULL);
338 from = ((signing_caps || !v15 || shost == NULL)
339 ? skin(myorigin(sbp->sb_hp)) : NULL);
341 if (signing_caps) {
342 if (from == NULL) {
343 #ifdef HAVE_SSL
344 fprintf(stderr, tr(531, "No *from* address for signing specified\n"));
345 goto jleave;
346 #endif
347 } else
348 sbp->sb_signer.l = strlen(sbp->sb_signer.s = from);
351 if ((smtp = ok_vlook(smtp)) == NULL) {
352 rv = TRU1;
353 goto jleave;
356 if (!url_parse(&sbp->sb_url, CPROTO_SMTP, smtp))
357 goto jleave;
359 if (v15) {
360 if (shost == NULL) {
361 assert(from != NULL);
362 sbp->sb_url.url_uh.l = strlen(sbp->sb_url.url_uh.s = from);
364 else
365 __sendout_ident = sbp->sb_url.url_uh.s;
366 if (!ccred_lookup(&sbp->sb_ccred, &sbp->sb_url))
367 goto jleave;
368 } else {
369 if (sbp->sb_url.url_had_user || sbp->sb_url.url_pass.s != NULL) {
370 fprintf(stderr, "New-style URL used without *v15-compat* being set\n");
371 goto jleave;
373 if (!ccred_lookup_old(&sbp->sb_ccred, CPROTO_SMTP, smtp))
374 goto jleave;
375 assert(from != NULL);
376 sbp->sb_url.url_uh.l = strlen(sbp->sb_url.url_uh.s = from);
379 rv = TRU1;
380 jleave:
381 NYD_LEAVE;
382 return rv;
385 static char const **
386 _prepare_mta_args(struct name *to, struct header *hp)
388 size_t j, i;
389 char const **args;
390 NYD_ENTER;
392 i = 4 + smopts_count + 2 + count(to) + 1;
393 args = salloc(i * sizeof(char*));
395 args[0] = ok_vlook(sendmail_progname);
396 if (args[0] == NULL || *args[0] == '\0')
397 args[0] = SENDMAIL_PROGNAME;
399 args[1] = "-i";
400 i = 2;
401 if (ok_blook(metoo))
402 args[i++] = "-m";
403 if (options & OPT_VERBOSE)
404 args[i++] = "-v";
406 for (j = 0; j < smopts_count; ++j, ++i)
407 args[i] = smopts[j];
409 /* -r option? We may only pass skinned addresses */
410 if (options & OPT_r_FLAG) {
411 char const *froma;
413 if (option_r_arg[0] != '\0')
414 froma = option_r_arg;
415 else if (hp != NULL) {
416 /* puthead() did it, then */
417 assert(hp->h_from != NULL);
418 froma = hp->h_from->n_name;
419 } else
420 froma = skin(myorigin(NULL)); /* XXX ugh! ugh!! */
421 if (froma != NULL) { /* XXX ugh! */
422 args[i++] = "-r";
423 args[i++] = froma;
427 /* Receivers follow */
428 for (; to != NULL; to = to->n_flink)
429 if (!(to->n_type & GDEL))
430 args[i++] = to->n_name;
431 args[i] = NULL;
432 NYD_LEAVE;
433 return args;
436 static struct name *
437 fixhead(struct header *hp, struct name *tolist)
439 struct name **npp, *np;
440 NYD_ENTER;
442 tolist = elide(tolist);
444 hp->h_to = hp->h_cc = hp->h_bcc = NULL;
445 for (np = tolist; np != NULL; np = np->n_flink) {
446 switch (np->n_type & (GDEL | GMASK)) {
447 case GTO: npp = &hp->h_to; break;
448 case GCC: npp = &hp->h_cc; break;
449 case GBCC: npp = &hp->h_bcc; break;
450 default: continue;
452 *npp = cat(*npp, ndup(np, np->n_type | GFULL));
454 NYD_LEAVE;
455 return tolist;
458 static int
459 put_signature(FILE *fo, int convert)
461 char buf[SEND_LINESIZE], *sig, c = '\n';
462 FILE *fsig;
463 size_t sz;
464 int rv;
465 NYD_ENTER;
467 if ((sig = ok_vlook(signature)) == NULL || *sig == '\0') {
468 rv = 0;
469 goto jleave;
471 rv = -1;
473 if ((sig = file_expand(sig)) == NULL)
474 goto jleave;
476 if ((fsig = Fopen(sig, "r")) == NULL) {
477 perror(sig);
478 goto jleave;
480 while ((sz = fread(buf, sizeof *buf, SEND_LINESIZE, fsig)) != 0) {
481 c = buf[sz - 1];
482 if (xmime_write(buf, sz, fo, convert, TD_NONE, NULL) < 0)
483 goto jerr;
485 if (ferror(fsig)) {
486 jerr:
487 perror(sig);
488 Fclose(fsig);
489 goto jleave;
491 Fclose(fsig);
492 if (c != '\n')
493 putc('\n', fo);
495 rv = 0;
496 jleave:
497 NYD_LEAVE;
498 return rv;
501 static int
502 attach_message(struct attachment *ap, FILE *fo)
504 struct message *mp;
505 char const *ccp;
506 int rv;
507 NYD_ENTER;
509 fprintf(fo, "\n--%s\nContent-Type: message/rfc822\n"
510 "Content-Disposition: inline\n", _sendout_boundary);
511 if ((ccp = ap->a_content_description) != NULL)
512 fprintf(fo, "Content-Description: %s\n", ccp);
513 fputc('\n', fo);
515 mp = message + ap->a_msgno - 1;
516 touch(mp);
517 rv = (sendmp(mp, fo, 0, NULL, SEND_RFC822, NULL) < 0) ? -1 : 0;
518 NYD_LEAVE;
519 return rv;
522 static int
523 make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
524 char const *contenttype, char const *charset)
526 struct attachment *att;
527 int rv = -1;
528 NYD_ENTER;
530 fputs("This is a multi-part message in MIME format.\n", fo);
531 if (fsize(fi) != 0) {
532 char *buf;
533 size_t sz, bufsize, cnt;
535 fprintf(fo, "\n--%s\n", _sendout_boundary);
536 fprintf(fo, "Content-Type: %s", contenttype);
537 if (charset != NULL)
538 fprintf(fo, "; charset=%s", charset);
539 fprintf(fo, "\nContent-Transfer-Encoding: %s\n"
540 "Content-Disposition: inline\n\n", _get_encoding(convert));
542 buf = smalloc(bufsize = SEND_LINESIZE);
543 if (convert == CONV_TOQP
544 #ifdef HAVE_ICONV
545 || iconvd != (iconv_t)-1
546 #endif
548 fflush(fi);
549 cnt = fsize(fi);
551 for (;;) {
552 if (convert == CONV_TOQP
553 #ifdef HAVE_ICONV
554 || iconvd != (iconv_t)-1
555 #endif
557 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
558 break;
559 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
560 break;
562 if (xmime_write(buf, sz, fo, convert, TD_ICONV, NULL) < 0) {
563 free(buf);
564 goto jleave;
567 free(buf);
569 if (ferror(fi))
570 goto jleave;
571 if (charset != NULL)
572 put_signature(fo, convert);
575 for (att = hp->h_attach; att != NULL; att = att->a_flink) {
576 if (att->a_msgno) {
577 if (attach_message(att, fo) != 0)
578 goto jleave;
579 } else if (_attach_file(att, fo) != 0)
580 goto jleave;
583 /* the final boundary with two attached dashes */
584 fprintf(fo, "\n--%s--\n", _sendout_boundary);
585 rv = 0;
586 jleave:
587 NYD_LEAVE;
588 return rv;
591 static FILE *
592 infix(struct header *hp, FILE *fi) /* TODO check */
594 FILE *nfo, *nfi = NULL;
595 char *tempMail;
596 char const *contenttype, *charset = NULL;
597 enum conversion convert;
598 int do_iconv = 0, err;
599 #ifdef HAVE_ICONV
600 char const *tcs, *convhdr = NULL;
601 #endif
602 NYD_ENTER;
604 if ((nfo = Ftmp(&tempMail, "infix", OF_WRONLY | OF_HOLDSIGS | OF_REGISTER,
605 0600)) == NULL) {
606 perror(tr(178, "temporary mail file"));
607 goto jleave;
609 if ((nfi = Fopen(tempMail, "r")) == NULL) {
610 perror(tempMail);
611 Fclose(nfo);
613 Ftmp_release(&tempMail);
614 if (nfi == NULL)
615 goto jleave;
617 contenttype = "text/plain"; /* XXX mail body - always text/plain, want XX? */
618 convert = mime_classify_file(fi, &contenttype, &charset, &do_iconv);
620 #ifdef HAVE_ICONV
621 tcs = charset_get_lc();
622 if ((convhdr = need_hdrconv(hp, GTO | GSUBJECT | GCC | GBCC | GIDENT))) {
623 if (iconvd != (iconv_t)-1) /* XXX */
624 n_iconv_close(iconvd);
625 if (asccasecmp(convhdr, tcs) != 0 &&
626 (iconvd = n_iconv_open(convhdr, tcs)) == (iconv_t)-1 &&
627 (err = errno) != 0)
628 goto jiconv_err;
630 #endif
631 if (puthead(hp, nfo,
632 (GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA | GUA | GMIME | GMSGID |
633 GIDENT | GREF | GDATE), SEND_MBOX, convert, contenttype, charset))
634 goto jerr;
635 #ifdef HAVE_ICONV
636 if (iconvd != (iconv_t)-1)
637 n_iconv_close(iconvd);
638 #endif
640 #ifdef HAVE_ICONV
641 if (do_iconv && charset != NULL) { /*TODO charset->mime_classify_file*/
642 if (asccasecmp(charset, tcs) != 0 &&
643 (iconvd = n_iconv_open(charset, tcs)) == (iconv_t)-1 &&
644 (err = errno) != 0) {
645 jiconv_err:
646 if (err == EINVAL)
647 fprintf(stderr, tr(179, "Cannot convert from %s to %s\n"),
648 tcs, charset);
649 else
650 perror("iconv_open");
651 goto jerr;
654 #endif
656 if (hp->h_attach != NULL) {
657 if (make_multipart(hp, convert, fi, nfo, contenttype, charset) != 0)
658 goto jerr;
659 } else {
660 size_t sz, bufsize, cnt;
661 char *buf;
663 if (convert == CONV_TOQP
664 #ifdef HAVE_ICONV
665 || iconvd != (iconv_t)-1
666 #endif
668 fflush(fi);
669 cnt = fsize(fi);
671 buf = smalloc(bufsize = SEND_LINESIZE);
672 for (err = 0;;) {
673 if (convert == CONV_TOQP
674 #ifdef HAVE_ICONV
675 || iconvd != (iconv_t)-1
676 #endif
678 if (fgetline(&buf, &bufsize, &cnt, &sz, fi, 0) == NULL)
679 break;
680 } else if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
681 break;
682 if (xmime_write(buf, sz, nfo, convert, TD_ICONV, NULL) < 0) {
683 err = 1;
684 break;
687 free(buf);
689 if (err || ferror(fi)) {
690 jerr:
691 Fclose(nfo);
692 Fclose(nfi);
693 #ifdef HAVE_ICONV
694 if (iconvd != (iconv_t)-1)
695 n_iconv_close(iconvd);
696 #endif
697 nfi = NULL;
698 goto jleave;
700 if (charset != NULL)
701 put_signature(nfo, convert); /* XXX if (text/) !! */
704 #ifdef HAVE_ICONV
705 if (iconvd != (iconv_t)-1)
706 n_iconv_close(iconvd);
707 #endif
709 fflush(nfo);
710 if ((err = ferror(nfo)))
711 perror(tr(180, "temporary mail file"));
712 Fclose(nfo);
713 if (!err) {
714 fflush_rewind(nfi);
715 Fclose(fi);
716 } else {
717 Fclose(nfi);
718 nfi = NULL;
720 jleave:
721 NYD_LEAVE;
722 return nfi;
725 static int
726 savemail(char const *name, FILE *fi)
728 FILE *fo;
729 char *buf;
730 size_t bufsize, buflen, cnt;
731 int prependnl = 0, rv = -1;
732 NYD_ENTER;
734 buf = smalloc(bufsize = LINESIZE);
736 if ((fo = Zopen(name, "a+", NULL)) == NULL) {
737 if ((fo = Zopen(name, "wx", NULL)) == NULL) {
738 perror(name);
739 goto jleave;
741 } else {
742 if (fseek(fo, -2L, SEEK_END) == 0) {
743 switch (fread(buf, sizeof *buf, 2, fo)) {
744 case 2:
745 if (buf[1] != '\n') {
746 prependnl = 1;
747 break;
749 /* FALLTHRU */
750 case 1:
751 if (buf[0] != '\n')
752 prependnl = 1;
753 break;
754 default:
755 if (ferror(fo)) {
756 perror(name);
757 goto jleave;
760 if (prependnl) {
761 putc('\n', fo);
763 fflush(fo);
767 fprintf(fo, "From %s %s", myname, time_current.tc_ctime);
768 fflush_rewind(fi);
769 cnt = fsize(fi);
770 buflen = 0;
771 while (fgetline(&buf, &bufsize, &cnt, &buflen, fi, 0) != NULL) {
772 #ifdef HAVE_DEBUG /* TODO assert legacy */
773 assert(!is_head(buf, buflen));
774 #else
775 if (is_head(buf, buflen))
776 putc('>', fo);
777 #endif
778 fwrite(buf, sizeof *buf, buflen, fo);
780 if (buflen && *(buf + buflen - 1) != '\n')
781 putc('\n', fo);
782 putc('\n', fo);
783 fflush(fo);
785 rv = 0;
786 if (ferror(fo)) {
787 perror(name);
788 rv = -1;
790 if (Fclose(fo) != 0)
791 rv = -1;
792 fflush_rewind(fi);
793 jleave:
794 free(buf);
795 NYD_LEAVE;
796 return rv;
799 static int
800 sendmail_internal(void *v, int recipient_record)
802 struct header head;
803 char *str = v;
804 int rv;
805 NYD_ENTER;
807 memset(&head, 0, sizeof head);
808 head.h_to = lextract(str, GTO | GFULL);
809 rv = mail1(&head, 0, NULL, NULL, recipient_record, 0);
810 NYD_LEAVE;
811 return rv;
814 static bool_t
815 _transfer(struct sendbundle *sbp)
817 struct name *np;
818 ui32_t cnt;
819 bool_t rv = TRU1;
820 NYD_ENTER;
822 for (cnt = 0, np = sbp->sb_to; np != NULL;) {
823 char const k[] = "smime-encrypt-";
824 size_t nl = strlen(np->n_name);
825 char *cp, *vs = ac_alloc(sizeof(k)-1 + nl +1);
826 memcpy(vs, k, sizeof(k) -1);
827 memcpy(vs + sizeof(k) -1, np->n_name, nl +1);
829 if ((cp = vok_vlook(vs)) != NULL) {
830 #ifdef HAVE_SSL
831 FILE *ef;
833 if ((ef = smime_encrypt(sbp->sb_input, cp, np->n_name)) != NULL) {
834 FILE *fisave = sbp->sb_input;
835 struct name *nsave = sbp->sb_to;
837 sbp->sb_to = ndup(np, np->n_type & ~(GFULL | GSKIN));
838 sbp->sb_input = ef;
839 if (!start_mta(sbp))
840 rv = FAL0;
841 sbp->sb_to = nsave;
842 sbp->sb_input = fisave;
844 Fclose(ef);
845 } else {
846 #else
847 fprintf(stderr, tr(225, "No SSL support compiled in.\n"));
848 rv = FAL0;
849 #endif
850 fprintf(stderr, tr(38, "Message not sent to <%s>\n"), np->n_name);
851 _sendout_error = TRU1;
852 #ifdef HAVE_SSL
854 #endif
855 rewind(sbp->sb_input);
857 if (np->n_flink != NULL)
858 np->n_flink->n_blink = np->n_blink;
859 if (np->n_blink != NULL)
860 np->n_blink->n_flink = np->n_flink;
861 if (np == sbp->sb_to)
862 sbp->sb_to = np->n_flink;
863 np = np->n_flink;
864 } else {
865 ++cnt;
866 np = np->n_flink;
868 ac_free(vs);
871 if (cnt > 0 && (ok_blook(smime_force_encryption) || !start_mta(sbp)))
872 rv = FAL0;
873 NYD_LEAVE;
874 return rv;
877 static bool_t
878 start_mta(struct sendbundle *sbp)
880 char const **args = NULL, **t, *mta;
881 char *smtp;
882 pid_t pid;
883 sigset_t nset;
884 bool_t rv = FAL0;
885 NYD_ENTER;
887 if ((smtp = ok_vlook(smtp)) == NULL) {
888 if ((mta = ok_vlook(sendmail)) != NULL) {
889 if ((mta = file_expand(mta)) == NULL)
890 goto jstop;
891 } else
892 mta = SENDMAIL;
894 args = _prepare_mta_args(sbp->sb_to, sbp->sb_hp);
895 if (options & OPT_DEBUG) {
896 printf(tr(181, "Sendmail arguments:"));
897 for (t = args; *t != NULL; ++t)
898 printf(" \"%s\"", *t);
899 printf("\n");
900 rv = TRU1;
901 goto jleave;
903 } else {
904 mta = NULL; /* Silence cc */
905 #ifndef HAVE_SMTP
906 fputs(tr(194, "No SMTP support compiled in.\n"), stderr);
907 goto jstop;
908 #else
909 /* XXX assert that sendbundle is setup? */
910 #endif
913 /* Fork, set up the temporary mail file as standard input for "mail", and
914 * exec with the user list we generated far above */
915 if ((pid = fork()) == -1) {
916 perror("fork");
917 jstop:
918 savedeadletter(sbp->sb_input, 0);
919 _sendout_error = TRU1;
920 goto jleave;
922 if (pid == 0) {
923 sigemptyset(&nset);
924 sigaddset(&nset, SIGHUP);
925 sigaddset(&nset, SIGINT);
926 sigaddset(&nset, SIGQUIT);
927 sigaddset(&nset, SIGTSTP);
928 sigaddset(&nset, SIGTTIN);
929 sigaddset(&nset, SIGTTOU);
930 freopen("/dev/null", "r", stdin);
931 #ifdef HAVE_SMTP
932 if (smtp != NULL) {
933 prepare_child(&nset, 0, 1);
934 if (smtp_mta(sbp))
935 _exit(0);
936 } else {
937 #endif
938 prepare_child(&nset, fileno(sbp->sb_input), -1);
939 /* If *record* is set then savemail() will move the file position;
940 * it'll call rewind(), but that may optimize away the systemcall if
941 * possible, and since dup2() shares the position with the original FD
942 * the MTA may end up reading nothing */
943 lseek(0, 0, SEEK_SET);
944 execv(mta, UNCONST(args));
945 perror(mta);
946 #ifdef HAVE_SMTP
948 #endif
949 savedeadletter(sbp->sb_input, 1);
950 fputs(tr(182, "... message not sent.\n"), stderr);
951 _exit(1);
953 if ((options & (OPT_DEBUG | OPT_VERBOSE | OPT_BATCH_FLAG)) ||
954 ok_blook(sendwait)) {
955 if (wait_child(pid, NULL))
956 rv = TRU1;
957 else
958 _sendout_error = TRU1;
959 } else {
960 rv = TRU1;
961 free_child(pid);
963 jleave:
964 NYD_LEAVE;
965 return rv;
968 static bool_t
969 mightrecord(FILE *fp, struct name *to)
971 char *cp, *cq;
972 char const *ep;
973 bool_t rv = TRU1;
974 NYD_ENTER;
976 if (to != NULL) {
977 cp = savestr(skinned_name(to));
978 for (cq = cp; *cq != '\0' && *cq != '@'; ++cq)
980 *cq = '\0';
981 } else
982 cp = ok_vlook(record);
984 if (cp != NULL) {
985 if ((ep = expand(cp)) == NULL) {
986 ep = "NULL";
987 goto jbail;
990 if (*ep != '/' && *ep != '+' && ok_blook(outfolder) &&
991 which_protocol(ep) == PROTO_FILE) {
992 size_t i = strlen(cp);
993 cq = salloc(i + 1 +1);
994 cq[0] = '+';
995 memcpy(cq + 1, cp, i +1);
996 cp = cq;
997 if ((ep = file_expand(cp)) == NULL) {
998 ep = "NULL";
999 goto jbail;
1003 if (savemail(ep, fp) != 0) {
1004 jbail:
1005 fprintf(stderr, tr(285, "Failed to save message in %s - "
1006 "message not sent\n"), ep);
1007 exit_status |= EXIT_ERR;
1008 savedeadletter(fp, 1);
1009 rv = FAL0;
1012 NYD_LEAVE;
1013 return rv;
1016 static void
1017 _message_id(FILE *fo, struct header *hp)
1019 char const *h;
1020 size_t rl;
1021 struct tm *tmp;
1022 NYD_ENTER;
1024 if (ok_blook(message_id_disable))
1025 goto jleave;
1027 if ((h = __sendout_ident) != NULL)
1028 rl = 8;
1029 else if ((h = ok_vlook(hostname)) != NULL)
1030 rl = 16;
1031 else if ((h = skin(myorigin(hp))) != NULL && strchr(h, '@') != NULL)
1032 rl = 8;
1033 else
1034 /* Up to MTA */
1035 goto jleave;
1037 tmp = &time_current.tc_gm;
1038 fprintf(fo, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
1039 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
1040 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
1041 getrandstring(rl), (rl == 8 ? '%' : '@'), h);
1042 jleave:
1043 __sendout_ident = NULL;
1044 NYD_LEAVE;
1047 static int
1048 fmt(char const *str, struct name *np, FILE *fo, int flags, int dropinvalid,
1049 int domime)
1051 enum {
1052 m_INIT = 1<<0,
1053 m_COMMA = 1<<1,
1054 m_NOPF = 1<<2,
1055 m_CSEEN = 1<<3
1056 } m = (flags & GCOMMA) ? m_COMMA : 0;
1057 ssize_t col, len;
1058 int rv = 1;
1059 NYD_ENTER;
1061 col = strlen(str);
1062 if (col) {
1063 fwrite(str, sizeof *str, col, fo);
1064 if (flags & GFILES)
1065 goto jstep;
1066 if (col == 9 && !asccasecmp(str, "reply-to:")) {
1067 m |= m_NOPF;
1068 goto jstep;
1070 if (ok_blook(add_file_recipients))
1071 goto jstep;
1072 if ((col == 3 && (!asccasecmp(str, "to:") || !asccasecmp(str, "cc:"))) ||
1073 (col == 4 && !asccasecmp(str, "bcc:")) ||
1074 (col == 10 && !asccasecmp(str, "Resent-To:")))
1075 m |= m_NOPF;
1077 jstep:
1078 for (; np != NULL; np = np->n_flink) {
1079 if ((m & m_NOPF) && is_fileorpipe_addr(np))
1080 continue;
1081 if (is_addr_invalid(np, !dropinvalid)) {
1082 if (dropinvalid)
1083 continue;
1084 else
1085 goto jleave;
1087 if ((m & (m_INIT | m_COMMA)) == (m_INIT | m_COMMA)) {
1088 putc(',', fo);
1089 m |= m_CSEEN;
1090 ++col;
1092 len = strlen(np->n_fullname);
1093 ++col; /* The separating space */
1094 if ((m & m_INIT) && col > 1 && col + len > 72) {
1095 fputs("\n ", fo);
1096 col = 1;
1097 m &= ~m_CSEEN;
1098 } else
1099 putc(' ', fo);
1100 m = (m & ~m_CSEEN) | m_INIT;
1101 len = xmime_write(np->n_fullname, len, fo,
1102 (domime ? CONV_TOHDR_A : CONV_NONE), TD_ICONV, NULL);
1103 if (len < 0)
1104 goto jleave;
1105 col += len;
1107 putc('\n', fo);
1108 rv = 0;
1109 jleave:
1110 NYD_LEAVE;
1111 return rv;
1114 static int
1115 infix_resend(FILE *fi, FILE *fo, struct message *mp, struct name *to,
1116 int add_resent)
1118 size_t cnt, c, bufsize = 0;
1119 char *buf = NULL;
1120 char const *cp;
1121 struct name *fromfield = NULL, *senderfield = NULL;
1122 int rv = 1;
1123 NYD_ENTER;
1125 cnt = mp->m_size;
1127 /* Write the Resent-Fields */
1128 if (add_resent) {
1129 fputs("Resent-", fo);
1130 mkdate(fo, "Date");
1131 if ((cp = myaddrs(NULL)) != NULL) {
1132 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-From:", fo,
1133 &fromfield))
1134 goto jleave;
1136 if ((cp = ok_vlook(sender)) != NULL) {
1137 if (_putname(cp, GCOMMA, SEND_MBOX, NULL, "Resent-Sender:", fo,
1138 &senderfield))
1139 goto jleave;
1141 if (fmt("Resent-To:", to, fo, 1, 1, 0))
1142 goto jleave;
1143 if ((cp = ok_vlook(stealthmua)) == NULL || !strcmp(cp, "noagent")) {
1144 fputs("Resent-", fo);
1145 _message_id(fo, NULL);
1148 if (check_from_and_sender(fromfield, senderfield))
1149 goto jleave;
1151 /* Write the original headers */
1152 while (cnt > 0) {
1153 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1154 break;
1155 /* XXX more checks: The From_ line may be seen when resending */
1156 /* During headers is_head() is actually overkill, so ^From_ is sufficient
1157 * && !is_head(buf, c) */
1158 if (ascncasecmp("status: ", buf, 8) && strncmp("From ", buf, 5))
1159 fwrite(buf, sizeof *buf, c, fo);
1160 if (cnt > 0 && *buf == '\n')
1161 break;
1164 /* Write the message body */
1165 while (cnt > 0) {
1166 if (fgetline(&buf, &bufsize, &cnt, &c, fi, 0) == NULL)
1167 break;
1168 if (cnt == 0 && *buf == '\n')
1169 break;
1170 fwrite(buf, sizeof *buf, c, fo);
1172 if (buf != NULL)
1173 free(buf);
1174 if (ferror(fo)) {
1175 perror(tr(188, "temporary mail file"));
1176 goto jleave;
1178 rv = 0;
1179 jleave:
1180 NYD_LEAVE;
1181 return rv;
1184 FL int
1185 mail(struct name *to, struct name *cc, struct name *bcc, char *subject,
1186 struct attachment *attach, char *quotefile, int recipient_record)
1188 struct header head;
1189 struct str in, out;
1190 NYD_ENTER;
1192 memset(&head, 0, sizeof head);
1194 /* The given subject may be in RFC1522 format. */
1195 if (subject != NULL) {
1196 in.s = subject;
1197 in.l = strlen(subject);
1198 mime_fromhdr(&in, &out, /* TODO ??? TD_ISPR |*/ TD_ICONV);
1199 head.h_subject = out.s;
1201 if (!(options & OPT_t_FLAG)) {
1202 head.h_to = to;
1203 head.h_cc = cc;
1204 head.h_bcc = bcc;
1206 head.h_attach = attach;
1208 mail1(&head, 0, NULL, quotefile, recipient_record, 0);
1210 if (subject != NULL)
1211 free(out.s);
1212 NYD_LEAVE;
1213 return 0;
1216 FL int
1217 c_sendmail(void *v)
1219 int rv;
1220 NYD_ENTER;
1222 rv = sendmail_internal(v, 0);
1223 NYD_LEAVE;
1224 return rv;
1227 FL int
1228 c_Sendmail(void *v)
1230 int rv;
1231 NYD_ENTER;
1233 rv = sendmail_internal(v, 1);
1234 NYD_LEAVE;
1235 return rv;
1238 FL enum okay
1239 mail1(struct header *hp, int printheaders, struct message *quote,
1240 char *quotefile, int recipient_record, int doprefix)
1242 struct sendbundle sb;
1243 struct name *to;
1244 FILE *mtf, *nmtf;
1245 int dosign = -1, err;
1246 char const *cp;
1247 enum okay rv = STOP;
1248 NYD_ENTER;
1250 _sendout_error = FAL0;
1252 /* Update some globals we likely need first */
1253 time_current_update(&time_current, TRU1);
1255 /* */
1256 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1257 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL)));
1258 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1259 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL)));
1261 /* Collect user's mail from standard input. Get the result as mtf */
1262 mtf = collect(hp, printheaders, quote, quotefile, doprefix);
1263 if (mtf == NULL)
1264 goto j_leave;
1266 if (options & OPT_INTERACTIVE) {
1267 err = (ok_blook(bsdcompat) || ok_blook(askatend));
1268 if (err == 0)
1269 goto jaskeot;
1270 if (ok_blook(askcc))
1271 ++err, grab_headers(hp, GCC, 1);
1272 if (ok_blook(askbcc))
1273 ++err, grab_headers(hp, GBCC, 1);
1274 if (ok_blook(askattach))
1275 ++err, edit_attachments(&hp->h_attach);
1276 if (ok_blook(asksign))
1277 ++err, dosign = getapproval(tr(35, "Sign this message (y/n)? "), TRU1);
1278 if (err == 1) {
1279 jaskeot:
1280 printf(tr(183, "EOT\n"));
1281 fflush(stdout);
1285 if (fsize(mtf) == 0) {
1286 if (options & OPT_E_FLAG)
1287 goto jleave;
1288 if (hp->h_subject == NULL)
1289 printf(tr(184, "No message, no subject; hope that's ok\n"));
1290 else if (ok_blook(bsdcompat) || ok_blook(bsdmsgs))
1291 printf(tr(185, "Null message body; hope that's ok\n"));
1294 if (dosign < 0)
1295 dosign = ok_blook(smime_sign);
1296 #ifndef HAVE_SSL
1297 if (dosign) {
1298 fprintf(stderr, tr(225, "No SSL support compiled in.\n"));
1299 goto jleave;
1301 #endif
1303 /* XXX Update time_current again; once collect() offers editing of more
1304 * XXX headers, including Date:, this must only happen if Date: is the
1305 * XXX same that it was before collect() (e.g., postponing etc.).
1306 * XXX But *do* update otherwise because the mail seems to be backdated
1307 * XXX if the user edited some time, which looks odd and it happened
1308 * XXX to me that i got mis-dated response mails due to that... */
1309 time_current_update(&time_current, TRU1);
1311 /* TODO hrmpf; the MIME/send layer rewrite MUST address the init crap:
1312 * TODO setup the header ONCE; note this affects edit.c, collect.c ...,
1313 * TODO but: offer a hook that rebuilds/expands/checks/fixates all
1314 * TODO header fields ONCE, call that ONCE after user editing etc. has
1315 * TODO completed (one edit cycle) */
1317 /* Take the user names from the combined to and cc lists and do all the
1318 * alias processing. The POSIX standard says:
1319 * The names shall be substituted when alias is used as a recipient
1320 * address specified by the user in an outgoing message (that is,
1321 * other recipients addressed indirectly through the reply command
1322 * shall not be substituted in this manner).
1323 * S-nail thus violates POSIX, as has been pointed out correctly by
1324 * Martin Neitzel, but logic and usability of POSIX standards is not seldom
1325 * disputable anyway. Go for user friendliness */
1327 /* Do alias expansion on Reply-To: members, too */
1328 /* TODO puthead() YET (!!! see ONCE note above) expands the value, but
1329 * TODO doesn't perform alias expansion; encapsulate in the ONCE-o */
1330 if (hp->h_replyto == NULL && (cp = ok_vlook(replyto)) != NULL)
1331 hp->h_replyto = checkaddrs(lextract(cp, GEXTRA | GFULL));
1332 if (hp->h_replyto != NULL)
1333 hp->h_replyto = elide(usermap(hp->h_replyto, TRU1));
1335 /* TODO what happens now is that all recipients are merged into
1336 * TODO a duplicated list with expanded aliases, then this list is
1337 * TODO splitted again into the three individual recipient lists (with
1338 * TODO duplicates removed).
1339 * TODO later on we use the merged list for outof() pipe/file saving,
1340 * TODO then we eliminate duplicates (again) and then we use that one
1341 * TODO for mightrecord() and _transfer(), and again. ... Please ... */
1343 /* NOTE: Due to elide() in fixhead(), ENSURE to,cc,bcc order of to!,
1344 * because otherwise the recipients will be "degraded" if they occur
1345 * multiple times */
1346 to = usermap(cat(hp->h_to, cat(hp->h_cc, hp->h_bcc)), FAL0);
1347 if (to == NULL) {
1348 fprintf(stderr, tr(186, "No recipients specified\n"));
1349 _sendout_error = TRU1;
1351 to = fixhead(hp, to);
1353 /* */
1354 memset(&sb, 0, sizeof sb);
1355 sb.sb_hp = hp;
1356 sb.sb_to = to;
1357 sb.sb_input = mtf;
1358 if ((dosign || count_nonlocal(to) > 0) &&
1359 !_sendbundle_setup_creds(&sb, (dosign > 0)))
1360 /* TODO saving $DEAD and recovering etc is not yet well defined */
1361 goto jfail_dead;
1363 /* 'Bit ugly kind of control flow until we find a charset that does it */
1364 for (charset_iter_reset(hp->h_charset);; charset_iter_next()) {
1365 if (!charset_iter_is_valid())
1367 else if ((nmtf = infix(hp, mtf)) != NULL)
1368 break;
1369 else if ((err = errno) == EILSEQ || err == EINVAL) {
1370 rewind(mtf);
1371 continue;
1374 perror("");
1375 jfail_dead:
1376 _sendout_error = TRU1;
1377 savedeadletter(mtf, TRU1);
1378 fputs(tr(182, "... message not sent.\n"), stderr);
1379 goto jleave;
1381 mtf = nmtf;
1383 /* */
1384 #ifdef HAVE_SSL
1385 if (dosign) {
1386 if ((nmtf = smime_sign(mtf, sb.sb_signer.s)) == NULL)
1387 goto jfail_dead;
1388 Fclose(mtf);
1389 mtf = nmtf;
1391 #endif
1393 /* TODO truly - i still don't get what follows: (1) we deliver file
1394 * TODO and pipe addressees, (2) we mightrecord() and (3) we transfer
1395 * TODO even if (1) savedeadletter() etc. To me this doesn't make sense? */
1397 /* Deliver pipe and file addressees */
1398 to = outof(to, mtf, &_sendout_error);
1399 if (_sendout_error)
1400 savedeadletter(mtf, FAL0);
1402 to = elide(to); /* XXX needed only to drop GDELs due to outof()! */
1403 { ui32_t cnt = count(to);
1404 if ((!recipient_record || cnt > 0) &&
1405 !mightrecord(mtf, (recipient_record ? to : NULL)))
1406 goto jleave;
1407 if (cnt > 0) {
1408 sb.sb_hp = hp;
1409 sb.sb_to = to;
1410 sb.sb_input = mtf;
1411 if (_transfer(&sb))
1412 rv = OKAY;
1413 } else if (!_sendout_error)
1414 rv = OKAY;
1416 jleave:
1417 Fclose(mtf);
1418 j_leave:
1419 if (_sendout_error)
1420 exit_status |= EXIT_SEND_ERROR;
1421 NYD_LEAVE;
1422 return rv;
1425 FL int
1426 mkdate(FILE *fo, char const *field)
1428 struct tm *tmptr;
1429 int tzdiff, tzdiff_hour, tzdiff_min, rv;
1430 NYD_ENTER;
1432 tzdiff = time_current.tc_time - mktime(&time_current.tc_gm);
1433 tzdiff_hour = (int)(tzdiff / 60);
1434 tzdiff_min = tzdiff_hour % 60;
1435 tzdiff_hour /= 60;
1436 tmptr = &time_current.tc_local;
1437 if (tmptr->tm_isdst > 0)
1438 ++tzdiff_hour;
1439 rv = fprintf(fo, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1440 field,
1441 weekday_names[tmptr->tm_wday],
1442 tmptr->tm_mday, month_names[tmptr->tm_mon],
1443 tmptr->tm_year + 1900, tmptr->tm_hour,
1444 tmptr->tm_min, tmptr->tm_sec,
1445 tzdiff_hour * 100 + tzdiff_min);
1446 NYD_LEAVE;
1447 return rv;
1450 FL int
1451 puthead(struct header *hp, FILE *fo, enum gfield w, enum sendaction action,
1452 enum conversion convert, char const *contenttype, char const *charset)
1454 #define FMT_CC_AND_BCC() \
1455 do {\
1456 if (hp->h_cc != NULL && (w & GCC)) {\
1457 if (fmt("Cc:", hp->h_cc, fo, (w & (GCOMMA | GFILES)), 0,\
1458 (action != SEND_TODISP)))\
1459 goto jleave;\
1460 ++gotcha;\
1462 if (hp->h_bcc != NULL && (w & GBCC)) {\
1463 if (fmt("Bcc:", hp->h_bcc, fo, (w & (GCOMMA | GFILES)), 0,\
1464 (action != SEND_TODISP)))\
1465 goto jleave;\
1466 ++gotcha;\
1468 } while (0)
1470 char const *addr;
1471 size_t gotcha, l;
1472 struct name *np, *fromfield = NULL, *senderfield = NULL;
1473 int stealthmua, rv = 1;
1474 bool_t nodisp;
1475 NYD_ENTER;
1477 if ((addr = ok_vlook(stealthmua)) != NULL)
1478 stealthmua = !strcmp(addr, "noagent") ? -1 : 1;
1479 else
1480 stealthmua = 0;
1481 gotcha = 0;
1482 nodisp = (action != SEND_TODISP);
1484 if (w & GDATE)
1485 mkdate(fo, "Date"), ++gotcha;
1486 if (w & GIDENT) {
1487 if (hp->h_from != NULL) {
1488 if (fmt("From:", hp->h_from, fo, (w & (GCOMMA | GFILES)), 0, nodisp))
1489 goto jleave;
1490 ++gotcha;
1491 fromfield = hp->h_from;
1492 } else if ((addr = myaddrs(hp)) != NULL) {
1493 if (_putname(addr, w, action, &gotcha, "From:", fo, &fromfield))
1494 goto jleave;
1495 hp->h_from = fromfield;
1498 if (((addr = hp->h_organization) != NULL ||
1499 (addr = ok_vlook(ORGANIZATION)) != NULL) &&
1500 (l = strlen(addr)) > 0) {
1501 fwrite("Organization: ", sizeof(char), 14, fo);
1502 if (xmime_write(addr, l, fo, (!nodisp ? CONV_NONE : CONV_TOHDR),
1503 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1504 goto jleave;
1505 ++gotcha;
1506 putc('\n', fo);
1509 /* TODO see the ONCE TODO note somewhere around this file;
1510 * TODO but anyway, do NOT perform alias expansion UNLESS
1511 * TODO we are actually sending out! */
1512 if (hp->h_replyto != NULL) {
1513 if (fmt("Reply-To:", hp->h_replyto, fo, w & GCOMMA, 0, nodisp))
1514 goto jleave;
1515 ++gotcha;
1516 } else if ((addr = ok_vlook(replyto)) != NULL)
1517 if (_putname(addr, w, action, &gotcha, "Reply-To:", fo, NULL))
1518 goto jleave;
1520 if (hp->h_sender != NULL) {
1521 if (fmt("Sender:", hp->h_sender, fo, w & GCOMMA, 0, nodisp))
1522 goto jleave;
1523 ++gotcha;
1524 senderfield = hp->h_sender;
1525 } else if ((addr = ok_vlook(sender)) != NULL)
1526 if (_putname(addr, w, action, &gotcha, "Sender:", fo, &senderfield))
1527 goto jleave;
1529 if (check_from_and_sender(fromfield, senderfield))
1530 goto jleave;
1533 if (hp->h_to != NULL && w & GTO) {
1534 if (fmt("To:", hp->h_to, fo, (w & (GCOMMA | GFILES)), 0, nodisp))
1535 goto jleave;
1536 ++gotcha;
1539 if (!ok_blook(bsdcompat) && !ok_blook(bsdorder))
1540 FMT_CC_AND_BCC();
1542 if (hp->h_subject != NULL && (w & GSUBJECT)) {
1543 fwrite("Subject: ", sizeof (char), 9, fo);
1544 if (!ascncasecmp(hp->h_subject, "re: ", 4)) {/* TODO localizable */
1545 fwrite("Re: ", sizeof(char), 4, fo);
1546 if (strlen(hp->h_subject + 4) > 0 &&
1547 xmime_write(hp->h_subject + 4, strlen(hp->h_subject + 4), fo,
1548 (!nodisp ? CONV_NONE : CONV_TOHDR),
1549 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1550 goto jleave;
1551 } else if (*hp->h_subject != '\0') {
1552 if (xmime_write(hp->h_subject, strlen(hp->h_subject), fo,
1553 (!nodisp ? CONV_NONE : CONV_TOHDR),
1554 (!nodisp ? TD_ISPR | TD_ICONV : TD_ICONV), NULL) < 0)
1555 goto jleave;
1557 ++gotcha;
1558 fwrite("\n", sizeof (char), 1, fo);
1561 if (ok_blook(bsdcompat) || ok_blook(bsdorder))
1562 FMT_CC_AND_BCC();
1564 if ((w & GMSGID) && stealthmua <= 0)
1565 _message_id(fo, hp), ++gotcha;
1567 if ((np = hp->h_ref) != NULL && (w & GREF)) {
1568 fmt("References:", np, fo, 0, 1, 0);
1569 if (np->n_name != NULL) {
1570 while (np->n_flink != NULL)
1571 np = np->n_flink;
1572 if (!is_addr_invalid(np, 0)) {
1573 fprintf(fo, "In-Reply-To: %s\n", np->n_name);
1574 ++gotcha;
1579 if ((w & GUA) && stealthmua == 0)
1580 fprintf(fo, "User-Agent: %s %s\n", uagent, version), ++gotcha;
1582 if (w & GMIME) {
1583 fputs("MIME-Version: 1.0\n", fo), ++gotcha;
1584 if (hp->h_attach != NULL) {
1585 _sendout_boundary = mime_create_boundary();/*TODO carrier*/
1586 fprintf(fo, "Content-Type: multipart/mixed;\n boundary=\"%s\"\n",
1587 _sendout_boundary);
1588 } else {
1589 fprintf(fo, "Content-Type: %s", contenttype);
1590 if (charset != NULL)
1591 fprintf(fo, "; charset=%s", charset);
1592 fprintf(fo, "\nContent-Transfer-Encoding: %s\n",
1593 _get_encoding(convert));
1597 if (gotcha && (w & GNL))
1598 putc('\n', fo);
1599 rv = 0;
1600 jleave:
1601 NYD_LEAVE;
1602 return rv;
1603 #undef FMT_CC_AND_BCC
1606 FL enum okay
1607 resend_msg(struct message *mp, struct name *to, int add_resent) /* TODO check */
1609 struct sendbundle sb;
1610 FILE *ibuf, *nfo, *nfi;
1611 char *tempMail;
1612 enum okay rv = STOP;
1613 NYD_ENTER;
1615 _sendout_error = FAL0;
1617 /* Update some globals we likely need first */
1618 time_current_update(&time_current, TRU1);
1620 if ((to = checkaddrs(to)) == NULL) {
1621 _sendout_error = TRU1;
1622 goto jleave;
1625 if ((nfo = Ftmp(&tempMail, "resend", OF_WRONLY | OF_HOLDSIGS | OF_REGISTER,
1626 0600)) == NULL) {
1627 _sendout_error = TRU1;
1628 perror(tr(189, "temporary mail file"));
1629 goto jleave;
1631 if ((nfi = Fopen(tempMail, "r")) == NULL) {
1632 _sendout_error = TRU1;
1633 perror(tempMail);
1635 Ftmp_release(&tempMail);
1636 if (nfi == NULL)
1637 goto jerr_o;
1639 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1640 goto jerr_all;
1642 memset(&sb, 0, sizeof sb);
1643 sb.sb_to = to;
1644 sb.sb_input = nfi;
1645 if (count_nonlocal(to) > 0 && !_sendbundle_setup_creds(&sb, FAL0))
1646 /* TODO saving $DEAD and recovering etc is not yet well defined */
1647 goto jerr_all;
1649 if (infix_resend(ibuf, nfo, mp, to, add_resent) != 0) {
1650 savedeadletter(nfi, TRU1);
1651 fputs(tr(182, "... message not sent.\n"), stderr);
1652 jerr_all:
1653 Fclose(nfi);
1654 jerr_o:
1655 Fclose(nfo);
1656 _sendout_error = TRU1;
1657 goto jleave;
1659 Fclose(nfo);
1660 rewind(nfi);
1662 to = outof(to, nfi, &_sendout_error);
1663 if (_sendout_error)
1664 savedeadletter(nfi, FAL0);
1666 to = elide(to); /* TODO should have been done in fixhead()? */
1667 if (count(to) != 0) {
1668 if (!ok_blook(record_resent) || mightrecord(nfi, to)) {
1669 sb.sb_to = to;
1670 /*sb.sb_input = nfi;*/
1671 if (_transfer(&sb))
1672 rv = OKAY;
1674 } else if (!_sendout_error)
1675 rv = OKAY;
1677 Fclose(nfi);
1678 jleave:
1679 if (_sendout_error)
1680 exit_status |= EXIT_SEND_ERROR;
1681 NYD_LEAVE;
1682 return rv;
1685 #undef SEND_LINESIZE
1687 /* vim:set fenc=utf-8:s-it-mode */