]>
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/smatch.h"
10 #include "sbr/remdir.h"
11 #include "sbr/ssequal.h"
12 #include "sbr/m_atoi.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/ext_hook.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_del.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/m_mktemp.h"
29 #define RMF_SWITCHES \
30 X("interactive", 0, INTRSW) \
31 X("nointeractive", 0, NINTRSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(RMF
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(RMF
, switches
);
46 static int rmf(char *);
47 static void rma (char *);
51 main (int argc
, char **argv
)
53 bool defolder
= false;
55 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
56 char buf
[BUFSIZ
], **argp
, **arguments
;
59 if (nmh_init(argv
[0], true, true)) { return 1; }
61 arguments
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
71 die("-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
90 if (*cp
== '+' || *cp
== '@') {
92 die("only one folder at a time!");
93 folder
= pluspath (cp
);
95 die("usage: %s [+folder] [switches]", invo_name
);
99 if (!context_find ("path"))
100 free (path ("./", TFOLDER
));
102 folder
= getfolder (1);
105 fp
= m_mailpath(folder
);
106 if (!strcmp(fp
, pwd()))
107 die("sorry, you can't remove the current working directory");
110 if (interactive
== -1)
111 interactive
= defolder
;
113 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
114 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
119 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
121 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
125 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
126 if (!read_yes_or_no_if_tty (cp
))
131 if (rmf (folder
) == OK
) {
132 char *cfolder
= context_find(pfolder
);
133 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
134 printf ("[+%s now current]\n", newfolder
);
135 context_replace (pfolder
, newfolder
); /* update current folder */
138 context_save (); /* save the context file */
154 switch (i
= chdir (maildir
= m_maildir (folder
))) {
156 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
161 fp
= m_mailpath(folder
);
162 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
164 if (!context_del (cur
)) {
165 printf ("[+%s de-referenced]\n", folder
);
168 inform("you have no profile entry for the %s folder +%s",
169 i
== NOTOK
? "unreadable" : "read-only", folder
);
173 if ((dd
= opendir (".")) == NULL
)
174 die("unable to read folder +%s", folder
);
178 * Run the external delete hook program.
181 (void)ext_hook("del-hook", maildir
, NULL
);
183 while ((dp
= readdir (dd
))) {
184 switch (dp
->d_name
[0]) {
186 if (strcmp (dp
->d_name
, ".") == 0
187 || strcmp (dp
->d_name
, "..") == 0)
195 if (m_atoi (dp
->d_name
))
197 if (strcmp (dp
->d_name
, LINK
) == 0
198 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
201 inform("file \"%s/%s\" not deleted, continuing...",
206 if (m_unlink (dp
->d_name
) == NOTOK
) {
207 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
215 * Remove any relevant private sequences
216 * or attributes from context file.
220 if (chdir ("..") < 0) {
221 advise ("..", "chdir");
223 if (!others
&& remdir (maildir
))
226 inform("folder +%s not removed", folder
);
232 * Remove all the (private) sequence information for
233 * this folder from the profile/context list.
241 struct node
*np
, *pp
;
244 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
247 * Search context list for keys that look like
248 * "atr-something-folderpath", and remove them.
250 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
251 if (ssequal ("atr-", np
->n_name
)
252 && (j
= strlen (np
->n_name
) - plen
) > alen
253 && *(np
->n_name
+ j
) == '-'
254 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
256 inform("bug: context_del(key=\"%s\"), continuing...",
259 pp
->n_next
= np
->n_next
;