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