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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid
[] = "@(#)quit.c 8.2 (Berkeley) 4/28/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
43 * Rcv -- receive mail rationally.
45 * Termination processing.
55 * If we are sourcing, then return 1 so execute() can handle it.
56 * Otherwise, return -1 to abort command loop.
64 * Save all of the undetermined messages at the top of "mbox"
65 * Save all untouched messages back in the system mailbox.
66 * Remove the system mailbox, if none saved there.
71 int mcount
, p
, modify
, autohold
, anystat
, holdbit
, nohold
;
72 FILE *ibuf
, *obuf
, *fbuf
, *rbuf
, *readstat
, *abuf
;
76 char *mbox
, tempname
[PATHSIZE
];
79 * If we are read only, we can't do anything,
80 * so just return quickly.
85 * If editing (not reading system mail box), then do the work
94 * See if there any messages to save in mbox. If no, we
95 * can save copying mbox to /tmp and back.
97 * Check also to see if any files need to be preserved.
98 * Delete all untouched messages to keep them out of mbox.
99 * If all the messages are to be preserved, just exit with
103 fbuf
= Fopen(mailname
, "r");
106 (void)flock(fileno(fbuf
), LOCK_EX
);
108 if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
109 printf("New mail has arrived.\n");
110 (void)snprintf(tempname
, sizeof(tempname
),
111 "%s/mail.RqXXXXXXXXXX", tmpdir
);
112 if ((fd
= mkstemp(tempname
)) == -1 ||
113 (rbuf
= Fdopen(fd
, "w")) == NULL
)
116 (void)fseeko(fbuf
, mailsize
, SEEK_SET
);
117 while ((c
= getc(fbuf
)) != EOF
)
120 p
= minfo
.st_size
- mailsize
;
129 if ((rbuf
= Fopen(tempname
, "r")) == NULL
)
135 * Adjust the message flags in each message.
139 autohold
= value("hold") != NULL
;
140 holdbit
= autohold
? MPRESERVE
: MBOX
;
141 nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
142 if (value("keepsave") != NULL
)
144 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
145 if (mp
->m_flag
& MNEW
) {
147 mp
->m_flag
|= MSTATUS
;
149 if (mp
->m_flag
& MSTATUS
)
151 if ((mp
->m_flag
& MTOUCH
) == 0)
152 mp
->m_flag
|= MPRESERVE
;
153 if ((mp
->m_flag
& nohold
) == 0)
154 mp
->m_flag
|= holdbit
;
158 if ((readstat
= Fopen(Tflag
, "w")) == NULL
)
161 for (c
= 0, p
= 0, mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
162 if (mp
->m_flag
& MBOX
)
164 if (mp
->m_flag
& MPRESERVE
)
166 if (mp
->m_flag
& MODIFY
)
168 if (Tflag
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
171 if ((id
= hfield("article-id", mp
)) != NULL
)
172 fprintf(readstat
, "%s\n", id
);
176 (void)Fclose(readstat
);
177 if (p
== msgCount
&& !modify
&& !anystat
) {
178 printf("Held %d message%s in %s\n",
179 p
, p
== 1 ? "" : "s", mailname
);
193 * Create another temporary file and copy user's mbox file
194 * darin. If there is no mbox, copy nothing.
195 * If he has specified "append" don't copy his mailbox,
196 * just copy saveable entries at the end.
201 if (value("append") == NULL
) {
202 (void)snprintf(tempname
, sizeof(tempname
),
203 "%s/mail.RmXXXXXXXXXX", tmpdir
);
204 if ((fd
= mkstemp(tempname
)) == -1 ||
205 (obuf
= Fdopen(fd
, "w")) == NULL
) {
206 warn("%s", tempname
);
210 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
211 warn("%s", tempname
);
218 if ((abuf
= Fopen(mbox
, "r")) != NULL
) {
219 while ((c
= getc(abuf
)) != EOF
)
224 warnx("%s", tempname
);
231 (void)close(open(mbox
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600));
232 if ((obuf
= Fopen(mbox
, "r+")) == NULL
) {
239 if (value("append") != NULL
) {
240 if ((obuf
= Fopen(mbox
, "a")) == NULL
) {
245 (void)fchmod(fileno(obuf
), 0600);
247 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
248 if (mp
->m_flag
& MBOX
)
249 if (sendmessage(mp
, obuf
, saveignore
, NULL
) < 0) {
258 * Copy the user's old mbox contents back
259 * to the end of the stuff we just saved.
260 * If we are appending, this is unnecessary.
263 if (value("append") == NULL
) {
284 printf("Saved 1 message in mbox\n");
286 printf("Saved %d messages in mbox\n", mcount
);
289 * Now we are ready to copy back preserved files to
290 * the system mailbox, if any were requested.
300 * Finally, remove his /var/mail file.
301 * If new mail has arrived, copy it back.
306 abuf
= Fopen(mailname
, "r+");
309 while ((c
= getc(rbuf
)) != EOF
)
323 printf("Thou hast new mail.\n");
329 * Preserve all the appropriate messages back in the system
330 * mailbox, and print a nice message indicated how many were
331 * saved. On any error, just return -1. Else return 0.
332 * Incorporate the any new mail that we found.
342 if ((obuf
= Fopen(mailname
, "r+")) == NULL
) {
343 warn("%s", mailname
);
348 while ((c
= getc(res
)) != EOF
)
351 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
352 if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
354 if (sendmessage(mp
, obuf
, NULL
, NULL
) < 0) {
355 warnx("%s", mailname
);
362 while ((c
= getc(res
)) != EOF
)
368 warn("%s", mailname
);
377 printf("Held 1 message in %s\n", mailname
);
379 printf("Held %d messages in %s\n", p
, mailname
);
384 * Terminate an editing session by attempting to write out the user's
385 * file from the temporary. Save any new stuff appended to the file.
392 FILE *obuf
, *ibuf
, *readstat
;
394 char tempname
[PATHSIZE
];
400 if ((readstat
= Fopen(Tflag
, "w")) == NULL
)
403 for (mp
= &message
[0], gotcha
= 0; mp
< &message
[msgCount
]; mp
++) {
404 if (mp
->m_flag
& MNEW
) {
406 mp
->m_flag
|= MSTATUS
;
408 if (mp
->m_flag
& (MODIFY
|MDELETED
|MSTATUS
))
410 if (Tflag
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
413 if ((id
= hfield("article-id", mp
)) != NULL
)
414 fprintf(readstat
, "%s\n", id
);
418 (void)Fclose(readstat
);
419 if (!gotcha
|| Tflag
!= NULL
)
422 if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
425 (void)snprintf(tempname
, sizeof(tempname
),
426 "%s/mbox.XXXXXXXXXX", tmpdir
);
427 if ((fd
= mkstemp(tempname
)) == -1 ||
428 (obuf
= Fdopen(fd
, "w")) == NULL
) {
429 warn("%s", tempname
);
433 if ((ibuf
= Fopen(mailname
, "r")) == NULL
) {
434 warn("%s", mailname
);
440 (void)fseeko(ibuf
, mailsize
, SEEK_SET
);
441 while ((c
= getc(ibuf
)) != EOF
)
445 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
446 warn("%s", tempname
);
453 printf("\"%s\" ", mailname
);
454 (void)fflush(stdout
);
455 if ((obuf
= Fopen(mailname
, "r+")) == NULL
) {
456 warn("%s", mailname
);
462 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
463 if ((mp
->m_flag
& MDELETED
) != 0)
466 if (sendmessage(mp
, obuf
, NULL
, NULL
) < 0) {
467 warnx("%s", mailname
);
472 gotcha
= (c
== 0 && ibuf
== NULL
);
474 while ((c
= getc(ibuf
)) != EOF
)
480 warn("%s", mailname
);
489 printf("complete\n");
490 (void)fflush(stdout
);