warning fixes
[apps/madmutt.git] / imap / browse.c
index 1a92a69..fd9c473 100644 (file)
 #include "lib/mem.h"
 #include "lib/str.h"
 #include "lib/intl.h"
+#include "lib/debug.h"
 
 #include "mutt.h"
+#include "ascii.h"
+#include "enter.h"
 #include "imap_private.h"
 
 /* -- forward declarations -- */
@@ -54,11 +57,12 @@ int imap_browse (char *path, struct browser_state *state)
   int i;
   int nsup;
   char ctmp;
-  int nns;
+  int nns = 0;
   char *cur_folder;
   short showparents = 0;
   int noselect;
   int noinferiors;
+  int save_lsub;
   IMAP_MBOX mx;
 
   if (imap_parse_path (path, &mx)) {
@@ -66,6 +70,8 @@ int imap_browse (char *path, struct browser_state *state)
     return -1;
   }
 
+  save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
+  unset_option (OPTIMAPCHECKSUBSCRIBED);
   strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST",
            sizeof (list_cmd));
 
@@ -75,8 +81,7 @@ int imap_browse (char *path, struct browser_state *state)
   if (!mx.mbox) {
     home_namespace = 1;
     mbox[0] = '\0';             /* Do not replace "" with "INBOX" here */
-    mx.mbox = safe_strdup (ImapHomeNamespace);
-    nns = 0;
+    mx.mbox = str_dup (ImapHomeNamespace);
     if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
       mutt_message _("Getting namespaces...");
 
@@ -97,9 +102,9 @@ int imap_browse (char *path, struct browser_state *state)
     imap_unquote_string (buf);  /* As kludgy as it gets */
     mbox[sizeof (mbox) - 1] = '\0';
     strncpy (mbox, buf, sizeof (mbox) - 1);
-    n = mutt_strlen (mbox);
+    n = str_len (mbox);
 
-    dprint (3, (debugfile, "imap_browse: mbox: %s\n", mbox));
+    debug_print (3, ("mbox: %s\n", mbox));
 
     /* if our target exists and has inferiors, enter it if we
      * aren't already going to */
@@ -115,7 +120,7 @@ int imap_browse (char *path, struct browser_state *state)
           imap_unmunge_mbox_name (cur_folder);
 
           if (!noinferiors && cur_folder[0] &&
-              (n = mutt_strlen (mbox)) < LONG_STRING - 1) {
+              (n = str_len (mbox)) < LONG_STRING - 1) {
             mbox[n++] = idata->delim;
             mbox[n] = '\0';
           }
@@ -130,7 +135,7 @@ int imap_browse (char *path, struct browser_state *state)
       if (!home_namespace)
         showparents = 1;
       imap_qualify_path (buf, sizeof (buf), &mx, mbox);
-      state->folder = safe_strdup (buf);
+      state->folder = str_dup (buf);
       n--;
     }
 
@@ -148,7 +153,7 @@ int imap_browse (char *path, struct browser_state *state)
       mbox[n] = '\0';
 
       if (showparents) {
-        dprint (3, (debugfile, "imap_init_browse: adding parent %s\n", mbox));
+        debug_print (3, ("adding parent %s\n", mbox));
         imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
       }
 
@@ -159,7 +164,7 @@ int imap_browse (char *path, struct browser_state *state)
         ctmp = mbox[n];
         mbox[n] = '\0';
         imap_qualify_path (buf, sizeof (buf), &mx, mbox);
-        state->folder = safe_strdup (buf);
+        state->folder = str_dup (buf);
       }
       mbox[n] = ctmp;
     }
@@ -173,7 +178,7 @@ int imap_browse (char *path, struct browser_state *state)
         imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
       if (!state->folder) {
         imap_qualify_path (buf, sizeof (buf), &mx, relpath);
-        state->folder = safe_strdup (buf);
+        state->folder = str_dup (buf);
       }
     }
   }
@@ -181,24 +186,24 @@ int imap_browse (char *path, struct browser_state *state)
   /* no namespace, no folder: set folder to host only */
   if (!state->folder) {
     imap_qualify_path (buf, sizeof (buf), &mx, NULL);
-    state->folder = safe_strdup (buf);
+    state->folder = str_dup (buf);
   }
 
   if (home_namespace && mbox[0] != '\0') {
     /* Listing the home namespace, so INBOX should be included. Home 
      * namespace is not "", so we have to list it explicitly. We ask the 
      * server to see if it has descendants. */
-    dprint (3, (debugfile, "imap_browse: adding INBOX\n"));
+    debug_print (3, ("adding INBOX\n"));
     if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
       goto fail;
   }
 
   nsup = state->entrylen;
 
