]> diplodocus.org Git - nmh/blob - uip/rmf.c
Use pref_encoding() to select the default encoding for all headers.
[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 #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 (0);
65 case VERSIONSW:
66 print_version(invo_name);
67 done (0);
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 break;
175
176 default:
177 if (m_atoi (dp->d_name))
178 break;
179 if (strcmp (dp->d_name, LINK) == 0
180 || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
181 break;
182
183 admonish (NULL, "file \"%s/%s\" not deleted",
184 folder, dp->d_name);
185 others++;
186 continue;
187 }
188 if (unlink (dp->d_name) == NOTOK) {
189 admonish (dp->d_name, "unable to unlink %s:", folder);
190 others++;
191 }
192 }
193
194 closedir (dd);
195
196 /*
197 * Remove any relevant private sequences
198 * or attributes from context file.
199 */
200 rma (folder);
201
202 chdir ("..");
203 if (others == 0 && remdir (maildir))
204 return OK;
205
206 advise (NULL, "folder +%s not removed", folder);
207 return NOTOK;
208 }
209
210
211 /*
212 * Remove all the (private) sequence information for
213 * this folder from the profile/context list.
214 */
215
216 static void
217 rma (char *folder)
218 {
219 register int alen, j, plen;
220 register char *cp;
221 register struct node *np, *pp;
222
223 alen = strlen ("atr-");
224 plen = strlen (cp = m_mailpath (folder)) + 1;
225
226 /*
227 * Search context list for keys that look like
228 * "atr-something-folderpath", and remove them.
229 */
230 for (np = m_defs, pp = NULL; np; np = np->n_next) {
231 if (ssequal ("atr-", np->n_name)
232 && (j = strlen (np->n_name) - plen) > alen
233 && *(np->n_name + j) == '-'
234 && strcmp (cp, np->n_name + j + 1) == 0) {
235 if (!np->n_context)
236 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
237 if (pp) {
238 pp->n_next = np->n_next;
239 np = pp;
240 } else {
241 m_defs = np->n_next;
242 }
243 ctxflags |= CTXMOD;
244 } else {
245 pp = np;
246 }
247 }
248 }