changeset: 6746:2b9d6165b8b7
user:      Kevin McCarthy <[email protected]>
date:      Mon Aug 01 15:04:45 2016 -0700
link:      http://dev.mutt.org/hg/mutt/rev/2b9d6165b8b7

Convert mx_open_mailbox_append() to use ctx->mx_ops.

Set the flag MUTT_NEWFOLDER to signal Maildir and MH to create the directory 
structure.

Distribute the "open append" code to mbox, mh, and imap/imap.c.

Set pop's mx_ops handler to NULL to signal it is not supported.

diffs (361 lines):

diff -r 3834da0c024e -r 2b9d6165b8b7 imap/imap.c
--- a/imap/imap.c       Sun Jul 31 18:42:07 2016 -0700
+++ b/imap/imap.c       Mon Aug 01 15:04:45 2016 -0700
@@ -771,7 +771,7 @@
   return -1;
 }
 
-int imap_open_mailbox_append (CONTEXT *ctx)
+static int imap_open_mailbox_append (CONTEXT *ctx, int flags)
 {
   IMAP_DATA *idata;
   char buf[LONG_STRING];
@@ -791,7 +791,6 @@
     return -1;
   }
 
-  ctx->magic = MUTT_IMAP;
   ctx->data = idata;
 
   imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
@@ -2174,6 +2173,7 @@
 
 struct mx_ops mx_imap_ops = {
   .open = imap_open_mailbox,
+  .open_append = imap_open_mailbox_append,
   .close = imap_close_mailbox,
   .open_msg = imap_fetch_message,
   .close_msg = imap_close_message,
diff -r 3834da0c024e -r 2b9d6165b8b7 imap/imap.h
--- a/imap/imap.h       Sun Jul 31 18:42:07 2016 -0700
+++ b/imap/imap.h       Mon Aug 01 15:04:45 2016 -0700
@@ -35,7 +35,6 @@
 int imap_access (const char*, int);
 int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force);
 int imap_delete_mailbox (CONTEXT* idata, IMAP_MBOX mx);
-int imap_open_mailbox_append (CONTEXT *ctx);
 int imap_sync_mailbox (CONTEXT *ctx, int expunge, int *index_hint);
 int imap_close_mailbox (CONTEXT *ctx);
 int imap_buffy_check (int force, int check_stats);
diff -r 3834da0c024e -r 2b9d6165b8b7 mbox.c
--- a/mbox.c    Sun Jul 31 18:42:07 2016 -0700
+++ b/mbox.c    Mon Aug 01 15:04:45 2016 -0700
@@ -442,6 +442,27 @@
   return (rc);
 }
 
