use my own APIS for headers, parameters and so on
[apps/madmutt.git] / imap / imap.c
index 022415f..57648c6 100644 (file)
 # include "config.h"
 #endif
 
+#include <lib-lib/mem.h>
+#include <lib-lib/ascii.h>
+#include <lib-lib/str.h>
+#include <lib-lib/buffer.h>
+
 #include "mutt.h"
-#include "ascii.h"
-#include "buffer.h"
 #include "mx.h"
 #include "globals.h"
 #include "sort.h"
@@ -29,9 +32,7 @@
 #endif
 #include "buffy.h"
 
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
+#include <lib-lib/macros.h>
 #include "lib/debug.h"
 
 #include <unistd.h>
@@ -66,7 +67,7 @@ int imap_access (const char *path, int flags)
   if (!(idata = imap_conn_find (&mx.account,
                                 option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW :
                                 0))) {
-    mem_free (&mx.mbox);
+    p_delete(&mx.mbox);
     return -1;
   }
 
@@ -74,11 +75,11 @@ int imap_access (const char *path, int flags)
 
   /* we may already be in the folder we're checking */
   if (!ascii_strcmp(idata->mailbox, mx.mbox)) {
-    mem_free (&mx.mbox);
+    p_delete(&mx.mbox);
     return 0;
   }
 
-  mem_free (&mx.mbox);
+  p_delete(&mx.mbox);
   imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
 
   if (mutt_bit_isset (idata->capabilities, IMAP4REV1))
@@ -138,7 +139,7 @@ int imap_delete_mailbox (CONTEXT * ctx, IMAP_MBOX mx)
     if (!(idata = imap_conn_find (&mx.account,
                                   option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW
                                   : 0))) {
-      mem_free (&mx.mbox);
+      p_delete(&mx.mbox);
       return -1;
     }
   }
@@ -214,7 +215,7 @@ int imap_read_literal (FILE * fp, IMAP_DATA * idata, long bytes, progress_t* bar
     if (bar && !(pos % 1024))
       mutt_progress_bar (bar, pos);
 #ifdef DEBUG
-    if (DebugLevel >= IMAP_LOG_LTRL)
+    if (DebugFile && DebugLevel >= IMAP_LOG_LTRL)
       fputc (c, DebugFile);
 #endif
   }
@@ -243,7 +244,7 @@ void imap_expunge_mailbox (IMAP_DATA * idata)
       if (idata->cache[cacheno].uid == HEADER_DATA (h)->uid &&
           idata->cache[cacheno].path) {
         unlink (idata->cache[cacheno].path);
-        mem_free (&idata->cache[cacheno].path);
+        p_delete(&idata->cache[cacheno].path);
       }
 
       imap_free_header_data (&h->data);
@@ -387,7 +388,7 @@ IMAP_DATA *imap_conn_find (const ACCOUNT * account, int flags)
     else
       mutt_account_unsetpass (&idata->conn->account);
 
-    mem_free (&idata->capstr);
+    p_delete(&idata->capstr);
   }
   if (new && idata->state == IMAP_AUTHENTICATED) {
     imap_get_delim (idata);
@@ -462,7 +463,7 @@ int imap_open_connection (IMAP_DATA * idata)
     idata->state = IMAP_AUTHENTICATED;
     if (imap_check_capabilities (idata) != 0)
       goto bail;
-    mem_free (&idata->capstr);
+    p_delete(&idata->capstr);
   }
   else {
     imap_error ("imap_open_connection()", buf);
@@ -475,7 +476,7 @@ err_close_conn:
   mutt_socket_close (idata->conn);
   idata->state = IMAP_DISCONNECTED;
 bail:
-  mem_free (&idata->capstr);
+  p_delete(&idata->capstr);
   return -1;
 }
 
@@ -492,8 +493,7 @@ 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;
@@ -504,8 +504,7 @@ static char *imap_get_flags (LIST ** hflags, char *s)
   *hflags = flags;
 
   while (*s && *s != ')') {
-    s++;
-    SKIPWS (s);
+    s = vskipspaces(s + 1);
     flag_word = s;
     while (*s && (*s != ')') && !ISSPACE (*s))
       s++;
@@ -557,12 +556,12 @@ 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));
-  mem_free (&(idata->mailbox));
-  idata->mailbox = str_dup (buf);
+  p_delete(&(idata->mailbox));
+  idata->mailbox = m_strdup(buf);
   imap_qualify_path (buf, sizeof (buf), &mx, idata->mailbox);
 
