]> granicus.if.org Git - neomutt/commitdiff
add mx_path_resolve()
authorRichard Russon <rich@flatcap.org>
Fri, 28 Dec 2018 14:51:34 +0000 (14:51 +0000)
committerRichard Russon <rich@flatcap.org>
Fri, 8 Feb 2019 16:28:45 +0000 (16:28 +0000)
commands.c
compose.c
editmsg.c
index.c
main.c
mutt_attach.c
mx.c
mx.h
pop/pop.c
postpone.c
sendlib.c

index 547b0bc5de258e4c8b73478fc3bb790823370cb7..bc2e07e822427290a9e348dc7558b8bbe1a8d334 100644 (file)
@@ -1067,9 +1067,13 @@ int mutt_save_message(struct Mailbox *m, struct EmailList *el, bool delete, bool
   }
 #endif
 
-  savectx = mx_mbox_open(NULL, buf, MUTT_APPEND);
+  struct Mailbox *m_save = mx_path_resolve(buf);
+  savectx = mx_mbox_open(m_save, NULL, MUTT_APPEND);
   if (!savectx)
+  {
+    mailbox_free(&m_save);
     return -1;
+  }
 
 #ifdef USE_COMPRESSED
   /* If we're saving to a compressed mailbox, the stats won't be updated
index 944f3ef52fb9a7aa799303a8c14efcd5e739e277..f05cc445e621f0c75098c7dab939300970d95605 100644 (file)
--- a/compose.c
+++ b/compose.c
@@ -1450,10 +1450,12 @@ int mutt_compose_menu(struct Email *msg, char *fcc, size_t fcclen, struct Email
 
         menu->redraw = REDRAW_FULL;
 
-        ctx = mx_mbox_open(NULL, fname, MUTT_READONLY);
+        struct Mailbox *m = mx_path_resolve(fname);
+        ctx = mx_mbox_open(m, NULL, MUTT_READONLY);
         if (!ctx)
         {
           mutt_error(_("Unable to open mailbox %s"), fname);
+          mailbox_free(&m);
           break;
         }
 
index 9955fb5fe980b47e1f79d5d99a8c5ecff4f4dd1c..eed210915b8069312348be36b4cab07f669c76d1 100644 (file)
--- a/editmsg.c
+++ b/editmsg.c
@@ -68,12 +68,14 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
 
   enum MailboxType omagic = MboxType;
   MboxType = MUTT_MBOX;
-  struct Context *tmpctx = mx_mbox_open(NULL, fname, MUTT_NEWFOLDER);
+  struct Mailbox *m_fname = mx_path_resolve(fname);
+  struct Context *tmpctx = mx_mbox_open(m_fname, NULL, MUTT_NEWFOLDER);
   MboxType = omagic;
 
   if (!tmpctx)
   {
     mutt_error(_("could not create temporary folder: %s"), strerror(errno));
+    mailbox_free(&m_fname);
     return -1;
   }
 
diff --git a/index.c b/index.c
index b215f4fa3d8aa115ec406b833249d8a28b984a94..deed4319916c9a471a959f27060dfb39bb08fa67 100644 (file)
--- a/index.c
+++ b/index.c
@@ -644,7 +644,13 @@ static int main_change_folder(struct Menu *menu, int op, struct Mailbox *m,
     ? MUTT_READONLY
     : 0;
 
-  Context = mx_mbox_open(m, buf, flags);
+  bool free_m = false;
+  if (!m)
+  {
+    m = mx_path_resolve(buf);
+    free_m = true;
+  }
+  Context = mx_mbox_open(m, NULL, flags);
   if (Context)
   {
     menu->current = ci_first_message();
@@ -653,7 +659,11 @@ static int main_change_folder(struct Menu *menu, int op, struct Mailbox *m,
 #endif
   }
   else
+  {
     menu->current = 0;
+    if (free_m)
+      mailbox_free(&m);
+  }
 
   if (((Sort & SORT_MASK) == SORT_THREADS) && CollapseAll)
     collapse_all(menu, 0);
diff --git a/main.c b/main.c
index 8b18ad8458ea77f1ccfb5b0f6ca6cae822c9e159..9b8d33396821a02f3928444d9461f1ae1b18ff70 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1208,8 +1208,12 @@ int main(int argc, char *argv[], char *envp[])
     mutt_startup_shutdown_hook(MUTT_STARTUP_HOOK);
 
     repeat_error = true;
-    Context = mx_mbox_open(NULL, folder,
-                           ((flags & MUTT_RO) || ReadOnly) ? MUTT_READONLY : 0);
+    struct Mailbox *m = mx_path_resolve(folder);
+    Context = mx_mbox_open(m, NULL, ((flags & MUTT_RO) || ReadOnly) ? MUTT_READONLY : 0);
+    if (!Context)
+    {
+      mailbox_free(&m);
+    }
     if (Context || !explicit_folder)
     {
 #ifdef USE_SIDEBAR
index 7c700264850efc5ee0d1f1c98f94114217e43c65..4f6664e9098fd6e4fad967913acb42d8f551dd68 100644 (file)
@@ -813,9 +813,13 @@ int mutt_save_attachment(FILE *fp, struct Body *m, char *path, int flags, struct
         return -1;
       if (!fgets(buf, sizeof(buf), fp))
         return -1;
-      struct Context *ctx = mx_mbox_open(NULL, path, MUTT_APPEND | MUTT_QUIET);
+      struct Mailbox *m_att = mx_path_resolve(path);
+      struct Context *ctx = mx_mbox_open(m_att, NULL, MUTT_APPEND | MUTT_QUIET);
       if (!ctx)
+      {
+        mailbox_free(&m_att);
         return -1;
+      }
       msg = mx_msg_open_new(ctx->mailbox, en, is_from(buf, NULL, 0, NULL) ? 0 : MUTT_ADD_FROM);
       if (!msg)
       {
diff --git a/mx.c b/mx.c
index d1542b5b5ce452643fd5dd3dfe459b3c8bc24531..f684794bf0616c02ec6ccef5cabb907c8ef9b710 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -525,7 +525,8 @@ static int trash_append(struct Mailbox *m)
   }
 #endif
 
-  struct Context *ctx_trash = mx_mbox_open(NULL, Trash, MUTT_APPEND);
+  struct Mailbox *m_trash = mx_path_resolve(Trash);
+  struct Context *ctx_trash = mx_mbox_open(m_trash, NULL, MUTT_APPEND);
   if (ctx_trash)
   {
     /* continue from initial scan above */
