]>
diplodocus.org Git - nmh/blob - uip/rmf.c
1e7f90c07eacf92e61dff1896d91ab7c55cd2477
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/read_yes_or_no_if_tty.h"
10 #include "sbr/concat.h"
11 #include "sbr/smatch.h"
12 #include "sbr/remdir.h"
13 #include "sbr/ssequal.h"
14 #include "sbr/m_atoi.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/ext_hook.h"
17 #include "sbr/context_save.h"
18 #include "sbr/context_replace.h"
19 #include "sbr/context_del.h"
20 #include "sbr/context_find.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
28 #include "sbr/m_maildir.h"
29 #include "sbr/m_mktemp.h"
31 #define RMF_SWITCHES \
32 X("interactive", 0, INTRSW) \
33 X("nointeractive", 0, NINTRSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(RMF
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(RMF
, switches
);
48 static int rmf(char *);
49 static void rma (char *);
53 main (int argc
, char **argv
)
55 bool defolder
= false;
57 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
58 char buf
[BUFSIZ
], **argp
, **arguments
;
61 if (nmh_init(argv
[0], true, true)) { return 1; }
63 arguments
= getarguments (invo_name
, argc
, argv
, 1);
66 while ((cp
= *argp
++)) {
68 switch (smatch (++cp
, switches
)) {
70 ambigsw (cp
, switches
);
73 die("-%s unknown", cp
);
76 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
78 print_help (buf
, switches
, 1);
81 print_version(invo_name
);
92 if (*cp
== '+' || *cp
== '@') {
94 die("only one folder at a time!");
95 folder
= pluspath (cp
);
97 die("usage: %s [+folder] [switches]", invo_name
);
101 if (!context_find ("path"))
102 free (path ("./", TFOLDER
));
104 folder
= getfolder (1);
107 fp
= m_mailpath(folder
);
108 if (!strcmp(fp
, pwd()))
109 die("sorry, you can't remove the current working directory");
112 if (interactive
== -1)
113 interactive
= defolder
;
115 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
116 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
121 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
123 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
127 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
128 if (!read_yes_or_no_if_tty (cp
))
133 if (rmf (folder
) == OK
) {
134 char *cfolder
= context_find(pfolder
);
135 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
136 printf ("[+%s now current]\n", newfolder
);
137 context_replace (pfolder
, newfolder
); /* update current folder */
140 context_save (); /* save the context file */
156 switch (i
= chdir (maildir
= m_maildir (folder
))) {
158 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
163 fp
= m_mailpath(folder
);
164 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
166 if (!context_del (cur
)) {
167 printf ("[+%s de-referenced]\n", folder
);
170 inform("you have no profile entry for the %s folder +%s",
171 i
== NOTOK
? "unreadable" : "read-only", folder
);
175 if ((dd
= opendir (".")) == NULL
)
176 die("unable to read folder +%s", folder
);
180 * Run the external delete hook program.
183 (void)ext_hook("del-hook", maildir
, NULL
);
185 while ((dp
= readdir (dd
))) {
186 switch (dp
->d_name
[0]) {
188 if (strcmp (dp
->d_name
, ".") == 0
189 || strcmp (dp
->d_name
, "..") == 0)
197 if (m_atoi (dp
->d_name
))
199 if (strcmp (dp
->d_name
, LINK
) == 0
200 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
203 inform("file \"%s/%s\" not deleted, continuing...",
208 if (m_unlink (dp
->d_name
) == NOTOK
) {
209 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
217 * Remove any relevant private sequences
218 * or attributes from context file.
222 if (chdir ("..") < 0) {
223 advise ("..", "chdir");
225 if (!others
&& remdir (maildir
))
228 inform("folder +%s not removed", folder
);
234 * Remove all the (private) sequence information for
235 * this folder from the profile/context list.
243 struct node
*np
, *pp
;
246 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
249 * Search context list for keys that look like
250 * "atr-something-folderpath", and remove them.
252 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
253 if (ssequal ("atr-", np
->n_name
)
254 && (j
= strlen (np
->n_name
) - plen
) > alen
255 && *(np
->n_name
+ j
) == '-'
256 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
258 inform("bug: context_del(key=\"%s\"), continuing...",
261 pp
->n_next
= np
->n_next
;