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