2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)send.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/mail/send.c,v 1.5.6.5 2003/01/06 05:46:03 mikeh Exp $
41 * Mail -- a mail program
47 * Send message described by the passed pointer to the
48 * passed output buffer. Return -1 on error.
49 * Adjust the status: field if need be.
50 * If doign is given, suppress ignored header fields.
51 * prefix is a string to prepend to each output line.
54 sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
59 char *cp, *cp2, line[LINESIZE];
60 int ishead, infld, ignoring, dostat, firstline;
61 int c, length, prefixlen;
64 * Compute the prefix string, without trailing whitespace
68 for (cp = prefix; *cp != '\0'; cp++)
69 if (*cp != ' ' && *cp != '\t')
71 prefixlen = cp2 == NULL ? 0 : cp2 - prefix + 1;
76 dostat = doign == NULL || !isign("status", doign);
80 * Process headers first
82 while (count > 0 && ishead) {
83 if (fgets(line, sizeof(line), ibuf) == NULL)
85 count -= length = strlen(line);
88 * First line is the From line, so no headers
89 * there to worry about
92 ignoring = doign == ignoreall;
93 } else if (line[0] == '\n') {
95 * If line is blank, we've reached end of
96 * headers, so force out status: field
97 * and note that we are no longer in header
101 statusput(mp, obuf, prefix);
105 ignoring = doign == ignoreall;
106 } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
108 * If this line is a continuation (via space or tab)
109 * of a previous header field, just echo it
110 * (unless the field should be ignored).
111 * In other words, nothing to do.
115 * Pick up the header field if we have one.
117 for (cp = line; (c = *cp++) != '\0' && c != ':' &&
118 !isspace((unsigned char)c);)
121 while (isspace((unsigned char)*cp++))
125 * Not a header line, force out status:
126 * This happens in uucp style mail where
127 * there are no headers at all.
130 statusput(mp, obuf, prefix);
133 if (doign != ignoreall)
140 * If it is an ignored field and
141 * we care about such things, skip it.
143 *cp2 = '\0'; /* temporarily null terminate */
144 if (doign && isign(line, doign))
146 else if ((line[0] == 's' || line[0] == 'S') &&
147 strcasecmp(line, "status") == 0) {
149 * If the field is "status," go compute
150 * and print the real Status: field
153 statusput(mp, obuf, prefix);
159 *cp2 = c; /* restore */
166 * Strip trailing whitespace from prefix
169 if (prefix != NULL) {
173 fwrite(prefix, sizeof(*prefix),
176 fwrite(line, sizeof(*line), length, obuf);
182 * Copy out message body
184 if (doign == ignoreall)
185 count--; /* skip final blank line */
188 if (fgets(line, sizeof(line), ibuf) == NULL) {
192 count -= c = strlen(line);
194 * Strip trailing whitespace from prefix
200 fwrite(prefix, sizeof(*prefix),
202 fwrite(line, sizeof(*line), c, obuf);
208 c = count < LINESIZE ? count : LINESIZE;
209 if ((c = fread(line, sizeof(*line), c, ibuf)) <= 0)
212 if (fwrite(line, sizeof(*line), c, obuf) != c)
215 if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
216 /* no final blank line */
217 if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
223 * Output a reasonable looking status field.
226 statusput(struct message *mp, FILE *obuf, char *prefix)
231 if (mp->m_flag & MREAD)
233 if ((mp->m_flag & MNEW) == 0)
236 if (statout[0] != '\0')
237 fprintf(obuf, "%sStatus: %s\n",
238 prefix == NULL ? "" : prefix, statout);
242 * Interface between the argument list and the mail1 routine
243 * which does all the dirty work.
246 mail(struct name *to, struct name *cc, struct name *bcc, struct name *smopts,
247 char *subject, char *replyto)
252 head.h_subject = subject;
255 head.h_smopts = smopts;
256 head.h_replyto = replyto;
257 head.h_inreplyto = NULL;
264 * Send mail to a bunch of user names. The interface is through
265 * the mail routine below.
272 head.h_to = extract(str, GTO);
273 head.h_subject = NULL;
276 head.h_smopts = NULL;
277 head.h_replyto = value("REPLYTO");
278 head.h_inreplyto = NULL;
284 * Mail a message on standard input to the people indicated
285 * in the passed header. (Internal interface).
288 mail1(struct header *hp, int printheaders)
294 struct name *to, *nsto;
298 * Collect user's mail from standard input.
299 * Get the result as mtf.
301 if ((mtf = collect(hp, printheaders)) == NULL)
303 if (value("interactive") != NULL) {
304 if (value("askcc") != NULL || value("askbcc") != NULL) {
305 if (value("askcc") != NULL)
307 if (value("askbcc") != NULL)
314 if (fsize(mtf) == 0) {
315 if (value("dontsendempty") != NULL)
317 if (hp->h_subject == NULL)
318 printf("No message, no subject; hope that's ok\n");
320 printf("Null message body; hope that's ok\n");
323 * Now, take the user names from the combined
324 * to and cc lists and do all the alias
328 to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
330 printf("No recipients specified\n");
334 * Look through the recipient list for names with /'s
335 * in them which we write to as files directly.
337 to = outof(to, mtf, hp);
343 if (value("recordrecip") != NULL) {
345 * Before fixing the header, save old To:.
346 * We do this because elide above has sorted To: list,
347 * and we would like to save the message in a file named by
348 * the first recipient the user has entered, not the one being
349 * the first after sorting happened.
351 if ((nsto = malloc(sizeof(struct name))) == NULL)
352 err(1, "Out of memory");
353 bcopy(hp->h_to, nsto, sizeof(struct name));
356 if ((mtf = infix(hp, mtf)) == NULL) {
357 fprintf(stderr, ". . . message lost, sorry.\n");
360 namelist = unpack(cat(hp->h_smopts, to));
364 printf("Sendmail arguments:");
365 for (t = namelist; *t != NULL; t++)
366 printf(" \"%s\"", *t);
370 if (value("recordrecip") != NULL) {
372 * Extract first recipient username from save To: and use it
375 if ((nbuf = malloc(strlen(detract(nsto, 0)) + 1)) == NULL)
376 err(1, "Out of memory");
377 if ((cp = yanklogin(detract(nsto, 0), nbuf)) != NULL)
378 savemail(expand(nbuf), mtf);
381 } else if ((cp = value("record")) != NULL)
382 savemail(expand(cp), mtf);
384 * Fork, set up the temporary mail file as standard
385 * input for "mail", and exec with the user list we generated
397 sigaddset(&nset, SIGHUP);
398 sigaddset(&nset, SIGINT);
399 sigaddset(&nset, SIGQUIT);
400 sigaddset(&nset, SIGTSTP);
401 sigaddset(&nset, SIGTTIN);
402 sigaddset(&nset, SIGTTOU);
403 prepare_child(&nset, fileno(mtf), -1);
404 if ((cp = value("sendmail")) != NULL)
412 if (value("verbose") != NULL)
421 * Fix the header by glopping all of the expanded names from
422 * the distribution list into the appropriate fields.
425 fixhead(struct header *hp, struct name *tolist)
432 for (np = tolist; np != NULL; np = np->n_flink) {
433 /* Don't copy deleted addresses to the header */
434 if (np->n_type & GDEL)
436 if ((np->n_type & GMASK) == GTO)
438 cat(hp->h_to, nalloc(np->n_name, np->n_type));
439 else if ((np->n_type & GMASK) == GCC)
441 cat(hp->h_cc, nalloc(np->n_name, np->n_type));
442 else if ((np->n_type & GMASK) == GBCC)
444 cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
449 * Prepend a header in front of the collected stuff
450 * and return the new file.
453 infix(struct header *hp, FILE *fi)
457 char tempname[PATHSIZE];
459 snprintf(tempname, sizeof(tempname), "%s/mail.RsXXXXXXXXXX", tmpdir);
460 if ((fd = mkstemp(tempname)) == -1 ||
461 (nfo = Fdopen(fd, "w")) == NULL) {
462 warn("%s", tempname);
465 if ((nfi = Fopen(tempname, "r")) == NULL) {
466 warn("%s", tempname);
472 puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GREPLYTO|GINREPLYTO|GNL|GCOMMA);
485 warn("%s", tempname);
498 * Dump the to, subject, cc header on the
499 * passed file buffer.
502 puthead(struct header *hp, FILE *fo, int w)
507 if (hp->h_to != NULL && w & GTO)
508 fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
509 if (hp->h_subject != NULL && w & GSUBJECT)
510 fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
511 if (hp->h_cc != NULL && w & GCC)
512 fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
513 if (hp->h_bcc != NULL && w & GBCC)
514 fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
515 if (hp->h_replyto != NULL && w & GREPLYTO)
516 fprintf(fo, "Reply-To: %s\n", hp->h_replyto), gotcha++;
517 if (hp->h_inreplyto != NULL && w & GINREPLYTO)
518 fprintf(fo, "In-Reply-To: <%s>\n", hp->h_inreplyto), gotcha++;
519 if (gotcha && w & GNL)
525 * Format the given header line to not exceed 72 characters.
528 fmt(const char *str, struct name *np, FILE *fo, int comma)
532 comma = comma ? 1 : 0;
536 for (; np != NULL; np = np->n_flink) {
537 if (np->n_flink == NULL)
539 len = strlen(np->n_name);
540 col++; /* for the space */
541 if (col + len + comma > 72 && col > 4) {
546 fputs(np->n_name, fo);
555 * Save the outgoing mail on the passed file.
560 savemail(char *name, FILE *fi)
567 if ((fo = Fopen(name, "a")) == NULL) {
572 fprintf(fo, "From %s %s", myname, ctime(&now));
573 while ((i = fread(buf, 1, sizeof(buf), fi)) > 0)
574 fwrite(buf, 1, i, fo);