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
53 /* Touch the indicated file */
54 static void alter(char const *name
);
56 static int writeback(FILE *res
, FILE *obuf
);
57 static void edstop(void);
60 alter(char const *name
)
67 utb
.actime
= time((time_t *)0) + 1;
68 utb
.modtime
= sb
.st_mtime
;
81 * If we are sourcing, then return 1 so execute() can handle it.
82 * Otherwise, return -1 to abort command loop.
90 * Preserve all the appropriate messages back in the system
91 * mailbox, and print a nice message indicated how many were
92 * saved. On any error, just return -1. Else return 0.
93 * Incorporate the any new mail that we found.
96 writeback(FILE *res
, FILE *obuf
)
102 if (fseek(obuf
, 0L, SEEK_SET
) < 0)
106 while ((c
= getc(res
)) != EOF
)
109 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
110 if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
112 if (send(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
114 (void)fseek(obuf
, 0L, SEEK_SET
);
120 while ((c
= getc(res
)) != EOF
)
127 (void)fseek(obuf
, 0L, SEEK_SET
);
132 if (fseek(obuf
, 0L, SEEK_SET
) < 0)
136 printf(tr(155, "Held 1 message in %s\n"), displayname
);
138 printf(tr(156, "Held %d messages in %s\n"), p
, displayname
);
143 * Save all of the undetermined messages at the top of "mbox"
144 * Save all untouched messages back in the system mailbox.
145 * Remove the system mailbox, if none saved there.
150 int p
, modify
, anystat
;
151 FILE *fbuf
, *rbuf
, *abuf
;
158 * If we are read only, we can't do anything,
159 * so just return quickly. IMAP can set some
160 * flags (e.g. "\\Seen") so imap_quit must be
163 if (mb
.mb_perm
== 0 && mb
.mb_type
!= MB_IMAP
)
165 switch (mb
.mb_type
) {
187 * If editing (not reading system mail box), then do the work
196 * See if there any messages to save in mbox. If no, we
197 * can save copying mbox to /tmp and back.
199 * Check also to see if any files need to be preserved.
200 * Delete all untouched messages to keep them out of mbox.
201 * If all the messages are to be preserved, just exit with
205 fbuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
);
211 if (fcntl_lock(fileno(fbuf
), F_WRLCK
) == -1) {
213 perror(catgets(catd
, CATSET
, 157, "Unable to lock mailbox"));
217 if (dot_lock(mailname
, fileno(fbuf
), 1, stdout
, ".") == -1)
220 if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
221 printf(catgets(catd
, CATSET
, 158, "New mail has arrived.\n"));
222 rbuf
= Ftemp(&tempResid
, "Rq", "w", 0600, 1);
223 if (rbuf
== NULL
|| fbuf
== NULL
)
226 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
227 while ((c
= getc(fbuf
)) != EOF
)
230 p
= minfo
.st_size
- mailsize
;
239 if ((rbuf
= Fopen(tempResid
, "r")) == NULL
)
245 anystat
= holdbits();
247 for (c
= 0, p
= 0, mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
248 if (mp
->m_flag
& MBOX
)
250 if (mp
->m_flag
& MPRESERVE
)
252 if (mp
->m_flag
& MODIFY
)
255 if (p
== msgCount
&& !modify
&& !anystat
) {
257 printf(tr(155, "Held 1 message in %s\n"), displayname
);
259 printf(tr(156, "Held %d messages in %s\n"),
262 dot_unlock(mailname
);
267 writeback(rbuf
, fbuf
);
269 dot_unlock(mailname
);
275 if (makembox() == STOP
) {
277 dot_unlock(mailname
);
281 * Now we are ready to copy back preserved files to
282 * the system mailbox, if any were requested.
286 writeback(rbuf
, fbuf
);
288 dot_unlock(mailname
);
293 * Finally, remove his /usr/mail file.
294 * If new mail has arrived, copy it back.
300 fseek(abuf
, 0L, SEEK_SET
);
301 while ((c
= getc(rbuf
)) != EOF
)
307 dot_unlock(mailname
);
312 dot_unlock(mailname
);
316 printf(catgets(catd
, CATSET
, 166, "Thou hast new mail.\n"));
319 dot_unlock(mailname
);
324 * Adjust the message flags in each message.
330 int anystat
, autohold
, holdbit
, nohold
;
333 autohold
= value("hold") != NULL
;
334 holdbit
= autohold
? MPRESERVE
: MBOX
;
335 nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
336 if (value("keepsave") != NULL
)
338 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
339 if (mp
->m_flag
& MNEW
) {
341 mp
->m_flag
|= MSTATUS
;
343 if (mp
->m_flag
& (MSTATUS
|MFLAG
|MUNFLAG
|MANSWER
|MUNANSWER
|
346 if ((mp
->m_flag
& MTOUCH
) == 0)
347 mp
->m_flag
|= MPRESERVE
;
348 if ((mp
->m_flag
& nohold
) == 0)
349 mp
->m_flag
|= holdbit
;
355 * Create another temporary file and copy user's mbox file
356 * darin. If there is no mbox, copy nothing.
357 * If he has specified "append" don't copy his mailbox,
358 * just copy saveable entries at the end.
365 char *mbox
, *tempQuit
;
367 FILE *ibuf
= NULL
, *obuf
, *abuf
;
372 if (value("append") == NULL
) {
373 if ((obuf
= Ftemp(&tempQuit
, "Rm", "w", 0600, 1)) == NULL
) {
374 perror(tr(163, "temporary mail quit file"));
377 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
) {
386 if ((abuf
= Zopen(mbox
, "r", NULL
)) != NULL
) {
387 while ((c
= getc(abuf
)) != EOF
)
392 perror(tr(163, "temporary mail quit file"));
399 if ((c
= open(mbox
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) >= 0)
401 if ((obuf
= Zopen(mbox
, "r+", NULL
)) == NULL
) {
408 if ((obuf
= Zopen(mbox
, "a", NULL
)) == NULL
) {
412 fchmod(fileno(obuf
), 0600);
414 prot
= which_protocol(mbox
);
415 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
416 if (mp
->m_flag
& MBOX
) {
418 if (prot
== PROTO_IMAP
&&
419 saveignore
[0].i_count
== 0 &&
420 saveignore
[1].i_count
== 0
421 #ifdef HAVE_IMAP /* TODO revisit */
422 && imap_thisaccount(mbox
)
426 if (imap_copy(mp
, mp
-message
+1, mbox
) == STOP
)
429 } else if (send(mp
, obuf
, saveignore
,
430 NULL
, SEND_MBOX
, NULL
) < 0) {
437 mp
->m_flag
|= MBOXED
;
441 * Copy the user's old mbox contents back
442 * to the end of the stuff we just saved.
443 * If we are appending, this is unnecessary.
446 if (value("append") == NULL
) {
464 if (Fclose(obuf
) != 0) {
465 if (prot
!= PROTO_IMAP
)
470 printf(catgets(catd
, CATSET
, 164, "Saved 1 message in mbox\n"));
472 printf(catgets(catd
, CATSET
, 165,
473 "Saved %d messages in mbox\n"), mcount
);
478 * Terminate an editing session by attempting to write out the user's
479 * file from the temporary. Save any new stuff appended to the file.
486 FILE *obuf
, *ibuf
= NULL
;
492 for (mp
= &message
[0], gotcha
= 0; mp
< &message
[msgCount
]; mp
++) {
493 if (mp
->m_flag
& MNEW
) {
495 mp
->m_flag
|= MSTATUS
;
497 if (mp
->m_flag
& (MODIFY
|MDELETED
|MSTATUS
|MFLAG
|MUNFLAG
|
498 MANSWER
|MUNANSWER
|MDRAFT
|MUNDRAFT
))
504 if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
507 if ((obuf
= Ftemp(&tempname
, "edstop", "w", 0600, 1)) == NULL
) {
508 perror(catgets(catd
, CATSET
, 167, "tmpfile"));
512 if ((ibuf
= Zopen(mailname
, "r", &mb
.mb_compressed
)) == NULL
) {
520 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
521 while ((c
= getc(ibuf
)) != EOF
)
525 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
535 printf(tr(168, "\"%s\" "), displayname
);
537 if ((obuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
)) == NULL
) {
544 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
545 if ((mp
->m_flag
& MDELETED
) != 0)
548 if (send(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
554 gotcha
= (c
== 0 && ibuf
== NULL
);
556 while ((c
= getc(ibuf
)) != EOF
)
567 if (gotcha
&& value("emptybox") == NULL
) {
569 printf(value("bsdcompat") || value("bsdmsgs") ?
570 catgets(catd
, CATSET
, 169, "removed\n") :
571 catgets(catd
, CATSET
, 211, "removed.\n"));
573 printf(value("bsdcompat") || value("bsdmsgs") ?
574 catgets(catd
, CATSET
, 170, "complete\n") :
575 catgets(catd
, CATSET
, 212, "updated.\n"));
584 QUITFLAG_KEEPSAVE
= 002,
585 QUITFLAG_APPEND
= 004,
586 QUITFLAG_EMPTYBOX
= 010
589 static const struct quitnames
{
593 { QUITFLAG_HOLD
, "hold" },
594 { QUITFLAG_KEEPSAVE
, "keepsave" },
595 { QUITFLAG_APPEND
, "append" },
596 { QUITFLAG_EMPTYBOX
, "emptybox" },
603 enum quitflags qf
= 0;
606 for (i
= 0; quitnames
[i
].name
; i
++)
607 if (value(quitnames
[i
].name
))
608 qf
|= quitnames
[i
].flag
;
613 restorequitflags(int qf
)
617 for (i
= 0; quitnames
[i
].name
; i
++)
618 if (qf
& quitnames
[i
].flag
) {
619 if (value(quitnames
[i
].name
) == NULL
)
620 assign(quitnames
[i
].name
, "");
621 } else if (value(quitnames
[i
].name
))
622 unset_internal(quitnames
[i
].name
);