xref: /freebsd/usr.bin/mail/quit.c (revision b7a1a0a5ff8895e6a5c9404d02357ac9e2eb8091)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  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[] = "@(#)quit.c	8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37 
38 #include "rcv.h"
39 #include <fcntl.h>
40 #include "extern.h"
41 
42 /*
43  * Rcv -- receive mail rationally.
44  *
45  * Termination processing.
46  */
47 
48 /*
49  * The "quit" command.
50  */
51 int
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 void
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, (long)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 int
331 writeback(res)
332 	register FILE *res;
333 {
334 	register struct message *mp;
335 	register int p, c;
336 	FILE *obuf;
337 
338 	p = 0;
339 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
340 		perror(mailname);
341 		return(-1);
342 	}
343 #ifndef APPEND
344 	if (res != NULL)
345 		while ((c = getc(res)) != EOF)
346 			(void) putc(c, obuf);
347 #endif
348 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
349 		if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
350 			p++;
351 			if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
352 				perror(mailname);
353 				Fclose(obuf);
354 				return(-1);
355 			}
356 		}
357 #ifdef APPEND
358 	if (res != NULL)
359 		while ((c = getc(res)) != EOF)
360 			(void) putc(c, obuf);
361 #endif
362 	fflush(obuf);
363 	trunc(obuf);
364 	if (ferror(obuf)) {
365 		perror(mailname);
366 		Fclose(obuf);
367 		return(-1);
368 	}
369 	if (res != NULL)
370 		Fclose(res);
371 	Fclose(obuf);
372 	alter(mailname);
373 	if (p == 1)
374 		printf("Held 1 message in %s\n", mailname);
375 	else
376 		printf("Held %d messages in %s\n", p, mailname);
377 	return(0);
378 }
379 
380 /*
381  * Terminate an editing session by attempting to write out the user's
382  * file from the temporary.  Save any new stuff appended to the file.
383  */
384 void
385 edstop()
386 {
387 	extern char *tmpdir;
388 	register int gotcha, c;
389 	register struct message *mp;
390 	FILE *obuf, *ibuf, *readstat;
391 	struct stat statb;
392 	char tempname[30];
393 	char *mktemp();
394 
395 	if (readonly)
396 		return;
397 	holdsigs();
398 	if (Tflag != NOSTR) {
399 		if ((readstat = Fopen(Tflag, "w")) == NULL)
400 			Tflag = NOSTR;
401 	}
402 	for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
403 		if (mp->m_flag & MNEW) {
404 			mp->m_flag &= ~MNEW;
405 			mp->m_flag |= MSTATUS;
406 		}
407 		if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
408 			gotcha++;
409 		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
410 			char *id;
411 
412 			if ((id = hfield("article-id", mp)) != NOSTR)
413 				fprintf(readstat, "%s\n", id);
414 		}
415 	}
416 	if (Tflag != NOSTR)
417 		Fclose(readstat);
418 	if (!gotcha || Tflag != NOSTR)
419 		goto done;
420 	ibuf = NULL;
421 	if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
422 		strcpy(tempname, tmpdir);
423 		strcat(tempname, "mboxXXXXXX");
424 		mktemp(tempname);
425 		if ((obuf = Fopen(tempname, "w")) == NULL) {
426 			perror(tempname);
427 			relsesigs();
428 			reset(0);
429 		}
430 		if ((ibuf = Fopen(mailname, "r")) == NULL) {
431 			perror(mailname);
432 			Fclose(obuf);
433 			rm(tempname);
434 			relsesigs();
435 			reset(0);
436 		}
437 		fseek(ibuf, (long)mailsize, 0);
438 		while ((c = getc(ibuf)) != EOF)
439 			(void) putc(c, obuf);
440 		Fclose(ibuf);
441 		Fclose(obuf);
442 		if ((ibuf = Fopen(tempname, "r")) == NULL) {
443 			perror(tempname);
444 			rm(tempname);
445 			relsesigs();
446 			reset(0);
447 		}
448 		rm(tempname);
449 	}
450 	printf("\"%s\" ", mailname);
451 	fflush(stdout);
452 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
453 		perror(mailname);
454 		relsesigs();
455 		reset(0);
456 	}
457 	trunc(obuf);
458 	c = 0;
459 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
460 		if ((mp->m_flag & MDELETED) != 0)
461 			continue;
462 		c++;
463 		if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
464 			perror(mailname);
465 			relsesigs();
466 			reset(0);
467 		}
468 	}
469 	gotcha = (c == 0 && ibuf == NULL);
470 	if (ibuf != NULL) {
471 		while ((c = getc(ibuf)) != EOF)
472 			(void) putc(c, obuf);
473 		Fclose(ibuf);
474 	}
475 	fflush(obuf);
476 	if (ferror(obuf)) {
477 		perror(mailname);
478 		relsesigs();
479 		reset(0);
480 	}
481 	Fclose(obuf);
482 	if (gotcha) {
483 		rm(mailname);
484 		printf("removed\n");
485 	} else
486 		printf("complete\n");
487 	fflush(stdout);
488 
489 done:
490 	relsesigs();
491 }
492