Removed configure flag --disable-locale and have it always enabled.
[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", 2 },
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         setlocale(LC_ALL, "");
37         invo_name = mhbasename(argv[0]);
38
39         context_read();
40
41         arguments = getarguments(invo_name, argc, argv, 1);
42         argp = arguments;
43
44         /* parse arguments */
45         while ((cp = *argp++)) {
46                 if (*cp == '-') {
47                         switch (smatch(++cp, switches)) {
48                         case AMBIGSW:
49                                 ambigsw(cp, switches);
50                                 done(1);
51                         case UNKWNSW:
52                                 adios(NULL, "-%s unknown\n", cp);
53
54                         case HELPSW:
55                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
56                                 print_help(buf, switches, 1);
57                                 done(1);
58                         case VERSIONSW:
59                                 print_version(invo_name);
60                                 done(1);
61
62                         case UNLINKSW:
63                                 unlink_msgs++;
64                                 continue;
65                         case NUNLINKSW:
66                                 unlink_msgs = 0;
67                                 continue;
68                         }
69                 }
70                 if (*cp == '+' || *cp == '@') {
71                         if (folder)
72                                 adios(NULL, "only one folder at a time!");
73                         else
74                                 folder = getcpy(expandfol(cp));
75                 } else
76                         app_msgarg(&msgs, cp);
77         }
78
79         if (!msgs.size)
80                 app_msgarg(&msgs, seq_cur);
81         if (!folder)
82                 folder = getcurfol();
83         maildir = toabsdir(folder);
84
85         if (chdir(maildir) == NOTOK)
86                 adios(maildir, "unable to change directory to");
87
88         /* read folder and create message structure */
89         if (!(mp = folder_read(folder)))
90                 adios(NULL, "unable to read folder %s", folder);
91
92         /* check for empty folder */
93         if (mp->nummsg == 0)
94                 adios(NULL, "no messages in %s", folder);
95
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]))
99                         done(1);
100         seq_setprev(mp);  /* set the previous-sequence */
101
102
103         if (unlink_msgs) {
104                 /* "remove" the SELECTED messages */
105                 folder_delmsgs(mp, 1);
106
107                 seq_save(mp);
108                 context_replace(curfolder, folder);
109                 context_save();
110                 folder_free(mp);
111                 done(0);
112                 return 1;
113         }
114
115         /*
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.
120         */
121         context_replace(curfolder, folder);
122         context_save();
123         fflush(stdout);
124
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);
130         }
131
132         /* Mark that the sequence information has changed */
133         mp->msgflags |= SEQMOD;
134
135         if (mp->numsel+4 > MAXARGS)
136                 adios(NULL, "more than %d messages for refile exec",
137                                 MAXARGS - 4);
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)) {
144                         continue;
145                 }
146                 if (!(vec[vecp++] = strdup(m_name(msgnum)))) {
147                         adios(NULL, "strdup failed");
148                 }
149         }
150         vec[vecp] = NULL;
151
152         fflush(stdout);
153         switch (pid = fork()) {
154         case -1:
155                 adios("fork", "unable to");
156
157         case 0:
158                 execvp(*vec, vec);
159                 fprintf(stderr, "unable to exec ");
160                 perror(*vec);
161                 _exit(-1);
162
163         default:
164                 pidwait(pid, -1);
165         }
166
167         folder_free(mp);
168         done(0);
169         return 1;
170 }