@@ -546,6 +547,7 @@ static int trash_append(struct Mailbox *m)
   else
   {
     mutt_error(_("Can't open trash folder"));
+    mailbox_free(&m_trash);
     return -1;
   }
 
@@ -700,29 +702,33 @@ int mx_mbox_close(struct Context **pctx)
     else /* use regular append-copy mode */
 #endif
     {
-      struct Context *f = mx_mbox_open(NULL, mbox, MUTT_APPEND);
-      if (!f)
+      struct Mailbox *m_read = mx_path_resolve(mbox);
+      struct Context *ctx_read = mx_mbox_open(m_read, NULL, MUTT_APPEND);
+      if (!ctx_read)
+      {
+        mailbox_free(&m_read);
         return -1;
+      }
 
       for (i = 0; i < m->msg_count; i++)
       {
         if (m->emails[i]->read && !m->emails[i]->deleted &&
             !(m->emails[i]->flagged && KeepFlagged))
         {
-          if (mutt_append_message(f->mailbox, ctx->mailbox, m->emails[i], 0, CH_UPDATE_LEN) == 0)
+          if (mutt_append_message(ctx_read->mailbox, ctx->mailbox, m->emails[i], 0, CH_UPDATE_LEN) == 0)
           {
             mutt_set_flag(m, m->emails[i], MUTT_DELETE, 1);
             mutt_set_flag(m, m->emails[i], MUTT_PURGE, 1);
           }
           else
           {
-            mx_mbox_close(&f);
+            mx_mbox_close(&ctx_read);
             return -1;
           }
         }
       }
 
-      mx_mbox_close(&f);
+      mx_mbox_close(&ctx_read);
     }
   }
   else if (!m->changed && m->msg_deleted == 0)
