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