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 */
21 #include "imap_private.h"
23 /* -- forward declarations -- */
24 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
25 struct browser_state *state,
27 static void imap_add_folder (char delim, char *folder, int noselect,
28 int noinferiors, struct browser_state *state,
30 static int compare_names (struct folder_file *a, struct folder_file *b);
31 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
32 IMAP_NAMESPACE_INFO * nsi, int nsilen,
34 static int browse_verify_namespace (IMAP_DATA * idata,
35 IMAP_NAMESPACE_INFO * nsi, int nns);
37 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
38 * given a current folder to browse */
39 int imap_browse (char *path, struct browser_state *state)
42 char buf[LONG_STRING];
43 char buf2[LONG_STRING];
44 char nsbuf[LONG_STRING];
45 char mbox[LONG_STRING];
47 IMAP_NAMESPACE_INFO nsi[16];
48 int home_namespace = 0;
55 short showparents = 0;
60 if (imap_parse_path (path, &mx)) {
61 mutt_error (_("%s is an invalid IMAP path"), path);
65 strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST",
68 if (!(idata = imap_conn_find (&(mx.account), 0)))
73 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
74 mx.mbox = safe_strdup (ImapHomeNamespace);
76 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
77 mutt_message _("Getting namespaces...");
79 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
80 nsi, sizeof (nsi), &nns) != 0)
82 if (browse_verify_namespace (idata, nsi, nns) != 0)
87 mutt_message _("Getting folder list...");
89 /* skip check for parents when at the root */
90 if (mx.mbox && mx.mbox[0] != '\0') {
91 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
92 imap_munge_mbox_name (buf, sizeof (buf), mbox);
93 imap_unquote_string (buf); /* As kludgy as it gets */
94 mbox[sizeof (mbox) - 1] = '\0';
95 strncpy (mbox, buf, sizeof (mbox) - 1);
96 n = mutt_strlen (mbox);
98 dprint (3, (debugfile, "imap_browse: mbox: %s\n", mbox));
100 /* if our target exists and has inferiors, enter it if we
101 * aren't already going to */
102 if (mbox[n - 1] != idata->delim) {
103 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
104 imap_cmd_start (idata, buf);
106 if (imap_parse_list_response (idata, &cur_folder, &noselect,
107 &noinferiors, &idata->delim) != 0)
111 imap_unmunge_mbox_name (cur_folder);
113 if (!noinferiors && cur_folder[0] &&
114 (n = mutt_strlen (mbox)) < LONG_STRING - 1) {
115 mbox[n++] = idata->delim;
120 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
123 /* if we're descending a folder, mark it as current in browser_state */
124 if (mbox[n - 1] == idata->delim) {
125 /* don't show parents in the home namespace */
128 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
129 state->folder = safe_strdup (buf);
133 /* Find superiors to list
134 * Note: UW-IMAP servers return folder + delimiter when asked to list
135 * folder + delimiter. Cyrus servers don't. So we ask for folder,
136 * and tack on delimiter ourselves.
137 * Further note: UW-IMAP servers return nothing when asked for
138 * NAMESPACES without delimiters at the end. Argh! */
139 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
140 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
141 /* forget the check, it is too delicate (see above). Have we ever
142 * had the parent not exist? */
147 dprint (3, (debugfile, "imap_init_browse: adding parent %s\n", mbox));
148 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
151 /* if our target isn't a folder, we are in our superior */
152 if (!state->folder) {
153 /* store folder with delimiter */
157 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
158 state->folder = safe_strdup (buf);
162 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
166 /* folder may be "/" */
167 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
169 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
170 if (!state->folder) {
171 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
172 state->folder = safe_strdup (buf);
177 /* no namespace, no folder: set folder to host only */
178 if (!state->folder) {
179 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
180 state->folder = safe_strdup (buf);
183 if (home_namespace && mbox[0] != '\0') {
184 /* Listing the home namespace, so INBOX should be included. Home
185 * namespace is not "", so we have to list it explicitly. We ask the
186 * server to see if it has descendants. */
187 dprint (3, (debugfile, "imap_browse: adding INBOX\n"));
188 if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
192 nsup = state->entrylen;
194 dprint (3, (debugfile, "imap_browse: Quoting mailbox scan: %s -> ", mbox));
195 snprintf (buf, sizeof (buf), "%s%%", mbox);
196 imap_quote_string (buf2, sizeof (buf2), buf);
197 dprint (3, (debugfile, "%s\n", buf2));
198 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
199 if (browse_add_list_result (idata, buf, state, 0))
202 if (!state->entrylen) {
203 mutt_error _("No such folder");
210 qsort (&(state->entry[nsup]), state->entrylen - nsup,
211 sizeof (state->entry[0]),
212 (int (*)(const void *, const void *)) compare_names);
213 if (home_namespace) { /* List additional namespaces */
214 for (i = 0; i < nns; i++)
215 if (nsi[i].listable && !nsi[i].home_namespace) {
216 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
217 nsi[i].noinferiors, state, 0);
218 dprint (3, (debugfile, "imap_browse: adding namespace: %s\n",
231 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
232 int imap_mailbox_create (const char *folder)
236 char buf[LONG_STRING];
239 if (imap_parse_path (folder, &mx) < 0) {
240 dprint (1, (debugfile, "imap_mailbox_create: Bad starting path %s\n",
245 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
248 "imap_mailbox_create: Couldn't find open connection to %s",
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 dprint (1, (debugfile, "imap_mailbox_rename: Bad source mailbox %s\n",
299 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
302 "imap_mailbox_rename: Couldn't find open connection to %s",
307 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
309 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
312 if (!mutt_strlen (newname)) {
313 mutt_error (_("Mailbox must have a name."));
318 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
319 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
324 mutt_message (_("Mailbox renamed."));
335 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
336 struct browser_state *state,
344 if (imap_parse_path (state->folder, &mx)) {
345 dprint (2, (debugfile,
346 "browse_add_list_result: current folder %s makes no sense\n",
351 imap_cmd_start (idata, cmd);
354 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
355 &idata->delim) != 0) {
361 /* Let a parent folder never be selectable for navigation */
364 /* prune current folder from output */
365 if (isparent || mutt_strncmp (name, mx.mbox, mutt_strlen (name)))
366 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
370 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
376 /* imap_add_folder: add a folder name to the browser list, formatting it as
378 static void imap_add_folder (char delim, char *folder, int noselect,
379 int noinferiors, struct browser_state *state,
382 char tmp[LONG_STRING];
383 char relpath[LONG_STRING];
386 if (imap_parse_path (state->folder, &mx))
389 imap_unmunge_mbox_name (folder);
391 if (state->entrylen + 1 == state->entrymax) {
392 safe_realloc (&state->entry,
393 sizeof (struct folder_file) * (state->entrymax += 256));
394 memset (state->entry + state->entrylen, 0,
395 (sizeof (struct folder_file) *
396 (state->entrymax - state->entrylen)));
399 /* render superiors as unix-standard ".." */
401 strfcpy (relpath, "../", sizeof (relpath));
402 /* strip current folder from target, to render a relative path */
403 else if (!mutt_strncmp (mx.mbox, folder, mutt_strlen (mx.mbox)))
404 strfcpy (relpath, folder + mutt_strlen (mx.mbox), sizeof (relpath));
406 strfcpy (relpath, folder, sizeof (relpath));
408 /* apply filemask filter. This should really be done at menu setup rather
409 * than at scan, since it's so expensive to scan. But that's big changes
411 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
416 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
417 (state->entry)[state->entrylen].name = safe_strdup (tmp);
419 /* mark desc with delim in browser if it can have subfolders */
420 if (!isparent && !noinferiors && mutt_strlen (relpath) < sizeof (relpath) - 1) {
421 relpath[mutt_strlen (relpath) + 1] = '\0';
422 relpath[mutt_strlen (relpath)] = delim;
425 (state->entry)[state->entrylen].desc = safe_strdup (relpath);
427 (state->entry)[state->entrylen].imap = 1;
428 /* delimiter at the root is useless. */
429 if (folder[0] == '\0')
431 (state->entry)[state->entrylen].delim = delim;
432 (state->entry)[state->entrylen].selectable = !noselect;
433 (state->entry)[state->entrylen].inferiors = !noinferiors;
439 static int compare_names (struct folder_file *a, struct folder_file *b)
441 return mutt_strcmp (a->name, b->name);
444 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
445 IMAP_NAMESPACE_INFO * nsi, int nsilen,
450 char ns[LONG_STRING];
457 nsbuf[nsblen - 1] = '\0';
459 imap_cmd_start (idata, "NAMESPACE");
462 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
465 s = imap_next_word (idata->cmd.buf);
466 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
467 /* There are three sections to the response, User, Other, Shared,
468 * and maybe more by extension */
469 for (type = IMAP_NS_PERSONAL; *s; type++) {
470 s = imap_next_word (s);
471 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
473 while (*s && *s != ')') {
481 while (*s && *s != '\"') {
491 while (*s && !ISSPACE (*s)) {
497 s = imap_next_word (s);
498 /* delimiter is meaningless if namespace is "". Why does
499 * Cyrus provide one?! */
500 if (n && *s && *s == '\"') {
501 if (s[1] && s[2] == '\"')
503 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
506 /* skip "" namespaces, they are already listed at the root */
507 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
509 (debugfile, "browse_get_namespace: adding %s\n", ns));
511 /* Cyrus doesn't append the delimiter to the namespace,
512 * but UW-IMAP does. We'll strip it here and add it back
513 * as if it were a normal directory, from the browser */
514 if (n && (ns[n - 1] == delim))
516 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
517 nsi->prefix = nsbuf + nsbused;
523 while (*s && *s != ')')
532 while (rc == IMAP_CMD_CONTINUE);
534 if (rc != IMAP_CMD_OK)
540 /* Check which namespaces have contents */
541 static int browse_verify_namespace (IMAP_DATA * idata,
542 IMAP_NAMESPACE_INFO * nsi, int nns)
544 char buf[LONG_STRING];
549 for (i = 0; i < nns; i++, nsi++) {
550 /* Cyrus gives back nothing if the % isn't added. This may return lots
551 * of data in some cases, I guess, but I currently feel that's better
552 * than invisible namespaces */
554 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
555 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix,
558 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
559 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix);
561 imap_cmd_start (idata, buf);
564 nsi->home_namespace = 0;
566 if (imap_parse_list_response (idata, &name, &nsi->noselect,
567 &nsi->noinferiors, &delim) != 0)
569 nsi->listable |= (name != NULL);
571 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));