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 */
13 #include <lib-lib/lib-lib.h>
16 #include "imap_private.h"
18 /* -- forward declarations -- */
19 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
20 struct browser_state *state,
22 static void imap_add_folder (char delim, char *folder, int noselect,
23 int noinferiors, struct browser_state *state,
25 static int compare_names (struct folder_file *a, struct folder_file *b);
26 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
27 IMAP_NAMESPACE_INFO * nsi, int nsilen,
29 static int browse_verify_namespace (IMAP_DATA * idata,
30 IMAP_NAMESPACE_INFO * nsi, int nns);
32 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
33 * given a current folder to browse */
34 int imap_browse (char *path, struct browser_state *state)
37 char buf[LONG_STRING];
38 char buf2[LONG_STRING];
39 char nsbuf[LONG_STRING];
40 char mbox[LONG_STRING];
42 IMAP_NAMESPACE_INFO nsi[16];
43 int home_namespace = 0;
50 short showparents = 0;
56 if (imap_parse_path (path, &mx)) {
57 mutt_error (_("%s is an invalid IMAP path"), path);
61 save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
62 unset_option (OPTIMAPCHECKSUBSCRIBED);
63 m_strcpy(list_cmd, sizeof(list_cmd),
64 option(OPTIMAPLSUB) ? "LSUB" : "LIST");
66 if (!(idata = imap_conn_find (&(mx.account), 0)))
71 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
72 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
73 mutt_message _("Getting namespaces...");
75 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
76 nsi, sizeof (nsi), &nns) != 0)
78 if (browse_verify_namespace (idata, nsi, nns) != 0)
83 mutt_message _("Getting folder list...");
85 /* skip check for parents when at the root */
86 if (mx.mbox && mx.mbox[0] != '\0') {
87 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
88 imap_munge_mbox_name (buf, sizeof (buf), mbox);
89 imap_unquote_string (buf); /* As kludgy as it gets */
90 m_strcpy(mbox, sizeof(mbox), buf);
93 /* if our target exists and has inferiors, enter it if we
94 * aren't already going to */
95 if (mbox[n - 1] != idata->delim) {
96 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
97 imap_cmd_start (idata, buf);
99 if (imap_parse_list_response (idata, &cur_folder, &noselect,
100 &noinferiors, &idata->delim) != 0)
104 imap_unmunge_mbox_name (cur_folder);
106 if (!noinferiors && cur_folder[0] &&
107 (n = m_strlen(mbox)) < LONG_STRING - 1) {
108 mbox[n++] = idata->delim;
112 } while (m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN));
115 /* if we're descending a folder, mark it as current in browser_state */
116 if (mbox[n - 1] == idata->delim) {
117 /* don't show parents in the home namespace */
120 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
121 state->folder = m_strdup(buf);
125 /* Find superiors to list
126 * Note: UW-IMAP servers return folder + delimiter when asked to list
127 * folder + delimiter. Cyrus servers don't. So we ask for folder,
128 * and tack on delimiter ourselves.
129 * Further note: UW-IMAP servers return nothing when asked for
130 * NAMESPACES without delimiters at the end. Argh! */
131 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
132 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
133 /* forget the check, it is too delicate (see above). Have we ever
134 * had the parent not exist? */
139 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
142 /* if our target isn't a folder, we are in our superior */
143 if (!state->folder) {
144 /* store folder with delimiter */
148 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
149 state->folder = m_strdup(buf);
153 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
157 /* folder may be "/" */
158 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
160 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
161 if (!state->folder) {
162 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
163 state->folder = m_strdup(buf);
168 /* no namespace, no folder: set folder to host only */
169 if (!state->folder) {
170 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
171 state->folder = m_strdup(buf);
174 if (home_namespace && mbox[0] != '\0') {
175 /* Listing the home namespace, so INBOX should be included. Home
176 * namespace is not "", so we have to list it explicitly. We ask the
177 * server to see if it has descendants. */
178 if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
182 nsup = state->entrylen;
184 snprintf (buf, sizeof (buf), "%s%%", mbox);
185 imap_quote_string (buf2, sizeof (buf2), buf);
186 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
187 if (browse_add_list_result (idata, buf, state, 0))
190 if (!state->entrylen) {
191 mutt_error _("No such folder");
198 qsort (&(state->entry[nsup]), state->entrylen - nsup,
199 sizeof (state->entry[0]),
200 (int (*)(const void *, const void *)) compare_names);
201 if (home_namespace) { /* List additional namespaces */
202 for (i = 0; i < nns; i++)
203 if (nsi[i].listable && !nsi[i].home_namespace) {
204 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
205 nsi[i].noinferiors, state, 0);
210 set_option (OPTIMAPCHECKSUBSCRIBED);
217 set_option (OPTIMAPCHECKSUBSCRIBED);
222 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
223 int imap_mailbox_create (const char *folder)
227 char buf[LONG_STRING];
230 if (imap_parse_path (folder, &mx) < 0) {
234 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
238 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
240 /* append a delimiter if necessary */
242 if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
243 buf[n++] = idata->delim;
247 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
250 if (!m_strlen(buf)) {
251 mutt_error (_("Mailbox must have a name."));
256 if (imap_create_mailbox (idata, buf) < 0)
259 mutt_message _("Mailbox created.");
271 int imap_mailbox_rename (const char *mailbox)
275 char buf[LONG_STRING];
276 char newname[STRING];
278 if (imap_parse_path (mailbox, &mx) < 0) {
282 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
286 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
288 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
291 if (!m_strlen(newname)) {
292 mutt_error (_("Mailbox must have a name."));
297 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
298 mutt_error (_("Rename failed: %s"),
299 imap_get_qualifier(idata->cmd.buf.data));
304 mutt_message (_("Mailbox renamed."));
315 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
316 struct browser_state *state,
324 if (imap_parse_path (state->folder, &mx)) {
328 imap_cmd_start (idata, cmd);
331 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
332 &idata->delim) != 0) {
338 /* Let a parent folder never be selectable for navigation */
341 /* prune current folder from output */
342 if (isparent || m_strncmp(name, mx.mbox, m_strlen(name)))
343 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
346 } while ((m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN) != 0));
352 /* imap_add_folder: add a folder name to the browser list, formatting it as
354 static void imap_add_folder (char delim, char *folder, int noselect,
355 int noinferiors, struct browser_state *state,
358 char tmp[LONG_STRING];
359 char relpath[LONG_STRING];
362 if (imap_parse_path (state->folder, &mx))
365 imap_unmunge_mbox_name (folder);
367 if (state->entrylen + 1 == state->entrymax) {
368 p_realloc(&state->entry, state->entrymax += 256);
369 p_clear(state->entry + state->entrylen,
370 state->entrymax - state->entrylen);
373 /* render superiors as unix-standard ".." */
375 m_strcpy(relpath, sizeof(relpath), "../");
376 /* strip current folder from target, to render a relative path */
377 else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
378 m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
380 m_strcpy(relpath, sizeof(relpath), folder);
382 /* apply filemask filter. This should really be done at menu setup rather
383 * than at scan, since it's so expensive to scan. But that's big changes
385 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.neg)) {
390 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
391 (state->entry)[state->entrylen].name = m_strdup(tmp);
393 /* mark desc with delim in browser if it can have subfolders */
394 if (!isparent && !noinferiors && m_strlen(relpath) < ssizeof (relpath) - 1) {
395 relpath[m_strlen(relpath) + 1] = '\0';
396 relpath[m_strlen(relpath)] = delim;
399 (state->entry)[state->entrylen].desc = m_strdup(relpath);
401 (state->entry)[state->entrylen].imap = 1;
402 /* delimiter at the root is useless. */
403 if (folder[0] == '\0')
405 (state->entry)[state->entrylen].delim = delim;
406 (state->entry)[state->entrylen].selectable = !noselect;
407 (state->entry)[state->entrylen].inferiors = !noinferiors;
413 static int compare_names (struct folder_file *a, struct folder_file *b)
415 return m_strcmp(a->name, b->name);
418 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
419 IMAP_NAMESPACE_INFO * nsi, int nsilen,
424 char ns[LONG_STRING];
431 nsbuf[nsblen - 1] = '\0';
433 imap_cmd_start (idata, "NAMESPACE");
436 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
439 s = imap_next_word (idata->cmd.buf.data);
440 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
441 /* There are three sections to the response, User, Other, Shared,
442 * and maybe more by extension */
443 for (type = IMAP_NS_PERSONAL; *s; type++) {
444 s = imap_next_word (s);
445 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
447 while (*s && *s != ')') {
455 while (*s && *s != '\"') {
465 while (*s && !ISSPACE (*s)) {
471 s = imap_next_word (s);
472 /* delimiter is meaningless if namespace is "". Why does
473 * Cyrus provide one?! */
474 if (n && *s && *s == '\"') {
475 if (s[1] && s[2] == '\"')
477 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
480 /* skip "" namespaces, they are already listed at the root */
481 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
483 /* Cyrus doesn't append the delimiter to the namespace,
484 * but UW-IMAP does. We'll strip it here and add it back
485 * as if it were a normal directory, from the browser */
486 if (n && (ns[n - 1] == delim))
488 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
489 nsi->prefix = nsbuf + nsbused;
495 while (*s && *s != ')')
504 while (rc == IMAP_CMD_CONTINUE);
506 if (rc != IMAP_CMD_OK)
512 /* Check which namespaces have contents */
513 static int browse_verify_namespace (IMAP_DATA * idata,
514 IMAP_NAMESPACE_INFO * nsi, int nns)
516 char buf[LONG_STRING];
521 for (i = 0; i < nns; i++, nsi++) {
522 /* Cyrus gives back nothing if the % isn't added. This may return lots
523 * of data in some cases, I guess, but I currently feel that's better
524 * than invisible namespaces */
526 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
527 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix,
530 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
531 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix);
533 imap_cmd_start (idata, buf);
536 nsi->home_namespace = 0;
538 if (imap_parse_list_response (idata, &name, &nsi->noselect,
539 &nsi->noinferiors, &delim) != 0)
541 nsi->listable |= (name != NULL);
542 } while ((m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN) != 0));