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