Move c_stouch(), c_mboxit() to cmd2.c: they belong
[s-mailx.git] / quit.c
blobf887544c652834b35da5db7ab8f56d8cd3871d24
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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 hold_sigs();
181 for (mp = message, gotcha = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
182 if (mp->m_flag & MNEW) {
183 mp->m_flag &= ~MNEW;
184 mp->m_flag |= MSTATUS;
186 if (mp->m_flag & (MODIFY | MDELETED | MSTATUS | MFLAG | MUNFLAG |
187 MANSWER | MUNANSWER | MDRAFT | MUNDRAFT))
188 ++gotcha;
190 if (!gotcha)
191 goto jleave;
193 rv = FAL0;
195 /* TODO This is too simple minded? We should regenerate an index file
196 * TODO to be able to truly tell wether *anything* has changed!
197 * TODO (Or better: only come here.. then! It is an *object method!* */
198 /* TODO Ignoring stat error is easy, huh? */
199 if (!stat(mailname, &statb) && statb.st_size > mailsize) {
200 if ((obuf = Ftmp(NULL, "edstop", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
201 NULL) {
202 n_perr(_("tmpfile"), 0);
203 goto jleave;
205 if ((ibuf = Zopen(mailname, "r")) == NULL) {
206 n_perr(mailname, 0);
207 Fclose(obuf);
208 goto jleave;
211 n_file_lock(fileno(ibuf), FLT_READ, 0,0, UIZ_MAX); /* TODO ign. lock err*/
212 fseek(ibuf, (long)mailsize, SEEK_SET);
213 while ((c = getc(ibuf)) != EOF) /* xxx bytewise??? TODO ... I/O error? */
214 putc(c, obuf);
215 Fclose(ibuf);
216 ibuf = obuf;
217 fflush_rewind(obuf);
220 printf(_("\"%s\" "), displayname);
221 fflush(stdout);
223 if ((obuf = Zopen(mailname, "r+")) == NULL) {
224 n_perr(mailname, 0);
225 goto jleave;
228 n_file_lock(fileno(obuf), FLT_WRITE, 0,0, UIZ_MAX); /* TODO ign. lock err! */
229 ftrunc(obuf);
231 srelax_hold();
232 c = 0;
233 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
234 if (mp->m_flag & MDELETED)
235 continue;
236 ++c;
237 if (sendmp(mp, obuf, NULL, NULL, SEND_MBOX, NULL) < 0) {
238 n_perr(mailname, 0);
239 srelax_rele();
240 goto jleave;
242 srelax();
244 srelax_rele();
246 gotcha = (c == 0 && ibuf == NULL);
247 if (ibuf != NULL) {
248 while ((c = getc(ibuf)) != EOF)
249 putc(c, obuf);
251 fflush(obuf);
252 if (ferror(obuf)) {
253 n_perr(mailname, 0);
254 goto jleave;
256 Fclose(obuf);
258 if (gotcha && !ok_blook(keep) && !ok_blook(emptybox)/* TODO obsolete eb*/) {
259 bool_t rms;
261 if ((rms = n_path_rm(mailname)) == TRU1)
262 printf((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
263 ? _("removed\n") : _("removed.\n"));
264 else {
265 int e = errno;
266 printf(_("removal error (ignored)\n"));
267 n_err(_("Error removing \"%s\" (ignored):"), mailname); /* 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 rele_sigs();
281 if(!rv){
282 /* TODO The codebase aborted by jumping to the main loop here.
283 * TODO The OpenBSD mailx simply ignores this error.
284 * TODO For now we follow the latter unless we are interactive,
285 * TODO in which case we ask the user wether the error is to be
286 * TODO ignored or not. More of this around here in this file! */
287 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
289 j_leave:
290 NYD_LEAVE;
291 return rv;
294 static void
295 _demail(void) /* TODO error handling */
297 NYD2_ENTER;
298 if (ok_blook(keep) || n_path_rm(mailname) <= FAL0) {
299 /* TODO demail(): try use f?truncate(2) instead?! */
300 int fd = open(mailname, (O_WRONLY | O_CREAT | n_O_NOFOLLOW | O_TRUNC),
301 0600);
302 if (fd >= 0)
303 close(fd);
305 NYD2_LEAVE;
308 FL bool_t
309 quit(void)
311 int p, modify, anystat, c;
312 FILE *fbuf = NULL, *lckfp = NULL, *rbuf, *abuf;
313 struct message *mp;
314 struct stat minfo;
315 bool_t rv;
316 NYD_ENTER;
318 rv = FAL0;
319 temporary_localopts_folder_hook_unroll();
321 /* If we are read only, we can't do anything, so just return quickly */
322 /* TODO yet we cannot return quickly if resources have to be released!
323 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
324 *if (mb.mb_perm == 0)
325 * goto jleave;*/
326 p = (mb.mb_perm == 0);
328 /* TODO lex.c:setfile() has just called hold_sigs(); before it called
329 * TODO us, but this causes uninterruptible hangs due to blocked sigs
330 * TODO anywhere except for MB_FILE (all others install their own
331 * TODO handlers, as it seems, properly); marked YYY */
332 switch (mb.mb_type) {
333 case MB_FILE:
334 break;
335 case MB_MAILDIR:
336 rele_sigs(); /* YYY */
337 rv = maildir_quit();
338 hold_sigs(); /* YYY */
339 goto jleave;
340 #ifdef HAVE_POP3
341 case MB_POP3:
342 rele_sigs(); /* YYY */
343 rv = pop3_quit();
344 hold_sigs(); /* YYY */
345 goto jleave;
346 #endif
347 case MB_VOID:
348 default:
349 goto jleave;
351 if (p) {
352 rv = TRU1;
353 goto jleave; /* TODO */
356 /* If editing (not reading system mail box), then do the work in edstop() */
357 if (pstate & PS_EDIT) {
358 rv = edstop();
359 goto jleave;
362 /* See if there any messages to save in mbox. If no, we
363 * can save copying mbox to /tmp and back.
365 * Check also to see if any files need to be preserved.
366 * Delete all untouched messages to keep them out of mbox.
367 * If all the messages are to be preserved, just exit with
368 * a message */
369 fbuf = Zopen(mailname, "r+");
370 if (fbuf == NULL) {
371 if (errno != ENOENT)
372 jnewmail:
373 printf(_("Thou hast new mail.\n"));
374 rv = TRU1;
375 goto jleave;
378 if ((lckfp = n_dotlock(mailname, fileno(fbuf), FLT_WRITE, 0,0, UIZ_MAX)
379 ) == NULL) {
380 n_perr(_("Unable to (dot) lock mailbox"), 0);
381 Fclose(fbuf);
382 fbuf = NULL;
383 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
384 goto jleave;
387 rbuf = NULL;
388 if (!fstat(fileno(fbuf), &minfo) && minfo.st_size > mailsize) {
389 printf(_("New mail has arrived.\n"));
390 rbuf = Ftmp(NULL, "quit", OF_RDWR | OF_UNLINK | OF_REGISTER);
391 if (rbuf == NULL || fbuf == NULL)
392 goto jnewmail;
393 #ifdef APPEND
394 fseek(fbuf, (long)mailsize, SEEK_SET);
395 while ((c = getc(fbuf)) != EOF)
396 putc(c, rbuf);
397 #else
398 p = minfo.st_size - mailsize;
399 while (p-- > 0) {
400 c = getc(fbuf);
401 if (c == EOF)
402 goto jnewmail;
403 putc(c, rbuf);
405 #endif
406 fflush_rewind(rbuf);
409 anystat = holdbits();
410 modify = 0;
411 for (c = 0, p = 0, mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
412 if (mp->m_flag & MBOX)
413 c++;
414 if (mp->m_flag & MPRESERVE)
415 p++;
416 if (mp->m_flag & MODIFY)
417 modify++;
419 if (p == msgCount && !modify && !anystat) {
420 rv = TRU1;
421 if (p == 1)
422 printf(_("Held 1 message in %s\n"), displayname);
423 else if (p > 1)
424 printf(_("Held %d messages in %s\n"), p, displayname);
425 goto jleave;
428 if (c == 0) {
429 if (p != 0) {
430 if (writeback(rbuf, fbuf) >= 0)
431 rv = TRU1;
432 else
433 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
434 goto jleave;
436 goto jcream;
439 if (makembox() == STOP) {
440 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
441 goto jleave;
444 /* Now we are ready to copy back preserved files to the system mailbox, if
445 * any were requested */
446 if (p != 0) {
447 if (writeback(rbuf, fbuf) < 0)
448 rv = getapproval(_("Continue, possibly loosing changes"), TRU1);
449 goto jleave;
452 /* Finally, remove his file. If new mail has arrived, copy it back */
453 jcream:
454 if (rbuf != NULL) {
455 abuf = fbuf;
456 fseek(abuf, 0L, SEEK_SET);
457 while ((c = getc(rbuf)) != EOF)
458 putc(c, abuf);
459 Fclose(rbuf);
460 ftrunc(abuf);
461 _alter(mailname);
462 rv = TRU1;
463 } else {
464 _demail();
465 rv = TRU1;
467 jleave:
468 if (fbuf != NULL) {
469 Fclose(fbuf);
470 if (lckfp != NULL && lckfp != (FILE*)-1)
471 Pclose(lckfp, FAL0);
473 NYD_LEAVE;
474 return rv;
477 FL int
478 holdbits(void)
480 struct message *mp;
481 int anystat, autohold, holdbit, nohold;
482 NYD_ENTER;
484 anystat = 0;
485 autohold = ok_blook(hold);
486 holdbit = autohold ? MPRESERVE : MBOX;
487 nohold = MBOX | MSAVED | MDELETED | MPRESERVE;
488 if (ok_blook(keepsave))
489 nohold &= ~MSAVED;
490 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
491 if (mp->m_flag & MNEW) {
492 mp->m_flag &= ~MNEW;
493 mp->m_flag |= MSTATUS;
495 if (mp->m_flag & (MSTATUS | MFLAG | MUNFLAG | MANSWER | MUNANSWER |
496 MDRAFT | MUNDRAFT))
497 ++anystat;
498 if (!(mp->m_flag & MTOUCH))
499 mp->m_flag |= MPRESERVE;
500 if (!(mp->m_flag & nohold))
501 mp->m_flag |= holdbit;
503 NYD_LEAVE;
504 return anystat;
507 FL enum okay
508 makembox(void) /* TODO oh my god */
510 struct message *mp;
511 char *mbox, *tempQuit;
512 int mcount, c;
513 FILE *ibuf = NULL, *obuf, *abuf;
514 enum okay rv = STOP;
515 NYD_ENTER;
517 mbox = _mboxname;
518 mcount = 0;
519 if (!ok_blook(append)) {
520 if ((obuf = Ftmp(&tempQuit, "makembox",
521 OF_WRONLY | OF_HOLDSIGS | OF_REGISTER)) == NULL) {
522 n_perr(_("temporary mail quit file"), 0);
523 goto jleave;
525 if ((ibuf = Fopen(tempQuit, "r")) == NULL)
526 n_perr(tempQuit, 0);
527 Ftmp_release(&tempQuit);
528 if (ibuf == NULL) {
529 Fclose(obuf);
530 goto jleave;
533 if ((abuf = Zopen(mbox, "r")) != NULL) {
534 while ((c = getc(abuf)) != EOF)
535 putc(c, obuf);
536 Fclose(abuf);
538 if (ferror(obuf)) {
539 n_perr(_("temporary mail quit file"), 0);
540 Fclose(ibuf);
541 Fclose(obuf);
542 goto jleave;
544 Fclose(obuf);
546 if ((c = open(mbox, (O_WRONLY | O_CREAT | n_O_NOFOLLOW | O_TRUNC), 0666)
547 ) != -1)
548 close(c);
549 if ((obuf = Zopen(mbox, "r+")) == NULL) {
550 n_perr(mbox, 0);
551 Fclose(ibuf);
552 goto jleave;
554 } else {
555 if ((obuf = Zopen(mbox, "a")) == NULL) {
556 n_perr(mbox, 0);
557 goto jleave;
561 srelax_hold();
562 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
563 if (mp->m_flag & MBOX) {
564 ++mcount;
565 if (sendmp(mp, obuf, saveignore, NULL, SEND_MBOX, NULL) < 0) {
566 n_perr(mbox, 0);
567 srelax_rele();
568 if (ibuf != NULL)
569 Fclose(ibuf);
570 Fclose(obuf);
571 goto jleave;
573 mp->m_flag |= MBOXED;
574 srelax();
577 srelax_rele();
579 /* Copy the user's old mbox contents back to the end of the stuff we just
580 * saved. If we are appending, this is unnecessary */
581 if (!ok_blook(append)) {
582 rewind(ibuf);
583 c = getc(ibuf);
584 while (c != EOF) {
585 putc(c, obuf);
586 if (ferror(obuf))
587 break;
588 c = getc(ibuf);
590 Fclose(ibuf);
591 fflush(obuf);
593 ftrunc(obuf);
594 if (ferror(obuf)) {
595 n_perr(mbox, 0);
596 Fclose(obuf);
597 goto jleave;
599 if (Fclose(obuf) != 0) {
600 n_perr(mbox, 0);
601 goto jleave;
603 if (mcount == 1)
604 printf(_("Saved 1 message in mbox\n"));
605 else
606 printf(_("Saved %d messages in mbox\n"), mcount);
607 rv = OKAY;
608 jleave:
609 NYD_LEAVE;
610 return rv;
613 FL void
614 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
616 char const *cp;
617 NYD_ENTER;
619 if ((cp = expand("&")) == NULL)
620 cp = "";
621 n_strscpy(_mboxname, cp, sizeof _mboxname);
622 NYD_LEAVE;
625 FL int
626 savequitflags(void)
628 enum quitflags qf = 0;
629 size_t i;
630 NYD_ENTER;
632 for (i = 0; i < NELEM(_quitnames); ++i)
633 if (n_var_oklook(_quitnames[i].okey) != NULL)
634 qf |= _quitnames[i].flag;
635 NYD_LEAVE;
636 return qf;
639 FL void
640 restorequitflags(int qf)
642 size_t i;
643 NYD_ENTER;
645 for (i = 0; i < NELEM(_quitnames); ++i) {
646 char *x = n_var_oklook(_quitnames[i].okey);
647 if (qf & _quitnames[i].flag) {
648 if (x == NULL)
649 n_var_okset(_quitnames[i].okey, TRU1);
650 } else if (x != NULL)
651 n_var_okclear(_quitnames[i].okey);
653 NYD_LEAVE;
656 /* s-it-mode */