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