# HG changeset patch
# User Damien Riegel <dam...@riegel.io>
# Date 1463093733 14400
#      Thu May 12 18:55:33 2016 -0400
# Node ID 12fc6e961dc15b1cfea86397b40cdb565c51c581
# Parent  ae203b2c56a623a3e5c2500f0e845acac554c44b
add open_new_msg operation to struct mx_ops

The code was already using a function pointer to do this operation. This
commit moves this function pointer to the mx_ops structure and the
open_new_message functions to their respective source files if it needs
to.

A few lines in mx_open_new_message are also reindented using spaces
instead of tabs.

diff -r ae203b2c56a6 -r 12fc6e961dc1 imap/imap.c
--- a/imap/imap.c       Thu May 12 12:41:25 2016 -0700
+++ b/imap/imap.c       Thu May 12 18:55:33 2016 -0400
@@ -828,6 +828,20 @@
   imap_free_idata (idata);
 }
 
+static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
+{
+  char tmp[_POSIX_PATH_MAX];
+
+  mutt_mktemp (tmp, sizeof (tmp));
+  if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
+  {
+    mutt_perror (tmp);
+    return (-1);
+  }
+  msg->path = safe_strdup(tmp);
+  return 0;
+}
+
 /* imap_set_flag: append str to flags if we currently have permission
  *   according to aclbit */
 static void imap_set_flag (IMAP_DATA* idata, int aclbit, int flag,
@@ -2041,4 +2055,5 @@
 struct mx_ops mx_imap_ops = {
   .open = imap_open_mailbox,
   .close = imap_close_mailbox,
+  .open_new_msg = imap_open_new_message,
 };
diff -r ae203b2c56a6 -r 12fc6e961dc1 mailbox.h
--- a/mailbox.h Thu May 12 12:41:25 2016 -0700
+++ b/mailbox.h Thu May 12 18:55:33 2016 -0400
@@ -41,7 +41,7 @@
   MUTT_FLAGS               /* nondestructive flags change (IMAP) */
 };
 
-typedef struct
+typedef struct _message
 {
   FILE *fp;    /* pointer to the message data */
   char *path;  /* path to temp file */
diff -r ae203b2c56a6 -r 12fc6e961dc1 mbox.c
--- a/mbox.c    Thu May 12 12:41:25 2016 -0700
+++ b/mbox.c    Thu May 12 18:55:33 2016 -0400
@@ -445,6 +445,12 @@
   return 0;
 }
 
+static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
+{
+  msg->fp = dest->fp;
+  return 0;
+}
+
 /* return 1 if address lists are strictly identical */
 static int strict_addrcmp (const ADDRESS *a, const ADDRESS *b)
 {
@@ -1266,9 +1272,11 @@
 struct mx_ops mx_mbox_ops = {
   .open = mbox_open_mailbox,
   .close = mbox_close_mailbox,
+  .open_new_msg = mbox_open_new_message,
 };
 
 struct mx_ops mx_mmdf_ops = {
   .open = mbox_open_mailbox,
   .close = mbox_close_mailbox,
+  .open_new_msg = mbox_open_new_message,
 };
diff -r ae203b2c56a6 -r 12fc6e961dc1 mh.c
--- a/mh.c      Thu May 12 12:41:25 2016 -0700
+++ b/mh.c      Thu May 12 18:55:33 2016 -0400
@@ -1248,7 +1248,7 @@
  * Open a new (temporary) message in an MH folder.
  */
 
-int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
+static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
 {
   return mh_mkstemp (dest, &msg->fp, &msg->path);
 }
@@ -1294,7 +1294,7 @@
  *
  */
 
-int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
+static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * 
hdr)
 {
   int fd;
   char path[_POSIX_PATH_MAX];
@@ -2361,9 +2361,11 @@
 struct mx_ops mx_maildir_ops = {
   .open = maildir_open_mailbox,
   .close = mh_close_mailbox,
+  .open_new_msg = maildir_open_new_message,
 };
 
 struct mx_ops mx_mh_ops = {
   .open = mh_open_mailbox,
   .close = mh_close_mailbox,
+  .open_new_msg = mh_open_new_message,
 };
diff -r ae203b2c56a6 -r 12fc6e961dc1 mutt.h
--- a/mutt.h    Thu May 12 12:41:25 2016 -0700
+++ b/mutt.h    Thu May 12 18:55:33 2016 -0400
@@ -869,11 +869,22 @@
 };
 
 struct _context;
