quit()+descendants: add hold_sigs_on argument, let'em handle it
[s-mailx.git] / quit.c
blob8c653f795b15e6c3e8d691c85cc3393dbe7afc6a
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Termination processing.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE quit
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #include <utime.h>
44 enum quitflags {
45 QUITFLAG_HOLD = 1<<0,
46 QUITFLAG_KEEP = 1<<1,
47 QUITFLAG_KEEPSAVE = 1<<2,
48 QUITFLAG_APPEND = 1<<3,
49 QUITFLAG_EMPTYBOX = 1<<4
52 struct quitnames {
53 enum quitflags flag;
54 enum okeys okey;
57 static struct quitnames const _quitnames[] = {
58 {QUITFLAG_HOLD, ok_b_hold},
59 {QUITFLAG_KEEP, ok_b_keep},
60 {QUITFLAG_KEEPSAVE, ok_b_keepsave},
61 {QUITFLAG_APPEND, ok_b_append},
62 {QUITFLAG_EMPTYBOX, ok_b_emptybox} /* TODO obsolete emptybox */
65 static char _mboxname[PATH_MAX]; /* Name of mbox */
67 /* Touch the indicated file */
68 static void _alter(char const *name);
70 /* Preserve all the appropriate messages back in the system mailbox, and print
71 * a nice message indicated how many were saved. On any error, just return -1.
72 * Else return 0. Incorporate the any new mail that we found */
73 static int writeback(FILE *res, FILE *obuf);
75 /* Terminate an editing session by attempting to write out the user's file from
76 * the temporary. Save any new stuff appended to the file */
77 static bool_t edstop(void);
79 /* Remove "mailname", unless *keep* says otherwise; force truncation, then */
80 static void _demail(void);
82 static void
83 _alter(char const *name) /* TODO error handling */
85 #ifdef HAVE_UTIMENSAT
86 struct timespec tsa[2];
87 #else
88 struct stat sb;
89 struct utimbuf utb;
90 #endif
91 NYD_ENTER;
93 #ifdef HAVE_UTIMENSAT
94 tsa[0].tv_sec = n_time_epoch() + 1;
95 tsa[0].tv_nsec = 0;
96 tsa[1].tv_nsec = UTIME_OMIT;
97 utimensat(AT_FDCWD, name, tsa, 0);
98 #else
99 if (!stat(name, &sb)) {
100 utb.actime = n_time_epoch() + 1;
101 utb.modtime = sb.st_mtime;
102 utime(name, &utb);
104 #endif
105 NYD_LEAVE;
108 static int
109 writeback(FILE *res, FILE *obuf)
111 struct message *mp;
112 int rv = -1, p, c;
113 NYD_ENTER;
115 if (fseek(obuf, 0L, SEEK_SET) == -1)
116 goto jleave;
118 #ifndef APPEND
119 if (res != NULL)
120 while ((c = getc(res)) != EOF)
121 putc(c, obuf);
122 #endif
123 srelax_hold();
124 for (p = 0, mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
125 if ((mp->m_flag & MPRESERVE) || !(mp->m_flag & MTOUCH)) {
126 ++p;
127 if (sendmp(mp, obuf, NULL, NULL, SEND_MBOX, NULL) < 0) {
128 n_perr(mailname, 0);
129 srelax_rele();
130 goto jerror;
132 srelax();
134 srelax_rele();
135 #ifdef APPEND
136 if (res != NULL)
137 while ((c = getc(res)) != EOF)
138 putc(c, obuf);
139 #endif
140 ftrunc(obuf);
142 if (ferror(obuf)) {
143 n_perr(mailname, 0);
144 jerror:
145 fseek(obuf, 0L, SEEK_SET);
146 goto jleave;
148 if (fseek(obuf, 0L, SEEK_SET) == -1)
149 goto jleave;
151 _alter(mailname);
152 if (p == 1)
153 printf(_("Held 1 message in %s\n"), displayname);
154 else
155 printf(_("Held %d messages in %s\n"), p, displayname);
156 rv = 0;
157 jleave:
158 if (res != NULL)
159 Fclose(res);
160 NYD_LEAVE;
161 return rv;
164 static bool_t
165 edstop(void) /* TODO oh my god */
167 int gotcha, c;
168 struct message *mp;
169 FILE *obuf = NULL, *ibuf = NULL;
170 struct stat statb;
171 bool_t rv;
172 NYD_ENTER;
174 rv = TRU1;
176 if (mb.mb_perm == 0)
177 goto j_leave;
179 for (mp = message, gotcha = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
180 if (mp->m_flag & MNEW) {
181 mp->m_flag &= ~MNEW;
182 mp->m_flag |= MSTATUS;
184 if (mp->m_flag & (MODIFY | MDELETED | MSTATUS | MFLAG | MUNFLAG |
185 MANSWER | MUNANSWER | MDRAFT | MUNDRAFT))
186 ++gotcha;
188 if (!gotcha)
189 goto jleave;
191 rv = FAL0;
193 /* TODO This is too simple minded? We should regenerate an index file
194 * TODO to be able to truly tell whether *anything* has changed!
195 * TODO (Or better: only come here.. then! It is an *object method!* */
196 /* TODO Ignoring stat error is easy, huh? */
197 if (!stat(mailname, &statb) && statb.st_size > mailsize) {
198 if ((obuf = Ftmp(NULL, "edstop", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
199 NULL) {
200 n_perr(_("tmpfile"), 0);
201 goto jleave;
203 if ((ibuf = Zopen(mailname, "r")) == NULL) {
204 n_perr(mailname, 0);
205 Fclose(obuf);
206 goto jleave;
209 n_file_lock(fileno(ibuf), FLT_READ, 0,0, UIZ_MAX); /* TODO ign. lock err*/
210 fseek(ibuf, (long)mailsize, SEEK_SET);
211 while ((c = getc(ibuf)) != EOF) /* xxx bytewise??? TODO ... I/O error? */
212 putc(c, obuf);
213 Fclose(ibuf);
214 ibuf = obuf;
215 fflush_rewind(obuf);
218 printf(_("%s "), n_shexp_quote_cp(displayname, FAL0));
219 fflush(stdout);
221 if ((obuf = Zopen(mailname, "r+")) == NULL) {
222 n_perr(mailname, 0);
223 goto jleave;
226 n_file_lock(fileno(obuf), FLT_WRITE, 0,0, UIZ_MAX); /* TODO ign. lock err! */
227 ftrunc(obuf);
229 srelax_hold();
230 c = 0;
231 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
232 if (mp->m_flag & MDELETED)
233 continue;
234 ++c;
235 if (sendmp(mp, obuf, NULL, NULL, SEND_MBOX, NULL) < 0) {
236 n_perr(mailname, 0);
237 srelax_rele();
238 goto jleave;
240 srelax();
242 srelax_rele();
244 gotcha = (c == 0 && ibuf == NULL);
245 if (ibuf != NULL) {
246 while ((c = getc(ibuf)) != EOF)
247 putc(c, obuf);
249 fflush(obuf);
250 if (ferror(obuf)) {
251 n_perr(mailname, 0);
252 goto jleave;
254 Fclose(obuf);
256 if (gotcha && !ok_blook(keep) && !ok_blook(emptybox)/* TODO obsolete eb*/) {
257 bool_t rms;
259 if ((rms = n_path_rm(mailname)) == TRU1)
260 printf((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
261 ? _("removed\n") : _("removed.\n"));
262 else {
263 int e = errno;
265 printf(_("removal error (ignored)\n"));
266 n_err(_("Error removing %s (ignored):"),
267 n_shexp_quote_cp(mailname, FAL0)); /* TODO */
268 n_perr(NULL, e); /* TODO */
270 } else
271 printf((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
272 ? _("complete\n") : _("updated.\n"));
273 fflush(stdout);
275 rv = TRU1;
276 jleave:
277 if (ibuf != NULL)
278 Fclose(ibuf);
279 if(!rv){
280 /* TODO The codebase aborted by jumping to the main loop here.
281 * TODO The OpenBSD mailx simply ignores this error.
282 * TODO For now we follow the latter unless we are interactive,
283 * TODO in which case we ask the user whether the error is to be
284 * TODO ignored or not. More of this around here in this file! */
285 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
287 j_leave:
288 NYD_LEAVE;
289 return rv;
292 static void
293 _demail(void) /* TODO error handling */
295 NYD2_ENTER;
296 if (ok_blook(keep) || n_path_rm(mailname) <= FAL0) {
297 /* TODO demail(): try use f?truncate(2) instead?! */
298 int fd = open(mailname, (O_WRONLY | O_CREAT | n_O_NOFOLLOW | O_TRUNC),
299 0600);
300 if (fd >= 0)
301 close(fd);
303 NYD2_LEAVE;
306 FL bool_t
307 quit(bool_t hold_sigs_on)
309 int p, modify, anystat, c;
310 FILE *fbuf = NULL, *lckfp = NULL, *rbuf, *abuf;
311 struct message *mp;
312 struct stat minfo;
313 bool_t rv;
314 NYD_ENTER;
316 if(!hold_sigs_on)
317 hold_sigs();
319 rv = FAL0;
320 temporary_localopts_folder_hook_unroll();
322 /* If we are read only, we can't do anything, so just return quickly */
323 /* TODO yet we cannot return quickly if resources have to be released!
324 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
325 *if (mb.mb_perm == 0)
326 * goto jleave;*/
327 p = (mb.mb_perm == 0);
329 switch (mb.mb_type) {
330 case MB_FILE:
331 break;
332 case MB_MAILDIR:
333 rv = maildir_quit(TRU1);
334 goto jleave;
335 #ifdef HAVE_POP3
336 case MB_POP3:
337 rv = pop3_quit(TRU1);
338 goto jleave;
339 #endif
340 case MB_VOID:
341 rv = TRU1;
342 /* FALLTHRU */
343 default:
344 goto jleave;
346 if (p) {
347 rv = TRU1;
348 goto jleave; /* TODO */
351 /* If editing (not reading system mail box), then do the work in edstop() */
352 if (pstate & PS_EDIT) {
353 rv = edstop();
354 goto jleave;
357 /* See if there any messages to save in mbox. If no, we
358 * can save copying mbox to /tmp and back.
360 * Check also to see if any files need to be preserved.
361 * Delete all untouched messages to keep them out of mbox.
362 * If all the messages are to be preserved, just exit with
363 * a message */
364 fbuf = Zopen(mailname, "r+");
365 if (fbuf == NULL) {
366 if (errno != ENOENT)
367 jnewmail:
368 printf(_("Thou hast new mail.\n"));
369 rv = TRU1;
370 goto jleave;
373 if ((lckfp = n_dotlock(mailname, fileno(fbuf), FLT_WRITE, 0,0, UIZ_MAX)
374 ) == NULL) {
375 n_perr(_("Unable to (dot) lock mailbox"), 0);
376 Fclose(fbuf);
377 fbuf = NULL;
378 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
379 goto jleave;
382 rbuf = NULL;
383 if (!fstat(fileno(fbuf), &minfo) && minfo.st_size > mailsize) {
384 printf(_("New mail has arrived.\n"));
385 rbuf = Ftmp(NULL, "quit", OF_RDWR | OF_UNLINK | OF_REGISTER);
386 if (rbuf == NULL || fbuf == NULL)
387 goto jnewmail;
388 #ifdef APPEND
389 fseek(fbuf, (long)mailsize, SEEK_SET);
390 while ((c = getc(fbuf)) != EOF)
391 putc(c, rbuf);
392 #else
393 p = minfo.st_size - mailsize;
394 while (p-- > 0) {
395 c = getc(fbuf);
396 if (c == EOF)
397 goto jnewmail;
398 putc(c, rbuf);
400 #endif
401 fflush_rewind(rbuf);
404 anystat = holdbits();
405 modify = 0;
406 for (c = 0, p = 0, mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
407 if (mp->m_flag & MBOX)
408 c++;
409 if (mp->m_flag & MPRESERVE)
410 p++;
411 if (mp->m_flag & MODIFY)
412 modify++;
414 if (p == msgCount && !modify && !anystat) {
415 rv = TRU1;
416 if (p == 1)
417 printf(_("Held 1 message in %s\n"), displayname);
418 else if (p > 1)
419 printf(_("Held %d messages in %s\n"), p, displayname);
420 goto jleave;
423 if (c == 0) {
424 if (p != 0) {
425 if (writeback(rbuf, fbuf) >= 0)
426 rv = TRU1;
427 else
428 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
429 goto jleave;
431 goto jcream;
434 if (makembox() == STOP) {
435 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
436 goto jleave;
439 /* Now we are ready to copy back preserved files to the system mailbox, if
440 * any were requested */
441 if (p != 0) {
442 if (writeback(rbuf, fbuf) < 0)
443 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
444 goto jleave;
447 /* Finally, remove his file. If new mail has arrived, copy it back */
448 jcream:
449 if (rbuf != NULL) {
450 abuf = fbuf;
451 fseek(abuf, 0L, SEEK_SET);
452 while ((c = getc(rbuf)) != EOF)
453 putc(c, abuf);
454 Fclose(rbuf);
455 ftrunc(abuf);
456 _alter(mailname);
457 rv = TRU1;
458 } else {
459 _demail();
460 rv = TRU1;
462 jleave:
463 if (fbuf != NULL) {
464 Fclose(fbuf);
465 if (lckfp != NULL && lckfp != (FILE*)-1)
466 Pclose(lckfp, FAL0);
468 if(!hold_sigs_on)
469 rele_sigs();
470 NYD_LEAVE;
471 return rv;
474 FL int
475 holdbits(void)
477 struct message *mp;
478 int anystat, autohold, holdbit, nohold;
479 NYD_ENTER;
481 anystat = 0;
482 autohold = ok_blook(hold);
483 holdbit = autohold ? MPRESERVE : MBOX;
484 nohold = MBOX | MSAVED | MDELETED | MPRESERVE;
485 if (ok_blook(keepsave))
486 nohold &= ~MSAVED;
487 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
488 if (mp->m_flag & MNEW) {
489 mp->m_flag &= ~MNEW;
490 mp->m_flag |= MSTATUS;
492 if (mp->m_flag & (MSTATUS | MFLAG | MUNFLAG | MANSWER | MUNANSWER |
493 MDRAFT | MUNDRAFT))
494 ++anystat;
495 if (!(mp->m_flag & MTOUCH))
496 mp->m_flag |= MPRESERVE;
497 if (!(mp->m_flag & nohold))
498 mp->m_flag |= holdbit;
500 NYD_LEAVE;
501 return anystat;
504 FL enum okay
505 makembox(void) /* TODO oh my god */
507 struct message *mp;
508 char *mbox, *tempQuit;
509 int mcount, c;
510 FILE *ibuf = NULL, *obuf, *abuf;
511 enum okay rv = STOP;
512 NYD_ENTER;
514 mbox = _mboxname;
515 mcount = 0;
516 if (!ok_blook(append)) {
517 if ((obuf = Ftmp(&tempQuit, "makembox",
518 OF_WRONLY | OF_HOLDSIGS | OF_REGISTER)) == NULL) {
519 n_perr(_("temporary mail quit file"), 0);
520 goto jleave;
522 if ((ibuf = Fopen(tempQuit, "r")) == NULL)
523 n_perr(tempQuit, 0);
524 Ftmp_release(&tempQuit);
525 if (ibuf == NULL) {
526 Fclose(obuf);
527 goto jleave;
530 if ((abuf = Zopen(mbox, "r")) != NULL) {
531 while ((c = getc(abuf)) != EOF)
532 putc(c, obuf);
533 Fclose(abuf);
535 if (ferror(obuf)) {
536 n_perr(_("temporary mail quit file"), 0);
537 Fclose(ibuf);
538 Fclose(obuf);
539 goto jleave;
541 Fclose(obuf);
543 if ((c = open(mbox, (O_WRONLY | O_CREAT | n_O_NOFOLLOW | O_TRUNC), 0666)
544 ) != -1)
545 close(c);
546 if ((obuf = Zopen(mbox, "r+")) == NULL) {
547 n_perr(mbox, 0);
548 Fclose(ibuf);
549 goto jleave;
551 } else {
552 if ((obuf = Zopen(mbox, "a")) == NULL) {
553 n_perr(mbox, 0);
554 goto jleave;
558 srelax_hold();
559 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
560 if (mp->m_flag & MBOX) {
561 ++mcount;
562 if (sendmp(mp, obuf, saveignore, NULL, SEND_MBOX, NULL) < 0) {
563 n_perr(mbox, 0);
564 srelax_rele();
565 if (ibuf != NULL)
566 Fclose(ibuf);
567 Fclose(obuf);
568 goto jleave;
570 mp->m_flag |= MBOXED;
571 srelax();
574 srelax_rele();
576 /* Copy the user's old mbox contents back to the end of the stuff we just
577 * saved. If we are appending, this is unnecessary */
578 if (!ok_blook(append)) {
579 rewind(ibuf);
580 c = getc(ibuf);
581 while (c != EOF) {
582 putc(c, obuf);
583 if (ferror(obuf))
584 break;
585 c = getc(ibuf);
587 Fclose(ibuf);
588 fflush(obuf);
590 ftrunc(obuf);
591 if (ferror(obuf)) {
592 n_perr(mbox, 0);
593 Fclose(obuf);
594 goto jleave;
596 if (Fclose(obuf) != 0) {
597 n_perr(mbox, 0);
598 goto jleave;
600 if (mcount == 1)
601 printf(_("Saved 1 message in mbox\n"));
602 else
603 printf(_("Saved %d messages in mbox\n"), mcount);
604 rv = OKAY;
605 jleave:
606 NYD_LEAVE;
607 return rv;
610 FL void
611 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
613 char const *cp;
614 NYD_ENTER;
616 if ((cp = expand("&")) == NULL)
617 cp = n_empty;
618 n_strscpy(_mboxname, cp, sizeof _mboxname);
619 NYD_LEAVE;
622 FL int
623 savequitflags(void)
625 enum quitflags qf = 0;
626 size_t i;
627 NYD_ENTER;
629 for (i = 0; i < n_NELEM(_quitnames); ++i)
630 if (n_var_oklook(_quitnames[i].okey) != NULL)
631 qf |= _quitnames[i].flag;
632 NYD_LEAVE;
633 return qf;
636 FL void
637 restorequitflags(int qf)
639 size_t i;
640 NYD_ENTER;
642 for (i = 0; i < n_NELEM(_quitnames); ++i) {
643 char *x = n_var_oklook(_quitnames[i].okey);
644 if (qf & _quitnames[i].flag) {
645 if (x == NULL)
646 n_var_okset(_quitnames[i].okey, TRU1);
647 } else if (x != NULL)
648 n_var_okclear(_quitnames[i].okey);
650 NYD_LEAVE;
653 /* s-it-mode */