-  dprint (3, (debugfile, "imap_browse: Quoting mailbox scan: %s -> ", mbox));
+  debug_print (3, ("Quoting mailbox scan: %s:\n", mbox));
   snprintf (buf, sizeof (buf), "%s%%", mbox);
   imap_quote_string (buf2, sizeof (buf2), buf);
-  dprint (3, (debugfile, "%s\n", buf2));
+  debug_print (3, ("%s\n", buf2));
   snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
   if (browse_add_list_result (idata, buf, state, 0))
     goto fail;
@@ -219,16 +224,20 @@ int imap_browse (char *path, struct browser_state *state)
       if (nsi[i].listable && !nsi[i].home_namespace) {
         imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
                          nsi[i].noinferiors, state, 0);
-        dprint (3, (debugfile, "imap_browse: adding namespace: %s\n",
-                    nsi[i].prefix));
+        debug_print (3, ("adding namespace: %s\n", nsi[i].prefix));
       }
   }
 
-  FREE (&mx.mbox);
+  if (save_lsub)
+    set_option (OPTIMAPCHECKSUBSCRIBED);
+
+  mem_free (&mx.mbox);
   return 0;
 
 fail:
-  FREE (&mx.mbox);
+  if (save_lsub)
+    set_option (OPTIMAPCHECKSUBSCRIBED);
+  mem_free (&mx.mbox);
   return -1;
 }
 
@@ -241,23 +250,19 @@ int imap_mailbox_create (const char *folder)
   short n;
 
   if (imap_parse_path (folder, &mx) < 0) {
-    dprint (1, (debugfile, "imap_mailbox_create: Bad starting path %s\n",
-                folder));
+    debug_print (1, ("Bad starting path %s\n", folder));
     return -1;
   }
 
   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
-    dprint (1,
-            (debugfile,
-             "imap_mailbox_create: Couldn't find open connection to %s",
-             mx.account.host));
+    debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
     goto fail;
   }
 
   strfcpy (buf, NONULL (mx.mbox), sizeof (buf));
 
   /* append a delimiter if necessary */
-  n = mutt_strlen (buf);
+  n = str_len (buf);
   if (n && (n < sizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
     buf[n++] = idata->delim;
     buf[n] = '\0';
@@ -266,7 +271,7 @@ int imap_mailbox_create (const char *folder)
   if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
     goto fail;
 
-  if (!mutt_strlen (buf)) {
+  if (!str_len (buf)) {
     mutt_error (_("Mailbox must have a name."));
     mutt_sleep (1);
     goto fail;
@@ -279,11 +284,11 @@ int imap_mailbox_create (const char *folder)
 
   mutt_sleep (0);
 
-  FREE (&mx.mbox);
+  mem_free (&mx.mbox);
   return 0;
 
 fail:
-  FREE (&mx.mbox);
+  mem_free (&mx.mbox);
   return -1;
 }
 
@@ -295,16 +300,12 @@ int imap_mailbox_rename (const char *mailbox)
   char newname[SHORT_STRING];
 
   if (imap_parse_path (mailbox, &mx) < 0) {
-    dprint (1, (debugfile, "imap_mailbox_rename: Bad source mailbox %s\n",
-                mailbox));
+    debug_print (1, ("Bad source mailbox %s\n", mailbox));
     return -1;
   }
 
   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
-    dprint (1,
-            (debugfile,
-             "imap_mailbox_rename: Couldn't find open connection to %s",
-             mx.account.host));
+    debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
     goto fail;
   }
 
@@ -313,7 +314,7 @@ int imap_mailbox_rename (const char *mailbox)
   if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
     goto fail;
 
-  if (!mutt_strlen (newname)) {
+  if (!str_len (newname)) {
     mutt_error (_("Mailbox must have a name."));
     mutt_sleep (1);
     goto fail;
@@ -328,11 +329,11 @@ int imap_mailbox_rename (const char *mailbox)
   mutt_message (_("Mailbox renamed."));
   mutt_sleep (0);
 
-  FREE (&mx.mbox);
+  mem_free (&mx.mbox);
   return 0;
 
 fail:
-  FREE (&mx.mbox);
+  mem_free (&mx.mbox);
   return -1;
 }
 
@@ -346,9 +347,7 @@ static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
   IMAP_MBOX mx;
 
   if (imap_parse_path (state->folder, &mx)) {
-    dprint (2, (debugfile,
-                "browse_add_list_result: current folder %s makes no sense\n",
-                state->folder));
+    debug_print (2, ("current folder %s makes no sense\n", state->folder));
     return -1;
   }
 
@@ -357,7 +356,7 @@ static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
   do {
     if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
                                   &idata->delim) != 0) {
-      FREE (&mx.mbox);
+      mem_free (&mx.mbox);
       return -1;
     }
 
@@ -366,14 +365,14 @@ static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
       if (isparent)
         noselect = 1;
       /* prune current folder from output */
-      if (isparent || mutt_strncmp (name, mx.mbox, mutt_strlen (name)))
+      if (isparent || str_ncmp (name, mx.mbox, str_len (name)))
         imap_add_folder (idata->delim, name, noselect, noinferiors, state,
                          isparent);
     }
   }
   while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
 
