#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)quit.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: quit.c,v 1.10 1997/12/07 21:27:10 bad Exp $");
#endif
#endif
#include "rcv.h"
#include "extern.h"
int
quitcmd(v)
void *v;
{
if (sourcing)
return 1;
return -1;
}
void
quit()
{
int mcount, p, modify, autohold, anystat, holdbit, nohold;
FILE *ibuf = NULL, *obuf, *fbuf, *rbuf, *readstat = NULL, *abuf;
struct message *mp;
int c;
extern char *tempQuit, *tempResid;
struct stat minfo;
char *mbox;
#ifdef __GNUC__
obuf = NULL;
#endif
if (readonly)
return;
if (edit) {
edstop();
return;
}
fbuf = Fopen(mailname, "r");
if (fbuf == NULL)
goto newmail;
if (flock(fileno(fbuf), LOCK_EX) == -1) {
nolock:
perror("Unable to lock mailbox");
Fclose(fbuf);
return;
}
if (dot_lock(mailname, 1, stdout, ".") == -1)
goto nolock;
rbuf = NULL;
if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
printf("New mail has arrived.\n");
rbuf = Fopen(tempResid, "w");
if (rbuf == NULL || fbuf == NULL)
goto newmail;
#ifdef APPEND
fseek(fbuf, (long)mailsize, 0);
while ((c = getc(fbuf)) != EOF)
(void) putc(c, rbuf);
#else
p = minfo.st_size - mailsize;
while (p-- > 0) {
c = getc(fbuf);
if (c == EOF)
goto newmail;
(void) putc(c, rbuf);
}
#endif
(void) fflush(rbuf);
if (ferror(rbuf)) {
perror(tempResid);
Fclose(rbuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
Fclose(rbuf);
if ((rbuf = Fopen(tempResid, "r")) == NULL)
goto newmail;
rm(tempResid);
}
anystat = 0;
autohold = value("hold") != NOSTR;
holdbit = autohold ? MPRESERVE : MBOX;
nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
if (value("keepsave") != NOSTR)
nohold &= ~MSAVED;
for (mp = &message[0]; mp < &message[msgCount]; mp++) {
if (mp->m_flag & MNEW) {
mp->m_flag &= ~MNEW;
mp->m_flag |= MSTATUS;
}
if (mp->m_flag & MSTATUS)
anystat++;
if ((mp->m_flag & MTOUCH) == 0)
mp->m_flag |= MPRESERVE;
if ((mp->m_flag & nohold) == 0)
mp->m_flag |= holdbit;
}
modify = 0;
if (Tflag != NOSTR) {
if ((readstat = Fopen(Tflag, "w")) == NULL)
Tflag = NOSTR;
}
for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
if (mp->m_flag & MBOX)
c++;
if (mp->m_flag & MPRESERVE)
p++;
if (mp->m_flag & MODIFY)
modify++;
if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
char *id;
if ((id = hfield("article-id", mp)) != NOSTR)
fprintf(readstat, "%s\n", id);
}
}
if (Tflag != NOSTR)
Fclose(readstat);
if (p == msgCount && !modify && !anystat) {
printf("Held %d message%s in %s\n",
p, p == 1 ? "" : "s", mailname);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
if (c == 0) {
if (p != 0) {
writeback(rbuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
goto cream;
}
mbox = expand("&");
mcount = c;
if (value("append") == NOSTR) {
if ((obuf = Fopen(tempQuit, "w")) == NULL) {
perror(tempQuit);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
perror(tempQuit);
rm(tempQuit);
Fclose(obuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
rm(tempQuit);
if ((abuf = Fopen(mbox, "r")) != NULL) {
while ((c = getc(abuf)) != EOF)
(void) putc(c, obuf);
Fclose(abuf);
}
if (ferror(obuf)) {
perror(tempQuit);
Fclose(ibuf);
Fclose(obuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
Fclose(obuf);
close(creat(mbox, 0600));
if ((obuf = Fopen(mbox, "r+")) == NULL) {
perror(mbox);
Fclose(ibuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
}
else {
if ((obuf = Fopen(mbox, "a")) == NULL) {
perror(mbox);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
fchmod(fileno(obuf), 0600);
}
for (mp = &message[0]; mp < &message[msgCount]; mp++)
if (mp->m_flag & MBOX)
if (send(mp, obuf, saveignore, NOSTR) < 0) {
perror(mbox);
Fclose(ibuf);
Fclose(obuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
if (value("append") == NOSTR) {
rewind(ibuf);
c = getc(ibuf);
while (c != EOF) {
(void) putc(c, obuf);
if (ferror(obuf))
break;
c = getc(ibuf);
}
Fclose(ibuf);
}
fflush(obuf);
if (!ferror(obuf))
trunc(obuf);
if (ferror(obuf)) {
perror(mbox);
Fclose(obuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
Fclose(obuf);
if (mcount == 1)
printf("Saved 1 message in mbox\n");
else
printf("Saved %d messages in mbox\n", mcount);
if (p != 0) {
writeback(rbuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
cream:
if (rbuf != NULL) {
abuf = Fopen(mailname, "r+");
if (abuf == NULL)
goto newmail;
while ((c = getc(rbuf)) != EOF)
(void) putc(c, abuf);
(void) fflush(abuf);
if (ferror(abuf)) {
perror(mailname);
Fclose(abuf);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
Fclose(rbuf);
trunc(abuf);
Fclose(abuf);
alter(mailname);
Fclose(fbuf);
dot_unlock(mailname);
return;
}
demail();
Fclose(fbuf);
dot_unlock(mailname);
return;
newmail:
printf("Thou hast new mail.\n");
if (fbuf != NULL) {
Fclose(fbuf);
dot_unlock(mailname);
}
}
int
writeback(res)
FILE *res;
{
struct message *mp;
int p, c;
FILE *obuf;
p = 0;
if ((obuf = Fopen(mailname, "r+")) == NULL) {
perror(mailname);
return(-1);
}
#ifndef APPEND
if (res != NULL) {
while ((c = getc(res)) != EOF)
(void) putc(c, obuf);
(void) fflush(obuf);
if (ferror(obuf)) {
perror(mailname);
Fclose(obuf);
return(-1);
}
}
#endif
for (mp = &message[0]; mp < &message[msgCount]; mp++)
if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
p++;
if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
perror(mailname);
Fclose(obuf);
return(-1);
}
}
#ifdef APPEND
if (res != NULL)
while ((c = getc(res)) != EOF)
(void) putc(c, obuf);
#endif
fflush(obuf);
if (!ferror(obuf))
trunc(obuf);
if (ferror(obuf)) {
perror(mailname);
Fclose(obuf);
return(-1);
}
if (res != NULL)
Fclose(res);
Fclose(obuf);
alter(mailname);
if (p == 1)
printf("Held 1 message in %s\n", mailname);
else
printf("Held %d messages in %s\n", p, mailname);
return(0);
}
void
edstop()
{
extern char *tmpdir;
int gotcha, c;
struct message *mp;
FILE *obuf, *ibuf, *readstat = NULL;
struct stat statb;
char *tempname;
if (readonly)
return;
holdsigs();
if (Tflag != NOSTR) {
if ((readstat = Fopen(Tflag, "w")) == NULL)
Tflag = NOSTR;
}
for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
if (mp->m_flag & MNEW) {
mp->m_flag &= ~MNEW;
mp->m_flag |= MSTATUS;
}
if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
gotcha++;
if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
char *id;
if ((id = hfield("article-id", mp)) != NOSTR)
fprintf(readstat, "%s\n", id);
}
}
if (Tflag != NOSTR)
Fclose(readstat);
if (!gotcha || Tflag != NOSTR)
goto done;
ibuf = NULL;
if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
tempname = tempnam(tmpdir, "mbox");
if ((obuf = Fopen(tempname, "w")) == NULL) {
perror(tempname);
relsesigs();
reset(0);
}
if ((ibuf = Fopen(mailname, "r")) == NULL) {
perror(mailname);
Fclose(obuf);
rm(tempname);
relsesigs();
reset(0);
}
fseek(ibuf, (long)mailsize, 0);
while ((c = getc(ibuf)) != EOF)
(void) putc(c, obuf);
(void) fflush(obuf);
if (ferror(obuf)) {
perror(tempname);
Fclose(obuf);
Fclose(ibuf);
rm(tempname);
relsesigs();
reset(0);
}
Fclose(ibuf);
Fclose(obuf);
if ((ibuf = Fopen(tempname, "r")) == NULL) {
perror(tempname);
rm(tempname);
relsesigs();
reset(0);
}
rm(tempname);
free(tempname);
}
printf("\"%s\" ", mailname);
fflush(stdout);
if ((obuf = Fopen(mailname, "r+")) == NULL) {
perror(mailname);
relsesigs();
reset(0);
}
trunc(obuf);
c = 0;
for (mp = &message[0]; mp < &message[msgCount]; mp++) {
if ((mp->m_flag & MDELETED) != 0)
continue;
c++;
if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
perror(mailname);
relsesigs();
reset(0);
}
}
gotcha = (c == 0 && ibuf == NULL);
if (ibuf != NULL) {
while ((c = getc(ibuf)) != EOF)
(void) putc(c, obuf);
Fclose(ibuf);
}
fflush(obuf);
if (ferror(obuf)) {
perror(mailname);
relsesigs();
reset(0);
}
Fclose(obuf);
if (gotcha) {
rm(mailname);
printf("removed\n");
} else
printf("complete\n");
fflush(stdout);
done:
relsesigs();
}