Merge ../mmh
[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
35         setlocale(LC_ALL, "");
36         invo_name = mhbasename(argv[0]);
37
38         context_read();
39
40         arguments = getarguments(invo_name, argc, argv, 1);
41         argp = arguments;
42
43         /* parse arguments */
44         while ((cp = *argp++)) {
45                 if (*cp == '-') {
46                         switch (smatch(++cp, switches)) {
47                         case AMBIGSW:
48                                 ambigsw(cp, switches);
49                                 exit(1);
50                         case UNKWNSW:
51                                 adios(NULL, "-%s unknown\n", cp);
52
53                         case HELPSW:
54                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
55                                 print_help(buf, switches, 1);
56                                 exit(0);
57                         case VERSIONSW:
58                                 print_version(invo_name);
59                                 exit(0);
60
61                         case UNLINKSW:
62                                 unlink_msgs++;
63                                 continue;
64                         case NUNLINKSW:
65                                 unlink_msgs = 0;
66                                 continue;
67                         }
68                 }
69                 if (*cp == '+' || *cp == '@') {
70                         if (folder) {
71                                 adios(NULL, "only one folder at a time!");
72                         } else {
73                                 folder = getcpy(expandfol(cp));
74                         }
75                 } else {
76                         app_msgarg(&msgs, cp);
77                 }
78         }
79
80         if (!msgs.size) {
81                 app_msgarg(&msgs, seq_cur);
82         }
83         if (!folder) {
84                 folder = getcurfol();
85         }
86         maildir = toabsdir(folder);
87
88         if (chdir(maildir) == NOTOK) {
89                 adios(maildir, "unable to change directory to");
90         }
91
92         /* read folder and create message structure */
93         if (!(mp = folder_read(folder))) {
94                 adios(NULL, "unable to read folder %s", folder);
95         }
96         if (mp->nummsg == 0) {
97                 adios(NULL, "no messages in %s", folder);
98         }
99         /*
100         ** parse all the message ranges/sequences and set SELECTED
101         ** (We do this for the refiling case as well, to complain
102         ** about invalid msg arguments in rmm, before we call refile.)
103         */
104         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
105                 if (!m_convert(mp, msgs.msgs[msgnum])) {
106                         /* sysexits EX_USAGE */
107                         exit(1);
108                 }
109         }
110
111         context_replace(curfolder, folder);
112         context_save();
113
114         if (unlink_msgs) {
115                 /* "remove" the SELECTED messages */
116                 folder_delmsgs(mp, 1);
117                 seq_setprev(mp);
118                 seq_save(mp);
119                 folder_free(mp);
120                 return 0;
121         }
122
123         /* remove by refiling. */
124
125         folder_free(mp);
126         fflush(stdout);
127
128         if (msgs.size+6 > MAXARGS) {
129                 adios(NULL, "more than %d messages for refile exec",
130                                 MAXARGS - 6);
131         }
132         vec = (char **)mh_xmalloc((size_t)(msgs.size + 6) * sizeof(*vec));
133         vec[vecp++] = "refile";
134         vec[vecp++] = "-src";
135         vec[vecp++] = concat("+", folder, NULL);
136         vec[vecp++] = "-nolink";
137         vec[vecp++] = concat("+", trashfolder, NULL);
138         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
139                 vec[vecp++] = msgs.msgs[msgnum];
140         }
141         vec[vecp] = NULL;
142
143         return execprog(*vec, vec);
144 }