]> diplodocus.org Git - nmh/blob - uip/rmm.c
b25f083f34bd21bf9bbffeba2cc8695fd9a7dfab
[nmh] / uip / rmm.c
1
2 /*
3 * rmm.c -- remove a message(s)
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/utils.h>
14
15 /*
16 * We allocate space for message names and ranges
17 * (msgs array) this number of elements at a time.
18 */
19 #define MAXMSGS 256
20
21 static struct swit switches[] = {
22 #define UNLINKSW 0
23 { "unlink", 0 },
24 #define NUNLINKSW 1
25 { "nounlink", 0 },
26 #define VERSIONSW 2
27 { "version", 0 },
28 #define HELPSW 3
29 { "help", 0 },
30 { NULL, 0 }
31 };
32
33
34 int
35 main (int argc, char **argv)
36 {
37 int nummsgs, maxmsgs, msgnum, unlink_msgs = 0;
38 char *cp, *maildir, *folder = NULL;
39 char buf[BUFSIZ], **argp;
40 char **arguments, **msgs;
41 struct msgs *mp;
42
43 #ifdef LOCALE
44 setlocale(LC_ALL, "");
45 #endif
46 invo_name = r1bindex (argv[0], '/');
47
48 /* read user profile/context */
49 context_read();
50
51 arguments = getarguments (invo_name, argc, argv, 1);
52 argp = arguments;
53
54 /*
55 * Allocate the initial space to record message
56 * names and ranges.
57 */
58 nummsgs = 0;
59 maxmsgs = MAXMSGS;
60 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
61
62 /* parse arguments */
63 while ((cp = *argp++)) {
64 if (*cp == '-') {
65 switch (smatch (++cp, switches)) {
66 case AMBIGSW:
67 ambigsw (cp, switches);
68 done (1);
69 case UNKWNSW:
70 adios (NULL, "-%s unknown\n", cp);
71
72 case HELPSW:
73 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
74 invo_name);
75 print_help (buf, switches, 1);
76 done (1);
77 case VERSIONSW:
78 print_version(invo_name);
79 done (1);
80
81 case UNLINKSW:
82 unlink_msgs++;
83 continue;
84 case NUNLINKSW:
85 unlink_msgs = 0;
86 continue;
87 }
88 }
89 if (*cp == '+' || *cp == '@') {
90 if (folder)
91 adios (NULL, "only one folder at a time!");
92 else
93 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
94 } else {
95 /*
96 * Check if we need to allocate more space
97 * for message names/ranges.
98 */
99 if (nummsgs >= maxmsgs){
100 maxmsgs += MAXMSGS;
101 if (!(msgs = (char **) realloc (msgs,
102 (size_t) (maxmsgs * sizeof(*msgs)))))
103 adios (NULL, "unable to reallocate msgs storage");
104 }
105 msgs[nummsgs++] = cp;
106 }
107 }
108
109 if (!context_find ("path"))
110 free (path ("./", TFOLDER));
111 if (!nummsgs)
112 msgs[nummsgs++] = "cur";
113 if (!folder)
114 folder = getfolder (1);
115 maildir = m_maildir (folder);
116
117 if (chdir (maildir) == NOTOK)
118 adios (maildir, "unable to change directory to");
119
120 /* read folder and create message structure */
121 if (!(mp = folder_read (folder)))
122 adios (NULL, "unable to read folder %s", folder);
123
124 /* check for empty folder */
125 if (mp->nummsg == 0)
126 adios (NULL, "no messages in %s", folder);
127
128 /* parse all the message ranges/sequences and set SELECTED */
129 for (msgnum = 0; msgnum < nummsgs; msgnum++)
130 if (!m_convert (mp, msgs[msgnum]))
131 done (1);
132 seq_setprev (mp); /* set the previous-sequence */
133
134 /*
135 * This is hackish. If we are using a external rmmproc,
136 * then we need to update the current folder in the
137 * context so the external rmmproc will remove files
138 * from the correct directory. This should be moved to
139 * folder_delmsgs().
140 */
141 if (rmmproc) {
142 context_replace (pfolder, folder);
143 context_save ();
144 fflush (stdout);
145 }
146
147 /* "remove" the SELECTED messages */
148 folder_delmsgs (mp, unlink_msgs, 0);
149
150 seq_save (mp); /* synchronize message sequences */
151 context_replace (pfolder, folder); /* update current folder */
152 context_save (); /* save the context file */
153 folder_free (mp); /* free folder structure */
154 return done (0);
155 }