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 */
25 #include "imap_private.h"
27 /* -- forward declarations -- */
28 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
29 struct browser_state *state,
31 static void imap_add_folder (char delim, char *folder, int noselect,
32 int noinferiors, struct browser_state *state,
34 static int compare_names (struct folder_file *a, struct folder_file *b);
35 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
36 IMAP_NAMESPACE_INFO * nsi, int nsilen,
38 static int browse_verify_namespace (IMAP_DATA * idata,
39 IMAP_NAMESPACE_INFO * nsi, int nns);
41 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
42 * given a current folder to browse */
43 int imap_browse (char *path, struct browser_state *state)
46 char buf[LONG_STRING];
47 char buf2[LONG_STRING];
48 char nsbuf[LONG_STRING];
49 char mbox[LONG_STRING];
51 IMAP_NAMESPACE_INFO nsi[16];
52 int home_namespace = 0;
59 short showparents = 0;
64 if (imap_parse_path (path, &mx)) {
65 mutt_error (_("%s is an invalid IMAP path"), path);
69 strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST",
72 if (!(idata = imap_conn_find (&(mx.account), 0)))
77 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
78 mx.mbox = safe_strdup (ImapHomeNamespace);
80 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
81 mutt_message _("Getting namespaces...");
83 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
84 nsi, sizeof (nsi), &nns) != 0)
86 if (browse_verify_namespace (idata, nsi, nns) != 0)
91 mutt_message _("Getting folder list...");
93 /* skip check for parents when at the root */
94 if (mx.mbox && mx.mbox[0] != '\0') {
95 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
96 imap_munge_mbox_name (buf, sizeof (buf), mbox);
97 imap_unquote_string (buf); /* As kludgy as it gets */
98 mbox[sizeof (mbox) - 1] = '\0';
99 strncpy (mbox, buf, sizeof (mbox) - 1);
100 n = mutt_strlen (mbox);
102 dprint (3, (debugfile, "imap_browse: mbox: %s\n", mbox));
104 /* if our target exists and has inferiors, enter it if we
105 * aren't already going to */
106 if (mbox[n - 1] != idata->delim) {
107 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
108 imap_cmd_start (idata, buf);
110 if (imap_parse_list_response (idata, &cur_folder, &noselect,
111 &noinferiors, &idata->delim) != 0)
115 imap_unmunge_mbox_name (cur_folder);
117 if (!noinferiors && cur_folder[0] &&
118 (n = mutt_strlen (mbox)) < LONG_STRING - 1) {
119 mbox[n++] = idata->delim;
124 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
127 /* if we're descending a folder, mark it as current in browser_state */
128 if (mbox[n - 1] == idata->delim) {
129 /* don't show parents in the home namespace */
132 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
133 state->folder = safe_strdup (buf);
137 /* Find superiors to list
138 * Note: UW-IMAP servers return folder + delimiter when asked to list
139 * folder + delimiter. Cyrus servers don't. So we ask for folder,
140 * and tack on delimiter ourselves.
141 * Further note: UW-IMAP servers return nothing when asked for
142 * NAMESPACES without delimiters at the end. Argh! */
143 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
144 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
145 /* forget the check, it is too delicate (see above). Have we ever
146 * had the parent not exist? */
151 dprint (3, (debugfile, "imap_init_browse: adding parent %s\n", mbox));
152 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
155 /* if our target isn't a folder, we are in our superior */
156 if (!state->folder) {
157 /* store folder with delimiter */
161 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
162 state->folder = safe_strdup (buf);
166 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
170 /* folder may be "/" */
171 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
173 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
174 if (!state->folder) {
175 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
176 state->folder = safe_strdup (buf);
181 /* no namespace, no folder: set folder to host only */
182 if (!state->folder) {
183 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
184 state->folder = safe_strdup (buf);
187 if (home_namespace && mbox[0] != '\0') {
188 /* Listing the home namespace, so INBOX should be included. Home
189 * namespace is not "", so we have to list it explicitly. We ask the
190 * server to see if it has descendants. */
191 dprint (3, (debugfile, "imap_browse: adding INBOX\n"));
192 if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
196 nsup = state->entrylen;
198 dprint (3, (debugfile, "imap_browse: Quoting mailbox scan: %s -> ", mbox));
199 snprintf (buf, sizeof (buf), "%s%%", mbox);
200 imap_quote_string (buf2, sizeof (buf2), buf);
201 dprint (3, (debugfile, "%s\n", buf2));
202 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
203 if (browse_add_list_result (idata, buf, state, 0))
206 if (!state->entrylen) {
207 mutt_error _("No such folder");
214 qsort (&(state->entry[nsup]), state->entrylen - nsup,
215 sizeof (state->entry[0]),
216 (int (*)(const void *, const void *)) compare_names);
217 if (home_namespace) { /* List additional namespaces */
218 for (i = 0; i < nns; i++)
219 if (nsi[i].listable && !nsi[i].home_namespace) {
220 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
221 nsi[i].noinferiors, state, 0);
222 dprint (3, (debugfile, "imap_browse: adding namespace: %s\n",
235 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
236 int imap_mailbox_create (const char *folder)
240 char buf[LONG_STRING];
243 if (imap_parse_path (folder, &mx) < 0) {
244 dprint (1, (debugfile, "imap_mailbox_create: Bad starting path %s\n",
249 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
252 "imap_mailbox_create: Couldn't find open connection to %s",
257 strfcpy (buf, NONULL (mx.mbox), sizeof (buf));
259 /* append a delimiter if necessary */
260 n = mutt_strlen (buf);
261 if (n && (n < sizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
262 buf[n++] = idata->delim;
266 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
269 if (!mutt_strlen (buf)) {
270 mutt_error (_("Mailbox must have a name."));
275 if (imap_create_mailbox (idata, buf) < 0)
278 mutt_message _("Mailbox created.");
290 int imap_mailbox_rename (const char *mailbox)
294 char buf[LONG_STRING];
295 char newname[SHORT_STRING];
297 if (imap_parse_path (mailbox, &mx) < 0) {
298 dprint (1, (debugfile, "imap_mailbox_rename: Bad source mailbox %s\n",
303 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
306 "imap_mailbox_rename: Couldn't find open connection to %s",
311 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
313 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
316 if (!mutt_strlen (newname)) {
317 mutt_error (_("Mailbox must have a name."));
322 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
323 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
328 mutt_message (_("Mailbox renamed."));
339 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
340 struct browser_state *state,
348 if (imap_parse_path (state->folder, &mx)) {
349 dprint (2, (debugfile,
350 "browse_add_list_result: current folder %s makes no sense\n",
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 || mutt_strncmp (name, mx.mbox, mutt_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 safe_realloc (&state->entry,
397 sizeof (struct folder_file) * (state->entrymax += 256));
398 memset (state->entry + state->entrylen, 0,
399 (sizeof (struct folder_file) *
400 (state->entrymax - state->entrylen)));
403 /* render superiors as unix-standard ".." */
405 strfcpy (relpath, "../", sizeof (relpath));
406 /* strip current folder from target, to render a relative path */
407 else if (!mutt_strncmp (mx.mbox, folder, mutt_strlen (mx.mbox)))
408 strfcpy (relpath, folder + mutt_strlen (mx.mbox), sizeof (relpath));
410 strfcpy (relpath, folder, sizeof (relpath));
412 /* apply filemask filter. This should really be done at menu setup rather
413 * than at scan, since it's so expensive to scan. But that's big changes
415 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
420 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
421 (state->entry)[state->entrylen].name = safe_strdup (tmp);
423 /* mark desc with delim in browser if it can have subfolders */
424 if (!isparent && !noinferiors && mutt_strlen (relpath) < sizeof (relpath) - 1) {
425 relpath[mutt_strlen (relpath) + 1] = '\0';
426 relpath[mutt_strlen (relpath)] = delim;
429 (state->entry)[state->entrylen].desc = safe_strdup (relpath);
431 (state->entry)[state->entrylen].imap = 1;
432 /* delimiter at the root is useless. */
433 if (folder[0] == '\0')
435 (state->entry)[state->entrylen].delim = delim;
436 (state->entry)[state->entrylen].selectable = !noselect;
437 (state->entry)[state->entrylen].inferiors = !noinferiors;
443 static int compare_names (struct folder_file *a, struct folder_file *b)
445 return mutt_strcmp (a->name, b->name);
448 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
449 IMAP_NAMESPACE_INFO * nsi, int nsilen,
454 char ns[LONG_STRING];
461 nsbuf[nsblen - 1] = '\0';
463 imap_cmd_start (idata, "NAMESPACE");
466 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
469 s = imap_next_word (idata->cmd.buf);
470 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
471 /* There are three sections to the response, User, Other, Shared,
472 * and maybe more by extension */
473 for (type = IMAP_NS_PERSONAL; *s; type++) {
474 s = imap_next_word (s);
475 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
477 while (*s && *s != ')') {
485 while (*s && *s != '\"') {
495 while (*s && !ISSPACE (*s)) {
501 s = imap_next_word (s);
502 /* delimiter is meaningless if namespace is "". Why does
503 * Cyrus provide one?! */
504 if (n && *s && *s == '\"') {
505 if (s[1] && s[2] == '\"')
507 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
510 /* skip "" namespaces, they are already listed at the root */
511 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
513 (debugfile, "browse_get_namespace: 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));