More modular way to open messages.
authorPierre Habouzit <madcoder@debian.org>
Wed, 28 Nov 2007 17:09:40 +0000 (18:09 +0100)
committerPierre Habouzit <madcoder@debian.org>
Wed, 28 Nov 2007 17:10:10 +0000 (18:10 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
imap/imap.c
lib-mx/compress.c
lib-mx/mbox.c
lib-mx/mh.c
lib-mx/mx.c
lib-mx/mx.h
lib-mx/pop.c
lib-mx/pop.h

index 38a633c..ea2eb80 100644 (file)
@@ -1595,6 +1595,7 @@ mx_t const imap_mx = {
     imap_access,
     imap_open_mailbox,
     imap_open_new_message,
     imap_access,
     imap_open_mailbox,
     imap_open_new_message,
+    imap_fetch_message,
     acl_check_imap,
     _imap_check_mailbox,
     imap_close_mailbox,
     acl_check_imap,
     _imap_check_mailbox,
     imap_close_mailbox,
index 65093c5..ec4db2b 100644 (file)
@@ -470,4 +470,5 @@ mx_t const compress_mx = {
     NULL,
     NULL,
     NULL,
     NULL,
     NULL,
     NULL,
+    NULL,
 };
 };
index 22f1172..9eb2809 100644 (file)
@@ -38,6 +38,12 @@ static int mbox_open_new_message(MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
     return 0;
 }
 
     return 0;
 }
 
+static int mbox_open_message(MESSAGE *msg, CONTEXT *ctx, int msgno)
+{
+    msg->fp = ctx->fp;
+    return 0;
+}
+
 /* prototypes */
 static int mbox_reopen_mailbox (CONTEXT*, int*);
 
 /* prototypes */
 static int mbox_reopen_mailbox (CONTEXT*, int*);
 
@@ -876,6 +882,7 @@ mx_t const mbox_mx = {
     access,
     mbox_open_mailbox,
     mbox_open_new_message,
     access,
     mbox_open_mailbox,
     mbox_open_new_message,
+    mbox_open_message,
     NULL,
     mbox_check_mailbox,
     NULL,
     NULL,
     mbox_check_mailbox,
     NULL,
index 23d21fe..5e889d6 100644 (file)
@@ -922,7 +922,24 @@ static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr
   return 0;
 }
 
   return 0;
 }
 
