122465Sdist /*
2*62083Sbostic * Copyright (c) 1980, 1993
3*62083Sbostic * The Regents of the University of California. All rights reserved.
433499Sbostic *
542741Sbostic * %sccs.include.redist.c%
622465Sdist */
722465Sdist
834905Sbostic #ifndef lint
9*62083Sbostic static char sccsid[] = "@(#)send.c 8.1 (Berkeley) 06/06/93";
1034905Sbostic #endif /* not lint */
111244Skas
121244Skas #include "rcv.h"
1354505Sbostic #include "extern.h"
141244Skas
151244Skas /*
161244Skas * Mail -- a mail program
171244Skas *
181244Skas * Mail to others.
191244Skas */
201244Skas
211244Skas /*
221244Skas * Send message described by the passed pointer to the
2334969Sedward * passed output buffer. Return -1 on error.
2434969Sedward * Adjust the status: field if need be.
2534969Sedward * If doign is given, suppress ignored header fields.
2634969Sedward * prefix is a string to prepend to each output line.
271244Skas */
2854505Sbostic int
send(mp,obuf,doign,prefix)2934969Sedward send(mp, obuf, doign, prefix)
3031142Sedward register struct message *mp;
311244Skas FILE *obuf;
3234692Sedward struct ignoretab *doign;
3334969Sedward char *prefix;
341244Skas {
3531142Sedward long count;
3631142Sedward register FILE *ibuf;
3731142Sedward char line[LINESIZE];
3834969Sedward int ishead, infld, ignoring, dostat, firstline;
3931142Sedward register char *cp, *cp2;
4031142Sedward register int c;
4131142Sedward int length;
4239905Sedward int prefixlen;
431244Skas
4439905Sedward /*
4539905Sedward * Compute the prefix string, without trailing whitespace
4639905Sedward */
4744894Sedward if (prefix != NOSTR) {
4844894Sedward cp2 = 0;
4944894Sedward for (cp = prefix; *cp; cp++)
5044894Sedward if (*cp != ' ' && *cp != '\t')
5144894Sedward cp2 = cp;
5244894Sedward prefixlen = cp2 == 0 ? 0 : cp2 - prefix + 1;
5344894Sedward }
541244Skas ibuf = setinput(mp);
5531142Sedward count = mp->m_size;
567581Skurt ishead = 1;
5734692Sedward dostat = doign == 0 || !isign("status", doign);
583239Skas infld = 0;
5934969Sedward firstline = 1;
6031142Sedward /*
6131142Sedward * Process headers first
6231142Sedward */
6331142Sedward while (count > 0 && ishead) {
6431142Sedward if (fgets(line, LINESIZE, ibuf) == NULL)
6531142Sedward break;
6631142Sedward count -= length = strlen(line);
6734969Sedward if (firstline) {
687581Skurt /*
697581Skurt * First line is the From line, so no headers
707581Skurt * there to worry about
717581Skurt */
7234969Sedward firstline = 0;
7334969Sedward ignoring = doign == ignoreall;
7431142Sedward } else if (line[0] == '\n') {
757581Skurt /*
767581Skurt * If line is blank, we've reached end of
777581Skurt * headers, so force out status: field
787581Skurt * and note that we are no longer in header
797581Skurt * fields
807581Skurt */
8131142Sedward if (dostat) {
8234969Sedward statusput(mp, obuf, prefix);
8331142Sedward dostat = 0;
841487Skas }
8531142Sedward ishead = 0;
8634969Sedward ignoring = doign == ignoreall;
8731142Sedward } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
887581Skurt /*
899290Sbush * If this line is a continuation (via space or tab)
909290Sbush * of a previous header field, just echo it
919290Sbush * (unless the field should be ignored).
9231142Sedward * In other words, nothing to do.
937581Skurt */
9431142Sedward } else {
957581Skurt /*
9631142Sedward * Pick up the header field if we have one.
977581Skurt */
9831142Sedward for (cp = line; (c = *cp++) && c != ':' && !isspace(c);)
9931142Sedward ;
10031142Sedward cp2 = --cp;
10131142Sedward while (isspace(*cp++))
10231142Sedward ;
10331142Sedward if (cp[-1] != ':') {
10431142Sedward /*
10531142Sedward * Not a header line, force out status:
10631142Sedward * This happens in uucp style mail where
10731142Sedward * there are no headers at all.
10831142Sedward */
1097581Skurt if (dostat) {
11034969Sedward statusput(mp, obuf, prefix);
1117581Skurt dostat = 0;
1127581Skurt }
11334969Sedward if (doign != ignoreall)
11434969Sedward /* add blank line */
11534969Sedward (void) putc('\n', obuf);
1163239Skas ishead = 0;
11731142Sedward ignoring = 0;
11831142Sedward } else {
11931142Sedward /*
12031142Sedward * If it is an ignored field and
12131142Sedward * we care about such things, skip it.
12231142Sedward */
12331142Sedward *cp2 = 0; /* temporarily null terminate */
12434692Sedward if (doign && isign(line, doign))
12531142Sedward ignoring = 1;
12631142Sedward else if ((line[0] == 's' || line[0] == 'S') &&
12734987Sedward strcasecmp(line, "status") == 0) {
12831142Sedward /*
12931142Sedward * If the field is "status," go compute
13031142Sedward * and print the real Status: field
13131142Sedward */
13231142Sedward if (dostat) {
13334969Sedward statusput(mp, obuf, prefix);
13431142Sedward dostat = 0;
13531142Sedward }
13631142Sedward ignoring = 1;
13731142Sedward } else {
13831142Sedward ignoring = 0;
13931142Sedward *cp2 = c; /* restore */
1407583Skurt }
14131142Sedward infld = 1;
1427583Skurt }
1431487Skas }
14431142Sedward if (!ignoring) {
14539905Sedward /*
14639905Sedward * Strip trailing whitespace from prefix
14739905Sedward * if line is blank.
14839905Sedward */
14939905Sedward if (prefix != NOSTR)
15039905Sedward if (length > 1)
15139905Sedward fputs(prefix, obuf);
15239905Sedward else
15339905Sedward (void) fwrite(prefix, sizeof *prefix,
15439905Sedward prefixlen, obuf);
15531143Sedward (void) fwrite(line, sizeof *line, length, obuf);
15631142Sedward if (ferror(obuf))
15731142Sedward return -1;
15831142Sedward }
1591244Skas }
16031142Sedward /*
16131142Sedward * Copy out message body
16231142Sedward */
16334969Sedward if (doign == ignoreall)
16434969Sedward count--; /* skip final blank line */
16534969Sedward if (prefix != NOSTR)
16634969Sedward while (count > 0) {
16734969Sedward if (fgets(line, LINESIZE, ibuf) == NULL) {
16834969Sedward c = 0;
16934969Sedward break;
17034969Sedward }
17134969Sedward count -= c = strlen(line);
17239905Sedward /*
17339905Sedward * Strip trailing whitespace from prefix
17439905Sedward * if line is blank.
17539905Sedward */
17634969Sedward if (c > 1)
17734969Sedward fputs(prefix, obuf);
17839905Sedward else
17939905Sedward (void) fwrite(prefix, sizeof *prefix,
18039905Sedward prefixlen, obuf);
18134969Sedward (void) fwrite(line, sizeof *line, c, obuf);
18234969Sedward if (ferror(obuf))
18334969Sedward return -1;
18434969Sedward }
18534969Sedward else
18634969Sedward while (count > 0) {
18734969Sedward c = count < LINESIZE ? count : LINESIZE;
18834969Sedward if ((c = fread(line, sizeof *line, c, ibuf)) <= 0)
18934969Sedward break;
19034969Sedward count -= c;
19134969Sedward if (fwrite(line, sizeof *line, c, obuf) != c)
19234969Sedward return -1;
19334969Sedward }
19434969Sedward if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
19534969Sedward /* no final blank line */
19634969Sedward if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
19731142Sedward return -1;
19834969Sedward return 0;
1991244Skas }
2001244Skas
2011244Skas /*
2021487Skas * Output a reasonable looking status field.
2031487Skas */
20454505Sbostic void
statusput(mp,obuf,prefix)20534969Sedward statusput(mp, obuf, prefix)
2061487Skas register struct message *mp;
20731142Sedward FILE *obuf;
20834969Sedward char *prefix;
2091487Skas {
2101487Skas char statout[3];
21131142Sedward register char *cp = statout;
2121487Skas
2131487Skas if (mp->m_flag & MREAD)
21431142Sedward *cp++ = 'R';
2151487Skas if ((mp->m_flag & MNEW) == 0)
21631142Sedward *cp++ = 'O';
21731142Sedward *cp = 0;
21831142Sedward if (statout[0])
21934969Sedward fprintf(obuf, "%sStatus: %s\n",
22034969Sedward prefix == NOSTR ? "" : prefix, statout);
2211487Skas }
2221487Skas
2231487Skas /*
2241244Skas * Interface between the argument list and the mail1 routine
2251244Skas * which does all the dirty work.
2261244Skas */
22754505Sbostic int
mail(to,cc,bcc,smopts,subject)22834800Sedward mail(to, cc, bcc, smopts, subject)
22934702Sedward struct name *to, *cc, *bcc, *smopts;
23034800Sedward char *subject;
2311244Skas {
2321244Skas struct header head;
2331244Skas
23434800Sedward head.h_to = to;
23534800Sedward head.h_subject = subject;
23634800Sedward head.h_cc = cc;
23734800Sedward head.h_bcc = bcc;
23834800Sedward head.h_smopts = smopts;
23934976Sedward mail1(&head, 0);
2401244Skas return(0);
2411244Skas }
2421244Skas
2431244Skas
2441244Skas /*
2451244Skas * Send mail to a bunch of user names. The interface is through
2461244Skas * the mail routine below.
2471244Skas */
24854505Sbostic int
sendmail(str)2491244Skas sendmail(str)
2501244Skas char *str;
2511244Skas {
2521244Skas struct header head;
2531244Skas
25434800Sedward head.h_to = extract(str, GTO);
2551244Skas head.h_subject = NOSTR;
25634800Sedward head.h_cc = NIL;
25734800Sedward head.h_bcc = NIL;
25834800Sedward head.h_smopts = NIL;
25934976Sedward mail1(&head, 0);
2601244Skas return(0);
2611244Skas }
2621244Skas
2631244Skas /*
2641244Skas * Mail a message on standard input to the people indicated
2651244Skas * in the passed header. (Internal interface).
2661244Skas */
26754505Sbostic void
mail1(hp,printheaders)26834800Sedward mail1(hp, printheaders)
2691244Skas struct header *hp;
27054505Sbostic int printheaders;
2711244Skas {
27234976Sedward char *cp;
27334976Sedward int pid;
27434976Sedward char **namelist;
27534976Sedward struct name *to;
27634976Sedward FILE *mtf;
2771244Skas
2781244Skas /*
2791244Skas * Collect user's mail from standard input.
2801244Skas * Get the result as mtf.
2811244Skas */
28234800Sedward if ((mtf = collect(hp, printheaders)) == NULL)
28334976Sedward return;
28434750Sedward if (value("interactive") != NOSTR)
28534750Sedward if (value("askcc") != NOSTR)
28634750Sedward grabh(hp, GCC);
28734750Sedward else {
28834750Sedward printf("EOT\n");
28934750Sedward (void) fflush(stdout);
29034750Sedward }
29134976Sedward if (fsize(mtf) == 0)
29234976Sedward if (hp->h_subject == NOSTR)
29334976Sedward printf("No message, no subject; hope that's ok\n");
29434976Sedward else
29534976Sedward printf("Null message body; hope that's ok\n");
2961244Skas /*
2971244Skas * Now, take the user names from the combined
2981244Skas * to and cc lists and do all the alias
2991244Skas * processing.
3001244Skas */
3011244Skas senderr = 0;
30234800Sedward to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
3031244Skas if (to == NIL) {
3041244Skas printf("No recipients specified\n");
30534976Sedward senderr++;
3061244Skas }
3071244Skas /*
3081244Skas * Look through the recipient list for names with /'s
3091244Skas * in them which we write to as files directly.
3101244Skas */
3111244Skas to = outof(to, mtf, hp);
31235351Sedward if (senderr)
31335351Sedward savedeadletter(mtf);
31434976Sedward to = elide(to);
31534976Sedward if (count(to) == 0)
3161244Skas goto out;
31734976Sedward fixhead(hp, to);
31834976Sedward if ((mtf = infix(hp, mtf)) == NULL) {
31934976Sedward fprintf(stderr, ". . . message lost, sorry.\n");
32034976Sedward return;
3211244Skas }
32234800Sedward namelist = unpack(cat(hp->h_smopts, to));
3231244Skas if (debug) {
32434976Sedward char **t;
32534976Sedward
32634800Sedward printf("Sendmail arguments:");
3271244Skas for (t = namelist; *t != NOSTR; t++)
3281244Skas printf(" \"%s\"", *t);
3291244Skas printf("\n");
33034976Sedward goto out;
3311244Skas }
3321244Skas if ((cp = value("record")) != NOSTR)
33331143Sedward (void) savemail(expand(cp), mtf);
3341244Skas /*
33534976Sedward * Fork, set up the temporary mail file as standard
33634976Sedward * input for "mail", and exec with the user list we generated
33734976Sedward * far above.
3381244Skas */
3391244Skas pid = fork();
3401244Skas if (pid == -1) {
3411244Skas perror("fork");
34235351Sedward savedeadletter(mtf);
3431244Skas goto out;
3441244Skas }
3451244Skas if (pid == 0) {
34634976Sedward prepare_child(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT)|
34734976Sedward sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU),
34834976Sedward fileno(mtf), -1);
34934976Sedward if ((cp = value("sendmail")) != NOSTR)
35034976Sedward cp = expand(cp);
35134976Sedward else
35237870Sbostic cp = _PATH_SENDMAIL;
35334976Sedward execv(cp, namelist);
35434976Sedward perror(cp);
35535066Sedward _exit(1);
3561244Skas }
35734976Sedward if (value("verbose") != NOSTR)
35834976Sedward (void) wait_child(pid);
35934976Sedward else
36034976Sedward free_child(pid);
3611244Skas out:
36243865Sedward (void) Fclose(mtf);
3631244Skas }
3641244Skas
3651244Skas /*
3661244Skas * Fix the header by glopping all of the expanded names from
3671244Skas * the distribution list into the appropriate fields.
3681244Skas */
36954505Sbostic void
fixhead(hp,tolist)3701244Skas fixhead(hp, tolist)
3711244Skas struct header *hp;
3721244Skas struct name *tolist;
3731244Skas {
3741244Skas register struct name *np;
3751244Skas
37634800Sedward hp->h_to = NIL;
37734800Sedward hp->h_cc = NIL;
37834800Sedward hp->h_bcc = NIL;
37934800Sedward for (np = tolist; np != NIL; np = np->n_flink)
38034800Sedward if ((np->n_type & GMASK) == GTO)
38134800Sedward hp->h_to =
38234800Sedward cat(hp->h_to, nalloc(np->n_name, np->n_type));
38334800Sedward else if ((np->n_type & GMASK) == GCC)
38434800Sedward hp->h_cc =
38534800Sedward cat(hp->h_cc, nalloc(np->n_name, np->n_type));
38634800Sedward else if ((np->n_type & GMASK) == GBCC)
38734800Sedward hp->h_bcc =
38834800Sedward cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
3891244Skas }
3901244Skas
3911244Skas /*
3921244Skas * Prepend a header in front of the collected stuff
3931244Skas * and return the new file.
3941244Skas */
3951244Skas FILE *
infix(hp,fi)3961244Skas infix(hp, fi)
3971244Skas struct header *hp;
3981244Skas FILE *fi;
3991244Skas {
4001244Skas extern char tempMail[];
4011244Skas register FILE *nfo, *nfi;
4021244Skas register int c;
4031244Skas
40443865Sedward if ((nfo = Fopen(tempMail, "w")) == NULL) {
4051244Skas perror(tempMail);
4061244Skas return(fi);
4071244Skas }
40843865Sedward if ((nfi = Fopen(tempMail, "r")) == NULL) {
4091244Skas perror(tempMail);
41043865Sedward (void) Fclose(nfo);
4111244Skas return(fi);
4121244Skas }
41346349Sedward (void) rm(tempMail);
41434800Sedward (void) puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA);
4151244Skas c = getc(fi);
4161244Skas while (c != EOF) {
41731143Sedward (void) putc(c, nfo);
4181244Skas c = getc(fi);
4191244Skas }
4201244Skas if (ferror(fi)) {
4211244Skas perror("read");
42234976Sedward rewind(fi);
4231244Skas return(fi);
4241244Skas }
42531143Sedward (void) fflush(nfo);
4261244Skas if (ferror(nfo)) {
4271244Skas perror(tempMail);
42843865Sedward (void) Fclose(nfo);
42943865Sedward (void) Fclose(nfi);
43034976Sedward rewind(fi);
4311244Skas return(fi);
4321244Skas }
43343865Sedward (void) Fclose(nfo);
43443865Sedward (void) Fclose(fi);
4351244Skas rewind(nfi);
4361244Skas return(nfi);
4371244Skas }
4381244Skas
4391244Skas /*
4401244Skas * Dump the to, subject, cc header on the
4411244Skas * passed file buffer.
4421244Skas */
44354505Sbostic int
puthead(hp,fo,w)4441244Skas puthead(hp, fo, w)
4451244Skas struct header *hp;
4461244Skas FILE *fo;
44754505Sbostic int w;
4481244Skas {
4491244Skas register int gotcha;
4501244Skas
4511244Skas gotcha = 0;
45234800Sedward if (hp->h_to != NIL && w & GTO)
45334976Sedward fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
4541244Skas if (hp->h_subject != NOSTR && w & GSUBJECT)
4551244Skas fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
45634800Sedward if (hp->h_cc != NIL && w & GCC)
45734976Sedward fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
45834800Sedward if (hp->h_bcc != NIL && w & GBCC)
45934976Sedward fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
4601244Skas if (gotcha && w & GNL)
46131143Sedward (void) putc('\n', fo);
4621244Skas return(0);
4631244Skas }
4641244Skas
4651244Skas /*
46634976Sedward * Format the given header line to not exceed 72 characters.
4671244Skas */
46854505Sbostic void
fmt(str,np,fo,comma)46934800Sedward fmt(str, np, fo, comma)
47034800Sedward char *str;
47134800Sedward register struct name *np;
47234800Sedward FILE *fo;
47334800Sedward int comma;
4741244Skas {
47534800Sedward register col, len;
4761244Skas
47734800Sedward comma = comma ? 1 : 0;
47810580Sleres col = strlen(str);
47910580Sleres if (col)
48034800Sedward fputs(str, fo);
48134976Sedward for (; np != NIL; np = np->n_flink) {
48234800Sedward if (np->n_flink == NIL)
48334800Sedward comma = 0;
48434800Sedward len = strlen(np->n_name);
48534976Sedward col++; /* for the space */
48634976Sedward if (col + len + comma > 72 && col > 4) {
48734800Sedward fputs("\n ", fo);
4881244Skas col = 4;
48934976Sedward } else
49034800Sedward putc(' ', fo);
49134976Sedward fputs(np->n_name, fo);
49234976Sedward if (comma)
49334976Sedward putc(',', fo);
49434976Sedward col += len + comma;
4951244Skas }
49634800Sedward putc('\n', fo);
4971244Skas }
4981244Skas
4991244Skas /*
5001244Skas * Save the outgoing mail on the passed file.
5011244Skas */
5021244Skas
50331142Sedward /*ARGSUSED*/
50454505Sbostic int
savemail(name,fi)50531142Sedward savemail(name, fi)
5061244Skas char name[];
50731142Sedward register FILE *fi;
5081244Skas {
5091244Skas register FILE *fo;
51031142Sedward char buf[BUFSIZ];
51131142Sedward register i;
51231142Sedward time_t now, time();
51331142Sedward char *ctime();
5141244Skas
51543865Sedward if ((fo = Fopen(name, "a")) == NULL) {
5161244Skas perror(name);
51731142Sedward return (-1);
5181244Skas }
51931143Sedward (void) time(&now);
52034783Sedward fprintf(fo, "From %s %s", myname, ctime(&now));
52131142Sedward while ((i = fread(buf, 1, sizeof buf, fi)) > 0)
52231143Sedward (void) fwrite(buf, 1, i, fo);
52331143Sedward (void) putc('\n', fo);
52431143Sedward (void) fflush(fo);
5251244Skas if (ferror(fo))
5261244Skas perror(name);
52743865Sedward (void) Fclose(fo);
52834976Sedward rewind(fi);
52931142Sedward return (0);
5301244Skas }
531