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