Move socket code to new socket.c
[s-mailx.git] / quit.c
blobc8c79fd589ae93395844b2a44b4d361a70a123df
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 void 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)
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 void
165 edstop(void) /* TODO oh my god - and REMOVE that CRAPPY reset(0) jump!! */
167 int gotcha, c;
168 struct message *mp;
169 FILE *obuf = NULL, *ibuf = NULL;
170 struct stat statb;
171 bool_t doreset;
172 NYD_ENTER;
174 hold_sigs();
175 doreset = FAL0;
177 if (mb.mb_perm == 0)
178 goto jleave;
180 for (mp = message, gotcha = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
181 if (mp->m_flag & MNEW) {
182 mp->m_flag &= ~MNEW;
183 mp->m_flag |= MSTATUS;
185 if (mp->m_flag & (MODIFY | MDELETED | MSTATUS | MFLAG | MUNFLAG |
186 MANSWER | MUNANSWER | MDRAFT | MUNDRAFT))
187 ++gotcha;
189 if (!gotcha)
190 goto jleave;
192 doreset = TRU1;
194 /* TODO This is too simple minded? We should regenerate an index file
195 * TODO to be able to truly tell wether *anything* has changed!
196 * TODO (Or better: only come here.. then! It is an *object method!* */
197 /* TODO Ignoring stat error is easy, huh? */
198 if (!stat(mailname, &statb) && statb.st_size > mailsize) {
199 if ((obuf = Ftmp(NULL, "edstop", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
200 NULL) {
201 n_perr(_("tmpfile"), 0);
202 goto jleave;
204 if ((ibuf = Zopen(mailname, "r")) == NULL) {
205 n_perr(mailname, 0);
206 Fclose(obuf);
207 goto jleave;
210 n_file_lock(fileno(ibuf), FLT_READ, 0,0, 1); /* TODO ign. lock error! */
211 fseek(ibuf, (long)mailsize, SEEK_SET);
212 while ((c = getc(ibuf)) != EOF) /* xxx bytewise??? TODO ... I/O error? */
213 putc(c, obuf);
214 Fclose(ibuf);
215 ibuf = obuf;
216 fflush_rewind(obuf);
219 printf(_("\"%s\" "), displayname);
220 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, 1); /* TODO ignoring lock error! */
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 doreset = FAL0;
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 printf(_("removal error\n"));
266 n_err(_("could not remove file \"%s\"\n")); /* XXX error cause */
268 } else
269 printf((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
270 ? _("complete\n") : _("updated.\n"));
271 fflush(stdout);
272 jleave:
273 if (ibuf != NULL)
274 Fclose(ibuf);
275 rele_sigs();
276 NYD_LEAVE;
277 if (doreset)
278 reset(0);
281 static void
282 _demail(void)
284 NYD2_ENTER;
285 if (ok_blook(keep) || n_path_rm(mailname) <= FAL0) {
286 /* TODO demail(): try use f?truncate(2) instead?! */
287 int fd = open(mailname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
288 if (fd >= 0)
289 close(fd);
291 NYD2_LEAVE;
294 FL void
295 quit(void)
297 int p, modify, anystat, c;
298 FILE *fbuf = NULL, *lckfp = NULL, *rbuf, *abuf;
299 struct message *mp;
300 struct stat minfo;
301 NYD_ENTER;
303 temporary_localopts_folder_hook_unroll();
305 /* If we are read only, we can't do anything, so just return quickly */
306 /* TODO yet we cannot return quickly if resources have to be released!
307 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
308 *if (mb.mb_perm == 0)
309 * goto jleave;*/
310 p = (mb.mb_perm == 0);
312 /* TODO lex.c:setfile() has just called hold_sigs(); before it called
313 * TODO us, but this causes uninterruptible hangs due to blocked sigs
314 * TODO anywhere except for MB_FILE (all others install their own
315 * TODO handlers, as it seems, properly); marked YYY */
316 switch (mb.mb_type) {
317 case MB_FILE:
318 break;
319 case MB_MAILDIR:
320 rele_sigs(); /* YYY */
321 maildir_quit();
322 hold_sigs(); /* YYY */
323 goto jleave;
324 #ifdef HAVE_POP3
325 case MB_POP3:
326 rele_sigs(); /* YYY */
327 pop3_quit();
328 hold_sigs(); /* YYY */
329 goto jleave;
330 #endif
331 case MB_VOID:
332 default:
333 goto jleave;
335 if (p) goto jleave; /* TODO */
337 /* If editing (not reading system mail box), then do the work in edstop() */
338 if (pstate & PS_EDIT) {
339 edstop();
340 goto jleave;
343 /* See if there any messages to save in mbox. If no, we
344 * can save copying mbox to /tmp and back.
346 * Check also to see if any files need to be preserved.
347 * Delete all untouched messages to keep them out of mbox.
348 * If all the messages are to be preserved, just exit with
349 * a message */
350 fbuf = Zopen(mailname, "r+");
351 if (fbuf == NULL) {
352 if (errno == ENOENT)
353 goto jleave;
354 jnewmail:
355 printf(_("Thou hast new mail.\n"));
356 goto jleave;
359 if ((lckfp = n_dotlock(mailname, fileno(fbuf), FLT_WRITE, 0,0, 1)) == NULL) {
360 n_perr(_("Unable to (dot) lock mailbox, aborting operation"), 0);
361 Fclose(fbuf);
362 fbuf = NULL;
363 goto jleave;
366 rbuf = NULL;
367 if (!fstat(fileno(fbuf), &minfo) && minfo.st_size > mailsize) {
368 printf(_("New mail has arrived.\n"));
369 rbuf = Ftmp(NULL, "quit", OF_RDWR | OF_UNLINK | OF_REGISTER);
370 if (rbuf == NULL || fbuf == NULL)
371 goto jnewmail;
372 #ifdef APPEND
373 fseek(fbuf, (long)mailsize, SEEK_SET);
374 while ((c = getc(fbuf)) != EOF)
375 putc(c, rbuf);
376 #else
377 p = minfo.st_size - mailsize;
378 while (p-- > 0) {
379 c = getc(fbuf);
380 if (c == EOF)
381 goto jnewmail;
382 putc(c, rbuf);
384 #endif
385 fflush_rewind(rbuf);
388 anystat = holdbits();
389 modify = 0;
390 for (c = 0, p = 0, mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
391 if (mp->m_flag & MBOX)
392 c++;
393 if (mp->m_flag & MPRESERVE)
394 p++;
395 if (mp->m_flag & MODIFY)
396 modify++;
398 if (p == msgCount && !modify && !anystat) {
399 if (p == 1)
400 printf(_("Held 1 message in %s\n"), displayname);
401 else if (p > 1)
402 printf(_("Held %d messages in %s\n"), p, displayname);
403 goto jleave;
405 if (c == 0) {
406 if (p != 0) {
407 writeback(rbuf, fbuf);
408 goto jleave;
410 goto jcream;
413 if (makembox() == STOP)
414 goto jleave;
416 /* Now we are ready to copy back preserved files to the system mailbox, if
417 * any were requested */
418 if (p != 0) {
419 writeback(rbuf, fbuf);
420 goto jleave;
423 /* Finally, remove his file. If new mail has arrived, copy it back */
424 jcream:
425 if (rbuf != NULL) {
426 abuf = fbuf;
427 fseek(abuf, 0L, SEEK_SET);
428 while ((c = getc(rbuf)) != EOF)
429 putc(c, abuf);
430 Fclose(rbuf);
431 ftrunc(abuf);
432 _alter(mailname);
433 goto jleave;
435 _demail();
436 jleave:
437 if (fbuf != NULL) {
438 Fclose(fbuf);
439 if (lckfp != NULL && lckfp != (FILE*)-1)
440 Pclose(lckfp, FAL0);
442 NYD_LEAVE;
445 FL int
446 holdbits(void)
448 struct message *mp;
449 int anystat, autohold, holdbit, nohold;
450 NYD_ENTER;
452 anystat = 0;
453 autohold = ok_blook(hold);
454 holdbit = autohold ? MPRESERVE : MBOX;
455 nohold = MBOX | MSAVED | MDELETED | MPRESERVE;
456 if (ok_blook(keepsave))
457 nohold &= ~MSAVED;
458 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
459 if (mp->m_flag & MNEW) {
460 mp->m_flag &= ~MNEW;
461 mp->m_flag |= MSTATUS;
463 if (mp->m_flag & (MSTATUS | MFLAG | MUNFLAG | MANSWER | MUNANSWER |
464 MDRAFT | MUNDRAFT))
465 ++anystat;
466 if (!(mp->m_flag & MTOUCH))
467 mp->m_flag |= MPRESERVE;
468 if (!(mp->m_flag & nohold))
469 mp->m_flag |= holdbit;
471 NYD_LEAVE;
472 return anystat;
475 FL enum okay
476 makembox(void) /* TODO oh my god */
478 struct message *mp;
479 char *mbox, *tempQuit;
480 int mcount, c;
481 FILE *ibuf = NULL, *obuf, *abuf;
482 enum okay rv = STOP;
483 NYD_ENTER;
485 mbox = _mboxname;
486 mcount = 0;
487 if (!ok_blook(append)) {
488 if ((obuf = Ftmp(&tempQuit, "makembox",
489 OF_WRONLY | OF_HOLDSIGS | OF_REGISTER)) == NULL) {
490 n_perr(_("temporary mail quit file"), 0);
491 goto jleave;
493 if ((ibuf = Fopen(tempQuit, "r")) == NULL)
494 n_perr(tempQuit, 0);
495 Ftmp_release(&tempQuit);
496 if (ibuf == NULL) {
497 Fclose(obuf);
498 goto jleave;
501 if ((abuf = Zopen(mbox, "r")) != NULL) {
502 while ((c = getc(abuf)) != EOF)
503 putc(c, obuf);
504 Fclose(abuf);
506 if (ferror(obuf)) {
507 n_perr(_("temporary mail quit file"), 0);
508 Fclose(ibuf);
509 Fclose(obuf);
510 goto jleave;
512 Fclose(obuf);
514 if ((c = open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600)) != -1)
515 close(c);
516 if ((obuf = Zopen(mbox, "r+")) == NULL) {
517 n_perr(mbox, 0);
518 Fclose(ibuf);
519 goto jleave;
521 } else {
522 if ((obuf = Zopen(mbox, "a")) == NULL) {
523 n_perr(mbox, 0);
524 goto jleave;
526 fchmod(fileno(obuf), 0600);
529 srelax_hold();
530 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
531 if (mp->m_flag & MBOX) {
532 ++mcount;
533 if (sendmp(mp, obuf, saveignore, NULL, SEND_MBOX, NULL) < 0) {
534 n_perr(mbox, 0);
535 srelax_rele();
536 if (ibuf != NULL)
537 Fclose(ibuf);
538 Fclose(obuf);
539 goto jleave;
541 mp->m_flag |= MBOXED;
542 srelax();
545 srelax_rele();
547 /* Copy the user's old mbox contents back to the end of the stuff we just
548 * saved. If we are appending, this is unnecessary */
549 if (!ok_blook(append)) {
550 rewind(ibuf);
551 c = getc(ibuf);
552 while (c != EOF) {
553 putc(c, obuf);
554 if (ferror(obuf))
555 break;
556 c = getc(ibuf);
558 Fclose(ibuf);
559 fflush(obuf);
561 ftrunc(obuf);
562 if (ferror(obuf)) {
563 n_perr(mbox, 0);
564 Fclose(obuf);
565 goto jleave;
567 if (Fclose(obuf) != 0) {
568 n_perr(mbox, 0);
569 goto jleave;
571 if (mcount == 1)
572 printf(_("Saved 1 message in mbox\n"));
573 else
574 printf(_("Saved %d messages in mbox\n"), mcount);
575 rv = OKAY;
576 jleave:
577 NYD_LEAVE;
578 return rv;
581 FL void
582 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
584 char const *cp;
585 NYD_ENTER;
587 if ((cp = expand("&")) == NULL)
588 cp = "";
589 n_strscpy(_mboxname, cp, sizeof _mboxname);
590 NYD_LEAVE;
593 FL int
594 savequitflags(void)
596 enum quitflags qf = 0;
597 size_t i;
598 NYD_ENTER;
600 for (i = 0; i < NELEM(_quitnames); ++i)
601 if (_var_oklook(_quitnames[i].okey) != NULL)
602 qf |= _quitnames[i].flag;
603 NYD_LEAVE;
604 return qf;
607 FL void
608 restorequitflags(int qf)
610 size_t i;
611 NYD_ENTER;
613 for (i = 0; i < NELEM(_quitnames); ++i) {
614 char *x = _var_oklook(_quitnames[i].okey);
615 if (qf & _quitnames[i].flag) {
616 if (x == NULL)
617 _var_okset(_quitnames[i].okey, TRU1);
618 } else if (x != NULL)
619 _var_okclear(_quitnames[i].okey);
621 NYD_LEAVE;
624 /* s-it-mode */