]> diplodocus.org Git - nmh/blob - uip/rmm.c
f238daee1efcf99636c9e441d755c96134965c74
[nmh] / uip / rmm.c
1 /* rmm.c -- remove a message(s)
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/folder_delmsgs.h"
10 #include "sbr/context_replace.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/path.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
17 #include "h/done.h"
18 #include "h/utils.h"
19 #include "sbr/m_maildir.h"
20
21 #define RMM_SWITCHES \
22 X("unlink", 0, UNLINKSW) \
23 X("nounlink", 0, NUNLINKSW) \
24 X("rmmproc program", 0, RPROCSW) \
25 X("normmproc", 0, NRPRCSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(RMM);
31 #undef X
32
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(RMM, switches);
35 #undef X
36
37
38 int
39 main (int argc, char **argv)
40 {
41 int msgnum;
42 bool unlink_msgs = false;
43 char *cp, *maildir, *folder = NULL;
44 char buf[BUFSIZ], **argp;
45 char **arguments;
46 struct msgs_array msgs = { 0, 0, NULL };
47 struct msgs *mp;
48
49 if (nmh_init(argv[0], true, true)) { return 1; }
50
51 arguments = getarguments (invo_name, argc, argv, 1);
52 argp = arguments;
53
54 /* parse arguments */
55 while ((cp = *argp++)) {
56 if (*cp == '-') {
57 switch (smatch (++cp, switches)) {
58 case AMBIGSW:
59 ambigsw (cp, switches);
60 done (1);
61 case UNKWNSW:
62 die("-%s unknown\n", cp);
63
64 case HELPSW:
65 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
66 invo_name);
67 print_help (buf, switches, 1);
68 done (0);
69 case VERSIONSW:
70 print_version(invo_name);
71 done (0);
72
73 case UNLINKSW:
74 unlink_msgs = true;
75 continue;
76 case NUNLINKSW:
77 unlink_msgs = false;
78 continue;
79
80 case RPROCSW:
81 if (!(rmmproc = *argp++) || *rmmproc == '-')
82 die("missing argument to %s", argp[-2]);
83 continue;
84 case NRPRCSW:
85 rmmproc = NULL;
86 continue;
87 }
88 }
89 if (*cp == '+' || *cp == '@') {
90 if (folder)
91 die("only one folder at a time!");
92 folder = pluspath (cp);
93 } else
94 app_msgarg(&msgs, cp);
95 }
96
97 if (!context_find ("path"))
98 free (path ("./", TFOLDER));
99 if (!msgs.size)
100 app_msgarg(&msgs, "cur");
101 if (!folder)
102 folder = getfolder (1);
103 maildir = m_maildir (folder);
104
105 if (chdir (maildir) == NOTOK)
106 adios (maildir, "unable to change directory to");
107
108 /* read folder and create message structure */
109 if (!(mp = folder_read (folder, 1)))
110 die("unable to read folder %s", folder);
111
112 /* check for empty folder */
113 if (mp->nummsg == 0)
114 die("no messages in %s", folder);
115
116 /* parse all the message ranges/sequences and set SELECTED */
117 for (msgnum = 0; msgnum < msgs.size; msgnum++)
118 if (!m_convert (mp, msgs.msgs[msgnum]))
119 done (1);
120 seq_setprev (mp); /* set the previous-sequence */
121
122 /*
123 * As part of the new world locking order, folder_delmsgs() now updates
124 * the sequence and context for us. But since folder_delmsgs() doesn't
125 * have access to the folder name, change the context now.
126 */
127
128 context_replace (pfolder, folder);
129
130 /* "remove" the SELECTED messages */
131 folder_delmsgs (mp, unlink_msgs, 0);
132
133 folder_free (mp); /* free folder structure */
134 done (0);
135 return 1;
136 }