18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1980, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329b50d902SRodney W. Grimes /*
339b50d902SRodney W. Grimes * Mail -- a mail program
349b50d902SRodney W. Grimes *
359b50d902SRodney W. Grimes * Collect input from standard input, handling
369b50d902SRodney W. Grimes * ~ escapes.
379b50d902SRodney W. Grimes */
389b50d902SRodney W. Grimes
399b50d902SRodney W. Grimes #include "rcv.h"
4069131e40SMike Heffner #include <fcntl.h>
419b50d902SRodney W. Grimes #include "extern.h"
429b50d902SRodney W. Grimes
439b50d902SRodney W. Grimes /*
4424b797ccSPedro F. Giffuni * Read a message from standard input and return a read file to it
459b50d902SRodney W. Grimes * or NULL on error.
469b50d902SRodney W. Grimes */
479b50d902SRodney W. Grimes
489b50d902SRodney W. Grimes /*
499b50d902SRodney W. Grimes * The following hokiness with global variables is so that on
509b50d902SRodney W. Grimes * receipt of an interrupt signal, the partial message can be salted
519b50d902SRodney W. Grimes * away on dead.letter.
529b50d902SRodney W. Grimes */
539b50d902SRodney W. Grimes
549b50d902SRodney W. Grimes static sig_t saveint; /* Previous SIGINT value */
559b50d902SRodney W. Grimes static sig_t savehup; /* Previous SIGHUP value */
569b50d902SRodney W. Grimes static sig_t savetstp; /* Previous SIGTSTP value */
579b50d902SRodney W. Grimes static sig_t savettou; /* Previous SIGTTOU value */
589b50d902SRodney W. Grimes static sig_t savettin; /* Previous SIGTTIN value */
599b50d902SRodney W. Grimes static FILE *collf; /* File for saving away */
609b50d902SRodney W. Grimes static int hadintr; /* Have seen one SIGINT so far */
619b50d902SRodney W. Grimes
629b50d902SRodney W. Grimes static jmp_buf colljmp; /* To get back to work */
639b50d902SRodney W. Grimes static int colljmp_p; /* whether to long jump */
649b50d902SRodney W. Grimes static jmp_buf collabort; /* To end collection with error */
659b50d902SRodney W. Grimes
669b50d902SRodney W. Grimes FILE *
collect(struct header * hp,int printheaders)676d8484b0SPhilippe Charnier collect(struct header *hp, int printheaders)
689b50d902SRodney W. Grimes {
699b50d902SRodney W. Grimes FILE *fbuf;
709ce73e90SMike Heffner int lc, cc, escape, eofcount, fd, c, t;
719ce73e90SMike Heffner char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub;
72856f23edSMike Heffner sigset_t nset;
73856f23edSMike Heffner int longline, lastlong, rc; /* So we don't make 2 or more lines
74856f23edSMike Heffner out of a long input line. */
759b50d902SRodney W. Grimes
769b50d902SRodney W. Grimes collf = NULL;
779b50d902SRodney W. Grimes /*
789b50d902SRodney W. Grimes * Start catching signals from here, but we're still die on interrupts
799b50d902SRodney W. Grimes * until we're in the main loop.
809b50d902SRodney W. Grimes */
81856f23edSMike Heffner (void)sigemptyset(&nset);
82856f23edSMike Heffner (void)sigaddset(&nset, SIGINT);
83856f23edSMike Heffner (void)sigaddset(&nset, SIGHUP);
84856f23edSMike Heffner (void)sigprocmask(SIG_BLOCK, &nset, NULL);
859b50d902SRodney W. Grimes if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
869ce73e90SMike Heffner (void)signal(SIGINT, collint);
879b50d902SRodney W. Grimes if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
889ce73e90SMike Heffner (void)signal(SIGHUP, collhup);
899b50d902SRodney W. Grimes savetstp = signal(SIGTSTP, collstop);
909b50d902SRodney W. Grimes savettou = signal(SIGTTOU, collstop);
919b50d902SRodney W. Grimes savettin = signal(SIGTTIN, collstop);
929b50d902SRodney W. Grimes if (setjmp(collabort) || setjmp(colljmp)) {
939ce73e90SMike Heffner (void)rm(tempname);
949b50d902SRodney W. Grimes goto err;
959b50d902SRodney W. Grimes }
96856f23edSMike Heffner (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
979b50d902SRodney W. Grimes
989b50d902SRodney W. Grimes noreset++;
999ce73e90SMike Heffner (void)snprintf(tempname, sizeof(tempname),
1009ce73e90SMike Heffner "%s/mail.RsXXXXXXXXXX", tmpdir);
1010c3a8314SMike Heffner if ((fd = mkstemp(tempname)) == -1 ||
1020c3a8314SMike Heffner (collf = Fdopen(fd, "w+")) == NULL) {
1030c3a8314SMike Heffner warn("%s", tempname);
1049b50d902SRodney W. Grimes goto err;
1059b50d902SRodney W. Grimes }
1069ce73e90SMike Heffner (void)rm(tempname);
1079b50d902SRodney W. Grimes
1089b50d902SRodney W. Grimes /*
1099b50d902SRodney W. Grimes * If we are going to prompt for a subject,
1109b50d902SRodney W. Grimes * refrain from printing a newline after
1119b50d902SRodney W. Grimes * the headers (since some people mind).
1129b50d902SRodney W. Grimes */
1139b50d902SRodney W. Grimes t = GTO|GSUBJECT|GCC|GNL;
1149b50d902SRodney W. Grimes getsub = 0;
1159ce73e90SMike Heffner if (hp->h_subject == NULL && value("interactive") != NULL &&
1169ce73e90SMike Heffner (value("ask") != NULL || value("asksub") != NULL))
1179b50d902SRodney W. Grimes t &= ~GNL, getsub++;
1189b50d902SRodney W. Grimes if (printheaders) {
1199b50d902SRodney W. Grimes puthead(hp, stdout, t);
1209ce73e90SMike Heffner (void)fflush(stdout);
1219b50d902SRodney W. Grimes }
1229ce73e90SMike Heffner if ((cp = value("escape")) != NULL)
1239b50d902SRodney W. Grimes escape = *cp;
1249b50d902SRodney W. Grimes else
1259b50d902SRodney W. Grimes escape = ESCAPE;
1269b50d902SRodney W. Grimes eofcount = 0;
1279b50d902SRodney W. Grimes hadintr = 0;
128856f23edSMike Heffner longline = 0;
1299b50d902SRodney W. Grimes
1309b50d902SRodney W. Grimes if (!setjmp(colljmp)) {
1319b50d902SRodney W. Grimes if (getsub)
1329b50d902SRodney W. Grimes grabh(hp, GSUBJECT);
1339b50d902SRodney W. Grimes } else {
1349b50d902SRodney W. Grimes /*
1359b50d902SRodney W. Grimes * Come here for printing the after-signal message.
1369b50d902SRodney W. Grimes * Duplicate messages won't be printed because
1379b50d902SRodney W. Grimes * the write is aborted if we get a SIGTTOU.
1389b50d902SRodney W. Grimes */
1399b50d902SRodney W. Grimes cont:
1409b50d902SRodney W. Grimes if (hadintr) {
1419ce73e90SMike Heffner (void)fflush(stdout);
1429b50d902SRodney W. Grimes fprintf(stderr,
1439b50d902SRodney W. Grimes "\n(Interrupt -- one more to kill letter)\n");
1449b50d902SRodney W. Grimes } else {
1459b50d902SRodney W. Grimes printf("(continue)\n");
1469ce73e90SMike Heffner (void)fflush(stdout);
1479b50d902SRodney W. Grimes }
1489b50d902SRodney W. Grimes }
1499b50d902SRodney W. Grimes for (;;) {
1509b50d902SRodney W. Grimes colljmp_p = 1;
1519b50d902SRodney W. Grimes c = readline(stdin, linebuf, LINESIZE);
1529b50d902SRodney W. Grimes colljmp_p = 0;
1539b50d902SRodney W. Grimes if (c < 0) {
1549ce73e90SMike Heffner if (value("interactive") != NULL &&
1559ce73e90SMike Heffner value("ignoreeof") != NULL && ++eofcount < 25) {
1569b50d902SRodney W. Grimes printf("Use \".\" to terminate letter\n");
1579b50d902SRodney W. Grimes continue;
1589b50d902SRodney W. Grimes }
1599b50d902SRodney W. Grimes break;
1609b50d902SRodney W. Grimes }
161856f23edSMike Heffner lastlong = longline;
162856f23edSMike Heffner longline = c == LINESIZE - 1;
1639b50d902SRodney W. Grimes eofcount = 0;
1649b50d902SRodney W. Grimes hadintr = 0;
1659b50d902SRodney W. Grimes if (linebuf[0] == '.' && linebuf[1] == '\0' &&
166856f23edSMike Heffner value("interactive") != NULL && !lastlong &&
1679ce73e90SMike Heffner (value("dot") != NULL || value("ignoreeof") != NULL))
1689b50d902SRodney W. Grimes break;
169856f23edSMike Heffner if (linebuf[0] != escape || value("interactive") == NULL ||
170856f23edSMike Heffner lastlong) {
171856f23edSMike Heffner if (putline(collf, linebuf, !longline) < 0)
1729b50d902SRodney W. Grimes goto err;
1739b50d902SRodney W. Grimes continue;
1749b50d902SRodney W. Grimes }
1759b50d902SRodney W. Grimes c = linebuf[1];
1769b50d902SRodney W. Grimes switch (c) {
1779b50d902SRodney W. Grimes default:
1789b50d902SRodney W. Grimes /*
1799b50d902SRodney W. Grimes * On double escape, just send the single one.
1809b50d902SRodney W. Grimes * Otherwise, it's an error.
1819b50d902SRodney W. Grimes */
1829b50d902SRodney W. Grimes if (c == escape) {
183856f23edSMike Heffner if (putline(collf, &linebuf[1], !longline) < 0)
1849b50d902SRodney W. Grimes goto err;
1859b50d902SRodney W. Grimes else
1869b50d902SRodney W. Grimes break;
1879b50d902SRodney W. Grimes }
1889b50d902SRodney W. Grimes printf("Unknown tilde escape.\n");
1899b50d902SRodney W. Grimes break;
1909b50d902SRodney W. Grimes case 'C':
1919b50d902SRodney W. Grimes /*
1929b50d902SRodney W. Grimes * Dump core.
1939b50d902SRodney W. Grimes */
194*d28a9551SJohn Baldwin core(NULL);
1959b50d902SRodney W. Grimes break;
1969b50d902SRodney W. Grimes case '!':
1979b50d902SRodney W. Grimes /*
1989b50d902SRodney W. Grimes * Shell escape, send the balance of the
1999b50d902SRodney W. Grimes * line to sh -c.
2009b50d902SRodney W. Grimes */
2019b50d902SRodney W. Grimes shell(&linebuf[2]);
2029b50d902SRodney W. Grimes break;
2039b50d902SRodney W. Grimes case ':':
20469131e40SMike Heffner case '_':
2059b50d902SRodney W. Grimes /*
2069b50d902SRodney W. Grimes * Escape to command mode, but be nice!
2079b50d902SRodney W. Grimes */
2089b50d902SRodney W. Grimes execute(&linebuf[2], 1);
2099b50d902SRodney W. Grimes goto cont;
2109b50d902SRodney W. Grimes case '.':
2119b50d902SRodney W. Grimes /*
2129b50d902SRodney W. Grimes * Simulate end of file on input.
2139b50d902SRodney W. Grimes */
2149b50d902SRodney W. Grimes goto out;
2159b50d902SRodney W. Grimes case 'q':
2169b50d902SRodney W. Grimes /*
2179b50d902SRodney W. Grimes * Force a quit of sending mail.
2189b50d902SRodney W. Grimes * Act like an interrupt happened.
2199b50d902SRodney W. Grimes */
2209b50d902SRodney W. Grimes hadintr++;
2219b50d902SRodney W. Grimes collint(SIGINT);
2229b50d902SRodney W. Grimes exit(1);
22369131e40SMike Heffner case 'x':
22469131e40SMike Heffner /*
22569131e40SMike Heffner * Exit, do not save in dead.letter.
22669131e40SMike Heffner */
22769131e40SMike Heffner goto err;
2289b50d902SRodney W. Grimes case 'h':
2299b50d902SRodney W. Grimes /*
2309b50d902SRodney W. Grimes * Grab a bunch of headers.
2319b50d902SRodney W. Grimes */
2329b50d902SRodney W. Grimes grabh(hp, GTO|GSUBJECT|GCC|GBCC);
2339b50d902SRodney W. Grimes goto cont;
2349b50d902SRodney W. Grimes case 't':
2359b50d902SRodney W. Grimes /*
2369b50d902SRodney W. Grimes * Add to the To list.
2379b50d902SRodney W. Grimes */
2389b50d902SRodney W. Grimes hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
2399b50d902SRodney W. Grimes break;
2409b50d902SRodney W. Grimes case 's':
2419b50d902SRodney W. Grimes /*
24299bd6601SJoerg Wunsch * Set the Subject line.
2439b50d902SRodney W. Grimes */
2449b50d902SRodney W. Grimes cp = &linebuf[2];
2456d48fa43SAndrey A. Chernov while (isspace((unsigned char)*cp))
2469b50d902SRodney W. Grimes cp++;
2479b50d902SRodney W. Grimes hp->h_subject = savestr(cp);
2489b50d902SRodney W. Grimes break;
24999bd6601SJoerg Wunsch case 'R':
25099bd6601SJoerg Wunsch /*
25199bd6601SJoerg Wunsch * Set the Reply-To line.
25299bd6601SJoerg Wunsch */
25399bd6601SJoerg Wunsch cp = &linebuf[2];
2546d48fa43SAndrey A. Chernov while (isspace((unsigned char)*cp))
25599bd6601SJoerg Wunsch cp++;
25699bd6601SJoerg Wunsch hp->h_replyto = savestr(cp);
25799bd6601SJoerg Wunsch break;
2589b50d902SRodney W. Grimes case 'c':
2599b50d902SRodney W. Grimes /*
2609b50d902SRodney W. Grimes * Add to the CC list.
2619b50d902SRodney W. Grimes */
2629b50d902SRodney W. Grimes hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
2639b50d902SRodney W. Grimes break;
2649b50d902SRodney W. Grimes case 'b':
2659b50d902SRodney W. Grimes /*
26669131e40SMike Heffner * Add to the BCC list.
2679b50d902SRodney W. Grimes */
2689b50d902SRodney W. Grimes hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
2699b50d902SRodney W. Grimes break;
27069131e40SMike Heffner case 'i':
27169131e40SMike Heffner case 'A':
27269131e40SMike Heffner case 'a':
27369131e40SMike Heffner /*
2749b12c3f3SMike Heffner * Insert named variable in message.
27569131e40SMike Heffner */
27669131e40SMike Heffner switch(c) {
27769131e40SMike Heffner case 'i':
27869131e40SMike Heffner cp = &linebuf[2];
27969131e40SMike Heffner while(isspace((unsigned char)*cp))
28069131e40SMike Heffner cp++;
28169131e40SMike Heffner break;
28269131e40SMike Heffner case 'a':
28369131e40SMike Heffner cp = "sign";
28469131e40SMike Heffner break;
28569131e40SMike Heffner case 'A':
28669131e40SMike Heffner cp = "Sign";
28769131e40SMike Heffner break;
28869131e40SMike Heffner default:
28969131e40SMike Heffner goto err;
29069131e40SMike Heffner }
29169131e40SMike Heffner
29269131e40SMike Heffner if(*cp != '\0' && (cp = value(cp)) != NULL) {
29369131e40SMike Heffner printf("%s\n", cp);
29469131e40SMike Heffner if(putline(collf, cp, 1) < 0)
29569131e40SMike Heffner goto err;
29669131e40SMike Heffner }
29769131e40SMike Heffner
29869131e40SMike Heffner break;
2999b50d902SRodney W. Grimes case 'd':
30069131e40SMike Heffner /*
30169131e40SMike Heffner * Read in the dead letter file.
30269131e40SMike Heffner */
30369131e40SMike Heffner if (strlcpy(linebuf + 2, getdeadletter(),
30469131e40SMike Heffner sizeof(linebuf) - 2)
3050c3a8314SMike Heffner >= sizeof(linebuf) - 2) {
3060c3a8314SMike Heffner printf("Line buffer overflow\n");
3070c3a8314SMike Heffner break;
3080c3a8314SMike Heffner }
30969131e40SMike Heffner /* FALLTHROUGH */
3109b50d902SRodney W. Grimes case 'r':
31169131e40SMike Heffner case '<':
3129b50d902SRodney W. Grimes /*
3139b50d902SRodney W. Grimes * Invoke a file:
3149b50d902SRodney W. Grimes * Search for the file name,
3159b50d902SRodney W. Grimes * then open it and copy the contents to collf.
3169b50d902SRodney W. Grimes */
3179b50d902SRodney W. Grimes cp = &linebuf[2];
3186d48fa43SAndrey A. Chernov while (isspace((unsigned char)*cp))
3199b50d902SRodney W. Grimes cp++;
3209b50d902SRodney W. Grimes if (*cp == '\0') {
3219b50d902SRodney W. Grimes printf("Interpolate what file?\n");
3229b50d902SRodney W. Grimes break;
3239b50d902SRodney W. Grimes }
3249b50d902SRodney W. Grimes cp = expand(cp);
3259ce73e90SMike Heffner if (cp == NULL)
3269b50d902SRodney W. Grimes break;
32769131e40SMike Heffner if (*cp == '!') {
32869131e40SMike Heffner /*
32969131e40SMike Heffner * Insert stdout of command.
33069131e40SMike Heffner */
33169131e40SMike Heffner char *sh;
33269131e40SMike Heffner int nullfd, tempfd, rc;
33369131e40SMike Heffner char tempname2[PATHSIZE];
33469131e40SMike Heffner
33556858424SXin LI if ((nullfd = open(_PATH_DEVNULL, O_RDONLY, 0))
33669131e40SMike Heffner == -1) {
33756858424SXin LI warn(_PATH_DEVNULL);
3389b50d902SRodney W. Grimes break;
3399b50d902SRodney W. Grimes }
34069131e40SMike Heffner
34169131e40SMike Heffner (void)snprintf(tempname2, sizeof(tempname2),
34269131e40SMike Heffner "%s/mail.ReXXXXXXXXXX", tmpdir);
34369131e40SMike Heffner if ((tempfd = mkstemp(tempname2)) == -1 ||
34469131e40SMike Heffner (fbuf = Fdopen(tempfd, "w+")) == NULL) {
34569131e40SMike Heffner warn("%s", tempname2);
34669131e40SMike Heffner break;
34769131e40SMike Heffner }
34869131e40SMike Heffner (void)unlink(tempname2);
34969131e40SMike Heffner
35069131e40SMike Heffner if ((sh = value("SHELL")) == NULL)
35169131e40SMike Heffner sh = _PATH_CSHELL;
35269131e40SMike Heffner
35369131e40SMike Heffner rc = run_command(sh, 0, nullfd, fileno(fbuf),
35469131e40SMike Heffner "-c", cp+1, NULL);
35569131e40SMike Heffner
35669131e40SMike Heffner close(nullfd);
35769131e40SMike Heffner
35869131e40SMike Heffner if (rc < 0) {
35969131e40SMike Heffner (void)Fclose(fbuf);
36069131e40SMike Heffner break;
36169131e40SMike Heffner }
36269131e40SMike Heffner
36369131e40SMike Heffner if (fsize(fbuf) == 0) {
36469131e40SMike Heffner fprintf(stderr,
36569131e40SMike Heffner "No bytes from command \"%s\"\n",
36669131e40SMike Heffner cp+1);
36769131e40SMike Heffner (void)Fclose(fbuf);
36869131e40SMike Heffner break;
36969131e40SMike Heffner }
37069131e40SMike Heffner
37169131e40SMike Heffner rewind(fbuf);
37269131e40SMike Heffner } else if (isdir(cp)) {
37369131e40SMike Heffner printf("%s: Directory\n", cp);
37469131e40SMike Heffner break;
37569131e40SMike Heffner } else if ((fbuf = Fopen(cp, "r")) == NULL) {
3760c3a8314SMike Heffner warn("%s", cp);
3779b50d902SRodney W. Grimes break;
3789b50d902SRodney W. Grimes }
3799b50d902SRodney W. Grimes printf("\"%s\" ", cp);
3809ce73e90SMike Heffner (void)fflush(stdout);
3819b50d902SRodney W. Grimes lc = 0;
3829b50d902SRodney W. Grimes cc = 0;
383856f23edSMike Heffner while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
384856f23edSMike Heffner if (rc != LINESIZE - 1)
3859b50d902SRodney W. Grimes lc++;
386856f23edSMike Heffner if ((t = putline(collf, linebuf,
387856f23edSMike Heffner rc != LINESIZE - 1)) < 0) {
3889ce73e90SMike Heffner (void)Fclose(fbuf);
3899b50d902SRodney W. Grimes goto err;
3909b50d902SRodney W. Grimes }
3919b50d902SRodney W. Grimes cc += t;
3929b50d902SRodney W. Grimes }
3939ce73e90SMike Heffner (void)Fclose(fbuf);
3949b50d902SRodney W. Grimes printf("%d/%d\n", lc, cc);
3959b50d902SRodney W. Grimes break;
3969b50d902SRodney W. Grimes case 'w':
3979b50d902SRodney W. Grimes /*
3989b50d902SRodney W. Grimes * Write the message on a file.
3999b50d902SRodney W. Grimes */
4009b50d902SRodney W. Grimes cp = &linebuf[2];
4019b50d902SRodney W. Grimes while (*cp == ' ' || *cp == '\t')
4029b50d902SRodney W. Grimes cp++;
4039b50d902SRodney W. Grimes if (*cp == '\0') {
4049b50d902SRodney W. Grimes fprintf(stderr, "Write what file!?\n");
4059b50d902SRodney W. Grimes break;
4069b50d902SRodney W. Grimes }
4079ce73e90SMike Heffner if ((cp = expand(cp)) == NULL)
4089b50d902SRodney W. Grimes break;
4099b50d902SRodney W. Grimes rewind(collf);
4109b50d902SRodney W. Grimes exwrite(cp, collf, 1);
4119b50d902SRodney W. Grimes break;
4129b50d902SRodney W. Grimes case 'm':
4139b50d902SRodney W. Grimes case 'M':
4149b50d902SRodney W. Grimes case 'f':
4159b50d902SRodney W. Grimes case 'F':
4169b50d902SRodney W. Grimes /*
4179b50d902SRodney W. Grimes * Interpolate the named messages, if we
4189b50d902SRodney W. Grimes * are in receiving mail mode. Does the
4199b50d902SRodney W. Grimes * standard list processing garbage.
4209b50d902SRodney W. Grimes * If ~f is given, we don't shift over.
4219b50d902SRodney W. Grimes */
4220c3a8314SMike Heffner if (forward(linebuf + 2, collf, tempname, c) < 0)
4239b50d902SRodney W. Grimes goto err;
4249b50d902SRodney W. Grimes goto cont;
4259b50d902SRodney W. Grimes case '?':
4269b50d902SRodney W. Grimes if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
4270c3a8314SMike Heffner warn("%s", _PATH_TILDE);
4289b50d902SRodney W. Grimes break;
4299b50d902SRodney W. Grimes }
4309b50d902SRodney W. Grimes while ((t = getc(fbuf)) != EOF)
4319b50d902SRodney W. Grimes (void)putchar(t);
4329ce73e90SMike Heffner (void)Fclose(fbuf);
4339b50d902SRodney W. Grimes break;
4349b50d902SRodney W. Grimes case 'p':
4359b50d902SRodney W. Grimes /*
4369b50d902SRodney W. Grimes * Print out the current state of the
4379b50d902SRodney W. Grimes * message without altering anything.
4389b50d902SRodney W. Grimes */
4399b50d902SRodney W. Grimes rewind(collf);
4409b50d902SRodney W. Grimes printf("-------\nMessage contains:\n");
4419b50d902SRodney W. Grimes puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
4429b50d902SRodney W. Grimes while ((t = getc(collf)) != EOF)
4439b50d902SRodney W. Grimes (void)putchar(t);
4449b50d902SRodney W. Grimes goto cont;
4459b50d902SRodney W. Grimes case '|':
4469b50d902SRodney W. Grimes /*
4479b50d902SRodney W. Grimes * Pipe message through command.
4489b50d902SRodney W. Grimes * Collect output as new message.
4499b50d902SRodney W. Grimes */
4509b50d902SRodney W. Grimes rewind(collf);
4519b50d902SRodney W. Grimes mespipe(collf, &linebuf[2]);
4529b50d902SRodney W. Grimes goto cont;
4539b50d902SRodney W. Grimes case 'v':
4549b50d902SRodney W. Grimes case 'e':
4559b50d902SRodney W. Grimes /*
4569b50d902SRodney W. Grimes * Edit the current message.
4579b50d902SRodney W. Grimes * 'e' means to use EDITOR
4589b50d902SRodney W. Grimes * 'v' means to use VISUAL
4599b50d902SRodney W. Grimes */
4609b50d902SRodney W. Grimes rewind(collf);
4619b50d902SRodney W. Grimes mesedit(collf, c);
4629b50d902SRodney W. Grimes goto cont;
4639b50d902SRodney W. Grimes }
4649b50d902SRodney W. Grimes }
4659b50d902SRodney W. Grimes goto out;
4669b50d902SRodney W. Grimes err:
4679b50d902SRodney W. Grimes if (collf != NULL) {
4689ce73e90SMike Heffner (void)Fclose(collf);
4699b50d902SRodney W. Grimes collf = NULL;
4709b50d902SRodney W. Grimes }
4719b50d902SRodney W. Grimes out:
4729b50d902SRodney W. Grimes if (collf != NULL)
4739b50d902SRodney W. Grimes rewind(collf);
4749b50d902SRodney W. Grimes noreset--;
475856f23edSMike Heffner (void)sigprocmask(SIG_BLOCK, &nset, NULL);
4769ce73e90SMike Heffner (void)signal(SIGINT, saveint);
4779ce73e90SMike Heffner (void)signal(SIGHUP, savehup);
4789ce73e90SMike Heffner (void)signal(SIGTSTP, savetstp);
4799ce73e90SMike Heffner (void)signal(SIGTTOU, savettou);
4809ce73e90SMike Heffner (void)signal(SIGTTIN, savettin);
481856f23edSMike Heffner (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
4829ce73e90SMike Heffner return (collf);
4839b50d902SRodney W. Grimes }
4849b50d902SRodney W. Grimes
4859b50d902SRodney W. Grimes /*
4869b50d902SRodney W. Grimes * Write a file, ex-like if f set.
4879b50d902SRodney W. Grimes */
4889b50d902SRodney W. Grimes int
exwrite(char name[],FILE * fp,int f)4896d8484b0SPhilippe Charnier exwrite(char name[], FILE *fp, int f)
4909b50d902SRodney W. Grimes {
4919ce73e90SMike Heffner FILE *of;
4929ce73e90SMike Heffner int c, lc;
4939b50d902SRodney W. Grimes long cc;
4949b50d902SRodney W. Grimes struct stat junk;
4959b50d902SRodney W. Grimes
4969b50d902SRodney W. Grimes if (f) {
4979b50d902SRodney W. Grimes printf("\"%s\" ", name);
4989ce73e90SMike Heffner (void)fflush(stdout);
4999b50d902SRodney W. Grimes }
5000c3a8314SMike Heffner if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
5019b50d902SRodney W. Grimes if (!f)
5029b50d902SRodney W. Grimes fprintf(stderr, "%s: ", name);
5039b50d902SRodney W. Grimes fprintf(stderr, "File exists\n");
5049b50d902SRodney W. Grimes return (-1);
5059b50d902SRodney W. Grimes }
5069b50d902SRodney W. Grimes if ((of = Fopen(name, "w")) == NULL) {
5079ce73e90SMike Heffner warn((char *)NULL);
5089b50d902SRodney W. Grimes return (-1);
5099b50d902SRodney W. Grimes }
5109b50d902SRodney W. Grimes lc = 0;
5119b50d902SRodney W. Grimes cc = 0;
5129b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) {
5139b50d902SRodney W. Grimes cc++;
5149b50d902SRodney W. Grimes if (c == '\n')
5159b50d902SRodney W. Grimes lc++;
5169b50d902SRodney W. Grimes (void)putc(c, of);
5179b50d902SRodney W. Grimes if (ferror(of)) {
5180c3a8314SMike Heffner warnx("%s", name);
5199ce73e90SMike Heffner (void)Fclose(of);
5209b50d902SRodney W. Grimes return (-1);
5219b50d902SRodney W. Grimes }
5229b50d902SRodney W. Grimes }
5239ce73e90SMike Heffner (void)Fclose(of);
5249b50d902SRodney W. Grimes printf("%d/%ld\n", lc, cc);
5259ce73e90SMike Heffner (void)fflush(stdout);
5269b50d902SRodney W. Grimes return (0);
5279b50d902SRodney W. Grimes }
5289b50d902SRodney W. Grimes
5299b50d902SRodney W. Grimes /*
5309b50d902SRodney W. Grimes * Edit the message being collected on fp.
5319b50d902SRodney W. Grimes * On return, make the edit file the new temp file.
5329b50d902SRodney W. Grimes */
5339b50d902SRodney W. Grimes void
mesedit(FILE * fp,int c)5346d8484b0SPhilippe Charnier mesedit(FILE *fp, int c)
5359b50d902SRodney W. Grimes {
5369b50d902SRodney W. Grimes sig_t sigint = signal(SIGINT, SIG_IGN);
5379b50d902SRodney W. Grimes FILE *nf = run_editor(fp, (off_t)-1, c, 0);
5389b50d902SRodney W. Grimes
5399b50d902SRodney W. Grimes if (nf != NULL) {
540af8c3262SAndrey A. Chernov (void)fseeko(nf, (off_t)0, SEEK_END);
5419b50d902SRodney W. Grimes collf = nf;
5429ce73e90SMike Heffner (void)Fclose(fp);
5439b50d902SRodney W. Grimes }
5449b50d902SRodney W. Grimes (void)signal(SIGINT, sigint);
5459b50d902SRodney W. Grimes }
5469b50d902SRodney W. Grimes
5479b50d902SRodney W. Grimes /*
5489b50d902SRodney W. Grimes * Pipe the message through the command.
5499b50d902SRodney W. Grimes * Old message is on stdin of command;
5509b50d902SRodney W. Grimes * New message collected from stdout.
5519b50d902SRodney W. Grimes * Sh -c must return 0 to accept the new message.
5529b50d902SRodney W. Grimes */
5539b50d902SRodney W. Grimes void
mespipe(FILE * fp,char cmd[])5546d8484b0SPhilippe Charnier mespipe(FILE *fp, char cmd[])
5559b50d902SRodney W. Grimes {
5569b50d902SRodney W. Grimes FILE *nf;
5570c3a8314SMike Heffner int fd;
5589b50d902SRodney W. Grimes sig_t sigint = signal(SIGINT, SIG_IGN);
5599ce73e90SMike Heffner char *sh, tempname[PATHSIZE];
5609b50d902SRodney W. Grimes
5619ce73e90SMike Heffner (void)snprintf(tempname, sizeof(tempname),
5629ce73e90SMike Heffner "%s/mail.ReXXXXXXXXXX", tmpdir);
5630c3a8314SMike Heffner if ((fd = mkstemp(tempname)) == -1 ||
5640c3a8314SMike Heffner (nf = Fdopen(fd, "w+")) == NULL) {
5650c3a8314SMike Heffner warn("%s", tempname);
5669b50d902SRodney W. Grimes goto out;
5679b50d902SRodney W. Grimes }
5680c3a8314SMike Heffner (void)rm(tempname);
5699b50d902SRodney W. Grimes /*
5709b50d902SRodney W. Grimes * stdin = current message.
5719b50d902SRodney W. Grimes * stdout = new message.
5729b50d902SRodney W. Grimes */
5739ce73e90SMike Heffner if ((sh = value("SHELL")) == NULL)
5749ce73e90SMike Heffner sh = _PATH_CSHELL;
5759ce73e90SMike Heffner if (run_command(sh,
5769ce73e90SMike Heffner 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
5779b50d902SRodney W. Grimes (void)Fclose(nf);
5789b50d902SRodney W. Grimes goto out;
5799b50d902SRodney W. Grimes }
5809b50d902SRodney W. Grimes if (fsize(nf) == 0) {
5819b50d902SRodney W. Grimes fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
5829b50d902SRodney W. Grimes (void)Fclose(nf);
5839b50d902SRodney W. Grimes goto out;
5849b50d902SRodney W. Grimes }
5859b50d902SRodney W. Grimes /*
5869b50d902SRodney W. Grimes * Take new files.
5879b50d902SRodney W. Grimes */
588af8c3262SAndrey A. Chernov (void)fseeko(nf, (off_t)0, SEEK_END);
5899b50d902SRodney W. Grimes collf = nf;
5909b50d902SRodney W. Grimes (void)Fclose(fp);
5919b50d902SRodney W. Grimes out:
5929b50d902SRodney W. Grimes (void)signal(SIGINT, sigint);
5939b50d902SRodney W. Grimes }
5949b50d902SRodney W. Grimes
5959b50d902SRodney W. Grimes /*
5969b50d902SRodney W. Grimes * Interpolate the named messages into the current
5979b50d902SRodney W. Grimes * message, preceding each line with a tab.
5989b50d902SRodney W. Grimes * Return a count of the number of characters now in
5999b50d902SRodney W. Grimes * the message, or -1 if an error is encountered writing
6009b50d902SRodney W. Grimes * the message temporary. The flag argument is 'm' if we
6019b50d902SRodney W. Grimes * should shift over and 'f' if not.
6029b50d902SRodney W. Grimes */
6039b50d902SRodney W. Grimes int
forward(char ms[],FILE * fp,char * fn,int f)6046d8484b0SPhilippe Charnier forward(char ms[], FILE *fp, char *fn, int f)
6059b50d902SRodney W. Grimes {
6069ce73e90SMike Heffner int *msgvec;
6079b50d902SRodney W. Grimes struct ignoretab *ig;
6089b50d902SRodney W. Grimes char *tabst;
6099b50d902SRodney W. Grimes
6109ce73e90SMike Heffner msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec));
6119ce73e90SMike Heffner if (msgvec == NULL)
6129b50d902SRodney W. Grimes return (0);
6139b50d902SRodney W. Grimes if (getmsglist(ms, msgvec, 0) < 0)
6149b50d902SRodney W. Grimes return (0);
6159b50d902SRodney W. Grimes if (*msgvec == 0) {
6169b50d902SRodney W. Grimes *msgvec = first(0, MMNORM);
617d030d2d2SPoul-Henning Kamp if (*msgvec == 0) {
6189b50d902SRodney W. Grimes printf("No appropriate messages\n");
6199b50d902SRodney W. Grimes return (0);
6209b50d902SRodney W. Grimes }
621d030d2d2SPoul-Henning Kamp msgvec[1] = 0;
6229b50d902SRodney W. Grimes }
6239b50d902SRodney W. Grimes if (f == 'f' || f == 'F')
6249ce73e90SMike Heffner tabst = NULL;
6259ce73e90SMike Heffner else if ((tabst = value("indentprefix")) == NULL)
6269b50d902SRodney W. Grimes tabst = "\t";
6276d48fa43SAndrey A. Chernov ig = isupper((unsigned char)f) ? NULL : ignore;
6289b50d902SRodney W. Grimes printf("Interpolating:");
6299b50d902SRodney W. Grimes for (; *msgvec != 0; msgvec++) {
6309b50d902SRodney W. Grimes struct message *mp = message + *msgvec - 1;
6319b50d902SRodney W. Grimes
6329b50d902SRodney W. Grimes touch(mp);
6339b50d902SRodney W. Grimes printf(" %d", *msgvec);
6340c3a8314SMike Heffner if (sendmessage(mp, fp, ig, tabst) < 0) {
6350c3a8314SMike Heffner warnx("%s", fn);
6369b50d902SRodney W. Grimes return (-1);
6379b50d902SRodney W. Grimes }
6389b50d902SRodney W. Grimes }
6399b50d902SRodney W. Grimes printf("\n");
6409b50d902SRodney W. Grimes return (0);
6419b50d902SRodney W. Grimes }
6429b50d902SRodney W. Grimes
6439b50d902SRodney W. Grimes /*
6449b50d902SRodney W. Grimes * Print (continue) when continued after ^Z.
6459b50d902SRodney W. Grimes */
6469b50d902SRodney W. Grimes /*ARGSUSED*/
6479b50d902SRodney W. Grimes void
collstop(int s)6486d8484b0SPhilippe Charnier collstop(int s)
6499b50d902SRodney W. Grimes {
6509b50d902SRodney W. Grimes sig_t old_action = signal(s, SIG_DFL);
651856f23edSMike Heffner sigset_t nset;
6529b50d902SRodney W. Grimes
653856f23edSMike Heffner (void)sigemptyset(&nset);
654856f23edSMike Heffner (void)sigaddset(&nset, s);
655856f23edSMike Heffner (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
6569ce73e90SMike Heffner (void)kill(0, s);
657856f23edSMike Heffner (void)sigprocmask(SIG_BLOCK, &nset, NULL);
6589ce73e90SMike Heffner (void)signal(s, old_action);
6599b50d902SRodney W. Grimes if (colljmp_p) {
6609b50d902SRodney W. Grimes colljmp_p = 0;
6619b50d902SRodney W. Grimes hadintr = 0;
6629b50d902SRodney W. Grimes longjmp(colljmp, 1);
6639b50d902SRodney W. Grimes }
6649b50d902SRodney W. Grimes }
6659b50d902SRodney W. Grimes
6669b50d902SRodney W. Grimes /*
6679b50d902SRodney W. Grimes * On interrupt, come here to save the partial message in ~/dead.letter.
6689b50d902SRodney W. Grimes * Then jump out of the collection loop.
6699b50d902SRodney W. Grimes */
6709b50d902SRodney W. Grimes /*ARGSUSED*/
6719b50d902SRodney W. Grimes void
collint(int s __unused)6726d8484b0SPhilippe Charnier collint(int s __unused)
6739b50d902SRodney W. Grimes {
6749b50d902SRodney W. Grimes /*
6759b50d902SRodney W. Grimes * the control flow is subtle, because we can be called from ~q.
6769b50d902SRodney W. Grimes */
6779b50d902SRodney W. Grimes if (!hadintr) {
6789ce73e90SMike Heffner if (value("ignore") != NULL) {
6799ce73e90SMike Heffner printf("@");
6809ce73e90SMike Heffner (void)fflush(stdout);
6819b50d902SRodney W. Grimes clearerr(stdin);
6829b50d902SRodney W. Grimes return;
6839b50d902SRodney W. Grimes }
6849b50d902SRodney W. Grimes hadintr = 1;
6859b50d902SRodney W. Grimes longjmp(colljmp, 1);
6869b50d902SRodney W. Grimes }
6879b50d902SRodney W. Grimes rewind(collf);
6889ce73e90SMike Heffner if (value("nosave") == NULL)
6899b50d902SRodney W. Grimes savedeadletter(collf);
6909b50d902SRodney W. Grimes longjmp(collabort, 1);
6919b50d902SRodney W. Grimes }
6929b50d902SRodney W. Grimes
6939b50d902SRodney W. Grimes /*ARGSUSED*/
6949b50d902SRodney W. Grimes void
collhup(int s __unused)6956d8484b0SPhilippe Charnier collhup(int s __unused)
6969b50d902SRodney W. Grimes {
6979b50d902SRodney W. Grimes rewind(collf);
6989b50d902SRodney W. Grimes savedeadletter(collf);
6999b50d902SRodney W. Grimes /*
7009b50d902SRodney W. Grimes * Let's pretend nobody else wants to clean up,
7019b50d902SRodney W. Grimes * a true statement at this time.
7029b50d902SRodney W. Grimes */
7039b50d902SRodney W. Grimes exit(1);
7049b50d902SRodney W. Grimes }
7059b50d902SRodney W. Grimes
7069b50d902SRodney W. Grimes void
savedeadletter(FILE * fp)7076d8484b0SPhilippe Charnier savedeadletter(FILE *fp)
7089b50d902SRodney W. Grimes {
7099ce73e90SMike Heffner FILE *dbuf;
7109ce73e90SMike Heffner int c;
7119b50d902SRodney W. Grimes char *cp;
7129b50d902SRodney W. Grimes
7139b50d902SRodney W. Grimes if (fsize(fp) == 0)
7149b50d902SRodney W. Grimes return;
7159b50d902SRodney W. Grimes cp = getdeadletter();
7169b50d902SRodney W. Grimes c = umask(077);
7179b50d902SRodney W. Grimes dbuf = Fopen(cp, "a");
7189b50d902SRodney W. Grimes (void)umask(c);
7199b50d902SRodney W. Grimes if (dbuf == NULL)
7209b50d902SRodney W. Grimes return;
7219b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF)
7229b50d902SRodney W. Grimes (void)putc(c, dbuf);
7239ce73e90SMike Heffner (void)Fclose(dbuf);
7249b50d902SRodney W. Grimes rewind(fp);
7259b50d902SRodney W. Grimes }
726