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