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 */
23 #include "lib/debug.h"
26 #include "imap_private.h"
28 /* -- forward declarations -- */
29 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
30 struct browser_state *state,
32 static void imap_add_folder (char delim, char *folder, int noselect,
33 int noinferiors, struct browser_state *state,
35 static int compare_names (struct folder_file *a, struct folder_file *b);
36 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
37 IMAP_NAMESPACE_INFO * nsi, int nsilen,
39 static int browse_verify_namespace (IMAP_DATA * idata,
40 IMAP_NAMESPACE_INFO * nsi, int nns);
42 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
43 * given a current folder to browse */
44 int imap_browse (char *path, struct browser_state *state)
47 char buf[LONG_STRING];
48 char buf2[LONG_STRING];
49 char nsbuf[LONG_STRING];
50 char mbox[LONG_STRING];
52 IMAP_NAMESPACE_INFO nsi[16];
53 int home_namespace = 0;
60 short showparents = 0;
65 if (imap_parse_path (path, &mx)) {
66 mutt_error (_("%s is an invalid IMAP path"), path);
70 strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST",
73 if (!(idata = imap_conn_find (&(mx.account), 0)))
78 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
79 mx.mbox = safe_strdup (ImapHomeNamespace);
81 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
82 mutt_message _("Getting namespaces...");
84 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
85 nsi, sizeof (nsi), &nns) != 0)
87 if (browse_verify_namespace (idata, nsi, nns) != 0)
92 mutt_message _("Getting folder list...");
94 /* skip check for parents when at the root */
95 if (mx.mbox && mx.mbox[0] != '\0') {
96 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
97 imap_munge_mbox_name (buf, sizeof (buf), mbox);
98 imap_unquote_string (buf); /* As kludgy as it gets */
99 mbox[sizeof (mbox) - 1] = '\0';
100 strncpy (mbox, buf, sizeof (mbox) - 1);
101 n = mutt_strlen (mbox);
103 debug_print (3, ("mbox: %s\n", mbox));
105 /* if our target exists and has inferiors, enter it if we
106 * aren't already going to */
107 if (mbox[n - 1] != idata->delim) {
108 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
109 imap_cmd_start (idata, buf);
111 if (imap_parse_list_response (idata, &cur_folder, &noselect,
112 &noinferiors, &idata->delim) != 0)
116 imap_unmunge_mbox_name (cur_folder);
118 if (!noinferiors && cur_folder[0] &&
119 (n = mutt_strlen (mbox)) < LONG_STRING - 1) {
120 mbox[n++] = idata->delim;
125 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
128 /* if we're descending a folder, mark it as current in browser_state */
129 if (mbox[n - 1] == idata->delim) {
130 /* don't show parents in the home namespace */
133 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
134 state->folder = safe_strdup (buf);
138 /* Find superiors to list
139 * Note: UW-IMAP servers return folder + delimiter when asked to list
140 * folder + delimiter. Cyrus servers don't. So we ask for folder,
141 * and tack on delimiter ourselves.
142 * Further note: UW-IMAP servers return nothing when asked for
143 * NAMESPACES without delimiters at the end. Argh! */
144 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
145 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
146 /* forget the check, it is too delicate (see above). Have we ever
147 * had the parent not exist? */
152 debug_print (3, ("adding parent %s\n", mbox));
153 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
156 /* if our target isn't a folder, we are in our superior */
157 if (!state->folder) {
158 /* store folder with delimiter */
162 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
163 state->folder = safe_strdup (buf);
167 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
171 /* folder may be "/" */
172 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
174 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
175 if (!state->folder) {
176 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
177 state->folder = safe_strdup (buf);
182 /* no namespace, no folder: set folder to host only */
183 if (!state->folder) {
184 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
185 state->folder = safe_strdup (buf);
188 if (home_namespace && mbox[0] != '\0') {
189 /* Listing the home namespace, so INBOX should be included. Home
190 * namespace is not "", so we have to list it explicitly. We ask the
191 * server to see if it has descendants. */
192 debug_print (3, ("adding INBOX\n"));
193 if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
197 nsup = state->entrylen;
199 debug_print (3, ("Quoting mailbox scan: %s:\n", mbox));
200 snprintf (buf, sizeof (buf), "%s%%", mbox);
201 imap_quote_string (buf2, sizeof (buf2), buf);
202 debug_print (3, ("%s\n", buf2));
203 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
204 if (browse_add_list_result (idata, buf, state, 0))
207 if (!state->entrylen) {
208 mutt_error _("No such folder");
215 qsort (&(state->entry[nsup]), state->entrylen - nsup,
216 sizeof (state->entry[0]),
217 (int (*)(const void *, const void *)) compare_names);
218 if (home_namespace) { /* List additional namespaces */
219 for (i = 0; i < nns; i++)
220 if (nsi[i].listable && !nsi[i].home_namespace) {
221 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
222 nsi[i].noinferiors, state, 0);
223 debug_print (3, ("adding namespace: %s\n", nsi[i].prefix));
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 debug_print (1, ("Bad starting path %s\n", folder));
248 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
249 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
253 strfcpy (buf, NONULL (mx.mbox), sizeof (buf));
255 /* append a delimiter if necessary */
256 n = mutt_strlen (buf);
257 if (n && (n < sizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
258 buf[n++] = idata->delim;
262 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
265 if (!mutt_strlen (buf)) {
266 mutt_error (_("Mailbox must have a name."));
271 if (imap_create_mailbox (idata, buf) < 0)
274 mutt_message _("Mailbox created.");
286 int imap_mailbox_rename (const char *mailbox)
290 char buf[LONG_STRING];
291 char newname[SHORT_STRING];
293 if (imap_parse_path (mailbox, &mx) < 0) {
294 debug_print (1, ("Bad source mailbox %s\n", mailbox));
298 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
299 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
303 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
305 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
308 if (!mutt_strlen (newname)) {
309 mutt_error (_("Mailbox must have a name."));
314 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
315 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
320 mutt_message (_("Mailbox renamed."));
331 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
332 struct browser_state *state,
340 if (imap_parse_path (state->folder, &mx)) {
341 debug_print (2, ("current folder %s makes no sense\n", state->folder));
345 imap_cmd_start (idata, cmd);
348 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
349 &idata->delim) != 0) {
355 /* Let a parent folder never be selectable for navigation */
358 /* prune current folder from output */
359 if (isparent || safe_strncmp (name, mx.mbox, mutt_strlen (name)))
360 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
364 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
370 /* imap_add_folder: add a folder name to the browser list, formatting it as
372 static void imap_add_folder (char delim, char *folder, int noselect,
373 int noinferiors, struct browser_state *state,
376 char tmp[LONG_STRING];
377 char relpath[LONG_STRING];
380 if (imap_parse_path (state->folder, &mx))
383 imap_unmunge_mbox_name (folder);
385 if (state->entrylen + 1 == state->entrymax) {
386 safe_realloc (&state->entry,
387 sizeof (struct folder_file) * (state->entrymax += 256));
388 memset (state->entry + state->entrylen, 0,
389 (sizeof (struct folder_file) *
390 (state->entrymax - state->entrylen)));
393 /* render superiors as unix-standard ".." */
395 strfcpy (relpath, "../", sizeof (relpath));
396 /* strip current folder from target, to render a relative path */
397 else if (!safe_strncmp (mx.mbox, folder, mutt_strlen (mx.mbox)))
398 strfcpy (relpath, folder + mutt_strlen (mx.mbox), sizeof (relpath));
400 strfcpy (relpath, folder, sizeof (relpath));
402 /* apply filemask filter. This should really be done at menu setup rather
403 * than at scan, since it's so expensive to scan. But that's big changes
405 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
410 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
411 (state->entry)[state->entrylen].name = safe_strdup (tmp);
413 /* mark desc with delim in browser if it can have subfolders */
414 if (!isparent && !noinferiors && mutt_strlen (relpath) < sizeof (relpath) - 1) {
415 relpath[mutt_strlen (relpath) + 1] = '\0';
416 relpath[mutt_strlen (relpath)] = delim;
419 (state->entry)[state->entrylen].desc = safe_strdup (relpath);
421 (state->entry)[state->entrylen].imap = 1;
422 /* delimiter at the root is useless. */
423 if (folder[0] == '\0')
425 (state->entry)[state->entrylen].delim = delim;
426 (state->entry)[state->entrylen].selectable = !noselect;
427 (state->entry)[state->entrylen].inferiors = !noinferiors;
433 static int compare_names (struct folder_file *a, struct folder_file *b)
435 return mutt_strcmp (a->name, b->name);
438 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
439 IMAP_NAMESPACE_INFO * nsi, int nsilen,
444 char ns[LONG_STRING];
451 nsbuf[nsblen - 1] = '\0';
453 imap_cmd_start (idata, "NAMESPACE");
456 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
459 s = imap_next_word (idata->cmd.buf);
460 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
461 /* There are three sections to the response, User, Other, Shared,
462 * and maybe more by extension */
463 for (type = IMAP_NS_PERSONAL; *s; type++) {
464 s = imap_next_word (s);
465 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
467 while (*s && *s != ')') {
475 while (*s && *s != '\"') {
485 while (*s && !ISSPACE (*s)) {
491 s = imap_next_word (s);
492 /* delimiter is meaningless if namespace is "". Why does
493 * Cyrus provide one?! */
494 if (n && *s && *s == '\"') {
495 if (s[1] && s[2] == '\"')
497 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
500 /* skip "" namespaces, they are already listed at the root */
501 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
502 debug_print (3, ("adding %s\n", ns));
504 /* Cyrus doesn't append the delimiter to the namespace,
505 * but UW-IMAP does. We'll strip it here and add it back
506 * as if it were a normal directory, from the browser */
507 if (n && (ns[n - 1] == delim))
509 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
510 nsi->prefix = nsbuf + nsbused;
516 while (*s && *s != ')')
525 while (rc == IMAP_CMD_CONTINUE);
527 if (rc != IMAP_CMD_OK)
533 /* Check which namespaces have contents */
534 static int browse_verify_namespace (IMAP_DATA * idata,
535 IMAP_NAMESPACE_INFO * nsi, int nns)
537 char buf[LONG_STRING];
542 for (i = 0; i < nns; i++, nsi++) {
543 /* Cyrus gives back nothing if the % isn't added. This may return lots
544 * of data in some cases, I guess, but I currently feel that's better
545 * than invisible namespaces */
547 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
548 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix,
551 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
552 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix);
554 imap_cmd_start (idata, buf);
557 nsi->home_namespace = 0;
559 if (imap_parse_list_response (idata, &name, &nsi->noselect,
560 &nsi->noinferiors, &delim) != 0)
562 nsi->listable |= (name != NULL);
564 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));