2 * S-nail - 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
49 * Mail -- a mail program
54 static char *send_boundary
;
56 static char *getencoding(enum conversion convert
);
57 static struct name
*fixhead(struct header
*hp
, struct name
*tolist
);
58 static int put_signature(FILE *fo
, int convert
);
59 static int attach_file1(struct attachment
*ap
, FILE *fo
, int dosign
);
60 static int attach_file(struct attachment
*ap
, FILE *fo
, int dosign
);
61 static int attach_message(struct attachment
*ap
, FILE *fo
, int dosign
);
62 static int make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
63 const char *contenttype
, const char *charset
, int dosign
);
64 static FILE *infix(struct header
*hp
, FILE *fi
, int dosign
);
65 static int savemail(char *name
, FILE *fi
);
66 static int sendmail_internal(void *v
, int recipient_record
);
67 static enum okay
transfer(struct name
*to
, FILE *input
, struct header
*hp
);
68 static char ** prepare_mta_args(struct name
*to
);
69 static enum okay
start_mta(struct name
*to
, FILE *input
, struct header
*hp
);
70 static void message_id(FILE *fo
, struct header
*hp
);
71 static int fmt(char *str
, struct name
*np
, FILE *fo
, int comma
,
72 int dropinvalid
, int domime
);
73 static int infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
,
74 struct name
*to
, int add_resent
);
77 * Generate a boundary for MIME multipart messages.
82 static char bound
[70];
86 snprintf(bound
, sizeof bound
, "=_%lx.%s", (long)now
, getrandstring(48));
87 send_boundary
= bound
;
92 * Get an encoding flag based on the given string.
95 getencoding(enum conversion convert
)
103 return "quoted-printable";
114 * Fix the header by glopping all of the expanded names from
115 * the distribution list into the appropriate fields.
118 fixhead(struct header
*hp
, struct name
*tolist
) /* TODO !HAVE_ASSERTS legacy*/
122 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
123 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
)
124 if (np
->n_type
& GDEL
) {
126 assert(0); /* Shouldn't happen here, but later on :)) */
130 } else switch (np
->n_type
& GMASK
) {
132 hp
->h_to
= cat(hp
->h_to
, ndup(np
, np
->n_type
|GFULL
));
135 hp
->h_cc
= cat(hp
->h_cc
, ndup(np
, np
->n_type
|GFULL
));
138 hp
->h_bcc
= cat(hp
->h_bcc
, ndup(np
, np
->n_type
|GFULL
));
148 * Do not change, you get incorrect base64 encodings else!
150 #define INFIX_BUF 972
153 * Put the signature file at fo.
156 put_signature(FILE *fo
, int convert
)
158 char *sig
, buf
[INFIX_BUF
], c
= '\n';
162 sig
= value("signature");
163 if (sig
== NULL
|| *sig
== '\0')
166 sig
= file_expand(sig
);
167 if ((fsig
= Fopen(sig
, "r")) == NULL
) {
171 while ((sz
= fread(buf
, sizeof *buf
, INFIX_BUF
, fsig
)) != 0) {
173 if (mime_write(buf
, sz
, fo
, convert
, TD_NONE
,
174 NULL
, (size_t)0, NULL
, NULL
)
193 * Write an attachment to the file buffer, converting to MIME.
196 attach_file1(struct attachment
*ap
, FILE *fo
, int dosign
)
199 char *charset
= NULL
, *contenttype
= NULL
, *basename
;
200 enum conversion convert
= CONV_TOB64
;
202 enum mimeclean isclean
;
205 size_t bufsize
, count
;
211 if ((fi
= Fopen(ap
->a_name
, "r")) == NULL
) {
215 if ((basename
= strrchr(ap
->a_name
, '/')) == NULL
)
216 basename
= ap
->a_name
;
219 if (ap
->a_content_type
)
220 contenttype
= ap
->a_content_type
;
222 contenttype
= mime_filecontent(basename
);
224 charset
= ap
->a_charset
;
225 convert
= get_mime_convert(fi
, &contenttype
, &charset
, &isclean
,
230 send_boundary
, contenttype
);
234 fprintf(fo
, ";\n charset=%s\n", charset
);
235 if (ap
->a_content_disposition
== NULL
)
236 ap
->a_content_disposition
= "attachment";
237 fprintf(fo
, "Content-Transfer-Encoding: %s\n"
238 "Content-Disposition: %s;\n"
240 getencoding(convert
),
241 ap
->a_content_disposition
);
242 mime_write(basename
, strlen(basename
), fo
,
243 CONV_TOHDR
, TD_NONE
, NULL
, (size_t)0, NULL
, NULL
);
244 fwrite("\"\n", sizeof (char), 2, fo
);
245 if (ap
->a_content_id
)
246 fprintf(fo
, "Content-ID: %s\n", ap
->a_content_id
);
247 if (ap
->a_content_description
)
248 fprintf(fo
, "Content-Description: %s\n",
249 ap
->a_content_description
);
252 if (iconvd
!= (iconv_t
)-1) {
254 iconvd
= (iconv_t
)-1;
257 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
258 ascncasecmp(contenttype
, "text/", 5) == 0 &&
259 isclean
& MIME_HIGHBIT
&&
261 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1 &&
264 fprintf(stderr
, catgets(catd
, CATSET
, 179,
265 "Cannot convert from %s to %s\n"), tcs
, charset
);
267 perror("iconv_open");
272 #endif /* HAVE_ICONV */
273 buf
= smalloc(bufsize
= INFIX_BUF
);
274 if (convert
== CONV_TOQP
276 || iconvd
!= (iconv_t
)-1
283 if (convert
== CONV_TOQP
285 || iconvd
!= (iconv_t
)-1
288 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
292 if ((sz
= fread(buf
, sizeof *buf
, bufsize
, fi
)) == 0)
296 if (mime_write(buf
, sz
, fo
, convert
, TD_ICONV
,
297 NULL
, (size_t)0, NULL
, NULL
) == 0)
300 if (convert
== CONV_TOQP
&& lastc
!= '\n')
301 fwrite("=\n", 1, 2, fo
);
310 * Try out different character set conversions to attach a file.
313 attach_file(struct attachment
*ap
, FILE *fo
, int dosign
)
315 char *_wantcharset
, *charsets
, *ncs
;
316 size_t offs
= ftell(fo
);
318 if (ap
->a_charset
|| (charsets
= value("sendcharsets")) == NULL
)
319 return attach_file1(ap
, fo
, dosign
);
320 _wantcharset
= wantcharset
;
321 wantcharset
= savestr(charsets
);
322 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
324 try: if (attach_file1(ap
, fo
, dosign
) != 0) {
325 if (errno
== EILSEQ
|| errno
== EINVAL
) {
329 fseek(fo
, offs
, SEEK_SET
);
333 if (wantcharset
== (char *)-1)
336 wantcharset
= (char *)-1;
338 fseek(fo
, offs
, SEEK_SET
);
344 wantcharset
= _wantcharset
;
349 * Attach a message to the file buffer.
352 attach_message(struct attachment
*ap
, FILE *fo
, int dosign
)
357 fprintf(fo
, "\n--%s\n"
358 "Content-Type: message/rfc822\n"
359 "Content-Disposition: inline\n\n", send_boundary
);
360 mp
= &message
[ap
->a_msgno
- 1];
362 if (send(mp
, fo
, 0, NULL
, SEND_RFC822
, NULL
) < 0)
368 * Generate the body of a MIME multipart message.
371 make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
372 const char *contenttype
, const char *charset
, int dosign
)
374 struct attachment
*att
;
376 fputs("This is a multi-part message in MIME format.\n", fo
);
377 if (fsize(fi
) != 0) {
379 size_t sz
, bufsize
, count
;
381 fprintf(fo
, "\n--%s\n", send_boundary
);
382 fprintf(fo
, "Content-Type: %s", contenttype
);
384 fprintf(fo
, "; charset=%s", charset
);
385 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n"
386 "Content-Disposition: inline\n\n",
387 getencoding(convert
));
388 buf
= smalloc(bufsize
= INFIX_BUF
);
389 if (convert
== CONV_TOQP
391 || iconvd
!= (iconv_t
)-1
392 #endif /* HAVE_ICONV */
398 if (convert
== CONV_TOQP
400 || iconvd
!= (iconv_t
)-1
401 #endif /* HAVE_ICONV */
403 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
407 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
412 if (mime_write(buf
, sz
, fo
, convert
,
413 TD_ICONV
, NULL
, (size_t)0,
425 put_signature(fo
, convert
);
427 for (att
= hp
->h_attach
; att
!= NULL
; att
= att
->a_flink
) {
429 if (attach_message(att
, fo
, dosign
) != 0)
432 if (attach_file(att
, fo
, dosign
) != 0)
436 /* the final boundary with two attached dashes */
437 fprintf(fo
, "\n--%s--\n", send_boundary
);
442 * Prepend a header in front of the collected stuff
443 * and return the new file.
446 infix(struct header
*hp
, FILE *fi
, int dosign
)
451 char *tcs
, *convhdr
= NULL
;
453 enum mimeclean isclean
;
454 enum conversion convert
;
455 char *charset
= NULL
, *contenttype
= NULL
;
458 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
459 perror(catgets(catd
, CATSET
, 178, "temporary mail file"));
462 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
469 convert
= get_mime_convert(fi
, &contenttype
, &charset
,
473 if ((convhdr
= need_hdrconv(hp
, GTO
|GSUBJECT
|GCC
|GBCC
|GIDENT
)) != 0) {
474 if (iconvd
!= (iconv_t
)-1)
476 if ((iconvd
= iconv_open_ft(convhdr
, tcs
)) == (iconv_t
)-1
479 fprintf(stderr
, catgets(catd
, CATSET
, 179,
480 "Cannot convert from %s to %s\n"), tcs
, convhdr
);
482 perror("iconv_open");
487 #endif /* HAVE_ICONV */
489 GTO
|GSUBJECT
|GCC
|GBCC
|GNL
|GCOMMA
|GUA
|GMIME
490 |GMSGID
|GIDENT
|GREF
|GDATE
,
491 SEND_MBOX
, convert
, contenttype
, charset
)) {
495 if (iconvd
!= (iconv_t
)-1) {
497 iconvd
= (iconv_t
)-1;
503 if (convhdr
&& iconvd
!= (iconv_t
)-1) {
505 iconvd
= (iconv_t
)-1;
507 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
508 ascncasecmp(contenttype
, "text/", 5) == 0 &&
509 isclean
& MIME_HIGHBIT
&&
511 if (iconvd
!= (iconv_t
)-1)
513 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1
516 fprintf(stderr
, catgets(catd
, CATSET
, 179,
517 "Cannot convert from %s to %s\n"), tcs
, charset
);
519 perror("iconv_open");
525 if (hp
->h_attach
!= NULL
) {
526 if (make_multipart(hp
, convert
, fi
, nfo
,
527 contenttype
, charset
, dosign
) != 0) {
531 if (iconvd
!= (iconv_t
)-1) {
533 iconvd
= (iconv_t
)-1;
539 size_t sz
, bufsize
, count
;
542 if (convert
== CONV_TOQP
544 || iconvd
!= (iconv_t
)-1
545 #endif /* HAVE_ICONV */
550 buf
= smalloc(bufsize
= INFIX_BUF
);
552 if (convert
== CONV_TOQP
554 || iconvd
!= (iconv_t
)-1
555 #endif /* HAVE_ICONV */
557 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
561 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
566 if (mime_write(buf
, sz
, nfo
, convert
,
567 TD_ICONV
, NULL
, (size_t)0,
572 if (iconvd
!= (iconv_t
)-1) {
574 iconvd
= (iconv_t
)-1;
581 if (convert
== CONV_TOQP
&& lastc
!= '\n')
582 fwrite("=\n", 1, 2, nfo
);
588 if (iconvd
!= (iconv_t
)-1) {
590 iconvd
= (iconv_t
)-1;
596 put_signature(nfo
, convert
);
599 if (iconvd
!= (iconv_t
)-1) {
601 iconvd
= (iconv_t
)-1;
606 perror(catgets(catd
, CATSET
, 180, "temporary mail file"));
619 * Save the outgoing mail on the passed file.
624 savemail(char *name
, FILE *fi
)
628 size_t bufsize
, buflen
, count
;
634 buf
= smalloc(bufsize
= LINESIZE
);
636 if ((fo
= Zopen(name
, "a+", NULL
)) == NULL
) {
637 if ((fo
= Zopen(name
, "wx", NULL
)) == NULL
) {
643 if (fseek(fo
, -2L, SEEK_END
) == 0) {
644 switch (fread(buf
, sizeof *buf
, 2, fo
)) {
646 if (buf
[1] != '\n') {
669 fprintf(fo
, "From %s %s", myname
, ctime(&now
));
674 while (fgetline(&buf
, &bufsize
, &count
, &buflen
, fi
, 0) != NULL
) {
679 if (strncmp(p
, "From ", 5) == 0)
680 /* we got a masked From line */
682 } else if (strncmp(buf
, "From ", 5) == 0)
684 fwrite(buf
, sizeof *buf
, buflen
, fo
);
686 if (buflen
&& *(buf
+ buflen
- 1) != '\n')
703 * Interface between the argument list and the mail1 routine
704 * which does all the dirty work.
707 mail(struct name
*to
, struct name
*cc
, struct name
*bcc
,
708 char *subject
, struct attachment
*attach
,
709 char *quotefile
, int recipient_record
, int tflag
, int Eflag
)
714 memset(&head
, 0, sizeof head
);
715 /* The given subject may be in RFC1522 format. */
716 if (subject
!= NULL
) {
718 in
.l
= strlen(subject
);
719 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
720 head
.h_subject
= out
.s
;
727 head
.h_attach
= attach
;
728 mail1(&head
, 0, NULL
, quotefile
, recipient_record
, 0, tflag
, Eflag
);
735 * Send mail to a bunch of user names. The interface is through
736 * the mail routine below.
739 sendmail_internal(void *v
, int recipient_record
)
745 memset(&head
, 0, sizeof head
);
746 head
.h_to
= lextract(str
, GTO
|GFULL
);
747 Eflag
= value("skipemptybody") != NULL
;
748 mail1(&head
, 0, NULL
, NULL
, recipient_record
, 0, 0, Eflag
);
755 return sendmail_internal(v
, 0);
761 return sendmail_internal(v
, 1);
765 transfer(struct name
*to
, FILE *input
, struct header
*hp
)
767 char o
[LINESIZE
], *cp
;
768 struct name
*np
, *nt
;
775 snprintf(o
, sizeof o
, "smime-encrypt-%s", np
->n_name
);
776 if ((cp
= value(o
)) != NULL
) {
777 if ((ef
= smime_encrypt(input
, cp
, np
->n_name
)) != 0) {
778 nt
= ndup(np
, np
->n_type
& ~(GFULL
|GSKIN
));
779 if (start_mta(nt
, ef
, hp
) != OKAY
)
783 fprintf(stderr
, "Message not sent to <%s>\n",
789 np
->n_flink
->n_blink
= np
->n_blink
;
791 np
->n_blink
->n_flink
= np
->n_flink
;
801 if (value("smime-force-encryption") ||
802 start_mta(to
, input
, hp
) != OKAY
)
809 prepare_mta_args(struct name
*to
)
811 size_t j
, i
= 4 + smopts_count
+ count(to
) + 1;
812 char **args
= salloc(i
* sizeof(char*));
814 args
[0] = "send-mail";
819 if (value("verbose"))
821 for (j
= 0; j
< smopts_count
; ++j
, ++i
)
823 for (; to
!= NULL
; to
= to
->n_flink
)
824 if ((to
->n_type
& GDEL
) == 0)
825 args
[i
++] = to
->n_name
;
831 * Start the Mail Transfer Agent
832 * mailing to namelist and stdin redirected to input.
835 start_mta(struct name
*to
, FILE *input
, struct header
*hp
)
841 char **args
= NULL
, *user
= NULL
, *password
= NULL
, *skinned
= NULL
,
847 if ((smtp
= value("smtp")) == NULL
) {
848 args
= prepare_mta_args(to
);
849 if (debug
|| value("debug")) {
850 printf(tr(181, "Sendmail arguments:"));
851 for (t
= args
; *t
!= NULL
; t
++)
852 printf(" \"%s\"", *t
);
859 skinned
= skin(myorigin(hp
));
860 if ((user
= smtp_auth_var("-user", skinned
)) != NULL
&&
861 (password
= smtp_auth_var("-password",
863 password
= getpassword(&otio
, &reset_tio
, NULL
);
867 * Fork, set up the temporary mail file as standard
868 * input for "mail", and exec with the user list we generated
871 if ((pid
= fork()) == -1) {
873 savedeadletter(input
);
879 sigaddset(&nset
, SIGHUP
);
880 sigaddset(&nset
, SIGINT
);
881 sigaddset(&nset
, SIGQUIT
);
882 sigaddset(&nset
, SIGTSTP
);
883 sigaddset(&nset
, SIGTTIN
);
884 sigaddset(&nset
, SIGTTOU
);
885 freopen("/dev/null", "r", stdin
);
887 prepare_child(&nset
, 0, 1);
888 if (smtp_mta(smtp
, to
, input
, hp
,
889 user
, password
, skinned
) == 0)
892 prepare_child(&nset
, fileno(input
), -1);
893 if ((cp
= value("sendmail")) != NULL
)
894 cp
= file_expand(cp
);
900 savedeadletter(input
);
901 fputs(catgets(catd
, CATSET
, 182,
902 ". . . message not sent.\n"), stderr
);
905 if (value("verbose") != NULL
|| value("sendwait") || debug
907 if (wait_child(pid
) == 0)
919 * Record outgoing mail if instructed to do so.
922 mightrecord(FILE *fp
, struct name
*to
, int recipient_record
)
926 if (recipient_record
) {
927 cq
= skinned_name(to
);
928 cp
= salloc(strlen(cq
) + 1);
930 for (cq
= cp
; *cq
&& *cq
!= '@'; cq
++);
933 cp
= value("record");
936 if (value("outfolder") && *ep
!= '/' && *ep
!= '+' &&
937 which_protocol(ep
) == PROTO_FILE
) {
938 cq
= salloc(strlen(cp
) + 2);
944 if (savemail(ep
, fp
) != 0) {
946 "Error while saving message to %s - "
947 "message not sent\n", ep
);
958 * Mail a message on standard input to the people indicated
959 * in the passed header. (Internal interface).
962 mail1(struct header
*hp
, int printheaders
, struct message
*quote
,
963 char *quotefile
, int recipient_record
, int doprefix
, int tflag
,
970 char *charsets
, *ncs
= NULL
, *cp
;
973 if ((hp
->h_to
= checkaddrs(hp
->h_to
)) == NULL
) {
978 if ((cp
= value("autocc")) != NULL
&& *cp
)
979 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
|GFULL
)));
980 if ((cp
= value("autobcc")) != NULL
&& *cp
)
981 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
,GBCC
|GFULL
)));
983 * Collect user's mail from standard input.
984 * Get the result as mtf.
986 if ((mtf
= collect(hp
, printheaders
, quote
, quotefile
, doprefix
,
989 if (value("interactive") != NULL
) {
990 if (((value("bsdcompat") || value("askatend"))
991 && (value("askcc") != NULL
||
992 value("askbcc") != NULL
)) ||
993 value("askattach") != NULL
||
994 value("asksign") != NULL
) {
995 if (value("askcc") != NULL
)
997 if (value("askbcc") != NULL
)
999 if (value("askattach") != NULL
)
1000 hp
->h_attach
= edit_attachments(hp
->h_attach
);
1001 if (value("asksign") != NULL
)
1002 dosign
= yorn("Sign this message (y/n)? ");
1004 printf(catgets(catd
, CATSET
, 183, "EOT\n"));
1008 if (fsize(mtf
) == 0) {
1011 if (hp
->h_subject
== NULL
)
1012 printf(catgets(catd
, CATSET
, 184,
1013 "No message, no subject; hope that's ok\n"));
1014 else if (value("bsdcompat") || value("bsdmsgs"))
1015 printf(catgets(catd
, CATSET
, 185,
1016 "Null message body; hope that's ok\n"));
1019 if (value("smime-sign") != NULL
)
1025 * Now, take the user names from the combined
1026 * to and cc lists and do all the alias
1030 if ((to
= usermap(cat(hp
->h_bcc
, cat(hp
->h_to
, hp
->h_cc
)))) == NULL
) {
1031 printf(catgets(catd
, CATSET
, 186, "No recipients specified\n"));
1034 to
= fixhead(hp
, to
);
1035 if (hp
->h_charset
) {
1036 wantcharset
= hp
->h_charset
;
1039 hloop
: wantcharset
= NULL
;
1040 if ((charsets
= value("sendcharsets")) != NULL
) {
1041 wantcharset
= savestr(charsets
);
1042 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
1045 try: if ((nmtf
= infix(hp
, mtf
, dosign
)) == NULL
) {
1046 if (hp
->h_charset
&& (errno
== EILSEQ
|| errno
== EINVAL
)) {
1048 hp
->h_charset
= NULL
;
1051 if (errno
== EILSEQ
|| errno
== EINVAL
) {
1057 if (wantcharset
&& value("interactive") == NULL
) {
1058 if (wantcharset
== (char *)-1)
1062 wantcharset
= (char *)-1;
1067 /* fprintf(stderr, ". . . message lost, sorry.\n"); */
1071 savedeadletter(mtf
);
1072 fputs(catgets(catd
, CATSET
, 187,
1073 ". . . message not sent.\n"), stderr
);
1078 if ((nmtf
= smime_sign(mtf
, hp
)) == NULL
)
1084 * Look through the recipient list for names with /'s
1085 * in them which we write to as files directly.
1087 to
= outof(to
, mtf
, hp
);
1089 savedeadletter(mtf
);
1091 if (count(to
) == 0) {
1096 if (mightrecord(mtf
, to
, recipient_record
) != OKAY
)
1098 ok
= transfer(to
, mtf
, hp
);
1105 * Create a Message-Id: header field.
1106 * Use either the host name or the from address.
1109 message_id(FILE *fo
, struct header
*hp
)
1118 if ((h
= value("hostname")) != NULL
)
1120 else if ((h
= skin(myorigin(hp
))) != NULL
&& strchr(h
, '@') != NULL
)
1123 /* Delivery seems to dependent on a MTA -- it's up to it */
1126 fprintf(fo
, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
1127 tmp
->tm_year
+ 1900, tmp
->tm_mon
+ 1, tmp
->tm_mday
,
1128 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
,
1129 getrandstring(rl
), (rl
== 16 ? '%' : '@'), h
);
1132 static const char *weekday_names
[] = {
1133 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1136 const char *month_names
[] = {
1137 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1138 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
1142 * Create a Date: header field.
1143 * We compare the localtime() and gmtime() results to get the timezone,
1144 * because numeric timezones are easier to read and because $TZ is
1145 * not set on most GNU systems.
1148 mkdate(FILE *fo
, const char *field
)
1152 int tzdiff
, tzdiff_hour
, tzdiff_min
;
1155 tzdiff
= t
- mktime(gmtime(&t
));
1156 tzdiff_hour
= (int)(tzdiff
/ 60);
1157 tzdiff_min
= tzdiff_hour
% 60;
1159 tmptr
= localtime(&t
);
1160 if (tmptr
->tm_isdst
> 0)
1162 return fprintf(fo
, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1164 weekday_names
[tmptr
->tm_wday
],
1165 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
],
1166 tmptr
->tm_year
+ 1900, tmptr
->tm_hour
,
1167 tmptr
->tm_min
, tmptr
->tm_sec
,
1168 tzdiff_hour
* 100 + tzdiff_min
);
1172 putname(char *line
, enum gfield w
, enum sendaction action
, int *gotcha
,
1173 char *prefix
, FILE *fo
, struct name
**xp
)
1177 np
= lextract(line
, GEXTRA
|GFULL
);
1182 if (fmt(prefix
, np
, fo
, w
&(GCOMMA
|GFILES
), 0, action
!= SEND_TODISP
))
1189 #define FMT_CC_AND_BCC { \
1190 if (hp->h_cc != NULL && w & GCC) { \
1191 if (fmt("Cc:", hp->h_cc, fo, \
1192 w&(GCOMMA|GFILES), 0, \
1193 action!=SEND_TODISP)) \
1197 if (hp->h_bcc != NULL && w & GBCC) { \
1198 if (fmt("Bcc:", hp->h_bcc, fo, \
1199 w&(GCOMMA|GFILES), 0, \
1200 action!=SEND_TODISP)) \
1206 * Dump the to, subject, cc header on the
1207 * passed file buffer.
1210 puthead(struct header
*hp
, FILE *fo
, enum gfield w
,
1211 enum sendaction action
, enum conversion convert
,
1212 char *contenttype
, char *charset
)
1215 char *addr
/*, *cp*/;
1217 struct name
*np
, *fromfield
= NULL
, *senderfield
= NULL
;
1220 if ((addr
= value("stealthmua")) != NULL
) {
1221 stealthmua
= (strcmp(addr
, "noagent") == 0) ? -1 : 1;
1226 mkdate(fo
, "Date"), gotcha
++;
1229 if (hp
->h_from
!= NULL
) {
1230 if (fmt("From:", hp
->h_from
, fo
, w
&(GCOMMA
|GFILES
), 0,
1231 action
!=SEND_TODISP
))
1234 fromfield
= hp
->h_from
;
1235 } else if ((addr
= myaddrs(hp
)) != NULL
)
1236 if (putname(addr
, w
, action
, &gotcha
, "From:", fo
,
1239 if (((addr
= hp
->h_organization
) != NULL
||
1240 (addr
= value("ORGANIZATION")) != NULL
)
1241 && strlen(addr
) > 0) {
1242 fwrite("Organization: ", sizeof (char), 14, fo
);
1243 if (mime_write(addr
, strlen(addr
), fo
,
1244 action
== SEND_TODISP
?
1245 CONV_NONE
:CONV_TOHDR
,
1246 action
== SEND_TODISP
?
1247 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1254 if (hp
->h_replyto
!= NULL
) {
1255 if (fmt("Reply-To:", hp
->h_replyto
, fo
,
1256 w
&(GCOMMA
|GFILES
), 0,
1257 action
!=SEND_TODISP
))
1260 } else if ((addr
= value("replyto")) != NULL
)
1261 if (putname(addr
, w
, action
, &gotcha
, "Reply-To:", fo
,
1264 if (hp
->h_sender
!= NULL
) {
1265 if (fmt("Sender:", hp
->h_sender
, fo
,
1266 w
&(GCOMMA
|GFILES
), 0,
1267 action
!=SEND_TODISP
))
1270 senderfield
= hp
->h_sender
;
1271 } else if ((addr
= value("sender")) != NULL
)
1272 if (putname(addr
, w
, action
, &gotcha
, "Sender:", fo
,
1275 if (check_from_and_sender(fromfield
, senderfield
))
1278 if (hp
->h_to
!= NULL
&& w
& GTO
) {
1279 if (fmt("To:", hp
->h_to
, fo
, w
&(GCOMMA
|GFILES
), 0,
1280 action
!=SEND_TODISP
))
1284 if (value("bsdcompat") == NULL
&& value("bsdorder") == NULL
)
1286 if (hp
->h_subject
!= NULL
&& w
& GSUBJECT
) {
1287 fwrite("Subject: ", sizeof (char), 9, fo
);
1288 if (ascncasecmp(hp
->h_subject
, "re: ", 4) == 0) {
1289 fwrite("Re: ", sizeof (char), 4, fo
);
1290 if (strlen(hp
->h_subject
+ 4) > 0 &&
1291 mime_write(hp
->h_subject
+ 4,
1292 strlen(hp
->h_subject
+ 4),
1293 fo
, action
== SEND_TODISP
?
1294 CONV_NONE
:CONV_TOHDR
,
1295 action
== SEND_TODISP
?
1296 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1300 } else if (*hp
->h_subject
) {
1301 if (mime_write(hp
->h_subject
,
1302 strlen(hp
->h_subject
),
1303 fo
, action
== SEND_TODISP
?
1304 CONV_NONE
:CONV_TOHDR
,
1305 action
== SEND_TODISP
?
1306 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1312 fwrite("\n", sizeof (char), 1, fo
);
1314 if (value("bsdcompat") || value("bsdorder"))
1316 if (w
& GMSGID
&& stealthmua
<= 0)
1317 message_id(fo
, hp
), gotcha
++;
1318 if ((np
= hp
->h_ref
) != NULL
&& w
& GREF
) {
1319 fmt("References:", np
, fo
, 0, 1, 0);
1323 if (is_addr_invalid(np
, 0) == 0) {
1324 fprintf(fo
, "In-Reply-To: %s\n", np
->n_name
);
1329 if (w
& GUA
&& stealthmua
== 0)
1330 fprintf(fo
, "User-Agent: %s %s\n", uagent
, version
), gotcha
++;
1332 fputs("MIME-Version: 1.0\n", fo
), gotcha
++;
1333 if (hp
->h_attach
!= NULL
) {
1335 fprintf(fo
, "Content-Type: multipart/mixed;\n"
1336 " boundary=\"%s\"\n", send_boundary
);
1338 fprintf(fo
, "Content-Type: %s", contenttype
);
1340 fprintf(fo
, "; charset=%s", charset
);
1341 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n",
1342 getencoding(convert
));
1345 if (gotcha
&& w
& GNL
)
1351 * Format the given header line to not exceed 72 characters.
1354 fmt(char *str
, struct name
*np
, FILE *fo
, int flags
, int dropinvalid
,
1362 } m
= (flags
& GCOMMA
) ? m_COMMA
: 0;
1367 fwrite(str
, sizeof *str
, strlen(str
), fo
);
1368 if ((flags
&GFILES
) == 0 && ! value("add-file-recipients") &&
1369 ((col
== 3 && ((asccasecmp(str
, "to:") == 0) ||
1370 asccasecmp(str
, "cc:") == 0)) ||
1371 (col
== 4 && asccasecmp(str
, "bcc:") == 0) ||
1373 asccasecmp(str
, "Resent-To:") == 0)))
1376 for (; np
!= NULL
; np
= np
->n_flink
) {
1377 if ((m
& m_NOPF
) && is_fileorpipe_addr(np
))
1379 if (is_addr_invalid(np
, ! dropinvalid
)) {
1385 if ((m
& (m_INIT
| m_COMMA
)) == (m_INIT
| m_COMMA
)) {
1390 len
= strlen(np
->n_fullname
);
1391 ++col
; /* The separating space */
1392 if ((m
& m_INIT
) && col
> 1 && col
+ len
> 72) {
1398 m
= (m
& ~m_CSEEN
) | m_INIT
;
1399 len
= mime_write(np
->n_fullname
,
1401 domime
?CONV_TOHDR_A
:CONV_NONE
,
1402 TD_ICONV
, NULL
, (size_t)0,
1411 * Rewrite a message for resending, adding the Resent-Headers.
1414 infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
, struct name
*to
,
1418 char *buf
= NULL
, *cp
/*, *cp2*/;
1419 size_t c
, bufsize
= 0;
1420 struct name
*fromfield
= NULL
, *senderfield
= NULL
;
1424 * Write the Resent-Fields.
1427 fputs("Resent-", fo
);
1429 if ((cp
= myaddrs(NULL
)) != NULL
) {
1430 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1431 "Resent-From:", fo
, &fromfield
))
1434 if ((cp
= value("sender")) != NULL
) {
1435 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1436 "Resent-Sender:", fo
, &senderfield
))
1439 if (fmt("Resent-To:", to
, fo
, 1, 1, 0)) {
1444 if ((cp
= value("stealthmua")) == NULL
||
1445 strcmp(cp
, "noagent") == 0) {
1446 fputs("Resent-", fo
);
1447 message_id(fo
, NULL
);
1450 if (check_from_and_sender(fromfield
, senderfield
))
1453 * Write the original headers.
1456 if ((cp
= foldergets(&buf
, &bufsize
, &count
, &c
, fi
)) == NULL
)
1458 if (ascncasecmp("status: ", buf
, 8) != 0
1459 && strncmp("From ", buf
, 5) != 0) {
1460 fwrite(buf
, sizeof *buf
, c
, fo
);
1462 if (count
> 0 && *buf
== '\n')
1466 * Write the message body.
1469 if (foldergets(&buf
, &bufsize
, &count
, &c
, fi
) == NULL
)
1471 if (count
== 0 && *buf
== '\n')
1473 fwrite(buf
, sizeof *buf
, c
, fo
);
1478 perror(catgets(catd
, CATSET
, 188, "temporary mail file"));
1485 resend_msg(struct message
*mp
, struct name
*to
, int add_resent
)
1487 FILE *ibuf
, *nfo
, *nfi
;
1490 enum okay ok
= STOP
;
1492 memset(&head
, 0, sizeof head
);
1493 if ((to
= checkaddrs(to
)) == NULL
) {
1497 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
1499 perror(catgets(catd
, CATSET
, 189, "temporary mail file"));
1502 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
1509 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1512 to
= fixhead(&head
, to
);
1513 if (infix_resend(ibuf
, nfo
, mp
, head
.h_to
, add_resent
) != 0) {
1516 savedeadletter(nfi
);
1517 fputs(catgets(catd
, CATSET
, 190,
1518 ". . . message not sent.\n"), stderr
);
1526 to
= outof(to
, nfi
, &head
);
1528 savedeadletter(nfi
);
1530 if (count(to
) != 0) {
1531 if (value("record-resent") == NULL
||
1532 mightrecord(nfi
, to
, 0) == OKAY
)
1533 ok
= transfer(to
, nfi
, NULL
);
1534 } else if (senderr
== 0)