Ejemplo n.º 1
0
/*
 * Save all of the undetermined messages at the top of "mbox"
 * Save all untouched messages back in the system mailbox.
 * Remove the system mailbox, if none saved there.
 */
void
quit(void)
{
    int mcount, p, modify, autohold, anystat, holdbit, nohold;
    FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
    struct message *mp;
    int c, fd;
    struct stat minfo;
    char *mbox, tempname[PATHSIZE];

    /*
     * If we are read only, we can't do anything,
     * so just return quickly.
     */
    if (readonly)
        return;
    /*
     * If editing (not reading system mail box), then do the work
     * in edstop()
     */
    if (edit) {
        edstop();
        return;
    }

    /*
     * See if there any messages to save in mbox.  If no, we
     * can save copying mbox to /tmp and back.
     *
     * Check also to see if any files need to be preserved.
     * Delete all untouched messages to keep them out of mbox.
     * If all the messages are to be preserved, just exit with
     * a message.
     */

    fbuf = Fopen(mailname, "r");
    if (fbuf == NULL)
        goto newmail;
    flock(fileno(fbuf), LOCK_EX);
    rbuf = NULL;
    if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
        printf("New mail has arrived.\n");
        snprintf(tempname, sizeof(tempname), "%s/mail.RqXXXXXXXXXX",
                 tmpdir);
        if ((fd = mkstemp(tempname)) == -1 ||
                (rbuf = Fdopen(fd, "w")) == NULL)
            goto newmail;
#ifdef APPEND
        fseeko(fbuf, mailsize, SEEK_SET);
        while ((c = getc(fbuf)) != EOF)
            putc(c, rbuf);
#else
        p = minfo.st_size - mailsize;
        while (p-- > 0) {
            c = getc(fbuf);
            if (c == EOF)
                goto newmail;
            putc(c, rbuf);
        }
#endif
        Fclose(rbuf);
        if ((rbuf = Fopen(tempname, "r")) == NULL)
            goto newmail;
        rm(tempname);
    }

    /*
     * Adjust the message flags in each message.
     */

    anystat = 0;
    autohold = value("hold") != NULL;
    holdbit = autohold ? MPRESERVE : MBOX;
    nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
    if (value("keepsave") != NULL)
        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 != NULL) {
        if ((readstat = Fopen(Tflag, "w")) == NULL)
            Tflag = NULL;
    }
    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 != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
            char *id;

            if ((id = hfield("article-id", mp)) != NULL)
                fprintf(readstat, "%s\n", id);
        }
    }
    if (Tflag != NULL)
        Fclose(readstat);
    if (p == msgCount && !modify && !anystat) {
        printf("Held %d message%s in %s\n",
               p, p == 1 ? "" : "s", mailname);
        Fclose(fbuf);
        return;
    }
    if (c == 0) {
        if (p != 0) {
            writeback(rbuf);
            Fclose(fbuf);
            return;
        }
        goto cream;
    }

    /*
     * Create another temporary file and copy user's mbox file
     * darin.  If there is no mbox, copy nothing.
     * If he has specified "append" don't copy his mailbox,
     * just copy saveable entries at the end.
     */

    mbox = expand("&");
    mcount = c;
    if (value("append") == NULL) {
        snprintf(tempname, sizeof(tempname), "%s/mail.RmXXXXXXXXXX",
                 tmpdir);
        if ((fd = mkstemp(tempname)) == -1 ||
                (obuf = Fdopen(fd, "w")) == NULL) {
            warn("%s", tempname);
            Fclose(fbuf);
            return;
        }
        if ((ibuf = Fopen(tempname, "r")) == NULL) {
            warn("%s", tempname);
            rm(tempname);
            Fclose(obuf);
            Fclose(fbuf);
            return;
        }
        rm(tempname);
        if ((abuf = Fopen(mbox, "r")) != NULL) {
            while ((c = getc(abuf)) != EOF)
                putc(c, obuf);
            Fclose(abuf);
        }
        if (ferror(obuf)) {
            warnx("%s", tempname);
            Fclose(ibuf);
            Fclose(obuf);
            Fclose(fbuf);
            return;
        }
        Fclose(obuf);
        close(open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600));
        if ((obuf = Fopen(mbox, "r+")) == NULL) {
            warn("%s", mbox);
            Fclose(ibuf);
            Fclose(fbuf);
            return;
        }
    }
    if (value("append") != NULL) {
        if ((obuf = Fopen(mbox, "a")) == NULL) {
            warn("%s", mbox);
            Fclose(fbuf);
            return;
        }
        fchmod(fileno(obuf), 0600);
    }
    for (mp = &message[0]; mp < &message[msgCount]; mp++)
        if (mp->m_flag & MBOX)
            if (sendmessage(mp, obuf, saveignore, NULL) < 0) {
                warnx("%s", mbox);
                Fclose(ibuf);
                Fclose(obuf);
                Fclose(fbuf);
                return;
            }

    /*
     * Copy the user's old mbox contents back
     * to the end of the stuff we just saved.
     * If we are appending, this is unnecessary.
     */

    if (value("append") == NULL) {
        rewind(ibuf);
        c = getc(ibuf);
        while (c != EOF) {
            putc(c, obuf);
            if (ferror(obuf))
                break;
            c = getc(ibuf);
        }
        Fclose(ibuf);
    }
    fflush(obuf);
    trunc(obuf);
    if (ferror(obuf)) {
        warn("%s", mbox);
        Fclose(obuf);
        Fclose(fbuf);
        return;
    }
    Fclose(obuf);
    if (mcount == 1)
        printf("Saved 1 message in mbox\n");
    else
        printf("Saved %d messages in mbox\n", mcount);

    /*
     * Now we are ready to copy back preserved files to
     * the system mailbox, if any were requested.
     */

    if (p != 0) {
        writeback(rbuf);
        Fclose(fbuf);
        return;
    }

    /*
     * Finally, remove his /var/mail file.
     * If new mail has arrived, copy it back.
     */

