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