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