2 ** rmm.c -- remove a message(s)
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.
12 static struct swit switches[] = {
26 main(int argc, char **argv)
28 int msgnum, unlink_msgs = 0, vecp = 0;
29 char *cp, *maildir, *folder = NULL, **vec;
30 char buf[BUFSIZ], **argp;
32 struct msgs_array msgs = { 0, 0, NULL };
36 setlocale(LC_ALL, "");
37 invo_name = mhbasename(argv[0]);
41 arguments = getarguments(invo_name, argc, argv, 1);
45 while ((cp = *argp++)) {
47 switch (smatch(++cp, switches)) {
49 ambigsw(cp, switches);
52 adios(NULL, "-%s unknown\n", cp);
55 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
56 print_help(buf, switches, 1);
59 print_version(invo_name);
70 if (*cp == '+' || *cp == '@') {
72 adios(NULL, "only one folder at a time!");
74 folder = getcpy(expandfol(cp));
76 app_msgarg(&msgs, cp);
80 app_msgarg(&msgs, seq_cur);
83 maildir = toabsdir(folder);
85 if (chdir(maildir) == NOTOK)
86 adios(maildir, "unable to change directory to");
88 /* read folder and create message structure */
89 if (!(mp = folder_read(folder)))
90 adios(NULL, "unable to read folder %s", folder);
92 /* check for empty folder */
94 adios(NULL, "no messages in %s", folder);
96 /* parse all the message ranges/sequences and set SELECTED */
97 for (msgnum = 0; msgnum < msgs.size; msgnum++)
98 if (!m_convert(mp, msgs.msgs[msgnum]))
100 seq_setprev(mp); /* set the previous-sequence */
104 /* "remove" the SELECTED messages */
105 folder_delmsgs(mp, 1);
108 context_replace(curfolder, folder);
116 ** This is hackish. If we don't unlink, but refile,
117 ** then we need to update the current folder in the
118 ** context so the external program will refile files
119 ** from the correct directory.
121 context_replace(curfolder, folder);
125 /* remove by refiling. */
126 /* Unset the EXISTS flag for each message to be removed */
127 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
128 if (is_selected(mp, msgnum))
129 unset_exists(mp, msgnum);
132 /* Mark that the sequence information has changed */
133 mp->msgflags |= SEQMOD;
135 if (mp->numsel+4 > MAXARGS)
136 adios(NULL, "more than %d messages for refile exec",
138 vec = (char **)mh_xmalloc((size_t)(mp->numsel + 4) * sizeof(*vec));
139 vec[vecp++] = "refile";
140 vec[vecp++] = "-nolink";
141 vec[vecp++] = concat("+", trashfolder, NULL);
142 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
143 if (!is_selected(mp, msgnum)) {
146 if (!(vec[vecp++] = strdup(m_name(msgnum)))) {
147 adios(NULL, "strdup failed");
153 switch (pid = fork()) {
155 adios("fork", "unable to");
159 fprintf(stderr, "unable to exec ");