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>.
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #ifndef HAVE_AMALGAMATION
51 QUITFLAG_KEEPSAVE
= 1<<2,
52 QUITFLAG_APPEND
= 1<<3,
53 QUITFLAG_EMPTYBOX
= 1<<4
61 static struct quitnames
const _quitnames
[] = {
62 {QUITFLAG_HOLD
, ok_b_hold
},
63 {QUITFLAG_KEEP
, ok_b_keep
},
64 {QUITFLAG_KEEPSAVE
, ok_b_keepsave
},
65 {QUITFLAG_APPEND
, ok_b_append
},
66 {QUITFLAG_EMPTYBOX
, ok_b_emptybox
} /* TODO obsolete emptybox */
69 static char _mboxname
[PATH_MAX
]; /* Name of mbox */
71 /* Touch the indicated file */
72 static void _alter(char const *name
);
74 /* Preserve all the appropriate messages back in the system mailbox, and print
75 * a nice message indicated how many were saved. On any error, just return -1.
76 * Else return 0. Incorporate the any new mail that we found */
77 static int writeback(FILE *res
, FILE *obuf
);
79 /* Terminate an editing session by attempting to write out the user's file from
80 * the temporary. Save any new stuff appended to the file */
81 static void edstop(void);
83 /* Remove "mailname", unless *keep* says otherwise; force truncation, then */
84 static void _demail(void);
87 _alter(char const *name
)
93 if (!stat(name
, &sb
)) {
94 utb
.actime
= time(NULL
) + 1;
95 utb
.modtime
= sb
.st_mtime
;
102 writeback(FILE *res
, FILE *obuf
)
108 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
113 while ((c
= getc(res
)) != EOF
)
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) {
130 while ((c
= getc(res
)) != EOF
)
138 fseek(obuf
, 0L, SEEK_SET
);
141 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
146 printf(_("Held 1 message in %s\n"), displayname
);
148 printf(_("Held %d messages in %s\n"), p
, displayname
);
158 edstop(void) /* TODO oh my god - and REMOVE that CRAPPY reset(0) jump!! */
162 FILE *obuf
= NULL
, *ibuf
= NULL
;
173 for (mp
= message
, gotcha
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
174 if (mp
->m_flag
& MNEW
) {
176 mp
->m_flag
|= MSTATUS
;
178 if (mp
->m_flag
& (MODIFY
| MDELETED
| MSTATUS
| MFLAG
| MUNFLAG
|
179 MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
))
187 /* TODO This is too simple minded? We should regenerate an index file
188 * TODO to be able to truly tell wether *anything* has changed!
189 * TODO (Or better: only come here.. then! It is an *object method!* */
190 /* TODO Ignoring stat error is easy, huh? */
191 if (!stat(mailname
, &statb
) && statb
.st_size
> mailsize
) {
192 if ((obuf
= Ftmp(NULL
, "edstop", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
194 n_perr(_("tmpfile"), 0);
197 if ((ibuf
= Zopen(mailname
, "r")) == NULL
) {
203 file_lock(fileno(ibuf
), FLT_READ
, 0,0, 1); /* TODO ignoring lock error! */
204 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
205 while ((c
= getc(ibuf
)) != EOF
) /* xxx bytewise??? TODO ... I/O error? */
212 printf(_("\"%s\" "), displayname
);
214 if ((obuf
= Zopen(mailname
, "r+")) == NULL
) {
219 file_lock(fileno(obuf
), FLT_WRITE
, 0,0, 1); /* TODO ignoring lock error! */
224 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
225 if (mp
->m_flag
& MDELETED
)
228 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
237 gotcha
= (c
== 0 && ibuf
== NULL
);
239 while ((c
= getc(ibuf
)) != EOF
)
251 if (gotcha
&& !ok_blook(keep
) && !ok_blook(emptybox
)/* TODO obsolete eb*/) {
253 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
254 ? _("removed\n") : _("removed.\n"));
256 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
257 ? _("complete\n") : _("updated.\n"));
272 if (ok_blook(keep
) || rm(mailname
) < 0) {
273 /* TODO demail(): try use f?truncate(2) instead?! */
274 int fd
= open(mailname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
284 int p
, modify
, anystat
, c
;
285 FILE *fbuf
= NULL
, *lckfp
= NULL
, *rbuf
, *abuf
;
290 temporary_localopts_folder_hook_unroll();
292 /* If we are read only, we can't do anything, so just return quickly */
293 /* TODO yet we cannot return quickly if resources have to be released!
294 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
295 *if (mb.mb_perm == 0)
297 p
= (mb
.mb_perm
== 0);
299 /* TODO lex.c:setfile() has just called hold_sigs(); before it called
300 * TODO us, but this causes uninterruptible hangs due to blocked sigs
301 * TODO anywhere except for MB_FILE (all others install their own
302 * TODO handlers, as it seems, properly); marked YYY */
303 switch (mb
.mb_type
) {
307 rele_sigs(); /* YYY */
309 hold_sigs(); /* YYY */
313 rele_sigs(); /* YYY */
315 hold_sigs(); /* YYY */
321 rele_sigs(); /* YYY */
323 hold_sigs(); /* YYY */
330 if (p
) goto jleave
; /* TODO */
332 /* If editing (not reading system mail box), then do the work in edstop() */
333 if (pstate
& PS_EDIT
) {
338 /* See if there any messages to save in mbox. If no, we
339 * can save copying mbox to /tmp and back.
341 * Check also to see if any files need to be preserved.
342 * Delete all untouched messages to keep them out of mbox.
343 * If all the messages are to be preserved, just exit with
345 fbuf
= Zopen(mailname
, "r+");
350 printf(_("Thou hast new mail.\n"));
354 if ((lckfp
= dot_lock(mailname
, fileno(fbuf
), FLT_WRITE
, 0,0, 1)) == NULL
) {
355 n_perr(_("Unable to (dot) lock mailbox, aborting operation"), 0);
362 if (!fstat(fileno(fbuf
), &minfo
) && minfo
.st_size
> mailsize
) {
363 printf(_("New mail has arrived.\n"));
364 rbuf
= Ftmp(NULL
, "quit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600);
365 if (rbuf
== NULL
|| fbuf
== NULL
)
368 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
369 while ((c
= getc(fbuf
)) != EOF
)
372 p
= minfo
.st_size
- mailsize
;
383 anystat
= holdbits();
385 for (c
= 0, p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
386 if (mp
->m_flag
& MBOX
)
388 if (mp
->m_flag
& MPRESERVE
)
390 if (mp
->m_flag
& MODIFY
)
393 if (p
== msgCount
&& !modify
&& !anystat
) {
395 printf(_("Held 1 message in %s\n"), displayname
);
397 printf(_("Held %d messages in %s\n"), p
, displayname
);
402 writeback(rbuf
, fbuf
);
408 if (makembox() == STOP
)
411 /* Now we are ready to copy back preserved files to the system mailbox, if
412 * any were requested */
414 writeback(rbuf
, fbuf
);
418 /* Finally, remove his file. If new mail has arrived, copy it back */
422 fseek(abuf
, 0L, SEEK_SET
);
423 while ((c
= getc(rbuf
)) != EOF
)
434 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
444 int anystat
, autohold
, holdbit
, nohold
;
448 autohold
= ok_blook(hold
);
449 holdbit
= autohold
? MPRESERVE
: MBOX
;
450 nohold
= MBOX
| MSAVED
| MDELETED
| MPRESERVE
;
451 if (ok_blook(keepsave
))
453 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
454 if (mp
->m_flag
& MNEW
) {
456 mp
->m_flag
|= MSTATUS
;
458 if (mp
->m_flag
& (MSTATUS
| MFLAG
| MUNFLAG
| MANSWER
| MUNANSWER
|
461 if (!(mp
->m_flag
& MTOUCH
))
462 mp
->m_flag
|= MPRESERVE
;
463 if (!(mp
->m_flag
& nohold
))
464 mp
->m_flag
|= holdbit
;
471 makembox(void) /* TODO oh my god */
474 char *mbox
, *tempQuit
;
476 FILE *ibuf
= NULL
, *obuf
, *abuf
;
483 if (!ok_blook(append
)) {
484 if ((obuf
= Ftmp(&tempQuit
, "makembox",
485 OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
, 0600)) == NULL
) {
486 n_perr(_("temporary mail quit file"), 0);
489 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
)
491 Ftmp_release(&tempQuit
);
497 if ((abuf
= Zopen(mbox
, "r")) != NULL
) {
498 while ((c
= getc(abuf
)) != EOF
)
503 n_perr(_("temporary mail quit file"), 0);
510 if ((c
= open(mbox
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600)) != -1)
512 if ((obuf
= Zopen(mbox
, "r+")) == NULL
) {
518 if ((obuf
= Zopen(mbox
, "a")) == NULL
) {
522 fchmod(fileno(obuf
), 0600);
526 prot
= which_protocol(mbox
);
527 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
528 if (mp
->m_flag
& MBOX
) {
530 if (prot
== PROTO_IMAP
&&
531 saveignore
[0].i_count
== 0 && saveignore
[1].i_count
== 0
532 #ifdef HAVE_IMAP /* TODO revisit */
533 && imap_thisaccount(mbox
)
537 if (imap_copy(mp
, PTR2SIZE(mp
- message
+ 1), mbox
) == STOP
)
540 } else if (sendmp(mp
, obuf
, saveignore
, NULL
, SEND_MBOX
, NULL
) < 0) {
549 mp
->m_flag
|= MBOXED
;
555 /* Copy the user's old mbox contents back to the end of the stuff we just
556 * saved. If we are appending, this is unnecessary */
557 if (!ok_blook(append
)) {
575 if (Fclose(obuf
) != 0) {
576 if (prot
!= PROTO_IMAP
)
581 printf(_("Saved 1 message in mbox\n"));
583 printf(_("Saved %d messages in mbox\n"), mcount
);
591 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
596 if ((cp
= expand("&")) == NULL
)
598 n_strlcpy(_mboxname
, cp
, sizeof _mboxname
);
605 enum quitflags qf
= 0;
609 for (i
= 0; i
< NELEM(_quitnames
); ++i
)
610 if (_var_oklook(_quitnames
[i
].okey
) != NULL
)
611 qf
|= _quitnames
[i
].flag
;
617 restorequitflags(int qf
)
622 for (i
= 0; i
< NELEM(_quitnames
); ++i
) {
623 char *x
= _var_oklook(_quitnames
[i
].okey
);
624 if (qf
& _quitnames
[i
].flag
) {
626 _var_okset(_quitnames
[i
].okey
, TRU1
);
627 } else if (x
!= NULL
)
628 _var_okclear(_quitnames
[i
].okey
);