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"
28 #include "imap_private.h"
30 /* -- forward declarations -- */
31 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
32 struct browser_state *state,
34 static void imap_add_folder (char delim, char *folder, int noselect,
35 int noinferiors, struct browser_state *state,
37 static int compare_names (struct folder_file *a, struct folder_file *b);
38 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
39 IMAP_NAMESPACE_INFO * nsi, int nsilen,
41 static int browse_verify_namespace (IMAP_DATA * idata,
42 IMAP_NAMESPACE_INFO * nsi, int nns);
44 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
45 * given a current folder to browse */
46 int imap_browse (char *path, struct browser_state *state)
49 char buf[LONG_STRING];
50 char buf2[LONG_STRING];
51 char nsbuf[LONG_STRING];
52 char mbox[LONG_STRING];
54 IMAP_NAMESPACE_INFO nsi[16];
55 int home_namespace = 0;
62 short showparents = 0;
68 if (imap_parse_path (path, &mx)) {
69 mutt_error (_("%s is an invalid IMAP path"), path);
73 save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
74 unset_option (OPTIMAPCHECKSUBSCRIBED);
75 strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST",
78 if (!(idata = imap_conn_find (&(mx.account), 0)))
83 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
84 mx.mbox = str_dup (ImapHomeNamespace);
85 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
86 mutt_message _("Getting namespaces...");
88 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
89 nsi, sizeof (nsi), &nns) != 0)
91 if (browse_verify_namespace (idata, nsi, nns) != 0)
96 mutt_message _("Getting folder list...");
98 /* skip check for parents when at the root */
99 if (mx.mbox && mx.mbox[0] != '\0') {
100 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
101 imap_munge_mbox_name (buf, sizeof (buf), mbox);
102 imap_unquote_string (buf); /* As kludgy as it gets */
103 mbox[sizeof (mbox) - 1] = '\0';
104 strncpy (mbox, buf, sizeof (mbox) - 1);
107 debug_print (3, ("mbox: %s\n", mbox));
109 /* if our target exists and has inferiors, enter it if we
110 * aren't already going to */
111 if (mbox[n - 1] != idata->delim) {
112 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
113 imap_cmd_start (idata, buf);
115 if (imap_parse_list_response (idata, &cur_folder, &noselect,
116 &noinferiors, &idata->delim) != 0)
120 imap_unmunge_mbox_name (cur_folder);
122 if (!noinferiors && cur_folder[0] &&
123 (n = str_len (mbox)) < LONG_STRING - 1) {
124 mbox[n++] = idata->delim;
129 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
132 /* if we're descending a folder, mark it as current in browser_state */
133 if (mbox[n - 1] == idata->delim) {
134 /* don't show parents in the home namespace */
137 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
138 state->folder = str_dup (buf);
142 /* Find superiors to list
143 * Note: UW-IMAP servers return folder + delimiter when asked to list
144 * folder + delimiter. Cyrus servers don't. So we ask for folder,
145 * and tack on delimiter ourselves.
146 * Further note: UW-IMAP servers return nothing when asked for
147 * NAMESPACES without delimiters at the end. Argh! */
148 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
149 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
150 /* forget the check, it is too delicate (see above). Have we ever
151 * had the parent not exist? */
156 debug_print (3, ("adding parent %s\n", mbox));
157 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
160 /* if our target isn't a folder, we are in our superior */
161 if (!state->folder) {
162 /* store folder with delimiter */
166 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
167 state->folder = str_dup (buf);
171 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
175 /* folder may be "/" */
176 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
178 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
179 if (!state->folder) {
180 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
181 state->folder = str_dup (buf);
186 /* no namespace, no folder: set folder to host only */
187 if (!state->folder) {
188 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
189 state->folder = str_dup (buf);
192 if (home_namespace && mbox[0] != '\0') {
193 /* Listing the home namespace, so INBOX should be included. Home
194 * namespace is not "", so we have to list it explicitly. We ask the
195 * server to see if it has descendants. */
196 debug_print (3, ("adding INBOX\n"));
197 if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
201 nsup = state->entrylen;
203 debug_print (3, ("Quoting mailbox scan: %s:\n", mbox));
204 snprintf (buf, sizeof (buf), "%s%%", mbox);
205 imap_quote_string (buf2, sizeof (buf2), buf);
206 debug_print (3, ("%s\n", buf2));
207 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
208 if (browse_add_list_result (idata, buf, state, 0))
211 if (!state->entrylen) {
212 mutt_error _("No such folder");
219 qsort (&(state->entry[nsup]), state->entrylen - nsup,
220 sizeof (state->entry[0]),
221 (int (*)(const void *, const void *)) compare_names);
222 if (home_namespace) { /* List additional namespaces */
223 for (i = 0; i < nns; i++)
224 if (nsi[i].listable && !nsi[i].home_namespace) {
225 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
226 nsi[i].noinferiors, state, 0);
227 debug_print (3, ("adding namespace: %s\n", nsi[i].prefix));
232 set_option (OPTIMAPCHECKSUBSCRIBED);
239 set_option (OPTIMAPCHECKSUBSCRIBED);
244 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
245 int imap_mailbox_create (const char *folder)
249 char buf[LONG_STRING];
252 if (imap_parse_path (folder, &mx) < 0) {
253 debug_print (1, ("Bad starting path %s\n", folder));
257 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
258 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
262 strfcpy (buf, NONULL (mx.mbox), sizeof (buf));
264 /* append a delimiter if necessary */
266 if (n && (n < sizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
267 buf[n++] = idata->delim;
271 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
274 if (!str_len (buf)) {
275 mutt_error (_("Mailbox must have a name."));
280 if (imap_create_mailbox (idata, buf) < 0)
283 mutt_message _("Mailbox created.");
295 int imap_mailbox_rename (const char *mailbox)
299 char buf[LONG_STRING];
300 char newname[SHORT_STRING];
302 if (imap_parse_path (mailbox, &mx) < 0) {
303 debug_print (1, ("Bad source mailbox %s\n", mailbox));
307 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
308 debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
312 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
314 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
317 if (!str_len (newname)) {
318 mutt_error (_("Mailbox must have a name."));
323 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
324 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
329 mutt_message (_("Mailbox renamed."));
340 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
341 struct browser_state *state,
349 if (imap_parse_path (state->folder, &mx)) {
350 debug_print (2, ("current folder %s makes no sense\n", state->folder));
354 imap_cmd_start (idata, cmd);
357 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
358 &idata->delim) != 0) {
364 /* Let a parent folder never be selectable for navigation */
367 /* prune current folder from output */
368 if (isparent || str_ncmp (name, mx.mbox, str_len (name)))
369 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
373 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
379 /* imap_add_folder: add a folder name to the browser list, formatting it as
381 static void imap_add_folder (char delim, char *folder, int noselect,
382 int noinferiors, struct browser_state *state,
385 char tmp[LONG_STRING];
386 char relpath[LONG_STRING];
389 if (imap_parse_path (state->folder, &mx))
392 imap_unmunge_mbox_name (folder);
394 if (state->entrylen + 1 == state->entrymax) {
395 mem_realloc (&state->entry,
396 sizeof (struct folder_file) * (state->entrymax += 256));
397 memset (state->entry + state->entrylen, 0,
398 (sizeof (struct folder_file) *
399 (state->entrymax - state->entrylen)));
402 /* render superiors as unix-standard ".." */
404 strfcpy (relpath, "../", sizeof (relpath));
405 /* strip current folder from target, to render a relative path */
406 else if (!str_ncmp (mx.mbox, folder, str_len (mx.mbox)))
407 strfcpy (relpath, folder + str_len (mx.mbox), sizeof (relpath));
409 strfcpy (relpath, folder, sizeof (relpath));
411 /* apply filemask filter. This should really be done at menu setup rather
412 * than at scan, since it's so expensive to scan. But that's big changes
414 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
419 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
420 (state->entry)[state->entrylen].name = str_dup (tmp);
422 /* mark desc with delim in browser if it can have subfolders */
423 if (!isparent && !noinferiors && str_len (relpath) < sizeof (relpath) - 1) {
424 relpath[str_len (relpath) + 1] = '\0';
425 relpath[str_len (relpath)] = delim;
428 (state->entry)[state->entrylen].desc = str_dup (relpath);
430 (state->entry)[state->entrylen].imap = 1;
431 /* delimiter at the root is useless. */
432 if (folder[0] == '\0')
434 (state->entry)[state->entrylen].delim = delim;
435 (state->entry)[state->entrylen].selectable = !noselect;
436 (state->entry)[state->entrylen].inferiors = !noinferiors;
442 static int compare_names (struct folder_file *a, struct folder_file *b)
444 return str_cmp (a->name, b->name);
447 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
448 IMAP_NAMESPACE_INFO * nsi, int nsilen,
453 char ns[LONG_STRING];
460 nsbuf[nsblen - 1] = '\0';
462 imap_cmd_start (idata, "NAMESPACE");
465 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
468 s = imap_next_word (idata->cmd.buf);
469 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
470 /* There are three sections to the response, User, Other, Shared,
471 * and maybe more by extension */
472 for (type = IMAP_NS_PERSONAL; *s; type++) {
473 s = imap_next_word (s);
474 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
476 while (*s && *s != ')') {
484 while (*s && *s != '\"') {
494 while (*s && !ISSPACE (*s)) {
500 s = imap_next_word (s);
501 /* delimiter is meaningless if namespace is "". Why does
502 * Cyrus provide one?! */
503 if (n && *s && *s == '\"') {
504 if (s[1] && s[2] == '\"')
506 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
509 /* skip "" namespaces, they are already listed at the root */
510 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
511 debug_print (3, ("adding %s\n", ns));
513 /* Cyrus doesn't append the delimiter to the namespace,
514 * but UW-IMAP does. We'll strip it here and add it back
515 * as if it were a normal directory, from the browser */
516 if (n && (ns[n - 1] == delim))
518 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
519 nsi->prefix = nsbuf + nsbused;
525 while (*s && *s != ')')
534 while (rc == IMAP_CMD_CONTINUE);
536 if (rc != IMAP_CMD_OK)
542 /* Check which namespaces have contents */
543 static int browse_verify_namespace (IMAP_DATA * idata,
544 IMAP_NAMESPACE_INFO * nsi, int nns)
546 char buf[LONG_STRING];
551 for (i = 0; i < nns; i++, nsi++) {
552 /* Cyrus gives back nothing if the % isn't added. This may return lots
553 * of data in some cases, I guess, but I currently feel that's better
554 * than invisible namespaces */
556 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
557 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix,
560 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
561 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix);
563 imap_cmd_start (idata, buf);
566 nsi->home_namespace = 0;
568 if (imap_parse_list_response (idata, &name, &nsi->noselect,
569 &nsi->noinferiors, &delim) != 0)
571 nsi->listable |= (name != NULL);
573 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));