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