rename a bunch of modules
[apps/madmutt.git] / lib-mx / mbox.c
index 81beb52..9729c5e 100644 (file)
@@ -7,7 +7,7 @@
  * please see the file GPL in the top level source directory.
  */
 
-/* This file contains code to parse ``mbox'' and ``mmdf'' style mailboxes */
+/* This file contains code to parse ``mbox'' style mailboxes */
 
 #include <lib-lib/lib-lib.h>
 
 
 /* struct used by mutt_sync_mailbox() to store new offsets */
 struct m_update_t {
-  short valid;
-  off_t hdr;
-  off_t body;
-  long lines;
-  off_t length;
+    short valid;
+    off_t hdr;
+    off_t body;
+    long lines;
+    off_t length;
 };
 
-
-static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
+static int mbox_open_new_message(MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
 {
-  msg->fp = dest->fp;
-  return 0;
+    msg->fp = dest->fp;
+    return 0;
 }
 
 /* prototypes */
@@ -47,11 +46,11 @@ static int mbox_reopen_mailbox (CONTEXT*, int*);
  * excl - exclusive lock?
  * retry - should retry if unable to lock?
  */
-int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
+int mbox_lock_mailbox(CONTEXT *ctx, int excl, int retry)
 {
   int r;
 
-  if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
+  if ((r = mx_lock_file(ctx->path, fileno(ctx->fp), excl, 1, retry)) == 0)
     ctx->locked = 1;
   else if (retry && !excl) {
     ctx->readonly = 1;
@@ -71,122 +70,6 @@ static void mbox_unlock_mailbox (CONTEXT * ctx)
   }
 }
 
-static int mmdf_parse_mailbox (CONTEXT * ctx)
-{
-  char buf[HUGE_STRING];
-  char return_path[LONG_STRING];
-  int count = 0, oldmsgcount = ctx->msgcount;
-  int lines;
-  time_t t, tz;
-  off_t loc, tmploc;
-  HEADER *hdr;
-  struct stat sb;
-
-  if (stat (ctx->path, &sb) == -1) {
-    mutt_perror (ctx->path);
-    return (-1);
-  }
-  ctx->mtime = sb.st_mtime;
-  ctx->size = sb.st_size;
-
-  /* precompute the local timezone to speed up calculation of the
-     received time */
-  tz = mutt_local_tz (0);
-
-  buf[sizeof (buf) - 1] = 0;
-
-  for (;;) {
-    if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
-      break;
-
-    if (m_strcmp(buf, MMDF_SEP) == 0) {
-      loc = ftello (ctx->fp);
-
-      count++;
-      if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
-        mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
-                      (int) (loc / (ctx->size / 100 + 1)));
-
-
-      if (ctx->msgcount == ctx->hdrmax)
-        mx_alloc_memory (ctx);
-      ctx->hdrs[ctx->msgcount] = hdr = header_new();
-      hdr->offset = loc;
-      hdr->index = ctx->msgcount;
-
-      if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
-        /* TODO: memory leak??? */
-        break;
-      }
-
-      return_path[0] = 0;
-
-      if (!is_from (buf, return_path, sizeof (return_path), &t)) {
-        if (fseeko (ctx->fp, loc, SEEK_SET) != 0) {
-          mutt_error _("Mailbox is corrupt!");
-
-          return (-1);
-        }
-      }
-      else
-        hdr->received = t - tz;
-
-      hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
-
-      loc = ftello (ctx->fp);
-
-      if (hdr->content->length > 0 && hdr->lines > 0) {
-        tmploc = loc + hdr->content->length;
-
-        if (0 < tmploc && tmploc < ctx->size) {
-          if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
-              fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
-              m_strcmp(MMDF_SEP, buf) != 0) {
-            fseeko (ctx->fp, loc, SEEK_SET);
-            hdr->content->length = -1;
-          }
-        }
-        else
-          hdr->content->length = -1;
-      }
-      else
-        hdr->content->length = -1;
-
-      if (hdr->content->length < 0) {
-        lines = -1;
-        do {
-          loc = ftello (ctx->fp);
-          if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
-            break;
-          lines++;
-        } while (m_strcmp(buf, MMDF_SEP) != 0);
-
-        hdr->lines = lines;
-        hdr->content->length = loc - hdr->content->offset;
-      }
-
-      if (!hdr->env->return_path && return_path[0])
-        hdr->env->return_path =
-          rfc822_parse_adrlist (hdr->env->return_path, return_path);
-
-      if (!hdr->env->from)
-        hdr->env->from = address_list_dup (hdr->env->return_path);
-
-      ctx->msgcount++;
-    }
-    else {
-      mutt_error _("Mailbox is corrupt!");
-
-      return (-1);
-    }
-  }
-
-  if (ctx->msgcount > oldmsgcount)
-    mx_update_context (ctx, ctx->msgcount - oldmsgcount);
-
-  return (0);
-}
-
 /* Note that this function is also called when new mail is appended to the
  * currently open folder, and NOT just when the mailbox is initially read.
  *
@@ -340,31 +223,27 @@ static int mbox_parse_mailbox (CONTEXT * ctx)
 
 #undef PREV
 
-/* open a mbox or mmdf style mailbox */
+/* open a mbox style mailbox */
 static int mbox_open_mailbox (CONTEXT * ctx)
 {
-  int rc;
+    int rc;
 
-  if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
-    mutt_perror (ctx->path);
-    return (-1);
-  }
-  mutt_block_signals ();
-  if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
-    mutt_unblock_signals ();
-    return (-1);
-  }
+    if (!(ctx->fp = fopen(ctx->path, "r"))) {
+        mutt_perror(ctx->path);
+        return -1;
+    }
 
