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