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