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