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