1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Folder related user commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 * SPDX-License-Identifier: BSD-3-Clause
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #define n_FILE cmd_folder
39 #ifndef HAVE_AMALGAMATION
44 static int _c_file(void *v
, enum fedit_mode fm
);
47 _c_file(void *v
, enum fedit_mode fm
)
54 n_folder_announce(n_ANNOUNCE_STATUS
);
59 if (n_pstate
& n_PS_HOOK_MASK
) {
60 n_err(_("Cannot change folder from within a hook\n"));
65 save_mbox_for_possible_quitstuff();
67 i
= setfile(*argv
, fm
);
72 assert(!(fm
& FEDIT_NEWMAIL
)); /* (Prevent implementation error) */
73 if (n_pstate
& n_PS_SETFILE_OPENED
)
74 temporary_folder_hook_check(FAL0
);
77 /* TODO Don't report "no messages" == 1 == error when we're in, e.g.,
78 * TODO a macro: because that recursed commando loop will terminate the
79 * TODO entire macro due to that! So either the user needs to be able
80 * TODO to react&ignore this "error" (as in "if DOSTUFF" or "DOSTUFF;
81 * TODO if $?", then "overriding an "error"), or we need a different
82 * TODO return that differentiates */
83 i
= (n_pstate
& n_PS_ROBOT
) ? 0 : 1;
87 if(n_pstate
& n_PS_SETFILE_OPENED
)
88 n_folder_announce(n_ANNOUNCE_CHANGE
);
101 rv
= _c_file(v
, FEDIT_NONE
);
112 rv
= _c_file(v
, FEDIT_RDONLY
);
124 if (n_pstate
& n_PS_HOOK_MASK
)
125 n_err(_("Cannot call `newmail' from within a hook\n"));
127 else if(mb
.mb_type
== MB_IMAP
&& !imap_newmail(1))
130 else if ((val
= setfile(mailname
,
131 FEDIT_NEWMAIL
| ((mb
.mb_perm
& MB_DELE
) ? 0 : FEDIT_RDONLY
))
134 setdot(message
+ mdot
- 1);
147 switch (mb
.mb_type
) {
170 char **args
, *name
, *ename
;
174 if (*(args
= v
) == NULL
) {
175 n_err(_("Synopsis: remove: <mailbox>...\n"));
182 fmt
= _("Remove %s");
183 fmt_len
= strlen(fmt
);
185 if ((name
= fexpand(*args
, FEXP_FULL
)) == NULL
)
187 ename
= n_shexp_quote_cp(name
, FAL0
);
189 if (!strcmp(name
, mailname
)) {
190 n_err(_("Cannot remove current mailbox %s\n"), ename
);
199 vl
= strlen(ename
) + fmt_len
+1;
200 vb
= n_autorec_alloc(vl
);
201 snprintf(vb
, vl
, fmt
, ename
);
202 asw
= getapproval(vb
, TRU1
);
207 switch (which_protocol(name
, TRU1
, FAL0
, NULL
)) {
209 if (unlink(name
) == -1) {
212 if (se
== n_ERR_ISDIR
) {
215 if (!stat(name
, &sb
) && S_ISDIR(sb
.st_mode
)) {
226 n_err(_("Cannot remove POP3 mailbox %s\n"), ename
);
231 if(maildir_remove(name
) != OKAY
)
234 n_err(_("No Maildir directory support compiled in\n"));
240 if(imap_remove(name
) != OKAY
)
243 n_err(_("No IMAP support compiled in\n"));
249 n_err(_("Not removed: unknown protocol: %s\n"), ename
);
253 } while (*++args
!= NULL
);
262 char **args
= v
, *oldn
, *newn
;
269 if (args
[0] == NULL
|| args
[1] == NULL
|| args
[2] != NULL
) {
270 n_err(_("Synopsis: rename: <old> <new>\n"));
274 if ((oldn
= fexpand(args
[0], FEXP_FULL
)) == NULL
)
276 oldp
= which_protocol(oldn
, TRU1
, FAL0
, NULL
);
277 if ((newn
= fexpand(args
[1], FEXP_FULL
)) == NULL
)
279 if(oldp
!= which_protocol(newn
, TRU1
, FAL0
, NULL
)) {
280 n_err(_("Can only rename folders of same type\n"));
283 if (!strcmp(oldn
, mailname
) || !strcmp(newn
, mailname
)) {
284 n_err(_("Cannot rename current mailbox %s\n"),
285 n_shexp_quote_cp(oldn
, FAL0
));
293 if (link(oldn
, newn
) == -1) {
297 case n_ERR_NAMETOOLONG
:
307 } else if (unlink(oldn
) == -1) {
314 if(rename(oldn
, newn
) == -1){
319 n_err(_("No Maildir directory support compiled in\n"));
324 n_err(_("Cannot rename POP3 mailboxes\n"));
329 if(imap_rename(oldn
, newn
) != OKAY
)
332 n_err(_("No IMAP support compiled in\n"));
338 n_err(_("Unknown protocol in %s and %s; not renamed\n"),
339 n_shexp_quote_cp(oldn
, FAL0
), n_shexp_quote_cp(newn
, FAL0
));
349 c_folders(void *v
){ /* TODO fexpand*/
350 enum fexp_mode
const fexp
= FEXP_NSHELL
363 if(*(argv
= v
) != NULL
){
364 if((cp
= fexpand(*argv
, fexp
)) == NULL
)
367 cp
= n_folder_query();
370 if(which_protocol(cp
, FAL0
, FAL0
, NULL
) == PROTO_IMAP
)
371 rv
= imap_folders(cp
, *argv
== NULL
);
374 rv
= n_child_run(ok_vlook(LISTER
), 0, n_CHILD_FD_PASS
, n_CHILD_FD_PASS
,
375 cp
, NULL
, NULL
, NULL
, NULL
);