+struct _message;
 
+/*
+ * struct mx_ops - a structure to store operations on a mailbox
+ * The following operations are mandatory:
+ *  - open
+ *  - close
+ *
+ * Optional operations
+ *  - open_new_msg
+ */
 struct mx_ops
 {
   int (*open)(struct _context *);
   int (*close)(struct _context *);
+  int (*open_new_msg) (struct _message *, struct _context *, HEADER *);
 };
 
 typedef struct _context
diff -r ae203b2c56a6 -r 12fc6e961dc1 mx.c
--- a/mx.c      Thu May 12 12:41:25 2016 -0700
+++ b/mx.c      Thu May 12 18:55:33 2016 -0400
@@ -1197,31 +1197,6 @@
   return (rc);
 }
 
-
-/* {maildir,mh}_open_new_message are in mh.c. */
-
-static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
-{
-  msg->fp = dest->fp;
-  return 0;
-}
-
-#ifdef USE_IMAP
-static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
-{
-  char tmp[_POSIX_PATH_MAX];
-
-  mutt_mktemp (tmp, sizeof (tmp));
-  if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
-  {
-    mutt_perror (tmp);
-    return (-1);
-  }
-  msg->path = safe_strdup(tmp);
-  return 0;
-}
-#endif
-
 /* args:
  *     dest    destination mailbox
  *     hdr     message being copied (required for maildir support, because
@@ -1229,31 +1204,15 @@
  */
 MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
 {
+  struct mx_ops *ops = mx_get_ops (dest->magic);
+  ADDRESS *p = NULL;
   MESSAGE *msg;
-  int (*func) (MESSAGE *, CONTEXT *, HEADER *);
-  ADDRESS *p = NULL;
 
-  switch (dest->magic)
+  if (!ops || !ops->open_new_msg)
   {
-    case MUTT_MMDF:
-    case MUTT_MBOX:
-      func = mbox_open_new_message;
-      break;
-    case MUTT_MAILDIR:
-      func = maildir_open_new_message;
-      break;
-    case MUTT_MH:
-      func = mh_open_new_message;
-      break;
-#ifdef USE_IMAP
-    case MUTT_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));
-      return (NULL);
+              dest->magic));
+      return NULL;
   }
 
   msg = safe_calloc (1, sizeof (MESSAGE));
@@ -1271,23 +1230,23 @@
 
   if(msg->received == 0)
     time(&msg->received);
-  
-  if (func (msg, dest, hdr) == 0)
+
+  if (ops->open_new_msg (msg, dest, hdr) == 0)
   {
     if (dest->magic == MUTT_MMDF)
       fputs (MMDF_SEP, msg->fp);
 
     if ((msg->magic == MUTT_MBOX || msg->magic ==  MUTT_MMDF) &&
-       flags & MUTT_ADD_FROM)
+         flags & MUTT_ADD_FROM)
     {
       if (hdr)
       {
-       if (hdr->env->return_path)
-         p = hdr->env->return_path;
-       else if (hdr->env->sender)
-         p = hdr->env->sender;
-       else
-         p = hdr->env->from;
+        if (hdr->env->return_path)
+          p = hdr->env->return_path;
+        else if (hdr->env->sender)
+          p = hdr->env->sender;
+        else
+          p = hdr->env->from;
       }
 
       fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime 
(&msg->received));
diff -r ae203b2c56a6 -r 12fc6e961dc1 mx.h
--- a/mx.h      Thu May 12 12:41:25 2016 -0700
+++ b/mx.h      Thu May 12 18:55:33 2016 -0400
@@ -62,9 +62,6 @@
 int maildir_commit_message (CONTEXT *, MESSAGE *, HEADER *);
 int mh_commit_message (CONTEXT *, MESSAGE *, HEADER *);
 
-int maildir_open_new_message (MESSAGE *, CONTEXT *, HEADER *);
-int mh_open_new_message (MESSAGE *, CONTEXT *, HEADER *);
-
 FILE *maildir_open_find_message (const char *, const char *);
 
 int mbox_strict_cmp_headers (const HEADER *, const HEADER *);

Reply via email to