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
)
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 else switch (np
->n_type
& GMASK
) {
129 hp
->h_to
= cat(hp
->h_to
, ndup(np
, np
->n_type
|GFULL
));
132 hp
->h_cc
= cat(hp
->h_cc
, ndup(np
, np
->n_type
|GFULL
));
135 hp
->h_bcc
= cat(hp
->h_bcc
, ndup(np
, np
->n_type
|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
= ndup(np
, 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
= skinned_name(to
);
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(lextract(cp
, GCC
|GFULL
)));
959 if ((cp
= value("autobcc")) != NULL
&& *cp
)
960 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
,GBCC
|GFULL
)));
962 * Collect user's mail from standard input.
963 * Get the result as mtf.
965 if ((mtf
= collect(hp
, printheaders
, quote
, quotefile
, doprefix
,
968 if (value("interactive") != NULL
) {
969 if (((value("bsdcompat") || value("askatend"))
970 && (value("askcc") != NULL
||
971 value("askbcc") != NULL
)) ||
972 value("askattach") != NULL
||
973 value("asksign") != NULL
) {
974 if (value("askcc") != NULL
)
976 if (value("askbcc") != NULL
)
978 if (value("askattach") != NULL
)
979 hp
->h_attach
= edit_attachments(hp
->h_attach
);
980 if (value("asksign") != NULL
)
981 dosign
= yorn("Sign this message (y/n)? ");
983 printf(catgets(catd
, CATSET
, 183, "EOT\n"));
987 if (fsize(mtf
) == 0) {
990 if (hp
->h_subject
== NULL
)
991 printf(catgets(catd
, CATSET
, 184,
992 "No message, no subject; hope that's ok\n"));
993 else if (value("bsdcompat") || value("bsdmsgs"))
994 printf(catgets(catd
, CATSET
, 185,
995 "Null message body; hope that's ok\n"));
998 if (value("smime-sign") != NULL
)
1004 * Now, take the user names from the combined
1005 * to and cc lists and do all the alias
1009 if ((to
= usermap(cat(hp
->h_bcc
, cat(hp
->h_to
, hp
->h_cc
)))) == NULL
) {
1010 printf(catgets(catd
, CATSET
, 186, "No recipients specified\n"));
1013 to
= fixhead(hp
, to
);
1014 if (hp
->h_charset
) {
1015 wantcharset
= hp
->h_charset
;
1018 hloop
: wantcharset
= NULL
;
1019 if ((charsets
= value("sendcharsets")) != NULL
) {
1020 wantcharset
= savestr(charsets
);
1021 loop
: if ((ncs
= strchr(wantcharset
, ',')) != NULL
)
1024 try: if ((nmtf
= infix(hp
, mtf
, dosign
)) == NULL
) {
1025 if (hp
->h_charset
&& (errno
== EILSEQ
|| errno
== EINVAL
)) {
1027 hp
->h_charset
= NULL
;
1030 if (errno
== EILSEQ
|| errno
== EINVAL
) {
1036 if (wantcharset
&& value("interactive") == NULL
) {
1037 if (wantcharset
== (char *)-1)
1041 wantcharset
= (char *)-1;
1046 /* fprintf(stderr, ". . . message lost, sorry.\n"); */
1050 savedeadletter(mtf
);
1051 fputs(catgets(catd
, CATSET
, 187,
1052 ". . . message not sent.\n"), stderr
);
1057 if ((nmtf
= smime_sign(mtf
, hp
)) == NULL
)
1063 * Look through the recipient list for names with /'s
1064 * in them which we write to as files directly.
1066 to
= outof(to
, mtf
, hp
);
1068 savedeadletter(mtf
);
1070 if (count(to
) == 0) {
1075 if (mightrecord(mtf
, to
, recipient_record
) != OKAY
)
1077 ok
= transfer(to
, hp
->h_smopts
, mtf
, hp
);
1084 * Create a Message-Id: header field.
1085 * Use either the host name or the from address.
1088 message_id(FILE *fo
, struct header
*hp
)
1097 if ((h
= value("hostname")) != NULL
)
1099 else if ((h
= skin(myorigin(hp
))) != NULL
&& strchr(h
, '@') != NULL
)
1102 /* Delivery seems to dependent on a MTA -- it's up to it */
1105 fprintf(fo
, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
1106 tmp
->tm_year
+ 1900, tmp
->tm_mon
+ 1, tmp
->tm_mday
,
1107 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
,
1108 getrandstring(rl
), (rl
== 16 ? '%' : '@'), h
);
1111 static const char *weekday_names
[] = {
1112 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1115 const char *month_names
[] = {
1116 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1117 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
1121 * Create a Date: header field.
1122 * We compare the localtime() and gmtime() results to get the timezone,
1123 * because numeric timezones are easier to read and because $TZ is
1124 * not set on most GNU systems.
1127 mkdate(FILE *fo
, const char *field
)
1131 int tzdiff
, tzdiff_hour
, tzdiff_min
;
1134 tzdiff
= t
- mktime(gmtime(&t
));
1135 tzdiff_hour
= (int)(tzdiff
/ 60);
1136 tzdiff_min
= tzdiff_hour
% 60;
1138 tmptr
= localtime(&t
);
1139 if (tmptr
->tm_isdst
> 0)
1141 return fprintf(fo
, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1143 weekday_names
[tmptr
->tm_wday
],
1144 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
],
1145 tmptr
->tm_year
+ 1900, tmptr
->tm_hour
,
1146 tmptr
->tm_min
, tmptr
->tm_sec
,
1147 tzdiff_hour
* 100 + tzdiff_min
);
1151 putname(char *line
, enum gfield w
, enum sendaction action
, int *gotcha
,
1152 char *prefix
, FILE *fo
, struct name
**xp
)
1156 np
= sextract(line
, GEXTRA
|GFULL
);
1161 if (fmt(prefix
, np
, fo
, w
&(GCOMMA
|GFILES
), 0, action
!= SEND_TODISP
))
1168 #define FMT_CC_AND_BCC { \
1169 if (hp->h_cc != NULL && w & GCC) { \
1170 if (fmt("Cc:", hp->h_cc, fo, \
1171 w&(GCOMMA|GFILES), 0, \
1172 action!=SEND_TODISP)) \
1176 if (hp->h_bcc != NULL && w & GBCC) { \
1177 if (fmt("Bcc:", hp->h_bcc, fo, \
1178 w&(GCOMMA|GFILES), 0, \
1179 action!=SEND_TODISP)) \
1185 * Dump the to, subject, cc header on the
1186 * passed file buffer.
1189 puthead(struct header
*hp
, FILE *fo
, enum gfield w
,
1190 enum sendaction action
, enum conversion convert
,
1191 char *contenttype
, char *charset
)
1194 char *addr
/*, *cp*/;
1196 struct name
*np
, *fromfield
= NULL
, *senderfield
= NULL
;
1199 if ((addr
= value("stealthmua")) != NULL
) {
1200 stealthmua
= (strcmp(addr
, "noagent") == 0) ? -1 : 1;
1205 mkdate(fo
, "Date"), gotcha
++;
1208 if (hp
->h_from
!= NULL
) {
1209 if (fmt("From:", hp
->h_from
, fo
, w
&(GCOMMA
|GFILES
), 0,
1210 action
!=SEND_TODISP
))
1213 fromfield
= hp
->h_from
;
1214 } else if ((addr
= myaddrs(hp
)) != NULL
)
1215 if (putname(addr
, w
, action
, &gotcha
, "From:", fo
,
1218 if (((addr
= hp
->h_organization
) != NULL
||
1219 (addr
= value("ORGANIZATION")) != NULL
)
1220 && strlen(addr
) > 0) {
1221 fwrite("Organization: ", sizeof (char), 14, fo
);
1222 if (mime_write(addr
, strlen(addr
), fo
,
1223 action
== SEND_TODISP
?
1224 CONV_NONE
:CONV_TOHDR
,
1225 action
== SEND_TODISP
?
1226 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1233 if (hp
->h_replyto
!= NULL
) {
1234 if (fmt("Reply-To:", hp
->h_replyto
, fo
,
1235 w
&(GCOMMA
|GFILES
), 0,
1236 action
!=SEND_TODISP
))
1239 } else if ((addr
= value("replyto")) != NULL
)
1240 if (putname(addr
, w
, action
, &gotcha
, "Reply-To:", fo
,
1243 if (hp
->h_sender
!= NULL
) {
1244 if (fmt("Sender:", hp
->h_sender
, fo
,
1245 w
&(GCOMMA
|GFILES
), 0,
1246 action
!=SEND_TODISP
))
1249 senderfield
= hp
->h_sender
;
1250 } else if ((addr
= value("sender")) != NULL
)
1251 if (putname(addr
, w
, action
, &gotcha
, "Sender:", fo
,
1254 if (check_from_and_sender(fromfield
, senderfield
))
1257 if (hp
->h_to
!= NULL
&& w
& GTO
) {
1258 if (fmt("To:", hp
->h_to
, fo
, w
&(GCOMMA
|GFILES
), 0,
1259 action
!=SEND_TODISP
))
1263 if (value("bsdcompat") == NULL
&& value("bsdorder") == NULL
)
1265 if (hp
->h_subject
!= NULL
&& w
& GSUBJECT
) {
1266 fwrite("Subject: ", sizeof (char), 9, fo
);
1267 if (ascncasecmp(hp
->h_subject
, "re: ", 4) == 0) {
1268 fwrite("Re: ", sizeof (char), 4, fo
);
1269 if (strlen(hp
->h_subject
+ 4) > 0 &&
1270 mime_write(hp
->h_subject
+ 4,
1271 strlen(hp
->h_subject
+ 4),
1272 fo
, action
== SEND_TODISP
?
1273 CONV_NONE
:CONV_TOHDR
,
1274 action
== SEND_TODISP
?
1275 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1279 } else if (*hp
->h_subject
) {
1280 if (mime_write(hp
->h_subject
,
1281 strlen(hp
->h_subject
),
1282 fo
, action
== SEND_TODISP
?
1283 CONV_NONE
:CONV_TOHDR
,
1284 action
== SEND_TODISP
?
1285 TD_ISPR
|TD_ICONV
:TD_ICONV
,
1291 fwrite("\n", sizeof (char), 1, fo
);
1293 if (value("bsdcompat") || value("bsdorder"))
1295 if (w
& GMSGID
&& stealthmua
<= 0)
1296 message_id(fo
, hp
), gotcha
++;
1297 if (hp
->h_ref
!= NULL
&& w
& GREF
) {
1298 fmt("References:", hp
->h_ref
, fo
, 0, 1, 0);
1299 if ((np
= hp
->h_ref
) != NULL
&& np
->n_name
) {
1302 if (mime_name_invalid(np
, 0) == 0) {
1303 fprintf(fo
, "In-Reply-To: %s\n", np
->n_name
);
1308 if (w
& GUA
&& stealthmua
== 0)
1309 fprintf(fo
, "User-Agent: %s %s\n", uagent
, version
), gotcha
++;
1311 fputs("MIME-Version: 1.0\n", fo
), gotcha
++;
1312 if (hp
->h_attach
!= NULL
) {
1314 fprintf(fo
, "Content-Type: multipart/mixed;\n"
1315 " boundary=\"%s\"\n", send_boundary
);
1317 fprintf(fo
, "Content-Type: %s", contenttype
);
1319 fprintf(fo
, "; charset=%s", charset
);
1320 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n",
1321 getencoding(convert
));
1324 if (gotcha
&& w
& GNL
)
1330 * Format the given header line to not exceed 72 characters.
1333 fmt(char *str
, struct name
*np
, FILE *fo
, int flags
, int dropinvalid
,
1341 } m
= (flags
& GCOMMA
) ? m_COMMA
: 0;
1346 fwrite(str
, sizeof *str
, strlen(str
), fo
);
1347 if ((flags
&GFILES
) == 0 && ! value("add-file-recipients") &&
1348 ((col
== 3 && ((asccasecmp(str
, "to:") == 0) ||
1349 asccasecmp(str
, "cc:") == 0)) ||
1350 (col
== 4 && asccasecmp(str
, "bcc:") == 0) ||
1352 asccasecmp(str
, "Resent-To:") == 0)))
1355 for (; np
!= NULL
; np
= np
->n_flink
) {
1356 if ((m
& m_NOPF
) && is_fileorpipe_addr(np
))
1358 if (mime_name_invalid(np
, ! dropinvalid
)) {
1364 if ((m
& (m_INIT
| m_COMMA
)) == (m_INIT
| m_COMMA
)) {
1369 len
= strlen(np
->n_fullname
);
1370 ++col
; /* The separating space */
1371 if ((m
& m_INIT
) && col
> 1 && col
+ len
> 72) {
1377 m
= (m
& ~m_CSEEN
) | m_INIT
;
1378 len
= mime_write(np
->n_fullname
,
1380 domime
?CONV_TOHDR_A
:CONV_NONE
,
1381 TD_ICONV
, NULL
, (size_t)0,
1390 * Rewrite a message for resending, adding the Resent-Headers.
1393 infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
, struct name
*to
,
1397 char *buf
= NULL
, *cp
/*, *cp2*/;
1398 size_t c
, bufsize
= 0;
1399 struct name
*fromfield
= NULL
, *senderfield
= NULL
;
1403 * Write the Resent-Fields.
1406 fputs("Resent-", fo
);
1408 if ((cp
= myaddrs(NULL
)) != NULL
) {
1409 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1410 "Resent-From:", fo
, &fromfield
))
1413 if ((cp
= value("sender")) != NULL
) {
1414 if (putname(cp
, GCOMMA
, SEND_MBOX
, NULL
,
1415 "Resent-Sender:", fo
, &senderfield
))
1418 if (fmt("Resent-To:", to
, fo
, 1, 1, 0)) {
1423 if ((cp
= value("stealthmua")) == NULL
||
1424 strcmp(cp
, "noagent") == 0) {
1425 fputs("Resent-", fo
);
1426 message_id(fo
, NULL
);
1429 if (check_from_and_sender(fromfield
, senderfield
))
1432 * Write the original headers.
1435 if ((cp
= foldergets(&buf
, &bufsize
, &count
, &c
, fi
)) == NULL
)
1437 if (ascncasecmp("status: ", buf
, 8) != 0
1438 && strncmp("From ", buf
, 5) != 0) {
1439 fwrite(buf
, sizeof *buf
, c
, fo
);
1441 if (count
> 0 && *buf
== '\n')
1445 * Write the message body.
1448 if (foldergets(&buf
, &bufsize
, &count
, &c
, fi
) == NULL
)
1450 if (count
== 0 && *buf
== '\n')
1452 fwrite(buf
, sizeof *buf
, c
, fo
);
1457 perror(catgets(catd
, CATSET
, 188, "temporary mail file"));
1464 resend_msg(struct message
*mp
, struct name
*to
, int add_resent
)
1466 FILE *ibuf
, *nfo
, *nfi
;
1469 enum okay ok
= STOP
;
1471 memset(&head
, 0, sizeof head
);
1472 if ((to
= checkaddrs(to
)) == NULL
) {
1476 if ((nfo
= Ftemp(&tempMail
, "Rs", "w", 0600, 1)) == NULL
) {
1478 perror(catgets(catd
, CATSET
, 189, "temporary mail file"));
1481 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
1488 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1491 to
= fixhead(&head
, to
);
1492 if (infix_resend(ibuf
, nfo
, mp
, head
.h_to
, add_resent
) != 0) {
1495 savedeadletter(nfi
);
1496 fputs(catgets(catd
, CATSET
, 190,
1497 ". . . message not sent.\n"), stderr
);
1505 to
= outof(to
, nfi
, &head
);
1507 savedeadletter(nfi
);
1509 if (count(to
) != 0) {
1510 if (value("record-resent") == NULL
||
1511 mightrecord(nfi
, to
, 0) == OKAY
)
1512 ok
= transfer(to
, head
.h_smopts
, nfi
, NULL
);
1513 } else if (senderr
== 0)