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