@@ -1526,6 +1532,26 @@ struct Mailbox *mx_mbox_find2(const char *path)
   return NULL;
 }
 
+/**
+ * mx_path_resolve - XXX
+ */
+struct Mailbox *mx_path_resolve(const char *path)
+{
+  if (!path)
+    return NULL;
+
+  struct Mailbox *m = mx_mbox_find2(path);
+  if (m)
+    return m;
+
+  m = mailbox_new();
+  m->flags = MB_HIDDEN;
+  mutt_str_strfcpy(m->path, path, sizeof(m->path));
+  mx_path_canon2(m, Folder);
+
+  return m;
+}
+
 /**
  * mx_ac_add - Add a Mailbox to an Account - Wrapper for MxOps::ac_add
  */
diff --git a/mx.h b/mx.h
index 8bfff7b770e9275a98a4b5148b97c0fce04b4729..a41f12e1a187e970313e458326fcb7ef57da31f5 100644 (file)
--- a/mx.h
+++ b/mx.h
@@ -284,6 +284,7 @@ int             mx_path_canon2     (struct Mailbox *m, const char *folder);
 int             mx_path_parent     (char *buf, size_t buflen);
 int             mx_path_pretty     (char *buf, size_t buflen, const char *folder);
 enum MailboxType mx_path_probe     (const char *path, struct stat *st);
+struct Mailbox *mx_path_resolve    (const char *path);
 int             mx_tags_commit     (struct Mailbox *m, struct Email *e, char *tags);
 int             mx_tags_edit       (struct Mailbox *m, const char *tags, char *buf, size_t buflen);
 
index d0289225e42d5842970eabbf050c7a5f6b0ed5c6..cd0ed0a21aff430ba7998cd1bd4fe8531958bef4 100644 (file)
--- a/pop/pop.c
+++ b/pop/pop.c
@@ -655,9 +655,13 @@ void pop_fetch_mail(void)
     goto finish;
   }
 
-  struct Context *ctx = mx_mbox_open(NULL, Spoolfile, MUTT_APPEND);
+  struct Mailbox *m_spool = mx_path_resolve(Spoolfile);
+  struct Context *ctx = mx_mbox_open(m_spool, NULL, MUTT_APPEND);
   if (!ctx)
+  {
+    mailbox_free(&m_spool);
     goto finish;
+  }
 
   delanswer = query_quadoption(PopDelete, _("Delete messages from server?"));
 
index 157a5e9b758e217e7b0a70ba8110c3698f6467e8..5b13c9d68586037314a77a3a1406eb2a9651d601 100644 (file)
@@ -170,9 +170,13 @@ int mutt_num_postponed(struct Mailbox *m, bool force)
     if (optnews)
       OptNews = false;
 #endif
-    struct Context *ctx = mx_mbox_open(NULL, Postponed, MUTT_NOSORT | MUTT_QUIET);
+    struct Mailbox *m_post = mx_path_resolve(Postponed);
+    struct Context *ctx = mx_mbox_open(m_post, NULL, MUTT_NOSORT | MUTT_QUIET);
     if (!ctx)
+    {
+      mailbox_free(&m_post);
       PostCount = 0;
+    }
     else
       PostCount = ctx->mailbox->msg_count;
     mx_fastclose_mailbox(ctx->mailbox);
