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