]> granicus.if.org Git - neomutt/commitdiff
add open_new_msg operation to struct mx_ops
authorDamien Riegel <damien@riegel.io>
Wed, 25 May 2016 22:53:07 +0000 (15:53 -0700)
committerDamien Riegel <damien@riegel.io>
Wed, 25 May 2016 22:53:07 +0000 (15:53 -0700)
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.

imap/imap.c
mailbox.h
mbox.c
mh.c
mutt.h
mx.c
mx.h

index e9e84191d27f8861ad1733a85cb71a8e6df73789..c2db3c222ecde62ddc3835c16a0acd7144ef93ef 100644 (file)
@@ -828,6 +828,20 @@ void imap_logout (IMAP_DATA** idata)
   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 @@ int imap_complete(char* dest, size_t dlen, char* path) {
 struct mx_ops mx_imap_ops = {
   .open = imap_open_mailbox,
   .close = imap_close_mailbox,
+  .open_new_msg = imap_open_new_message,
 };
index 1b1a7880d870f7e43dd3e241d12e43f69a7edb6e..f61e5865f6d609424b4b80b07a5c58e0b1b8825c 100644 (file)
--- a/mailbox.h
+++ b/mailbox.h
@@ -41,7 +41,7 @@ enum
   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 --git a/mbox.c b/mbox.c
index b7dda14492ad2b807075a569ed7fb5cb219f4631..823c8e9c2a355ecaea2a73f9ff7bae3f46ab0f91 100644 (file)
--- a/mbox.c
+++ b/mbox.c
@@ -445,6 +445,12 @@ static int mbox_close_mailbox (CONTEXT *ctx)
   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 @@ int mbox_check_empty (const char *path)
 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 --git a/mh.c b/mh.c
index c156a10c2778c5540b23a23f9c4b8146bede8a46..1bca8ddd4dd1eede0dfcf54f6eca455b9f1d3399 100644 (file)
--- a/mh.c
+++ b/mh.c
@@ -1248,7 +1248,7 @@ static int mh_open_mailbox (CONTEXT *ctx)
  * 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 @@ static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
  *
  */
 
-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 @@ int mx_is_mh (const char *path)
 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 --git a/mutt.h b/mutt.h
index 582904192d86a318ff7f8348d3f65d928ddd3f56..00529624d150ecdf744e06cc76c599d0e96ccf19 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -869,11 +869,22 @@ enum
 };
 
 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 --git a/mx.c b/mx.c
index 256248c13e8dbadfd3942af84b12ebc5a52cd2d8..5ac8804fb69ce6d2b708a96b6d53d59d60918169 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -1197,31 +1197,6 @@ int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
   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 @@ static 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 *);
+  struct mx_ops *ops = mx_get_ops (dest->magic);
   ADDRESS *p = NULL;
+  MESSAGE *msg;
 
-  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,8 +1230,8 @@ 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 (ops->open_new_msg (msg, dest, hdr) == 0)
   {
     if (dest->magic == MUTT_MMDF)
       fputs (MMDF_SEP, msg->fp);
diff --git a/mx.h b/mx.h
index dfc549e0445daee123fc691a402075aa1b45ea5a..bb6a3a3548e5eea380bb7289439acfecbbab1ae9 100644 (file)
--- a/mx.h
+++ b/mx.h
@@ -62,9 +62,6 @@ int maildir_check_empty (const char *);
 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 *);