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>
21 #include <lib-lib/str.h>
22 #include <lib-lib/ascii.h>
23 #include <lib-lib/macros.h>
24 #include <lib-lib/debug.h>
26 #include <lib-ui/enter.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 m_strcpy(list_cmd, sizeof(list_cmd),
78 option(OPTIMAPLSUB) ? "LSUB" : "string_list_t");
80 if (!(idata = imap_conn_find (&(mx.account), 0)))
85 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
86 mx.mbox = m_strdup(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 m_strcpy(mbox, sizeof(mbox), buf);
108 debug_print (3, ("mbox: %s\n", mbox));
110 /* if our target exists and has inferiors, enter it if we
111 * aren't already going to */
112 if (mbox[n - 1] != idata->delim) {
113 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
114 imap_cmd_start (idata, buf);
116 if (imap_parse_list_response (idata, &cur_folder, &noselect,
117 &noinferiors, &idata->delim) != 0)
121 imap_unmunge_mbox_name (cur_folder);
123 if (!noinferiors && cur_folder[0] &&
124 (n = m_strlen(mbox)) < LONG_STRING - 1) {
125 mbox[n++] = idata->delim;
130 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
133 /* if we're descending a folder, mark it as current in browser_state */
134 if (mbox[n - 1] == idata->delim) {
135 /* don't show parents in the home namespace */
138 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
139 state->folder = m_strdup(buf);
143 /* Find superiors to list
144 * Note: UW-IMAP servers return folder + delimiter when asked to list
145 * folder + delimiter. Cyrus servers don't. So we ask for folder,
146 * and tack on delimiter ourselves.
147 * Further note: UW-IMAP servers return nothing when asked for
148 * NAMESPACES without delimiters at the end. Argh! */
149 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
150 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
151 /* forget the check, it is too delicate (see above). Have we ever
152 * had the parent not exist? */
157 debug_print (3, ("adding parent %s\n", mbox));
158 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
161 /* if our target isn't a folder, we are in our superior */
162 if (!state->folder) {
163 /* store folder with delimiter */
167 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
168 state->folder = m_strdup(buf);
172 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
176 /* folder may be "/" */
177 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
179 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
180 if (!state->folder) {
181 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
182 state->folder = m_strdup(buf);
187 /* no namespace, no folder: set folder to host only */
188 if (!state->folder) {
189 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
190 state->folder = m_strdup(buf);
193 if (home_namespace && mbox[0] != '\0') {
194 /* Listing the home namespace, so INBOX should be included. Home
195 * namespace is not "", so we have to list it explicitly. We ask the
196 * server to see if it has descendants. */
197 debug_print (3, ("adding INBOX\n"));
198 if (browse_add_list_result (idata, "string_list_t \"\" \"INBOX\"", state, 0))
202 nsup = state->entrylen;
204 debug_print (3, ("Quoting mailbox scan: %s:\n", mbox));
205 snprintf (buf, sizeof (buf), "%s%%", mbox);
206 imap_quote_string (buf2, sizeof (buf2), buf);
207 debug_print (3, ("%s\n", buf2));
208 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
209 if (browse_add_list_result (idata, buf, state, 0))
212 if (!state->entrylen) {
213 mutt_error _("No such folder");
220 qsort (&(state->entry[nsup]), state->entrylen - nsup,
221 sizeof (state->entry[0]),
222 (int (*)(const void *, const void *)) compare_names);
223 if (home_namespace) { /* List additional namespaces */
224 for (i = 0; i < nns; i++)
225 if (nsi[i].listable && !nsi[i].home_namespace) {
226 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
227 nsi[i].noinferiors, state, 0);
228 debug_print (3, ("adding namespace: %s\n", nsi[i].prefix));
233 set_option (OPTIMAPCHECKSUBSCRIBED);
240 set_option (OPTIMAPCHECKSUBSCRIBED);
245 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
246 int imap_mailbox_create (const char *folder)
250 char buf[LONG_STRING];
253 if (imap_parse_path (folder, &mx) < 0) {
254 debug_print (1, ("Bad starting path %s\n", folder));
258 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
259 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
263 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
265 /* append a delimiter if necessary */
267 if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
268 buf[n++] = idata->delim;
272 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
275 if (!m_strlen(buf)) {
276 mutt_error (_("Mailbox must have a name."));
281 if (imap_create_mailbox (idata, buf) < 0)
284 mutt_message _("Mailbox created.");
296 int imap_mailbox_rename (const char *mailbox)
300 char buf[LONG_STRING];
301 char newname[SHORT_STRING];
303 if (imap_parse_path (mailbox, &mx) < 0) {
304 debug_print (1, ("Bad source mailbox %s\n", mailbox));
308 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
309 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
313 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
315 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
318 if (!m_strlen(newname)) {
319 mutt_error (_("Mailbox must have a name."));
324 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
325 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
330 mutt_message (_("Mailbox renamed."));
341 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
342 struct browser_state *state,
350 if (imap_parse_path (state->folder, &mx)) {
351 debug_print (2, ("current folder %s makes no sense\n", state->folder));
355 imap_cmd_start (idata, cmd);
358 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
359 &idata->delim) != 0) {
365 /* Let a parent folder never be selectable for navigation */
368 /* prune current folder from output */
369 if (isparent || m_strncmp(name, mx.mbox, m_strlen(name)))
370 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
374 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
380 /* imap_add_folder: add a folder name to the browser list, formatting it as
382 static void imap_add_folder (char delim, char *folder, int noselect,
383 int noinferiors, struct browser_state *state,
386 char tmp[LONG_STRING];
387 char relpath[LONG_STRING];
390 if (imap_parse_path (state->folder, &mx))
393 imap_unmunge_mbox_name (folder);
395 if (state->entrylen + 1 == state->entrymax) {
396 p_realloc(&state->entry, state->entrymax += 256);
397 p_clear(state->entry + state->entrylen,
398 state->entrymax - state->entrylen);
401 /* render superiors as unix-standard ".." */
403 m_strcpy(relpath, sizeof(relpath), "../");
404 /* strip current folder from target, to render a relative path */
405 else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
406 m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
408 m_strcpy(relpath, sizeof(relpath), folder);
410 /* apply filemask filter. This should really be done at menu setup rather
411 * than at scan, since it's so expensive to scan. But that's big changes
413 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
418 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
419 (state->entry)[state->entrylen].name = m_strdup(tmp);
421 /* mark desc with delim in browser if it can have subfolders */
422 if (!isparent && !noinferiors && m_strlen(relpath) < ssizeof (relpath) - 1) {
423 relpath[m_strlen(relpath) + 1] = '\0';
424 relpath[m_strlen(relpath)] = delim;
427 (state->entry)[state->entrylen].desc = m_strdup(relpath);
429 (state->entry)[state->entrylen].imap = 1;
430 /* delimiter at the root is useless. */
431 if (folder[0] == '\0')
433 (state->entry)[state->entrylen].delim = delim;
434 (state->entry)[state->entrylen].selectable = !noselect;
435 (state->entry)[state->entrylen].inferiors = !noinferiors;
441 static int compare_names (struct folder_file *a, struct folder_file *b)
443 return m_strcmp(a->name, b->name);
446 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
447 IMAP_NAMESPACE_INFO * nsi, int nsilen,
452 char ns[LONG_STRING];
459 nsbuf[nsblen - 1] = '\0';
461 imap_cmd_start (idata, "NAMESPACE");
464 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
467 s = imap_next_word (idata->cmd.buf);
468 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
469 /* There are three sections to the response, User, Other, Shared,
470 * and maybe more by extension */
471 for (type = IMAP_NS_PERSONAL; *s; type++) {
472 s = imap_next_word (s);
473 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
475 while (*s && *s != ')') {
483 while (*s && *s != '\"') {
493 while (*s && !ISSPACE (*s)) {
499 s = imap_next_word (s);
500 /* delimiter is meaningless if namespace is "". Why does
501 * Cyrus provide one?! */
502 if (n && *s && *s == '\"') {
503 if (s[1] && s[2] == '\"')
505 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
508 /* skip "" namespaces, they are already listed at the root */
509 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
510 debug_print (3, ("adding %s\n", ns));
512 /* Cyrus doesn't append the delimiter to the namespace,
513 * but UW-IMAP does. We'll strip it here and add it back
514 * as if it were a normal directory, from the browser */
515 if (n && (ns[n - 1] == delim))
517 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
518 nsi->prefix = nsbuf + nsbused;
524 while (*s && *s != ')')
533 while (rc == IMAP_CMD_CONTINUE);
535 if (rc != IMAP_CMD_OK)
541 /* Check which namespaces have contents */
542 static int browse_verify_namespace (IMAP_DATA * idata,
543 IMAP_NAMESPACE_INFO * nsi, int nns)
545 char buf[LONG_STRING];
550 for (i = 0; i < nns; i++, nsi++) {
551 /* Cyrus gives back nothing if the % isn't added. This may return lots
552 * of data in some cases, I guess, but I currently feel that's better
553 * than invisible namespaces */
555 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
556 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix,
559 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
560 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix);
562 imap_cmd_start (idata, buf);
565 nsi->home_namespace = 0;
567 if (imap_parse_list_response (idata, &name, &nsi->noselect,
568 &nsi->noinferiors, &delim) != 0)
570 nsi->listable |= (name != NULL);
572 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));