2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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 static char sccsid
[] = "@(#)quit.c 2.30 (gritter) 11/11/08";
56 * Rcv -- receive mail rationally.
58 * Termination processing.
61 static int writeback(FILE *res
, FILE *obuf
);
62 static void edstop(void);
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 fseek(obuf
, 0L, SEEK_SET
);
97 while ((c
= getc(res
)) != EOF
)
100 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
101 if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
103 if (send(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
105 fseek(obuf
, 0L, SEEK_SET
);
111 while ((c
= getc(res
)) != EOF
)
118 fseek(obuf
, 0L, SEEK_SET
);
123 fseek(obuf
, 0L, SEEK_SET
);
126 printf(catgets(catd
, CATSET
, 155,
127 "Held 1 message in %s\n"), mailname
);
129 printf(catgets(catd
, CATSET
, 156,
130 "Held %d messages in %s\n"), p
, mailname
);
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
, *readstat
= NULL
, *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 switch (mb
.mb_type
) {
174 * If editing (not reading system mail box), then do the work
183 * See if there any messages to save in mbox. If no, we
184 * can save copying mbox to /tmp and back.
186 * Check also to see if any files need to be preserved.
187 * Delete all untouched messages to keep them out of mbox.
188 * If all the messages are to be preserved, just exit with
192 fbuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
);
198 if (fcntl_lock(fileno(fbuf
), F_WRLCK
) == -1) {
200 perror(catgets(catd
, CATSET
, 157, "Unable to lock mailbox"));
204 if (dot_lock(mailname
, fileno(fbuf
), 1, stdout
, ".") == -1)
207 if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
208 printf(catgets(catd
, CATSET
, 158, "New mail has arrived.\n"));
209 rbuf
= Ftemp(&tempResid
, "Rq", "w", 0600, 1);
210 if (rbuf
== NULL
|| fbuf
== NULL
)
213 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
214 while ((c
= getc(fbuf
)) != EOF
)
217 p
= minfo
.st_size
- mailsize
;
226 if ((rbuf
= Fopen(tempResid
, "r")) == NULL
)
232 anystat
= holdbits();
235 if ((readstat
= Zopen(Tflag
, "w", NULL
)) == NULL
)
238 for (c
= 0, p
= 0, mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
239 if (mp
->m_flag
& MBOX
)
241 if (mp
->m_flag
& MPRESERVE
)
243 if (mp
->m_flag
& MODIFY
)
245 if (readstat
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
248 if ((id
= hfield("message-id", mp
)) != NULL
||
249 (id
= hfield("article-id", mp
)) != NULL
)
250 fprintf(readstat
, "%s\n", id
);
253 if (readstat
!= NULL
)
255 if (p
== msgCount
&& !modify
&& !anystat
) {
257 printf(catgets(catd
, CATSET
, 155,
258 "Held 1 message in %s\n"), mailname
);
260 printf(catgets(catd
, CATSET
, 156,
261 "Held %d messages in %s\n"), p
, mailname
);
263 dot_unlock(mailname
);
268 writeback(rbuf
, fbuf
);
270 dot_unlock(mailname
);
276 if (makembox() == STOP
) {
278 dot_unlock(mailname
);
282 * Now we are ready to copy back preserved files to
283 * the system mailbox, if any were requested.
287 writeback(rbuf
, fbuf
);
289 dot_unlock(mailname
);
294 * Finally, remove his /usr/mail file.
295 * If new mail has arrived, copy it back.
301 fseek(abuf
, 0L, SEEK_SET
);
302 while ((c
= getc(rbuf
)) != EOF
)
308 dot_unlock(mailname
);
313 dot_unlock(mailname
);
317 printf(catgets(catd
, CATSET
, 166, "Thou hast new mail.\n"));
320 dot_unlock(mailname
);
325 * Adjust the message flags in each message.
331 int anystat
, autohold
, holdbit
, nohold
;
334 autohold
= value("hold") != NULL
;
335 holdbit
= autohold
? MPRESERVE
: MBOX
;
336 nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
337 if (value("keepsave") != NULL
)
339 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
340 if (mp
->m_flag
& MNEW
) {
342 mp
->m_flag
|= MSTATUS
;
344 if (mp
->m_flag
& (MSTATUS
|MFLAG
|MUNFLAG
|MANSWER
|MUNANSWER
|
347 if ((mp
->m_flag
& MTOUCH
) == 0)
348 mp
->m_flag
|= MPRESERVE
;
349 if ((mp
->m_flag
& nohold
) == 0)
350 mp
->m_flag
|= holdbit
;
356 * Create another temporary file and copy user's mbox file
357 * darin. If there is no mbox, copy nothing.
358 * If he has specified "append" don't copy his mailbox,
359 * just copy saveable entries at the end.
366 char *mbox
, *tempQuit
;
368 FILE *ibuf
= NULL
, *obuf
, *abuf
;
373 if (value("append") == NULL
) {
374 if ((obuf
= Ftemp(&tempQuit
, "Rm", "w", 0600, 1)) == NULL
) {
375 perror(catgets(catd
, CATSET
, 162,
376 "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(catgets(catd
, CATSET
, 163,
395 "temporary mail quit file"));
401 close(creat(mbox
, 0600));
402 if ((obuf
= Zopen(mbox
, "r+", NULL
)) == NULL
) {
409 if ((obuf
= Zopen(mbox
, "a", NULL
)) == NULL
) {
413 fchmod(fileno(obuf
), 0600);
415 prot
= which_protocol(mbox
);
416 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
417 if (mp
->m_flag
& MBOX
) {
419 if (prot
== PROTO_IMAP
&&
420 saveignore
[0].i_count
== 0 &&
421 saveignore
[1].i_count
== 0 &&
422 imap_thisaccount(mbox
)) {
423 if (imap_copy(mp
, mp
-message
+1, mbox
) == STOP
)
425 } else if (send(mp
, obuf
, saveignore
,
426 NULL
, SEND_MBOX
, NULL
) < 0) {
433 mp
->m_flag
|= MBOXED
;
437 * Copy the user's old mbox contents back
438 * to the end of the stuff we just saved.
439 * If we are appending, this is unnecessary.
442 if (value("append") == NULL
) {
460 if (Fclose(obuf
) != 0) {
461 if (prot
!= PROTO_IMAP
)
466 printf(catgets(catd
, CATSET
, 164, "Saved 1 message in mbox\n"));
468 printf(catgets(catd
, CATSET
, 165,
469 "Saved %d messages in mbox\n"), mcount
);
474 * Terminate an editing session by attempting to write out the user's
475 * file from the temporary. Save any new stuff appended to the file.
482 FILE *obuf
, *ibuf
= NULL
, *readstat
= NULL
;
489 if ((readstat
= Zopen(Tflag
, "w", NULL
)) == 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
))
500 if (readstat
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
503 if ((id
= hfield("message-id", mp
)) != NULL
||
504 (id
= hfield("article-id", mp
)) != NULL
)
505 fprintf(readstat
, "%s\n", id
);
508 if (readstat
!= NULL
)
510 if (!gotcha
|| Tflag
!= NULL
)
513 if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
516 if ((obuf
= Ftemp(&tempname
, "mbox.", "w", 0600, 1)) == NULL
) {
517 perror(catgets(catd
, CATSET
, 167, "tmpfile"));
521 if ((ibuf
= Zopen(mailname
, "r", &mb
.mb_compressed
)) == NULL
) {
529 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
530 while ((c
= getc(ibuf
)) != EOF
)
534 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
544 printf(catgets(catd
, CATSET
, 168, "\"%s\" "), mailname
);
546 if ((obuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
)) == NULL
) {
553 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
554 if ((mp
->m_flag
& MDELETED
) != 0)
557 if (send(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
563 gotcha
= (c
== 0 && ibuf
== NULL
);
565 while ((c
= getc(ibuf
)) != EOF
)
576 if (gotcha
&& value("emptybox") == NULL
) {
578 printf(value("bsdcompat") || value("bsdmsgs") ?
579 catgets(catd
, CATSET
, 169, "removed\n") :
580 catgets(catd
, CATSET
, 211, "removed.\n"));
582 printf(value("bsdcompat") || value("bsdmsgs") ?
583 catgets(catd
, CATSET
, 170, "complete\n") :
584 catgets(catd
, CATSET
, 212, "updated.\n"));
593 QUITFLAG_KEEPSAVE
= 002,
594 QUITFLAG_APPEND
= 004,
595 QUITFLAG_EMPTYBOX
= 010
598 static const struct quitnames
{
602 { QUITFLAG_HOLD
, "hold" },
603 { QUITFLAG_KEEPSAVE
, "keepsave" },
604 { QUITFLAG_APPEND
, "append" },
605 { QUITFLAG_EMPTYBOX
, "emptybox" },
612 enum quitflags qf
= 0;
615 for (i
= 0; quitnames
[i
].name
; i
++)
616 if (value(quitnames
[i
].name
))
617 qf
|= quitnames
[i
].flag
;
622 restorequitflags(int qf
)
626 for (i
= 0; quitnames
[i
].name
; i
++)
627 if (qf
& quitnames
[i
].flag
) {
628 if (value(quitnames
[i
].name
) == NULL
)
629 assign(quitnames
[i
].name
, "");
630 } else if (value(quitnames
[i
].name
))
631 unset_internal(quitnames
[i
].name
);