+static int mbox_open_mailbox_append (CONTEXT *ctx, int flags)
+{
+  ctx->fp = safe_fopen (ctx->path, flags & MUTT_NEWFOLDER ? "w" : "a");
+  if (!ctx->fp)
+  {
+    mutt_perror (ctx->path);
+    return -1;
+  }
+
+  if (mbox_lock_mailbox (ctx, 1, 1) != 0)
+  {
+    mutt_error (_("Couldn't lock %s\n"), ctx->path);
+    safe_fclose (&ctx->fp);
+    return -1;
+  }
+
+  fseek (ctx->fp, 0, 2);
+
+  return 0;
+}
+
 static int mbox_close_mailbox (CONTEXT *ctx)
 {
   return 0;
@@ -1318,6 +1339,7 @@
 
 struct mx_ops mx_mbox_ops = {
   .open = mbox_open_mailbox,
+  .open_append = mbox_open_mailbox_append,
   .close = mbox_close_mailbox,
   .open_msg = mbox_open_message,
   .close_msg = mbox_close_message,
@@ -1328,6 +1350,7 @@
 
 struct mx_ops mx_mmdf_ops = {
   .open = mbox_open_mailbox,
+  .open_append = mbox_open_mailbox_append,
   .close = mbox_close_mailbox,
   .open_msg = mbox_open_message,
   .close_msg = mbox_close_message,
diff -r 3834da0c024e -r 2b9d6165b8b7 mh.c
--- a/mh.c      Sun Jul 31 18:42:07 2016 -0700
+++ b/mh.c      Mon Aug 01 15:04:45 2016 -0700
@@ -1290,11 +1290,84 @@
   return maildir_read_dir (ctx);
 }
 
+static int maildir_open_mailbox_append (CONTEXT *ctx, int flags)
+{
+  char tmp[_POSIX_PATH_MAX];
+
+  if (flags & MUTT_NEWFOLDER)
+  {
+    if (mkdir (ctx->path, S_IRWXU))
+    {
+      mutt_perror (ctx->path);
+      return (-1);
+    }
+
+    snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
+    if (mkdir (tmp, S_IRWXU))
+    {
+      mutt_perror (tmp);
+      rmdir (ctx->path);
+      return (-1);
+    }
+
+    snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
+    if (mkdir (tmp, S_IRWXU))
+    {
+      mutt_perror (tmp);
+      snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
+      rmdir (tmp);
+      rmdir (ctx->path);
+      return (-1);
+    }
+
+    snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
+    if (mkdir (tmp, S_IRWXU))
+    {
+      mutt_perror (tmp);
+      snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
+      rmdir (tmp);
+      snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
+      rmdir (tmp);
+      rmdir (ctx->path);
+      return (-1);
+    }
+  }
+
+  return 0;
+}
+
 static int mh_open_mailbox (CONTEXT *ctx)
 {
   return mh_read_dir (ctx, NULL);
 }
 
+static int mh_open_mailbox_append (CONTEXT *ctx, int flags)
+{
+  char tmp[_POSIX_PATH_MAX];
+  int i;
+
+  if (flags & MUTT_NEWFOLDER)
+  {
+    if (mkdir (ctx->path, S_IRWXU))
+    {
+      mutt_perror (ctx->path);
+      return (-1);
+    }
+
+    snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
+    if ((i = creat (tmp, S_IRWXU)) == -1)
+    {
+      mutt_perror (tmp);
+      rmdir (ctx->path);
+      return (-1);
+    }
+    close (i);
+  }
+
+  return 0;
+}
+
+
 /*
  * Open a new (temporary) message in an MH folder.
  */
@@ -2453,6 +2526,7 @@
 
 struct mx_ops mx_maildir_ops = {
   .open = maildir_open_mailbox,
+  .open_append = maildir_open_mailbox_append,
   .close = mh_close_mailbox,
   .open_msg = maildir_open_message,
   .close_msg = mh_close_message,
@@ -2463,6 +2537,7 @@
 
 struct mx_ops mx_mh_ops = {
   .open = mh_open_mailbox,
+  .open_append = mh_open_mailbox_append,
   .close = mh_close_mailbox,
   .open_msg = mh_open_message,
   .close_msg = mh_close_message,
diff -r 3834da0c024e -r 2b9d6165b8b7 mutt.h
--- a/mutt.h    Sun Jul 31 18:42:07 2016 -0700
+++ b/mutt.h    Mon Aug 01 15:04:45 2016 -0700
@@ -894,8 +894,9 @@
  */
 struct mx_ops
 {
-  int (*open)(struct _context *);
-  int (*close)(struct _context *);
+  int (*open) (struct _context *);
+  int (*open_append) (struct _context *, int flags);
+  int (*close) (struct _context *);
   int (*check) (struct _context *ctx, int *index_hint);
   int (*open_msg) (struct _context *, struct _message *, int msgno);
   int (*close_msg) (struct _context *, struct _message *);
diff -r 3834da0c024e -r 2b9d6165b8b7 mx.c
--- a/mx.c      Sun Jul 31 18:42:07 2016 -0700
+++ b/mx.c      Mon Aug 01 15:04:45 2016 -0700
@@ -481,122 +481,37 @@
   struct stat sb;
 
   ctx->append = 1;
+  ctx->magic = mx_get_magic (ctx->path);
+  if (ctx->magic == 0)
+  {
+    mutt_error (_("%s is not a mailbox."), ctx->path);
+    return -1;
+  }
 
-#ifdef USE_IMAP
-  
-  if(mx_is_imap(ctx->path))  
-    return imap_open_mailbox_append (ctx);
-
-#endif
-  
-  if(stat(ctx->path, &sb) == 0)
+  if (ctx->magic < 0)
   {
-    ctx->magic = mx_get_magic (ctx->path);
-    
-    switch (ctx->magic)
+    if (stat (ctx->path, &sb) == -1)
     {
-      case 0:
-       mutt_error (_("%s is not a mailbox."), ctx->path);
-       /* fall through */
-      case -1:
-       return (-1);
-    }
-  }
-  else if (errno == ENOENT)
-  {
-    ctx->magic = DefaultMagic;
-
-    if (ctx->magic == MUTT_MH || ctx->magic == MUTT_MAILDIR)
-    {
-      char tmp[_POSIX_PATH_MAX];
-
-      if (mkdir (ctx->path, S_IRWXU))
+      if (errno == ENOENT)
       {
-       mutt_perror (ctx->path);
-       return (-1);
-      }
-
-      if (ctx->magic == MUTT_MAILDIR)
-      {
-       snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
-       if (mkdir (tmp, S_IRWXU))
-       {
-         mutt_perror (tmp);
-         rmdir (ctx->path);
-         return (-1);
-       }
-
-       snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
-       if (mkdir (tmp, S_IRWXU))
-       {
-         mutt_perror (tmp);
-         snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
-         rmdir (tmp);
-         rmdir (ctx->path);
-         return (-1);
-       }
-       snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
-       if (mkdir (tmp, S_IRWXU))
-       {
-         mutt_perror (tmp);
-         snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
-         rmdir (tmp);
-         snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
-         rmdir (tmp);
-         rmdir (ctx->path);
-         return (-1);
-       }
+        ctx->magic = DefaultMagic;
+        flags |= MUTT_NEWFOLDER;
       }
       else
       {
-       int i;
-
-       snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
-       if ((i = creat (tmp, S_IRWXU)) == -1)
-       {
-         mutt_perror (tmp);
-         rmdir (ctx->path);
-         return (-1);
-       }
-       close (i);
+        mutt_perror (ctx->path);
+        return -1;
       }
     }
-  }
-  else
-  {
-    mutt_perror (ctx->path);
-    return (-1);
+    else
+      return -1;
   }
 
-  switch (ctx->magic)
-  {
-    case MUTT_MBOX:
-    case MUTT_MMDF:
-    if ((ctx->fp = safe_fopen (ctx->path, flags & MUTT_NEWFOLDER ? "w" : "a")) 
== NULL ||
-         mbox_lock_mailbox (ctx, 1, 1) != 0)
-      {
-       if (!ctx->fp)
-         mutt_perror (ctx->path);
-       else
-       {
-         mutt_error (_("Couldn't lock %s\n"), ctx->path);
-         safe_fclose (&ctx->fp);
-       }
-       return (-1);
-      }
-      fseek (ctx->fp, 0, 2);
-      break;
+  ctx->mx_ops = mx_get_ops (ctx->magic);
+  if (!ctx->mx_ops || !ctx->mx_ops->open_append)
+    return -1;
 
-    case MUTT_MH:
-    case MUTT_MAILDIR:
-      /* nothing to do */
-      break;
-
-    default:
-      return (-1);
-  }
-
-  return 0;
+  return ctx->mx_ops->open_append (ctx, flags);
 }
 
 /* close a mailbox opened in write-mode */
diff -r 3834da0c024e -r 2b9d6165b8b7 pop.c
--- a/pop.c     Sun Jul 31 18:42:07 2016 -0700
+++ b/pop.c     Mon Aug 01 15:04:45 2016 -0700
@@ -937,6 +937,7 @@
 
 struct mx_ops mx_pop_ops = {
   .open = pop_open_mailbox,
+  .open_append = NULL,
   .close = pop_close_mailbox,
   .open_msg = pop_fetch_message,
   .close_msg = pop_close_message,

Reply via email to