changeset: 6684:70eb7e0dbb58
user:      Damien Riegel <damien.rie...@gmail.com>
date:      Fri Jun 17 19:01:31 2016 -0700
link:      http://dev.mutt.org/hg/mutt/rev/70eb7e0dbb58

Add open_msg to struct mx_ops

Add the callback to open an existing message to struct mx_ops. For mbox,
mmdf, maildir, and mh, the code was implemented directly into
mx_open_message, so it is moved in their respective source files. For
imap and pop, there were already <mailbox>_fetch_message functions, but
their argument order has been changed to pass the context as a first
argument.

diffs (258 lines):

diff -r d83239fd794a -r 70eb7e0dbb58 imap/imap.c
--- a/imap/imap.c       Fri Jun 17 10:33:56 2016 -0700
+++ b/imap/imap.c       Fri Jun 17 19:01:31 2016 -0700
@@ -2074,6 +2074,7 @@
 struct mx_ops mx_imap_ops = {
   .open = imap_open_mailbox,
   .close = imap_close_mailbox,
+  .open_msg = imap_fetch_message,
   .open_new_msg = imap_open_new_message,
   .check = imap_check_mailbox_reopen,
 };
diff -r d83239fd794a -r 70eb7e0dbb58 imap/imap.h
--- a/imap/imap.h       Fri Jun 17 10:33:56 2016 -0700
+++ b/imap/imap.h       Fri Jun 17 19:01:31 2016 -0700
@@ -58,7 +58,6 @@
 /* message.c */
 int imap_append_message (CONTEXT* ctx, MESSAGE* msg);
 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete);
-int imap_fetch_message (MESSAGE* msg, CONTEXT* ctx, int msgno);
 
 /* socket.c */
 void imap_logout_all (void);
diff -r d83239fd794a -r 70eb7e0dbb58 imap/imap_private.h
--- a/imap/imap_private.h       Fri Jun 17 10:33:56 2016 -0700
+++ b/imap/imap_private.h       Fri Jun 17 19:01:31 2016 -0700
@@ -268,6 +268,8 @@
 int imap_cache_del (IMAP_DATA* idata, HEADER* h);
 int imap_cache_clean (IMAP_DATA* idata);
 
+int imap_fetch_message (CONTEXT *ctx, MESSAGE *msg, int msgno);
+
 /* util.c */
 #ifdef USE_HCACHE
 header_cache_t* imap_hcache_open (IMAP_DATA* idata, const char* path);
diff -r d83239fd794a -r 70eb7e0dbb58 imap/message.c
--- a/imap/message.c    Fri Jun 17 10:33:56 2016 -0700
+++ b/imap/message.c    Fri Jun 17 19:01:31 2016 -0700
@@ -391,7 +391,7 @@
   return retval;
 }
 
-int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
+int imap_fetch_message (CONTEXT *ctx, MESSAGE *msg, int msgno)
 {
   IMAP_DATA* idata;
   HEADER* h;
diff -r d83239fd794a -r 70eb7e0dbb58 mbox.c
--- a/mbox.c    Fri Jun 17 10:33:56 2016 -0700
+++ b/mbox.c    Fri Jun 17 19:01:31 2016 -0700
@@ -447,6 +447,13 @@
   return 0;
 }
 
+static int mbox_open_message (CONTEXT *ctx,  MESSAGE *msg, int msgno)
+{
+  msg->fp = ctx->fp;
+
+  return 0;
+}
+
 static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
 {
   msg->fp = dest->fp;
@@ -1274,6 +1281,7 @@
 struct mx_ops mx_mbox_ops = {
   .open = mbox_open_mailbox,
   .close = mbox_close_mailbox,
+  .open_msg = mbox_open_message,
   .open_new_msg = mbox_open_new_message,
   .check = mbox_check_mailbox,
 };
@@ -1281,6 +1289,7 @@
 struct mx_ops mx_mmdf_ops = {
   .open = mbox_open_mailbox,
   .close = mbox_close_mailbox,
+  .open_msg = mbox_open_message,
   .open_new_msg = mbox_open_new_message,
   .check = mbox_check_mailbox,
 };
diff -r d83239fd794a -r 70eb7e0dbb58 mh.c
--- a/mh.c      Fri Jun 17 10:33:56 2016 -0700
+++ b/mh.c      Fri Jun 17 19:01:31 2016 -0700
@@ -1336,6 +1336,38 @@
   }
 }
 
