Rocco Rutte:
[apps/madmutt.git] / mx.c
diff --git a/mx.c b/mx.c
index 51fc1b3..7f92fb6 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -55,6 +55,7 @@
 #include "lib/intl.h"
 #include "lib/str.h"
 #include "lib/list.h"
+#include "lib/debug.h"
 
 #include <dirent.h>
 #include <fcntl.h>
@@ -65,9 +66,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
-#ifndef BUFFY_SIZE
 #include <utime.h>
-#endif
 
 static list2_t* MailboxFormats = NULL;
 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
@@ -202,7 +201,7 @@ int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
   while (fcntl (fd, F_SETLK, &lck) == -1) {
     struct stat sb;
 
-    dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
+    debug_print (1, ("fcntl errno %d.\n", errno));
     if (errno != EAGAIN && errno != EACCES) {
       mutt_perror ("fcntl");
       return (-1);
@@ -529,6 +528,8 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
     ctx->quiet = 1;
   if (flags & M_READONLY)
     ctx->readonly = 1;
+  if (flags & M_COUNT)
+    ctx->counting = 1;
 
   if (flags & (M_APPEND | M_NEWFOLDER)) {
     if (mx_open_mailbox_append (ctx, flags) != 0) {
@@ -602,18 +603,8 @@ void mx_fastclose_mailbox (CONTEXT * ctx)
   if (!ctx)
     return;
 
-#ifdef USE_IMAP
-  if (ctx->magic == M_IMAP)
-    imap_close_mailbox (ctx);
-#endif /* USE_IMAP */
-#ifdef USE_POP
-  if (ctx->magic == M_POP)
-    pop_close_mailbox (ctx);
-#endif /* USE_POP */
-#ifdef USE_NNTP
-  if (ctx->magic == M_NNTP)
-    nntp_fastclose_mailbox (ctx);
-#endif /* USE_NNTP */
+  if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
+    MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
   if (ctx->subj_hash)
     hash_destroy (&ctx->subj_hash, NULL);
   if (ctx->id_hash)
@@ -638,57 +629,14 @@ void mx_fastclose_mailbox (CONTEXT * ctx)
 /* save changes to disk */
 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
 {
-#ifdef BUFFY_SIZE
-  BUFFY *tmp = NULL;
-#endif
   int rc = -1;
 
   if (!ctx->quiet)
     mutt_message (_("Writing %s..."), ctx->path);
 
-  switch (ctx->magic) {
-  case M_MBOX:
-  case M_MMDF:
-    rc = mbox_sync_mailbox (ctx, index_hint);
-#ifdef BUFFY_SIZE
-    tmp = mutt_find_mailbox (ctx->path);
-#endif
-    break;
-
-  case M_MH:
-  case M_MAILDIR:
-    rc = mh_sync_mailbox (ctx, index_hint);
-    break;
-
-#ifdef USE_IMAP
-  case M_IMAP:
-    /* extra argument means EXPUNGE */
-    rc = imap_sync_mailbox (ctx, 1, index_hint);
-    break;
-#endif /* USE_IMAP */
-
-#ifdef USE_POP
-  case M_POP:
-    rc = pop_sync_mailbox (ctx, index_hint);
-    break;
-#endif /* USE_POP */
-
-#ifdef USE_NNTP
-  case M_NNTP:
-    rc = nntp_sync_mailbox (ctx);
-    break;
-#endif /* USE_NNTP */
-  }
-
-#if 0
-  if (!ctx->quiet && !ctx->shutup && rc == -1)
-    mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
-#endif
-
-#ifdef BUFFY_SIZE
-  if (tmp && tmp->new == 0)
-    mutt_update_mailbox (tmp);
-#endif
+  if (MX_IDX(ctx->magic-1))
+    /* 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)
@@ -1147,19 +1095,6 @@ int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
   return (rc);
 }
 
-#ifdef USE_IMAP
-int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
-{
-  char tmp[_POSIX_PATH_MAX];
-
-  mutt_mktemp (tmp);
-  if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
-    return (-1);
-  msg->path = safe_strdup (tmp);
-  return 0;
-}
-#endif
-
 /* args:
  *     dest    destintation mailbox
  *     hdr     message being copied (required for maildir support, because
@@ -1168,30 +1103,10 @@ int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
 {
   MESSAGE *msg;
-  int (*func) (MESSAGE *, CONTEXT *, HEADER *);
   ADDRESS *p = NULL;
 
-  switch (dest->magic) {
-  case M_MMDF:
-  case M_MBOX:
-    func = mbox_open_new_message;
-    break;
-  case M_MAILDIR:
-    func = maildir_open_new_message;
-    break;
-  case M_MH:
-    func = mh_open_new_message;
-    break;
-#ifdef USE_IMAP
-  case M_IMAP:
-    func = imap_open_new_message;
-    break;
-#endif
-  default:
-    dprint (1,
-            (debugfile,
-             "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
-             dest->magic));
+  if (!MX_IDX(dest->magic-1)) {
+    debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic));
     return (NULL);
   }
 
@@ -1209,7 +1124,7 @@ MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
   if (msg->received == 0)
     time (&msg->received);
 
-  if (func (msg, dest, hdr) == 0) {
+  if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
     if (dest->magic == M_MMDF)
       fputs (MMDF_SEP, msg->fp);
 
@@ -1291,7 +1206,7 @@ int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
     }
   }
 
-  dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
+  debug_print (1, ("null or invalid context.\n"));
   return (-1);
 }
 
@@ -1321,8 +1236,7 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
 
       if (msg->fp == NULL) {
         mutt_perror (path);
-        dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
-                    path, strerror (errno), errno));
+        debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
         FREE (&msg);
       }
     }
@@ -1356,10 +1270,7 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
 #endif /* USE_NNTP */
 
   default:
-    dprint (1,
-            (debugfile,
-             "mx_open_message(): function not implemented for mailbox type %d.\n",
-             ctx->magic));
+    debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
     FREE (&msg);
     break;
   }
@@ -1373,10 +1284,7 @@ int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
   int r = 0;
 
   if (!(msg->write && ctx->append)) {
-    dprint (1,
-            (debugfile,
-             "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
-             msg->write, ctx->append));
+    debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
     return -1;
   }
 
@@ -1449,8 +1357,7 @@ int mx_close_message (MESSAGE ** msg)
     (*msg)->fp = NULL;
 
   if ((*msg)->path) {
-    dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
-                (*msg)->path));
+    debug_print (1, ("unlinking %s\n", (*msg)->path));
     unlink ((*msg)->path);
     FREE (&(*msg)->path);
   }
@@ -1520,7 +1427,7 @@ void mx_update_context (CONTEXT * ctx, int new_messages)
       /* FREE (&h->env->supersedes); should I ? */
       if (h2) {
         h2->superseded = 1;
-        if (option (OPTSCORE))
+        if (!ctx->counting && option (OPTSCORE))
           mutt_score_message (ctx, h2, 1);
       }
     }
@@ -1528,11 +1435,13 @@ void mx_update_context (CONTEXT * ctx, int new_messages)
     /* add this message to the hash tables */
     if (ctx->id_hash && h->env->message_id)
       hash_insert (ctx->id_hash, h->env->message_id, h, 0);
-    if (ctx->subj_hash && h->env->real_subj)
-      hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
+    if (!ctx->counting) {
+      if (ctx->subj_hash && h->env->real_subj)
+        hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
 
-    if (option (OPTSCORE))
-      mutt_score_message (ctx, h, 0);
+      if (option (OPTSCORE))
+        mutt_score_message (ctx, h, 0);
+    }
 
     if (h->changed)
       ctx->changed = 1;
@@ -1599,6 +1508,7 @@ void mx_init (void) {
     if (MX_COMMAND(i,type) < 1)         EXITWITHERR("type");
     if (!MX_COMMAND(i,mx_is_magic))     EXITWITHERR("mx_is_magic");
     if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
+/*    if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/
   }
 #undef EXITWITHERR
 #endif /* DEBUG */