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