1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Termination processing. TODO MBOX -> VFS; error handling: catastrophe!
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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
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
38 #ifndef HAVE_AMALGAMATION
47 QUITFLAG_KEEPSAVE
= 1<<2,
48 QUITFLAG_APPEND
= 1<<3
56 static struct quitnames
const _quitnames
[] = {
57 {QUITFLAG_HOLD
, ok_b_hold
},
58 {QUITFLAG_KEEP
, ok_b_keep
},
59 {QUITFLAG_KEEPSAVE
, ok_b_keepsave
},
60 {QUITFLAG_APPEND
, ok_b_append
}
63 static char _mboxname
[PATH_MAX
]; /* Name of mbox */
65 /* Touch the indicated file */
66 static void _alter(char const *name
);
68 /* Preserve all the appropriate messages back in the system mailbox, and print
69 * a nice message indicated how many were saved. On any error, just return -1.
70 * Else return 0. Incorporate the any new mail that we found */
71 static int writeback(FILE *res
, FILE *obuf
);
73 /* Terminate an editing session by attempting to write out the user's file from
74 * the temporary. Save any new stuff appended to the file */
75 static bool_t
edstop(void);
78 _alter(char const *name
) /* TODO error handling */
81 struct timespec tsa
[2];
86 struct n_timespec
const *tsp
;
89 tsp
= n_time_now(TRU1
); /* TODO -> eventloop */
92 tsa
[0].tv_sec
= tsp
->ts_sec
+ 1;
93 tsa
[0].tv_nsec
= tsp
->ts_nsec
;
94 tsa
[1].tv_nsec
= UTIME_OMIT
;
95 utimensat(AT_FDCWD
, name
, tsa
, 0);
97 if (!stat(name
, &sb
)) {
98 utb
.actime
= tsp
->ts_sec
;
99 utb
.modtime
= sb
.st_mtime
;
107 writeback(FILE *res
, FILE *obuf
) /* TODO errors */
113 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
117 for (p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
118 if ((mp
->m_flag
& MPRESERVE
) || !(mp
->m_flag
& MTOUCH
)) {
120 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
132 for(lastnl
= FAL0
; (c
= getc(res
)) != EOF
&& putc(c
, obuf
) != EOF
;)
133 lastnl
= (c
== '\n') ? (lastnl
? TRU2
: TRU1
) : FAL0
;
142 fseek(obuf
, 0L, SEEK_SET
);
145 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
150 fprintf(n_stdout
, _("Held 1 message in %s\n"), displayname
);
152 fprintf(n_stdout
, _("Held %d messages in %s\n"), p
, displayname
);
160 edstop(void) /* TODO oh my god */
164 FILE *obuf
= NULL
, *ibuf
= NULL
;
166 enum n_fopen_state fs
;
175 for (mp
= message
, gotcha
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
176 if (mp
->m_flag
& MNEW
) {
178 mp
->m_flag
|= MSTATUS
;
180 if (mp
->m_flag
& (MODIFY
| MDELETED
| MSTATUS
| MFLAG
| MUNFLAG
|
181 MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
))
189 /* TODO This is too simple minded? We should regenerate an index file
190 * TODO to be able to truly tell whether *anything* has changed!
191 * TODO (Or better: only come here.. then! It is an *object method!* */
192 /* TODO Ignoring stat error is easy, huh? */
193 if (!stat(mailname
, &statb
) && statb
.st_size
> mailsize
) {
194 if ((obuf
= Ftmp(NULL
, "edstop", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
196 n_perr(_("tmpfile"), 0);
199 if ((ibuf
= n_fopen_any(mailname
, "r", NULL
)) == NULL
) {
204 n_file_lock(fileno(ibuf
), FLT_READ
, 0,0, UIZ_MAX
); /* TODO ign. lock err*/
205 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
206 while ((c
= getc(ibuf
)) != EOF
) /* xxx bytewise??? TODO ... I/O error? */
214 fprintf(n_stdout
, _("%s "), n_shexp_quote_cp(displayname
, FAL0
));
217 if ((obuf
= n_fopen_any(mailname
, "r+", &fs
)) == NULL
) {
219 n_perr(n_shexp_quote_cp(mailname
, FAL0
), e
);
222 n_file_lock(fileno(obuf
), FLT_WRITE
, 0,0, UIZ_MAX
); /* TODO ign. lock err! */
227 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
228 if (mp
->m_flag
& MDELETED
)
231 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
233 n_err(_("Failed to finalize %s\n"), n_shexp_quote_cp(mailname
, FAL0
));
240 gotcha
= (c
== 0 && ibuf
== NULL
);
244 for(lastnl
= FAL0
; (c
= getc(ibuf
)) != EOF
&& putc(c
, obuf
) != EOF
;)
245 lastnl
= (c
== '\n') ? (lastnl
? TRU2
: TRU1
) : FAL0
;
246 if(lastnl
!= TRU2
&& (fs
& n_PROTO_MASK
) == n_PROTO_FILE
)
249 /* May nonetheless be a broken MBOX TODO really: VFS, object KNOWS!! */
250 else if(!gotcha
&& (fs
& n_PROTO_MASK
) == n_PROTO_FILE
)
251 n_folder_mbox_prepare_append(obuf
, NULL
);
254 n_err(_("Failed to finalize %s\n"), n_shexp_quote_cp(mailname
, FAL0
));
259 /* Non-system boxes are never removed except forced via POSIX mode */
260 #ifdef HAVE_FTRUNCATE
261 ftruncate(fileno(obuf
), 0);
265 if((fd
= open(mailname
, (O_WRONLY
| O_CREAT
| n_O_NOXY_BITS
| O_TRUNC
),
270 if(ok_blook(posix
) && !ok_blook(keep
) && n_path_rm(mailname
))
271 fputs(_("removed\n"), n_stdout
);
273 fputs(_("truncated\n"), n_stdout
);
275 fputs((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
276 ? _("complete\n") : _("updated.\n"), n_stdout
);
286 /* TODO The codebase aborted by jumping to the main loop here.
287 * TODO The OpenBSD mailx simply ignores this error.
288 * TODO For now we follow the latter unless we are interactive,
289 * TODO in which case we ask the user whether the error is to be
290 * TODO ignored or not. More of this around here in this file! */
291 rv
= getapproval(_("Continue, possibly loosing changes"), TRU1
);
299 quit(bool_t hold_sigs_on
)
301 int p
, modify
, anystat
, c
;
302 FILE *fbuf
, *lckfp
, *rbuf
, *abuf
;
312 fbuf
= lckfp
= rbuf
= NULL
;
313 temporary_folder_hook_unroll();
315 /* If we are read only, we can't do anything, so just return quickly */
316 /* TODO yet we cannot return quickly if resources have to be released!
317 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
318 *if (mb.mb_perm == 0)
320 p
= (mb
.mb_perm
== 0);
322 switch (mb
.mb_type
) {
326 rv
= maildir_quit(TRU1
);
330 rv
= pop3_quit(TRU1
);
336 rv
= imap_quit(TRU1
);
347 goto jleave
; /* TODO */
350 /* If editing (not reading system mail box), then do the work in edstop() */
351 if (n_pstate
& n_PS_EDIT
) {
356 /* See if there any messages to save in mbox. If no, we
357 * can save copying mbox to /tmp and back.
359 * Check also to see if any files need to be preserved.
360 * Delete all untouched messages to keep them out of mbox.
361 * If all the messages are to be preserved, just exit with
363 fbuf
= n_fopen_any(mailname
, "r+", NULL
);
365 if (n_err_no
!= n_ERR_NOENT
)
367 fprintf(n_stdout
, _("Thou hast new mail.\n"));
372 if ((lckfp
= n_dotlock(mailname
, fileno(fbuf
), FLT_WRITE
, 0,0, UIZ_MAX
)
374 n_perr(_("Unable to (dot) lock mailbox"), 0);
377 rv
= getapproval(_("Continue, possibly loosing changes"), TRU1
);
382 if (!fstat(fileno(fbuf
), &minfo
) && minfo
.st_size
> mailsize
) {
385 fprintf(n_stdout
, _("New mail has arrived.\n"));
386 rbuf
= Ftmp(NULL
, "quit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
);
387 if (rbuf
== NULL
|| fbuf
== NULL
)
389 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
390 for(lastnl
= FAL0
; (c
= getc(fbuf
)) != EOF
&& putc(c
, rbuf
) != EOF
;)
391 lastnl
= (c
== '\n') ? (lastnl
? TRU2
: TRU1
) : FAL0
;
397 anystat
= holdbits();
399 for (c
= 0, p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
400 if (mp
->m_flag
& MBOX
)
402 if (mp
->m_flag
& MPRESERVE
)
404 if (mp
->m_flag
& MODIFY
)
407 if (p
== msgCount
&& !modify
&& !anystat
) {
410 fprintf(n_stdout
, _("Held 1 message in %s\n"), displayname
);
412 fprintf(n_stdout
, _("Held %d messages in %s\n"), p
, displayname
);
418 if (writeback(rbuf
, fbuf
) >= 0)
421 rv
= getapproval(_("Continue, possibly loosing changes"), TRU1
);
427 if (makembox() == STOP
) {
428 rv
= getapproval(_("Continue, possibly loosing changes"), TRU1
);
432 /* Now we are ready to copy back preserved files to the system mailbox, if
433 * any were requested */
435 if (writeback(rbuf
, fbuf
) < 0)
436 rv
= getapproval(_("Continue, possibly loosing changes"), TRU1
);
440 /* Finally, remove his file. If new mail has arrived, copy it back */
444 fseek(abuf
, 0L, SEEK_SET
);
445 while ((c
= getc(rbuf
)) != EOF
)
451 #ifdef HAVE_FTRUNCATE
452 ftruncate(fileno(fbuf
), 0);
456 if((fd
= open(mailname
, (O_WRONLY
| O_CREAT
| n_O_NOXY_BITS
| O_TRUNC
),
469 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
483 int anystat
, autohold
, holdbit
, nohold
;
487 autohold
= ok_blook(hold
);
488 holdbit
= autohold
? MPRESERVE
: MBOX
;
489 nohold
= MBOX
| MSAVED
| MDELETED
| MPRESERVE
;
490 if (ok_blook(keepsave
))
492 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
493 if (mp
->m_flag
& MNEW
) {
495 mp
->m_flag
|= MSTATUS
;
497 if (mp
->m_flag
& (MSTATUS
| MFLAG
| MUNFLAG
| MANSWER
| MUNANSWER
|
500 if (!(mp
->m_flag
& MTOUCH
))
501 mp
->m_flag
|= MPRESERVE
;
502 if (!(mp
->m_flag
& nohold
))
503 mp
->m_flag
|= holdbit
;
510 makembox(void) /* TODO oh my god (also error reporting) */
513 char *mbox
, *tempQuit
;
515 FILE *ibuf
= NULL
, *obuf
, *abuf
;
516 enum n_fopen_state fs
;
522 if (ok_blook(append
)) {
523 if ((obuf
= n_fopen_any(mbox
, "a+", &fs
)) == NULL
) {
527 if((fs
& n_PROTO_MASK
) == n_PROTO_FILE
)
528 n_folder_mbox_prepare_append(obuf
, NULL
);
530 if ((obuf
= Ftmp(&tempQuit
, "makembox",
531 OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
)) == NULL
) {
532 n_perr(_("temporary mail quit file"), 0);
535 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
)
537 Ftmp_release(&tempQuit
);
543 if ((abuf
= n_fopen_any(mbox
, "r", &fs
)) != NULL
) {
546 for (lastnl
= FAL0
; (c
= getc(abuf
)) != EOF
&& putc(c
, obuf
) != EOF
;)
547 lastnl
= (c
== '\n') ? (lastnl
? TRU2
: TRU1
) : FAL0
;
548 if(lastnl
!= TRU2
&& (fs
& n_PROTO_MASK
) == n_PROTO_FILE
)
554 n_perr(_("temporary mail quit file"), 0);
561 if ((c
= open(mbox
, (O_WRONLY
| O_CREAT
| n_O_NOXY_BITS
| O_TRUNC
),
564 if ((obuf
= n_fopen_any(mbox
, "r+", &fs
)) == NULL
) {
572 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
573 if (mp
->m_flag
& MBOX
) {
576 if((fs
& n_PROTO_MASK
) == n_PROTO_IMAP
&&
577 !n_ignore_is_any(n_IGNORE_SAVE
) && imap_thisaccount(mbox
)){
578 if(imap_copy(mp
, PTR2SIZE(mp
- message
+ 1), mbox
) == STOP
)
582 if (sendmp(mp
, obuf
, n_IGNORE_SAVE
, NULL
, SEND_MBOX
, NULL
) < 0) {
593 mp
->m_flag
|= MBOXED
;
599 /* Copy the user's old mbox contents back to the end of the stuff we just
600 * saved. If we are appending, this is unnecessary */
601 if (!ok_blook(append
)) {
605 for(lastnl
= FAL0
; (c
= getc(ibuf
)) != EOF
&& putc(c
, obuf
) != EOF
;)
606 lastnl
= (c
== '\n') ? (lastnl
? TRU2
: TRU1
) : FAL0
;
607 if(lastnl
!= TRU2
&& (fs
& n_PROTO_MASK
) == n_PROTO_FILE
)
618 if (Fclose(obuf
) != 0) {
620 if((fs
& n_PROTO_MASK
) != n_PROTO_IMAP
)
626 fprintf(n_stdout
, _("Saved 1 message in mbox\n"));
628 fprintf(n_stdout
, _("Saved %d messages in mbox\n"), mcount
);
636 save_mbox_for_possible_quitstuff(void){ /* TODO try to get rid of that */
640 if((cp
= fexpand("&", FEXP_NVAR
)) == NULL
)
642 n_strscpy(_mboxname
, cp
, sizeof _mboxname
);
649 enum quitflags qf
= 0;
653 for (i
= 0; i
< n_NELEM(_quitnames
); ++i
)
654 if (n_var_oklook(_quitnames
[i
].okey
) != NULL
)
655 qf
|= _quitnames
[i
].flag
;
661 restorequitflags(int qf
)
666 for (i
= 0; i
< n_NELEM(_quitnames
); ++i
) {
667 char *x
= n_var_oklook(_quitnames
[i
].okey
);
668 if (qf
& _quitnames
[i
].flag
) {
670 n_var_okset(_quitnames
[i
].okey
, TRU1
);
671 } else if (x
!= NULL
)
672 n_var_okclear(_quitnames
[i
].okey
);