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
, struct name
*mailargs
, FILE *input
,
69 static enum okay
start_mta(struct name
*to
, struct name
*mailargs
, FILE *input
,
71 static void message_id(FILE *fo
, struct header
*hp
);
72 static int fmt(char *str
, struct name
*np
, FILE *fo
, int comma
,
73 int dropinvalid
, int domime
);
74 static int infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
,
75 struct name
*to
, int add_resent
);
78 * Generate a boundary for MIME multipart messages.
83 static char bound
[70];
87 snprintf(bound
, sizeof bound
, "=_%lx.%s", (long)now
, getrandstring(48));
88 send_boundary
= bound
;
93 * Get an encoding flag based on the given string.
96 getencoding(enum conversion convert
)
104 return "quoted-printable";
115 * Fix the header by glopping all of the expanded names from
116 * the distribution list into the appropriate fields.
119 fixhead(struct header
*hp
, struct name
*tolist
) /* TODO !HAVE_ASSERTS legacy*/
123 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
124 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
)
125 if (np
->n_type
& GDEL
) {
127 assert(0); /* Shouldn't happen here, but later on :)) */
131 } else switch (np
->n_type
& GMASK
) {
133 hp
->h_to
= cat(hp
->h_to
, ndup(np
, np
->n_type
|GFULL
));
136 hp
->h_cc
= cat(hp
->h_cc
, ndup(np
, np
->n_type
|GFULL
));
139 hp
->h_bcc
= cat(hp
->h_bcc
, ndup(np
, np
->n_type
|GFULL
));
149 * Do not change, you get incorrect base64 encodings else!
151 #define INFIX_BUF 972
154 * Put the signature file at fo.
157 put_signature(FILE *fo
, int convert
)
159 char *sig
, buf
[INFIX_BUF
], c
= '\n';
163 sig
= value("signature");
164 if (sig
== NULL
|| *sig
== '\0')
168 if ((fsig
= Fopen(sig
, "r")) == NULL
) {
172 while ((sz
= fread(buf
, sizeof *buf
, INFIX_BUF
, fsig
)) != 0) {
174 if (mime_write(buf
, sz
, fo
, convert
, TD_NONE
,
175 NULL
, (size_t)0, NULL
, NULL
)
194 * Write an attachment to the file buffer, converting to MIME.
197 attach_file1(struct attachment
*ap
, FILE *fo
, int dosign
)
200 char *charset
= NULL
, *contenttype
= NULL
, *basename
;
201 enum conversion convert
= CONV_TOB64
;
203 enum mimeclean isclean
;
206 size_t bufsize
, count
;
212 if ((fi
= Fopen(ap
->a_name
, "r")) == NULL
) {
216 if ((basename
= strrchr(ap
->a_name
, '/')) == NULL
)
217 basename
= ap
->a_name
;
220 if (ap
->a_content_type
)
221 contenttype
= ap
->a_content_type
;
223 contenttype
= mime_filecontent(basename
);
225 charset
= ap
->a_charset
;
226 convert
= get_mime_convert(fi
, &contenttype
, &charset
, &isclean
,
231 send_boundary
, contenttype
);
235 fprintf(fo
, ";\n charset=%s\n", charset
);
236 if (ap
->a_content_disposition
== NULL
)
237 ap
->a_content_disposition
= "attachment";
238 fprintf(fo
, "Content-Transfer-Encoding: %s\n"
239 "Content-Disposition: %s;\n"
241 getencoding(convert
),
242 ap
->a_content_disposition
);
243 mime_write(basename
, strlen(basename
), fo
,
244 CONV_TOHDR
, TD_NONE
, NULL
, (size_t)0, NULL
, NULL
);
245 fwrite("\"\n", sizeof (char), 2, fo
);
246 if (ap
->a_content_id
)
247 fprintf(fo
, "Content-ID: %s\n", ap
->a_content_id
);
248 if (ap
->a_content_description
)
249 fprintf(fo
, "Content-Description: %s\n",
250 ap
->a_content_description
);
253 if (iconvd
!= (iconv_t
)-1) {
255 iconvd
= (iconv_t
)-1;
258 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
259 ascncasecmp(contenttype
, "text/", 5) == 0 &&
260 isclean
& MIME_HIGHBIT
&&
262 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1 &&
265 fprintf(stderr
, catgets(catd
, CATSET
, 179,
266 "Cannot convert from %s to %s\n"), tcs
, charset
);
268 perror("iconv_open");
273 #endif /* HAVE_ICONV */
274 buf
= smalloc(bufsize
= INFIX_BUF
);
275 if (convert
== CONV_TOQP
277 || iconvd
!= (iconv_t
)-1
284 if (convert
== CONV_TOQP
286 || iconvd
!= (iconv_t
)-1
289 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
293 if ((sz
= fread(buf
, sizeof *buf
, bufsize
, fi
)) == 0)
297 if (mime_write(buf
, sz
, fo
, convert
, TD_ICONV
,
298 NULL
, (size_t)0, NULL
, NULL
) == 0)
301 if (convert
== CONV_TOQP
&& lastc
!= '\n')
302 fwrite("=\n", 1, 2, fo
);
311 * Try out different character set conversions to attach a file.
314 attach_file(struct attachment
*ap
, FILE *fo
, int dosign
)
316 char *_wantcharset
, *charsets
, *ncs
;
317 size_t offs
= ftell(fo
);
319 if (ap
->a_charset
|| (charsets
= value("sendcharsets")) == NULL
)
320 return attach_file1(ap
, fo
, dosign
);
321 _wantcharset
= wantcharset
;
322 wantcharset
= savestr(charsets
);
323 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
325 try: if (attach_file1(ap
, fo
, dosign
) != 0) {
326 if (errno
== EILSEQ
|| errno
== EINVAL
) {
330 fseek(fo
, offs
, SEEK_SET
);
334 if (wantcharset
== (char *)-1)
337 wantcharset
= (char *)-1;
339 fseek(fo
, offs
, SEEK_SET
);
345 wantcharset
= _wantcharset
;
350 * Attach a message to the file buffer.
353 attach_message(struct attachment
*ap
, FILE *fo
, int dosign
)
358 fprintf(fo
, "\n--%s\n"
359 "Content-Type: message/rfc822\n"
360 "Content-Disposition: inline\n\n", send_boundary
);
361 mp
= &message
[ap
->a_msgno
- 1];
363 if (send(mp
, fo
, 0, NULL
, SEND_RFC822
, NULL
) < 0)
369 * Generate the body of a MIME multipart message.
372 make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
373 const char *contenttype
, const char *charset
, int dosign
)
375 struct attachment
*att
;
377 fputs("This is a multi-part message in MIME format.\n", fo
);
378 if (fsize(fi
) != 0) {
380 size_t sz
, bufsize
, count
;
382 fprintf(fo
, "\n--%s\n", send_boundary
);
383 fprintf(fo
, "Content-Type: %s", contenttype
);
385 fprintf(fo
, "; charset=%s", charset
);
386 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n"
387 "Content-Disposition: inline\n\n",
388 getencoding(convert
));
389 buf
= smalloc(bufsize
= INFIX_BUF
);
390 if (convert
== CONV_TOQP
392 || iconvd
!= (iconv_t
)-1
393 #endif /* HAVE_ICONV */
399 if (convert
== CONV_TOQP
401 || iconvd
!= (iconv_t
)-1
402 #endif /* HAVE_ICONV */
404 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
408 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
413 if (mime_write(buf
, sz
, fo
, convert
,
414 TD_ICONV
, NULL
, (size_t)0,
426 put_signature(fo
, convert
);
428 for (att
= hp
->h_attach
; att
!= NULL
; att
= att
->a_flink
) {
430 if (attach_message(att
, fo
, dosign
) != 0)
433 if (attach_file(att
, fo
, dosign
) != 0)
437 /* the final boundary with two attached dashes */
438 fprintf(fo
, "\n--%s--\n", send_boundary
);
443 * Prepend a header in front of the collected stuff
444 * and return the new file.
447 infix(struct header
*hp
, FILE *fi
, int dosign
)
452 char *tcs
, *convhdr
= NULL
;
454 enum mimeclean isclean
;
455 enum conversion convert
;
456 char *charset
= NULL
, *contenttype
= NULL
;
459 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
460 perror(catgets(catd
, CATSET
, 178, "temporary mail file"));
463 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
470 convert
= get_mime_convert(fi
, &contenttype
, &charset
,
474 if ((convhdr
= need_hdrconv(hp
, GTO
|GSUBJECT
|GCC
|GBCC
|GIDENT
)) != 0) {
475 if (iconvd
!= (iconv_t
)-1)
477 if ((iconvd
= iconv_open_ft(convhdr
, tcs
)) == (iconv_t
)-1
480 fprintf(stderr
, catgets(catd
, CATSET
, 179,
481 "Cannot convert from %s to %s\n"), tcs
, convhdr
);
483 perror("iconv_open");
488 #endif /* HAVE_ICONV */
490 GTO
|GSUBJECT
|GCC
|GBCC
|GNL
|GCOMMA
|GUA
|GMIME
491 |GMSGID
|GIDENT
|GREF
|GDATE
,
492 SEND_MBOX
, convert
, contenttype
, charset
)) {
496 if (iconvd
!= (iconv_t
)-1) {
498 iconvd
= (iconv_t
)-1;
504 if (convhdr
&& iconvd
!= (iconv_t
)-1) {
506 iconvd
= (iconv_t
)-1;
508 if ((isclean
& (MIME_HASNUL
|MIME_CTRLCHAR
)) == 0 &&
509 ascncasecmp(contenttype
, "text/", 5) == 0 &&
510 isclean
& MIME_HIGHBIT
&&
512 if (iconvd
!= (iconv_t
)-1)
514 if ((iconvd
= iconv_open_ft(charset
, tcs
)) == (iconv_t
)-1
517 fprintf(stderr
, catgets(catd
, CATSET
, 179,
518 "Cannot convert from %s to %s\n"), tcs
, charset
);
520 perror("iconv_open");
526 if (hp
->h_attach
!= NULL
) {
527 if (make_multipart(hp
, convert
, fi
, nfo
,
528 contenttype
, charset
, dosign
) != 0) {
532 if (iconvd
!= (iconv_t
)-1) {
534 iconvd
= (iconv_t
)-1;
540 size_t sz
, bufsize
, count
;
543 if (convert
== CONV_TOQP
545 || iconvd
!= (iconv_t
)-1
546 #endif /* HAVE_ICONV */
551 buf
= smalloc(bufsize
= INFIX_BUF
);
553 if (convert
== CONV_TOQP
555 || iconvd
!= (iconv_t
)-1
556 #endif /* HAVE_ICONV */
558 if (fgetline(&buf
, &bufsize
, &count
, &sz
, fi
, 0)
562 sz
= fread(buf
, sizeof *buf
, bufsize
, fi
);
567 if (mime_write(buf
, sz
, nfo
, convert
,
568 TD_ICONV
, NULL
, (size_t)0,
573 if (iconvd
!= (iconv_t
)-1) {
575 iconvd
= (iconv_t
)-1;
582 if (convert
== CONV_TOQP
&& lastc
!= '\n')
583 fwrite("=\n", 1, 2, nfo
);
589 if (iconvd
!= (iconv_t
)-1) {
591 iconvd
= (iconv_t
)-1;
597 put_signature(nfo
, convert
);
600 if (iconvd
!= (iconv_t
)-1) {
602 iconvd
= (iconv_t
)-1;
607 perror(catgets(catd
, CATSET
, 180, "temporary mail file"));
620 * Save the outgoing mail on the passed file.
625 savemail(char *name
, FILE *fi
)
629 size_t bufsize
, buflen
, count
;
635 buf
= smalloc(bufsize
= LINESIZE
);
637 if ((fo
= Zopen(name
, "a+", NULL
)) == NULL
) {
638 if ((fo
= Zopen(name
, "wx", NULL
)) == NULL
) {
644 if (fseek(fo
, -2L, SEEK_END
) == 0) {
645 switch (fread(buf
, sizeof *buf
, 2, fo
)) {
647 if (buf
[1] != '\n') {
670 fprintf(fo
, "From %s %s", myname
, ctime(&now
));
675 while (fgetline(&buf
, &bufsize
, &count
, &buflen
, fi
, 0) != NULL
) {
680 if (strncmp(p
, "From ", 5) == 0)
681 /* we got a masked From line */
683 } else if (strncmp(buf
, "From ", 5) == 0)
685 fwrite(buf
, sizeof *buf
, buflen
, fo
);
687 if (buflen
&& *(buf
+ buflen
- 1) != '\n')
704 * Interface between the argument list and the mail1 routine
705 * which does all the dirty work.
708 mail(struct name
*to
, struct name
*cc
, struct name
*bcc
,
709 struct name
*smopts
, char *subject
, struct attachment
*attach
,
710 char *quotefile
, int recipient_record
, int tflag
, int Eflag
)
715 memset(&head
, 0, sizeof head
);
716 /* The given subject may be in RFC1522 format. */
717 if (subject
!= NULL
) {
719 in
.l
= strlen(subject
);
720 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
721 head
.h_subject
= out
.s
;
728 head
.h_attach
= attach
;
729 head
.h_smopts
= smopts
;
730 mail1(&head
, 0, NULL
, quotefile
, recipient_record
, 0, tflag
, Eflag
);
737 * Send mail to a bunch of user names. The interface is through
738 * the mail routine below.
741 sendmail_internal(void *v
, int recipient_record
)
747 memset(&head
, 0, sizeof head
);
748 head
.h_to
= extract(str
, GTO
|GFULL
);
749 Eflag
= value("skipemptybody") != NULL
;
750 mail1(&head
, 0, NULL
, NULL
, recipient_record
, 0, 0, Eflag
);
757 return sendmail_internal(v
, 0);
763 return sendmail_internal(v
, 1);
767 transfer(struct name
*to
, struct name
*mailargs
, FILE *input
, struct header
*hp
)
769 char o
[LINESIZE
], *cp
;
770 struct name
*np
, *nt
;
777 snprintf(o
, sizeof o
, "smime-encrypt-%s", np
->n_name
);
778 if ((cp
= value(o
)) != NULL
) {
779 if ((ef
= smime_encrypt(input
, cp
, np
->n_name
)) != 0) {
780 nt
= ndup(np
, np
->n_type
& ~(GFULL
|GSKIN
));
781 if (start_mta(nt
, mailargs
, ef
, hp
) != OKAY
)
785 fprintf(stderr
, "Message not sent to <%s>\n",
791 np
->n_flink
->n_blink
= np
->n_blink
;
793 np
->n_blink
->n_flink
= np
->n_flink
;
803 if (value("smime-force-encryption") ||
804 start_mta(to
, mailargs
, input
, hp
) != OKAY
)
811 * Start the Mail Transfer Agent
812 * mailing to namelist and stdin redirected to input.
815 start_mta(struct name
*to
, struct name
*mailargs
, FILE *input
,
818 char **args
= NULL
, **t
;
822 char *user
= NULL
, *password
= NULL
, *skinned
= NULL
;
829 if ((smtp
= value("smtp")) == NULL
) {
830 args
= unpack(cat(mailargs
, to
));
831 if (debug
|| value("debug")) {
832 printf(catgets(catd
, CATSET
, 181,
833 "Sendmail arguments:"));
834 for (t
= args
; *t
!= NULL
; t
++)
835 printf(" \"%s\"", *t
);
842 skinned
= skin(myorigin(hp
));
843 if ((user
= smtp_auth_var("-user", skinned
)) != NULL
&&
844 (password
= smtp_auth_var("-password",
846 password
= getpassword(&otio
, &reset_tio
, NULL
);
850 * Fork, set up the temporary mail file as standard
851 * input for "mail", and exec with the user list we generated
854 if ((pid
= fork()) == -1) {
856 savedeadletter(input
);
862 sigaddset(&nset
, SIGHUP
);
863 sigaddset(&nset
, SIGINT
);
864 sigaddset(&nset
, SIGQUIT
);
865 sigaddset(&nset
, SIGTSTP
);
866 sigaddset(&nset
, SIGTTIN
);
867 sigaddset(&nset
, SIGTTOU
);
868 freopen("/dev/null", "r", stdin
);
870 prepare_child(&nset
, 0, 1);
871 if (smtp_mta(smtp
, to
, input
, hp
,
872 user
, password
, skinned
) == 0)
875 prepare_child(&nset
, fileno(input
), -1);
876 if ((cp
= value("sendmail")) != NULL
)
883 savedeadletter(input
);
884 fputs(catgets(catd
, CATSET
, 182,
885 ". . . message not sent.\n"), stderr
);
888 if (value("verbose") != NULL
|| value("sendwait") || debug
890 if (wait_child(pid
) == 0)
902 * Record outgoing mail if instructed to do so.
905 mightrecord(FILE *fp
, struct name
*to
, int recipient_record
)
909 if (recipient_record
) {
910 cq
= skinned_name(to
);
911 cp
= salloc(strlen(cq
) + 1);
913 for (cq
= cp
; *cq
&& *cq
!= '@'; cq
++);
916 cp
= value("record");
919 if (value("outfolder") && *ep
!= '/' && *ep
!= '+' &&
920 which_protocol(ep
) == PROTO_FILE
) {
921 cq
= salloc(strlen(cp
) + 2);
927 if (savemail(ep
, fp
) != 0) {
929 "Error while saving message to %s - "
930 "message not sent\n", ep
);
941 * Mail a message on standard input to the people indicated
942 * in the passed header. (Internal interface).
945 mail1(struct header
*hp
, int printheaders
, struct message
*quote
,
946 char *quotefile
, int recipient_record
, int doprefix
, int tflag
,
953 char *charsets
, *ncs
= NULL
, *cp
;
956 if ((hp
->h_to
= checkaddrs(hp
->h_to
)) == NULL
) {
961 if ((cp
= value("autocc")) != NULL
&& *cp
)
962 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
|GFULL
)));
963 if ((cp
= value("autobcc")) != NULL
&& *cp
)
964 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
,GBCC
|GFULL
)));
966 * Collect user's mail from standard input.
967 * Get the result as mtf.
969 if ((mtf
= collect(hp
, printheaders
, quote
, quotefile
, doprefix
,
972 if (value("interactive") != NULL
) {
973 if (((value("bsdcompat") || value("askatend"))
974 && (value("askcc") != NULL
||
975 value("askbcc") != NULL
)) ||
976 value("askattach") != NULL
||
977 value("asksign") != NULL
) {
978 if (value("askcc") != NULL
)
980 if (value("askbcc") != NULL
)
982 if (value("askattach") != NULL
)
983 hp
->h_attach
= edit_attachments(hp
->h_attach
);
984 if (value("asksign") != NULL
)
985 dosign
= yorn("Sign this message (y/n)? ");
987 printf(catgets(catd
, CATSET
, 183, "EOT\n"));
991 if (fsize(mtf
) == 0) {
994 if (hp
->h_subject
== NULL
)
995 printf(catgets(catd
, CATSET
, 184,
996 "No message, no subject; hope that's ok\n"));
997 else if (value("bsdcompat") || value("bsdmsgs"))
998 printf(catgets(catd
, CATSET
, 185,
999 "Null message body; hope that's ok\n"));
1002 if (value("smime-sign") != NULL
)
1008 * Now, take the user names from the combined
1009 * to and cc lists and do all the alias
1013 if ((to
= usermap(cat(hp
->h_bcc
, cat(hp
->h_to
, hp
->h_cc
)))) == NULL
) {
1014 printf(catgets(catd
, CATSET
, 186, "No recipients specified\n"));
1017 to
= fixhead(hp
, to
);
1018 if (hp
->h_charset
) {
1019 wantcharset
= hp
->h_charset
;
1022 hloop
: wantcharset
= NULL
;
1023 if ((charsets
= value("sendcharsets")) != NULL
) {
1024 wantcharset
= savestr(charsets
);
1025 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
1028 try: if ((nmtf
= infix(hp
, mtf
, dosign
)) == NULL
) {
1029 if (hp
->h_charset
&& (errno
== EILSEQ
|| errno
== EINVAL
)) {
1031 hp
->h_charset
= NULL
;
1034 if (errno
== EILSEQ
|| errno
== EINVAL
) {
1040 if (wantcharset
&& value("interactive") == NULL
) {
1041 if (wantcharset
== (char *)-1)
1045 wantcharset
= (char *)-1;
1050 /* fprintf(stderr, ". . . message lost, sorry.\n"); */
1054 savedeadletter(mtf
);
1055 fputs(catgets(catd
, CATSET
, 187,
1056 ". . . message not sent.\n"), stderr
);
1061 if ((nmtf
= smime_sign(mtf
, hp
)) == NULL
)
1067 * Look through the recipient list for names with /'s
1068 * in them which we write to as files directly.
1070 to
= outof(to
, mtf
, hp
);
1072 savedeadletter(mtf
);
1074 if (count(to
) == 0) {
1079 if (mightrecord(mtf
, to
, recipient_record
) != OKAY
)
1081 ok
= transfer(to
, hp
->h_smopts
, mtf
, hp
);
1088 * Create a Message-Id: header field.
1089 * Use either the host name or the from address.
1092 message_id(FILE *fo
, struct header
*hp
)
1101 if ((h
= value("hostname")) != NULL
)
1103 else if ((h
= skin(myorigin(hp
))) != NULL
&& strchr(h
, '@') != NULL
)
1106 /* Delivery seems to dependent on a MTA -- it's up to it */
1109 fprintf(fo
, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
1110 tmp
->tm_year
+ 1900, tmp
->tm_mon
+ 1, tmp
->tm_mday
,
1111 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
,
1112 getrandstring(rl
), (rl
== 16 ? '%' : '@'), h
);
1115 static const char *weekday_names
[] = {
1116 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1119 const char *month_names
[] = {
1120 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1121 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
1125 * Create a Date: header field.
1126 * We compare the localtime() and gmtime() results to get the timezone,
1127 * because numeric timezones are easier to read and because $TZ is
1128 * not set on most GNU systems.
1131 mkdate(FILE *fo
, const char *field
)
1135 int tzdiff
, tzdiff_hour
, tzdiff_min
;
1138 tzdiff
= t
- mktime(gmtime(&t
));
1139 tzdiff_hour
= (int)(tzdiff
/ 60);
1140 tzdiff_min
= tzdiff_hour
% 60;
1142 tmptr
= localtime(&t
);
1143 if (tmptr
->tm_isdst
> 0)
1145 return fprintf(fo
, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1147 weekday_names
[tmptr
->tm_wday
],
1148 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
],
1149 tmptr
->tm_year
+ 1900, tmptr
->tm_hour
,
1150 tmptr
->tm_min
, tmptr
->tm_sec
,
1151 tzdiff_hour
* 100 + tzdiff_min
);
1155 putname(char *line
, enum gfield w
, enum sendaction action
, int *gotcha
,
1156 char *prefix
, FILE *fo
, struct name
**xp
)
1160 np
= sextract(line
, GEXTRA
|GFULL
);
1165 if (fmt(prefix
, np
, fo
, w
&(GCOMMA
|GFILES
), 0, action
!= SEND_TODISP
))
1172 #define FMT_CC_AND_BCC { \
1173 if (hp->h_cc != NULL && w & GCC) { \
1174 if (fmt("Cc:", hp->h_cc, fo, \
1175 w&(GCOMMA|GFILES), 0, \
1176 action!=SEND_TODISP)) \
1180 if (hp->h_bcc != NULL && w & GBCC) { \
1181 if (fmt("Bcc:", hp->h_bcc, fo, \
1182 w&(GCOMMA|GFILES), 0, \
1183 action!=SEND_TODISP)) \
1189 * Dump the to, subject, cc header on the
1190 * passed file buffer.
1193 puthead(struct header
*hp
, FILE *fo
, enum gfield w
,
1194 enum sendaction action
, enum conversion convert
,
1195 char *contenttype
, char *charset
)
1198 char *addr
/*, *cp*/;
1200 struct name
*np
, *fromfield
= NULL
, *senderfield
= NULL
;
1203 if ((addr
= value("stealthmua")) != NULL
) {
1204 stealthmua
= (strcmp(addr
, "noagent") == 0) ? -1 : 1;
1209 mkdate(fo
, "Date"), gotcha
++;
1212 if (hp
->h_from
!= NULL
) {
1213 if (fmt("From:", hp
->h_from
, fo
, w
&(GCOMMA
|GFILES
), 0,
1214 action
!=SEND_TODISP
))
1217 fromfield
= hp
->h_from
;
1218 } else if ((addr
= myaddrs(hp
)) != NULL
)
1219 if (putname(addr
, w
, action
, &gotcha
, "From:", fo
,
1222 if (((addr
= hp
->h_organization
) != NULL
||
1223 (addr
= value("ORGANIZATION")) != NULL
)
1224 && strlen(addr
) > 0) {
1225 fwrite("Organization: ", sizeof (char), 14, fo
);
1226 if (mime_write(addr
, strlen(addr
), fo
,
1227 action
== SEND_TODISP
?
1228 CONV_NONE
:CONV_TOHDR
,
1229 action
== SEND_TODISP
?
1230 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1237 if (hp
->h_replyto
!= NULL
) {
1238 if (fmt("Reply-To:", hp
->h_replyto
, fo
,
1239 w
&(GCOMMA
|GFILES
), 0,
1240 action
!=SEND_TODISP
))
1243 } else if ((addr
= value("replyto")) != NULL
)
1244 if (putname(addr
, w
, action
, &gotcha
, "Reply-To:", fo
,
1247 if (hp
->h_sender
!= NULL
) {
1248 if (fmt("Sender:", hp
->h_sender
, fo
,
1249 w
&(GCOMMA
|GFILES
), 0,
1250 action
!=SEND_TODISP
))
1253 senderfield
= hp
->h_sender
;
1254 } else if ((addr
= value("sender")) != NULL
)
1255 if (putname(addr
, w
, action
, &gotcha
, "Sender:", fo
,
1258 if (check_from_and_sender(fromfield
, senderfield
))
1261 if (hp
->h_to
!= NULL
&& w
& GTO
) {
1262 if (fmt("To:", hp
->h_to
, fo
, w
&(GCOMMA
|GFILES
), 0,
1263 action
!=SEND_TODISP
))
1267 if (value("bsdcompat") == NULL
&& value("bsdorder") == NULL
)
1269 if (hp
->h_subject
!= NULL
&& w
& GSUBJECT
) {
1270 fwrite("Subject: ", sizeof (char), 9, fo
);
1271 if (ascncasecmp(hp
->h_subject
, "re: ", 4) == 0) {
1272 fwrite("Re: ", sizeof (char), 4, fo
);
1273 if (strlen(hp
->h_subject
+ 4) > 0 &&
1274 mime_write(hp
->h_subject
+ 4,
1275 strlen(hp
->h_subject
+ 4),
1276 fo
, action
== SEND_TODISP
?
1277 CONV_NONE
:CONV_TOHDR
,
1278 action
== SEND_TODISP
?
1279 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1283 } else if (*hp
->h_subject
) {
1284 if (mime_write(hp
->h_subject
,
1285 strlen(hp
->h_subject
),
1286 fo
, action
== SEND_TODISP
?
1287 CONV_NONE
:CONV_TOHDR
,
1288 action
== SEND_TODISP
?
1289 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1295 fwrite("\n", sizeof (char), 1, fo
);
1297 if (value("bsdcompat") || value("bsdorder"))
1299 if (w
& GMSGID
&& stealthmua
<= 0)
1300 message_id(fo
, hp
), gotcha
++;
1301 if ((np
= hp
->h_ref
) != NULL
&& w
& GREF
) {
1302 fmt("References:", np
, fo
, 0, 1, 0);
1306 if (is_addr_invalid(np
, 0) == 0) {
1307 fprintf(fo
, "In-Reply-To: %s\n", np
->n_name
);
1312 if (w
& GUA
&& stealthmua
== 0)
1313 fprintf(fo
, "User-Agent: %s %s\n", uagent
, version
), gotcha
++;
1315 fputs("MIME-Version: 1.0\n", fo
), gotcha
++;
1316 if (hp
->h_attach
!= NULL
) {
1318 fprintf(fo
, "Content-Type: multipart/mixed;\n"
1319 " boundary=\"%s\"\n", send_boundary
);
1321 fprintf(fo
, "Content-Type: %s", contenttype
);
1323 fprintf(fo
, "; charset=%s", charset
);
1324 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n",
1325 getencoding(convert
));
1328 if (gotcha
&& w
& GNL
)
1334 * Format the given header line to not exceed 72 characters.
1337 fmt(char *str
, struct name
*np
, FILE *fo
, int flags
, int dropinvalid
,
1345 } m
= (flags
& GCOMMA
) ? m_COMMA
: 0;
1350 fwrite(str
, sizeof *str
, strlen(str
), fo
);
1351 if ((flags
&GFILES
) == 0 && ! value("add-file-recipients") &&
1352 ((col
== 3 && ((asccasecmp(str
, "to:") == 0) ||
1353 asccasecmp(str
, "cc:") == 0)) ||
1354 (col
== 4 && asccasecmp(str
, "bcc:") == 0) ||
1356 asccasecmp(str
, "Resent-To:") == 0)))
1359 for (; np
!= NULL
; np
= np
->n_flink
) {
1360 if ((m
& m_NOPF
) && is_fileorpipe_addr(np
))
1362 if (is_addr_invalid(np
, ! dropinvalid
)) {
1368 if ((m
& (m_INIT
| m_COMMA
)) == (m_INIT
| m_COMMA
)) {
1373 len
= strlen(np
->n_fullname
);
1374 ++col
; /* The separating space */
1375 if ((m
& m_INIT
) && col
> 1 && col
+ len
> 72) {
1381 m
= (m
& ~m_CSEEN
) | m_INIT
;
1382 len
= mime_write(np
->n_fullname
,
1384 domime
?CONV_TOHDR_A
:CONV_NONE
,
1385 TD_ICONV
, NULL
, (size_t)0,
1394 * Rewrite a message for resending, adding the Resent-Headers.
1397 infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
, struct name
*to
,
1401 char *buf
= NULL
, *cp
/*, *cp2*/;
1402 size_t c
, bufsize
= 0;
1403 struct name
*fromfield
= NULL
, *senderfield
= NULL
;
1407 * Write the Resent-Fields.
1410 fputs("Resent-", fo
);
1412 if ((cp
= myaddrs(NULL
)) != NULL
) {
1413 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1414 "Resent-From:", fo
, &fromfield
))
1417 if ((cp
= value("sender")) != NULL
) {
1418 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1419 "Resent-Sender:", fo
, &senderfield
))
1422 if (fmt("Resent-To:", to
, fo
, 1, 1, 0)) {
1427 if ((cp
= value("stealthmua")) == NULL
||
1428 strcmp(cp
, "noagent") == 0) {
1429 fputs("Resent-", fo
);
1430 message_id(fo
, NULL
);
1433 if (check_from_and_sender(fromfield
, senderfield
))
1436 * Write the original headers.
1439 if ((cp
= foldergets(&buf
, &bufsize
, &count
, &c
, fi
)) == NULL
)
1441 if (ascncasecmp("status: ", buf
, 8) != 0
1442 && strncmp("From ", buf
, 5) != 0) {
1443 fwrite(buf
, sizeof *buf
, c
, fo
);
1445 if (count
> 0 && *buf
== '\n')
1449 * Write the message body.
1452 if (foldergets(&buf
, &bufsize
, &count
, &c
, fi
) == NULL
)
1454 if (count
== 0 && *buf
== '\n')
1456 fwrite(buf
, sizeof *buf
, c
, fo
);
1461 perror(catgets(catd
, CATSET
, 188, "temporary mail file"));
1468 resend_msg(struct message
*mp
, struct name
*to
, int add_resent
)
1470 FILE *ibuf
, *nfo
, *nfi
;
1473 enum okay ok
= STOP
;
1475 memset(&head
, 0, sizeof head
);
1476 if ((to
= checkaddrs(to
)) == NULL
) {
1480 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
1482 perror(catgets(catd
, CATSET
, 189, "temporary mail file"));
1485 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
1492 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1495 to
= fixhead(&head
, to
);
1496 if (infix_resend(ibuf
, nfo
, mp
, head
.h_to
, add_resent
) != 0) {
1499 savedeadletter(nfi
);
1500 fputs(catgets(catd
, CATSET
, 190,
1501 ". . . message not sent.\n"), stderr
);
1509 to
= outof(to
, nfi
, &head
);
1511 savedeadletter(nfi
);
1513 if (count(to
) != 0) {
1514 if (value("record-resent") == NULL
||
1515 mightrecord(nfi
, to
, 0) == OKAY
)
1516 ok
= transfer(to
, head
.h_smopts
, nfi
, NULL
);
1517 } else if (senderr
== 0)