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
)
102 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
103 if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
105 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
107 (void)fseek(obuf
, 0L, SEEK_SET
);
117 while ((c
= getc(res
)) != EOF
)
124 (void)fseek(obuf
, 0L, SEEK_SET
);
129 if (fseek(obuf
, 0L, SEEK_SET
) < 0)
133 printf(tr(155, "Held 1 message in %s\n"), displayname
);
135 printf(tr(156, "Held %d messages in %s\n"), p
, displayname
);
140 * Save all of the undetermined messages at the top of "mbox"
141 * Save all untouched messages back in the system mailbox.
142 * Remove the system mailbox, if none saved there.
147 int p
, modify
, anystat
;
148 FILE *fbuf
, *rbuf
, *abuf
;
155 * If we are read only, we can't do anything,
156 * so just return quickly. IMAP can set some
157 * flags (e.g. "\\Seen") so imap_quit must be
160 if (mb
.mb_perm
== 0 && mb
.mb_type
!= MB_IMAP
)
162 /* TODO lex.c:setfile() has just called holdsigs(); before it called
163 * TODO us, but this causes uninterruptible hangs due to blocked sigs
164 * TODO anywhere except for MB_FILE (all others install their own
165 * TODO handlers, as it seems, properly); marked YYY */
166 switch (mb
.mb_type
) {
170 relsesigs(); /* YYY */
172 holdsigs(); /* YYY */
176 relsesigs(); /* YYY */
178 holdsigs(); /* YYY */
184 relsesigs(); /* YYY */
186 holdsigs(); /* YYY */
194 * If editing (not reading system mail box), then do the work
203 * See if there any messages to save in mbox. If no, we
204 * can save copying mbox to /tmp and back.
206 * Check also to see if any files need to be preserved.
207 * Delete all untouched messages to keep them out of mbox.
208 * If all the messages are to be preserved, just exit with
212 fbuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
);
218 if (fcntl_lock(fileno(fbuf
), F_WRLCK
) == -1) {
220 perror(tr(157, "Unable to lock mailbox"));
224 if (dot_lock(mailname
, fileno(fbuf
), 1, stdout
, ".") == -1)
227 if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
228 printf(tr(158, "New mail has arrived.\n"));
229 rbuf
= Ftemp(&tempResid
, "Rq", "w", 0600, 1);
230 if (rbuf
== NULL
|| fbuf
== NULL
)
233 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
234 while ((c
= getc(fbuf
)) != EOF
)
237 p
= minfo
.st_size
- mailsize
;
246 if ((rbuf
= Fopen(tempResid
, "r")) == NULL
)
252 anystat
= holdbits();
254 for (c
= 0, p
= 0, mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
255 if (mp
->m_flag
& MBOX
)
257 if (mp
->m_flag
& MPRESERVE
)
259 if (mp
->m_flag
& MODIFY
)
262 if (p
== msgCount
&& !modify
&& !anystat
) {
264 printf(tr(155, "Held 1 message in %s\n"), displayname
);
266 printf(tr(156, "Held %d messages in %s\n"),
269 dot_unlock(mailname
);
274 writeback(rbuf
, fbuf
);
276 dot_unlock(mailname
);
282 if (makembox() == STOP
) {
284 dot_unlock(mailname
);
288 * Now we are ready to copy back preserved files to
289 * the system mailbox, if any were requested.
293 writeback(rbuf
, fbuf
);
295 dot_unlock(mailname
);
300 * Finally, remove his /usr/mail file.
301 * If new mail has arrived, copy it back.
307 fseek(abuf
, 0L, SEEK_SET
);
308 while ((c
= getc(rbuf
)) != EOF
)
314 dot_unlock(mailname
);
319 dot_unlock(mailname
);
323 printf(tr(166, "Thou hast new mail.\n"));
326 dot_unlock(mailname
);
331 * Adjust the message flags in each message.
337 int anystat
, autohold
, holdbit
, nohold
;
340 autohold
= value("hold") != NULL
;
341 holdbit
= autohold
? MPRESERVE
: MBOX
;
342 nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
343 if (value("keepsave") != NULL
)
345 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
346 if (mp
->m_flag
& MNEW
) {
348 mp
->m_flag
|= MSTATUS
;
350 if (mp
->m_flag
& (MSTATUS
|MFLAG
|MUNFLAG
|MANSWER
|MUNANSWER
|
353 if ((mp
->m_flag
& MTOUCH
) == 0)
354 mp
->m_flag
|= MPRESERVE
;
355 if ((mp
->m_flag
& nohold
) == 0)
356 mp
->m_flag
|= holdbit
;
362 * Create another temporary file and copy user's mbox file
363 * darin. If there is no mbox, copy nothing.
364 * If he has specified "append" don't copy his mailbox,
365 * just copy saveable entries at the end.
372 char *mbox
, *tempQuit
;
374 FILE *ibuf
= NULL
, *obuf
, *abuf
;
379 if (value("append") == NULL
) {
380 if ((obuf
= Ftemp(&tempQuit
, "Rm", "w", 0600, 1)) == NULL
) {
381 perror(tr(163, "temporary mail quit file"));
384 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
) {
393 if ((abuf
= Zopen(mbox
, "r", NULL
)) != NULL
) {
394 while ((c
= getc(abuf
)) != EOF
)
399 perror(tr(163, "temporary mail quit file"));
406 if ((c
= open(mbox
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) >= 0)
408 if ((obuf
= Zopen(mbox
, "r+", NULL
)) == NULL
) {
415 if ((obuf
= Zopen(mbox
, "a", NULL
)) == NULL
) {
419 fchmod(fileno(obuf
), 0600);
423 prot
= which_protocol(mbox
);
424 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
425 if (mp
->m_flag
& MBOX
) {
427 if (prot
== PROTO_IMAP
&&
428 saveignore
[0].i_count
== 0 &&
429 saveignore
[1].i_count
== 0
430 #ifdef HAVE_IMAP /* TODO revisit */
431 && imap_thisaccount(mbox
)
435 if (imap_copy(mp
, mp
-message
+1, mbox
) == STOP
)
438 } else if (sendmp(mp
, obuf
, saveignore
,
439 NULL
, SEND_MBOX
, NULL
) < 0) {
448 mp
->m_flag
|= MBOXED
;
455 * Copy the user's old mbox contents back
456 * to the end of the stuff we just saved.
457 * If we are appending, this is unnecessary.
460 if (value("append") == NULL
) {
478 if (Fclose(obuf
) != 0) {
479 if (prot
!= PROTO_IMAP
)
484 printf(tr(164, "Saved 1 message in mbox\n"));
486 printf(tr(165, "Saved %d messages in mbox\n"), mcount
);
491 * Terminate an editing session by attempting to write out the user's
492 * file from the temporary. Save any new stuff appended to the file.
499 FILE *obuf
, *ibuf
= NULL
;
505 for (mp
= &message
[0], gotcha
= 0; mp
< &message
[msgCount
]; mp
++) {
506 if (mp
->m_flag
& MNEW
) {
508 mp
->m_flag
|= MSTATUS
;
510 if (mp
->m_flag
& (MODIFY
|MDELETED
|MSTATUS
|MFLAG
|MUNFLAG
|
511 MANSWER
|MUNANSWER
|MDRAFT
|MUNDRAFT
))
517 if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
520 if ((obuf
= Ftemp(&tempname
, "edstop", "w", 0600, 1)) == NULL
) {
521 perror(tr(167, "tmpfile"));
525 if ((ibuf
= Zopen(mailname
, "r", &mb
.mb_compressed
)) == NULL
) {
533 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
534 while ((c
= getc(ibuf
)) != EOF
)
538 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
548 printf(tr(168, "\"%s\" "), displayname
);
550 if ((obuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
)) == NULL
) {
559 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
560 if ((mp
->m_flag
& MDELETED
) != 0)
563 if (sendmp(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
567 reset(0);/* XXX jump aways are terrible */
573 gotcha
= (c
== 0 && ibuf
== NULL
);
575 while ((c
= getc(ibuf
)) != EOF
)
586 if (gotcha
&& value("emptybox") == NULL
) {
588 printf((value("bsdcompat") || value("bsdmsgs"))
589 ? tr(169, "removed\n") : tr(211, "removed.\n"));
591 printf((value("bsdcompat") || value("bsdmsgs"))
592 ? tr(170, "complete\n") : tr(212, "updated.\n"));
601 QUITFLAG_KEEPSAVE
= 002,
602 QUITFLAG_APPEND
= 004,
603 QUITFLAG_EMPTYBOX
= 010
606 static const struct quitnames
{
610 { QUITFLAG_HOLD
, "hold" },
611 { QUITFLAG_KEEPSAVE
, "keepsave" },
612 { QUITFLAG_APPEND
, "append" },
613 { QUITFLAG_EMPTYBOX
, "emptybox" },
620 enum quitflags qf
= 0;
623 for (i
= 0; quitnames
[i
].name
; i
++)
624 if (value(quitnames
[i
].name
))
625 qf
|= quitnames
[i
].flag
;
630 restorequitflags(int qf
)
634 for (i
= 0; quitnames
[i
].name
; i
++)
635 if (qf
& quitnames
[i
].flag
) {
636 if (value(quitnames
[i
].name
) == NULL
)
637 assign(quitnames
[i
].name
, "");
638 } else if (value(quitnames
[i
].name
))
639 unset_internal(quitnames
[i
].name
);