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