2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)quit.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
47 * Rcv -- receive mail rationally.
49 * Termination processing.
59 * If we are sourcing, then return 1 so execute() can handle it.
60 * Otherwise, return -1 to abort command loop.
68 * Save all of the undetermined messages at the top of "mbox"
69 * Save all untouched messages back in the system mailbox.
70 * Remove the system mailbox, if none saved there.
75 int mcount, p, modify, autohold, anystat, holdbit, nohold;
76 FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
77 register struct message *mp;
81 char *mbox, tempname[PATHSIZE];
84 * If we are read only, we can't do anything,
85 * so just return quickly.
90 * If editing (not reading system mail box), then do the work
99 * See if there any messages to save in mbox. If no, we
100 * can save copying mbox to /tmp and back.
102 * Check also to see if any files need to be preserved.
103 * Delete all untouched messages to keep them out of mbox.
104 * If all the messages are to be preserved, just exit with
108 fbuf = Fopen(mailname, "r");
111 flock(fileno(fbuf), LOCK_EX);
113 if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
114 printf("New mail has arrived.\n");
115 snprintf(tempname, sizeof(tempname), "%s/mail.RqXXXXXXXXXX", tmpdir);
116 if ((fd = mkstemp(tempname)) == -1 ||
117 (rbuf = Fdopen(fd, "w")) == NULL)
120 fseek(fbuf, (long)mailsize, 0);
121 while ((c = getc(fbuf)) != EOF)
122 (void) putc(c, rbuf);
124 p = minfo.st_size - mailsize;
129 (void) putc(c, rbuf);
133 if ((rbuf = Fopen(tempname, "r")) == NULL)
139 * Adjust the message flags in each message.
143 autohold = value("hold") != NOSTR;
144 holdbit = autohold ? MPRESERVE : MBOX;
145 nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
146 if (value("keepsave") != NOSTR)
148 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
149 if (mp->m_flag & MNEW) {
151 mp->m_flag |= MSTATUS;
153 if (mp->m_flag & MSTATUS)
155 if ((mp->m_flag & MTOUCH) == 0)
156 mp->m_flag |= MPRESERVE;
157 if ((mp->m_flag & nohold) == 0)
158 mp->m_flag |= holdbit;
161 if (Tflag != NOSTR) {
162 if ((readstat = Fopen(Tflag, "w")) == NULL)
165 for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
166 if (mp->m_flag & MBOX)
168 if (mp->m_flag & MPRESERVE)
170 if (mp->m_flag & MODIFY)
172 if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
175 if ((id = hfield("article-id", mp)) != NOSTR)
176 fprintf(readstat, "%s\n", id);
181 if (p == msgCount && !modify && !anystat) {
182 printf("Held %d message%s in %s\n",
183 p, p == 1 ? "" : "s", mailname);
197 * Create another temporary file and copy user's mbox file
198 * darin. If there is no mbox, copy nothing.
199 * If he has specified "append" don't copy his mailbox,
200 * just copy saveable entries at the end.
205 if (value("append") == NOSTR) {
206 snprintf(tempname, sizeof(tempname), "%s/mail.RmXXXXXXXXXX", tmpdir);
207 if ((fd = mkstemp(tempname)) == -1 ||
208 (obuf = Fdopen(fd, "w")) == NULL) {
209 warn("%s", tempname);
213 if ((ibuf = Fopen(tempname, "r")) == NULL) {
214 warn("%s", tempname);
221 if ((abuf = Fopen(mbox, "r")) != NULL) {
222 while ((c = getc(abuf)) != EOF)
223 (void) putc(c, obuf);
227 warnx("%s", tempname);
234 close(open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600));
235 if ((obuf = Fopen(mbox, "r+")) == NULL) {
242 if (value("append") != NOSTR) {
243 if ((obuf = Fopen(mbox, "a")) == NULL) {
248 fchmod(fileno(obuf), 0600);
250 for (mp = &message[0]; mp < &message[msgCount]; mp++)
251 if (mp->m_flag & MBOX)
252 if (sendmessage(mp, obuf, saveignore, NOSTR) < 0) {
261 * Copy the user's old mbox contents back
262 * to the end of the stuff we just saved.
263 * If we are appending, this is unnecessary.
266 if (value("append") == NOSTR) {
270 (void) putc(c, obuf);
287 printf("Saved 1 message in mbox\n");
289 printf("Saved %d messages in mbox\n", mcount);
292 * Now we are ready to copy back preserved files to
293 * the system mailbox, if any were requested.
303 * Finally, remove his /usr/mail file.
304 * If new mail has arrived, copy it back.
309 abuf = Fopen(mailname, "r+");
312 while ((c = getc(rbuf)) != EOF)
313 (void) putc(c, abuf);
326 printf("Thou hast new mail.\n");
332 * Preserve all the appropriate messages back in the system
333 * mailbox, and print a nice message indicated how many were
334 * saved. On any error, just return -1. Else return 0.
335 * Incorporate the any new mail that we found.
341 register struct message *mp;
346 if ((obuf = Fopen(mailname, "r+")) == NULL) {
347 warn("%s", mailname);
352 while ((c = getc(res)) != EOF)
353 (void) putc(c, obuf);
355 for (mp = &message[0]; mp < &message[msgCount]; mp++)
356 if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
358 if (sendmessage(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
359 warnx("%s", mailname);
366 while ((c = getc(res)) != EOF)
367 (void) putc(c, obuf);
372 warn("%s", mailname);
381 printf("Held 1 message in %s\n", mailname);
383 printf("Held %d messages in %s\n", p, mailname);
388 * Terminate an editing session by attempting to write out the user's
389 * file from the temporary. Save any new stuff appended to the file.
394 register int gotcha, c;
395 register struct message *mp;
396 FILE *obuf, *ibuf, *readstat;
398 char tempname[PATHSIZE];
403 if (Tflag != NOSTR) {
404 if ((readstat = Fopen(Tflag, "w")) == NULL)
407 for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
408 if (mp->m_flag & MNEW) {
410 mp->m_flag |= MSTATUS;
412 if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
414 if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
417 if ((id = hfield("article-id", mp)) != NOSTR)
418 fprintf(readstat, "%s\n", id);
423 if (!gotcha || Tflag != NOSTR)
426 if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
429 snprintf(tempname, sizeof(tempname), "%s/mbox.XXXXXXXXXX", tmpdir);
430 if ((fd = mkstemp(tempname)) == -1 || (obuf = Fdopen(fd, "w")) == NULL) {
431 warn("%s", tempname);
435 if ((ibuf = Fopen(mailname, "r")) == NULL) {
436 warn("%s", mailname);
442 fseek(ibuf, (long)mailsize, 0);
443 while ((c = getc(ibuf)) != EOF)
444 (void) putc(c, obuf);
447 if ((ibuf = Fopen(tempname, "r")) == NULL) {
448 warn("%s", tempname);
455 printf("\"%s\" ", mailname);
457 if ((obuf = Fopen(mailname, "r+")) == NULL) {
458 warn("%s", mailname);
464 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
465 if ((mp->m_flag & MDELETED) != 0)
468 if (sendmessage(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
469 warnx("%s", mailname);
474 gotcha = (c == 0 && ibuf == NULL);
476 while ((c = getc(ibuf)) != EOF)
477 (void) putc(c, obuf);
482 warn("%s", mailname);
491 printf("complete\n");