rationalize list handling in mutt a bit.
[apps/madmutt.git] / imap / imap.c
index 3f988e8..dbe6f76 100644 (file)
 #endif
 
 #include <lib-lib/mem.h>
+#include <lib-lib/ascii.h>
+#include <lib-lib/str.h>
+#include <lib-lib/buffer.h>
+#include <lib-lib/debug.h>
 
 #include "mutt.h"
-#include "ascii.h"
-#include "buffer.h"
 #include "mx.h"
 #include "globals.h"
 #include "sort.h"
 #include "message.h"
 #include "imap_private.h"
 #if defined(USE_SSL) || defined(USE_GNUTLS)
-# include "mutt_ssl.h"
+# include <lib-sys/mutt_ssl.h>
 #endif
 #include "buffy.h"
 
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
-#include "lib/debug.h"
+#include <lib-lib/macros.h>
 
 #include <unistd.h>
 #include <ctype.h>
@@ -45,7 +44,7 @@
 
 /* imap forward declarations */
 static int imap_get_delim (IMAP_DATA * idata);
-static char *imap_get_flags (LIST ** hflags, char *s);
+static char *imap_get_flags (string_list_t ** hflags, char *s);
 static int imap_check_acl (IMAP_DATA * idata);
 static int imap_check_capabilities (IMAP_DATA * idata);
 static void imap_set_flag (IMAP_DATA * idata, int aclbit, int flag,
@@ -267,14 +266,14 @@ static int imap_get_delim (IMAP_DATA * idata)
    * than getting the delim wrong */
   idata->delim = '/';
 
-  imap_cmd_start (idata, "LIST \"\" \"\"");
+  imap_cmd_start (idata, "string_list_t \"\" \"\"");
 
   do {
     if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
       break;
 
     s = imap_next_word (idata->cmd.buf);
-    if (ascii_strncasecmp ("LIST", s, 4) == 0) {
+    if (ascii_strncasecmp ("string_list_t", s, 4) == 0) {
       s = imap_next_word (s);
       s = imap_next_word (s);
       if (s && s[0] == '\"' && s[1] && s[2] == '\"')
@@ -483,9 +482,9 @@ bail:
 
 /* imap_get_flags: Make a simple list out of a FLAGS response.
  *   return stream following FLAGS response */
-static char *imap_get_flags (LIST ** hflags, char *s)
+static char *imap_get_flags (string_list_t ** hflags, char *s)
 {
-  LIST *flags;
+  string_list_t *flags;
   char *flag_word;
   char ctmp;
 
@@ -494,20 +493,18 @@ static char *imap_get_flags (LIST ** hflags, char *s)
     debug_print (1, ("not a FLAGS response: %s\n", s));
     return NULL;
   }
-  s += 5;
-  SKIPWS (s);
+  s = vskipspaces(s + 5);
   if (*s != '(') {
     debug_print (1, ("bogus FLAGS response: %s\n", s));
     return NULL;
   }
 
   /* create list, update caller's flags handle */
-  flags = mutt_new_list ();
+  flags = string_item_new();
   *hflags = flags;
 
   while (*s && *s != ')') {
-    s++;
-    SKIPWS (s);
+    s = vskipspaces(s + 1);
     flag_word = s;
     while (*s && (*s != ')') && !ISSPACE (*s))
       s++;
@@ -521,7 +518,7 @@ static char *imap_get_flags (LIST ** hflags, char *s)
   /* note bad flags response */
   if (*s != ')') {
     debug_print (1, ("Unterminated FLAGS response: %s\n", s));
-    mutt_free_list (hflags);
+    string_list_wipe(hflags);
 
     return NULL;
   }
@@ -560,11 +557,11 @@ int imap_open_mailbox (CONTEXT * ctx)
   /* Clean up path and replace the one in the ctx */
   imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
   p_delete(&(idata->mailbox));
-  idata->mailbox = str_dup (buf);
+  idata->mailbox = m_strdup(buf);
   imap_qualify_path (buf, sizeof (buf), &mx, idata->mailbox);
 
   p_delete(&(ctx->path));
-  ctx->path = str_dup (buf);
+  ctx->path = m_strdup(buf);
 
   idata->ctx = ctx;
 
@@ -604,7 +601,7 @@ int imap_open_mailbox (CONTEXT * ctx)
     else if (ascii_strncasecmp ("OK [PERMANENTFLAGS", pc, 18) == 0) {
       debug_print (2, ("Getting mailbox PERMANENTFLAGS\n"));
       /* safe to call on NULL */
-      mutt_free_list (&(idata->flags));
+      string_list_wipe(&(idata->flags));
       /* skip "OK [PERMANENT" so syntax is the same as FLAGS */
       pc += 13;
       if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)
@@ -657,7 +654,7 @@ int imap_open_mailbox (CONTEXT * ctx)
     if (!idata->flags)
       debug_print (3, ("No folder flags found\n"));
     else {
-      LIST *t = idata->flags;
+      string_list_t *t = idata->flags;
 
       debug_print (3, ("Mailbox flags:\n"));
 
@@ -786,12 +783,13 @@ int imap_close_connection (CONTEXT *ctx)
 
 /* imap_set_flag: append str to flags if we currently have permission
  *   according to aclbit */
-static void imap_set_flag (IMAP_DATA * idata, int aclbit, int flag,
-                           const char *str, char *flags, size_t flsize)
+static void imap_set_flag(IMAP_DATA *idata, int aclbit, int flag,
+                          const char *str, char *flags, size_t flsize)
 {
-  if (mutt_bit_isset (idata->rights, aclbit))
-    if (flag)
-      str_cat (flags, flsize, str);
+    if (mutt_bit_isset(idata->rights, aclbit)) {
+        if (flag)
+            m_strcat(flags, flsize, str);
+    }
 }
 
 /* imap_make_msg_set: make an IMAP4rev1 UID message set out of a set of
@@ -906,7 +904,7 @@ int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd,
   if (mutt_bit_isset (idata->rights, ACL_WRITE))
     imap_add_keywords (flags, hdr, idata->flags, sizeof (flags));
 
-  str_skip_trailws (flags);
+  m_strrtrim(flags);
 
   /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to
    * explicitly revoke all system flags (if we have permission) */
@@ -917,7 +915,7 @@ int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd,
     imap_set_flag (idata, ACL_WRITE, 1, "\\Answered ", flags, sizeof (flags));
     imap_set_flag (idata, ACL_DELETE, 1, "\\Deleted ", flags, sizeof (flags));
 
-    str_skip_trailws (flags);
+    m_strrtrim(flags);
 
     mutt_buffer_addstr (cmd, " -FLAGS.SILENT (");
   } else
@@ -976,7 +974,7 @@ int imap_sync_mailbox (CONTEXT * ctx, int expunge, int *index_hint)
   if ((rc = imap_check_mailbox (ctx, index_hint, 0)) != 0)
     return rc;
 
-  memset (&cmd, 0, sizeof (cmd));
+  p_clear(&cmd, 1);
 
   /* if we are expunging anyway, we can do deleted messages very quickly... */
   if (expunge && mutt_bit_isset (idata->rights, ACL_DELETE)) {
@@ -1084,7 +1082,7 @@ void imap_close_mailbox (CONTEXT * ctx)
 
     idata->reopen &= IMAP_REOPEN_ALLOW;
     p_delete(&(idata->mailbox));
-    mutt_free_list (&idata->flags);
+    string_list_wipe(&idata->flags);
     idata->ctx = NULL;
   }
 
@@ -1176,16 +1174,16 @@ int imap_mailbox_check (char *path, int new)
   p_delete(&mx.mbox);
 
   imap_munge_mbox_name (mbox, sizeof (mbox), buf);
-  strfcpy (mbox_unquoted, buf, sizeof (mbox_unquoted));
+  m_strcpy(mbox_unquoted, sizeof(mbox_unquoted), buf);
 
   /* The draft IMAP implementor's guide warns againts using the STATUS
    * command on a mailbox that you have selected 
    */
 
-  if (str_cmp (mbox_unquoted, idata->mailbox) == 0
+  if (m_strcmp(mbox_unquoted, idata->mailbox) == 0
       || (ascii_strcasecmp (mbox_unquoted, "INBOX") == 0
-          && str_casecmp (mbox_unquoted, idata->mailbox) == 0)) {
-    strfcpy (buf, "NOOP", sizeof (buf));
+          && m_strcasecmp(mbox_unquoted, idata->mailbox) == 0)) {
+    m_strcpy(buf, sizeof(buf), "NOOP");
   }
   else if (mutt_bit_isset (idata->capabilities, IMAP4REV1) ||
            mutt_bit_isset (idata->capabilities, STATUS)) {
@@ -1209,8 +1207,8 @@ int imap_mailbox_check (char *path, int new)
       /* The mailbox name may or may not be quoted here. We could try to 
        * munge the server response and compare with quoted (or vise versa)
        * but it is probably more efficient to just strncmp against both. */
-      if (str_ncmp (mbox_unquoted, s, str_len (mbox_unquoted)) == 0
-          || str_ncmp (mbox, s, str_len (mbox)) == 0) {
+      if (m_strncmp(mbox_unquoted, s, m_strlen(mbox_unquoted)) == 0
+          || m_strncmp(mbox, s, m_strlen(mbox)) == 0) {
         s = imap_next_word (s);
         s = imap_next_word (s);
         if (isdigit ((unsigned char) *s)) {
@@ -1311,9 +1309,8 @@ static int imap_compile_search (const pattern_t* pat, BUFFER* buf)
         mutt_buffer_addch (buf, ' ');
 
         /* and field */
-        *delim = ':';
-        delim++;
-        SKIPWS(delim);
+        *delim++ = ':';
+        delim = vskipspaces(delim);
         imap_quote_string (term, sizeof (term), delim);
         mutt_buffer_addstr (buf, term);
         break;
@@ -1346,7 +1343,7 @@ int imap_search (CONTEXT* ctx, const pattern_t* pat) {
   if (!do_search (pat, 1))
     return 0;
 
-  memset (&buf, 0, sizeof (buf));
+  p_clear(&buf, 1);
   mutt_buffer_addstr (&buf, "UID SEARCH ");
   if (imap_compile_search (pat, &buf) < 0) {
     p_delete(&buf.data);
@@ -1364,7 +1361,7 @@ int imap_search (CONTEXT* ctx, const pattern_t* pat) {
 /* all this listing/browsing is a mess. I don't like that name is a pointer
  *   into idata->buf (used to be a pointer into the passed in buffer, just
  *   as bad), nor do I like the fact that the fetch is done here. This
- *   code can't possibly handle non-LIST untagged responses properly.
+ *   code can't possibly handle non-string_list_t untagged responses properly.
  *   FIXME. ?! */
 int imap_parse_list_response (IMAP_DATA * idata, char **name, int *noselect,
                               int *noinferiors, char *delim)
@@ -1382,7 +1379,7 @@ int imap_parse_list_response (IMAP_DATA * idata, char **name, int *noselect,
     return -1;
 
   s = imap_next_word (idata->cmd.buf);
-  if ((ascii_strncasecmp ("LIST", s, 4) == 0) ||
+  if ((ascii_strncasecmp ("string_list_t", s, 4) == 0) ||
       (ascii_strncasecmp ("LSUB", s, 4) == 0)) {
     *noselect = 0;
     *noinferiors = 0;
@@ -1458,7 +1455,7 @@ int imap_subscribe (char *path, int subscribe)
   imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
 
   if (option (OPTIMAPCHECKSUBSCRIBED)) {
-    memset (&token, 0, sizeof (token));
+    p_clear(&token, 1);
     err.data = errstr;
     err.dsize = sizeof (errstr);
     snprintf (mbox, sizeof (mbox), "%smailboxes \"%s\"",
@@ -1509,14 +1506,14 @@ static int imap_complete_hosts (char *dest, size_t len) {
   int matchlen;
   int i = 0;
 
-  matchlen = str_len (dest);
+  matchlen = m_strlen(dest);
   if (list_empty (Incoming))
     return (-1);
   for (i = 0; i < Incoming->length; i++) {
     mailbox = (BUFFY*) Incoming->data[i];
-    if (!str_ncmp (dest, mailbox->path, matchlen)) {
+    if (!m_strncmp(dest, mailbox->path, matchlen)) {
       if (rc) {
-        strfcpy (dest, mailbox->path, len);
+        m_strcpy(dest, len, mailbox->path);
         rc = 0;
       } else
         longest_common_prefix (dest, mailbox->path, matchlen, len);
@@ -1535,9 +1532,9 @@ static int imap_complete_hosts (char *dest, size_t len) {
     url.user = NULL;
     url.path = NULL;
     url_ciss_tostring (&url, urlstr, sizeof (urlstr), 0);
-    if (!str_ncmp (dest, urlstr, matchlen)) {
+    if (!m_strncmp(dest, urlstr, matchlen)) {
       if (rc) {
-        strfcpy (dest, urlstr, len);
+        m_strcpy(dest, len, urlstr);
         rc = 0;
       } else
         longest_common_prefix (dest, urlstr, matchlen, len);
@@ -1563,7 +1560,7 @@ int imap_complete (char *dest, size_t dlen, char *path) {
   IMAP_MBOX mx;
 
   if (imap_parse_path (path, &mx) || !mx.mbox) {
-    strfcpy (dest, path, dlen);
+    m_strcpy(dest, dlen, path);
     return imap_complete_hosts (dest, dlen);
   }
 
@@ -1571,7 +1568,7 @@ int imap_complete (char *dest, size_t dlen, char *path) {
    * known mailboxes/hooks/etc */
   if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NONEW))) {
     p_delete(&mx.mbox);
-    strfcpy (dest, path, dlen);
+    m_strcpy(dest, dlen, path);
     return imap_complete_hosts (dest, dlen);
   }
   conn = idata->conn;
@@ -1585,12 +1582,12 @@ int imap_complete (char *dest, size_t dlen, char *path) {
 
   /* fire off command */
   snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
-            option (OPTIMAPLSUB) ? "LSUB" : "LIST", list);
+            option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", list);
 
   imap_cmd_start (idata, buf);
 
   /* and see what the results are */
-  strfcpy (completion, NONULL (mx.mbox), sizeof (completion));
+  m_strcpy(completion, sizeof(completion), NONULL(mx.mbox));
   do {
     if (imap_parse_list_response (idata, &list_word, &noselect, &noinferiors,
                                   &delim))
@@ -1603,14 +1600,14 @@ int imap_complete (char *dest, size_t dlen, char *path) {
       /* if the folder isn't selectable, append delimiter to force browse
        * to enter it on second tab. */
       if (noselect) {
-        clen = str_len (list_word);
+        clen = m_strlen(list_word);
         list_word[clen++] = delim;
         list_word[clen] = '\0';
       }
       /* copy in first word */
       if (!completions) {
-        strfcpy (completion, list_word, sizeof (completion));
-        matchlen = str_len (completion);
+        m_strcpy(completion, sizeof(completion), list_word);
+        matchlen = m_strlen(completion);
         completions++;
         continue;
       }