ecc9b9b425dacd929b7c8d22f4dc9d4722025dcb
[mmh] / uip / rmm.c
1 /*
2 ** rmm.c -- remove a message(s)
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 #include <h/utils.h>
11
12 static struct swit switches[] = {
13 #define UNLINKSW  0
14         { "unlink", 0 },
15 #define NUNLINKSW  1
16         { "nounlink", 0 },
17 #define VERSIONSW  2
18         { "version", 0 },
19 #define HELPSW  3
20         { "help", 0 },
21         { NULL, 0 }
22 };
23
24
25 int
26 main(int argc, char **argv)
27 {
28         int msgnum, unlink_msgs = 0, vecp = 0;
29         char *cp, *maildir, *folder = NULL, **vec;
30         char buf[BUFSIZ], **argp;
31         char **arguments;
32         struct msgs_array msgs = { 0, 0, NULL };
33         struct msgs *mp;
34         pid_t pid;
35
36
37 #ifdef LOCALE
38         setlocale(LC_ALL, "");
39 #endif
40         invo_name = mhbasename(argv[0]);
41
42         context_read();
43
44         arguments = getarguments(invo_name, argc, argv, 1);
45         argp = arguments;
46
47         /* parse arguments */
48         while ((cp = *argp++)) {
49                 if (*cp == '-') {
50                         switch (smatch(++cp, switches)) {
51                         case AMBIGSW:
52                                 ambigsw(cp, switches);
53                                 done(1);
54                         case UNKWNSW:
55                                 adios(NULL, "-%s unknown\n", cp);
56
57                         case HELPSW:
58                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
59                                 print_help(buf, switches, 1);
60                                 done(1);
61                         case VERSIONSW:
62                                 print_version(invo_name);
63                                 done(1);
64
65                         case UNLINKSW:
66                                 unlink_msgs++;
67                                 continue;
68                         case NUNLINKSW:
69                                 unlink_msgs = 0;
70                                 continue;
71                         }
72                 }
73                 if (*cp == '+' || *cp == '@') {
74                         if (folder)
75                                 adios(NULL, "only one folder at a time!");
76                         else
77                                 folder = getcpy(expandfol(cp));
78                 } else
79                         app_msgarg(&msgs, cp);
80         }
81
82         if (!msgs.size)
83                 app_msgarg(&msgs, seq_cur);
84         if (!folder)
85                 folder = getcurfol();
86         maildir = toabsdir(folder);
87
88         if (chdir(maildir) == NOTOK)
89                 adios(maildir, "unable to change directory to");
90
91         /* read folder and create message structure */
92         if (!(mp = folder_read(folder)))
93                 adios(NULL, "unable to read folder %s", folder);
94
95         /* check for empty folder */
96         if (mp->nummsg == 0)
97                 adios(NULL, "no messages in %s", folder);
98
99         /* parse all the message ranges/sequences and set SELECTED */
100         for (msgnum = 0; msgnum < msgs.size; msgnum++)
101                 if (!m_convert(mp, msgs.msgs[msgnum]))
102                         done(1);
103         seq_setprev(mp);  /* set the previous-sequence */
104
105
106         if (unlink_msgs) {
107                 /* "remove" the SELECTED messages */
108                 folder_delmsgs(mp, 1);
109
110                 seq_save(mp);
111                 context_replace(curfolder, folder);
112                 context_save();
113                 folder_free(mp);
114                 done(0);
115                 return 1;
116         }
117
118         /*
119         ** This is hackish.  If we don't unlink, but refile,
120         ** then we need to update the current folder in the
121         ** context so the external program will refile files
122         ** from the correct directory.
123         */
124         context_replace(curfolder, folder);
125         context_save();
126         fflush(stdout);
127
128         /* remove by refiling. */
129         /* Unset the EXISTS flag for each message to be removed */
130         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
131                 if (is_selected(mp, msgnum))
132                         unset_exists(mp, msgnum);
133         }
134
135         /* Mark that the sequence information has changed */
136         mp->msgflags |= SEQMOD;
137
138         if (mp->numsel+4 > MAXARGS)
139                 adios(NULL, "more than %d messages for refile exec",
140                                 MAXARGS - 4);
141         vec = (char **)mh_xmalloc((size_t)(mp->numsel + 4) * sizeof(*vec));
142         vec[vecp++] = "refile";
143         vec[vecp++] = "-nolink";
144         vec[vecp++] = concat("+", trashfolder, NULL);
145         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
146                 if (!is_selected(mp, msgnum)) {
147                         continue;
148                 }
149                 if (!(vec[vecp++] = strdup(m_name(msgnum)))) {
150                         adios(NULL, "strdup failed");
151                 }
152         }
153         vec[vecp] = NULL;
154
155         fflush(stdout);
156         switch (pid = fork()) {
157         case -1:
158                 adios("fork", "unable to");
159
160         case 0:
161                 execvp(*vec, vec);
162                 fprintf(stderr, "unable to exec ");
163                 perror(*vec);
164                 _exit(-1);
165
166         default:
167                 pidwait(pid, -1);
168         }
169
170         folder_free(mp);
171         done(0);
172         return 1;
173 }