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" : "LIST");
68 if (!(idata = imap_conn_find (&(mx.account), 0)))
73 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
74 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
75 mutt_message _("Getting namespaces...");
77 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
78 nsi, sizeof (nsi), &nns) != 0)
80 if (browse_verify_namespace (idata, nsi, nns) != 0)
85 mutt_message _("Getting folder list...");
87 /* skip check for parents when at the root */
88 if (mx.mbox && mx.mbox[0] != '\0') {
89 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
90 imap_munge_mbox_name (buf, sizeof (buf), mbox);
91 imap_unquote_string (buf); /* As kludgy as it gets */
92 m_strcpy(mbox, sizeof(mbox), buf);
95 /* if our target exists and has inferiors, enter it if we
96 * aren't already going to */
97 if (mbox[n - 1] != idata->delim) {
98 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
99 imap_cmd_start (idata, buf);
101 if (imap_parse_list_response (idata, &cur_folder, &noselect,
102 &noinferiors, &idata->delim) != 0)
106 imap_unmunge_mbox_name (cur_folder);
108 if (!noinferiors && cur_folder[0] &&
109 (n = m_strlen(mbox)) < LONG_STRING - 1) {
110 mbox[n++] = idata->delim;
114 } while (m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN));
117 /* if we're descending a folder, mark it as current in browser_state */
118 if (mbox[n - 1] == idata->delim) {
119 /* don't show parents in the home namespace */
122 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
123 state->folder = m_strdup(buf);
127 /* Find superiors to list
128 * Note: UW-IMAP servers return folder + delimiter when asked to list
129 * folder + delimiter. Cyrus servers don't. So we ask for folder,
130 * and tack on delimiter ourselves.
131 * Further note: UW-IMAP servers return nothing when asked for
132 * NAMESPACES without delimiters at the end. Argh! */
133 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
134 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
135 /* forget the check, it is too delicate (see above). Have we ever
136 * had the parent not exist? */
141 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
144 /* if our target isn't a folder, we are in our superior */
145 if (!state->folder) {
146 /* store folder with delimiter */
150 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
151 state->folder = m_strdup(buf);
155 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
159 /* folder may be "/" */
160 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
162 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
163 if (!state->folder) {
164 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
165 state->folder = m_strdup(buf);
170 /* no namespace, no folder: set folder to host only */
171 if (!state->folder) {
172 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
173 state->folder = m_strdup(buf);
176 if (home_namespace && mbox[0] != '\0') {
177 /* Listing the home namespace, so INBOX should be included. Home
178 * namespace is not "", so we have to list it explicitly. We ask the
179 * server to see if it has descendants. */
180 if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
184 nsup = state->entrylen;
186 snprintf (buf, sizeof (buf), "%s%%", mbox);
187 imap_quote_string (buf2, sizeof (buf2), buf);
188 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
189 if (browse_add_list_result (idata, buf, state, 0))
192 if (!state->entrylen) {
193 mutt_error _("No such folder");
200 qsort (&(state->entry[nsup]), state->entrylen - nsup,
201 sizeof (state->entry[0]),
202 (int (*)(const void *, const void *)) compare_names);
203 if (home_namespace) { /* List additional namespaces */
204 for (i = 0; i < nns; i++)
205 if (nsi[i].listable && !nsi[i].home_namespace) {
206 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
207 nsi[i].noinferiors, state, 0);
212 set_option (OPTIMAPCHECKSUBSCRIBED);
219 set_option (OPTIMAPCHECKSUBSCRIBED);
224 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
225 int imap_mailbox_create (const char *folder)
229 char buf[LONG_STRING];
232 if (imap_parse_path (folder, &mx) < 0) {
236 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
240 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
242 /* append a delimiter if necessary */
244 if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
245 buf[n++] = idata->delim;
249 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
252 if (!m_strlen(buf)) {
253 mutt_error (_("Mailbox must have a name."));
258 if (imap_create_mailbox (idata, buf) < 0)
261 mutt_message _("Mailbox created.");
273 int imap_mailbox_rename (const char *mailbox)
277 char buf[LONG_STRING];
278 char newname[STRING];
280 if (imap_parse_path (mailbox, &mx) < 0) {
284 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
288 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
290 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
293 if (!m_strlen(newname)) {
294 mutt_error (_("Mailbox must have a name."));
299 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
300 mutt_error (_("Rename failed: %s"),
301 imap_get_qualifier(idata->cmd.buf.data));
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.data, 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.neg)) {
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.data);
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" : "LIST", nsi->prefix,
532 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
533 option (OPTIMAPLSUB) ? "LSUB" : "LIST", 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.data, idata->cmd.seq, SEQLEN) != 0));