2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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
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
42 static char sccsid
[] = "@(#)sendout.c 2.100 (gritter) 3/1/09";
56 * Mail -- a mail program
61 static char *send_boundary
;
63 static char *getencoding(enum conversion convert
);
64 static struct name
*fixhead(struct header
*hp
, struct name
*tolist
);
65 static int put_signature(FILE *fo
, int convert
);
66 static int attach_file1(struct attachment
*ap
, FILE *fo
, int dosign
);
67 static int attach_file(struct attachment
*ap
, FILE *fo
, int dosign
);
68 static int attach_message(struct attachment
*ap
, FILE *fo
, int dosign
);
69 static int make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
70 const char *contenttype
, const char *charset
, int dosign
);
71 static FILE *infix(struct header
*hp
, FILE *fi
, int dosign
);
72 static int savemail(char *name
, FILE *fi
);
73 static int sendmail_internal(void *v
, int recipient_record
);
74 static enum okay
transfer(struct name
*to
, struct name
*mailargs
, FILE *input
,
76 static enum okay
start_mta(struct name
*to
, struct name
*mailargs
, FILE *input
,
78 static void message_id(FILE *fo
, struct header
*hp
);
79 static int fmt(char *str
, struct name
*np
, FILE *fo
, int comma
,
80 int dropinvalid
, int domime
);
81 static int infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
,
82 struct name
*to
, int add_resent
);
85 * Generate a boundary for MIME multipart messages.
90 static char bound
[70];
94 snprintf(bound
, sizeof bound
, "=_%lx.%s", (long)now
, getrandstring(48));
95 send_boundary
= bound
;
100 * Get an encoding flag based on the given string.
103 getencoding(enum conversion convert
)
111 return "quoted-printable";
122 * Fix the header by glopping all of the expanded names from
123 * the distribution list into the appropriate fields.
126 fixhead(struct header
*hp
, struct name
*tolist
)
133 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
)
134 if ((np
->n_type
& GMASK
) == GTO
)
135 hp
->h_to
= cat(hp
->h_to
, ndup(np
, GFULL
));
136 else if ((np
->n_type
& GMASK
) == GCC
)
137 hp
->h_cc
= cat(hp
->h_cc
, ndup(np
, GFULL
));
138 else if ((np
->n_type
& GMASK
) == GBCC
)
139 hp
->h_bcc
= cat(hp
->h_bcc
, ndup(np
, GFULL
));
145 * Do not change, you get incorrect base64 encodings else!
147 #define INFIX_BUF 972
150 * Put the signature file at fo.
153 put_signature(FILE *fo
, int convert
)
155 char *sig
, buf
[INFIX_BUF
], c
= '\n';
159 sig
= value("signature");
160 if (sig
== NULL
|| *sig
== '\0')
164 if ((fsig
= Fopen(sig
, "r")) == NULL
) {
168 while ((sz
= fread(buf
, sizeof *buf
, INFIX_BUF
, fsig
)) != 0) {
170 if (mime_write(buf
, sz
, fo
, convert
, TD_NONE
,
171 NULL
, (size_t)0, NULL
, NULL
)
190 * Write an attachment to the file buffer, converting to MIME.
193 attach_file1(struct attachment
*ap
, FILE *fo
, int dosign
)
196 char *charset
= NULL
, *contenttype
= NULL
, *basename
;
197 enum conversion convert
= CONV_TOB64
;
199 enum mimeclean isclean
;
202 size_t bufsize
, count
;
208 if ((fi
= Fopen(ap
->a_name
, "r")) == NULL
) {
212 if ((basename
= strrchr(ap
->a_name
, '/')) == NULL
)
213 basename
= ap
->a_name
;
216 if (ap
->a_content_type
)
217 contenttype
= ap
->a_content_type
;
219 contenttype
= mime_filecontent(basename
);
221 charset
= ap
->a_charset
;
222 convert
= get_mime_convert(fi
, &contenttype
, &charset
, &isclean
,
227 send_boundary
, contenttype
);
231 fprintf(fo
, ";\n charset=%s\n", charset
);
232 if (ap
->a_content_disposition
== NULL
)
233 ap
->a_content_disposition
= "attachment";
234 fprintf(fo
, "Content-Transfer-Encoding: %s\n"
235 "Content-Disposition: %s;\n"
237 getencoding(convert
),
238 ap
->a_content_disposition
);
239 mime_write(basename
, strlen(basename
), fo
,
240 CONV_TOHDR
, TD_NONE
, NULL
, (size_t)0, NULL
, NULL
);
241 fwrite("\"\n", sizeof (char), 2, fo
);
242 if (ap
->a_content_id
)
243 fprintf(fo
, "Content-ID: %s\n", ap
->a_content_id
);
244 if (ap
->a_content_description
)
245 fprintf(fo
, "Content-Description: %s\n",
246 ap
->a_content_description
);
249 if (iconvd
!= (iconv_t
)-1) {
251 iconvd
= (iconv_t
)-1;
254 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
255 ascncasecmp(contenttype
, "text/", 5) == 0 &&
256 isclean
& MIME_HIGHBIT
&&
258 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1 &&
261 fprintf(stderr
, catgets(catd
, CATSET
, 179,
262 "Cannot convert from %s to %s\n"), tcs
, charset
);
264 perror("iconv_open");
269 #endif /* HAVE_ICONV */
270 buf
= smalloc(bufsize
= INFIX_BUF
);
271 if (convert
== CONV_TOQP
273 || iconvd
!= (iconv_t
)-1
280 if (convert
== CONV_TOQP
282 || iconvd
!= (iconv_t
)-1
285 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
289 if ((sz
= fread(buf
, sizeof *buf
, bufsize
, fi
)) == 0)
293 if (mime_write(buf
, sz
, fo
, convert
, TD_ICONV
,
294 NULL
, (size_t)0, NULL
, NULL
) == 0)
297 if (convert
== CONV_TOQP
&& lastc
!= '\n')
298 fwrite("=\n", 1, 2, fo
);
307 * Try out different character set conversions to attach a file.
310 attach_file(struct attachment
*ap
, FILE *fo
, int dosign
)
312 char *_wantcharset
, *charsets
, *ncs
;
313 size_t offs
= ftell(fo
);
315 if (ap
->a_charset
|| (charsets
= value("sendcharsets")) == NULL
)
316 return attach_file1(ap
, fo
, dosign
);
317 _wantcharset
= wantcharset
;
318 wantcharset
= savestr(charsets
);
319 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
321 try: if (attach_file1(ap
, fo
, dosign
) != 0) {
322 if (errno
== EILSEQ
|| errno
== EINVAL
) {
326 fseek(fo
, offs
, SEEK_SET
);
330 if (wantcharset
== (char *)-1)
333 wantcharset
= (char *)-1;
335 fseek(fo
, offs
, SEEK_SET
);
341 wantcharset
= _wantcharset
;
346 * Attach a message to the file buffer.
349 attach_message(struct attachment
*ap
, FILE *fo
, int dosign
)
354 fprintf(fo
, "\n--%s\n"
355 "Content-Type: message/rfc822\n"
356 "Content-Disposition: inline\n\n", send_boundary
);
357 mp
= &message
[ap
->a_msgno
- 1];
359 if (send(mp
, fo
, 0, NULL
, SEND_RFC822
, NULL
) < 0)
365 * Generate the body of a MIME multipart message.
368 make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
369 const char *contenttype
, const char *charset
, int dosign
)
371 struct attachment
*att
;
373 fputs("This is a multi-part message in MIME format.\n", fo
);
374 if (fsize(fi
) != 0) {
376 size_t sz
, bufsize
, count
;
378 fprintf(fo
, "\n--%s\n", send_boundary
);
379 fprintf(fo
, "Content-Type: %s", contenttype
);
381 fprintf(fo
, "; charset=%s", charset
);
382 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n"
383 "Content-Disposition: inline\n\n",
384 getencoding(convert
));
385 buf
= smalloc(bufsize
= INFIX_BUF
);
386 if (convert
== CONV_TOQP
388 || iconvd
!= (iconv_t
)-1
389 #endif /* HAVE_ICONV */
395 if (convert
== CONV_TOQP
397 || iconvd
!= (iconv_t
)-1
398 #endif /* HAVE_ICONV */
400 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
404 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
409 if (mime_write(buf
, sz
, fo
, convert
,
410 TD_ICONV
, NULL
, (size_t)0,
422 put_signature(fo
, convert
);
424 for (att
= hp
->h_attach
; att
!= NULL
; att
= att
->a_flink
) {
426 if (attach_message(att
, fo
, dosign
) != 0)
429 if (attach_file(att
, fo
, dosign
) != 0)
433 /* the final boundary with two attached dashes */
434 fprintf(fo
, "\n--%s--\n", send_boundary
);
439 * Prepend a header in front of the collected stuff
440 * and return the new file.
443 infix(struct header
*hp
, FILE *fi
, int dosign
)
448 char *tcs
, *convhdr
= NULL
;
450 enum mimeclean isclean
;
451 enum conversion convert
;
452 char *charset
= NULL
, *contenttype
= NULL
;
455 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
456 perror(catgets(catd
, CATSET
, 178, "temporary mail file"));
459 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
466 convert
= get_mime_convert(fi
, &contenttype
, &charset
,
470 if ((convhdr
= need_hdrconv(hp
, GTO
|GSUBJECT
|GCC
|GBCC
|GIDENT
)) != 0) {
471 if (iconvd
!= (iconv_t
)-1)
473 if ((iconvd
= iconv_open_ft(convhdr
, tcs
)) == (iconv_t
)-1
476 fprintf(stderr
, catgets(catd
, CATSET
, 179,
477 "Cannot convert from %s to %s\n"), tcs
, convhdr
);
479 perror("iconv_open");
484 #endif /* HAVE_ICONV */
486 GTO
|GSUBJECT
|GCC
|GBCC
|GNL
|GCOMMA
|GUA
|GMIME
487 |GMSGID
|GIDENT
|GREF
|GDATE
,
488 SEND_MBOX
, convert
, contenttype
, charset
)) {
492 if (iconvd
!= (iconv_t
)-1) {
494 iconvd
= (iconv_t
)-1;
500 if (convhdr
&& iconvd
!= (iconv_t
)-1) {
502 iconvd
= (iconv_t
)-1;
504 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
505 ascncasecmp(contenttype
, "text/", 5) == 0 &&
506 isclean
& MIME_HIGHBIT
&&
508 if (iconvd
!= (iconv_t
)-1)
510 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1
513 fprintf(stderr
, catgets(catd
, CATSET
, 179,
514 "Cannot convert from %s to %s\n"), tcs
, charset
);
516 perror("iconv_open");
522 if (hp
->h_attach
!= NULL
) {
523 if (make_multipart(hp
, convert
, fi
, nfo
,
524 contenttype
, charset
, dosign
) != 0) {
528 if (iconvd
!= (iconv_t
)-1) {
530 iconvd
= (iconv_t
)-1;
536 size_t sz
, bufsize
, count
;
539 if (convert
== CONV_TOQP
541 || iconvd
!= (iconv_t
)-1
542 #endif /* HAVE_ICONV */
547 buf
= smalloc(bufsize
= INFIX_BUF
);
549 if (convert
== CONV_TOQP
551 || iconvd
!= (iconv_t
)-1
552 #endif /* HAVE_ICONV */
554 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
558 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
563 if (mime_write(buf
, sz
, nfo
, convert
,
564 TD_ICONV
, NULL
, (size_t)0,
569 if (iconvd
!= (iconv_t
)-1) {
571 iconvd
= (iconv_t
)-1;
578 if (convert
== CONV_TOQP
&& lastc
!= '\n')
579 fwrite("=\n", 1, 2, nfo
);
585 if (iconvd
!= (iconv_t
)-1) {
587 iconvd
= (iconv_t
)-1;
593 put_signature(nfo
, convert
);
596 if (iconvd
!= (iconv_t
)-1) {
598 iconvd
= (iconv_t
)-1;
603 perror(catgets(catd
, CATSET
, 180, "temporary mail file"));
616 * Save the outgoing mail on the passed file.
621 savemail(char *name
, FILE *fi
)
625 size_t bufsize
, buflen
, count
;
631 buf
= smalloc(bufsize
= LINESIZE
);
633 if ((fo
= Zopen(name
, "a+", NULL
)) == NULL
) {
634 if ((fo
= Zopen(name
, "wx", NULL
)) == NULL
) {
640 if (fseek(fo
, -2L, SEEK_END
) == 0) {
641 switch (fread(buf
, sizeof *buf
, 2, fo
)) {
643 if (buf
[1] != '\n') {
666 fprintf(fo
, "From %s %s", myname
, ctime(&now
));
671 while (fgetline(&buf
, &bufsize
, &count
, &buflen
, fi
, 0) != NULL
) {
676 if (strncmp(p
, "From ", 5) == 0)
677 /* we got a masked From line */
679 } else if (strncmp(buf
, "From ", 5) == 0)
681 fwrite(buf
, sizeof *buf
, buflen
, fo
);
683 if (buflen
&& *(buf
+ buflen
- 1) != '\n')
700 * Interface between the argument list and the mail1 routine
701 * which does all the dirty work.
704 mail(struct name
*to
, struct name
*cc
, struct name
*bcc
,
705 struct name
*smopts
, char *subject
, struct attachment
*attach
,
706 char *quotefile
, int recipient_record
, int tflag
, int Eflag
)
711 memset(&head
, 0, sizeof head
);
712 /* The given subject may be in RFC1522 format. */
713 if (subject
!= NULL
) {
715 in
.l
= strlen(subject
);
716 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
717 head
.h_subject
= out
.s
;
724 head
.h_attach
= attach
;
725 head
.h_smopts
= smopts
;
726 mail1(&head
, 0, NULL
, quotefile
, recipient_record
, 0, tflag
, Eflag
);
733 * Send mail to a bunch of user names. The interface is through
734 * the mail routine below.
737 sendmail_internal(void *v
, int recipient_record
)
743 memset(&head
, 0, sizeof head
);
744 head
.h_to
= extract(str
, GTO
|GFULL
);
745 Eflag
= value("skipemptybody") != NULL
;
746 mail1(&head
, 0, NULL
, NULL
, recipient_record
, 0, 0, Eflag
);
753 return sendmail_internal(v
, 0);
759 return sendmail_internal(v
, 1);
763 transfer(struct name
*to
, struct name
*mailargs
, FILE *input
, struct header
*hp
)
765 char o
[LINESIZE
], *cp
;
766 struct name
*np
, *nt
;
773 snprintf(o
, sizeof o
, "smime-encrypt-%s", np
->n_name
);
774 if ((cp
= value(o
)) != NULL
) {
775 if ((ef
= smime_encrypt(input
, cp
, np
->n_name
)) != 0) {
776 nt
= nalloc(np
->n_name
,
777 np
->n_type
& ~(GFULL
|GSKIN
));
778 if (start_mta(nt
, mailargs
, ef
, hp
) != OKAY
)
782 fprintf(stderr
, "Message not sent to <%s>\n",
788 np
->n_flink
->n_blink
= np
->n_blink
;
790 np
->n_blink
->n_flink
= np
->n_flink
;
800 if (value("smime-force-encryption") ||
801 start_mta(to
, mailargs
, input
, hp
) != OKAY
)
808 * Start the Mail Transfer Agent
809 * mailing to namelist and stdin redirected to input.
812 start_mta(struct name
*to
, struct name
*mailargs
, FILE *input
,
815 char **args
= NULL
, **t
;
819 char *user
= NULL
, *password
= NULL
, *skinned
= NULL
;
826 if ((smtp
= value("smtp")) == NULL
) {
827 args
= unpack(cat(mailargs
, to
));
828 if (debug
|| value("debug")) {
829 printf(catgets(catd
, CATSET
, 181,
830 "Sendmail arguments:"));
831 for (t
= args
; *t
!= NULL
; t
++)
832 printf(" \"%s\"", *t
);
839 skinned
= skin(myorigin(hp
));
840 if ((user
= smtp_auth_var("-user", skinned
)) != NULL
&&
841 (password
= smtp_auth_var("-password",
843 password
= getpassword(&otio
, &reset_tio
, NULL
);
847 * Fork, set up the temporary mail file as standard
848 * input for "mail", and exec with the user list we generated
851 if ((pid
= fork()) == -1) {
853 savedeadletter(input
);
859 sigaddset(&nset
, SIGHUP
);
860 sigaddset(&nset
, SIGINT
);
861 sigaddset(&nset
, SIGQUIT
);
862 sigaddset(&nset
, SIGTSTP
);
863 sigaddset(&nset
, SIGTTIN
);
864 sigaddset(&nset
, SIGTTOU
);
865 freopen("/dev/null", "r", stdin
);
867 prepare_child(&nset
, 0, 1);
868 if (smtp_mta(smtp
, to
, input
, hp
,
869 user
, password
, skinned
) == 0)
872 prepare_child(&nset
, fileno(input
), -1);
873 if ((cp
= value("sendmail")) != NULL
)
880 savedeadletter(input
);
881 fputs(catgets(catd
, CATSET
, 182,
882 ". . . message not sent.\n"), stderr
);
885 if (value("verbose") != NULL
|| value("sendwait") || debug
887 if (wait_child(pid
) == 0)
899 * Record outgoing mail if instructed to do so.
902 mightrecord(FILE *fp
, struct name
*to
, int recipient_record
)
906 if (recipient_record
) {
907 cq
= skin(to
->n_name
);
908 cp
= salloc(strlen(cq
) + 1);
910 for (cq
= cp
; *cq
&& *cq
!= '@'; cq
++);
913 cp
= value("record");
916 if (value("outfolder") && *ep
!= '/' && *ep
!= '+' &&
917 which_protocol(ep
) == PROTO_FILE
) {
918 cq
= salloc(strlen(cp
) + 2);
924 if (savemail(ep
, fp
) != 0) {
926 "Error while saving message to %s - "
927 "message not sent\n", ep
);
938 * Mail a message on standard input to the people indicated
939 * in the passed header. (Internal interface).
942 mail1(struct header
*hp
, int printheaders
, struct message
*quote
,
943 char *quotefile
, int recipient_record
, int doprefix
, int tflag
,
950 char *charsets
, *ncs
= NULL
, *cp
;
953 if ((hp
->h_to
= checkaddrs(hp
->h_to
)) == NULL
) {
958 if ((cp
= value("autocc")) != NULL
&& *cp
)
959 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(sextract(cp
, GCC
|GFULL
)));
960 if ((cp
= value("autobcc")) != NULL
&& *cp
)
961 hp
->h_bcc
= cat(hp
->h_bcc
,
962 checkaddrs(sextract(cp
, GBCC
|GFULL
)));
964 * Collect user's mail from standard input.
965 * Get the result as mtf.
967 if ((mtf
= collect(hp
, printheaders
, quote
, quotefile
, doprefix
,
970 if (value("interactive") != NULL
) {
971 if (((value("bsdcompat") || value("askatend"))
972 && (value("askcc") != NULL
||
973 value("askbcc") != NULL
)) ||
974 value("askattach") != NULL
||
975 value("asksign") != NULL
) {
976 if (value("askcc") != NULL
)
978 if (value("askbcc") != NULL
)
980 if (value("askattach") != NULL
)
981 hp
->h_attach
= edit_attachments(hp
->h_attach
);
982 if (value("asksign") != NULL
)
983 dosign
= yorn("Sign this message (y/n)? ");
985 printf(catgets(catd
, CATSET
, 183, "EOT\n"));
989 if (fsize(mtf
) == 0) {
992 if (hp
->h_subject
== NULL
)
993 printf(catgets(catd
, CATSET
, 184,
994 "No message, no subject; hope that's ok\n"));
995 else if (value("bsdcompat") || value("bsdmsgs"))
996 printf(catgets(catd
, CATSET
, 185,
997 "Null message body; hope that's ok\n"));
1000 if (value("smime-sign") != NULL
)
1006 * Now, take the user names from the combined
1007 * to and cc lists and do all the alias
1011 if ((to
= usermap(cat(hp
->h_bcc
, cat(hp
->h_to
, hp
->h_cc
)))) == NULL
) {
1012 printf(catgets(catd
, CATSET
, 186, "No recipients specified\n"));
1015 to
= fixhead(hp
, to
);
1016 if (hp
->h_charset
) {
1017 wantcharset
= hp
->h_charset
;
1020 hloop
: wantcharset
= NULL
;
1021 if ((charsets
= value("sendcharsets")) != NULL
) {
1022 wantcharset
= savestr(charsets
);
1023 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
1026 try: if ((nmtf
= infix(hp
, mtf
, dosign
)) == NULL
) {
1027 if (hp
->h_charset
&& (errno
== EILSEQ
|| errno
== EINVAL
)) {
1029 hp
->h_charset
= NULL
;
1032 if (errno
== EILSEQ
|| errno
== EINVAL
) {
1038 if (wantcharset
&& value("interactive") == NULL
) {
1039 if (wantcharset
== (char *)-1)
1043 wantcharset
= (char *)-1;
1048 /* fprintf(stderr, ". . . message lost, sorry.\n"); */
1052 savedeadletter(mtf
);
1053 fputs(catgets(catd
, CATSET
, 187,
1054 ". . . message not sent.\n"), stderr
);
1059 if ((nmtf
= smime_sign(mtf
, hp
)) == NULL
)
1065 * Look through the recipient list for names with /'s
1066 * in them which we write to as files directly.
1068 to
= outof(to
, mtf
, hp
);
1070 savedeadletter(mtf
);
1072 if (count(to
) == 0) {
1077 if (mightrecord(mtf
, to
, recipient_record
) != OKAY
)
1079 ok
= transfer(to
, hp
->h_smopts
, mtf
, hp
);
1086 * Create a Message-Id: header field.
1087 * Use either the host name or the from address.
1090 message_id(FILE *fo
, struct header
*hp
)
1096 if ((cp
= value("hostname")) != NULL
)
1097 fprintf(fo
, "Message-ID: <%lx.%s@%s>\n",
1098 (long)now
, getrandstring(24), cp
);
1099 else if ((cp
= skin(myorigin(hp
))) != NULL
&& strchr(cp
, '@') != NULL
)
1100 fprintf(fo
, "Message-ID: <%lx.%s%%%s>\n",
1101 (long)now
, getrandstring(16), cp
);
1104 static const char *weekday_names
[] = {
1105 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1108 const char *month_names
[] = {
1109 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1110 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
1114 * Create a Date: header field.
1115 * We compare the localtime() and gmtime() results to get the timezone,
1116 * because numeric timezones are easier to read and because $TZ is
1117 * not set on most GNU systems.
1120 mkdate(FILE *fo
, const char *field
)
1124 int tzdiff
, tzdiff_hour
, tzdiff_min
;
1127 tzdiff
= t
- mktime(gmtime(&t
));
1128 tzdiff_hour
= (int)(tzdiff
/ 60);
1129 tzdiff_min
= tzdiff_hour
% 60;
1131 tmptr
= localtime(&t
);
1132 if (tmptr
->tm_isdst
> 0)
1134 return fprintf(fo
, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1136 weekday_names
[tmptr
->tm_wday
],
1137 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
],
1138 tmptr
->tm_year
+ 1900, tmptr
->tm_hour
,
1139 tmptr
->tm_min
, tmptr
->tm_sec
,
1140 tzdiff_hour
* 100 + tzdiff_min
);
1144 putname(char *line
, enum gfield w
, enum sendaction action
, int *gotcha
,
1145 char *prefix
, FILE *fo
, struct name
**xp
)
1149 np
= sextract(line
, GEXTRA
|GFULL
);
1154 if (fmt(prefix
, np
, fo
, w
&(GCOMMA
|GFILES
), 0, action
!= SEND_TODISP
))
1161 #define FMT_CC_AND_BCC { \
1162 if (hp->h_cc != NULL && w & GCC) { \
1163 if (fmt("Cc:", hp->h_cc, fo, \
1164 w&(GCOMMA|GFILES), 0, \
1165 action!=SEND_TODISP)) \
1169 if (hp->h_bcc != NULL && w & GBCC) { \
1170 if (fmt("Bcc:", hp->h_bcc, fo, \
1171 w&(GCOMMA|GFILES), 0, \
1172 action!=SEND_TODISP)) \
1178 * Dump the to, subject, cc header on the
1179 * passed file buffer.
1182 puthead(struct header
*hp
, FILE *fo
, enum gfield w
,
1183 enum sendaction action
, enum conversion convert
,
1184 char *contenttype
, char *charset
)
1187 char *addr
/*, *cp*/;
1189 struct name
*np
, *fromfield
= NULL
, *senderfield
= NULL
;
1192 if (value("stealthmua"))
1198 mkdate(fo
, "Date"), gotcha
++;
1201 if (hp
->h_from
!= NULL
) {
1202 if (fmt("From:", hp
->h_from
, fo
, w
&(GCOMMA
|GFILES
), 0,
1203 action
!=SEND_TODISP
))
1206 fromfield
= hp
->h_from
;
1207 } else if ((addr
= myaddrs(hp
)) != NULL
)
1208 if (putname(addr
, w
, action
, &gotcha
, "From:", fo
,
1211 if (((addr
= hp
->h_organization
) != NULL
||
1212 (addr
= value("ORGANIZATION")) != NULL
)
1213 && strlen(addr
) > 0) {
1214 fwrite("Organization: ", sizeof (char), 14, fo
);
1215 if (mime_write(addr
, strlen(addr
), fo
,
1216 action
== SEND_TODISP
?
1217 CONV_NONE
:CONV_TOHDR
,
1218 action
== SEND_TODISP
?
1219 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1226 if (hp
->h_replyto
!= NULL
) {
1227 if (fmt("Reply-To:", hp
->h_replyto
, fo
,
1228 w
&(GCOMMA
|GFILES
), 0,
1229 action
!=SEND_TODISP
))
1232 } else if ((addr
= value("replyto")) != NULL
)
1233 if (putname(addr
, w
, action
, &gotcha
, "Reply-To:", fo
,
1236 if (hp
->h_sender
!= NULL
) {
1237 if (fmt("Sender:", hp
->h_sender
, fo
,
1238 w
&(GCOMMA
|GFILES
), 0,
1239 action
!=SEND_TODISP
))
1242 senderfield
= hp
->h_sender
;
1243 } else if ((addr
= value("sender")) != NULL
)
1244 if (putname(addr
, w
, action
, &gotcha
, "Sender:", fo
,
1247 if (check_from_and_sender(fromfield
, senderfield
))
1250 if (hp
->h_to
!= NULL
&& w
& GTO
) {
1251 if (fmt("To:", hp
->h_to
, fo
, w
&(GCOMMA
|GFILES
), 0,
1252 action
!=SEND_TODISP
))
1256 if (value("bsdcompat") == NULL
&& value("bsdorder") == NULL
)
1258 if (hp
->h_subject
!= NULL
&& w
& GSUBJECT
) {
1259 fwrite("Subject: ", sizeof (char), 9, fo
);
1260 if (ascncasecmp(hp
->h_subject
, "re: ", 4) == 0) {
1261 fwrite("Re: ", sizeof (char), 4, fo
);
1262 if (strlen(hp
->h_subject
+ 4) > 0 &&
1263 mime_write(hp
->h_subject
+ 4,
1264 strlen(hp
->h_subject
+ 4),
1265 fo
, action
== SEND_TODISP
?
1266 CONV_NONE
:CONV_TOHDR
,
1267 action
== SEND_TODISP
?
1268 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1272 } else if (*hp
->h_subject
) {
1273 if (mime_write(hp
->h_subject
,
1274 strlen(hp
->h_subject
),
1275 fo
, action
== SEND_TODISP
?
1276 CONV_NONE
:CONV_TOHDR
,
1277 action
== SEND_TODISP
?
1278 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1284 fwrite("\n", sizeof (char), 1, fo
);
1286 if (value("bsdcompat") || value("bsdorder"))
1288 if (w
& GMSGID
&& stealthmua
== 0)
1289 message_id(fo
, hp
), gotcha
++;
1290 if (hp
->h_ref
!= NULL
&& w
& GREF
) {
1291 fmt("References:", hp
->h_ref
, fo
, 0, 1, 0);
1292 if ((np
= hp
->h_ref
) != NULL
&& np
->n_name
) {
1295 if (mime_name_invalid(np
, 0) == 0) {
1296 fprintf(fo
, "In-Reply-To: %s\n", np
->n_name
);
1301 if (w
& GUA
&& stealthmua
== 0)
1302 fprintf(fo
, "User-Agent: S-nail %s\n",
1305 fputs("MIME-Version: 1.0\n", fo
), gotcha
++;
1306 if (hp
->h_attach
!= NULL
) {
1308 fprintf(fo
, "Content-Type: multipart/mixed;\n"
1309 " boundary=\"%s\"\n", send_boundary
);
1311 fprintf(fo
, "Content-Type: %s", contenttype
);
1313 fprintf(fo
, "; charset=%s", charset
);
1314 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n",
1315 getencoding(convert
));
1318 if (gotcha
&& w
& GNL
)
1324 * Format the given header line to not exceed 72 characters.
1327 fmt(char *str
, struct name
*np
, FILE *fo
, int flags
, int dropinvalid
,
1330 int col
, len
, count
= 0;
1331 int is_to
= 0, comma
;
1333 comma
= flags
&GCOMMA
? 1 : 0;
1336 fwrite(str
, sizeof *str
, strlen(str
), fo
);
1337 if (((flags
&GFILES
) == 0 &&
1338 col
== 3 && asccasecmp(str
, "to:") == 0) ||
1339 (col
== 3 && asccasecmp(str
, "cc:") == 0) ||
1340 (col
== 4 && asccasecmp(str
, "bcc:") == 0) ||
1342 asccasecmp(str
, "Resent-To:") == 0))
1345 for (; np
!= NULL
; np
= np
->n_flink
) {
1346 if (is_to
&& is_fileaddr(np
->n_name
))
1348 if (np
->n_flink
== NULL
)
1350 if (mime_name_invalid(np
, !dropinvalid
)) {
1356 len
= strlen(np
->n_fullname
);
1357 col
++; /* for the space */
1358 if (count
&& col
+ len
+ comma
> 72 && col
> 1) {
1363 len
= mime_write(np
->n_fullname
,
1365 domime
?CONV_TOHDR_A
:CONV_NONE
,
1366 TD_ICONV
, NULL
, (size_t)0,
1368 if (comma
&& !(is_to
&& is_fileaddr(np
->n_flink
->n_name
)))
1378 * Rewrite a message for resending, adding the Resent-Headers.
1381 infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
, struct name
*to
,
1385 char *buf
= NULL
, *cp
/*, *cp2*/;
1386 size_t c
, bufsize
= 0;
1387 struct name
*fromfield
= NULL
, *senderfield
= NULL
;
1391 * Write the Resent-Fields.
1394 fputs("Resent-", fo
);
1396 if ((cp
= myaddrs(NULL
)) != NULL
) {
1397 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1398 "Resent-From:", fo
, &fromfield
))
1401 if ((cp
= value("sender")) != NULL
) {
1402 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1403 "Resent-Sender:", fo
, &senderfield
))
1406 if (fmt("Resent-To:", to
, fo
, 1, 1, 0)) {
1411 if (value("stealthmua") == NULL
) {
1412 fputs("Resent-", fo
);
1413 message_id(fo
, NULL
);
1416 if (check_from_and_sender(fromfield
, senderfield
))
1419 * Write the original headers.
1422 if ((cp
= foldergets(&buf
, &bufsize
, &count
, &c
, fi
)) == NULL
)
1424 if (ascncasecmp("status: ", buf
, 8) != 0
1425 && strncmp("From ", buf
, 5) != 0) {
1426 fwrite(buf
, sizeof *buf
, c
, fo
);
1428 if (count
> 0 && *buf
== '\n')
1432 * Write the message body.
1435 if (foldergets(&buf
, &bufsize
, &count
, &c
, fi
) == NULL
)
1437 if (count
== 0 && *buf
== '\n')
1439 fwrite(buf
, sizeof *buf
, c
, fo
);
1444 perror(catgets(catd
, CATSET
, 188, "temporary mail file"));
1451 resend_msg(struct message
*mp
, struct name
*to
, int add_resent
)
1453 FILE *ibuf
, *nfo
, *nfi
;
1456 enum okay ok
= STOP
;
1458 memset(&head
, 0, sizeof head
);
1459 if ((to
= checkaddrs(to
)) == NULL
) {
1463 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
1465 perror(catgets(catd
, CATSET
, 189, "temporary mail file"));
1468 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
1475 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1478 to
= fixhead(&head
, to
);
1479 if (infix_resend(ibuf
, nfo
, mp
, head
.h_to
, add_resent
) != 0) {
1482 savedeadletter(nfi
);
1483 fputs(catgets(catd
, CATSET
, 190,
1484 ". . . message not sent.\n"), stderr
);
1492 to
= outof(to
, nfi
, &head
);
1494 savedeadletter(nfi
);
1496 if (count(to
) != 0) {
1497 if (value("record-resent") == NULL
||
1498 mightrecord(nfi
, to
, 0) == OKAY
)
1499 ok
= transfer(to
, head
.h_smopts
, nfi
, NULL
);
1500 } else if (senderr
== 0)