-  FREE (&mx.mbox);
+  mem_free (&mx.mbox);
   return 0;
 }
 
@@ -393,7 +392,7 @@ static void imap_add_folder (char delim, char *folder, int noselect,
   imap_unmunge_mbox_name (folder);
 
   if (state->entrylen + 1 == state->entrymax) {
-    safe_realloc (&state->entry,
+    mem_realloc (&state->entry,
                   sizeof (struct folder_file) * (state->entrymax += 256));
     memset (state->entry + state->entrylen, 0,
             (sizeof (struct folder_file) *
@@ -404,8 +403,8 @@ static void imap_add_folder (char delim, char *folder, int noselect,
   if (isparent)
     strfcpy (relpath, "../", sizeof (relpath));
   /* strip current folder from target, to render a relative path */
-  else if (!mutt_strncmp (mx.mbox, folder, mutt_strlen (mx.mbox)))
-    strfcpy (relpath, folder + mutt_strlen (mx.mbox), sizeof (relpath));
+  else if (!str_ncmp (mx.mbox, folder, str_len (mx.mbox)))
+    strfcpy (relpath, folder + str_len (mx.mbox), sizeof (relpath));
   else
     strfcpy (relpath, folder, sizeof (relpath));
 
@@ -413,20 +412,20 @@ static void imap_add_folder (char delim, char *folder, int noselect,
    * than at scan, since it's so expensive to scan. But that's big changes
    * to browser.c */
   if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
-    FREE (&mx.mbox);
+    mem_free (&mx.mbox);
     return;
   }
 
   imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
-  (state->entry)[state->entrylen].name = safe_strdup (tmp);
+  (state->entry)[state->entrylen].name = str_dup (tmp);
 
   /* mark desc with delim in browser if it can have subfolders */
-  if (!isparent && !noinferiors && mutt_strlen (relpath) < sizeof (relpath) - 1) {
-    relpath[mutt_strlen (relpath) + 1] = '\0';
-    relpath[mutt_strlen (relpath)] = delim;
+  if (!isparent && !noinferiors && str_len (relpath) < sizeof (relpath) - 1) {
+    relpath[str_len (relpath) + 1] = '\0';
+    relpath[str_len (relpath)] = delim;
   }
 
-  (state->entry)[state->entrylen].desc = safe_strdup (relpath);
+  (state->entry)[state->entrylen].desc = str_dup (relpath);
 
   (state->entry)[state->entrylen].imap = 1;
   /* delimiter at the root is useless. */
@@ -437,12 +436,12 @@ static void imap_add_folder (char delim, char *folder, int noselect,
   (state->entry)[state->entrylen].inferiors = !noinferiors;
   (state->entrylen)++;
 
-  FREE (&mx.mbox);
+  mem_free (&mx.mbox);
 }
 
 static int compare_names (struct folder_file *a, struct folder_file *b)
 {
-  return mutt_strcmp (a->name, b->name);
+  return str_cmp (a->name, b->name);
 }
 
 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
@@ -509,8 +508,7 @@ static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
             }
             /* skip "" namespaces, they are already listed at the root */
             if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
-              dprint (3,
-                      (debugfile, "browse_get_namespace: adding %s\n", ns));
+              debug_print (3, ("adding %s\n", ns));
               nsi->type = type;
               /* Cyrus doesn't append the delimiter to the namespace,
                * but UW-IMAP does. We'll strip it here and add it back