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/debug.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" : "LIST");
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 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 = m_strlen(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 = m_strdup(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 = m_strdup(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 = m_strdup(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 = m_strdup(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 m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
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 (!m_strlen(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 (!m_strlen(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 || m_strncmp(name, mx.mbox, m_strlen(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 p_realloc(&state->entry, state->entrymax += 256);
396 p_clear(state->entry + state->entrylen,
397 state->entrymax - state->entrylen);
400 /* render superiors as unix-standard ".." */
402 m_strcpy(relpath, sizeof(relpath), "../");
403 /* strip current folder from target, to render a relative path */
404 else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
405 m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
407 m_strcpy(relpath, sizeof(relpath), folder);
409 /* apply filemask filter. This should really be done at menu setup rather
410 * than at scan, since it's so expensive to scan. But that's big changes
412 if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
417 imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
418 (state->entry)[state->entrylen].name = m_strdup(tmp);
420 /* mark desc with delim in browser if it can have subfolders */
421 if (!isparent && !noinferiors && m_strlen(relpath) < sizeof (relpath) - 1) {
422 relpath[m_strlen(relpath) + 1] = '\0';
423 relpath[m_strlen(relpath)] = delim;
426 (state->entry)[state->entrylen].desc = m_strdup(relpath);
428 (state->entry)[state->entrylen].imap = 1;
429 /* delimiter at the root is useless. */
430 if (folder[0] == '\0')
432 (state->entry)[state->entrylen].delim = delim;
433 (state->entry)[state->entrylen].selectable = !noselect;
434 (state->entry)[state->entrylen].inferiors = !noinferiors;
440 static int compare_names (struct folder_file *a, struct folder_file *b)
442 return m_strcmp(a->name, b->name);
445 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
446 IMAP_NAMESPACE_INFO * nsi, int nsilen,
451 char ns[LONG_STRING];
458 nsbuf[nsblen - 1] = '\0';
460 imap_cmd_start (idata, "NAMESPACE");
463 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
466 s = imap_next_word (idata->cmd.buf);
467 if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
468 /* There are three sections to the response, User, Other, Shared,
469 * and maybe more by extension */
470 for (type = IMAP_NS_PERSONAL; *s; type++) {
471 s = imap_next_word (s);
472 if (*s && ascii_strncasecmp (s, "NIL", 3)) {
474 while (*s && *s != ')') {
482 while (*s && *s != '\"') {
492 while (*s && !ISSPACE (*s)) {
498 s = imap_next_word (s);
499 /* delimiter is meaningless if namespace is "". Why does
500 * Cyrus provide one?! */
501 if (n && *s && *s == '\"') {
502 if (s[1] && s[2] == '\"')
504 else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
507 /* skip "" namespaces, they are already listed at the root */
508 if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
509 debug_print (3, ("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));