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 - 2013 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
45 /* Touch the indicated file */
46 static void alter(char const *name
);
48 static int writeback(FILE *res
, FILE *obuf
);
49 static void edstop(void);
52 alter(char const *name
)
59 utb
.actime
= time((time_t *)0) + 1;
60 utb
.modtime
= sb
.st_mtime
;
73 * If we are sourcing, then return 1 so execute() can handle it.
74 * Otherwise, return -1 to abort command loop.
82 * Preserve all the appropriate messages back in the system
83 * mailbox, and print a nice message indicated how many were
84 * saved. On any error, just return -1. Else return 0.
85 * Incorporate the any new mail that we found.
88 writeback(FILE *res
, FILE *obuf
)
94 if (fseek(obuf
, 0L, SEEK_SET
) < 0)
98 while ((c
= getc(res
)) != EOF
)
101 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
102 if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
104 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
106 (void)fseek(obuf
, 0L, SEEK_SET
);
112 while ((c
= getc(res
)) != EOF
)
119 (void)fseek(obuf
, 0L, SEEK_SET
);
124 if (fseek(obuf
, 0L, SEEK_SET
) < 0)
128 printf(tr(155, "Held 1 message in %s\n"), displayname
);
130 printf(tr(156, "Held %d messages in %s\n"), p
, displayname
);
135 * Save all of the undetermined messages at the top of "mbox"
136 * Save all untouched messages back in the system mailbox.
137 * Remove the system mailbox, if none saved there.
142 int p
, modify
, anystat
;
143 FILE *fbuf
, *rbuf
, *abuf
;
150 * If we are read only, we can't do anything,
151 * so just return quickly. IMAP can set some
152 * flags (e.g. "\\Seen") so imap_quit must be
155 if (mb
.mb_perm
== 0 && mb
.mb_type
!= MB_IMAP
)
157 /* TODO lex.c:setfile() has just called holdsigs(); before it called
158 * TODO us, but this causes uninterruptible hangs due to blocked sigs
159 * TODO anywhere except for MB_FILE (all others install their own
160 * TODO handlers, as it seems, properly); marked YYY */
161 switch (mb
.mb_type
) {
165 relsesigs(); /* YYY */
167 holdsigs(); /* YYY */
171 relsesigs(); /* YYY */
173 holdsigs(); /* YYY */
179 relsesigs(); /* YYY */
181 holdsigs(); /* YYY */
189 * If editing (not reading system mail box), then do the work
198 * See if there any messages to save in mbox. If no, we
199 * can save copying mbox to /tmp and back.
201 * Check also to see if any files need to be preserved.
202 * Delete all untouched messages to keep them out of mbox.
203 * If all the messages are to be preserved, just exit with
207 fbuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
);
213 if (fcntl_lock(fileno(fbuf
), F_WRLCK
) == -1) {
215 perror(tr(157, "Unable to lock mailbox"));
219 if (dot_lock(mailname
, fileno(fbuf
), 1, stdout
, ".") == -1)
222 if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
223 printf(tr(158, "New mail has arrived.\n"));
224 rbuf
= Ftemp(&tempResid
, "Rq", "w", 0600, 1);
225 if (rbuf
== NULL
|| fbuf
== NULL
)
228 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
229 while ((c
= getc(fbuf
)) != EOF
)
232 p
= minfo
.st_size
- mailsize
;
241 if ((rbuf
= Fopen(tempResid
, "r")) == NULL
)
247 anystat
= holdbits();
249 for (c
= 0, p
= 0, mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
250 if (mp
->m_flag
& MBOX
)
252 if (mp
->m_flag
& MPRESERVE
)
254 if (mp
->m_flag
& MODIFY
)
257 if (p
== msgCount
&& !modify
&& !anystat
) {
259 printf(tr(155, "Held 1 message in %s\n"), displayname
);
261 printf(tr(156, "Held %d messages in %s\n"),
264 dot_unlock(mailname
);
269 writeback(rbuf
, fbuf
);
271 dot_unlock(mailname
);
277 if (makembox() == STOP
) {
279 dot_unlock(mailname
);
283 * Now we are ready to copy back preserved files to
284 * the system mailbox, if any were requested.
288 writeback(rbuf
, fbuf
);
290 dot_unlock(mailname
);
295 * Finally, remove his /usr/mail file.
296 * If new mail has arrived, copy it back.
302 fseek(abuf
, 0L, SEEK_SET
);
303 while ((c
= getc(rbuf
)) != EOF
)
309 dot_unlock(mailname
);
314 dot_unlock(mailname
);
318 printf(tr(166, "Thou hast new mail.\n"));
321 dot_unlock(mailname
);
326 * Adjust the message flags in each message.
332 int anystat
, autohold
, holdbit
, nohold
;
335 autohold
= value("hold") != NULL
;
336 holdbit
= autohold
? MPRESERVE
: MBOX
;
337 nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
338 if (value("keepsave") != NULL
)
340 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
341 if (mp
->m_flag
& MNEW
) {
343 mp
->m_flag
|= MSTATUS
;
345 if (mp
->m_flag
& (MSTATUS
|MFLAG
|MUNFLAG
|MANSWER
|MUNANSWER
|
348 if ((mp
->m_flag
& MTOUCH
) == 0)
349 mp
->m_flag
|= MPRESERVE
;
350 if ((mp
->m_flag
& nohold
) == 0)
351 mp
->m_flag
|= holdbit
;
357 * Create another temporary file and copy user's mbox file
358 * darin. If there is no mbox, copy nothing.
359 * If he has specified "append" don't copy his mailbox,
360 * just copy saveable entries at the end.
367 char *mbox
, *tempQuit
;
369 FILE *ibuf
= NULL
, *obuf
, *abuf
;
374 if (value("append") == NULL
) {
375 if ((obuf
= Ftemp(&tempQuit
, "Rm", "w", 0600, 1)) == NULL
) {
376 perror(tr(163, "temporary mail quit file"));
379 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
) {
388 if ((abuf
= Zopen(mbox
, "r", NULL
)) != NULL
) {
389 while ((c
= getc(abuf
)) != EOF
)
394 perror(tr(163, "temporary mail quit file"));
401 if ((c
= open(mbox
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) >= 0)
403 if ((obuf
= Zopen(mbox
, "r+", NULL
)) == NULL
) {
410 if ((obuf
= Zopen(mbox
, "a", NULL
)) == NULL
) {
414 fchmod(fileno(obuf
), 0600);
416 prot
= which_protocol(mbox
);
417 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
418 if (mp
->m_flag
& MBOX
) {
420 if (prot
== PROTO_IMAP
&&
421 saveignore
[0].i_count
== 0 &&
422 saveignore
[1].i_count
== 0
423 #ifdef HAVE_IMAP /* TODO revisit */
424 && imap_thisaccount(mbox
)
428 if (imap_copy(mp
, mp
-message
+1, mbox
) == STOP
)
431 } else if (sendmp(mp
, obuf
, saveignore
,
432 NULL
, SEND_MBOX
, NULL
) < 0) {
439 mp
->m_flag
|= MBOXED
;
443 * Copy the user's old mbox contents back
444 * to the end of the stuff we just saved.
445 * If we are appending, this is unnecessary.
448 if (value("append") == NULL
) {
466 if (Fclose(obuf
) != 0) {
467 if (prot
!= PROTO_IMAP
)
472 printf(tr(164, "Saved 1 message in mbox\n"));
474 printf(tr(165, "Saved %d messages in mbox\n"), mcount
);
479 * Terminate an editing session by attempting to write out the user's
480 * file from the temporary. Save any new stuff appended to the file.
487 FILE *obuf
, *ibuf
= NULL
;
493 for (mp
= &message
[0], gotcha
= 0; mp
< &message
[msgCount
]; mp
++) {
494 if (mp
->m_flag
& MNEW
) {
496 mp
->m_flag
|= MSTATUS
;
498 if (mp
->m_flag
& (MODIFY
|MDELETED
|MSTATUS
|MFLAG
|MUNFLAG
|
499 MANSWER
|MUNANSWER
|MDRAFT
|MUNDRAFT
))
505 if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
508 if ((obuf
= Ftemp(&tempname
, "edstop", "w", 0600, 1)) == NULL
) {
509 perror(tr(167, "tmpfile"));
513 if ((ibuf
= Zopen(mailname
, "r", &mb
.mb_compressed
)) == NULL
) {
521 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
522 while ((c
= getc(ibuf
)) != EOF
)
526 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
536 printf(tr(168, "\"%s\" "), displayname
);
538 if ((obuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
)) == NULL
) {
545 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
546 if ((mp
->m_flag
& MDELETED
) != 0)
549 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
555 gotcha
= (c
== 0 && ibuf
== NULL
);
557 while ((c
= getc(ibuf
)) != EOF
)
568 if (gotcha
&& value("emptybox") == NULL
) {
570 printf((value("bsdcompat") || value("bsdmsgs"))
571 ? tr(169, "removed\n") : tr(211, "removed.\n"));
573 printf((value("bsdcompat") || value("bsdmsgs"))
574 ? tr(170, "complete\n") : tr(212, "updated.\n"));
583 QUITFLAG_KEEPSAVE
= 002,
584 QUITFLAG_APPEND
= 004,
585 QUITFLAG_EMPTYBOX
= 010
588 static const struct quitnames
{
592 { QUITFLAG_HOLD
, "hold" },
593 { QUITFLAG_KEEPSAVE
, "keepsave" },
594 { QUITFLAG_APPEND
, "append" },
595 { QUITFLAG_EMPTYBOX
, "emptybox" },
602 enum quitflags qf
= 0;
605 for (i
= 0; quitnames
[i
].name
; i
++)
606 if (value(quitnames
[i
].name
))
607 qf
|= quitnames
[i
].flag
;
612 restorequitflags(int qf
)
616 for (i
= 0; quitnames
[i
].name
; i
++)
617 if (qf
& quitnames
[i
].flag
) {
618 if (value(quitnames
[i
].name
) == NULL
)
619 assign(quitnames
[i
].name
, "");
620 } else if (value(quitnames
[i
].name
))
621 unset_internal(quitnames
[i
].name
);