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