@@ -295,16 +299,17 @@ int mutt_get_postponed(struct Context *ctx, struct Email *hdr,
   if (!Postponed)
     return -1;
 
-  struct Mailbox *m = mx_mbox_find2(Postponed);
+  struct Mailbox *m = mx_path_resolve(Postponed);
   if (ctx->mailbox == m)
     PostContext = ctx;
   else
-    PostContext = mx_mbox_open(m, Postponed, MUTT_NOSORT);
+    PostContext = mx_mbox_open(m, NULL, MUTT_NOSORT);
 
   if (!PostContext)
   {
     PostCount = 0;
     mutt_error(_("No postponed messages"));
+    mailbox_free(&m);
     return -1;
   }
 
index 6dfd20135ef2b1ce7be9eccd4e8f78e0c6c16c85..08180f49fc0138d01c3831908ccb6a5f7789a9ee 100644 (file)
--- a/sendlib.c
+++ b/sendlib.c
@@ -3159,24 +3159,26 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid,
 #ifdef RECORD_FOLDER_HOOK
   mutt_folder_hook(path, NULL);
 #endif
-  struct Context *f = mx_mbox_open(NULL, path, MUTT_APPEND | MUTT_QUIET);
-  if (!f)
+  struct Mailbox *m_fcc = mx_path_resolve(path);
+  struct Context *ctx_fcc = mx_mbox_open(m_fcc, NULL, MUTT_APPEND | MUTT_QUIET);
+  if (!ctx_fcc)
   {
     mutt_debug(1, "unable to open mailbox %s in append-mode, aborting.\n", path);
+    mailbox_free(&m_fcc);
     goto done;
   }
 
   /* We need to add a Content-Length field to avoid problems where a line in
    * the message body begins with "From "
    */
-  if ((f->mailbox->magic == MUTT_MMDF) || (f->mailbox->magic == MUTT_MBOX))
+  if ((ctx_fcc->mailbox->magic == MUTT_MMDF) || (ctx_fcc->mailbox->magic == MUTT_MBOX))
   {
     mutt_mktemp(tempfile, sizeof(tempfile));
     tempfp = mutt_file_fopen(tempfile, "w+");
     if (!tempfp)
     {
       mutt_perror(tempfile);
-      mx_mbox_close(&f);
+      mx_mbox_close(&ctx_fcc);
       goto done;
     }
     /* remember new mail status before appending message */
@@ -3188,11 +3190,11 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid,
   onm_flags = MUTT_ADD_FROM;
   if (post)
     onm_flags |= MUTT_SET_DRAFT;
-  msg = mx_msg_open_new(f->mailbox, e, onm_flags);
+  msg = mx_msg_open_new(ctx_fcc->mailbox, e, onm_flags);
   if (!msg)
   {
     mutt_file_fclose(&tempfp);
-    mx_mbox_close(&f);
+    mx_mbox_close(&ctx_fcc);
     goto done;
   }
 
@@ -3218,7 +3220,7 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid,
   if (post && fcc)
     fprintf(msg->fp, "X-Mutt-Fcc: %s\n", fcc);
 
-  if ((f->mailbox->magic == MUTT_MMDF) || (f->mailbox->magic == MUTT_MBOX))
+  if ((ctx_fcc->mailbox->magic == MUTT_MMDF) || (ctx_fcc->mailbox->magic == MUTT_MBOX))
     fprintf(msg->fp, "Status: RO\n");
 
   /* mutt_rfc822_write_header() only writes out a Date: header with
@@ -3310,9 +3312,9 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid,
       mutt_debug(1, "%s: write failed.\n", tempfile);
       mutt_file_fclose(&tempfp);
       unlink(tempfile);
-      mx_msg_commit(f->mailbox, msg); /* XXX really? */
-      mx_msg_close(f->mailbox, &msg);
-      mx_mbox_close(&f);
+      mx_msg_commit(ctx_fcc->mailbox, msg); /* XXX really? */
+      mx_msg_close(ctx_fcc->mailbox, &msg);
+      mx_mbox_close(&ctx_fcc);
       goto done;
     }
 
@@ -3338,12 +3340,12 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid,
     rc = mutt_write_mime_body(e->content, msg->fp);
   }
 
-  if (mx_msg_commit(f->mailbox, msg) != 0)
+  if (mx_msg_commit(ctx_fcc->mailbox, msg) != 0)
     rc = -1;
   else if (finalpath)
     *finalpath = mutt_str_strdup(msg->committed_path);
-  mx_msg_close(f->mailbox, &msg);
-  mx_mbox_close(&f);
+  mx_msg_close(ctx_fcc->mailbox, &msg);
+  mx_mbox_close(&ctx_fcc);
 
   if (!post && need_mailbox_cleanup)
     mutt_mailbox_cleanup(path, &st);