]> diplodocus.org Git - nmh/blob - uip/rmf.c
decode_rfc2047(): Stop unused-variable warning if HAVE_ICONV false.
[nmh] / uip / rmf.c
1 /* rmf.c -- remove a folder
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/getarguments.h"
10 #include "sbr/read_yes_or_no_if_tty.h"
11 #include "sbr/concat.h"
12 #include "sbr/smatch.h"
13 #include "sbr/remdir.h"
14 #include "sbr/ssequal.h"
15 #include "sbr/m_atoi.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/ext_hook.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_del.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
23 #include "sbr/path.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
27 #include "h/done.h"
28 #include "h/utils.h"
29 #include "sbr/m_maildir.h"
30 #include "sbr/m_mktemp.h"
31
32 #define RMF_SWITCHES \
33 X("interactive", 0, INTRSW) \
34 X("nointeractive", 0, NINTRSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
37
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(RMF);
40 #undef X
41
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(RMF, switches);
44 #undef X
45
46 /*
47 * static prototypes
48 */
49 static int rmf(char *);
50 static void rma (char *);
51
52
53 int
54 main (int argc, char **argv)
55 {
56 bool defolder = false;
57 int interactive = -1;
58 char *cp, *folder = NULL, newfolder[BUFSIZ];
59 char buf[BUFSIZ], **argp, **arguments;
60 char *fp;
61
62 if (nmh_init(argv[0], true, true)) { return 1; }
63
64 arguments = getarguments (invo_name, argc, argv, 1);
65 argp = arguments;
66
67 while ((cp = *argp++)) {
68 if (*cp == '-') {
69 switch (smatch (++cp, switches)) {
70 case AMBIGSW:
71 ambigsw (cp, switches);
72 done (1);
73 case UNKWNSW:
74 die("-%s unknown", cp);
75
76 case HELPSW:
77 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
78 invo_name);
79 print_help (buf, switches, 1);
80 done (0);
81 case VERSIONSW:
82 print_version(invo_name);
83 done (0);
84
85 case INTRSW:
86 interactive = 1;
87 continue;
88 case NINTRSW:
89 interactive = 0;
90 continue;
91 }
92 }
93 if (*cp == '+' || *cp == '@') {
94 if (folder)
95 die("only one folder at a time!");
96 folder = pluspath (cp);
97 } else {
98 die("usage: %s [+folder] [switches]", invo_name);
99 }
100 }
101
102 if (!context_find ("path"))
103 free (path ("./", TFOLDER));
104 if (!folder) {
105 folder = getfolder (1);
106 defolder = true;
107 }
108 fp = m_mailpath(folder);
109 if (!strcmp(fp, pwd()))
110 die("sorry, you can't remove the current working directory");
111 free(fp);
112
113 if (interactive == -1)
114 interactive = defolder;
115
116 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
117 for (cp = stpcpy(newfolder, folder); cp > newfolder && *cp != '/'; cp--)
118 continue;
119 if (cp > newfolder)
120 *cp = '\0';
121 else
122 strncpy (newfolder, getfolder(0), sizeof(newfolder));
123 } else {
124 strncpy (newfolder, getfolder(0), sizeof(newfolder));
125 }
126
127 if (interactive) {
128 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
129 if (!read_yes_or_no_if_tty (cp))
130 done (0);
131 free (cp);
132 }
133
134 if (rmf (folder) == OK) {
135 char *cfolder = context_find(pfolder);
136 if (cfolder && strcmp (cfolder, newfolder)) {
137 printf ("[+%s now current]\n", newfolder);
138 context_replace (pfolder, newfolder); /* update current folder */
139 }
140 }
141 context_save (); /* save the context file */
142 done (0);
143 return 1;
144 }
145
146 static int
147 rmf (char *folder)
148 {
149 int i;
150 bool others;
151 char *fp;
152 char *maildir;
153 char cur[BUFSIZ];
154 struct dirent *dp;
155 DIR *dd;
156
157 switch (i = chdir (maildir = m_maildir (folder))) {
158 case OK:
159 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
160 break;
161 /* FALLTHRU */
162
163 case NOTOK:
164 fp = m_mailpath(folder);
165 snprintf (cur, sizeof(cur), "atr-%s-%s", current, fp);
166 free(fp);
167 if (!context_del (cur)) {
168 printf ("[+%s de-referenced]\n", folder);
169 return OK;
170 }
171 inform("you have no profile entry for the %s folder +%s",
172 i == NOTOK ? "unreadable" : "read-only", folder);
173 return NOTOK;
174 }
175
176 if ((dd = opendir (".")) == NULL)
177 die("unable to read folder +%s", folder);
178 others = false;
179
180 /*
181 * Run the external delete hook program.
182 */
183
184 (void)ext_hook("del-hook", maildir, NULL);
185
186 while ((dp = readdir (dd))) {
187 switch (dp->d_name[0]) {
188 case '.':
189 if (strcmp (dp->d_name, ".") == 0
190 || strcmp (dp->d_name, "..") == 0)
191 continue;
192 break;
193
194 case ',':
195 break;
196
197 default:
198 if (m_atoi (dp->d_name))
199 break;
200 if (strcmp (dp->d_name, LINK) == 0
201 || has_prefix(dp->d_name, BACKUP_PREFIX))
202 break;
203
204 inform("file \"%s/%s\" not deleted, continuing...",
205 folder, dp->d_name);
206 others = true;
207 continue;
208 }
209 if (m_unlink (dp->d_name) == NOTOK) {
210 admonish (dp->d_name, "unable to unlink %s:", folder);
211 others = true;
212 }
213 }
214
215 closedir (dd);
216
217 /*
218 * Remove any relevant private sequences
219 * or attributes from context file.
220 */
221 rma (folder);
222
223 if (chdir ("..") < 0) {
224 advise ("..", "chdir");
225 }
226 if (!others && remdir (maildir))
227 return OK;
228
229 inform("folder +%s not removed", folder);
230 return NOTOK;
231 }
232
233
234 /*
235 * Remove all the (private) sequence information for
236 * this folder from the profile/context list.
237 */
238
239 static void
240 rma (char *folder)
241 {
242 int alen, j, plen;
243 char *cp;
244 struct node *np, *pp;
245
246 alen = LEN("atr-");
247 plen = strlen (cp = m_mailpath (folder)) + 1;
248
249 /*
250 * Search context list for keys that look like
251 * "atr-something-folderpath", and remove them.
252 */
253 for (np = m_defs, pp = NULL; np; np = np->n_next) {
254 if (ssequal ("atr-", np->n_name)
255 && (j = strlen (np->n_name) - plen) > alen
256 && *(np->n_name + j) == '-'
257 && strcmp (cp, np->n_name + j + 1) == 0) {
258 if (!np->n_context)
259 inform("bug: context_del(key=\"%s\"), continuing...",
260 np->n_name);
261 if (pp) {
262 pp->n_next = np->n_next;
263 np = pp;
264 } else {
265 m_defs = np->n_next;
266 }
267 ctxflags |= CTXMOD;
268 } else {
269 pp = np;
270 }
271 }
272
273 free(cp);
274 }