s/pfolder/curfolder/g
[mmh] / uip / rmf.c
1 /*
2 ** rmf.c -- remove a folder
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11 static struct swit switches[] = {
12 #define INTRSW  0
13         { "interactive", 0 },
14 #define NINTRSW  1
15         { "nointeractive", 0 },
16 #define VERSIONSW  2
17         { "version", 0 },
18 #define HELPSW  3
19         { "help", 0 },
20         { NULL, 0 }
21 };
22
23 /*
24 ** static prototypes
25 */
26 static int rmf(char *);
27 static void rma(char *);
28
29
30 int
31 main(int argc, char **argv)
32 {
33         int defolder = 0, interactive = -1;
34         char *cp, *folder = NULL, newfolder[BUFSIZ];
35         char buf[BUFSIZ], **argp, **arguments;
36
37 #ifdef LOCALE
38         setlocale(LC_ALL, "");
39 #endif
40         invo_name = mhbasename(argv[0]);
41
42         /* read user profile/context */
43         context_read();
44
45         arguments = getarguments(invo_name, argc, argv, 1);
46         argp = arguments;
47
48         while ((cp = *argp++)) {
49                 if (*cp == '-') {
50                         switch (smatch(++cp, switches)) {
51                                 case AMBIGSW:
52                                         ambigsw(cp, switches);
53                                         done(1);
54                                 case UNKWNSW:
55                                         adios(NULL, "-%s unknown", cp);
56
57                                 case HELPSW:
58                                         snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
59                                         print_help(buf, switches, 1);
60                                         done(1);
61                                 case VERSIONSW:
62                                         print_version(invo_name);
63                                         done(1);
64
65                                 case INTRSW:
66                                         interactive = 1;
67                                         continue;
68                                 case NINTRSW:
69                                         interactive = 0;
70                                         continue;
71                         }
72                 }
73                 if (*cp == '+' || *cp == '@') {
74                         if (folder)
75                                 adios(NULL, "only one folder at a time!");
76                         else
77                                 folder = getcpy(expandfol(cp));
78                 } else {
79                         adios(NULL, "usage: %s [+folder] [switches]",
80                                         invo_name);
81                 }
82         }
83
84         if (!folder) {
85                 folder = getcurfol();
86                 defolder++;
87         }
88         if (strcmp(toabsdir(folder), pwd()) == 0)
89                 adios(NULL, "You can't remove the current working directory");
90
91         if (interactive == -1)
92                 interactive = defolder;
93
94         if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
95                 for (cp = copy(folder, newfolder);
96                                 cp > newfolder && *cp != '/'; cp--)
97                         continue;
98                 if (cp > newfolder)
99                         *cp = '\0';
100                 else
101                         strncpy(newfolder, getdeffol(), sizeof(newfolder));
102         } else {
103                 strncpy(newfolder, getdeffol(), sizeof(newfolder));
104         }
105
106         if (interactive) {
107                 cp = concat("Remove folder \"", folder, "\"? ", NULL);
108                 if (!getanswer(cp))
109                         done(0);
110                 free(cp);
111         }
112
113         if (rmf(folder) == OK) {
114                 char *cfolder = context_find(curfolder);
115                 if (cfolder && strcmp(cfolder, newfolder)) {
116                         printf("[+%s now current]\n", newfolder);
117                         /* update current folder */
118                         context_replace(curfolder, newfolder);
119                 }
120         }
121         context_save();  /* save the context file */
122         done(0);
123         return 1;
124 }
125
126 static int
127 rmf(char *folder)
128 {
129         int i, j, others;
130         register char *maildir;
131         char cur[BUFSIZ];
132         register struct dirent *dp;
133         register DIR *dd;
134
135         switch (i = chdir(maildir = toabsdir(folder))) {
136                 case OK:
137                         if (access(".", W_OK) != NOTOK &&
138                                         access("..", W_OK) != NOTOK)
139                                 break;  /* fall otherwise */
140
141                 case NOTOK:
142                         snprintf(cur, sizeof(cur), "atr-%s-%s",
143                                                 current, toabsdir(folder));
144                         if (!context_del(cur)) {
145                                 printf("[+%s de-referenced]\n", folder);
146                                 return OK;
147                         }
148                         advise(NULL, "you have no profile entry for the %s folder +%s", i == NOTOK ? "unreadable" : "read-only", folder);
149                         return NOTOK;
150         }
151
152         if ((dd = opendir(".")) == NULL)
153                 adios(NULL, "unable to read folder +%s", folder);
154         others = 0;
155
156         /*
157         ** Run the external delete hook program.
158         */
159
160         (void)ext_hook("del-hook", maildir, (char *)0);
161
162         j = strlen(backup_prefix);
163         while ((dp = readdir(dd))) {
164                 switch (dp->d_name[0]) {
165                         case '.':
166                                 if (strcmp(dp->d_name, ".") == 0 ||
167                                                 strcmp(dp->d_name, "..") == 0)
168                                         continue;  /* else fall */
169
170                         case ',':
171 #ifdef MHE
172                         case '+':
173 #endif /* MHE */
174 #ifdef UCI
175                         case '_':
176                         case '#':
177 #endif /* UCI */
178                                 break;
179
180                         default:
181                                 if (m_atoi(dp->d_name))
182                                         break;
183                                 if (strcmp(dp->d_name, altmsglink) == 0 ||
184                                                 strncmp(dp->d_name,
185                                                 backup_prefix, j) == 0)
186                                         break;
187
188                                 admonish(NULL, "file \"%s/%s\" not deleted",
189                                                 folder, dp->d_name);
190                                 others++;
191                                 continue;
192                 }
193                 if (unlink(dp->d_name) == NOTOK) {
194                         admonish(dp->d_name, "unable to unlink %s:", folder);
195                         others++;
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         chdir("..");
208         if (others == 0) {
209                 context_save();  /* Is this needed? meillo 2011-10 */
210                 fflush(stdout);  /* Is this needed? meillo 2011-10 */
211                 if (rmdir(maildir) != -1) {
212                         return OK;
213                 }
214                 admonish(maildir, "unable to remove directory");
215         }
216
217         advise(NULL, "folder +%s not removed", folder);
218         return NOTOK;
219 }
220
221
222 /*
223 ** Remove all the (private) sequence information for
224 ** this folder from the profile/context list.
225 */
226
227 static void
228 rma(char *folder)
229 {
230         register int alen, j, plen;
231         register char *cp;
232         register struct node *np, *pp;
233
234         alen = strlen("atr-");
235         plen = strlen(cp = getcpy(toabsdir(folder))) + 1;
236
237         /*
238         ** Search context list for keys that look like
239         ** "atr-something-folderpath", and remove them.
240         */
241         for (np = m_defs, pp = NULL; np; np = np->n_next) {
242                 if (isprefix("atr-", np->n_name) &&
243                                 (j = strlen(np->n_name) - plen) > alen &&
244                                 *(np->n_name + j) == '-' &&
245                                 strcmp(cp, np->n_name + j + 1) == 0) {
246                         if (!np->n_context)
247                                 admonish(NULL, "bug: context_del(key=\"%s\")",
248                                                 np->n_name);
249                         if (pp) {
250                                 pp->n_next = np->n_next;
251                                 np = pp;
252                         } else {
253                                 m_defs = np->n_next;
254                         }
255                         ctxflags |= CTXMOD;
256                 } else {
257                         pp = np;
258                 }
259         }
260         free(cp);
261 }