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 - 2014 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
40 #ifndef HAVE_AMALGAMATION
49 QUITFLAG_KEEPSAVE
= 002,
50 QUITFLAG_APPEND
= 004,
51 QUITFLAG_EMPTYBOX
= 010
59 static struct quitnames
const _quitnames
[] = {
60 {QUITFLAG_HOLD
, ok_b_hold
},
61 {QUITFLAG_KEEPSAVE
, ok_b_keepsave
},
62 {QUITFLAG_APPEND
, ok_b_append
},
63 {QUITFLAG_EMPTYBOX
, ok_b_emptybox
}
66 static char _mboxname
[PATH_MAX
]; /* Name of mbox */
68 /* Touch the indicated file */
69 static void _alter(char const *name
);
71 /* Preserve all the appropriate messages back in the system mailbox, and print
72 * a nice message indicated how many were saved. On any error, just return -1.
73 * Else return 0. Incorporate the any new mail that we found */
74 static int writeback(FILE *res
, FILE *obuf
);
76 /* Terminate an editing session by attempting to write out the user's file from
77 * the temporary. Save any new stuff appended to the file */
78 static void edstop(void);
81 _alter(char const *name
)
87 if (!stat(name
, &sb
)) {
88 utb
.actime
= time(NULL
) + 1;
89 utb
.modtime
= sb
.st_mtime
;
96 writeback(FILE *res
, FILE *obuf
)
102 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
107 while ((c
= getc(res
)) != EOF
)
111 for (p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
112 if ((mp
->m_flag
& MPRESERVE
) || !(mp
->m_flag
& MTOUCH
)) {
114 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
123 while ((c
= getc(res
)) != EOF
)
131 fseek(obuf
, 0L, SEEK_SET
);
134 if (fseek(obuf
, 0L, SEEK_SET
) == -1)
139 printf(_("Held 1 message in %s\n"), displayname
);
141 printf(_("Held %d messages in %s\n"), p
, displayname
);
151 edstop(void) /* TODO oh my god - and REMOVE that CRAPPY reset(0) jump!! */
155 FILE *obuf
= NULL
, *ibuf
= NULL
;
166 for (mp
= message
, gotcha
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
167 if (mp
->m_flag
& MNEW
) {
169 mp
->m_flag
|= MSTATUS
;
171 if (mp
->m_flag
& (MODIFY
| MDELETED
| MSTATUS
| MFLAG
| MUNFLAG
|
172 MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
))
180 if (!stat(mailname
, &statb
) && statb
.st_size
> mailsize
) {
181 if ((obuf
= Ftmp(NULL
, "edstop", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
183 perror(_("tmpfile"));
186 if ((ibuf
= Zopen(mailname
, "r", &mb
.mb_compressed
)) == NULL
) {
191 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
192 while ((c
= getc(ibuf
)) != EOF
)
199 printf(_("\"%s\" "), displayname
);
201 if ((obuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
)) == NULL
) {
209 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
210 if (mp
->m_flag
& MDELETED
)
213 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
222 gotcha
= (c
== 0 && ibuf
== NULL
);
224 while ((c
= getc(ibuf
)) != EOF
)
236 if (gotcha
&& !ok_blook(emptybox
)) {
238 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
239 ? _("removed\n") : _("removed.\n"));
241 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
242 ? _("complete\n") : _("updated.\n"));
260 /* If we are sourcing, then return 1 so evaluate() can handle it.
261 * Otherwise return -1 to abort command loop */
262 rv
= sourcing
? 1 : -1;
270 int p
, modify
, anystat
, c
;
271 FILE *fbuf
= NULL
, *rbuf
, *abuf
;
277 /* If we are read only, we can't do anything, so just return quickly. IMAP
278 * can set some flags (e.g. "\\Seen") so imap_quit must be called anyway */
279 if (mb
.mb_perm
== 0 && mb
.mb_type
!= MB_IMAP
)
282 /* TODO lex.c:setfile() has just called hold_sigs(); before it called
283 * TODO us, but this causes uninterruptible hangs due to blocked sigs
284 * TODO anywhere except for MB_FILE (all others install their own
285 * TODO handlers, as it seems, properly); marked YYY */
286 switch (mb
.mb_type
) {
290 rele_sigs(); /* YYY */
292 hold_sigs(); /* YYY */
296 rele_sigs(); /* YYY */
298 hold_sigs(); /* YYY */
304 rele_sigs(); /* YYY */
306 hold_sigs(); /* YYY */
314 /* If editing (not reading system mail box), then do the work in edstop() */
320 /* See if there any messages to save in mbox. If no, we
321 * can save copying mbox to /tmp and back.
323 * Check also to see if any files need to be preserved.
324 * Delete all untouched messages to keep them out of mbox.
325 * If all the messages are to be preserved, just exit with
327 fbuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
);
332 printf(_("Thou hast new mail.\n"));
336 if (fcntl_lock(fileno(fbuf
), FLOCK_WRITE
) == -1 ||
337 dot_lock(mailname
, fileno(fbuf
), 1, stdout
, ".") == -1) {
338 perror(_("Unable to lock mailbox"));
345 if (!fstat(fileno(fbuf
), &minfo
) && minfo
.st_size
> mailsize
) {
346 printf(_("New mail has arrived.\n"));
347 rbuf
= Ftmp(&tempResid
, "quit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600);
348 if (rbuf
== NULL
|| fbuf
== NULL
)
351 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
352 while ((c
= getc(fbuf
)) != EOF
)
355 p
= minfo
.st_size
- mailsize
;
366 anystat
= holdbits();
368 for (c
= 0, p
= 0, mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
369 if (mp
->m_flag
& MBOX
)
371 if (mp
->m_flag
& MPRESERVE
)
373 if (mp
->m_flag
& MODIFY
)
376 if (p
== msgCount
&& !modify
&& !anystat
) {
378 printf(_("Held 1 message in %s\n"), displayname
);
380 printf(_("Held %d messages in %s\n"), p
, displayname
);
385 writeback(rbuf
, fbuf
);
391 if (makembox() == STOP
)
394 /* Now we are ready to copy back preserved files to the system mailbox, if
395 * any were requested */
397 writeback(rbuf
, fbuf
);
401 /* Finally, remove his file. If new mail has arrived, copy it back */
405 fseek(abuf
, 0L, SEEK_SET
);
406 while ((c
= getc(rbuf
)) != EOF
)
417 dot_unlock(mailname
);
426 int anystat
, autohold
, holdbit
, nohold
;
430 autohold
= ok_blook(hold
);
431 holdbit
= autohold
? MPRESERVE
: MBOX
;
432 nohold
= MBOX
| MSAVED
| MDELETED
| MPRESERVE
;
433 if (ok_blook(keepsave
))
435 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
436 if (mp
->m_flag
& MNEW
) {
438 mp
->m_flag
|= MSTATUS
;
440 if (mp
->m_flag
& (MSTATUS
| MFLAG
| MUNFLAG
| MANSWER
| MUNANSWER
|
443 if (!(mp
->m_flag
& MTOUCH
))
444 mp
->m_flag
|= MPRESERVE
;
445 if (!(mp
->m_flag
& nohold
))
446 mp
->m_flag
|= holdbit
;
453 makembox(void) /* TODO oh my god */
456 char *mbox
, *tempQuit
;
458 FILE *ibuf
= NULL
, *obuf
, *abuf
;
465 if (!ok_blook(append
)) {
466 if ((obuf
= Ftmp(&tempQuit
, "makembox",
467 OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
, 0600)) == NULL
) {
468 perror(_("temporary mail quit file"));
471 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
)
473 Ftmp_release(&tempQuit
);
479 if ((abuf
= Zopen(mbox
, "r", NULL
)) != NULL
) {
480 while ((c
= getc(abuf
)) != EOF
)
485 perror(_("temporary mail quit file"));
492 if ((c
= open(mbox
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600)) != -1)
494 if ((obuf
= Zopen(mbox
, "r+", NULL
)) == NULL
) {
500 if ((obuf
= Zopen(mbox
, "a", NULL
)) == NULL
) {
504 fchmod(fileno(obuf
), 0600);
508 prot
= which_protocol(mbox
);
509 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
510 if (mp
->m_flag
& MBOX
) {
512 if (prot
== PROTO_IMAP
&&
513 saveignore
[0].i_count
== 0 && saveignore
[1].i_count
== 0
514 #ifdef HAVE_IMAP /* TODO revisit */
515 && imap_thisaccount(mbox
)
519 if (imap_copy(mp
, PTR2SIZE(mp
- message
+ 1), mbox
) == STOP
)
522 } else if (sendmp(mp
, obuf
, saveignore
, NULL
, SEND_MBOX
, NULL
) < 0) {
531 mp
->m_flag
|= MBOXED
;
537 /* Copy the user's old mbox contents back to the end of the stuff we just
538 * saved. If we are appending, this is unnecessary */
539 if (!ok_blook(append
)) {
557 if (Fclose(obuf
) != 0) {
558 if (prot
!= PROTO_IMAP
)
563 printf(_("Saved 1 message in mbox\n"));
565 printf(_("Saved %d messages in mbox\n"), mcount
);
573 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
578 if ((cp
= expand("&")) == NULL
)
580 n_strlcpy(_mboxname
, cp
, sizeof _mboxname
);
587 enum quitflags qf
= 0;
591 for (i
= 0; i
< NELEM(_quitnames
); ++i
)
592 if (_var_oklook(_quitnames
[i
].okey
) != NULL
)
593 qf
|= _quitnames
[i
].flag
;
599 restorequitflags(int qf
)
604 for (i
= 0; i
< NELEM(_quitnames
); ++i
) {
605 char *x
= _var_oklook(_quitnames
[i
].okey
);
606 if (qf
& _quitnames
[i
].flag
) {
608 _var_okset(_quitnames
[i
].okey
, TRU1
);
609 } else if (x
!= NULL
)
610 _var_okclear(_quitnames
[i
].okey
);