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