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>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #define n_FILE cmd_folder
38 #ifndef HAVE_AMALGAMATION
43 static int _c_file(void *v
, enum fedit_mode fm
);
46 _c_file(void *v
, enum fedit_mode fm
)
53 n_folder_announce(n_ANNOUNCE_STATUS
);
58 if (n_pstate
& n_PS_HOOK_MASK
) {
59 n_err(_("Cannot change folder from within a hook\n"));
64 save_mbox_for_possible_quitstuff();
66 i
= setfile(*argv
, fm
);
71 assert(!(fm
& FEDIT_NEWMAIL
)); /* (Prevent implementation error) */
72 if (n_pstate
& n_PS_SETFILE_OPENED
)
73 temporary_folder_hook_check(FAL0
);
76 /* TODO Don't report "no messages" == 1 == error when we're in, e.g.,
77 * TODO a macro: because that recursed commando loop will terminate the
78 * TODO entire macro due to that! So either the user needs to be able
79 * TODO to react&ignore this "error" (as in "if DOSTUFF" or "DOSTUFF;
80 * TODO if $?", then "overriding an "error"), or we need a different
81 * TODO return that differentiates */
82 i
= (n_pstate
& n_PS_ROBOT
) ? 0 : 1;
86 if(n_pstate
& n_PS_SETFILE_OPENED
)
87 n_folder_announce(n_ANNOUNCE_CHANGE
);
100 rv
= _c_file(v
, FEDIT_NONE
);
111 rv
= _c_file(v
, FEDIT_RDONLY
);
123 if (n_pstate
& n_PS_HOOK_MASK
)
124 n_err(_("Cannot call `newmail' from within a hook\n"));
126 else if(mb
.mb_type
== MB_IMAP
&& !imap_newmail(1))
129 else if ((val
= setfile(mailname
,
130 FEDIT_NEWMAIL
| ((mb
.mb_perm
& MB_DELE
) ? 0 : FEDIT_RDONLY
))
133 setdot(message
+ mdot
- 1);
146 switch (mb
.mb_type
) {
169 char **args
, *name
, *ename
;
173 if (*(args
= v
) == NULL
) {
174 n_err(_("Synopsis: remove: <mailbox>...\n"));
181 fmt
= _("Remove %s");
182 fmt_len
= strlen(fmt
);
184 if ((name
= fexpand(*args
, FEXP_FULL
)) == NULL
)
186 ename
= n_shexp_quote_cp(name
, FAL0
);
188 if (!strcmp(name
, mailname
)) {
189 n_err(_("Cannot remove current mailbox %s\n"), ename
);
198 vl
= strlen(ename
) + fmt_len
+1;
200 snprintf(vb
, vl
, fmt
, ename
);
201 asw
= getapproval(vb
, TRU1
);
206 switch (which_protocol(name
, TRU1
, FAL0
, NULL
)) {
208 if (unlink(name
) == -1) {
211 if (se
== n_ERR_ISDIR
) {
214 if (!stat(name
, &sb
) && S_ISDIR(sb
.st_mode
)) {
225 n_err(_("Cannot remove POP3 mailbox %s\n"), ename
);
229 if (maildir_remove(name
) != OKAY
)
234 if(imap_remove(name
) != OKAY
)
237 n_err(_("No IMAP support compiled in\n"));
243 n_err(_("Not removed: unknown protocol: %s\n"), ename
);
247 } while (*++args
!= NULL
);
256 char **args
= v
, *oldn
, *newn
;
263 if (args
[0] == NULL
|| args
[1] == NULL
|| args
[2] != NULL
) {
264 n_err(_("Synopsis: rename: <old> <new>\n"));
268 if ((oldn
= fexpand(args
[0], FEXP_FULL
)) == NULL
)
270 oldp
= which_protocol(oldn
, TRU1
, FAL0
, NULL
);
271 if ((newn
= fexpand(args
[1], FEXP_FULL
)) == NULL
)
273 if(oldp
!= which_protocol(newn
, TRU1
, FAL0
, NULL
)) {
274 n_err(_("Can only rename folders of same type\n"));
277 if (!strcmp(oldn
, mailname
) || !strcmp(newn
, mailname
)) {
278 n_err(_("Cannot rename current mailbox %s\n"),
279 n_shexp_quote_cp(oldn
, FAL0
));
287 if (link(oldn
, newn
) == -1) {
291 case n_ERR_NAMETOOLONG
:
301 } else if (unlink(oldn
) == -1) {
307 if (rename(oldn
, newn
) == -1) {
313 n_err(_("Cannot rename POP3 mailboxes\n"));
318 if(imap_rename(oldn
, newn
) != OKAY
)
321 n_err(_("No IMAP support compiled in\n"));
327 n_err(_("Unknown protocol in %s and %s; not renamed\n"),
328 n_shexp_quote_cp(oldn
, FAL0
), n_shexp_quote_cp(newn
, FAL0
));
338 c_folders(void *v
){ /* TODO fexpand*/
339 enum fexp_mode
const fexp
= FEXP_NSHELL
352 if(*(argv
= v
) != NULL
){
353 if((cp
= fexpand(*argv
, fexp
)) == NULL
)
356 cp
= n_folder_query();
359 if(which_protocol(cp
, FAL0
, FAL0
, NULL
) == PROTO_IMAP
)
360 rv
= imap_folders(cp
, *argv
== NULL
);
363 rv
= n_child_run(ok_vlook(LISTER
), 0, n_CHILD_FD_PASS
, n_CHILD_FD_PASS
,
364 cp
, NULL
, NULL
, NULL
, NULL
);