2 * rmf.c -- remove a folder
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.
11 static struct swit switches[] = {
15 { "nointeractive", 0 },
26 static int rmf(char *);
27 static void rma (char *);
31 main (int argc, char **argv)
33 int defolder = 0, interactive = -1;
34 char *cp, *folder = NULL, newfolder[BUFSIZ];
35 char buf[BUFSIZ], **argp, **arguments;
38 setlocale(LC_ALL, "");
40 invo_name = r1bindex (argv[0], '/');
42 /* read user profile/context */
45 arguments = getarguments (invo_name, argc, argv, 1);
48 while ((cp = *argp++)) {
50 switch (smatch (++cp, switches)) {
52 ambigsw (cp, switches);
55 adios (NULL, "-%s unknown", cp);
58 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
60 print_help (buf, switches, 1);
63 print_version(invo_name);
74 if (*cp == '+' || *cp == '@') {
76 adios (NULL, "only one folder at a time!");
78 folder = pluspath (cp);
80 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
84 if (!context_find ("path"))
85 free (path ("./", TFOLDER));
87 folder = getfolder (1);
90 if (strcmp (m_mailpath (folder), pwd ()) == 0)
91 adios (NULL, "sorry, you can't remove the current working directory");
93 if (interactive == -1)
94 interactive = defolder;
96 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
97 for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
102 strncpy (newfolder, getfolder(0), sizeof(newfolder));
104 strncpy (newfolder, getfolder(0), sizeof(newfolder));
108 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
114 if (rmf (folder) == OK) {
115 char *cfolder = context_find(pfolder);
116 if (cfolder && strcmp (cfolder, newfolder)) {
117 printf ("[+%s now current]\n", newfolder);
118 context_replace (pfolder, newfolder); /* update current folder */
121 context_save (); /* save the context file */
130 register char *maildir;
132 register struct dirent *dp;
135 switch (i = chdir (maildir = m_maildir (folder))) {
137 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
138 break; /* fall otherwise */
141 snprintf (cur, sizeof(cur), "atr-%s-%s",
142 current, m_mailpath (folder));
143 if (!context_del (cur)) {
144 printf ("[+%s de-referenced]\n", folder);
147 advise (NULL, "you have no profile entry for the %s folder +%s",
148 i == NOTOK ? "unreadable" : "read-only", folder);
152 if ((dd = opendir (".")) == NULL)
153 adios (NULL, "unable to read folder +%s", folder);
157 * Run the external delete hook program.
160 (void)ext_hook("del-hook", maildir, (char *)0);
162 j = strlen(backup_prefix);
163 while ((dp = readdir (dd))) {
164 switch (dp->d_name[0]) {
166 if (strcmp (dp->d_name, ".") == 0
167 || strcmp (dp->d_name, "..") == 0)
168 continue; /* else fall */
181 if (m_atoi (dp->d_name))
183 if (strcmp (dp->d_name, altmsglink) == 0
184 || strncmp (dp->d_name, backup_prefix, j) == 0)
187 admonish (NULL, "file \"%s/%s\" not deleted",
192 if (unlink (dp->d_name) == NOTOK) {
193 admonish (dp->d_name, "unable to unlink %s:", folder);
201 * Remove any relevant private sequences
202 * or attributes from context file.
208 context_save(); /* Is this needed? meillo 2011-10 */
209 fflush(stdout); /* Is this needed? meillo 2011-10 */
210 if (rmdir(maildir) != -1) {
213 admonish (maildir, "unable to remove directory");
216 advise (NULL, "folder +%s not removed", folder);
222 * Remove all the (private) sequence information for
223 * this folder from the profile/context list.
229 register int alen, j, plen;
231 register struct node *np, *pp;
233 alen = strlen ("atr-");
234 plen = strlen (cp = m_mailpath (folder)) + 1;
237 * Search context list for keys that look like
238 * "atr-something-folderpath", and remove them.
240 for (np = m_defs, pp = NULL; np; np = np->n_next) {
241 if (ssequal ("atr-", np->n_name)
242 && (j = strlen (np->n_name) - plen) > alen
243 && *(np->n_name + j) == '-'
244 && strcmp (cp, np->n_name + j + 1) == 0) {
246 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
248 pp->n_next = np->n_next;