+static int maildir_mh_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno,
+                                    int is_maildir)
+{
+  HEADER *cur = ctx->hdrs[msgno];
+  char path[_POSIX_PATH_MAX];
+
+  snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
+
+  msg->fp = fopen (path, "r");
+  if (msg->fp == NULL && errno == ENOENT && is_maildir)
+    msg->fp = maildir_open_find_message (ctx->path, cur->path);
+
+  if (!msg->fp)
+  {
+    mutt_perror (path);
+    dprint (1, (debugfile, "maildir_mh_open_message: fopen: %s: %s (errno 
%d).\n",
+            path, strerror (errno), errno));
+    return -1;
+  }
+
+  return 0;
+}
+
+static int maildir_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno)
+{
+  return maildir_mh_open_message (ctx, msg, msgno, 1);
+}
+
+static int mh_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno)
+{
+  return maildir_mh_open_message (ctx, msg, msgno, 0);
+}
 
 /*
  * Open a new (temporary) message in a maildir folder.
@@ -2412,6 +2444,7 @@
 struct mx_ops mx_maildir_ops = {
   .open = maildir_open_mailbox,
   .close = mh_close_mailbox,
+  .open_msg = maildir_open_message,
   .open_new_msg = maildir_open_new_message,
   .check = maildir_check_mailbox,
 };
@@ -2419,6 +2452,7 @@
 struct mx_ops mx_mh_ops = {
   .open = mh_open_mailbox,
   .close = mh_close_mailbox,
+  .open_msg = mh_open_message,
   .open_new_msg = mh_open_new_message,
   .check = mh_check_mailbox,
 };
diff -r d83239fd794a -r 70eb7e0dbb58 mutt.h
--- a/mutt.h    Fri Jun 17 10:33:56 2016 -0700
+++ b/mutt.h    Fri Jun 17 19:01:31 2016 -0700
@@ -894,6 +894,7 @@
   int (*open)(struct _context *);
   int (*close)(struct _context *);
   int (*check) (struct _context *ctx, int *index_hint);
+  int (*open_msg) (struct _context *, struct _message *, int msgno);
   int (*open_new_msg) (struct _message *, struct _context *, HEADER *);
 };
 
diff -r d83239fd794a -r 70eb7e0dbb58 mx.c
--- a/mx.c      Fri Jun 17 10:33:56 2016 -0700
+++ b/mx.c      Fri Jun 17 19:01:31 2016 -0700
@@ -1308,62 +1308,23 @@
 /* return a stream pointer for a message */
 MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
 {
+  struct mx_ops *ops = mx_get_ops (ctx->magic);
   MESSAGE *msg;
-  
+  int ret;
+
+  if (!ops || !ops->open_msg)
+  {
+    dprint (1, (debugfile, "mx_open_message(): function not implemented for 
mailbox type %d.\n", ctx->magic));
+    return NULL;
+  }
+
   msg = safe_calloc (1, sizeof (MESSAGE));
-  switch (msg->magic = ctx->magic)
-  {
-    case MUTT_MBOX:
-    case MUTT_MMDF:
-      msg->fp = ctx->fp;
-      break;
+  msg->magic = ctx->magic;
+  ret = ops->open_msg (ctx, msg, msgno);
+  if (ret)
+    FREE (&msg);
 
-    case MUTT_MH:
-    case MUTT_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 == MUTT_MAILDIR)
-       msg->fp = maildir_open_find_message (ctx->path, cur->path);
-      
-      if (msg->fp == NULL)
-      {
-       mutt_perror (path);
-       dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
-                   path, strerror (errno), errno));
-       FREE (&msg);
-      }
-    }
-    break;
-    
-#ifdef USE_IMAP
-    case MUTT_IMAP:
-    {
-      if (imap_fetch_message (msg, ctx, msgno) != 0)
-       FREE (&msg);
-      break;
-    }
-#endif /* USE_IMAP */
-
-#ifdef USE_POP
-    case MUTT_POP:
-    {
-      if (pop_fetch_message (msg, ctx, msgno) != 0)
-       FREE (&msg);
-      break;
-    }
-#endif /* USE_POP */
-
-    default:
-      dprint (1, (debugfile, "mx_open_message(): function not implemented for 
mailbox type %d.\n", ctx->magic));
-      FREE (&msg);
-      break;
-  }
-  return (msg);
+  return msg;
 }
 
 /* commit a message to a folder */
diff -r d83239fd794a -r 70eb7e0dbb58 pop.c
--- a/pop.c     Fri Jun 17 10:33:56 2016 -0700
+++ b/pop.c     Fri Jun 17 19:01:31 2016 -0700
@@ -512,7 +512,7 @@
 }
 
 /* fetch message from POP server */
-int pop_fetch_message (MESSAGE* msg, CONTEXT* ctx, int msgno)
+static int pop_fetch_message (CONTEXT* ctx, MESSAGE* msg, int msgno)
 {
   int ret;
   void *uidl;
@@ -931,5 +931,6 @@
 struct mx_ops mx_pop_ops = {
   .open = pop_open_mailbox,
   .close = pop_close_mailbox,
+  .open_msg = pop_fetch_message,
   .check = pop_check_mailbox,
 };
diff -r d83239fd794a -r 70eb7e0dbb58 pop.h
--- a/pop.h     Fri Jun 17 10:33:56 2016 -0700
+++ b/pop.h     Fri Jun 17 19:01:31 2016 -0700
@@ -106,7 +106,6 @@
 
 /* pop.c */
 int pop_sync_mailbox (CONTEXT *, int *);
-int pop_fetch_message (MESSAGE *, CONTEXT *, int);
 int pop_close_mailbox (CONTEXT *);
 void pop_fetch_mail (void);
 

Reply via email to