]> diplodocus.org Git - nmh/blob - uip/rmf.c
Added context_find_prefix().
[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
12 #define RMF_SWITCHES \
13 X("interactive", 0, INTRSW) \
14 X("nointeractive", 0, NINTRSW) \
15 X("version", 0, VERSIONSW) \
16 X("help", 0, HELPSW) \
17
18 #define X(sw, minchars, id) id,
19 DEFINE_SWITCH_ENUM(RMF);
20 #undef X
21
22 #define X(sw, minchars, id) { sw, minchars, id },
23 DEFINE_SWITCH_ARRAY(RMF, switches);
24 #undef X
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 if (nmh_init(argv[0], 1)) { return 1; }
41
42 arguments = getarguments (invo_name, argc, argv, 1);
43 argp = arguments;
44
45 while ((cp = *argp++)) {
46 if (*cp == '-') {
47 switch (smatch (++cp, switches)) {
48 case AMBIGSW:
49 ambigsw (cp, switches);
50 done (1);
51 case UNKWNSW:
52 adios (NULL, "-%s unknown", cp);
53
54 case HELPSW:
55 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
56 invo_name);
57 print_help (buf, switches, 1);
58 done (0);
59 case VERSIONSW:
60 print_version(invo_name);
61 done (0);
62
63 case INTRSW:
64 interactive = 1;
65 continue;
66 case NINTRSW:
67 interactive = 0;
68 continue;
69 }
70 }
71 if (*cp == '+' || *cp == '@') {
72 if (folder)
73 adios (NULL, "only one folder at a time!");
74 else
75 folder = pluspath (cp);
76 } else {
77 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
78 }
79 }
80
81 if (!context_find ("path"))
82 free (path ("./", TFOLDER));
83 if (!folder) {
84 folder = getfolder (1);
85 defolder++;
86 }
87 if (strcmp (m_mailpath (folder), pwd ()) == 0)
88 adios (NULL, "sorry, you can't remove the current working directory");
89
90 if (interactive == -1)
91 interactive = defolder;
92
93 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
94 for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
95 continue;
96 if (cp > newfolder)
97 *cp = '\0';
98 else
99 strncpy (newfolder, getfolder(0), sizeof(newfolder));
100 } else {
101 strncpy (newfolder, getfolder(0), sizeof(newfolder));
102 }
103
104 if (interactive) {
105 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
106 if (!read_yes_or_no_if_tty (cp))
107 done (0);
108 free (cp);
109 }
110
111 if (rmf (folder) == OK) {
112 char *cfolder = context_find(pfolder);
113 if (cfolder && strcmp (cfolder, newfolder)) {
114 printf ("[+%s now current]\n", newfolder);
115 context_replace (pfolder, newfolder); /* update current folder */
116 }
117 }
118 context_save (); /* save the context file */
119 done (0);
120 return 1;
121 }
122
123 static int
124 rmf (char *folder)
125 {
126 int i, j, others;
127 register char *maildir;
128 char cur[BUFSIZ];
129 register struct dirent *dp;
130 register DIR *dd;
131
132 switch (i = chdir (maildir = m_maildir (folder))) {
133 case OK:
134 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
135 break; /* fall otherwise */
136
137 case NOTOK:
138 snprintf (cur, sizeof(cur), "atr-%s-%s",
139 current, m_mailpath (folder));
140 if (!context_del (cur)) {
141 printf ("[+%s de-referenced]\n", folder);
142 return OK;
143 }
144 advise (NULL, "you have no profile entry for the %s folder +%s",
145 i == NOTOK ? "unreadable" : "read-only", folder);
146 return NOTOK;
147 }
148
149 if ((dd = opendir (".")) == NULL)
150 adios (NULL, "unable to read folder +%s", folder);
151 others = 0;
152
153 /*
154 * Run the external delete hook program.
155 */
156
157 (void)ext_hook("del-hook", maildir, (char *)0);
158
159 j = strlen(BACKUP_PREFIX);
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 || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
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 register int alen, j, plen;
216 register char *cp;
217 register struct node *np, *pp;
218
219 alen = strlen ("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 }