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";
55 * Mail -- a mail program
60 static char *send_boundary
;
62 static char *getencoding(enum conversion convert
);
63 static struct name
*fixhead(struct header
*hp
, struct name
*tolist
);
64 static int put_signature(FILE *fo
, int convert
);
65 static int attach_file1(struct attachment
*ap
, FILE *fo
, int dosign
);
66 static int attach_file(struct attachment
*ap
, FILE *fo
, int dosign
);
67 static int attach_message(struct attachment
*ap
, FILE *fo
, int dosign
);
68 static int make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
69 const char *contenttype
, const char *charset
, int dosign
);
70 static FILE *infix(struct header
*hp
, FILE *fi
, int dosign
);
71 static int savemail(char *name
, FILE *fi
);
72 static int sendmail_internal(void *v
, int recipient_record
);
73 static enum okay
transfer(struct name
*to
, struct name
*mailargs
, FILE *input
,
75 static enum okay
start_mta(struct name
*to
, struct name
*mailargs
, FILE *input
,
77 static void message_id(FILE *fo
, struct header
*hp
);
78 static int fmt(char *str
, struct name
*np
, FILE *fo
, int comma
,
79 int dropinvalid
, int domime
);
80 static int infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
,
81 struct name
*to
, int add_resent
);
84 * Generate a boundary for MIME multipart messages.
89 static char bound
[70];
93 snprintf(bound
, sizeof bound
, "=_%lx.%s", (long)now
, getrandstring(48));
94 send_boundary
= bound
;
99 * Get an encoding flag based on the given string.
102 getencoding(enum conversion convert
)
110 return "quoted-printable";
121 * Fix the header by glopping all of the expanded names from
122 * the distribution list into the appropriate fields.
125 fixhead(struct header
*hp
, struct name
*tolist
)
132 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
)
133 if ((np
->n_type
& GMASK
) == GTO
)
134 hp
->h_to
= cat(hp
->h_to
, ndup(np
, GFULL
));
135 else if ((np
->n_type
& GMASK
) == GCC
)
136 hp
->h_cc
= cat(hp
->h_cc
, ndup(np
, GFULL
));
137 else if ((np
->n_type
& GMASK
) == GBCC
)
138 hp
->h_bcc
= cat(hp
->h_bcc
, ndup(np
, GFULL
));
144 * Do not change, you get incorrect base64 encodings else!
146 #define INFIX_BUF 972
149 * Put the signature file at fo.
152 put_signature(FILE *fo
, int convert
)
154 char *sig
, buf
[INFIX_BUF
], c
= '\n';
158 sig
= value("signature");
159 if (sig
== NULL
|| *sig
== '\0')
163 if ((fsig
= Fopen(sig
, "r")) == NULL
) {
167 while ((sz
= fread(buf
, sizeof *buf
, INFIX_BUF
, fsig
)) != 0) {
169 if (mime_write(buf
, sz
, fo
, convert
, TD_NONE
,
170 NULL
, (size_t)0, NULL
, NULL
)
189 * Write an attachment to the file buffer, converting to MIME.
192 attach_file1(struct attachment
*ap
, FILE *fo
, int dosign
)
195 char *charset
= NULL
, *contenttype
= NULL
, *basename
;
196 enum conversion convert
= CONV_TOB64
;
198 enum mimeclean isclean
;
201 size_t bufsize
, count
;
207 if ((fi
= Fopen(ap
->a_name
, "r")) == NULL
) {
211 if ((basename
= strrchr(ap
->a_name
, '/')) == NULL
)
212 basename
= ap
->a_name
;
215 if (ap
->a_content_type
)
216 contenttype
= ap
->a_content_type
;
218 contenttype
= mime_filecontent(basename
);
220 charset
= ap
->a_charset
;
221 convert
= get_mime_convert(fi
, &contenttype
, &charset
, &isclean
,
226 send_boundary
, contenttype
);
230 fprintf(fo
, ";\n charset=%s\n", charset
);
231 if (ap
->a_content_disposition
== NULL
)
232 ap
->a_content_disposition
= "attachment";
233 fprintf(fo
, "Content-Transfer-Encoding: %s\n"
234 "Content-Disposition: %s;\n"
236 getencoding(convert
),
237 ap
->a_content_disposition
);
238 mime_write(basename
, strlen(basename
), fo
,
239 CONV_TOHDR
, TD_NONE
, NULL
, (size_t)0, NULL
, NULL
);
240 fwrite("\"\n", sizeof (char), 2, fo
);
241 if (ap
->a_content_id
)
242 fprintf(fo
, "Content-ID: %s\n", ap
->a_content_id
);
243 if (ap
->a_content_description
)
244 fprintf(fo
, "Content-Description: %s\n",
245 ap
->a_content_description
);
248 if (iconvd
!= (iconv_t
)-1) {
250 iconvd
= (iconv_t
)-1;
253 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
254 ascncasecmp(contenttype
, "text/", 5) == 0 &&
255 isclean
& MIME_HIGHBIT
&&
257 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1 &&
260 fprintf(stderr
, catgets(catd
, CATSET
, 179,
261 "Cannot convert from %s to %s\n"), tcs
, charset
);
263 perror("iconv_open");
268 #endif /* HAVE_ICONV */
269 buf
= smalloc(bufsize
= INFIX_BUF
);
270 if (convert
== CONV_TOQP
272 || iconvd
!= (iconv_t
)-1
279 if (convert
== CONV_TOQP
281 || iconvd
!= (iconv_t
)-1
284 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
288 if ((sz
= fread(buf
, sizeof *buf
, bufsize
, fi
)) == 0)
292 if (mime_write(buf
, sz
, fo
, convert
, TD_ICONV
,
293 NULL
, (size_t)0, NULL
, NULL
) == 0)
296 if (convert
== CONV_TOQP
&& lastc
!= '\n')
297 fwrite("=\n", 1, 2, fo
);
306 * Try out different character set conversions to attach a file.
309 attach_file(struct attachment
*ap
, FILE *fo
, int dosign
)
311 char *_wantcharset
, *charsets
, *ncs
;
312 size_t offs
= ftell(fo
);
314 if (ap
->a_charset
|| (charsets
= value("sendcharsets")) == NULL
)
315 return attach_file1(ap
, fo
, dosign
);
316 _wantcharset
= wantcharset
;
317 wantcharset
= savestr(charsets
);
318 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
320 try: if (attach_file1(ap
, fo
, dosign
) != 0) {
321 if (errno
== EILSEQ
|| errno
== EINVAL
) {
325 fseek(fo
, offs
, SEEK_SET
);
329 if (wantcharset
== (char *)-1)
332 wantcharset
= (char *)-1;
334 fseek(fo
, offs
, SEEK_SET
);
340 wantcharset
= _wantcharset
;
345 * Attach a message to the file buffer.
348 attach_message(struct attachment
*ap
, FILE *fo
, int dosign
)
353 fprintf(fo
, "\n--%s\n"
354 "Content-Type: message/rfc822\n"
355 "Content-Disposition: inline\n\n", send_boundary
);
356 mp
= &message
[ap
->a_msgno
- 1];
358 if (send(mp
, fo
, 0, NULL
, SEND_RFC822
, NULL
) < 0)
364 * Generate the body of a MIME multipart message.
367 make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
368 const char *contenttype
, const char *charset
, int dosign
)
370 struct attachment
*att
;
372 fputs("This is a multi-part message in MIME format.\n", fo
);
373 if (fsize(fi
) != 0) {
375 size_t sz
, bufsize
, count
;
377 fprintf(fo
, "\n--%s\n", send_boundary
);
378 fprintf(fo
, "Content-Type: %s", contenttype
);
380 fprintf(fo
, "; charset=%s", charset
);
381 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n"
382 "Content-Disposition: inline\n\n",
383 getencoding(convert
));
384 buf
= smalloc(bufsize
= INFIX_BUF
);
385 if (convert
== CONV_TOQP
387 || iconvd
!= (iconv_t
)-1
388 #endif /* HAVE_ICONV */
394 if (convert
== CONV_TOQP
396 || iconvd
!= (iconv_t
)-1
397 #endif /* HAVE_ICONV */
399 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
403 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
408 if (mime_write(buf
, sz
, fo
, convert
,
409 TD_ICONV
, NULL
, (size_t)0,
421 put_signature(fo
, convert
);
423 for (att
= hp
->h_attach
; att
!= NULL
; att
= att
->a_flink
) {
425 if (attach_message(att
, fo
, dosign
) != 0)
428 if (attach_file(att
, fo
, dosign
) != 0)
432 /* the final boundary with two attached dashes */
433 fprintf(fo
, "\n--%s--\n", send_boundary
);
438 * Prepend a header in front of the collected stuff
439 * and return the new file.
442 infix(struct header
*hp
, FILE *fi
, int dosign
)
447 char *tcs
, *convhdr
= NULL
;
449 enum mimeclean isclean
;
450 enum conversion convert
;
451 char *charset
= NULL
, *contenttype
= NULL
;
454 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
455 perror(catgets(catd
, CATSET
, 178, "temporary mail file"));
458 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
465 convert
= get_mime_convert(fi
, &contenttype
, &charset
,
469 if ((convhdr
= need_hdrconv(hp
, GTO
|GSUBJECT
|GCC
|GBCC
|GIDENT
)) != 0) {
470 if (iconvd
!= (iconv_t
)-1)
472 if ((iconvd
= iconv_open_ft(convhdr
, tcs
)) == (iconv_t
)-1
475 fprintf(stderr
, catgets(catd
, CATSET
, 179,
476 "Cannot convert from %s to %s\n"), tcs
, convhdr
);
478 perror("iconv_open");
483 #endif /* HAVE_ICONV */
485 GTO
|GSUBJECT
|GCC
|GBCC
|GNL
|GCOMMA
|GUA
|GMIME
486 |GMSGID
|GIDENT
|GREF
|GDATE
,
487 SEND_MBOX
, convert
, contenttype
, charset
)) {
491 if (iconvd
!= (iconv_t
)-1) {
493 iconvd
= (iconv_t
)-1;
499 if (convhdr
&& iconvd
!= (iconv_t
)-1) {
501 iconvd
= (iconv_t
)-1;
503 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
504 ascncasecmp(contenttype
, "text/", 5) == 0 &&
505 isclean
& MIME_HIGHBIT
&&
507 if (iconvd
!= (iconv_t
)-1)
509 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1
512 fprintf(stderr
, catgets(catd
, CATSET
, 179,
513 "Cannot convert from %s to %s\n"), tcs
, charset
);
515 perror("iconv_open");
521 if (hp
->h_attach
!= NULL
) {
522 if (make_multipart(hp
, convert
, fi
, nfo
,
523 contenttype
, charset
, dosign
) != 0) {
527 if (iconvd
!= (iconv_t
)-1) {
529 iconvd
= (iconv_t
)-1;
535 size_t sz
, bufsize
, count
;
538 if (convert
== CONV_TOQP
540 || iconvd
!= (iconv_t
)-1
541 #endif /* HAVE_ICONV */
546 buf
= smalloc(bufsize
= INFIX_BUF
);
548 if (convert
== CONV_TOQP
550 || iconvd
!= (iconv_t
)-1
551 #endif /* HAVE_ICONV */
553 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
557 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
562 if (mime_write(buf
, sz
, nfo
, convert
,
563 TD_ICONV
, NULL
, (size_t)0,
568 if (iconvd
!= (iconv_t
)-1) {
570 iconvd
= (iconv_t
)-1;
577 if (convert
== CONV_TOQP
&& lastc
!= '\n')
578 fwrite("=\n", 1, 2, nfo
);
584 if (iconvd
!= (iconv_t
)-1) {
586 iconvd
= (iconv_t
)-1;
592 put_signature(nfo
, convert
);
595 if (iconvd
!= (iconv_t
)-1) {
597 iconvd
= (iconv_t
)-1;
602 perror(catgets(catd
, CATSET
, 180, "temporary mail file"));
615 * Save the outgoing mail on the passed file.
620 savemail(char *name
, FILE *fi
)
624 size_t bufsize
, buflen
, count
;
630 buf
= smalloc(bufsize
= LINESIZE
);
632 if ((fo
= Zopen(name
, "a+", NULL
)) == NULL
) {
633 if ((fo
= Zopen(name
, "wx", NULL
)) == NULL
) {
639 if (fseek(fo
, -2L, SEEK_END
) == 0) {
640 switch (fread(buf
, sizeof *buf
, 2, fo
)) {
642 if (buf
[1] != '\n') {
665 fprintf(fo
, "From %s %s", myname
, ctime(&now
));
670 while (fgetline(&buf
, &bufsize
, &count
, &buflen
, fi
, 0) != NULL
) {
675 if (strncmp(p
, "From ", 5) == 0)
676 /* we got a masked From line */
678 } else if (strncmp(buf
, "From ", 5) == 0)
680 fwrite(buf
, sizeof *buf
, buflen
, fo
);
682 if (buflen
&& *(buf
+ buflen
- 1) != '\n')
699 * Interface between the argument list and the mail1 routine
700 * which does all the dirty work.
703 mail(struct name
*to
, struct name
*cc
, struct name
*bcc
,
704 struct name
*smopts
, char *subject
, struct attachment
*attach
,
705 char *quotefile
, int recipient_record
, int tflag
, int Eflag
)
710 memset(&head
, 0, sizeof head
);
711 /* The given subject may be in RFC1522 format. */
712 if (subject
!= NULL
) {
714 in
.l
= strlen(subject
);
715 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
716 head
.h_subject
= out
.s
;
723 head
.h_attach
= attach
;
724 head
.h_smopts
= smopts
;
725 mail1(&head
, 0, NULL
, quotefile
, recipient_record
, 0, tflag
, Eflag
);
732 * Send mail to a bunch of user names. The interface is through
733 * the mail routine below.
736 sendmail_internal(void *v
, int recipient_record
)
742 memset(&head
, 0, sizeof head
);
743 head
.h_to
= extract(str
, GTO
|GFULL
);
744 Eflag
= value("skipemptybody") != NULL
;
745 mail1(&head
, 0, NULL
, NULL
, recipient_record
, 0, 0, Eflag
);
752 return sendmail_internal(v
, 0);
758 return sendmail_internal(v
, 1);
762 transfer(struct name
*to
, struct name
*mailargs
, FILE *input
, struct header
*hp
)
764 char o
[LINESIZE
], *cp
;
765 struct name
*np
, *nt
;
772 snprintf(o
, sizeof o
, "smime-encrypt-%s", np
->n_name
);
773 if ((cp
= value(o
)) != NULL
) {
774 if ((ef
= smime_encrypt(input
, cp
, np
->n_name
)) != 0) {
775 nt
= nalloc(np
->n_name
,
776 np
->n_type
& ~(GFULL
|GSKIN
));
777 if (start_mta(nt
, mailargs
, ef
, hp
) != OKAY
)
781 fprintf(stderr
, "Message not sent to <%s>\n",
787 np
->n_flink
->n_blink
= np
->n_blink
;
789 np
->n_blink
->n_flink
= np
->n_flink
;
799 if (value("smime-force-encryption") ||
800 start_mta(to
, mailargs
, input
, hp
) != OKAY
)
807 * Start the Mail Transfer Agent
808 * mailing to namelist and stdin redirected to input.
811 start_mta(struct name
*to
, struct name
*mailargs
, FILE *input
,
814 char **args
= NULL
, **t
;
818 char *user
= NULL
, *password
= NULL
, *skinned
= NULL
;
825 if ((smtp
= value("smtp")) == NULL
) {
826 args
= unpack(cat(mailargs
, to
));
827 if (debug
|| value("debug")) {
828 printf(catgets(catd
, CATSET
, 181,
829 "Sendmail arguments:"));
830 for (t
= args
; *t
!= NULL
; t
++)
831 printf(" \"%s\"", *t
);
838 skinned
= skin(myorigin(hp
));
839 if ((user
= smtp_auth_var("-user", skinned
)) != NULL
&&
840 (password
= smtp_auth_var("-password",
842 password
= getpassword(&otio
, &reset_tio
, NULL
);
846 * Fork, set up the temporary mail file as standard
847 * input for "mail", and exec with the user list we generated
850 if ((pid
= fork()) == -1) {
852 savedeadletter(input
);
858 sigaddset(&nset
, SIGHUP
);
859 sigaddset(&nset
, SIGINT
);
860 sigaddset(&nset
, SIGQUIT
);
861 sigaddset(&nset
, SIGTSTP
);
862 sigaddset(&nset
, SIGTTIN
);
863 sigaddset(&nset
, SIGTTOU
);
864 freopen("/dev/null", "r", stdin
);
866 prepare_child(&nset
, 0, 1);
867 if (smtp_mta(smtp
, to
, input
, hp
,
868 user
, password
, skinned
) == 0)
871 prepare_child(&nset
, fileno(input
), -1);
872 if ((cp
= value("sendmail")) != NULL
)
879 savedeadletter(input
);
880 fputs(catgets(catd
, CATSET
, 182,
881 ". . . message not sent.\n"), stderr
);
884 if (value("verbose") != NULL
|| value("sendwait") || debug
886 if (wait_child(pid
) == 0)
898 * Record outgoing mail if instructed to do so.
901 mightrecord(FILE *fp
, struct name
*to
, int recipient_record
)
905 if (recipient_record
) {
906 cq
= skin(to
->n_name
);
907 cp
= salloc(strlen(cq
) + 1);
909 for (cq
= cp
; *cq
&& *cq
!= '@'; cq
++);
912 cp
= value("record");
915 if (value("outfolder") && *ep
!= '/' && *ep
!= '+' &&
916 which_protocol(ep
) == PROTO_FILE
) {
917 cq
= salloc(strlen(cp
) + 2);
923 if (savemail(ep
, fp
) != 0) {
925 "Error while saving message to %s - "
926 "message not sent\n", ep
);
937 * Mail a message on standard input to the people indicated
938 * in the passed header. (Internal interface).
941 mail1(struct header
*hp
, int printheaders
, struct message
*quote
,
942 char *quotefile
, int recipient_record
, int doprefix
, int tflag
,
949 char *charsets
, *ncs
= NULL
, *cp
;
952 if ((hp
->h_to
= checkaddrs(hp
->h_to
)) == NULL
) {
957 if ((cp
= value("autocc")) != NULL
&& *cp
)
958 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(sextract(cp
, GCC
|GFULL
)));
959 if ((cp
= value("autobcc")) != NULL
&& *cp
)
960 hp
->h_bcc
= cat(hp
->h_bcc
,
961 checkaddrs(sextract(cp
, GBCC
|GFULL
)));
963 * Collect user's mail from standard input.
964 * Get the result as mtf.
966 if ((mtf
= collect(hp
, printheaders
, quote
, quotefile
, doprefix
,
969 if (value("interactive") != NULL
) {
970 if (((value("bsdcompat") || value("askatend"))
971 && (value("askcc") != NULL
||
972 value("askbcc") != NULL
)) ||
973 value("askattach") != NULL
||
974 value("asksign") != NULL
) {
975 if (value("askcc") != NULL
)
977 if (value("askbcc") != NULL
)
979 if (value("askattach") != NULL
)
980 hp
->h_attach
= edit_attachments(hp
->h_attach
);
981 if (value("asksign") != NULL
)
982 dosign
= yorn("Sign this message (y/n)? ");
984 printf(catgets(catd
, CATSET
, 183, "EOT\n"));
988 if (fsize(mtf
) == 0) {
991 if (hp
->h_subject
== NULL
)
992 printf(catgets(catd
, CATSET
, 184,
993 "No message, no subject; hope that's ok\n"));
994 else if (value("bsdcompat") || value("bsdmsgs"))
995 printf(catgets(catd
, CATSET
, 185,
996 "Null message body; hope that's ok\n"));
999 if (value("smime-sign") != NULL
)
1005 * Now, take the user names from the combined
1006 * to and cc lists and do all the alias
1010 if ((to
= usermap(cat(hp
->h_bcc
, cat(hp
->h_to
, hp
->h_cc
)))) == NULL
) {
1011 printf(catgets(catd
, CATSET
, 186, "No recipients specified\n"));
1014 to
= fixhead(hp
, to
);
1015 if (hp
->h_charset
) {
1016 wantcharset
= hp
->h_charset
;
1019 hloop
: wantcharset
= NULL
;
1020 if ((charsets
= value("sendcharsets")) != NULL
) {
1021 wantcharset
= savestr(charsets
);
1022 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
1025 try: if ((nmtf
= infix(hp
, mtf
, dosign
)) == NULL
) {
1026 if (hp
->h_charset
&& (errno
== EILSEQ
|| errno
== EINVAL
)) {
1028 hp
->h_charset
= NULL
;
1031 if (errno
== EILSEQ
|| errno
== EINVAL
) {
1037 if (wantcharset
&& value("interactive") == NULL
) {
1038 if (wantcharset
== (char *)-1)
1042 wantcharset
= (char *)-1;
1047 /* fprintf(stderr, ". . . message lost, sorry.\n"); */
1051 savedeadletter(mtf
);
1052 fputs(catgets(catd
, CATSET
, 187,
1053 ". . . message not sent.\n"), stderr
);
1058 if ((nmtf
= smime_sign(mtf
, hp
)) == NULL
)
1064 * Look through the recipient list for names with /'s
1065 * in them which we write to as files directly.
1067 to
= outof(to
, mtf
, hp
);
1069 savedeadletter(mtf
);
1071 if (count(to
) == 0) {
1076 if (mightrecord(mtf
, to
, recipient_record
) != OKAY
)
1078 ok
= transfer(to
, hp
->h_smopts
, mtf
, hp
);
1085 * Create a Message-Id: header field.
1086 * Use either the host name or the from address.
1089 message_id(FILE *fo
, struct header
*hp
)
1095 if ((cp
= value("hostname")) != NULL
)
1096 fprintf(fo
, "Message-ID: <%lx.%s@%s>\n",
1097 (long)now
, getrandstring(24), cp
);
1098 else if ((cp
= skin(myorigin(hp
))) != NULL
&& strchr(cp
, '@') != NULL
)
1099 fprintf(fo
, "Message-ID: <%lx.%s%%%s>\n",
1100 (long)now
, getrandstring(16), cp
);
1103 static const char *weekday_names
[] = {
1104 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1107 const char *month_names
[] = {
1108 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1109 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
1113 * Create a Date: header field.
1114 * We compare the localtime() and gmtime() results to get the timezone,
1115 * because numeric timezones are easier to read and because $TZ is
1116 * not set on most GNU systems.
1119 mkdate(FILE *fo
, const char *field
)
1123 int tzdiff
, tzdiff_hour
, tzdiff_min
;
1126 tzdiff
= t
- mktime(gmtime(&t
));
1127 tzdiff_hour
= (int)(tzdiff
/ 60);
1128 tzdiff_min
= tzdiff_hour
% 60;
1130 tmptr
= localtime(&t
);
1131 if (tmptr
->tm_isdst
> 0)
1133 return fprintf(fo
, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1135 weekday_names
[tmptr
->tm_wday
],
1136 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
],
1137 tmptr
->tm_year
+ 1900, tmptr
->tm_hour
,
1138 tmptr
->tm_min
, tmptr
->tm_sec
,
1139 tzdiff_hour
* 100 + tzdiff_min
);
1143 putname(char *line
, enum gfield w
, enum sendaction action
, int *gotcha
,
1144 char *prefix
, FILE *fo
, struct name
**xp
)
1148 np
= sextract(line
, GEXTRA
|GFULL
);
1153 if (fmt(prefix
, np
, fo
, w
&(GCOMMA
|GFILES
), 0, action
!= SEND_TODISP
))
1160 #define FMT_CC_AND_BCC { \
1161 if (hp->h_cc != NULL && w & GCC) { \
1162 if (fmt("Cc:", hp->h_cc, fo, \
1163 w&(GCOMMA|GFILES), 0, \
1164 action!=SEND_TODISP)) \
1168 if (hp->h_bcc != NULL && w & GBCC) { \
1169 if (fmt("Bcc:", hp->h_bcc, fo, \
1170 w&(GCOMMA|GFILES), 0, \
1171 action!=SEND_TODISP)) \
1177 * Dump the to, subject, cc header on the
1178 * passed file buffer.
1181 puthead(struct header
*hp
, FILE *fo
, enum gfield w
,
1182 enum sendaction action
, enum conversion convert
,
1183 char *contenttype
, char *charset
)
1186 char *addr
/*, *cp*/;
1188 struct name
*np
, *fromfield
= NULL
, *senderfield
= NULL
;
1191 if (value("stealthmua"))
1197 mkdate(fo
, "Date"), gotcha
++;
1200 if (hp
->h_from
!= NULL
) {
1201 if (fmt("From:", hp
->h_from
, fo
, w
&(GCOMMA
|GFILES
), 0,
1202 action
!=SEND_TODISP
))
1205 fromfield
= hp
->h_from
;
1206 } else if ((addr
= myaddrs(hp
)) != NULL
)
1207 if (putname(addr
, w
, action
, &gotcha
, "From:", fo
,
1210 if (((addr
= hp
->h_organization
) != NULL
||
1211 (addr
= value("ORGANIZATION")) != NULL
)
1212 && strlen(addr
) > 0) {
1213 fwrite("Organization: ", sizeof (char), 14, fo
);
1214 if (mime_write(addr
, strlen(addr
), fo
,
1215 action
== SEND_TODISP
?
1216 CONV_NONE
:CONV_TOHDR
,
1217 action
== SEND_TODISP
?
1218 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1225 if (hp
->h_replyto
!= NULL
) {
1226 if (fmt("Reply-To:", hp
->h_replyto
, fo
,
1227 w
&(GCOMMA
|GFILES
), 0,
1228 action
!=SEND_TODISP
))
1231 } else if ((addr
= value("replyto")) != NULL
)
1232 if (putname(addr
, w
, action
, &gotcha
, "Reply-To:", fo
,
1235 if (hp
->h_sender
!= NULL
) {
1236 if (fmt("Sender:", hp
->h_sender
, fo
,
1237 w
&(GCOMMA
|GFILES
), 0,
1238 action
!=SEND_TODISP
))
1241 senderfield
= hp
->h_sender
;
1242 } else if ((addr
= value("sender")) != NULL
)
1243 if (putname(addr
, w
, action
, &gotcha
, "Sender:", fo
,
1246 if (check_from_and_sender(fromfield
, senderfield
))
1249 if (hp
->h_to
!= NULL
&& w
& GTO
) {
1250 if (fmt("To:", hp
->h_to
, fo
, w
&(GCOMMA
|GFILES
), 0,
1251 action
!=SEND_TODISP
))
1255 if (value("bsdcompat") == NULL
&& value("bsdorder") == NULL
)
1257 if (hp
->h_subject
!= NULL
&& w
& GSUBJECT
) {
1258 fwrite("Subject: ", sizeof (char), 9, fo
);
1259 if (ascncasecmp(hp
->h_subject
, "re: ", 4) == 0) {
1260 fwrite("Re: ", sizeof (char), 4, fo
);
1261 if (strlen(hp
->h_subject
+ 4) > 0 &&
1262 mime_write(hp
->h_subject
+ 4,
1263 strlen(hp
->h_subject
+ 4),
1264 fo
, action
== SEND_TODISP
?
1265 CONV_NONE
:CONV_TOHDR
,
1266 action
== SEND_TODISP
?
1267 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1271 } else if (*hp
->h_subject
) {
1272 if (mime_write(hp
->h_subject
,
1273 strlen(hp
->h_subject
),
1274 fo
, action
== SEND_TODISP
?
1275 CONV_NONE
:CONV_TOHDR
,
1276 action
== SEND_TODISP
?
1277 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1283 fwrite("\n", sizeof (char), 1, fo
);
1285 if (value("bsdcompat") || value("bsdorder"))
1287 if (w
& GMSGID
&& stealthmua
== 0)
1288 message_id(fo
, hp
), gotcha
++;
1289 if (hp
->h_ref
!= NULL
&& w
& GREF
) {
1290 fmt("References:", hp
->h_ref
, fo
, 0, 1, 0);
1291 if ((np
= hp
->h_ref
) != NULL
&& np
->n_name
) {
1294 if (mime_name_invalid(np
, 0) == 0) {
1295 fprintf(fo
, "In-Reply-To: %s\n", np
->n_name
);
1300 if (w
& GUA
&& stealthmua
== 0)
1301 fprintf(fo
, "User-Agent: S-nail %s\n",
1304 fputs("MIME-Version: 1.0\n", fo
), gotcha
++;
1305 if (hp
->h_attach
!= NULL
) {
1307 fprintf(fo
, "Content-Type: multipart/mixed;\n"
1308 " boundary=\"%s\"\n", send_boundary
);
1310 fprintf(fo
, "Content-Type: %s", contenttype
);
1312 fprintf(fo
, "; charset=%s", charset
);
1313 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n",
1314 getencoding(convert
));
1317 if (gotcha
&& w
& GNL
)
1323 * Format the given header line to not exceed 72 characters.
1326 fmt(char *str
, struct name
*np
, FILE *fo
, int flags
, int dropinvalid
,
1329 int col
, len
, count
= 0;
1330 int is_to
= 0, comma
;
1332 comma
= flags
&GCOMMA
? 1 : 0;
1335 fwrite(str
, sizeof *str
, strlen(str
), fo
);
1336 if (((flags
&GFILES
) == 0 &&
1337 col
== 3 && asccasecmp(str
, "to:") == 0) ||
1338 (col
== 3 && asccasecmp(str
, "cc:") == 0) ||
1339 (col
== 4 && asccasecmp(str
, "bcc:") == 0) ||
1341 asccasecmp(str
, "Resent-To:") == 0))
1344 for (; np
!= NULL
; np
= np
->n_flink
) {
1345 if (is_to
&& is_fileaddr(np
->n_name
))
1347 if (np
->n_flink
== NULL
)
1349 if (mime_name_invalid(np
, !dropinvalid
)) {
1355 len
= strlen(np
->n_fullname
);
1356 col
++; /* for the space */
1357 if (count
&& col
+ len
+ comma
> 72 && col
> 1) {
1362 len
= mime_write(np
->n_fullname
,
1364 domime
?CONV_TOHDR_A
:CONV_NONE
,
1365 TD_ICONV
, NULL
, (size_t)0,
1367 if (comma
&& !(is_to
&& is_fileaddr(np
->n_flink
->n_name
)))
1377 * Rewrite a message for resending, adding the Resent-Headers.
1380 infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
, struct name
*to
,
1384 char *buf
= NULL
, *cp
/*, *cp2*/;
1385 size_t c
, bufsize
= 0;
1386 struct name
*fromfield
= NULL
, *senderfield
= NULL
;
1390 * Write the Resent-Fields.
1393 fputs("Resent-", fo
);
1395 if ((cp
= myaddrs(NULL
)) != NULL
) {
1396 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1397 "Resent-From:", fo
, &fromfield
))
1400 if ((cp
= value("sender")) != NULL
) {
1401 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1402 "Resent-Sender:", fo
, &senderfield
))
1405 if (fmt("Resent-To:", to
, fo
, 1, 1, 0)) {
1410 if (value("stealthmua") == NULL
) {
1411 fputs("Resent-", fo
);
1412 message_id(fo
, NULL
);
1415 if (check_from_and_sender(fromfield
, senderfield
))
1418 * Write the original headers.
1421 if ((cp
= foldergets(&buf
, &bufsize
, &count
, &c
, fi
)) == NULL
)
1423 if (ascncasecmp("status: ", buf
, 8) != 0
1424 && strncmp("From ", buf
, 5) != 0) {
1425 fwrite(buf
, sizeof *buf
, c
, fo
);
1427 if (count
> 0 && *buf
== '\n')
1431 * Write the message body.
1434 if (foldergets(&buf
, &bufsize
, &count
, &c
, fi
) == NULL
)
1436 if (count
== 0 && *buf
== '\n')
1438 fwrite(buf
, sizeof *buf
, c
, fo
);
1443 perror(catgets(catd
, CATSET
, 188, "temporary mail file"));
1450 resend_msg(struct message
*mp
, struct name
*to
, int add_resent
)
1452 FILE *ibuf
, *nfo
, *nfi
;
1455 enum okay ok
= STOP
;
1457 memset(&head
, 0, sizeof head
);
1458 if ((to
= checkaddrs(to
)) == NULL
) {
1462 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
1464 perror(catgets(catd
, CATSET
, 189, "temporary mail file"));
1467 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
1474 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1477 to
= fixhead(&head
, to
);
1478 if (infix_resend(ibuf
, nfo
, mp
, head
.h_to
, add_resent
) != 0) {
1481 savedeadletter(nfi
);
1482 fputs(catgets(catd
, CATSET
, 190,
1483 ". . . message not sent.\n"), stderr
);
1491 to
= outof(to
, nfi
, &head
);
1493 savedeadletter(nfi
);
1495 if (count(to
) != 0) {
1496 if (value("record-resent") == NULL
||
1497 mightrecord(nfi
, to
, 0) == OKAY
)
1498 ok
= transfer(to
, head
.h_smopts
, nfi
, NULL
);
1499 } else if (senderr
== 0)