-  mem_free (&(ctx->path));
-  ctx->path = str_dup (buf);
+  p_delete(&(ctx->path));
+  ctx->path = m_strdup(buf);
 
   idata->ctx = ctx;
 
@@ -690,8 +689,8 @@ int imap_open_mailbox (CONTEXT * ctx)
   }
 
   ctx->hdrmax = count;
-  ctx->hdrs = mem_calloc (count, sizeof (HEADER *));
-  ctx->v2r = mem_calloc (count, sizeof (int));
+  ctx->hdrs = p_new(HEADER *, count);
+  ctx->v2r = p_new(int, count);
   ctx->msgcount = 0;
   if (count && (imap_read_headers (idata, 0, count - 1) < 0)) {
     mutt_error _("Error opening mailbox");
@@ -701,14 +700,14 @@ int imap_open_mailbox (CONTEXT * ctx)
   }
 
   debug_print (2, ("msgcount is %d\n", ctx->msgcount));
-  mem_free (&mx.mbox);
+  p_delete(&mx.mbox);
   return 0;
 
 fail:
   if (idata->state == IMAP_SELECTED)
     idata->state = IMAP_AUTHENTICATED;
 fail_noidata:
-  mem_free (&mx.mbox);
+  p_delete(&mx.mbox);
   return -1;
 }
 
@@ -727,7 +726,7 @@ int imap_open_mailbox_append (CONTEXT * ctx)
    * ctx is brand new and mostly empty */
 
   if (!(idata = imap_conn_find (&(mx.account), 0))) {
-    mem_free(&mx.mbox);
+    p_delete(&mx.mbox);
     return (-1);
   }
   conn = idata->conn;
@@ -737,7 +736,7 @@ int imap_open_mailbox_append (CONTEXT * ctx)
 
   imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
 
-  mem_free(&mx.mbox);
+  p_delete(&mx.mbox);
 
   /* really we should also check for W_OK */
   if (!imap_access (ctx->path, F_OK))
@@ -761,8 +760,8 @@ void imap_logout (IMAP_DATA * idata)
   idata->status = IMAP_BYE;
   imap_cmd_start (idata, "LOGOUT");
   while (imap_cmd_step (idata) == IMAP_CMD_CONTINUE);
-  mem_free (&idata->cmd.buf);
-  mem_free (&idata);
+  p_delete(&idata->cmd.buf);
+  p_delete(&idata);
 }
 
 /*
@@ -784,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
@@ -814,8 +814,7 @@ int imap_make_msg_set (IMAP_DATA * idata, BUFFER * buf, int flag, int changed)
   int started = 0;
 
   /* make copy of header pointers to sort in natural order */
-  hdrs = mem_calloc (idata->ctx->msgcount, sizeof (HEADER *));
-  memcpy (hdrs, idata->ctx->hdrs, idata->ctx->msgcount * sizeof (HEADER *));
+  hdrs = p_dup(idata->ctx->hdrs, idata->ctx->msgcount);
 
   if (Sort != SORT_ORDER) {
     oldsort = Sort;
@@ -870,7 +869,7 @@ int imap_make_msg_set (IMAP_DATA * idata, BUFFER * buf, int flag, int changed)
     }
   }
 
-  mem_free (&hdrs);
+  p_delete(&hdrs);
 
   return count;
 }
@@ -905,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) */
@@ -916,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
@@ -975,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)) {
@@ -1053,10 +1052,10 @@ int imap_sync_mailbox (CONTEXT * ctx, int expunge, int *index_hint)
   rc = 0;
 out:
   if (cmd.data)
-    mem_free (&cmd.data);
+    p_delete(&cmd.data);
   if (appendctx) {
     mx_fastclose_mailbox (appendctx);
-    mem_free (&appendctx);
+    p_delete(&appendctx);
   }
   return rc;
 }
@@ -1082,7 +1081,7 @@ void imap_close_mailbox (CONTEXT * ctx)
     }
 
     idata->reopen &= IMAP_REOPEN_ALLOW;
-    mem_free (&(idata->mailbox));
+    p_delete(&(idata->mailbox));
     mutt_free_list (&idata->flags);
     idata->ctx = NULL;
   }
