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 if (!stat(mailname
, &statb
) && statb
.st_size
> mailsize
) {
188 if ((obuf
= Ftmp(NULL
, "edstop", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
190 perror(_("tmpfile"));
193 if ((ibuf
= Zopen(mailname
, "r")) == NULL
) {
198 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
199 while ((c
= getc(ibuf
)) != EOF
)
206 printf(_("\"%s\" "), displayname
);
208 if ((obuf
= Zopen(mailname
, "r+")) == NULL
) {
216 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
217 if (mp
->m_flag
& MDELETED
)
220 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
229 gotcha
= (c
== 0 && ibuf
== NULL
);
231 while ((c
= getc(ibuf
)) != EOF
)
243 if (gotcha
&& !ok_blook(keep
) && !ok_blook(emptybox
)/* TODO obsolete eb*/) {
245 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
246 ? _("removed\n") : _("removed.\n"));
248 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
249 ? _("complete\n") : _("updated.\n"));
264 if (ok_blook(keep
) || rm(mailname
) < 0) {
265 /* TODO demail(): try use f?truncate(2) instead?! */
266 int fd
= open(mailname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
276 int p
, modify
, anystat
, c
;
277 FILE *fbuf
= NULL
, *rbuf
, *abuf
;
282 temporary_localopts_folder_hook_unroll();
284 /* If we are read only, we can't do anything, so just return quickly */
285 /* TODO yet we cannot return quickly if resources have to be released!
286 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
287 *if (mb.mb_perm == 0)
289 p
= (mb
.mb_perm
== 0);
291 /* TODO lex.c:setfile() has just called hold_sigs(); before it called
292 * TODO us, but this causes uninterruptible hangs due to blocked sigs
293 * TODO anywhere except for MB_FILE (all others install their own
294 * TODO handlers, as it seems, properly); marked YYY */
295 switch (mb
.mb_type
) {
299 rele_sigs(); /* YYY */
301 hold_sigs(); /* YYY */
305 rele_sigs(); /* YYY */
307 hold_sigs(); /* YYY */
313 rele_sigs(); /* YYY */
315 hold_sigs(); /* YYY */
322 if (p
) goto jleave
; /* TODO */
324 /* If editing (not reading system mail box), then do the work in edstop() */
325 if (pstate
& PS_EDIT
) {
330 /* See if there any messages to save in mbox. If no, we
331 * can save copying mbox to /tmp and back.
333 * Check also to see if any files need to be preserved.
334 * Delete all untouched messages to keep them out of mbox.
335 * If all the messages are to be preserved, just exit with
337 fbuf
= Zopen(mailname
, "r+");
342 printf(_("Thou hast new mail.\n"));
346 if (!dot_lock(mailname
, fileno(fbuf
), 1)) {
347 perror(_("Unable to lock mailbox"));
354 if (!fstat(fileno(fbuf
), &minfo
) && minfo
.st_size
> mailsize
) {
355 printf(_("New mail has arrived.\n"));
356 rbuf
= Ftmp(NULL
, "quit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600);
357 if (rbuf
== NULL
|| fbuf
== NULL
)
360 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
361 while ((c
= getc(fbuf
)) != EOF
)
364 p
= minfo
.st_size
- mailsize
;
375 anystat
= holdbits();
377 for (c
= 0, p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
378 if (mp
->m_flag
& MBOX
)
380 if (mp
->m_flag
& MPRESERVE
)
382 if (mp
->m_flag
& MODIFY
)
385 if (p
== msgCount
&& !modify
&& !anystat
) {
387 printf(_("Held 1 message in %s\n"), displayname
);
389 printf(_("Held %d messages in %s\n"), p
, displayname
);
394 writeback(rbuf
, fbuf
);
400 if (makembox() == STOP
)
403 /* Now we are ready to copy back preserved files to the system mailbox, if
404 * any were requested */
406 writeback(rbuf
, fbuf
);
410 /* Finally, remove his file. If new mail has arrived, copy it back */
414 fseek(abuf
, 0L, SEEK_SET
);
415 while ((c
= getc(rbuf
)) != EOF
)
426 dot_unlock(mailname
);
435 int anystat
, autohold
, holdbit
, nohold
;
439 autohold
= ok_blook(hold
);
440 holdbit
= autohold
? MPRESERVE
: MBOX
;
441 nohold
= MBOX
| MSAVED
| MDELETED
| MPRESERVE
;
442 if (ok_blook(keepsave
))
444 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
445 if (mp
->m_flag
& MNEW
) {
447 mp
->m_flag
|= MSTATUS
;
449 if (mp
->m_flag
& (MSTATUS
| MFLAG
| MUNFLAG
| MANSWER
| MUNANSWER
|
452 if (!(mp
->m_flag
& MTOUCH
))
453 mp
->m_flag
|= MPRESERVE
;
454 if (!(mp
->m_flag
& nohold
))
455 mp
->m_flag
|= holdbit
;
462 makembox(void) /* TODO oh my god */
465 char *mbox
, *tempQuit
;
467 FILE *ibuf
= NULL
, *obuf
, *abuf
;
474 if (!ok_blook(append
)) {
475 if ((obuf
= Ftmp(&tempQuit
, "makembox",
476 OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
, 0600)) == NULL
) {
477 perror(_("temporary mail quit file"));
480 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
)
482 Ftmp_release(&tempQuit
);
488 if ((abuf
= Zopen(mbox
, "r")) != NULL
) {
489 while ((c
= getc(abuf
)) != EOF
)
494 perror(_("temporary mail quit file"));
501 if ((c
= open(mbox
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600)) != -1)
503 if ((obuf
= Zopen(mbox
, "r+")) == NULL
) {
509 if ((obuf
= Zopen(mbox
, "a")) == NULL
) {
513 fchmod(fileno(obuf
), 0600);
517 prot
= which_protocol(mbox
);
518 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
519 if (mp
->m_flag
& MBOX
) {
521 if (prot
== PROTO_IMAP
&&
522 saveignore
[0].i_count
== 0 && saveignore
[1].i_count
== 0
523 #ifdef HAVE_IMAP /* TODO revisit */
524 && imap_thisaccount(mbox
)
528 if (imap_copy(mp
, PTR2SIZE(mp
- message
+ 1), mbox
) == STOP
)
531 } else if (sendmp(mp
, obuf
, saveignore
, NULL
, SEND_MBOX
, NULL
) < 0) {
540 mp
->m_flag
|= MBOXED
;
546 /* Copy the user's old mbox contents back to the end of the stuff we just
547 * saved. If we are appending, this is unnecessary */
548 if (!ok_blook(append
)) {
566 if (Fclose(obuf
) != 0) {
567 if (prot
!= PROTO_IMAP
)
572 printf(_("Saved 1 message in mbox\n"));
574 printf(_("Saved %d messages in mbox\n"), mcount
);
582 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
587 if ((cp
= expand("&")) == NULL
)
589 n_strlcpy(_mboxname
, cp
, sizeof _mboxname
);
596 enum quitflags qf
= 0;
600 for (i
= 0; i
< NELEM(_quitnames
); ++i
)
601 if (_var_oklook(_quitnames
[i
].okey
) != NULL
)
602 qf
|= _quitnames
[i
].flag
;
608 restorequitflags(int qf
)
613 for (i
= 0; i
< NELEM(_quitnames
); ++i
) {
614 char *x
= _var_oklook(_quitnames
[i
].okey
);
615 if (qf
& _quitnames
[i
].flag
) {
617 _var_okset(_quitnames
[i
].okey
, TRU1
);
618 } else if (x
!= NULL
)
619 _var_okclear(_quitnames
[i
].okey
);