]> diplodocus.org Git - nmh/blob - uip/rmf.c
Quiet lock warning.
[nmh] / uip / rmf.c
1
2 /*
3 * rmf.c -- remove a folder
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13
14 static struct swit switches[] = {
15 #define INTRSW 0
16 { "interactive", 0 },
17 #define NINTRSW 1
18 { "nointeractive", 0 },
19 #define VERSIONSW 2
20 { "version", 0 },
21 #define HELPSW 3
22 { "help", 0 },
23 { NULL, 0 }
24 };
25
26 /*
27 * static prototypes
28 */
29 static int rmf(char *);
30 static void rma (char *);
31
32
33 int
34 main (int argc, char **argv)
35 {
36 int defolder = 0, interactive = -1;
37 char *cp, *folder = NULL, newfolder[BUFSIZ];
38 char buf[BUFSIZ], **argp, **arguments;
39
40 #ifdef LOCALE
41 setlocale(LC_ALL, "");
42 #endif
43 invo_name = r1bindex (argv[0], '/');
44
45 /* read user profile/context */
46 context_read();
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 adios (NULL, "-%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 (1);
65 case VERSIONSW:
66 print_version(invo_name);
67 done (1);
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 adios (NULL, "only one folder at a time!");
80 else
81 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
82 } else {
83 adios (NULL, "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++;
92 }
93 if (strcmp (m_mailpath (folder), pwd ()) == 0)
94 adios (NULL, "sorry, you can't remove the current working directory");
95
96 if (interactive == -1)
97 interactive = defolder;
98
99 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
100 for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
101 continue;
102 if (cp > newfolder)
103 *cp = '\0';
104 else
105 strncpy (newfolder, getfolder(0), sizeof(newfolder));
106 } else {
107 strncpy (newfolder, getfolder(0), sizeof(newfolder));
108 }
109
110 if (interactive) {
111 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
112 if (!getanswer (cp))
113 done (0);
114 free (cp);
115 }
116
117 if (rmf (folder) == OK && strcmp (context_find (pfolder), newfolder)) {
118 printf ("[+%s now current]\n", newfolder);
119 context_replace (pfolder, newfolder); /* update current folder */
120 }
121 context_save (); /* save the context file */
122 return done (0);
123 }
124
125 static int
126 rmf (char *folder)
127 {
128 int i, j, others;
129 register char *maildir;
130 char cur[BUFSIZ];
131 register struct dirent *dp;
132 register DIR *dd;
133
134 switch (i = chdir (maildir = m_maildir (folder))) {
135 case OK:
136 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
137 break; /* fall otherwise */
138
139 case NOTOK:
140 snprintf (cur, sizeof(cur), "atr-%s-%s",
141 current, m_mailpath (folder));
142 if (!context_del (cur)) {
143 printf ("[+%s de-referenced]\n", folder);
144 return OK;
145 }
146 advise (NULL, "you have no profile entry for the %s folder +%s",
147 i == NOTOK ? "unreadable" : "read-only", folder);
148 return NOTOK;
149 }
150
151 if ((dd = opendir (".")) == NULL)
152 adios (NULL, "unable to read folder +%s", folder);
153 others = 0;
154
155 /*
156 * Run the external delete hook program.
157 */
158
159 (void)ext_hook("del-hook", maildir, (char *)0);
160
161 j = strlen(BACKUP_PREFIX);
162 while ((dp = readdir (dd))) {
163 switch (dp->d_name[0]) {
164 case '.':
165 if (strcmp (dp->d_name, ".") == 0
166 || strcmp (dp->d_name, "..") == 0)
167 continue; /* else fall */
168
169 case ',':
170 #ifdef MHE
171 case '+':
172 #endif /* MHE */
173 #ifdef UCI
174 case '_':
175 case '#':
176 #endif /* UCI */
177 break;
178
179 default:
180 if (m_atoi (dp->d_name))
181 break;
182 if (strcmp (dp->d_name, LINK) == 0
183 || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
184 break;
185
186 admonish (NULL, "file \"%s/%s\" not deleted",
187 folder, dp->d_name);
188 others++;
189 continue;
190 }
191 if (unlink (dp->d_name) == NOTOK) {
192 admonish (dp->d_name, "unable to unlink %s:", folder);
193 others++;
194 }
195 }
196
197 closedir (dd);
198
199 /*
200 * Remove any relevant private sequences
201 * or attributes from context file.
202 */
203 rma (folder);
204
205 chdir ("..");
206 if (others == 0 && remdir (maildir))
207 return OK;
208
209 advise (NULL, "folder +%s not removed", folder);
210 return NOTOK;
211 }
212
213
214 /*
215 * Remove all the (private) sequence information for
216 * this folder from the profile/context list.
217 */
218
219 static void
220 rma (char *folder)
221 {
222 register int alen, j, plen;
223 register char *cp;
224 register struct node *np, *pp;
225
226 alen = strlen ("atr-");
227 plen = strlen (cp = m_mailpath (folder)) + 1;
228
229 /*
230 * Search context list for keys that look like
231 * "atr-something-folderpath", and remove them.
232 */
233 for (np = m_defs, pp = NULL; np; np = np->n_next) {
234 if (ssequal ("atr-", np->n_name)
235 && (j = strlen (np->n_name) - plen) > alen
236 && *(np->n_name + j) == '-'
237 && strcmp (cp, np->n_name + j + 1) == 0) {
238 if (!np->n_context)
239 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
240 if (pp) {
241 pp->n_next = np->n_next;
242 np = pp;
243 } else {
244 m_defs = np->n_next;
245 }
246 ctxflags |= CTXMOD;
247 } else {
248 pp = np;
249 }
250 }
251 }