]> diplodocus.org Git - nmh/blob - uip/rmf.c
Cope with sasl_decode64() returning SASL_CONTINUE as well as SASL_OK.
[nmh] / 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 = pluspath (cp);
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) {
118 char *cfolder = context_find(pfolder);
119 if (cfolder && strcmp (cfolder, newfolder)) {
120 printf ("[+%s now current]\n", newfolder);
121 context_replace (pfolder, newfolder); /* update current folder */
122 }
123 }
124 context_save (); /* save the context file */
125 done (0);
126 return 1;
127 }
128
129 static int
130 rmf (char *folder)
131 {
132 int i, j, others;
133 register char *maildir;
134 char cur[BUFSIZ];
135 register struct dirent *dp;
136 register DIR *dd;
137
138 switch (i = chdir (maildir = m_maildir (folder))) {
139 case OK:
140 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
141 break; /* fall otherwise */
142
143 case NOTOK:
144 snprintf (cur, sizeof(cur), "atr-%s-%s",
145 current, m_mailpath (folder));
146 if (!context_del (cur)) {
147 printf ("[+%s de-referenced]\n", folder);
148 return OK;
149 }
150 advise (NULL, "you have no profile entry for the %s folder +%s",
151 i == NOTOK ? "unreadable" : "read-only", folder);
152 return NOTOK;
153 }
154
155 if ((dd = opendir (".")) == NULL)
156 adios (NULL, "unable to read folder +%s", folder);
157 others = 0;
158
159 /*
160 * Run the external delete hook program.
161 */
162
163 (void)ext_hook("del-hook", maildir, (char *)0);
164
165 j = strlen(BACKUP_PREFIX);
166 while ((dp = readdir (dd))) {
167 switch (dp->d_name[0]) {
168 case '.':
169 if (strcmp (dp->d_name, ".") == 0
170 || strcmp (dp->d_name, "..") == 0)
171 continue; /* else fall */
172
173 case ',':
174 #ifdef MHE
175 case '+':
176 #endif /* MHE */
177 #ifdef UCI
178 case '_':
179 case '#':
180 #endif /* UCI */
181 break;
182
183 default:
184 if (m_atoi (dp->d_name))
185 break;
186 if (strcmp (dp->d_name, LINK) == 0
187 || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
188 break;
189
190 admonish (NULL, "file \"%s/%s\" not deleted",
191 folder, dp->d_name);
192 others++;
193 continue;
194 }
195 if (unlink (dp->d_name) == NOTOK) {
196 admonish (dp->d_name, "unable to unlink %s:", folder);
197 others++;
198 }
199 }
200
201 closedir (dd);
202
203 /*
204 * Remove any relevant private sequences
205 * or attributes from context file.
206 */
207 rma (folder);
208
209 chdir ("..");
210 if (others == 0 && remdir (maildir))
211 return OK;
212
213 advise (NULL, "folder +%s not removed", folder);
214 return NOTOK;
215 }
216
217
218 /*
219 * Remove all the (private) sequence information for
220 * this folder from the profile/context list.
221 */
222
223 static void
224 rma (char *folder)
225 {
226 register int alen, j, plen;
227 register char *cp;
228 register struct node *np, *pp;
229
230 alen = strlen ("atr-");
231 plen = strlen (cp = m_mailpath (folder)) + 1;
232
233 /*
234 * Search context list for keys that look like
235 * "atr-something-folderpath", and remove them.
236 */
237 for (np = m_defs, pp = NULL; np; np = np->n_next) {
238 if (ssequal ("atr-", np->n_name)
239 && (j = strlen (np->n_name) - plen) > alen
240 && *(np->n_name + j) == '-'
241 && strcmp (cp, np->n_name + j + 1) == 0) {
242 if (!np->n_context)
243 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
244 if (pp) {
245 pp->n_next = np->n_next;
246 np = pp;
247 } else {
248 m_defs = np->n_next;
249 }
250 ctxflags |= CTXMOD;
251 } else {
252 pp = np;
253 }
254 }
255 }