2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
11 /* Mutt browser support routines */
20 #include <lib-lib/mem.h>
25 #include "lib/debug.h"
30 #include "imap_private.h"
32 /* -- forward declarations -- */
33 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
34 struct browser_state *state,
36 static void imap_add_folder (char delim, char *folder, int noselect,
37 int noinferiors, struct browser_state *state,
39 static int compare_names (struct folder_file *a, struct folder_file *b);
40 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
41 IMAP_NAMESPACE_INFO * nsi, int nsilen,
43 static int browse_verify_namespace (IMAP_DATA * idata,
44 IMAP_NAMESPACE_INFO * nsi, int nns);
46 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
47 * given a current folder to browse */
48 int imap_browse (char *path, struct browser_state *state)
51 char buf[LONG_STRING];
52 char buf2[LONG_STRING];
53 char nsbuf[LONG_STRING];
54 char mbox[LONG_STRING];
56 IMAP_NAMESPACE_INFO nsi[16];
57 int home_namespace = 0;
64 short showparents = 0;
70 if (imap_parse_path (path, &mx)) {
71 mutt_error (_("%s is an invalid IMAP path"), path);
75 save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
76 unset_option (OPTIMAPCHECKSUBSCRIBED);
77 strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST",
80 if (!(idata = imap_conn_find (&(mx.account), 0)))
85 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
86 mx.mbox = str_dup (ImapHomeNamespace);
87 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
88 mutt_message _("Getting namespaces...");
90 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
91 nsi, sizeof (nsi), &nns) != 0)
93 if (browse_verify_namespace (idata, nsi, nns) != 0)
98 mutt_message _("Getting folder list...");
100 /* skip check for parents when at the root */
101 if (mx.mbox && mx.mbox[0] != '\0') {
102 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
103 imap_munge_mbox_name (buf, sizeof (buf), mbox);
104 imap_unquote_string (buf); /* As kludgy as it gets */
105 mbox[sizeof (mbox) - 1] = '\0';
106 strncpy (mbox, buf, sizeof (mbox) - 1);
109 debug_print (3, ("mbox: %s\n", mbox));
111 /* if our target exists and has inferiors, enter it if we
112 * aren't already going to */
113 if (mbox[n - 1] != idata->delim) {
114 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
115 imap_cmd_start (idata, buf);
117 if (imap_parse_list_response (idata, &cur_folder, &noselect,
118 &noinferiors, &idata->delim) != 0)
122 imap_unmunge_mbox_name (cur_folder);
124 if (!noinferiors && cur_folder[0] &&
125 (n = str_len (mbox)) < LONG_STRING - 1) {
126 mbox[n++] = idata->delim;
131 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
134 /* if we're descending a folder, mark it as current in browser_state */
135 if (mbox[n - 1] == idata->delim) {
136 /* don't show parents in the home namespace */
139 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
140 state->folder = str_dup (buf);
144 /* Find superiors to list
145 * Note: UW-IMAP servers return folder + delimiter when asked to list
146 * folder + delimiter. Cyrus servers don't. So we ask for folder,
147 * and tack on delimiter ourselves.
148 * Further note: UW-IMAP servers return nothing when asked for
149 * NAMESPACES without delimiters at the end. Argh! */
150 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
151 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
152 /* forget the check, it is too delicate (see above). Have we ever
153 * had the parent not exist? */
158 debug_print (3, ("adding parent %s\n", mbox));
159 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
162 /* if our target isn't a folder, we are in our superior */
163 if (!state->folder) {
164 /* store folder with delimiter */
168 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
169 state->folder = str_dup (buf);
173 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
177 /* folder may be "/" */
178 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
180 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
181 if (!state->folder) {
182 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
183 state->folder = str_dup (buf);
188 /* no namespace, no folder: set folder to host only */
189 if (!state->folder) {
190 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
191 state->folder = str_dup (buf);
194 if (home_namespace && mbox[0] != '\0') {
195 /* Listing the home namespace, so INBOX should be included. Home
196 * namespace is not "", so we have to list it explicitly. We ask the
197 * server to see if it has descendants. */
198 debug_print (3, ("adding INBOX\n"));
199 if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
203 nsup = state->entrylen;
205 debug_print (3, ("Quoting mailbox scan: %s:\n", mbox));
206 snprintf (buf, sizeof (buf), "%s%%", mbox);
207 imap_quote_string (buf2, sizeof (buf2), buf);
208 debug_print (3, ("%s\n", buf2));
209 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
210 if (browse_add_list_result (idata, buf, state, 0))
213 if (!state->entrylen) {
214 mutt_error _("No such folder");
221 qsort (&(state->entry[nsup]), state->entrylen - nsup,
222 sizeof (state->entry[0]),
223 (int (*)(const void *, const void *)) compare_names);
224 if (home_namespace) { /* List additional namespaces */
225 for (i = 0; i < nns; i++)
226 if (nsi[i].listable && !nsi[i].home_namespace) {
227 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
228 nsi[i].noinferiors, state, 0);
229 debug_print (3, ("adding namespace: %s\n", nsi[i].prefix));
234 set_option (OPTIMAPCHECKSUBSCRIBED);
241 set_option (OPTIMAPCHECKSUBSCRIBED);
246 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
247 int imap_mailbox_create (const char *folder)
251 char buf[LONG_STRING];
254 if (imap_parse_path (folder, &mx) < 0) {
255 debug_print (1, ("Bad starting path %s\n", folder));
259 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
260 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
264 strfcpy (buf, NONULL (mx.mbox), sizeof (buf));
266 /* append a delimiter if necessary */
268 if (n && (n < sizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
269 buf[n++] = idata->delim;
273 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
276 if (!str_len (buf)) {
277 mutt_error (_("Mailbox must have a name."));
282 if (imap_create_mailbox (idata, buf) < 0)
285 mutt_message _("Mailbox created.");
297 int imap_mailbox_rename (const char *mailbox)
301 char buf[LONG_STRING];
302 char newname[SHORT_STRING];
304 if (imap_parse_path (mailbox, &mx) < 0) {
305 debug_print (1, ("Bad source mailbox %s\n", mailbox));
309 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
310 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
314 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
316 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
319 if (!str_len (newname)) {
320 mutt_error (_("Mailbox must have a name."));
325 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
326 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
331 mutt_message (_("Mailbox renamed."));
342 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
343 struct browser_state *state,
351 if (imap_parse_path (state->folder, &mx)) {
352 debug_print (2, ("current folder %s makes no sense\n", state->folder));
356 imap_cmd_start (idata, cmd);
359 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
360 &idata->delim) != 0) {
366 /* Let a parent folder never be selectable for navigation */
369 /* prune current folder from output */
370 if (isparent || str_ncmp (name, mx.mbox, str_len (name)))
371 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
375 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
381 /* imap_add_folder: add a folder name to the browser list, formatting it as
383 static void imap_add_folder (char delim, char *folder, int noselect,
384 int noinferiors, struct browser_state *state,
387 char tmp[LONG_STRING];
388 char relpath[LONG_STRING];
391 if (imap_parse_path (state->folder, &mx))
394 imap_unmunge_mbox_name (folder);
396 if (state->entrylen + 1 == state->entrymax) {
397 mem_realloc (&state->entry,
398 sizeof (struct folder_file) * (state->entrymax += 256));
399 memset (state->entry + state->entrylen, 0,
400 (sizeof (struct folder_file) *
401 (state->entrymax - state->entrylen)));
404 /* render superiors as unix-standard ".." */
406 strfcpy (relpath, "../", sizeof (relpath));
407 /* strip current folder from target, to render a relative path */
408 else if (!str_ncmp (mx.mbox, folder, str_len (mx.mbox)))
409 strfcpy (relpath, folder + str_len (mx.mbox), sizeof (relpath));
411 strfcpy (relpath, folder, sizeof (relpath));
413 /* apply filemask filter. This should really be done at menu setup rather
414 * than at scan, since it's so expensive to scan. But that's big changes
416 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
421 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
422 (state->entry)[state->entrylen].name = str_dup (tmp);
424 /* mark desc with delim in browser if it can have subfolders */
425 if (!isparent && !noinferiors && str_len (relpath) < sizeof (relpath) - 1) {
426 relpath[str_len (relpath) + 1] = '\0';
427 relpath[str_len (relpath)] = delim;
430 (state->entry)[state->entrylen].desc = str_dup (relpath);
432 (state->entry)[state->entrylen].imap = 1;
433 /* delimiter at the root is useless. */
434 if (folder[0] == '\0')
436 (state->entry)[state->entrylen].delim = delim;
437 (state->entry)[state->entrylen].selectable = !noselect;
438 (state->entry)[state->entrylen].inferiors = !noinferiors;
444 static int compare_names (struct folder_file *a, struct folder_file *b)
446 return str_cmp (a->name, b->name);
449 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
450 IMAP_NAMESPACE_INFO * nsi, int nsilen,
455 char ns[LONG_STRING];
462 nsbuf[nsblen - 1] = '\0';
464 imap_cmd_start (idata, "NAMESPACE");
467 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
470 s = imap_next_word (idata->cmd.buf);
471 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
472 /* There are three sections to the response, User, Other, Shared,
473 * and maybe more by extension */
474 for (type = IMAP_NS_PERSONAL; *s; type++) {
475 s = imap_next_word (s);
476 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
478 while (*s && *s != ')') {
486 while (*s && *s != '\"') {
496 while (*s && !ISSPACE (*s)) {
502 s = imap_next_word (s);
503 /* delimiter is meaningless if namespace is "". Why does
504 * Cyrus provide one?! */
505 if (n && *s && *s == '\"') {
506 if (s[1] && s[2] == '\"')
508 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
511 /* skip "" namespaces, they are already listed at the root */
512 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
513 debug_print (3, ("adding %s\n", ns));
515 /* Cyrus doesn't append the delimiter to the namespace,
516 * but UW-IMAP does. We'll strip it here and add it back
517 * as if it were a normal directory, from the browser */
518 if (n && (ns[n - 1] == delim))
520 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
521 nsi->prefix = nsbuf + nsbused;
527 while (*s && *s != ')')
536 while (rc == IMAP_CMD_CONTINUE);
538 if (rc != IMAP_CMD_OK)
544 /* Check which namespaces have contents */
545 static int browse_verify_namespace (IMAP_DATA * idata,
546 IMAP_NAMESPACE_INFO * nsi, int nns)
548 char buf[LONG_STRING];
553 for (i = 0; i < nns; i++, nsi++) {
554 /* Cyrus gives back nothing if the % isn't added. This may return lots
555 * of data in some cases, I guess, but I currently feel that's better
556 * than invisible namespaces */
558 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
559 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix,
562 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
563 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix);
565 imap_cmd_start (idata, buf);
568 nsi->home_namespace = 0;
570 if (imap_parse_list_response (idata, &name, &nsi->noselect,
571 &nsi->noinferiors, &delim) != 0)
573 nsi->listable |= (name != NULL);
575 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));