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 */
20 #include <lib-lib/lib-lib.h>
22 #include <lib-ui/enter.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;
66 if (imap_parse_path (path, &mx)) {
67 mutt_error (_("%s is an invalid IMAP path"), path);
71 save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
72 unset_option (OPTIMAPCHECKSUBSCRIBED);
73 m_strcpy(list_cmd, sizeof(list_cmd),
74 option(OPTIMAPLSUB) ? "LSUB" : "string_list_t");
76 if (!(idata = imap_conn_find (&(mx.account), 0)))
81 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
82 mx.mbox = m_strdup(ImapHomeNamespace);
83 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
84 mutt_message _("Getting namespaces...");
86 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
87 nsi, sizeof (nsi), &nns) != 0)
89 if (browse_verify_namespace (idata, nsi, nns) != 0)
94 mutt_message _("Getting folder list...");
96 /* skip check for parents when at the root */
97 if (mx.mbox && mx.mbox[0] != '\0') {
98 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
99 imap_munge_mbox_name (buf, sizeof (buf), mbox);
100 imap_unquote_string (buf); /* As kludgy as it gets */
101 m_strcpy(mbox, sizeof(mbox), buf);
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 = m_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 = m_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 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
154 /* if our target isn't a folder, we are in our superior */
155 if (!state->folder) {
156 /* store folder with delimiter */
160 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
161 state->folder = m_strdup(buf);
165 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
169 /* folder may be "/" */
170 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
172 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
173 if (!state->folder) {
174 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
175 state->folder = m_strdup(buf);
180 /* no namespace, no folder: set folder to host only */
181 if (!state->folder) {
182 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
183 state->folder = m_strdup(buf);
186 if (home_namespace && mbox[0] != '\0') {
187 /* Listing the home namespace, so INBOX should be included. Home
188 * namespace is not "", so we have to list it explicitly. We ask the
189 * server to see if it has descendants. */
190 if (browse_add_list_result (idata, "string_list_t \"\" \"INBOX\"", state, 0))
194 nsup = state->entrylen;
196 snprintf (buf, sizeof (buf), "%s%%", mbox);
197 imap_quote_string (buf2, sizeof (buf2), buf);
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);
222 set_option (OPTIMAPCHECKSUBSCRIBED);
229 set_option (OPTIMAPCHECKSUBSCRIBED);
234 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
235 int imap_mailbox_create (const char *folder)
239 char buf[LONG_STRING];
242 if (imap_parse_path (folder, &mx) < 0) {
246 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
250 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
252 /* append a delimiter if necessary */
254 if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
255 buf[n++] = idata->delim;
259 if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
262 if (!m_strlen(buf)) {
263 mutt_error (_("Mailbox must have a name."));
268 if (imap_create_mailbox (idata, buf) < 0)
271 mutt_message _("Mailbox created.");
283 int imap_mailbox_rename (const char *mailbox)
287 char buf[LONG_STRING];
288 char newname[SHORT_STRING];
290 if (imap_parse_path (mailbox, &mx) < 0) {
294 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
298 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
300 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
303 if (!m_strlen(newname)) {
304 mutt_error (_("Mailbox must have a name."));
309 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
310 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
315 mutt_message (_("Mailbox renamed."));
326 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
327 struct browser_state *state,
335 if (imap_parse_path (state->folder, &mx)) {
339 imap_cmd_start (idata, cmd);
342 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
343 &idata->delim) != 0) {
349 /* Let a parent folder never be selectable for navigation */
352 /* prune current folder from output */
353 if (isparent || m_strncmp(name, mx.mbox, m_strlen(name)))
354 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
358 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
364 /* imap_add_folder: add a folder name to the browser list, formatting it as
366 static void imap_add_folder (char delim, char *folder, int noselect,
367 int noinferiors, struct browser_state *state,
370 char tmp[LONG_STRING];
371 char relpath[LONG_STRING];
374 if (imap_parse_path (state->folder, &mx))
377 imap_unmunge_mbox_name (folder);
379 if (state->entrylen + 1 == state->entrymax) {
380 p_realloc(&state->entry, state->entrymax += 256);
381 p_clear(state->entry + state->entrylen,
382 state->entrymax - state->entrylen);
385 /* render superiors as unix-standard ".." */
387 m_strcpy(relpath, sizeof(relpath), "../");
388 /* strip current folder from target, to render a relative path */
389 else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
390 m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
392 m_strcpy(relpath, sizeof(relpath), folder);
394 /* apply filemask filter. This should really be done at menu setup rather
395 * than at scan, since it's so expensive to scan. But that's big changes
397 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
402 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
403 (state->entry)[state->entrylen].name = m_strdup(tmp);
405 /* mark desc with delim in browser if it can have subfolders */
406 if (!isparent && !noinferiors && m_strlen(relpath) < ssizeof (relpath) - 1) {
407 relpath[m_strlen(relpath) + 1] = '\0';
408 relpath[m_strlen(relpath)] = delim;
411 (state->entry)[state->entrylen].desc = m_strdup(relpath);
413 (state->entry)[state->entrylen].imap = 1;
414 /* delimiter at the root is useless. */
415 if (folder[0] == '\0')
417 (state->entry)[state->entrylen].delim = delim;
418 (state->entry)[state->entrylen].selectable = !noselect;
419 (state->entry)[state->entrylen].inferiors = !noinferiors;
425 static int compare_names (struct folder_file *a, struct folder_file *b)
427 return m_strcmp(a->name, b->name);
430 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
431 IMAP_NAMESPACE_INFO * nsi, int nsilen,
436 char ns[LONG_STRING];
443 nsbuf[nsblen - 1] = '\0';
445 imap_cmd_start (idata, "NAMESPACE");
448 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
451 s = imap_next_word (idata->cmd.buf);
452 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
453 /* There are three sections to the response, User, Other, Shared,
454 * and maybe more by extension */
455 for (type = IMAP_NS_PERSONAL; *s; type++) {
456 s = imap_next_word (s);
457 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
459 while (*s && *s != ')') {
467 while (*s && *s != '\"') {
477 while (*s && !ISSPACE (*s)) {
483 s = imap_next_word (s);
484 /* delimiter is meaningless if namespace is "". Why does
485 * Cyrus provide one?! */
486 if (n && *s && *s == '\"') {
487 if (s[1] && s[2] == '\"')
489 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
492 /* skip "" namespaces, they are already listed at the root */
493 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
495 /* Cyrus doesn't append the delimiter to the namespace,
496 * but UW-IMAP does. We'll strip it here and add it back
497 * as if it were a normal directory, from the browser */
498 if (n && (ns[n - 1] == delim))
500 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
501 nsi->prefix = nsbuf + nsbused;
507 while (*s && *s != ')')
516 while (rc == IMAP_CMD_CONTINUE);
518 if (rc != IMAP_CMD_OK)
524 /* Check which namespaces have contents */
525 static int browse_verify_namespace (IMAP_DATA * idata,
526 IMAP_NAMESPACE_INFO * nsi, int nns)
528 char buf[LONG_STRING];
533 for (i = 0; i < nns; i++, nsi++) {
534 /* Cyrus gives back nothing if the % isn't added. This may return lots
535 * of data in some cases, I guess, but I currently feel that's better
536 * than invisible namespaces */
538 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
539 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix,
542 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
543 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix);
545 imap_cmd_start (idata, buf);
548 nsi->home_namespace = 0;
550 if (imap_parse_list_response (idata, &name, &nsi->noselect,
551 &nsi->noinferiors, &delim) != 0)
553 nsi->listable |= (name != NULL);
555 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));