@@ -1094,7 +1093,7 @@ void imap_close_mailbox (CONTEXT * ctx)
   for (i = 0; i < IMAP_CACHE_LEN; i++) {
     if (idata->cache[i].path) {
       unlink (idata->cache[i].path);
-      mem_free (&idata->cache[i].path);
+      p_delete(&idata->cache[i].path);
     }
   }
 }
@@ -1136,9 +1135,12 @@ int imap_check_mailbox (CONTEXT * ctx, int *index_hint, int force)
   return result;
 }
 
-/* returns count of recent messages if new = 1, else count of total messages.
- * (useful for at least postponed function)
- * Question of taste: use RECENT or UNSEEN for new?
+/*
+ * count messages:
+ *      new == 1:  recent
+ *      new == 2:  unseen
+ *      otherwise: total
+ * return:
  *   0+   number of messages in mailbox
  *  -1    error while polling mailboxes
  */
@@ -1163,30 +1165,30 @@ int imap_mailbox_check (char *path, int new)
     connflags = M_IMAP_CONN_NONEW;
 
   if (!(idata = imap_conn_find (&(mx.account), connflags))) {
-    mem_free (&mx.mbox);
+    p_delete(&mx.mbox);
     return -1;
   }
   conn = idata->conn;
 
   imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
-  mem_free (&mx.mbox);
+  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)) {
     snprintf (buf, sizeof (buf), "STATUS %s (%s)", mbox,
-              new ? "RECENT" : "MESSAGES");
+              new == 1 ? "RECENT" : (new == 2 ? "UNSEEN" : "MESSAGES"));
   }
   else
     /* Server does not support STATUS, and this is not the current mailbox.
@@ -1205,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)) {
@@ -1307,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;
@@ -1342,18 +1343,18 @@ 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) {
-    mem_free (&buf.data);
+    p_delete(&buf.data);
     return -1;
   }
   if (imap_exec (idata, buf.data, 0) < 0) {
-    mem_free (&buf.data);
+    p_delete(&buf.data);
     return -1;
   }
 
-  mem_free (&buf.data);
+  p_delete(&buf.data);
   return 0;
 }
 
@@ -1441,7 +1442,7 @@ int imap_subscribe (char *path, int subscribe)
   BUFFER err, token;
   IMAP_MBOX mx;
 
-  if (mx_get_magic (path) == M_IMAP || imap_parse_path (path, &mx)) {
+  if (mx_get_magic (path) != M_IMAP || imap_parse_path (path, &mx) < 0) {
     mutt_error (_("Bad mailbox name"));
     return -1;
   }
@@ -1454,14 +1455,14 @@ 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\"",
               subscribe ? "" : "un", path);
     if (mutt_parse_rc_line (mbox, &token, &err))
       debug_print (1, ("Error adding subscribed mailbox: %s\n", errstr));
-    mem_free (&token.data);
+    p_delete(&token.data);
   }
 
   if (subscribe)
@@ -1475,11 +1476,11 @@ int imap_subscribe (char *path, int subscribe)
   if (imap_exec (idata, buf, 0) < 0)
     goto fail;
 
-  mem_free (&mx.mbox);
+  p_delete(&mx.mbox);
   return 0;
 
 fail:
-  mem_free (&mx.mbox);
+  p_delete(&mx.mbox);
   return -1;
 }
 
@@ -1505,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);
@@ -1531,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);
@@ -1559,15 +1560,15 @@ 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);
   }
 
   /* don't open a new socket just for completion. Instead complete over
    * known mailboxes/hooks/etc */
   if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NONEW))) {
-    mem_free (&mx.mbox);
-    strfcpy (dest, path, dlen);
+    p_delete(&mx.mbox);
+    m_strcpy(dest, dlen, path);
     return imap_complete_hosts (dest, dlen);
   }
   conn = idata->conn;
@@ -1586,7 +1587,7 @@ int imap_complete (char *dest, size_t dlen, char *path) {
   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))
@@ -1599,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;
       }
@@ -1622,7 +1623,7 @@ int imap_complete (char *dest, size_t dlen, char *path) {
     imap_qualify_path (dest, dlen, &mx, completion);
     mutt_pretty_mailbox (dest);
 
-    mem_free (&mx.mbox);
+    p_delete(&mx.mbox);
     return 0;
   }