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