17b99bef6f857524eec1d92bb68f2e25339b38af
[mmh] / sbr / folder_delmsgs.c
1 /*
2 ** folder_delmsgs.c -- remove (= unlink) SELECTED messages from a folder
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11 /*
12 ** Unlink the SELECTED messages.
13 **
14 ** If there is an error, return -1, else return 0.
15 */
16 int
17 folder_delmsgs(struct msgs *mp, int hook)
18 {
19         int msgnum, retval = 0;
20         char msgpath[BUFSIZ];
21
22         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
23                 if (!is_selected(mp, msgnum)) {
24                         continue;
25                 }
26
27                 /* unselect message */
28                 unset_selected(mp, msgnum);
29                 mp->numsel--;
30
31                 snprintf(msgpath, sizeof (msgpath), "%s/%d",
32                                 mp->foldpath, msgnum);
33
34                 if (hook) {
35                         /* Run the external hook on the message. */
36                         ext_hook("del-hook", msgpath, NULL);
37                 }
38
39                 /* just unlink the messages */
40                 if (unlink(msgpath) == -1) {
41                         admonish(msgpath, "unable to unlink");
42                         retval = -1;
43                         continue;
44                 }
45
46                 /* If removal was successful, decrement message count */
47                 unset_exists(mp, msgnum);
48                 mp->nummsg--;
49         }
50
51         /* Sanity check */
52         if (mp->numsel != 0)
53                 adios(NULL, "oops, mp->numsel should be 0");
54
55         /* Mark that the sequence information has changed */
56         mp->msgflags |= SEQMOD;
57
58         return retval;
59 }