fccbb0960c16b061402b1d09da3e052e5d0135a7
[mmh] / 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     j = strlen(BACKUP_PREFIX);
156     while ((dp = readdir (dd))) {
157         switch (dp->d_name[0]) {
158             case '.': 
159                 if (strcmp (dp->d_name, ".") == 0
160                         || strcmp (dp->d_name, "..") == 0)
161                     continue;   /* else fall */
162
163             case ',': 
164 #ifdef MHE
165             case '+': 
166 #endif /* MHE */
167 #ifdef UCI
168             case '_': 
169             case '#': 
170 #endif /* UCI */
171                 break;
172
173             default: 
174                 if (m_atoi (dp->d_name))
175                     break;
176                 if (strcmp (dp->d_name, LINK) == 0
177                         || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
178                     break;
179
180                 admonish (NULL, "file \"%s/%s\" not deleted",
181                         folder, dp->d_name);
182                 others++;
183                 continue;
184         }
185         if (unlink (dp->d_name) == NOTOK) {
186             admonish (dp->d_name, "unable to unlink %s:", folder);
187             others++;
188         }
189     }
190
191     closedir (dd);
192
193     /*
194      * Remove any relevant private sequences
195      * or attributes from context file.
196      */
197     rma (folder);
198
199     chdir ("..");
200     if (others == 0 && remdir (maildir))
201         return OK;
202
203     advise (NULL, "folder +%s not removed", folder);
204     return NOTOK;
205 }
206
207
208 /*
209  * Remove all the (private) sequence information for
210  * this folder from the profile/context list.
211  */
212
213 static void
214 rma (char *folder)
215 {
216     register int alen, j, plen;
217     register char *cp;
218     register struct node *np, *pp;
219
220     /* sanity check - check that context has been read */
221     if (defpath == NULL)
222         adios (NULL, "oops, context hasn't been read yet");
223
224     alen = strlen ("atr-");
225     plen = strlen (cp = m_mailpath (folder)) + 1;
226
227     /*
228      * Search context list for keys that look like
229      * "atr-something-folderpath", and remove them.
230      */
231     for (np = m_defs, pp = NULL; np; np = np->n_next) {
232         if (ssequal ("atr-", np->n_name)
233                 && (j = strlen (np->n_name) - plen) > alen
234                 && *(np->n_name + j) == '-'
235                 && strcmp (cp, np->n_name + j + 1) == 0) {
236             if (!np->n_context)
237                 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
238             if (pp) {
239                 pp->n_next = np->n_next;
240                 np = pp;
241             } else {
242                 m_defs = np->n_next;
243             }
244             ctxflags |= CTXMOD;
245         } else {
246             pp = np;
247         }
248     }
249 }