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