quit.c revision 1.2 1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)quit.c 5.16 (Berkeley) 2/9/91";*/
36 static char rcsid[] = "$Id: quit.c,v 1.2 1993/08/01 18:12:58 mycroft Exp $";
37 #endif /* not lint */
38
39 #include "rcv.h"
40 #include <sys/stat.h>
41 #include <sys/file.h>
42
43 /*
44 * Rcv -- receive mail rationally.
45 *
46 * Termination processing.
47 */
48
49 /*
50 * The "quit" command.
51 */
52 quitcmd()
53 {
54 /*
55 * If we are sourcing, then return 1 so execute() can handle it.
56 * Otherwise, return -1 to abort command loop.
57 */
58 if (sourcing)
59 return 1;
60 return -1;
61 }
62
63 /*
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.
67 */
68
69 quit()
70 {
71 int mcount, p, modify, autohold, anystat, holdbit, nohold;
72 FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
73 register struct message *mp;
74 register int c;
75 extern char tempQuit[], tempResid[];
76 struct stat minfo;
77 char *mbox;
78
79 /*
80 * If we are read only, we can't do anything,
81 * so just return quickly.
82 */
83 if (readonly)
84 return;
85 /*
86 * If editing (not reading system mail box), then do the work
87 * in edstop()
88 */
89 if (edit) {
90 edstop();
91 return;
92 }
93
94 /*
95 * See if there any messages to save in mbox. If no, we
96 * can save copying mbox to /tmp and back.
97 *
98 * Check also to see if any files need to be preserved.
99 * Delete all untouched messages to keep them out of mbox.
100 * If all the messages are to be preserved, just exit with
101 * a message.
102 */
103
104 fbuf = Fopen(mailname, "r");
105 if (fbuf == NULL)
106 goto newmail;
107 flock(fileno(fbuf), LOCK_EX);
108 rbuf = NULL;
109 if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
110 printf("New mail has arrived.\n");
111 rbuf = Fopen(tempResid, "w");
112 if (rbuf == NULL || fbuf == NULL)
113 goto newmail;
114 #ifdef APPEND
115 fseek(fbuf, mailsize, 0);
116 while ((c = getc(fbuf)) != EOF)
117 (void) putc(c, rbuf);
118 #else
119 p = minfo.st_size - mailsize;
120 while (p-- > 0) {
121 c = getc(fbuf);
122 if (c == EOF)
123 goto newmail;
124 (void) putc(c, rbuf);
125 }
126 #endif
127 Fclose(rbuf);
128 if ((rbuf = Fopen(tempResid, "r")) == NULL)
129 goto newmail;
130 rm(tempResid);
131 }
132
133 /*
134 * Adjust the message flags in each message.
135 */
136
137 anystat = 0;
138 autohold = value("hold") != NOSTR;
139 holdbit = autohold ? MPRESERVE : MBOX;
140 nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
141 if (value("keepsave") != NOSTR)
142 nohold &= ~MSAVED;
143 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
144 if (mp->m_flag & MNEW) {
145 mp->m_flag &= ~MNEW;
146 mp->m_flag |= MSTATUS;
147 }
148 if (mp->m_flag & MSTATUS)
149 anystat++;
150 if ((mp->m_flag & MTOUCH) == 0)
151 mp->m_flag |= MPRESERVE;
152 if ((mp->m_flag & nohold) == 0)
153 mp->m_flag |= holdbit;
154 }
155 modify = 0;
156 if (Tflag != NOSTR) {
157 if ((readstat = Fopen(Tflag, "w")) == NULL)
158 Tflag = NOSTR;
159 }
160 for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
161 if (mp->m_flag & MBOX)
162 c++;
163 if (mp->m_flag & MPRESERVE)
164 p++;
165 if (mp->m_flag & MODIFY)
166 modify++;
167 if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
168 char *id;
169
170 if ((id = hfield("article-id", mp)) != NOSTR)
171 fprintf(readstat, "%s\n", id);
172 }
173 }
174 if (Tflag != NOSTR)
175 Fclose(readstat);
176 if (p == msgCount && !modify && !anystat) {
177 printf("Held %d message%s in %s\n",
178 p, p == 1 ? "" : "s", mailname);
179 Fclose(fbuf);
180 return;
181 }
182 if (c == 0) {
183 if (p != 0) {
184 writeback(rbuf);
185 Fclose(fbuf);
186 return;
187 }
188 goto cream;
189 }
190
191 /*
192 * Create another temporary file and copy user's mbox file
193 * darin. If there is no mbox, copy nothing.
194 * If he has specified "append" don't copy his mailbox,
195 * just copy saveable entries at the end.
196 */
197
198 mbox = expand("&");
199 mcount = c;
200 if (value("append") == NOSTR) {
201 if ((obuf = Fopen(tempQuit, "w")) == NULL) {
202 perror(tempQuit);
203 Fclose(fbuf);
204 return;
205 }
206 if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
207 perror(tempQuit);
208 rm(tempQuit);
209 Fclose(obuf);
210 Fclose(fbuf);
211 return;
212 }
213 rm(tempQuit);
214 if ((abuf = Fopen(mbox, "r")) != NULL) {
215 while ((c = getc(abuf)) != EOF)
216 (void) putc(c, obuf);
217 Fclose(abuf);
218 }
219 if (ferror(obuf)) {
220 perror(tempQuit);
221 Fclose(ibuf);
222 Fclose(obuf);
223 Fclose(fbuf);
224 return;
225 }
226 Fclose(obuf);
227 close(creat(mbox, 0600));
228 if ((obuf = Fopen(mbox, "r+")) == NULL) {
229 perror(mbox);
230 Fclose(ibuf);
231 Fclose(fbuf);
232 return;
233 }
234 }
235 if (value("append") != NOSTR) {
236 if ((obuf = Fopen(mbox, "a")) == NULL) {
237 perror(mbox);
238 Fclose(fbuf);
239 return;
240 }
241 fchmod(fileno(obuf), 0600);
242 }
243 for (mp = &message[0]; mp < &message[msgCount]; mp++)
244 if (mp->m_flag & MBOX)
245 if (send(mp, obuf, saveignore, NOSTR) < 0) {
246 perror(mbox);
247 Fclose(ibuf);
248 Fclose(obuf);
249 Fclose(fbuf);
250 return;
251 }
252
253 /*
254 * Copy the user's old mbox contents back
255 * to the end of the stuff we just saved.
256 * If we are appending, this is unnecessary.
257 */
258
259 if (value("append") == NOSTR) {
260 rewind(ibuf);
261 c = getc(ibuf);
262 while (c != EOF) {
263 (void) putc(c, obuf);
264 if (ferror(obuf))
265 break;
266 c = getc(ibuf);
267 }
268 Fclose(ibuf);
269 fflush(obuf);
270 }
271 trunc(obuf);
272 if (ferror(obuf)) {
273 perror(mbox);
274 Fclose(obuf);
275 Fclose(fbuf);
276 return;
277 }
278 Fclose(obuf);
279 if (mcount == 1)
280 printf("Saved 1 message in mbox\n");
281 else
282 printf("Saved %d messages in mbox\n", mcount);
283
284 /*
285 * Now we are ready to copy back preserved files to
286 * the system mailbox, if any were requested.
287 */
288
289 if (p != 0) {
290 writeback(rbuf);
291 Fclose(fbuf);
292 return;
293 }
294
295 /*
296 * Finally, remove his /usr/mail file.
297 * If new mail has arrived, copy it back.
298 */
299
300 cream:
301 if (rbuf != NULL) {
302 abuf = Fopen(mailname, "r+");
303 if (abuf == NULL)
304 goto newmail;
305 while ((c = getc(rbuf)) != EOF)
306 (void) putc(c, abuf);
307 Fclose(rbuf);
308 trunc(abuf);
309 Fclose(abuf);
310 alter(mailname);
311 Fclose(fbuf);
312 return;
313 }
314 demail();
315 Fclose(fbuf);
316 return;
317
318 newmail:
319 printf("Thou hast new mail.\n");
320 if (fbuf != NULL)
321 Fclose(fbuf);
322 }
323
324 /*
325 * Preserve all the appropriate messages back in the system
326 * mailbox, and print a nice message indicated how many were
327 * saved. On any error, just return -1. Else return 0.
328 * Incorporate the any new mail that we found.
329 */
330 writeback(res)
331 register FILE *res;
332 {
333 register struct message *mp;
334 register int p, c;
335 FILE *obuf;
336
337 p = 0;
338 if ((obuf = Fopen(mailname, "r+")) == NULL) {
339 perror(mailname);
340 return(-1);
341 }
342 #ifndef APPEND
343 if (res != NULL)
344 while ((c = getc(res)) != EOF)
345 (void) putc(c, obuf);
346 #endif
347 for (mp = &message[0]; mp < &message[msgCount]; mp++)
348 if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
349 p++;
350 if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
351 perror(mailname);
352 Fclose(obuf);
353 return(-1);
354 }
355 }
356 #ifdef APPEND
357 if (res != NULL)
358 while ((c = getc(res)) != EOF)
359 (void) putc(c, obuf);
360 #endif
361 fflush(obuf);
362 trunc(obuf);
363 if (ferror(obuf)) {
364 perror(mailname);
365 Fclose(obuf);
366 return(-1);
367 }
368 if (res != NULL)
369 Fclose(res);
370 Fclose(obuf);
371 alter(mailname);
372 if (p == 1)
373 printf("Held 1 message in %s\n", mailname);
374 else
375 printf("Held %d messages in %s\n", p, mailname);
376 return(0);
377 }
378
379 /*
380 * Terminate an editing session by attempting to write out the user's
381 * file from the temporary. Save any new stuff appended to the file.
382 */
383 edstop()
384 {
385 register int gotcha, c;
386 register struct message *mp;
387 FILE *obuf, *ibuf, *readstat;
388 struct stat statb;
389 char tempname[30];
390 char *mktemp();
391
392 if (readonly)
393 return;
394 holdsigs();
395 if (Tflag != NOSTR) {
396 if ((readstat = Fopen(Tflag, "w")) == NULL)
397 Tflag = NOSTR;
398 }
399 for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
400 if (mp->m_flag & MNEW) {
401 mp->m_flag &= ~MNEW;
402 mp->m_flag |= MSTATUS;
403 }
404 if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
405 gotcha++;
406 if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
407 char *id;
408
409 if ((id = hfield("article-id", mp)) != NOSTR)
410 fprintf(readstat, "%s\n", id);
411 }
412 }
413 if (Tflag != NOSTR)
414 Fclose(readstat);
415 if (!gotcha || Tflag != NOSTR)
416 goto done;
417 ibuf = NULL;
418 if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
419 strcpy(tempname, _PATH_TMP);
420 strcat(tempname, "mboxXXXXXX");
421 mktemp(tempname);
422 if ((obuf = Fopen(tempname, "w")) == NULL) {
423 perror(tempname);
424 relsesigs();
425 reset(0);
426 }
427 if ((ibuf = Fopen(mailname, "r")) == NULL) {
428 perror(mailname);
429 Fclose(obuf);
430 rm(tempname);
431 relsesigs();
432 reset(0);
433 }
434 fseek(ibuf, mailsize, 0);
435 while ((c = getc(ibuf)) != EOF)
436 (void) putc(c, obuf);
437 Fclose(ibuf);
438 Fclose(obuf);
439 if ((ibuf = Fopen(tempname, "r")) == NULL) {
440 perror(tempname);
441 rm(tempname);
442 relsesigs();
443 reset(0);
444 }
445 rm(tempname);
446 }
447 printf("\"%s\" ", mailname);
448 fflush(stdout);
449 if ((obuf = Fopen(mailname, "r+")) == NULL) {
450 perror(mailname);
451 relsesigs();
452 reset(0);
453 }
454 trunc(obuf);
455 c = 0;
456 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
457 if ((mp->m_flag & MDELETED) != 0)
458 continue;
459 c++;
460 if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
461 perror(mailname);
462 relsesigs();
463 reset(0);
464 }
465 }
466 gotcha = (c == 0 && ibuf == NULL);
467 if (ibuf != NULL) {
468 while ((c = getc(ibuf)) != EOF)
469 (void) putc(c, obuf);
470 Fclose(ibuf);
471 }
472 fflush(obuf);
473 if (ferror(obuf)) {
474 perror(mailname);
475 relsesigs();
476 reset(0);
477 }
478 Fclose(obuf);
479 if (gotcha) {
480 rm(mailname);
481 printf("removed\n");
482 } else
483 printf("complete\n");
484 fflush(stdout);
485
486 done:
487 relsesigs();
488 }
489