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
36 static char sccsid[] = "@(#)send.c 8.1 (Berkeley) 6/6/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
46 * Mail -- a mail program
52 * Send message described by the passed pointer to the
53 * passed output buffer. Return -1 on error.
54 * Adjust the status: field if need be.
55 * If doign is given, suppress ignored header fields.
56 * prefix is a string to prepend to each output line.
59 sendmessage(mp, obuf, doign, prefix)
62 struct ignoretab *doign;
67 char *cp, *cp2, line[LINESIZE];
68 int ishead, infld, ignoring, dostat, firstline;
69 int c, length, prefixlen;
72 * Compute the prefix string, without trailing whitespace
76 for (cp = prefix; *cp != '\0'; cp++)
77 if (*cp != ' ' && *cp != '\t')
79 prefixlen = cp2 == NULL ? 0 : cp2 - prefix + 1;
84 dostat = doign == 0 || !isign("status", doign);
88 * Process headers first
90 while (count > 0 && ishead) {
91 if (fgets(line, sizeof(line), ibuf) == NULL)
93 count -= length = strlen(line);
96 * First line is the From line, so no headers
97 * there to worry about
100 ignoring = doign == ignoreall;
101 } else if (line[0] == '\n') {
103 * If line is blank, we've reached end of
104 * headers, so force out status: field
105 * and note that we are no longer in header
109 statusput(mp, obuf, prefix);
113 ignoring = doign == ignoreall;
114 } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
116 * If this line is a continuation (via space or tab)
117 * of a previous header field, just echo it
118 * (unless the field should be ignored).
119 * In other words, nothing to do.
123 * Pick up the header field if we have one.
125 for (cp = line; (c = *cp++) != '\0' && c != ':' &&
126 !isspace((unsigned char)c);)
129 while (isspace((unsigned char)*cp++))
133 * Not a header line, force out status:
134 * This happens in uucp style mail where
135 * there are no headers at all.
138 statusput(mp, obuf, prefix);
141 if (doign != ignoreall)
143 (void)putc('\n', obuf);
148 * If it is an ignored field and
149 * we care about such things, skip it.
151 *cp2 = '\0'; /* temporarily null terminate */
152 if (doign && isign(line, doign))
154 else if ((line[0] == 's' || line[0] == 'S') &&
155 strcasecmp(line, "status") == 0) {
157 * If the field is "status," go compute
158 * and print the real Status: field
161 statusput(mp, obuf, prefix);
167 *cp2 = c; /* restore */
174 * Strip trailing whitespace from prefix
177 if (prefix != NULL) {
181 (void)fwrite(prefix, sizeof(*prefix),
184 (void)fwrite(line, sizeof(*line), length, obuf);
190 * Copy out message body
192 if (doign == ignoreall)
193 count--; /* skip final blank line */
196 if (fgets(line, sizeof(line), ibuf) == NULL) {
200 count -= c = strlen(line);
202 * Strip trailing whitespace from prefix
208 (void)fwrite(prefix, sizeof(*prefix),
210 (void)fwrite(line, sizeof(*line), c, obuf);
216 c = count < LINESIZE ? count : LINESIZE;
217 if ((c = fread(line, sizeof(*line), c, ibuf)) <= 0)
220 if (fwrite(line, sizeof(*line), c, obuf) != c)
223 if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
224 /* no final blank line */
225 if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
231 * Output a reasonable looking status field.
234 statusput(mp, obuf, prefix)
242 if (mp->m_flag & MREAD)
244 if ((mp->m_flag & MNEW) == 0)
247 if (statout[0] != '\0')
248 fprintf(obuf, "%sStatus: %s\n",
249 prefix == NULL ? "" : prefix, statout);
253 * Interface between the argument list and the mail1 routine
254 * which does all the dirty work.
257 mail(to, cc, bcc, smopts, subject, replyto)
258 struct name *to, *cc, *bcc, *smopts;
259 char *subject, *replyto;
264 head.h_subject = subject;
267 head.h_smopts = smopts;
268 head.h_replyto = replyto;
269 head.h_inreplyto = NULL;
276 * Send mail to a bunch of user names. The interface is through
277 * the mail routine below.
285 head.h_to = extract(str, GTO);
286 head.h_subject = NULL;
289 head.h_smopts = NULL;
290 head.h_replyto = value("REPLYTO");
291 head.h_inreplyto = NULL;
297 * Mail a message on standard input to the people indicated
298 * in the passed header. (Internal interface).
301 mail1(hp, printheaders)
309 struct name *to, *nsto;
313 * Collect user's mail from standard input.
314 * Get the result as mtf.
316 if ((mtf = collect(hp, printheaders)) == NULL)
318 if (value("interactive") != NULL) {
319 if (value("askcc") != NULL || value("askbcc") != NULL) {
320 if (value("askcc") != NULL)
322 if (value("askbcc") != NULL)
326 (void)fflush(stdout);
329 if (fsize(mtf) == 0) {
330 if (value("dontsendempty") != NULL)
332 if (hp->h_subject == NULL)
333 printf("No message, no subject; hope that's ok\n");
335 printf("Null message body; hope that's ok\n");
338 * Now, take the user names from the combined
339 * to and cc lists and do all the alias
343 to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
345 printf("No recipients specified\n");
349 * Look through the recipient list for names with /'s
350 * in them which we write to as files directly.
352 to = outof(to, mtf, hp);
358 if (value("recordrecip") != NULL) {
360 * Before fixing the header, save old To:.
361 * We do this because elide above has sorted To: list, and
362 * we would like to save message in a file named by the first
363 * recipient the user has entered, not the one being the first
364 * after sorting happened.
366 if ((nsto = malloc(sizeof(struct name))) == NULL)
367 err(1, "Out of memory");
368 bcopy(hp->h_to, nsto, sizeof(struct name));
371 if ((mtf = infix(hp, mtf)) == NULL) {
372 fprintf(stderr, ". . . message lost, sorry.\n");
375 namelist = unpack(cat(hp->h_smopts, to));
379 printf("Sendmail arguments:");
380 for (t = namelist; *t != NULL; t++)
381 printf(" \"%s\"", *t);
385 if (value("recordrecip") != NULL) {
387 * Extract first recipient username from saved To: and use it
390 if ((nbuf = malloc(strlen(detract(nsto, 0)) + 1)) == NULL)
391 err(1, "Out of memory");
392 if ((cp = yanklogin(detract(nsto, 0), nbuf)) != NULL)
393 (void)savemail(expand(nbuf), mtf);
396 } else if ((cp = value("record")) != NULL)
397 (void)savemail(expand(cp), mtf);
399 * Fork, set up the temporary mail file as standard
400 * input for "mail", and exec with the user list we generated
411 (void)sigemptyset(&nset);
412 (void)sigaddset(&nset, SIGHUP);
413 (void)sigaddset(&nset, SIGINT);
414 (void)sigaddset(&nset, SIGQUIT);
415 (void)sigaddset(&nset, SIGTSTP);
416 (void)sigaddset(&nset, SIGTTIN);
417 (void)sigaddset(&nset, SIGTTOU);
418 prepare_child(&nset, fileno(mtf), -1);
419 if ((cp = value("sendmail")) != NULL)
427 if (value("verbose") != NULL)
428 (void)wait_child(pid);
436 * Fix the header by glopping all of the expanded names from
437 * the distribution list into the appropriate fields.
449 for (np = tolist; np != NULL; np = np->n_flink) {
450 /* Don't copy deleted addresses to the header */
451 if (np->n_type & GDEL)
453 if ((np->n_type & GMASK) == GTO)
455 cat(hp->h_to, nalloc(np->n_name, np->n_type));
456 else if ((np->n_type & GMASK) == GCC)
458 cat(hp->h_cc, nalloc(np->n_name, np->n_type));
459 else if ((np->n_type & GMASK) == GBCC)
461 cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
466 * Prepend a header in front of the collected stuff
467 * and return the new file.
476 char tempname[PATHSIZE];
478 (void)snprintf(tempname, sizeof(tempname),
479 "%s/mail.RsXXXXXXXXXX", tmpdir);
480 if ((fd = mkstemp(tempname)) == -1 ||
481 (nfo = Fdopen(fd, "w")) == NULL) {
482 warn("%s", tempname);
485 if ((nfi = Fopen(tempname, "r")) == NULL) {
486 warn("%s", tempname);
492 (void)puthead(hp, nfo,
493 GTO|GSUBJECT|GCC|GBCC|GREPLYTO|GINREPLYTO|GNL|GCOMMA);
506 warn("%s", tempname);
519 * Dump the to, subject, cc header on the
520 * passed file buffer.
531 if (hp->h_to != NULL && w & GTO)
532 fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
533 if (hp->h_subject != NULL && w & GSUBJECT)
534 fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
535 if (hp->h_cc != NULL && w & GCC)
536 fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
537 if (hp->h_bcc != NULL && w & GBCC)
538 fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
539 if (hp->h_replyto != NULL && w & GREPLYTO)
540 fprintf(fo, "Reply-To: %s\n", hp->h_replyto), gotcha++;
541 if (hp->h_inreplyto != NULL && w & GINREPLYTO)
542 fprintf(fo, "In-Reply-To: <%s>\n", hp->h_inreplyto), gotcha++;
543 if (gotcha && w & GNL)
544 (void)putc('\n', fo);
549 * Format the given header line to not exceed 72 characters.
552 fmt(str, np, fo, comma)
560 comma = comma ? 1 : 0;
564 for (; np != NULL; np = np->n_flink) {
565 if (np->n_flink == NULL)
567 len = strlen(np->n_name);
568 col++; /* for the space */
569 if (col + len + comma > 72 && col > 4) {
574 fputs(np->n_name, fo);
583 * Save the outgoing mail on the passed file.
597 if ((fo = Fopen(name, "a")) == NULL) {
602 fprintf(fo, "From %s %s", myname, ctime(&now));
603 while ((i = fread(buf, 1, sizeof(buf), fi)) > 0)
604 (void)fwrite(buf, 1, i, fo);