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