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;
115 } while (m_strncmp(idata->cmd.buf, idata->cmd.seq, SEQLEN));
118 /* if we're descending a folder, mark it as current in browser_state */
119 if (mbox[n - 1] == idata->delim) {
120 /* don't show parents in the home namespace */
123 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
124 state->folder = m_strdup(buf);
128 /* Find superiors to list
129 * Note: UW-IMAP servers return folder + delimiter when asked to list
130 * folder + delimiter. Cyrus servers don't. So we ask for folder,
131 * and tack on delimiter ourselves.
132 * Further note: UW-IMAP servers return nothing when asked for
133 * NAMESPACES without delimiters at the end. Argh! */
134 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
135 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
136 /* forget the check, it is too delicate (see above). Have we ever
137 * had the parent not exist? */
142 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
145 /* if our target isn't a folder, we are in our superior */
146 if (!state->folder) {
147 /* store folder with delimiter */
151 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
152 state->folder = m_strdup(buf);
156 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
160 /* folder may be "/" */
161 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
163 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
164 if (!state->folder) {
165 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
166 state->folder = m_strdup(buf);
171 /* no namespace, no folder: set folder to host only */
172 if (!state->folder) {
173 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
174 state->folder = m_strdup(buf);
177 if (home_namespace && mbox[0] != '\0') {
178 /* Listing the home namespace, so INBOX should be included. Home
179 * namespace is not "", so we have to list it explicitly. We ask the
180 * server to see if it has descendants. */
181 if (browse_add_list_result (idata, "string_list_t \"\" \"INBOX\"", state, 0))
185 nsup = state->entrylen;
187 snprintf (buf, sizeof (buf), "%s%%", mbox);
188 imap_quote_string (buf2, sizeof (buf2), buf);
189 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
190 if (browse_add_list_result (idata, buf, state, 0))
193 if (!state->entrylen) {
194 mutt_error _("No such folder");
201 qsort (&(state->entry[nsup]), state->entrylen - nsup,
202 sizeof (state->entry[0]),
203 (int (*)(const void *, const void *)) compare_names);
204 if (home_namespace) { /* List additional namespaces */
205 for (i = 0; i < nns; i++)
206 if (nsi[i].listable && !nsi[i].home_namespace) {
207 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
208 nsi[i].noinferiors, state, 0);
213 set_option (OPTIMAPCHECKSUBSCRIBED);
220 set_option (OPTIMAPCHECKSUBSCRIBED);
225 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
226 int imap_mailbox_create (const char *folder)
230 char buf[LONG_STRING];
233 if (imap_parse_path (folder, &mx) < 0) {
237 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
241 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
243 /* append a delimiter if necessary */
245 if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
246 buf[n++] = idata->delim;
250 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
253 if (!m_strlen(buf)) {
254 mutt_error (_("Mailbox must have a name."));
259 if (imap_create_mailbox (idata, buf) < 0)
262 mutt_message _("Mailbox created.");
274 int imap_mailbox_rename (const char *mailbox)
278 char buf[LONG_STRING];
279 char newname[SHORT_STRING];
281 if (imap_parse_path (mailbox, &mx) < 0) {
285 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
289 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
291 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
294 if (!m_strlen(newname)) {
295 mutt_error (_("Mailbox must have a name."));
300 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
301 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
306 mutt_message (_("Mailbox renamed."));
317 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
318 struct browser_state *state,
326 if (imap_parse_path (state->folder, &mx)) {
330 imap_cmd_start (idata, cmd);
333 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
334 &idata->delim) != 0) {
340 /* Let a parent folder never be selectable for navigation */
343 /* prune current folder from output */
344 if (isparent || m_strncmp(name, mx.mbox, m_strlen(name)))
345 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
348 } while ((m_strncmp(idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
354 /* imap_add_folder: add a folder name to the browser list, formatting it as
356 static void imap_add_folder (char delim, char *folder, int noselect,
357 int noinferiors, struct browser_state *state,
360 char tmp[LONG_STRING];
361 char relpath[LONG_STRING];
364 if (imap_parse_path (state->folder, &mx))
367 imap_unmunge_mbox_name (folder);
369 if (state->entrylen + 1 == state->entrymax) {
370 p_realloc(&state->entry, state->entrymax += 256);
371 p_clear(state->entry + state->entrylen,
372 state->entrymax - state->entrylen);
375 /* render superiors as unix-standard ".." */
377 m_strcpy(relpath, sizeof(relpath), "../");
378 /* strip current folder from target, to render a relative path */
379 else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
380 m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
382 m_strcpy(relpath, sizeof(relpath), folder);
384 /* apply filemask filter. This should really be done at menu setup rather
385 * than at scan, since it's so expensive to scan. But that's big changes
387 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
392 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
393 (state->entry)[state->entrylen].name = m_strdup(tmp);
395 /* mark desc with delim in browser if it can have subfolders */
396 if (!isparent && !noinferiors && m_strlen(relpath) < ssizeof (relpath) - 1) {
397 relpath[m_strlen(relpath) + 1] = '\0';
398 relpath[m_strlen(relpath)] = delim;
401 (state->entry)[state->entrylen].desc = m_strdup(relpath);
403 (state->entry)[state->entrylen].imap = 1;
404 /* delimiter at the root is useless. */
405 if (folder[0] == '\0')
407 (state->entry)[state->entrylen].delim = delim;
408 (state->entry)[state->entrylen].selectable = !noselect;
409 (state->entry)[state->entrylen].inferiors = !noinferiors;
415 static int compare_names (struct folder_file *a, struct folder_file *b)
417 return m_strcmp(a->name, b->name);
420 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
421 IMAP_NAMESPACE_INFO * nsi, int nsilen,
426 char ns[LONG_STRING];
433 nsbuf[nsblen - 1] = '\0';
435 imap_cmd_start (idata, "NAMESPACE");
438 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
441 s = imap_next_word (idata->cmd.buf);
442 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
443 /* There are three sections to the response, User, Other, Shared,
444 * and maybe more by extension */
445 for (type = IMAP_NS_PERSONAL; *s; type++) {
446 s = imap_next_word (s);
447 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
449 while (*s && *s != ')') {
457 while (*s && *s != '\"') {
467 while (*s && !ISSPACE (*s)) {
473 s = imap_next_word (s);
474 /* delimiter is meaningless if namespace is "". Why does
475 * Cyrus provide one?! */
476 if (n && *s && *s == '\"') {
477 if (s[1] && s[2] == '\"')
479 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
482 /* skip "" namespaces, they are already listed at the root */
483 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
485 /* Cyrus doesn't append the delimiter to the namespace,
486 * but UW-IMAP does. We'll strip it here and add it back
487 * as if it were a normal directory, from the browser */
488 if (n && (ns[n - 1] == delim))
490 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
491 nsi->prefix = nsbuf + nsbused;
497 while (*s && *s != ')')
506 while (rc == IMAP_CMD_CONTINUE);
508 if (rc != IMAP_CMD_OK)
514 /* Check which namespaces have contents */
515 static int browse_verify_namespace (IMAP_DATA * idata,
516 IMAP_NAMESPACE_INFO * nsi, int nns)
518 char buf[LONG_STRING];
523 for (i = 0; i < nns; i++, nsi++) {
524 /* Cyrus gives back nothing if the % isn't added. This may return lots
525 * of data in some cases, I guess, but I currently feel that's better
526 * than invisible namespaces */
528 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
529 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix,
532 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
533 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix);
535 imap_cmd_start (idata, buf);
538 nsi->home_namespace = 0;
540 if (imap_parse_list_response (idata, &name, &nsi->noselect,
541 &nsi->noinferiors, &delim) != 0)
543 nsi->listable |= (name != NULL);
544 } while ((m_strncmp(idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));