copyright statements
[apps/madmutt.git] / mx.c
diff --git a/mx.c b/mx.c
index b596bca..8e645c1 100644 (file)
--- a/mx.c
+++ b/mx.c
 # include "config.h"
 #endif
 
+#include <dirent.h>
+#include <fcntl.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <utime.h>
+
 #include <lib-lib/mem.h>
 #include <lib-lib/ascii.h>
 #include <lib-lib/str.h>
 #include <lib-lib/macros.h>
 #include <lib-lib/file.h>
+#include <lib-lib/debug.h>
+#include <lib-lib/url.h>
+
+#include <lib-sys/unix.h>
 
 #include <lib-mime/mime.h>
 
 #include "thread.h"
 #include "copy.h"
 #include "keymap.h"
-#include "url.h"
 #include "sidebar.h"
-
-#ifdef USE_COMPRESSED
 #include "compress.h"
-#endif
 
-#ifdef USE_IMAP
-#include "imap/imap.h"
-#include "imap/mx_imap.h"
-#endif
+#include <imap/imap.h>
+#include <imap/mx_imap.h>
 
-#ifdef USE_POP
-#include "pop/pop.h"
-#include "pop/mx_pop.h"
-#endif
+#include <pop/pop.h>
+#include <pop/mx_pop.h>
 
 #ifdef USE_NNTP
 #include "nntp/nntp.h"
 #include "dotlock.h"
 #endif
 
-#include "mutt_crypt.h"
+#include <lib-crypt/crypt.h>
 
 #include "lib/list.h"
-#include "lib/debug.h"
-
-#include <dirent.h>
-#include <fcntl.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <utime.h>
 
 static list2_t* MailboxFormats = NULL;
 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
@@ -385,22 +380,16 @@ static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
 {
   struct stat sb;
 
-#ifdef USE_COMPRESSED
   /* special case for appending to compressed folders -
    * even if we can not open them for reading */
   if (mutt_can_append_compressed (ctx->path))
     mutt_open_append_compressed (ctx);
-#endif
 
   ctx->append = 1;
 
-#ifdef USE_IMAP
-
   if (mx_get_magic (ctx->path) == M_IMAP)
     return imap_open_mailbox_append (ctx);
 
-#endif
-
   if (stat (ctx->path, &sb) == 0) {
     ctx->magic = mx_get_magic (ctx->path);
 
@@ -540,10 +529,8 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
   if (!MX_IDX(ctx->magic-1))
     ctx->magic = mx_get_magic (path);
 
-#ifdef USE_COMPRESSED
   if (ctx->magic == M_COMPRESSED)
     mutt_open_read_compressed (ctx);
-#endif
 
   if (ctx->magic == 0)
     mutt_error (_("%s is not a mailbox."), path);
@@ -607,13 +594,13 @@ void mx_fastclose_mailbox (CONTEXT * ctx)
     hash_destroy (&ctx->id_hash, NULL);
   mutt_clear_threads (ctx);
   for (i = 0; i < ctx->msgcount; i++)
-    mutt_free_header (&ctx->hdrs[i]);
+    header_delete(&ctx->hdrs[i]);
   p_delete(&ctx->hdrs);
   p_delete(&ctx->v2r);
-#ifdef USE_COMPRESSED
+
   if (ctx->compressinfo)
     mutt_fast_close_compressed (ctx);
-#endif
+
   p_delete(&ctx->path);
   p_delete(&ctx->pattern);
   if (ctx->limit_pattern)
@@ -634,10 +621,8 @@ static int sync_mailbox (CONTEXT * ctx, int *index_hint)
     /* the 1 is only of interest for IMAP and means EXPUNGE */
     rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
 
-#ifdef USE_COMPRESSED
   if (rc == 0 && ctx->compressinfo)
     return mutt_sync_compressed (ctx);
-#endif
 
   return rc;
 }
@@ -769,10 +754,8 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
     }
   }
 
-#ifdef USE_IMAP
   /* IMAP servers manage the OLD flag themselves */
   if (ctx->magic != M_IMAP)
-#endif
     if (option (OPTMARKOLD)) {
       for (i = 0; i < ctx->msgcount; i++) {
         if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
@@ -783,7 +766,6 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
   if (move_messages) {
     mutt_message (_("Moving read messages to %s..."), mbox);
 
-#ifdef USE_IMAP
     /* try to use server-side copy first */
     i = 1;
 
@@ -806,7 +788,6 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
       return -1;
     }
     else                        /* use regular append-copy mode */
-#endif
     {
       if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
         ctx->closing = 0;
@@ -847,7 +828,6 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
       return -1;
     }
 
-#ifdef USE_IMAP
   /* allow IMAP to preserve the deleted flag across sessions */
   if (ctx->magic == M_IMAP) {
     if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
@@ -856,7 +836,6 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
     }
   }
   else
-#endif
   {
     if (!purge) {
       for (i = 0; i < ctx->msgcount; i++)
@@ -884,10 +863,8 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
       !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
     mx_unlink_empty (ctx->path);
 
-#ifdef USE_COMPRESSED
   if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
     return (-1);
-#endif
 
   mx_fastclose_mailbox (ctx);
 
@@ -972,7 +949,7 @@ void mx_update_tables (CONTEXT * ctx, int committing)
       if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
         hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
                      ctx->hdrs[i], NULL);
-      mutt_free_header (&ctx->hdrs[i]);
+      header_delete(&ctx->hdrs[i]);
     }
   }
 #undef this_body
