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