cream:
    if (rbuf != NULL) {
        abuf = Fopen(mailname, "r+");
        if (abuf == NULL)
            goto newmail;
        while ((c = getc(rbuf)) != EOF)
            putc(c, abuf);
        Fclose(rbuf);
        trunc(abuf);
        Fclose(abuf);
        alter(mailname);
        Fclose(fbuf);
        return;
    }
    demail();
    Fclose(fbuf);
    return;

newmail:
    printf("Thou hast new mail.\n");
    if (fbuf != NULL)
        Fclose(fbuf);
}
Ejemplo n.º 2
0
quit()
{
	int mcount, p, modify, autohold, anystat, holdbit, nohold;
	FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
	register struct message *mp;
	register int c;
	extern char tempQuit[], tempResid[];
	struct stat minfo;
	char *id;

	/*
	 * If we are read only, we can't do anything,
	 * so just return quickly.
	 */

	if (readonly)
		return;
	/*
	 * See if there any messages to save in mbox.  If no, we
	 * can save copying mbox to /tmp and back.
	 *
	 * Check also to see if any files need to be preserved.
	 * Delete all untouched messages to keep them out of mbox.
	 * If all the messages are to be preserved, just exit with
	 * a message.
	 *
	 * If the luser has sent mail to himself, refuse to do
	 * anything with the mailbox, unless mail locking works.
	 */

	fbuf = fopen(mailname, "r");
	if (fbuf == NULL)
		goto newmail;
	flock(fileno(fbuf), LOCK_EX);
#ifndef CANLOCK
	if (selfsent) {
		printf("You have new mail.\n");
		fclose(fbuf);
		return;
	}
#endif
	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, mailsize, 0);
		while ((c = getc(fbuf)) != EOF)
			putc(c, rbuf);
#else
		p = minfo.st_size - mailsize;
		while (p-- > 0) {
			c = getc(fbuf);
			if (c == EOF)
				goto newmail;
			putc(c, rbuf);
		}
