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