#ifndef lint
#if 0
static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
#endif
static const char rcsid[] =
"$FreeBSD: src/usr.bin/mail/collect.c,v 1.12 2002/06/30 05:25:06 obrien Exp $";
#endif
#include <sys/cdefs.h>
#include "rcv.h"
#include <fcntl.h>
#include "extern.h"
static sig_t saveint;
static sig_t savehup;
static sig_t savetstp;
static sig_t savettou;
static sig_t savettin;
static FILE *collf;
static int hadintr;
static jmp_buf colljmp;
static int colljmp_p;
static jmp_buf collabort;
static jmp_buf pipejmp;
void
brokthepipe(signo)
int signo;
{
longjmp(pipejmp, 1);
}
FILE *
collect(hp, printheaders)
struct header *hp;
int printheaders;
{
FILE *fbuf;
int lc, cc, escape, eofcount, fd, c, t;
char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub;
sigset_t nset;
int longline, lastlong, rc;
int nlines, usepager;
char *envptr;
collf = NULL;
(void)sigemptyset(&nset);
(void)sigaddset(&nset, SIGINT);
(void)sigaddset(&nset, SIGHUP);
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
(void)signal(SIGINT, collint);
if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
(void)signal(SIGHUP, collhup);
savetstp = signal(SIGTSTP, collstop);
savettou = signal(SIGTTOU, collstop);
savettin = signal(SIGTTIN, collstop);
if (setjmp(collabort) || setjmp(colljmp)) {
(void)rm(tempname);
goto err;
}
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
noreset++;
(void)snprintf(tempname, sizeof(tempname),
"%s/mail.RsXXXXXXXXXX", tmpdir);
if ((fd = mkstemp(tempname)) == -1 ||
(collf = Fdopen(fd, "w+")) == NULL) {
warn("%s", tempname);
goto err;
}
(void)rm(tempname);
t = GTO|GSUBJECT|GCC|GNL;
getsub = 0;
if (hp->h_subject == NULL && value("interactive") != NULL &&
(value("ask") != NULL || value("asksub") != NULL))
t &= ~GNL, getsub++;
if (printheaders) {
puthead(hp, stdout, t);
(void)fflush(stdout);
}
if ((cp = value("escape")) != NULL)
escape = *cp;
else
escape = ESCAPE;
eofcount = 0;
hadintr = 0;
lastlong = 0;
longline = 0;
if (!setjmp(colljmp)) {
if (getsub) {
if (grabh(hp, GSUBJECT)) {
hadintr++;
goto cont;
}
}
} else {
cont:
if (hadintr) {
(void)fflush(stdout);
fprintf(stderr,
"\n(Interrupt -- one more to kill letter)\n");
} else {
printf("(continue)\n");
(void)fflush(stdout);
}
}
for (;;) {
colljmp_p = 1;
c = readline(stdin, linebuf, LINESIZE);
colljmp_p = 0;
if (c < 0) {
if (value("interactive") != NULL &&
value("ignoreeof") != NULL && ++eofcount < 25) {
printf("Use \".\" to terminate letter\n");
continue;
}
break;
}
lastlong = longline;
longline = c == LINESIZE - 1;
eofcount = 0;
hadintr = 0;
if (linebuf[0] == '.' && linebuf[1] == '\0' &&
value("interactive") != NULL && !lastlong &&
(value("dot") != NULL || value("ignoreeof") != NULL))
break;
if (linebuf[0] != escape || value("interactive") == NULL ||
lastlong) {
if (putline(collf, linebuf, !longline) < 0)
goto err;
continue;
}
c = linebuf[1];
switch (c) {
default:
if (c == escape) {
if (putline(collf, &linebuf[1], !longline) < 0)
goto err;
else
break;
}
printf("Unknown tilde escape.\n");
break;
case 'C':
core();
break;
case '!':
shell(&linebuf[2]);
break;
case ':':
case '_':
execute(&linebuf[2], 1);
goto cont;
case '.':
goto out;
case 'q':
hadintr++;
collint(SIGINT);
exit(1);
case 'x':
goto err;
case 'h':
grabh(hp, GTO|GSUBJECT|GCC|GBCC);
goto cont;
case 't':
hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
break;
case 's':
cp = &linebuf[2];
while (isspace((unsigned char)*cp))
cp++;
hp->h_subject = savestr(cp);
break;
case 'R':
cp = &linebuf[2];
while (isspace((unsigned char)*cp))
cp++;
hp->h_replyto = savestr(cp);
break;
case 'c':
hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
break;
case 'b':
hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
break;
case 'i':
case 'A':
case 'a':
switch(c) {
case 'i':
cp = &linebuf[2];
while(isspace((unsigned char)*cp))
cp++;
break;
case 'a':
cp = "sign";
break;
case 'A':
cp = "Sign";
break;
default:
goto err;
}
if(*cp != '\0' && (cp = value(cp)) != NULL) {
if (*cp != '\0') {
printf("%s\n", cp);
if(putline(collf, cp, 1) < 0)
goto err;
}
}
break;
case 'd':
if (strlcpy(linebuf + 2, getdeadletter(),
sizeof(linebuf) - 2)
>= sizeof(linebuf) - 2) {
printf("Line buffer overflow\n");
break;
}
case 'r':
case '<':
cp = &linebuf[2];
while (isspace((unsigned char)*cp))
cp++;
if (*cp == '\0') {
printf("Interpolate what file?\n");
break;
}
cp = expand(cp);
if (cp == NULL)
break;
if (*cp == '!') {
char *sh;
int nullfd, tempfd, rc;
char tempname2[PATHSIZE];
if ((nullfd = open("/dev/null", O_RDONLY, 0))
== -1) {
warn("/dev/null");
break;
}
(void)snprintf(tempname2, sizeof(tempname2),
"%s/mail.ReXXXXXXXXXX", tmpdir);
if ((tempfd = mkstemp(tempname2)) == -1 ||
(fbuf = Fdopen(tempfd, "w+")) == NULL) {
warn("%s", tempname2);
break;
}
(void)unlink(tempname2);
if ((sh = value("SHELL")) == NULL)
sh = _PATH_BSHELL;
rc = run_command(sh, 0, nullfd, fileno(fbuf),
"-c", cp+1, NULL);
close(nullfd);
if (rc < 0) {
(void)Fclose(fbuf);
break;
}
if (fsize(fbuf) == 0) {
fprintf(stderr,
"No bytes from command \"%s\"\n",
cp+1);
(void)Fclose(fbuf);
break;
}
rewind(fbuf);
} else if (isdir(cp)) {
printf("%s: Directory\n", cp);
break;
} else if ((fbuf = Fopen(cp, "r")) == NULL) {
warn("%s", cp);
break;
}
printf("\"%s\" ", cp);
(void)fflush(stdout);
lc = 0;
cc = 0;
while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
if (rc != LINESIZE - 1)
lc++;
if ((t = putline(collf, linebuf,
rc != LINESIZE - 1)) < 0) {
(void)Fclose(fbuf);
goto err;
}
cc += t;
}
(void)Fclose(fbuf);
printf("%d/%d\n", lc, cc);
break;
case 'w':
cp = &linebuf[2];
while (*cp == ' ' || *cp == '\t')
cp++;
if (*cp == '\0') {
fprintf(stderr, "Write what file!?\n");
break;
}
if ((cp = expand(cp)) == NULL)
break;
rewind(collf);
exwrite(cp, collf, 1);
break;
case 'm':
case 'M':
case 'f':
case 'F':
if (forward(linebuf + 2, collf, tempname, c) < 0)
goto err;
goto cont;
case '?':
if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
warn("%s", _PATH_TILDE);
break;
}
while ((t = getc(fbuf)) != EOF)
(void)putchar(t);
(void)Fclose(fbuf);
break;
case 'p':
rewind(collf);
printf("-------\nMessage contains:\n");
puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
if ((envptr = value("crt")) != NULL) {
nlines = atoi(envptr);
} else {
nlines = 0;
}
fbuf = stdout;
usepager = 0;
if (nlines>0) {
int countlines = 0;
while ((t = getc(collf)) != EOF) {
if (t=='\n') {
countlines++;
if (nlines < countlines) {
break;
}
}
}
rewind(collf);
if (nlines < countlines) {
usepager = 1;
envptr = value("PAGER");
if (envptr == NULL || *envptr == '\0')
envptr = _PATH_MORE;
if (setjmp(pipejmp))
goto close_pipe;
fbuf = Popen(envptr, "w");
if (fbuf == NULL) {
warnx("%s", envptr);
fbuf = stdout;
} else
(void)signal(SIGPIPE, brokthepipe);
}
}
while ((t = getc(collf)) != EOF)
(void)putchar(t);
if (usepager) {
close_pipe:
if (fbuf != stdout) {
(void)signal(SIGPIPE, SIG_IGN);
(void)Pclose(fbuf);
(void)signal(SIGPIPE, SIG_DFL);
}
}
goto cont;
case '|':
rewind(collf);
mespipe(collf, &linebuf[2]);
goto cont;
case 'v':
case 'e':
rewind(collf);
mesedit(collf, c);
goto cont;
}
}
goto out;
err:
senderr++;
if (collf != NULL) {
(void)Fclose(collf);
collf = NULL;
}
out:
if (collf != NULL)
rewind(collf);
noreset--;
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
(void)signal(SIGINT, saveint);
(void)signal(SIGHUP, savehup);
(void)signal(SIGTSTP, savetstp);
(void)signal(SIGTTOU, savettou);
(void)signal(SIGTTIN, savettin);
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
return (collf);
}
int
exwrite(name, fp, f)
char name[];
FILE *fp;
int f;
{
FILE *of;
int c, lc;
long cc;
#if 0
struct stat junk;
#endif
if (f) {
printf("\"%s\" ", name);
(void)fflush(stdout);
}
#if 0
if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
if (!f)
fprintf(stderr, "%s: ", name);
fprintf(stderr, "File exists\n");
return (-1);
}
#endif
if ((of = Fopen(name, "a")) == NULL) {
warn((char *)NULL);
return (-1);
}
lc = 0;
cc = 0;
while ((c = getc(fp)) != EOF) {
cc++;
if (c == '\n')
lc++;
(void)putc(c, of);
if (ferror(of)) {
warnx("%s", name);
(void)Fclose(of);
return (-1);
}
}
(void)Fclose(of);
printf("%d/%ld\n", lc, cc);
(void)fflush(stdout);
return (0);
}
void
mesedit(fp, c)
FILE *fp;
int c;
{
sig_t sigint = signal(SIGINT, SIG_IGN);
FILE *nf = run_editor(fp, (off_t)-1, c, 0);
if (nf != NULL) {
(void)fseeko(nf, (off_t)0, SEEK_END);
collf = nf;
(void)Fclose(fp);
}
(void)signal(SIGINT, sigint);
}
static char *
parse_pipe_args(str, msglist)
char str[];
char **msglist;
{
char *cp;
char quoted;
*msglist = NULL;
if (str==NULL) return NULL;
if (*str=='\0') return NULL;
cp = strlen(str) + str - 1;
while (cp > str && isspace((unsigned char)*cp))
cp--;
*++cp = '\0';
quoted = 0;
if (cp > str) {
cp--;
if (*cp=='"' || *cp=='\'' ) {
quoted=*cp;
cp--;
}
}
while (cp > str && (!isspace((unsigned char)*cp) || quoted)) {
if (quoted) {
if (*cp==quoted) {
cp--;
if (cp>str) {
if (!(*cp=='\\' || *cp==quoted)) {
quoted=0;
continue;
}
} else
break;
}
}
cp--;
}
if (cp == str) {
return (cp);
}
*msglist = str;
if (isspace((unsigned char)*cp))
*cp++ = '\0';
else {
printf("malformed arguments:%s\n",str);
}
return (cp);
}
int
mailpipe(str)
char str[];
{
struct message *mp;
int *msgvec, *ip;
char *msglist = NULL;
char * cmd;
char * sh;
char cmdline[4096];
int do_pagefeed;
FILE *fbuf;
fbuf = stdout;
cmd = parse_pipe_args(str, &msglist);
msgvec = (int *)salloc((msgCount + 2) * sizeof(*msgvec));
if (msglist==NULL) {
*msgvec = first(0, MMNORM);
if (*msgvec == 0) {
printf("No messages to %s.\n", cmd);
return (1);
}
msgvec[1] = 0;
} else {
if (getmsglist(msglist, msgvec, 0) < 0)
return (1);
}
if (cmd==NULL) {
cmd = value("cmd");
if (cmd==NULL || *cmd == '\0') {
printf("No command to pipe.\n");
return(1);
}
}
if ((sh = value("SHELL")) == NULL)
sh = _PATH_BSHELL;
cmdline[0] = '\0';
strlcpy(cmdline, sh, sizeof(cmdline));
strlcat(cmdline, " -c ", sizeof(cmdline));
if (*cmd!='"' && *cmd!='\'') {
strlcat(cmdline, "\"", sizeof(cmdline));
strlcat(cmdline, cmd, sizeof(cmdline));
strlcat(cmdline, "\"", sizeof(cmdline));
} else {
strlcat(cmdline, cmd, sizeof(cmdline));
}
if (setjmp(pipejmp))
goto close_pipe;
fbuf = Popen(cmdline, "w");
if (fbuf == NULL) {
warnx("%s", cmdline);
return(1);
} else
(void)signal(SIGPIPE, brokthepipe);
if (value("page") == NULL) {
do_pagefeed = 0;
} else {
do_pagefeed = 1;
}
for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
mp = &message[*ip - 1];
touch(mp);
dot = mp;
if (sendmessage(mp, fbuf, 0, NULL) < 0) {
warnx("%s", cmdline);
(void)Fclose(fbuf);
return (1);
}
if (do_pagefeed)
fprintf(fbuf,"\f");
}
(void)fflush(fbuf);
close_pipe:
if (fbuf != stdout) {
(void)signal(SIGPIPE, SIG_IGN);
(void)Pclose(fbuf);
(void)signal(SIGPIPE, SIG_DFL);
}
return (0);
}
void
mespipe(fp, cmd)
FILE *fp;
char cmd[];
{
FILE *nf;
int fd;
sig_t sigint = signal(SIGINT, SIG_IGN);
char *sh, tempname[PATHSIZE];
(void)snprintf(tempname, sizeof(tempname),
"%s/mail.ReXXXXXXXXXX", tmpdir);
if ((fd = mkstemp(tempname)) == -1 ||
(nf = Fdopen(fd, "w+")) == NULL) {
warn("%s", tempname);
goto out;
}
(void)rm(tempname);
if ((sh = value("SHELL")) == NULL)
sh = _PATH_BSHELL;
if (run_command(sh,
0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
(void)Fclose(nf);
goto out;
}
if (fsize(nf) == 0) {
fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
(void)Fclose(nf);
goto out;
}
(void)fseeko(nf, (off_t)0, SEEK_END);
collf = nf;
(void)Fclose(fp);
out:
(void)signal(SIGINT, sigint);
}
int
forward(ms, fp, fn, f)
char ms[];
FILE *fp;
char *fn;
int f;
{
int *msgvec;
struct ignoretab *ig;
char *tabst;
msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec));
if (msgvec == NULL)
return (0);
if (getmsglist(ms, msgvec, 0) < 0)
return (0);
if (*msgvec == 0) {
*msgvec = first(0, MMNORM);
if (*msgvec == 0) {
printf("No appropriate messages\n");
return (0);
}
msgvec[1] = 0;
}
if (f == 'f' || f == 'F')
tabst = NULL;
else if ((tabst = value("indentprefix")) == NULL)
tabst = "\t";
ig = isupper((unsigned char)f) ? NULL : ignore;
printf("Interpolating:");
for (; *msgvec != 0; msgvec++) {
struct message *mp = message + *msgvec - 1;
touch(mp);
printf(" %d", *msgvec);
if (sendmessage(mp, fp, ig, tabst) < 0) {
warnx("%s", fn);
return (-1);
}
}
printf("\n");
return (0);
}
void
collstop(s)
int s;
{
sig_t old_action = signal(s, SIG_DFL);
sigset_t nset;
(void)sigemptyset(&nset);
(void)sigaddset(&nset, s);
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
(void)kill(0, s);
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
(void)signal(s, old_action);
if (colljmp_p) {
colljmp_p = 0;
hadintr = 0;
longjmp(colljmp, 1);
}
}
void
collint(s)
int s;
{
if (!hadintr) {
if (value("ignore") != NULL) {
printf("@");
(void)fflush(stdout);
clearerr(stdin);
return;
}
hadintr = 1;
longjmp(colljmp, 1);
}
rewind(collf);
if (value("save") != NULL)
savedeadletter(collf);
longjmp(collabort, 1);
}
void
collhup(s)
int s;
{
rewind(collf);
savedeadletter(collf);
exit(1);
}
void
savedeadletter(fp)
FILE *fp;
{
FILE *dbuf;
int c;
char *cp;
if (fsize(fp) == 0)
return;
cp = getdeadletter();
c = umask(077);
dbuf = Fopen(cp, "w");
(void)umask(c);
if (dbuf == NULL)
return;
while ((c = getc(fp)) != EOF)
(void)putc(c, dbuf);
(void)Fclose(dbuf);
rewind(fp);
}