]> diplodocus.org Git - nmh/blob - uip/rmm.c
Simplified m_strn() per Ralph's suggestions.
[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 else
83 folder = pluspath (cp);
84 } else
85 app_msgarg(&msgs, cp);
86 }
87
88 if (!context_find ("path"))
89 free (path ("./", TFOLDER));
90 if (!msgs.size)
91 app_msgarg(&msgs, "cur");
92 if (!folder)
93 folder = getfolder (1);
94 maildir = m_maildir (folder);
95
96 if (chdir (maildir) == NOTOK)
97 adios (maildir, "unable to change directory to");
98
99 /* read folder and create message structure */
100 if (!(mp = folder_read (folder, 1)))
101 adios (NULL, "unable to read folder %s", folder);
102
103 /* check for empty folder */
104 if (mp->nummsg == 0)
105 adios (NULL, "no messages in %s", folder);
106
107 /* parse all the message ranges/sequences and set SELECTED */
108 for (msgnum = 0; msgnum < msgs.size; msgnum++)
109 if (!m_convert (mp, msgs.msgs[msgnum]))
110 done (1);
111 seq_setprev (mp); /* set the previous-sequence */
112
113 /*
114 * As part of the new world locking order, folder_delmsgs() now updates
115 * the sequence and context for us. But since folder_delmsgs() doesn't
116 * have access to the folder name, change the context now.
117 */
118
119 context_replace (pfolder, folder);
120
121 /* "remove" the SELECTED messages */
122 folder_delmsgs (mp, unlink_msgs, 0);
123
124 folder_free (mp); /* free folder structure */
125 done (0);
126 return 1;
127 }