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. 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,
49 QUITFLAG_EMPTYBOX
= 1<<4
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);
83 _alter(char const *name
)
89 if (!stat(name
, &sb
)) {
90 utb
.actime
= time(NULL
) + 1;
91 utb
.modtime
= sb
.st_mtime
;
98 writeback(FILE *res
, FILE *obuf
)
104 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
109 while ((c
= getc(res
)) != EOF
)
113 for (p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
114 if ((mp
->m_flag
& MPRESERVE
) || !(mp
->m_flag
& MTOUCH
)) {
116 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
126 while ((c
= getc(res
)) != EOF
)
134 fseek(obuf
, 0L, SEEK_SET
);
137 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
142 printf(_("Held 1 message in %s\n"), displayname
);
144 printf(_("Held %d messages in %s\n"), p
, displayname
);
154 edstop(void) /* TODO oh my god - and REMOVE that CRAPPY reset(0) jump!! */
158 FILE *obuf
= NULL
, *ibuf
= NULL
;
169 for (mp
= message
, gotcha
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
170 if (mp
->m_flag
& MNEW
) {
172 mp
->m_flag
|= MSTATUS
;
174 if (mp
->m_flag
& (MODIFY
| MDELETED
| MSTATUS
| MFLAG
| MUNFLAG
|
175 MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
))
183 /* TODO This is too simple minded? We should regenerate an index file
184 * TODO to be able to truly tell wether *anything* has changed!
185 * TODO (Or better: only come here.. then! It is an *object method!* */
186 /* TODO Ignoring stat error is easy, huh? */
187 if (!stat(mailname
, &statb
) && statb
.st_size
> mailsize
) {
188 if ((obuf
= Ftmp(NULL
, "edstop", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
190 n_perr(_("tmpfile"), 0);
193 if ((ibuf
= Zopen(mailname
, "r")) == NULL
) {
199 file_lock(fileno(ibuf
), FLT_READ
, 0,0, 1); /* TODO ignoring lock error! */
200 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
201 while ((c
= getc(ibuf
)) != EOF
) /* xxx bytewise??? TODO ... I/O error? */
208 printf(_("\"%s\" "), displayname
);
210 if ((obuf
= Zopen(mailname
, "r+")) == NULL
) {
215 file_lock(fileno(obuf
), FLT_WRITE
, 0,0, 1); /* TODO ignoring lock error! */
220 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
221 if (mp
->m_flag
& MDELETED
)
224 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
233 gotcha
= (c
== 0 && ibuf
== NULL
);
235 while ((c
= getc(ibuf
)) != EOF
)
247 if (gotcha
&& !ok_blook(keep
) && !ok_blook(emptybox
)/* TODO obsolete eb*/) {
249 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
250 ? _("removed\n") : _("removed.\n"));
252 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
253 ? _("complete\n") : _("updated.\n"));
268 if (ok_blook(keep
) || rm(mailname
) < 0) {
269 /* TODO demail(): try use f?truncate(2) instead?! */
270 int fd
= open(mailname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
280 int p
, modify
, anystat
, c
;
281 FILE *fbuf
= NULL
, *lckfp
= NULL
, *rbuf
, *abuf
;
286 temporary_localopts_folder_hook_unroll();
288 /* If we are read only, we can't do anything, so just return quickly */
289 /* TODO yet we cannot return quickly if resources have to be released!
290 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
291 *if (mb.mb_perm == 0)
293 p
= (mb
.mb_perm
== 0);
295 /* TODO lex.c:setfile() has just called hold_sigs(); before it called
296 * TODO us, but this causes uninterruptible hangs due to blocked sigs
297 * TODO anywhere except for MB_FILE (all others install their own
298 * TODO handlers, as it seems, properly); marked YYY */
299 switch (mb
.mb_type
) {
303 rele_sigs(); /* YYY */
305 hold_sigs(); /* YYY */
309 rele_sigs(); /* YYY */
311 hold_sigs(); /* YYY */
317 rele_sigs(); /* YYY */
319 hold_sigs(); /* YYY */
326 if (p
) goto jleave
; /* TODO */
328 /* If editing (not reading system mail box), then do the work in edstop() */
329 if (pstate
& PS_EDIT
) {
334 /* See if there any messages to save in mbox. If no, we
335 * can save copying mbox to /tmp and back.
337 * Check also to see if any files need to be preserved.
338 * Delete all untouched messages to keep them out of mbox.
339 * If all the messages are to be preserved, just exit with
341 fbuf
= Zopen(mailname
, "r+");
346 printf(_("Thou hast new mail.\n"));
350 if ((lckfp
= dot_lock(mailname
, fileno(fbuf
), FLT_WRITE
, 0,0, 1)) == NULL
) {
351 n_perr(_("Unable to (dot) lock mailbox, aborting operation"), 0);
358 if (!fstat(fileno(fbuf
), &minfo
) && minfo
.st_size
> mailsize
) {
359 printf(_("New mail has arrived.\n"));
360 rbuf
= Ftmp(NULL
, "quit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600);
361 if (rbuf
== NULL
|| fbuf
== NULL
)
364 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
365 while ((c
= getc(fbuf
)) != EOF
)
368 p
= minfo
.st_size
- mailsize
;
379 anystat
= holdbits();
381 for (c
= 0, p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
382 if (mp
->m_flag
& MBOX
)
384 if (mp
->m_flag
& MPRESERVE
)
386 if (mp
->m_flag
& MODIFY
)
389 if (p
== msgCount
&& !modify
&& !anystat
) {
391 printf(_("Held 1 message in %s\n"), displayname
);
393 printf(_("Held %d messages in %s\n"), p
, displayname
);
398 writeback(rbuf
, fbuf
);
404 if (makembox() == STOP
)
407 /* Now we are ready to copy back preserved files to the system mailbox, if
408 * any were requested */
410 writeback(rbuf
, fbuf
);
414 /* Finally, remove his file. If new mail has arrived, copy it back */
418 fseek(abuf
, 0L, SEEK_SET
);
419 while ((c
= getc(rbuf
)) != EOF
)
430 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
440 int anystat
, autohold
, holdbit
, nohold
;
444 autohold
= ok_blook(hold
);
445 holdbit
= autohold
? MPRESERVE
: MBOX
;
446 nohold
= MBOX
| MSAVED
| MDELETED
| MPRESERVE
;
447 if (ok_blook(keepsave
))
449 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
450 if (mp
->m_flag
& MNEW
) {
452 mp
->m_flag
|= MSTATUS
;
454 if (mp
->m_flag
& (MSTATUS
| MFLAG
| MUNFLAG
| MANSWER
| MUNANSWER
|
457 if (!(mp
->m_flag
& MTOUCH
))
458 mp
->m_flag
|= MPRESERVE
;
459 if (!(mp
->m_flag
& nohold
))
460 mp
->m_flag
|= holdbit
;
467 makembox(void) /* TODO oh my god */
470 char *mbox
, *tempQuit
;
472 FILE *ibuf
= NULL
, *obuf
, *abuf
;
479 if (!ok_blook(append
)) {
480 if ((obuf
= Ftmp(&tempQuit
, "makembox",
481 OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
, 0600)) == NULL
) {
482 n_perr(_("temporary mail quit file"), 0);
485 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
)
487 Ftmp_release(&tempQuit
);
493 if ((abuf
= Zopen(mbox
, "r")) != NULL
) {
494 while ((c
= getc(abuf
)) != EOF
)
499 n_perr(_("temporary mail quit file"), 0);
506 if ((c
= open(mbox
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600)) != -1)
508 if ((obuf
= Zopen(mbox
, "r+")) == NULL
) {
514 if ((obuf
= Zopen(mbox
, "a")) == NULL
) {
518 fchmod(fileno(obuf
), 0600);
522 prot
= which_protocol(mbox
);
523 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
524 if (mp
->m_flag
& MBOX
) {
526 if (prot
== PROTO_IMAP
&&
527 saveignore
[0].i_count
== 0 && saveignore
[1].i_count
== 0
528 #ifdef HAVE_IMAP /* TODO revisit */
529 && imap_thisaccount(mbox
)
533 if (imap_copy(mp
, PTR2SIZE(mp
- message
+ 1), mbox
) == STOP
)
536 } else if (sendmp(mp
, obuf
, saveignore
, NULL
, SEND_MBOX
, NULL
) < 0) {
545 mp
->m_flag
|= MBOXED
;
551 /* Copy the user's old mbox contents back to the end of the stuff we just
552 * saved. If we are appending, this is unnecessary */
553 if (!ok_blook(append
)) {
571 if (Fclose(obuf
) != 0) {
572 if (prot
!= PROTO_IMAP
)
577 printf(_("Saved 1 message in mbox\n"));
579 printf(_("Saved %d messages in mbox\n"), mcount
);
587 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
592 if ((cp
= expand("&")) == NULL
)
594 n_strlcpy(_mboxname
, cp
, sizeof _mboxname
);
601 enum quitflags qf
= 0;
605 for (i
= 0; i
< NELEM(_quitnames
); ++i
)
606 if (_var_oklook(_quitnames
[i
].okey
) != NULL
)
607 qf
|= _quitnames
[i
].flag
;
613 restorequitflags(int qf
)
618 for (i
= 0; i
< NELEM(_quitnames
); ++i
) {
619 char *x
= _var_oklook(_quitnames
[i
].okey
);
620 if (qf
& _quitnames
[i
].flag
) {
622 _var_okset(_quitnames
[i
].okey
, TRU1
);
623 } else if (x
!= NULL
)
624 _var_okclear(_quitnames
[i
].okey
);