]>
diplodocus.org Git - nmh/blob - uip/rmf.c
1 /* rmf.c -- remove a folder
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/m_atoi.h"
10 #include "sbr/getfolder.h"
11 #include "sbr/ext_hook.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_replace.h"
14 #include "sbr/context_del.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/error.h"
23 #include "sbr/m_maildir.h"
24 #include "sbr/m_mktemp.h"
26 #define RMF_SWITCHES \
27 X("interactive", 0, INTRSW) \
28 X("nointeractive", 0, NINTRSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(RMF
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(RMF
, switches
);
43 static int rmf(char *);
44 static void rma (char *);
48 main (int argc
, char **argv
)
50 bool defolder
= false;
52 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
53 char buf
[BUFSIZ
], **argp
, **arguments
;
56 if (nmh_init(argv
[0], true, true)) { return 1; }
58 arguments
= getarguments (invo_name
, argc
, argv
, 1);
61 while ((cp
= *argp
++)) {
63 switch (smatch (++cp
, switches
)) {
65 ambigsw (cp
, switches
);
68 die("-%s unknown", cp
);
71 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
73 print_help (buf
, switches
, 1);
76 print_version(invo_name
);
87 if (*cp
== '+' || *cp
== '@') {
89 die("only one folder at a time!");
90 folder
= pluspath (cp
);
92 die("usage: %s [+folder] [switches]", invo_name
);
96 if (!context_find ("path"))
97 free (path ("./", TFOLDER
));
99 folder
= getfolder (1);
102 fp
= m_mailpath(folder
);
103 if (!strcmp(fp
, pwd()))
104 die("sorry, you can't remove the current working directory");
107 if (interactive
== -1)
108 interactive
= defolder
;
110 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
111 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
116 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
118 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
122 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
123 if (!read_yes_or_no_if_tty (cp
))
128 if (rmf (folder
) == OK
) {
129 char *cfolder
= context_find(pfolder
);
130 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
131 printf ("[+%s now current]\n", newfolder
);
132 context_replace (pfolder
, newfolder
); /* update current folder */
135 context_save (); /* save the context file */
151 switch (i
= chdir (maildir
= m_maildir (folder
))) {
153 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
158 fp
= m_mailpath(folder
);
159 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
161 if (!context_del (cur
)) {
162 printf ("[+%s de-referenced]\n", folder
);
165 inform("you have no profile entry for the %s folder +%s",
166 i
== NOTOK
? "unreadable" : "read-only", folder
);
170 if ((dd
= opendir (".")) == NULL
)
171 die("unable to read folder +%s", folder
);
175 * Run the external delete hook program.
178 (void)ext_hook("del-hook", maildir
, NULL
);
180 while ((dp
= readdir (dd
))) {
181 switch (dp
->d_name
[0]) {
183 if (strcmp (dp
->d_name
, ".") == 0
184 || strcmp (dp
->d_name
, "..") == 0)
192 if (m_atoi (dp
->d_name
))
194 if (strcmp (dp
->d_name
, LINK
) == 0
195 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
198 inform("file \"%s/%s\" not deleted, continuing...",
203 if (m_unlink (dp
->d_name
) == NOTOK
) {
204 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
212 * Remove any relevant private sequences
213 * or attributes from context file.
217 if (chdir ("..") < 0) {
218 advise ("..", "chdir");
220 if (!others
&& remdir (maildir
))
223 inform("folder +%s not removed", folder
);
229 * Remove all the (private) sequence information for
230 * this folder from the profile/context list.
238 struct node
*np
, *pp
;
241 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
244 * Search context list for keys that look like
245 * "atr-something-folderpath", and remove them.
247 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
248 if (ssequal ("atr-", np
->n_name
)
249 && (j
= strlen (np
->n_name
) - plen
) > alen
250 && *(np
->n_name
+ j
) == '-'
251 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
253 inform("bug: context_del(key=\"%s\"), continuing...",
256 pp
->n_next
= np
->n_next
;