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/mem.h>
21 #include <lib-lib/str.h>
22 #include <lib-lib/ascii.h>
23 #include <lib-lib/macros.h>
25 #include <lib-ui/enter.h>
29 #include "imap_private.h"
31 /* -- forward declarations -- */
32 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
33 struct browser_state *state,
35 static void imap_add_folder (char delim, char *folder, int noselect,
36 int noinferiors, struct browser_state *state,
38 static int compare_names (struct folder_file *a, struct folder_file *b);
39 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
40 IMAP_NAMESPACE_INFO * nsi, int nsilen,
42 static int browse_verify_namespace (IMAP_DATA * idata,
43 IMAP_NAMESPACE_INFO * nsi, int nns);
45 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
46 * given a current folder to browse */
47 int imap_browse (char *path, struct browser_state *state)
50 char buf[LONG_STRING];
51 char buf2[LONG_STRING];
52 char nsbuf[LONG_STRING];
53 char mbox[LONG_STRING];
55 IMAP_NAMESPACE_INFO nsi[16];
56 int home_namespace = 0;
63 short showparents = 0;
69 if (imap_parse_path (path, &mx)) {
70 mutt_error (_("%s is an invalid IMAP path"), path);
74 save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
75 unset_option (OPTIMAPCHECKSUBSCRIBED);
76 m_strcpy(list_cmd, sizeof(list_cmd),
77 option(OPTIMAPLSUB) ? "LSUB" : "string_list_t");
79 if (!(idata = imap_conn_find (&(mx.account), 0)))
84 mbox[0] = '\0'; /* Do not replace "" with "INBOX" here */
85 mx.mbox = m_strdup(ImapHomeNamespace);
86 if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
87 mutt_message _("Getting namespaces...");
89 if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
90 nsi, sizeof (nsi), &nns) != 0)
92 if (browse_verify_namespace (idata, nsi, nns) != 0)
97 mutt_message _("Getting folder list...");
99 /* skip check for parents when at the root */
100 if (mx.mbox && mx.mbox[0] != '\0') {
101 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
102 imap_munge_mbox_name (buf, sizeof (buf), mbox);
103 imap_unquote_string (buf); /* As kludgy as it gets */
104 m_strcpy(mbox, sizeof(mbox), buf);
107 /* if our target exists and has inferiors, enter it if we
108 * aren't already going to */
109 if (mbox[n - 1] != idata->delim) {
110 snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
111 imap_cmd_start (idata, buf);
113 if (imap_parse_list_response (idata, &cur_folder, &noselect,
114 &noinferiors, &idata->delim) != 0)
118 imap_unmunge_mbox_name (cur_folder);
120 if (!noinferiors && cur_folder[0] &&
121 (n = m_strlen(mbox)) < LONG_STRING - 1) {
122 mbox[n++] = idata->delim;
127 while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
130 /* if we're descending a folder, mark it as current in browser_state */
131 if (mbox[n - 1] == idata->delim) {
132 /* don't show parents in the home namespace */
135 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
136 state->folder = m_strdup(buf);
140 /* Find superiors to list
141 * Note: UW-IMAP servers return folder + delimiter when asked to list
142 * folder + delimiter. Cyrus servers don't. So we ask for folder,
143 * and tack on delimiter ourselves.
144 * Further note: UW-IMAP servers return nothing when asked for
145 * NAMESPACES without delimiters at the end. Argh! */
146 for (n--; n >= 0 && mbox[n] != idata->delim; n--);
147 if (n > 0) { /* "aaaa/bbbb/" -> "aaaa" */
148 /* forget the check, it is too delicate (see above). Have we ever
149 * had the parent not exist? */
154 imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
157 /* if our target isn't a folder, we are in our superior */
158 if (!state->folder) {
159 /* store folder with delimiter */
163 imap_qualify_path (buf, sizeof (buf), &mx, mbox);
164 state->folder = m_strdup(buf);
168 /* "/bbbb/" -> add "/", "aaaa/" -> add "" */
172 /* folder may be "/" */
173 snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
175 imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
176 if (!state->folder) {
177 imap_qualify_path (buf, sizeof (buf), &mx, relpath);
178 state->folder = m_strdup(buf);
183 /* no namespace, no folder: set folder to host only */
184 if (!state->folder) {
185 imap_qualify_path (buf, sizeof (buf), &mx, NULL);
186 state->folder = m_strdup(buf);
189 if (home_namespace && mbox[0] != '\0') {
190 /* Listing the home namespace, so INBOX should be included. Home
191 * namespace is not "", so we have to list it explicitly. We ask the
192 * server to see if it has descendants. */
193 if (browse_add_list_result (idata, "string_list_t \"\" \"INBOX\"", state, 0))
197 nsup = state->entrylen;
199 snprintf (buf, sizeof (buf), "%s%%", mbox);
200 imap_quote_string (buf2, sizeof (buf2), buf);
201 snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
202 if (browse_add_list_result (idata, buf, state, 0))
205 if (!state->entrylen) {
206 mutt_error _("No such folder");
213 qsort (&(state->entry[nsup]), state->entrylen - nsup,
214 sizeof (state->entry[0]),
215 (int (*)(const void *, const void *)) compare_names);
216 if (home_namespace) { /* List additional namespaces */
217 for (i = 0; i < nns; i++)
218 if (nsi[i].listable && !nsi[i].home_namespace) {
219 imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
220 nsi[i].noinferiors, state, 0);
225 set_option (OPTIMAPCHECKSUBSCRIBED);
232 set_option (OPTIMAPCHECKSUBSCRIBED);
237 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
238 int imap_mailbox_create (const char *folder)
242 char buf[LONG_STRING];
245 if (imap_parse_path (folder, &mx) < 0) {
249 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
253 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
255 /* append a delimiter if necessary */
257 if (n && (n < ssizeof (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 (!m_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) {
297 if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
301 snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
303 if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
306 if (!m_strlen(newname)) {
307 mutt_error (_("Mailbox must have a name."));
312 if (imap_rename_mailbox (idata, &mx, newname) < 0) {
313 mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
318 mutt_message (_("Mailbox renamed."));
329 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
330 struct browser_state *state,
338 if (imap_parse_path (state->folder, &mx)) {
342 imap_cmd_start (idata, cmd);
345 if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
346 &idata->delim) != 0) {
352 /* Let a parent folder never be selectable for navigation */
355 /* prune current folder from output */
356 if (isparent || m_strncmp(name, mx.mbox, m_strlen(name)))
357 imap_add_folder (idata->delim, name, noselect, noinferiors, state,
361 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
367 /* imap_add_folder: add a folder name to the browser list, formatting it as
369 static void imap_add_folder (char delim, char *folder, int noselect,
370 int noinferiors, struct browser_state *state,
373 char tmp[LONG_STRING];
374 char relpath[LONG_STRING];
377 if (imap_parse_path (state->folder, &mx))
380 imap_unmunge_mbox_name (folder);
382 if (state->entrylen + 1 == state->entrymax) {
383 p_realloc(&state->entry, state->entrymax += 256);
384 p_clear(state->entry + state->entrylen,
385 state->entrymax - state->entrylen);
388 /* render superiors as unix-standard ".." */
390 m_strcpy(relpath, sizeof(relpath), "../");
391 /* strip current folder from target, to render a relative path */
392 else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
393 m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
395 m_strcpy(relpath, sizeof(relpath), folder);
397 /* apply filemask filter. This should really be done at menu setup rather
398 * than at scan, since it's so expensive to scan. But that's big changes
400 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
405 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
406 (state->entry)[state->entrylen].name = m_strdup(tmp);
408 /* mark desc with delim in browser if it can have subfolders */
409 if (!isparent && !noinferiors && m_strlen(relpath) < ssizeof (relpath) - 1) {
410 relpath[m_strlen(relpath) + 1] = '\0';
411 relpath[m_strlen(relpath)] = delim;
414 (state->entry)[state->entrylen].desc = m_strdup(relpath);
416 (state->entry)[state->entrylen].imap = 1;
417 /* delimiter at the root is useless. */
418 if (folder[0] == '\0')
420 (state->entry)[state->entrylen].delim = delim;
421 (state->entry)[state->entrylen].selectable = !noselect;
422 (state->entry)[state->entrylen].inferiors = !noinferiors;
428 static int compare_names (struct folder_file *a, struct folder_file *b)
430 return m_strcmp(a->name, b->name);
433 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
434 IMAP_NAMESPACE_INFO * nsi, int nsilen,
439 char ns[LONG_STRING];
446 nsbuf[nsblen - 1] = '\0';
448 imap_cmd_start (idata, "NAMESPACE");
451 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
454 s = imap_next_word (idata->cmd.buf);
455 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
456 /* There are three sections to the response, User, Other, Shared,
457 * and maybe more by extension */
458 for (type = IMAP_NS_PERSONAL; *s; type++) {
459 s = imap_next_word (s);
460 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
462 while (*s && *s != ')') {
470 while (*s && *s != '\"') {
480 while (*s && !ISSPACE (*s)) {
486 s = imap_next_word (s);
487 /* delimiter is meaningless if namespace is "". Why does
488 * Cyrus provide one?! */
489 if (n && *s && *s == '\"') {
490 if (s[1] && s[2] == '\"')
492 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
495 /* skip "" namespaces, they are already listed at the root */
496 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
498 /* Cyrus doesn't append the delimiter to the namespace,
499 * but UW-IMAP does. We'll strip it here and add it back
500 * as if it were a normal directory, from the browser */
501 if (n && (ns[n - 1] == delim))
503 strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
504 nsi->prefix = nsbuf + nsbused;
510 while (*s && *s != ')')
519 while (rc == IMAP_CMD_CONTINUE);
521 if (rc != IMAP_CMD_OK)
527 /* Check which namespaces have contents */
528 static int browse_verify_namespace (IMAP_DATA * idata,
529 IMAP_NAMESPACE_INFO * nsi, int nns)
531 char buf[LONG_STRING];
536 for (i = 0; i < nns; i++, nsi++) {
537 /* Cyrus gives back nothing if the % isn't added. This may return lots
538 * of data in some cases, I guess, but I currently feel that's better
539 * than invisible namespaces */
541 snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
542 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix,
545 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
546 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix);
548 imap_cmd_start (idata, buf);
551 nsi->home_namespace = 0;
553 if (imap_parse_list_response (idata, &name, &nsi->noselect,
554 &nsi->noinferiors, &delim) != 0)
556 nsi->listable |= (name != NULL);
558 while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));