]>
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/getarguments.h"
10 #include "sbr/read_yes_or_no_if_tty.h"
11 #include "sbr/concat.h"
12 #include "sbr/smatch.h"
13 #include "sbr/remdir.h"
14 #include "sbr/ssequal.h"
15 #include "sbr/m_atoi.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/ext_hook.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_del.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
29 #include "sbr/m_maildir.h"
30 #include "sbr/m_mktemp.h"
32 #define RMF_SWITCHES \
33 X("interactive", 0, INTRSW) \
34 X("nointeractive", 0, NINTRSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(RMF
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(RMF
, switches
);
49 static int rmf(char *);
50 static void rma (char *);
54 main (int argc
, char **argv
)
56 bool defolder
= false;
58 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
59 char buf
[BUFSIZ
], **argp
, **arguments
;
62 if (nmh_init(argv
[0], true, true)) { return 1; }
64 arguments
= getarguments (invo_name
, argc
, argv
, 1);
67 while ((cp
= *argp
++)) {
69 switch (smatch (++cp
, switches
)) {
71 ambigsw (cp
, switches
);
74 die("-%s unknown", cp
);
77 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
79 print_help (buf
, switches
, 1);
82 print_version(invo_name
);
93 if (*cp
== '+' || *cp
== '@') {
95 die("only one folder at a time!");
96 folder
= pluspath (cp
);
98 die("usage: %s [+folder] [switches]", invo_name
);
102 if (!context_find ("path"))
103 free (path ("./", TFOLDER
));
105 folder
= getfolder (1);
108 fp
= m_mailpath(folder
);
109 if (!strcmp(fp
, pwd()))
110 die("sorry, you can't remove the current working directory");
113 if (interactive
== -1)
114 interactive
= defolder
;
116 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
117 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
122 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
124 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
128 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
129 if (!read_yes_or_no_if_tty (cp
))
134 if (rmf (folder
) == OK
) {
135 char *cfolder
= context_find(pfolder
);
136 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
137 printf ("[+%s now current]\n", newfolder
);
138 context_replace (pfolder
, newfolder
); /* update current folder */
141 context_save (); /* save the context file */
157 switch (i
= chdir (maildir
= m_maildir (folder
))) {
159 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
164 fp
= m_mailpath(folder
);
165 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
167 if (!context_del (cur
)) {
168 printf ("[+%s de-referenced]\n", folder
);
171 inform("you have no profile entry for the %s folder +%s",
172 i
== NOTOK
? "unreadable" : "read-only", folder
);
176 if ((dd
= opendir (".")) == NULL
)
177 die("unable to read folder +%s", folder
);
181 * Run the external delete hook program.
184 (void)ext_hook("del-hook", maildir
, NULL
);
186 while ((dp
= readdir (dd
))) {
187 switch (dp
->d_name
[0]) {
189 if (strcmp (dp
->d_name
, ".") == 0
190 || strcmp (dp
->d_name
, "..") == 0)
198 if (m_atoi (dp
->d_name
))
200 if (strcmp (dp
->d_name
, LINK
) == 0
201 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
204 inform("file \"%s/%s\" not deleted, continuing...",
209 if (m_unlink (dp
->d_name
) == NOTOK
) {
210 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
218 * Remove any relevant private sequences
219 * or attributes from context file.
223 if (chdir ("..") < 0) {
224 advise ("..", "chdir");
226 if (!others
&& remdir (maildir
))
229 inform("folder +%s not removed", folder
);
235 * Remove all the (private) sequence information for
236 * this folder from the profile/context list.
244 struct node
*np
, *pp
;
247 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
250 * Search context list for keys that look like
251 * "atr-something-folderpath", and remove them.
253 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
254 if (ssequal ("atr-", np
->n_name
)
255 && (j
= strlen (np
->n_name
) - plen
) > alen
256 && *(np
->n_name
+ j
) == '-'
257 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
259 inform("bug: context_del(key=\"%s\"), continuing...",
262 pp
->n_next
= np
->n_next
;