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.2 (Berkeley) 4/28/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
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
;
80 char *mbox
, tempname
[PATHSIZE
];
83 * If we are read only, we can't do anything,
84 * so just return quickly.
89 * If editing (not reading system mail box), then do the work
98 * See if there any messages to save in mbox. If no, we
99 * can save copying mbox to /tmp and back.
101 * Check also to see if any files need to be preserved.
102 * Delete all untouched messages to keep them out of mbox.
103 * If all the messages are to be preserved, just exit with
107 fbuf
= Fopen(mailname
, "r");
110 (void)flock(fileno(fbuf
), LOCK_EX
);
112 if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
113 printf("New mail has arrived.\n");
114 (void)snprintf(tempname
, sizeof(tempname
),
115 "%s/mail.RqXXXXXXXXXX", tmpdir
);
116 if ((fd
= mkstemp(tempname
)) == -1 ||
117 (rbuf
= Fdopen(fd
, "w")) == NULL
)
120 (void)fseeko(fbuf
, mailsize
, SEEK_SET
);
121 while ((c
= getc(fbuf
)) != EOF
)
124 p
= minfo
.st_size
- mailsize
;
133 if ((rbuf
= Fopen(tempname
, "r")) == NULL
)
139 * Adjust the message flags in each message.
143 autohold
= value("hold") != NULL
;
144 holdbit
= autohold
? MPRESERVE
: MBOX
;
145 nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
146 if (value("keepsave") != NULL
)
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
;
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
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
175 if ((id
= hfield("article-id", mp
)) != NULL
)
176 fprintf(readstat
, "%s\n", id
);
180 (void)Fclose(readstat
);
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") == NULL
) {
206 (void)snprintf(tempname
, sizeof(tempname
),
207 "%s/mail.RmXXXXXXXXXX", tmpdir
);
208 if ((fd
= mkstemp(tempname
)) == -1 ||
209 (obuf
= Fdopen(fd
, "w")) == NULL
) {
210 warn("%s", tempname
);
214 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
215 warn("%s", tempname
);
222 if ((abuf
= Fopen(mbox
, "r")) != NULL
) {
223 while ((c
= getc(abuf
)) != EOF
)
228 warnx("%s", tempname
);
235 (void)close(open(mbox
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600));
236 if ((obuf
= Fopen(mbox
, "r+")) == NULL
) {
243 if (value("append") != NULL
) {
244 if ((obuf
= Fopen(mbox
, "a")) == NULL
) {
249 (void)fchmod(fileno(obuf
), 0600);
251 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
252 if (mp
->m_flag
& MBOX
)
253 if (sendmessage(mp
, obuf
, saveignore
, NULL
) < 0) {
262 * Copy the user's old mbox contents back
263 * to the end of the stuff we just saved.
264 * If we are appending, this is unnecessary.
267 if (value("append") == NULL
) {
288 printf("Saved 1 message in mbox\n");
290 printf("Saved %d messages in mbox\n", mcount
);
293 * Now we are ready to copy back preserved files to
294 * the system mailbox, if any were requested.
304 * Finally, remove his /var/mail file.
305 * If new mail has arrived, copy it back.
310 abuf
= Fopen(mailname
, "r+");
313 while ((c
= getc(rbuf
)) != EOF
)
327 printf("Thou hast new mail.\n");
333 * Preserve all the appropriate messages back in the system
334 * mailbox, and print a nice message indicated how many were
335 * saved. On any error, just return -1. Else return 0.
336 * Incorporate the any new mail that we found.
347 if ((obuf
= Fopen(mailname
, "r+")) == NULL
) {
348 warn("%s", mailname
);
353 while ((c
= getc(res
)) != EOF
)
356 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
357 if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
359 if (sendmessage(mp
, obuf
, NULL
, NULL
) < 0) {
360 warnx("%s", mailname
);
367 while ((c
= getc(res
)) != EOF
)
373 warn("%s", mailname
);
382 printf("Held 1 message in %s\n", mailname
);
384 printf("Held %d messages in %s\n", p
, mailname
);
389 * Terminate an editing session by attempting to write out the user's
390 * file from the temporary. Save any new stuff appended to the file.
397 FILE *obuf
, *ibuf
, *readstat
;
399 char tempname
[PATHSIZE
];
405 if ((readstat
= Fopen(Tflag
, "w")) == NULL
)
408 for (mp
= &message
[0], gotcha
= 0; mp
< &message
[msgCount
]; mp
++) {
409 if (mp
->m_flag
& MNEW
) {
411 mp
->m_flag
|= MSTATUS
;
413 if (mp
->m_flag
& (MODIFY
|MDELETED
|MSTATUS
))
415 if (Tflag
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
418 if ((id
= hfield("article-id", mp
)) != NULL
)
419 fprintf(readstat
, "%s\n", id
);
423 (void)Fclose(readstat
);
424 if (!gotcha
|| Tflag
!= NULL
)
427 if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
430 (void)snprintf(tempname
, sizeof(tempname
),
431 "%s/mbox.XXXXXXXXXX", tmpdir
);
432 if ((fd
= mkstemp(tempname
)) == -1 ||
433 (obuf
= Fdopen(fd
, "w")) == NULL
) {
434 warn("%s", tempname
);
438 if ((ibuf
= Fopen(mailname
, "r")) == NULL
) {
439 warn("%s", mailname
);
445 (void)fseeko(ibuf
, mailsize
, SEEK_SET
);
446 while ((c
= getc(ibuf
)) != EOF
)
450 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
451 warn("%s", tempname
);
458 printf("\"%s\" ", mailname
);
459 (void)fflush(stdout
);
460 if ((obuf
= Fopen(mailname
, "r+")) == NULL
) {
461 warn("%s", mailname
);
467 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
468 if ((mp
->m_flag
& MDELETED
) != 0)
471 if (sendmessage(mp
, obuf
, NULL
, NULL
) < 0) {
472 warnx("%s", mailname
);
477 gotcha
= (c
== 0 && ibuf
== NULL
);
479 while ((c
= getc(ibuf
)) != EOF
)
485 warn("%s", mailname
);
494 printf("complete\n");
495 (void)fflush(stdout
);