#endif
		fclose(rbuf);
		if ((rbuf = fopen(tempResid, "r")) == NULL)
			goto newmail;
		remove(tempResid);
	}

	/*
	 * Adjust the message flags in each message.
	 */

	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) {
			id = hfield("article-id", mp);
			if (id != NOSTR)
				fprintf(readstat, "%s\n", id);
		}
	}
	if (Tflag != NOSTR)
		fclose(readstat);
	if (p == msgCount && !modify && !anystat) {
		if (p == 1)
			printf("Held 1 message in %s\n", mailname);
		else
			printf("Held %2d messages in %s\n", p, mailname);
		fclose(fbuf);
		return;
	}
	if (c == 0) {
		if (p != 0) {
			writeback(rbuf);
			fclose(fbuf);
			return;
		}
		goto cream;
	}

	/*
	 * Create another temporary file and copy user's mbox file
	 * darin.  If there is no mbox, copy nothing.
	 * If he has specified "append" don't copy his mailbox,
	 * just copy saveable entries at the end.
	 */

	mcount = c;
	if (value("append") == NOSTR) {
		if ((obuf = fopen(tempQuit, "w")) == NULL) {
			perror(tempQuit);
			fclose(fbuf);
			return;
		}
		if ((ibuf = fopen(tempQuit, "r")) == NULL) {
			perror(tempQuit);
			remove(tempQuit);
			fclose(obuf);
			fclose(fbuf);
			return;
		}
		remove(tempQuit);
		if ((abuf = fopen(mbox, "r")) != NULL) {
			while ((c = getc(abuf)) != EOF)
				putc(c, obuf);
			fclose(abuf);
		}
		if (ferror(obuf)) {
			perror(tempQuit);
			fclose(ibuf);
			fclose(obuf);
			fclose(fbuf);
			return;
		}
		fclose(obuf);
		close(creat(mbox, 0600));
		if ((obuf = fopen(mbox, "r+")) == NULL) {
			perror(mbox);
			fclose(ibuf);
			fclose(fbuf);
			return;
		}
	}
	if (value("append") != NOSTR) {
		if ((obuf = fopen(mbox, "a")) == NULL) {
			perror(mbox);
			fclose(fbuf);
			return;
		}
		fchmod(fileno(obuf), 0600);
	}
	for (mp = &message[0]; mp < &message[msgCount]; mp++)
		if (mp->m_flag & MBOX)
			if (send(mp, obuf, 0) < 0) {
				perror(mbox);
				fclose(ibuf);
				fclose(obuf);
				fclose(fbuf);
				return;
			}

	/*
	 * Copy the user's old mbox contents back
	 * to the end of the stuff we just saved.
	 * If we are appending, this is unnecessary.
	 */

	if (value("append") == NOSTR) {
		rewind(ibuf);
		c = getc(ibuf);
		while (c != EOF) {
			putc(c, obuf);
			if (ferror(obuf))
				break;
			c = getc(ibuf);
		}
		fclose(ibuf);
		fflush(obuf);
	}
	trunc(obuf);
	if (ferror(obuf)) {
		perror(mbox);
		fclose(obuf);
		fclose(fbuf);
		return;
	}
	fclose(obuf);
	if (mcount == 1)
		printf("Saved 1 message in mbox\n");
	else
		printf("Saved %d messages in mbox\n", mcount);

	/*
	 * Now we are ready to copy back preserved files to
	 * the system mailbox, if any were requested.
	 */

	if (p != 0) {
		writeback(rbuf);
		fclose(fbuf);
		return;
	}

	/*
	 * Finally, remove his /usr/mail file.
	 * If new mail has arrived, copy it back.
	 */

cream:
	if (rbuf != NULL) {
		abuf = fopen(mailname, "r+");
		if (abuf == NULL)
			goto newmail;
		while ((c = getc(rbuf)) != EOF)
			putc(c, abuf);
		fclose(rbuf);
		trunc(abuf);
		fclose(abuf);
		alter(mailname);
		fclose(fbuf);
		return;
	}
	demail();
	fclose(fbuf);
	return;

newmail:
	printf("Thou hast new mail.\n");
	if (fbuf != NULL)
		fclose(fbuf);
}