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 */
13 #include <lib-lib/lib-lib.h>
15 #include <lib-ui/enter.h>
18 #include "imap_private.h"
20 /* -- forward declarations -- */
21 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
22 struct browser_state *state,
24 static void imap_add_folder (char delim, char *folder, int noselect,
25 int noinferiors, struct browser_state *state,
27 static int compare_names (struct folder_file *a, struct folder_file *b);
28 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
29 IMAP_NAMESPACE_INFO * nsi, int nsilen,
31 static int browse_verify_namespace (IMAP_DATA * idata,
32 IMAP_NAMESPACE_INFO * nsi, int nns);
34 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
35 * given a current folder to browse */
36 int imap_browse (char *path, struct browser_state *state)
39 char buf[LONG_STRING];
40 char buf2[LONG_STRING];
41 char nsbuf[LONG_STRING];
42 char mbox[LONG_STRING];
44 IMAP_NAMESPACE_INFO nsi[16];
45 int home_namespace = 0;
52 short showparents = 0;
58 if (imap_parse_path (path, &mx)) {
59 mutt_error (_("%s is an invalid IMAP path"), path);
63 save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
64 unset_option (OPTIMAPCHECKSUBSCRIBED);
65 m_strcpy(list_cmd, sizeof(list_cmd),
66 option(OPTIMAPLSUB) ? "LSUB" : "string_list_t");
68 if (!(idata = imap_conn_find (&(mx.account), 0)))
73 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
74 mx.mbox = m_strdup(ImapHomeNamespace);
75 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
76 mutt_message _("Getting namespaces...");
78 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
79 nsi, sizeof (nsi), &nns) != 0)
81 if (browse_verify_namespace (idata, nsi, nns) != 0)
86 mutt_message _("Getting folder list...");
88 /* skip check for parents when at the root */
89 if (mx.mbox && mx.mbox[0] != '\0') {
90 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
91 imap_munge_mbox_name (buf, sizeof (buf), mbox);
92 imap_unquote_string (buf); /* As kludgy as it gets */
93 m_strcpy(mbox, sizeof(mbox), buf);
96 /* if our target exists and has inferiors, enter it if we
97 * aren't already going to */
98 if (mbox[n - 1] != idata->delim) {
99 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
100 imap_cmd_start (idata, buf);
102 if (imap_parse_list_response (idata, &cur_folder, &noselect,
103 &noinferiors, &idata->delim) != 0)
107 imap_unmunge_mbox_name (cur_folder);
109 if (!noinferiors && cur_folder[0] &&
110 (n = m_strlen(mbox)) < LONG_STRING - 1) {
111 mbox[n++] = idata->delim;
116 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
119 /* if we're descending a folder, mark it as current in browser_state */
120 if (mbox[n - 1] == idata->delim) {
121 /* don't show parents in the home namespace */
124 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
125 state->folder = m_strdup(buf);
129 /* Find superiors to list
130 * Note: UW-IMAP servers return folder + delimiter when asked to list
131 * folder + delimiter. Cyrus servers don't. So we ask for folder,
132 * and tack on delimiter ourselves.
133 * Further note: UW-IMAP servers return nothing when asked for
134 * NAMESPACES without delimiters at the end. Argh! */
135 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
136 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
137 /* forget the check, it is too delicate (see above). Have we ever
138 * had the parent not exist? */
143 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
146 /* if our target isn't a folder, we are in our superior */
147 if (!state->folder) {
148 /* store folder with delimiter */
152 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
153 state->folder = m_strdup(buf);
157 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
161 /* folder may be "/" */
162 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
164 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
165 if (!state->folder) {
166 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
167 state->folder = m_strdup(buf);
172 /* no namespace, no folder: set folder to host only */
173 if (!state->folder) {
174 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
175 state->folder = m_strdup(buf);
178 if (home_namespace && mbox[0] != '\0') {
179 /* Listing the home namespace, so INBOX should be included. Home
180 * namespace is not "", so we have to list it explicitly. We ask the
181 * server to see if it has descendants. */
182 if (browse_add_list_result (idata, "string_list_t \"\" \"INBOX\"", state, 0))
186 nsup = state->entrylen;
188 snprintf (buf, sizeof (buf), "%s%%", mbox);
189 imap_quote_string (buf2, sizeof (buf2), buf);
190 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
191 if (browse_add_list_result (idata, buf, state, 0))
194 if (!state->entrylen) {
195 mutt_error _("No such folder");
202 qsort (&(state->entry[nsup]), state->entrylen - nsup,
203 sizeof (state->entry[0]),
204 (int (*)(const void *, const void *)) compare_names);
205 if (home_namespace) { /* List additional namespaces */
206 for (i = 0; i < nns; i++)
207 if (nsi[i].listable && !nsi[i].home_namespace) {
208 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
209 nsi[i].noinferiors, state, 0);
214 set_option (OPTIMAPCHECKSUBSCRIBED);
221 set_option (OPTIMAPCHECKSUBSCRIBED);
226 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
227 int imap_mailbox_create (const char *folder)
231 char buf[LONG_STRING];
234 if (imap_parse_path (folder, &mx) < 0) {
238 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
242 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
244 /* append a delimiter if necessary */
246 if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
247 buf[n++] = idata->delim;
251 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
254 if (!m_strlen(buf)) {
255 mutt_error (_("Mailbox must have a name."));
260 if (imap_create_mailbox (idata, buf) < 0)
263 mutt_message _("Mailbox created.");
275 int imap_mailbox_rename (const char *mailbox)
279 char buf[LONG_STRING];
280 char newname[SHORT_STRING];
282 if (imap_parse_path (mailbox, &mx) < 0) {
286 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
290 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
292 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
295 if (!m_strlen(newname)) {
296 mutt_error (_("Mailbox must have a name."));
301 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
302 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
307 mutt_message (_("Mailbox renamed."));
318 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
319 struct browser_state *state,
327 if (imap_parse_path (state->folder, &mx)) {
331 imap_cmd_start (idata, cmd);
334 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
335 &idata->delim) != 0) {
341 /* Let a parent folder never be selectable for navigation */
344 /* prune current folder from output */
345 if (isparent || m_strncmp(name, mx.mbox, m_strlen(name)))
346 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
350 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
356 /* imap_add_folder: add a folder name to the browser list, formatting it as
358 static void imap_add_folder (char delim, char *folder, int noselect,
359 int noinferiors, struct browser_state *state,
362 char tmp[LONG_STRING];
363 char relpath[LONG_STRING];
366 if (imap_parse_path (state->folder, &mx))
369 imap_unmunge_mbox_name (folder);
371 if (state->entrylen + 1 == state->entrymax) {
372 p_realloc(&state->entry, state->entrymax += 256);
373 p_clear(state->entry + state->entrylen,
374 state->entrymax - state->entrylen);
377 /* render superiors as unix-standard ".." */
379 m_strcpy(relpath, sizeof(relpath), "../");
380 /* strip current folder from target, to render a relative path */
381 else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
382 m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
384 m_strcpy(relpath, sizeof(relpath), folder);
386 /* apply filemask filter. This should really be done at menu setup rather
387 * than at scan, since it's so expensive to scan. But that's big changes
389 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
394 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
395 (state->entry)[state->entrylen].name = m_strdup(tmp);
397 /* mark desc with delim in browser if it can have subfolders */
398 if (!isparent && !noinferiors && m_strlen(relpath) < ssizeof (relpath) - 1) {
399 relpath[m_strlen(relpath) + 1] = '\0';
400 relpath[m_strlen(relpath)] = delim;
403 (state->entry)[state->entrylen].desc = m_strdup(relpath);
405 (state->entry)[state->entrylen].imap = 1;
406 /* delimiter at the root is useless. */
407 if (folder[0] == '\0')
409 (state->entry)[state->entrylen].delim = delim;
410 (state->entry)[state->entrylen].selectable = !noselect;
411 (state->entry)[state->entrylen].inferiors = !noinferiors;
417 static int compare_names (struct folder_file *a, struct folder_file *b)
419 return m_strcmp(a->name, b->name);
422 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
423 IMAP_NAMESPACE_INFO * nsi, int nsilen,
428 char ns[LONG_STRING];
435 nsbuf[nsblen - 1] = '\0';
437 imap_cmd_start (idata, "NAMESPACE");
440 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
443 s = imap_next_word (idata->cmd.buf);
444 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
445 /* There are three sections to the response, User, Other, Shared,
446 * and maybe more by extension */
447 for (type = IMAP_NS_PERSONAL; *s; type++) {
448 s = imap_next_word (s);
449 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
451 while (*s && *s != ')') {
459 while (*s && *s != '\"') {
469 while (*s && !ISSPACE (*s)) {
475 s = imap_next_word (s);
476 /* delimiter is meaningless if namespace is "". Why does
477 * Cyrus provide one?! */
478 if (n && *s && *s == '\"') {
479 if (s[1] && s[2] == '\"')
481 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
484 /* skip "" namespaces, they are already listed at the root */
485 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
487 /* Cyrus doesn't append the delimiter to the namespace,
488 * but UW-IMAP does. We'll strip it here and add it back
489 * as if it were a normal directory, from the browser */
490 if (n && (ns[n - 1] == delim))
492 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
493 nsi->prefix = nsbuf + nsbused;
499 while (*s && *s != ')')
508 while (rc == IMAP_CMD_CONTINUE);
510 if (rc != IMAP_CMD_OK)
516 /* Check which namespaces have contents */
517 static int browse_verify_namespace (IMAP_DATA * idata,
518 IMAP_NAMESPACE_INFO * nsi, int nns)
520 char buf[LONG_STRING];
525 for (i = 0; i < nns; i++, nsi++) {
526 /* Cyrus gives back nothing if the % isn't added. This may return lots
527 * of data in some cases, I guess, but I currently feel that's better
528 * than invisible namespaces */
530 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
531 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix,
534 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
535 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix);
537 imap_cmd_start (idata, buf);
540 nsi->home_namespace = 0;
542 if (imap_parse_list_response (idata, &name, &nsi->noselect,
543 &nsi->noinferiors, &delim) != 0)
545 nsi->listable |= (name != NULL);
547 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));