@@ -1030,10 +1007,8 @@ static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
     else if (purge == M_NO) {
       if (!ctx->changed)
         return 0;               /* nothing to do! */
-#ifdef USE_IMAP
       /* let IMAP servers hold on to D flags */
       if (ctx->magic != M_IMAP)
-#endif
       {
         for (i = 0; i < ctx->msgcount; i++)
           ctx->hdrs[i]->deleted = 0;
@@ -1054,19 +1029,15 @@ static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
       return -1;
   }
 
-#ifdef USE_IMAP
   if (ctx->magic == M_IMAP)
     rc = imap_sync_mailbox (ctx, purge, index_hint);
   else
-#endif
     rc = sync_mailbox (ctx, index_hint);
   if (rc == 0) {
-#ifdef USE_IMAP
     if (ctx->magic == M_IMAP && !purge)
       mutt_message (_("Mailbox checkpointed."));
 
     else
-#endif
       mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
 
     mutt_sleep (0);
@@ -1087,10 +1058,8 @@ static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
      * at least with the new threading code.
      */
     if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
-#ifdef USE_IMAP
       /* IMAP does this automatically after handling EXPUNGE */
       if (ctx->magic != M_IMAP)
-#endif
       {
         mx_update_tables (ctx, 1);
         mutt_sort_headers (ctx, 1);     /* rethread from scratch */
@@ -1162,10 +1131,8 @@ MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
 
 /* check for new mail */
 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
-#ifdef USE_COMPRESSED
   if (ctx->compressinfo)
     return mutt_check_mailbox_compressed (ctx);
-#endif
 
   if (ctx) {
     if (ctx->locked)
@@ -1211,23 +1178,19 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
     }
     break;
 
-#ifdef USE_IMAP
   case M_IMAP:
     {
       if (imap_fetch_message (msg, ctx, msgno) != 0)
         p_delete(&msg);
       break;
     }
-#endif /* USE_IMAP */
 
-#ifdef USE_POP
   case M_POP:
     {
       if (pop_fetch_message (msg, ctx, msgno) != 0)
         p_delete(&msg);
       break;
     }
-#endif /* USE_POP */
 
 #ifdef USE_NNTP
   case M_NNTP:
@@ -1264,12 +1227,8 @@ int mx_close_message (MESSAGE ** msg)
   int r = 0;
 
   if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
-#ifdef USE_IMAP
       || (*msg)->magic == M_IMAP
-#endif
-#ifdef USE_POP
       || (*msg)->magic == M_POP
-#endif
 #ifdef USE_NNTP
       || (*msg)->magic == M_NNTP
 #endif
@@ -1326,10 +1285,8 @@ void mx_update_context (CONTEXT * ctx, int new_messages)
   for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
     h = ctx->hdrs[msgno];
 
-    if (WithCrypto) {
-      /* NOTE: this _must_ be done before the check for mailcap! */
-      h->security = crypt_query (h->content);
-    }
+    /* NOTE: this _must_ be done before the check for mailcap! */
+    h->security = crypt_query (h->content);
 
     if (!ctx->pattern) {
       ctx->v2r[ctx->vcount] = msgno;
@@ -1414,18 +1371,12 @@ void mx_init (void) {
   list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
   list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
   list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
-#ifdef USE_IMAP
   list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
-#endif
-#ifdef USE_POP
   list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
-#endif
 #ifdef USE_NNTP
   list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
 #endif
-#ifdef USE_COMPRESSED
   list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
-#endif
 #ifdef DEBUG
   /* check module registration for completeness with debug versions */
 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
@@ -1456,11 +1407,7 @@ int mx_rebuild_cache (void) {
   for (i = 0; i < Incoming->length; i++) {
     b = (BUFFY*) Incoming->data[i];
     magic = mx_get_magic (b->path);
-    if (magic != M_MAILDIR && magic != M_MH
-#ifdef USE_IMAP
-        && magic != M_IMAP
-#endif
-    )
+    if (magic != M_MAILDIR && magic != M_MH && magic != M_IMAP)
       continue;
     sidebar_set_current (b->path);
     sidebar_draw (CurrentMenu);
@@ -1478,3 +1425,27 @@ int mx_rebuild_cache (void) {
   return (0);
 #endif
 }
+
+void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
+{
+  MESSAGE *msg;
+  int flags = 0;
+
+  do {
+    if (cur->content->type != TYPEMESSAGE
+        && cur->content->type != TYPEMULTIPART)
+      break;                     /* nothing to do */
+
+    if (cur->content->parts)
+      break;                     /* The message was parsed earlier. */
+
+    if ((msg = mx_open_message (ctx, cur->msgno))) {
+      mutt_parse_part (msg->fp, cur->content);
+
+      cur->security = crypt_query (cur->content);
+
+      mx_close_message (&msg);
+    }
+  } while (0);
+  mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
+}