-  if (ctx->magic == M_MBOX)
-    rc = mbox_parse_mailbox (ctx);
-  else if (ctx->magic == M_MMDF)
-    rc = mmdf_parse_mailbox (ctx);
-  else
-    rc = -1;
+    mutt_block_signals();
+    if (mbox_lock_mailbox(ctx, 0, 1) < 0) {
+        mutt_unblock_signals();
+        return -1;
+    }
 
-  mbox_unlock_mailbox (ctx);
-  mutt_unblock_signals ();
-  return (rc);
+    rc = ctx->magic == M_MBOX ? mbox_parse_mailbox(ctx) : -1;
+
+    mbox_unlock_mailbox(ctx);
+    mutt_unblock_signals();
+    return rc;
 }
 
 /* check to see if the mailbox has changed on disk.
@@ -416,13 +295,9 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
        */
       fseeko (ctx->fp, ctx->size, SEEK_SET);
       if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
-        if ((ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0)
-            || (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buffer) == 0)) {
+        if (ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0) {
           fseeko (ctx->fp, ctx->size, SEEK_SET);
-          if (ctx->magic == M_MBOX)
-            mbox_parse_mailbox (ctx);
-          else
-            mmdf_parse_mailbox (ctx);
+          mbox_parse_mailbox (ctx);
 
           /* Only unlock the folder if it was locked inside of this routine.
            * It may have been locked elsewhere, like in
@@ -466,24 +341,25 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
   return (-1);
 }
 
-static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
-  int rc = 0;
+static int mbox_check_mailbox(CONTEXT *ctx, int *index_hint, int lock)
+{
+    int rc = 0;
 
-  if (lock) {
-    mutt_block_signals ();
-    if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
-      mutt_unblock_signals ();
-      return M_LOCKED;
+    if (lock) {
+        mutt_block_signals();
+        if (mbox_lock_mailbox(ctx, 0, 0) < 0) {
+            mutt_unblock_signals();
+            return M_LOCKED;
+        }
     }
-  }
 
-  rc = _mbox_check_mailbox (ctx, index_hint);
+    rc = _mbox_check_mailbox(ctx, index_hint);
 
-  if (lock) {
-    mutt_unblock_signals ();
-    mbox_unlock_mailbox (ctx);
-  }
-  return rc;
+    if (lock) {
+        mutt_unblock_signals ();
+        mbox_unlock_mailbox (ctx);
+    }
+    return rc;
 }
 
 /* return values:
@@ -546,7 +422,7 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
     return (-1);
 
   /* Create a temporary file to write the new version of the mailbox in. */
-  fp = m_tempfile(tempfile, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
+  fp = m_tempfile(tempfile, _POSIX_PATH_MAX, NONULL(mod_core.tmpdir), NULL);
   if (fp == NULL) {
     mutt_error _("Could not create temporary file!");
     mutt_sleep (5);
@@ -575,12 +451,6 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
   /* where to start overwriting */
   offset = ctx->hdrs[i]->offset;
 
-  /* the offset stored in the header does not include the MMDF_SEP, so make
-   * sure we seek to the correct location
-   */
-  if (ctx->magic == M_MMDF)
-    offset -= (sizeof MMDF_SEP - 1);
-
   /* allocate space for the new offsets */
   newOffset = p_new(struct m_update_t, ctx->msgcount - first);
   oldOffset = p_new(struct m_update_t, ctx->msgcount - first);
@@ -603,15 +473,6 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
         mutt_message (_("Writing messages... %d (%d%%)"), i,
                       (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
 
-      if (ctx->magic == M_MMDF) {
-        if (fputs (MMDF_SEP, fp) == EOF) {
-          mutt_perror (_("Can't create temporary file"));
-          mutt_sleep (5);
-          unlink (tempfile);
-          goto bail;
-        }
-      }
-
       /* save the new offset for this message.  we add `offset' because the
        * temporary file only contains saved message which are located after
        * `offset' in the real mailbox
@@ -637,22 +498,11 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
         ftello (fp) - ctx->hdrs[i]->content->length + offset;
       body_list_wipe(&ctx->hdrs[i]->content->parts);
 
-      switch (ctx->magic) {
-      case M_MMDF:
-        if (fputs (MMDF_SEP, fp) == EOF) {
-          mutt_perror (_("Can't create temporary file"));
-          mutt_sleep (5);
-          unlink (tempfile);
-          goto bail;
-        }
-        break;
-      default:
-        if (fputs ("\n", fp) == EOF) {
-          mutt_perror (_("Can't create temporary file"));
-          mutt_sleep (5);
-          unlink (tempfile);
-          goto bail;
-        }
+      if (fputs ("\n", fp) == EOF) {
+        mutt_perror (_("Can't create temporary file"));
+        mutt_sleep (5);
+        unlink (tempfile);
+        goto bail;
       }
     }
   }
@@ -680,11 +530,11 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
     return (-1);
   }
 
-  if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
+  if (fseeko (ctx->fp, offset, SEEK_SET) != 0 /* seek the append location */
       /* do a sanity check to make sure the mailbox looks ok */
-      fgets (buf, sizeof (buf), ctx->fp) == NULL ||
-      (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0) ||
-      (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buf) != 0)) {
+  ||  fgets (buf, sizeof (buf), ctx->fp) == NULL
+  ||  (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0))
+  {
     i = -1;
   }
   else {
@@ -718,7 +568,7 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
     char savefile[_POSIX_PATH_MAX];
 
     snprintf(savefile, sizeof (savefile), "%s/mutt.%s-%u",
-             NONULL(Tempdir), NONULL(Username), (unsigned int)getpid());
+             NONULL(mod_core.tmpdir), NONULL(mod_core.username), (unsigned int)getpid());
     rename (tempfile, savefile);
     mutt_unblock_signals ();
     mx_fastclose_mailbox (ctx);
@@ -838,9 +688,9 @@ static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
 
   /* simulate a close */
   if (ctx->id_hash)
-    hash_destroy (&ctx->id_hash, NULL);
+    hash_delete (&ctx->id_hash, NULL);
   if (ctx->subj_hash)
-    hash_destroy (&ctx->subj_hash, NULL);
+    hash_delete (&ctx->subj_hash, NULL);
   mutt_clear_threads (ctx);
   p_delete(&ctx->v2r);
   if (ctx->readonly) {
@@ -867,24 +717,11 @@ static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
   ctx->id_hash = NULL;
   ctx->subj_hash = NULL;
 
-  switch (ctx->magic) {
-  case M_MBOX:
-  case M_MMDF:
-    if (fseeko (ctx->fp, 0, SEEK_SET) != 0) {
-      rc = -1;
-    }
-    else {
-      cmp_headers = mutt_cmp_header;
-      if (ctx->magic == M_MBOX)
-        rc = mbox_parse_mailbox (ctx);
-      else
-        rc = mmdf_parse_mailbox (ctx);
-    }
-    break;
-
-  default:
+  if (ctx->magic == M_MBOX && !fseeko(ctx->fp, 0, SEEK_SET)) {
+    cmp_headers = mutt_cmp_header;
+    rc = mbox_parse_mailbox (ctx);
+  } else {
     rc = -1;
-    break;
   }
 
   if (rc == -1) {
@@ -983,7 +820,8 @@ int mbox_check_empty (const char *path)
   return ((st.st_size == 0));
 }
 
-int mbox_is_magic (const char* path, struct stat* st) {
+int mbox_is_magic (const char* path, struct stat* st)
+{
   int magic = -1;
   FILE* f;
   char tmp[_POSIX_PATH_MAX];
@@ -992,11 +830,7 @@ int mbox_is_magic (const char* path, struct stat* st) {
     return (-1);
 
   if (st->st_size == 0) {
-    /* hard to tell what zero-length files are, so assume the default magic */
-    if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
-      return (DefaultMagic);
-    else
-      return (M_MBOX);
+    return M_MBOX;
   }
   else if ((f = fopen (path, "r")) != NULL) {
     struct utimbuf times;
@@ -1004,8 +838,6 @@ int mbox_is_magic (const char* path, struct stat* st) {
     fgets (tmp, sizeof (tmp), f);
     if (m_strncmp("From ", tmp, 5) == 0)
       magic = M_MBOX;
-    else if (m_strcmp(MMDF_SEP, tmp) == 0)
-      magic = M_MMDF;
     m_fclose(&f);
 
     /* need to restore the times here, the file was not really accessed,
@@ -1025,23 +857,14 @@ int mbox_is_magic (const char* path, struct stat* st) {
   return (magic);
 }
 
-static int commit_message (MESSAGE* msg, CONTEXT* ctx __attribute__ ((unused)), int mbox) {
-  if ((mbox && fputc ('\n', msg->fp) == EOF) ||
-      (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
-    return (-1);
+static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
+  if (fputc ('\n', msg->fp) == EOF)
+    return -1;
   if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
     mutt_perror (_("Can't write message"));
-    return (-1);
+    return -1;
   }
-  return (0);
-}
-
-static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
-  return (commit_message (msg, ctx, 1));
-}
-
-static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
-  return (commit_message (msg, ctx, 0));
+  return 0;
 }
 
 mx_t const mbox_mx = {
@@ -1058,18 +881,3 @@ mx_t const mbox_mx = {
     mbox_sync_mailbox,
     mbox_commit_message,
 };
-
-mx_t const mmdf_mx = {
-    M_MMDF,
-    1,
-    mbox_is_magic,
-    mbox_check_empty,
-    access,
-    mbox_open_mailbox,
-    mbox_open_new_message,
-    NULL,
-    mbox_check_mailbox,
-    NULL,
-    mbox_sync_mailbox,
-    mmdf_commit_message,
-};