+static int maildir_open_message(MESSAGE *msg, CONTEXT *ctx, int msgno)
+{
+    HEADER *cur = ctx->hdrs[msgno];
+    char path[_POSIX_PATH_MAX];
+
+    snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
 
 
+    if ((msg->fp = fopen(path, "r")) == NULL
+    && errno == ENOENT && ctx->magic == M_MAILDIR) {
+        msg->fp = maildir_open_find_message (ctx->path, cur->path);
+    }
+
+    if (msg->fp == NULL) {
+        mutt_perror (path);
+        return -1;
+    }
+    return 0;
+}
 
 /*
  * Commit a message to a maildir folder.
 
 /*
  * Commit a message to a maildir folder.
@@ -1812,6 +1829,7 @@ mx_t const maildir_mx = {
     access,
     maildir_read_dir,
     maildir_open_new_message,
     access,
     maildir_read_dir,
     maildir_open_new_message,
+    maildir_open_message,
     NULL,
     maildir_check_mailbox,
     NULL,
     NULL,
     maildir_check_mailbox,
     NULL,
@@ -1827,6 +1845,7 @@ mx_t const mh_mx = {
     access,
     mh_read_dir,
     mh_open_new_message,
     access,
     mh_read_dir,
     mh_open_new_message,
+    maildir_open_message,
     NULL,
     mh_check_mailbox,
     NULL,
     NULL,
     mh_check_mailbox,
     NULL,
index 609b047..1d0e8d7 100644 (file)
@@ -35,7 +35,6 @@
 
 #define MAXLOCKATTEMPT 5
 
 
 #define MAXLOCKATTEMPT 5
 
-
 static mx_t const *mxfmts[] = {
     &mbox_mx,
     &mh_mx,
 static mx_t const *mxfmts[] = {
     &mbox_mx,
     &mh_mx,
@@ -817,6 +816,7 @@ int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
  */
 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
 {
  */
 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
 {
+
   MESSAGE *msg;
   address_t *p = NULL;
 
   MESSAGE *msg;
   address_t *p = NULL;
 
@@ -877,52 +877,16 @@ int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
 /* return a stream pointer for a message */
 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
 {
 /* return a stream pointer for a message */
 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
 {
-  MESSAGE *msg;
-
-  msg = p_new(MESSAGE, 1);
-  switch (msg->magic = ctx->magic) {
-  case M_MBOX:
-    msg->fp = ctx->fp;
-    break;
-
-  case M_MH:
-  case M_MAILDIR:
-    {
-      HEADER *cur = ctx->hdrs[msgno];
-      char path[_POSIX_PATH_MAX];
-
-      snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
-
-      if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
-          ctx->magic == M_MAILDIR)
-        msg->fp = maildir_open_find_message (ctx->path, cur->path);
-
-      if (msg->fp == NULL) {
-        mutt_perror (path);
-        p_delete(&msg);
-      }
-    }
-    break;
+    MESSAGE *msg;
 
 
-  case M_IMAP:
-    {
-      if (imap_fetch_message (msg, ctx, msgno) != 0)
-        p_delete(&msg);
-      break;
-    }
+    if (!MX_IDX(ctx->magic) || !mxfmts[ctx->magic - 1]->mx_open_message)
+        return NULL;
 
 
-  case M_POP:
-    {
-      if (pop_fetch_message (msg, ctx, msgno) != 0)
+    msg = p_new(MESSAGE, 1);
+    msg->magic = ctx->magic;
+    if (mxfmts[ctx->magic - 1]->mx_open_message(msg, ctx, msgno))
         p_delete(&msg);
         p_delete(&msg);
-      break;
-    }
-
-  default:
-    p_delete(&msg);
-    break;
-  }
-  return msg;
+    return msg;
 }
 
 /* commit a message to a folder */
 }
 
 /* commit a message to a folder */
index 9ccae83..deccf85 100644 (file)
@@ -81,6 +81,7 @@ typedef struct mx_t {
   int (*mx_open_mailbox) (CONTEXT*);
   /* open new message */
   int (*mx_open_new_message) (MESSAGE*, CONTEXT*, HEADER*);
   int (*mx_open_mailbox) (CONTEXT*);
   /* open new message */
   int (*mx_open_new_message) (MESSAGE*, CONTEXT*, HEADER*);
+  int (*mx_open_message)(MESSAGE*, CONTEXT*, int);
   /* check ACL flags; if not implemented, always assume granted
    * permissions */
   int (*mx_acl_check) (CONTEXT*, int);
   /* check ACL flags; if not implemented, always assume granted
    * permissions */
   int (*mx_acl_check) (CONTEXT*, int);
index 2258d0f..8f522c7 100644 (file)
@@ -1179,6 +1179,8 @@ static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
   }
 }
 
   }
 }
 
+static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno);
+
 /* }}} */
 
 mx_t const pop_mx = {
 /* }}} */
 
 mx_t const pop_mx = {
@@ -1189,6 +1191,7 @@ mx_t const pop_mx = {
     NULL,
     pop_open_mailbox,
     NULL,
     NULL,
     pop_open_mailbox,
     NULL,
+    pop_fetch_message,
     pop_acl_check,
     pop_check_mailbox,
     pop_close_mailbox,
     pop_acl_check,
     pop_check_mailbox,
     pop_close_mailbox,
@@ -1347,7 +1350,7 @@ fail:
 }
 
 /* fetch message from POP server */
 }
 
 /* fetch message from POP server */
-int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
+static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
 {
   int ret;
   void *uidl;
 {
   int ret;
   void *uidl;
index 22806cb..c0f6cd2 100644 (file)
@@ -14,8 +14,6 @@
 #include <lib-mx/mx.h>
 
 extern mx_t const pop_mx;
 #include <lib-mx/mx.h>
 
 extern mx_t const pop_mx;
-
-int pop_fetch_message(MESSAGE *, CONTEXT *, int);
 void pop_fetch_mail(void);
 
 #endif
 void pop_fetch_mail(void);
 
 #endif