2 * S-nail - 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
50 * Rcv -- receive mail rationally.
52 * Termination processing.
55 static int writeback(FILE *res
, FILE *obuf
);
56 static void edstop(void);
67 * If we are sourcing, then return 1 so execute() can handle it.
68 * Otherwise, return -1 to abort command loop.
76 * Preserve all the appropriate messages back in the system
77 * mailbox, and print a nice message indicated how many were
78 * saved. On any error, just return -1. Else return 0.
79 * Incorporate the any new mail that we found.
82 writeback(FILE *res
, FILE *obuf
)
88 fseek(obuf
, 0L, SEEK_SET
);
91 while ((c
= getc(res
)) != EOF
)
94 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
95 if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
97 if (send(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
99 fseek(obuf
, 0L, SEEK_SET
);
105 while ((c
= getc(res
)) != EOF
)
112 fseek(obuf
, 0L, SEEK_SET
);
117 fseek(obuf
, 0L, SEEK_SET
);
120 printf(catgets(catd
, CATSET
, 155,
121 "Held 1 message in %s\n"), mailname
);
123 printf(catgets(catd
, CATSET
, 156,
124 "Held %d messages in %s\n"), p
, mailname
);
129 * Save all of the undetermined messages at the top of "mbox"
130 * Save all untouched messages back in the system mailbox.
131 * Remove the system mailbox, if none saved there.
136 int p
, modify
, anystat
;
137 FILE *fbuf
, *rbuf
, *readstat
= NULL
, *abuf
;
144 * If we are read only, we can't do anything,
145 * so just return quickly. IMAP can set some
146 * flags (e.g. "\\Seen") so imap_quit must be
149 if (mb
.mb_perm
== 0 && mb
.mb_type
!= MB_IMAP
)
151 switch (mb
.mb_type
) {
168 * If editing (not reading system mail box), then do the work
177 * See if there any messages to save in mbox. If no, we
178 * can save copying mbox to /tmp and back.
180 * Check also to see if any files need to be preserved.
181 * Delete all untouched messages to keep them out of mbox.
182 * If all the messages are to be preserved, just exit with
186 fbuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
);
192 if (fcntl_lock(fileno(fbuf
), F_WRLCK
) == -1) {
194 perror(catgets(catd
, CATSET
, 157, "Unable to lock mailbox"));
198 if (dot_lock(mailname
, fileno(fbuf
), 1, stdout
, ".") == -1)
201 if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
202 printf(catgets(catd
, CATSET
, 158, "New mail has arrived.\n"));
203 rbuf
= Ftemp(&tempResid
, "Rq", "w", 0600, 1);
204 if (rbuf
== NULL
|| fbuf
== NULL
)
207 fseek(fbuf
, (long)mailsize
, SEEK_SET
);
208 while ((c
= getc(fbuf
)) != EOF
)
211 p
= minfo
.st_size
- mailsize
;
220 if ((rbuf
= Fopen(tempResid
, "r")) == NULL
)
226 anystat
= holdbits();
229 if ((readstat
= Zopen(Tflag
, "w", NULL
)) == NULL
)
232 for (c
= 0, p
= 0, mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
233 if (mp
->m_flag
& MBOX
)
235 if (mp
->m_flag
& MPRESERVE
)
237 if (mp
->m_flag
& MODIFY
)
239 if (readstat
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
242 if ((id
= hfield1("message-id", mp
)) != NULL
||
243 (id
= hfield1("article-id",mp
)) != NULL
)
244 fprintf(readstat
, "%s\n", id
);
247 if (readstat
!= NULL
)
249 if (p
== msgCount
&& !modify
&& !anystat
) {
251 printf(catgets(catd
, CATSET
, 155,
252 "Held 1 message in %s\n"), mailname
);
254 printf(catgets(catd
, CATSET
, 156,
255 "Held %d messages in %s\n"), p
, mailname
);
257 dot_unlock(mailname
);
262 writeback(rbuf
, fbuf
);
264 dot_unlock(mailname
);
270 if (makembox() == STOP
) {
272 dot_unlock(mailname
);
276 * Now we are ready to copy back preserved files to
277 * the system mailbox, if any were requested.
281 writeback(rbuf
, fbuf
);
283 dot_unlock(mailname
);
288 * Finally, remove his /usr/mail file.
289 * If new mail has arrived, copy it back.
295 fseek(abuf
, 0L, SEEK_SET
);
296 while ((c
= getc(rbuf
)) != EOF
)
302 dot_unlock(mailname
);
307 dot_unlock(mailname
);
311 printf(catgets(catd
, CATSET
, 166, "Thou hast new mail.\n"));
314 dot_unlock(mailname
);
319 * Adjust the message flags in each message.
325 int anystat
, autohold
, holdbit
, nohold
;
328 autohold
= value("hold") != NULL
;
329 holdbit
= autohold
? MPRESERVE
: MBOX
;
330 nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
331 if (value("keepsave") != NULL
)
333 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
334 if (mp
->m_flag
& MNEW
) {
336 mp
->m_flag
|= MSTATUS
;
338 if (mp
->m_flag
& (MSTATUS
|MFLAG
|MUNFLAG
|MANSWER
|MUNANSWER
|
341 if ((mp
->m_flag
& MTOUCH
) == 0)
342 mp
->m_flag
|= MPRESERVE
;
343 if ((mp
->m_flag
& nohold
) == 0)
344 mp
->m_flag
|= holdbit
;
350 * Create another temporary file and copy user's mbox file
351 * darin. If there is no mbox, copy nothing.
352 * If he has specified "append" don't copy his mailbox,
353 * just copy saveable entries at the end.
360 char *mbox
, *tempQuit
;
362 FILE *ibuf
= NULL
, *obuf
, *abuf
;
367 if (value("append") == NULL
) {
368 if ((obuf
= Ftemp(&tempQuit
, "Rm", "w", 0600, 1)) == NULL
) {
369 perror(catgets(catd
, CATSET
, 162,
370 "temporary mail quit file"));
373 if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
) {
382 if ((abuf
= Zopen(mbox
, "r", NULL
)) != NULL
) {
383 while ((c
= getc(abuf
)) != EOF
)
388 perror(catgets(catd
, CATSET
, 163,
389 "temporary mail quit file"));
395 close(creat(mbox
, 0600));
396 if ((obuf
= Zopen(mbox
, "r+", NULL
)) == NULL
) {
403 if ((obuf
= Zopen(mbox
, "a", NULL
)) == NULL
) {
407 fchmod(fileno(obuf
), 0600);
409 prot
= which_protocol(mbox
);
410 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
411 if (mp
->m_flag
& MBOX
) {
413 if (prot
== PROTO_IMAP
&&
414 saveignore
[0].i_count
== 0 &&
415 saveignore
[1].i_count
== 0 &&
416 imap_thisaccount(mbox
)) {
417 if (imap_copy(mp
, mp
-message
+1, mbox
) == STOP
)
419 } else if (send(mp
, obuf
, saveignore
,
420 NULL
, SEND_MBOX
, NULL
) < 0) {
427 mp
->m_flag
|= MBOXED
;
431 * Copy the user's old mbox contents back
432 * to the end of the stuff we just saved.
433 * If we are appending, this is unnecessary.
436 if (value("append") == NULL
) {
454 if (Fclose(obuf
) != 0) {
455 if (prot
!= PROTO_IMAP
)
460 printf(catgets(catd
, CATSET
, 164, "Saved 1 message in mbox\n"));
462 printf(catgets(catd
, CATSET
, 165,
463 "Saved %d messages in mbox\n"), mcount
);
468 * Terminate an editing session by attempting to write out the user's
469 * file from the temporary. Save any new stuff appended to the file.
476 FILE *obuf
, *ibuf
= NULL
, *readstat
= NULL
;
483 if ((readstat
= Zopen(Tflag
, "w", NULL
)) == NULL
)
486 for (mp
= &message
[0], gotcha
= 0; mp
< &message
[msgCount
]; mp
++) {
487 if (mp
->m_flag
& MNEW
) {
489 mp
->m_flag
|= MSTATUS
;
491 if (mp
->m_flag
& (MODIFY
|MDELETED
|MSTATUS
|MFLAG
|MUNFLAG
|
492 MANSWER
|MUNANSWER
|MDRAFT
|MUNDRAFT
))
494 if (readstat
!= NULL
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
497 if ((id
= hfield1("message-id", mp
)) != NULL
||
498 (id
= hfield1("article-id",mp
)) != NULL
)
499 fprintf(readstat
, "%s\n", id
);
502 if (readstat
!= NULL
)
504 if (!gotcha
|| Tflag
!= NULL
)
507 if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
510 if ((obuf
= Ftemp(&tempname
, "mbox.", "w", 0600, 1)) == NULL
) {
511 perror(catgets(catd
, CATSET
, 167, "tmpfile"));
515 if ((ibuf
= Zopen(mailname
, "r", &mb
.mb_compressed
)) == NULL
) {
523 fseek(ibuf
, (long)mailsize
, SEEK_SET
);
524 while ((c
= getc(ibuf
)) != EOF
)
528 if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
538 printf(catgets(catd
, CATSET
, 168, "\"%s\" "), mailname
);
540 if ((obuf
= Zopen(mailname
, "r+", &mb
.mb_compressed
)) == NULL
) {
547 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
548 if ((mp
->m_flag
& MDELETED
) != 0)
551 if (send(mp
, obuf
, NULL
, NULL
, SEND_MBOX
, NULL
) < 0) {
557 gotcha
= (c
== 0 && ibuf
== NULL
);
559 while ((c
= getc(ibuf
)) != EOF
)
570 if (gotcha
&& value("emptybox") == NULL
) {
572 printf(value("bsdcompat") || value("bsdmsgs") ?
573 catgets(catd
, CATSET
, 169, "removed\n") :
574 catgets(catd
, CATSET
, 211, "removed.\n"));
576 printf(value("bsdcompat") || value("bsdmsgs") ?
577 catgets(catd
, CATSET
, 170, "complete\n") :
578 catgets(catd
, CATSET
, 212, "updated.\n"));
587 QUITFLAG_KEEPSAVE
= 002,
588 QUITFLAG_APPEND
= 004,
589 QUITFLAG_EMPTYBOX
= 010
592 static const struct quitnames
{
596 { QUITFLAG_HOLD
, "hold" },
597 { QUITFLAG_KEEPSAVE
, "keepsave" },
598 { QUITFLAG_APPEND
, "append" },
599 { QUITFLAG_EMPTYBOX
, "emptybox" },
606 enum quitflags qf
= 0;
609 for (i
= 0; quitnames
[i
].name
; i
++)
610 if (value(quitnames
[i
].name
))
611 qf
|= quitnames
[i
].flag
;
616 restorequitflags(int qf
)
620 for (i
= 0; quitnames
[i
].name
; i
++)
621 if (qf
& quitnames
[i
].flag
) {
622 if (value(quitnames
[i
].name
) == NULL
)
623 assign(quitnames
[i
].name
, "");
624 } else if (value(quitnames
[i
].name
))
625 unset_internal(quitnames
[i
].name
);