mutt_*mktemp--
[apps/madmutt.git] / recvcmd.c
index 4e37015..45dec59 100644 (file)
--- a/recvcmd.c
+++ b/recvcmd.c
@@ -14,6 +14,7 @@
 #include <lib-ui/curses.h>
 #include <lib-ui/enter.h>
 #include <lib-ui/menu.h>
+#include <lib-mx/mx.h>
 
 #include "mutt.h"
 #include "alias.h"
@@ -21,7 +22,6 @@
 #include "handler.h"
 #include "recvattach.h"
 #include "attach.h"
-#include "mx.h"
 #include "copy.h"
 #include "mutt_idna.h"
 
@@ -316,6 +316,70 @@ static void include_header (int quote, FILE * ifp,
   mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
 }
 
+/* create a send-mode duplicate from a receive-mode body */
+static int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
+{
+  char tmp[_POSIX_PATH_MAX];
+  BODY *b;
+
+  parameter_t *par, **ppar;
+
+  short use_disp;
+
+  if (src->filename) {
+    use_disp = 1;
+    m_strcpy(tmp, sizeof(tmp), src->filename);
+  } else {
+    use_disp = 0;
+    tmp[0] = '\0';
+  }
+
+  mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
+  if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
+    return -1;
+
+  *tgt = body_new();
+  b = *tgt;
+
+  memcpy (b, src, sizeof (BODY));
+  b->parts = NULL;
+  b->next = NULL;
+
+  b->filename = m_strdup(tmp);
+  b->use_disp = use_disp;
+  b->unlink = 1;
+
+  if (mutt_is_text_part (b))
+    b->noconv = 1;
+
+  b->xtype = m_strdup(b->xtype);
+  b->subtype = m_strdup(b->subtype);
+  b->form_name = m_strdup(b->form_name);
+  b->filename = m_strdup(b->filename);
+  b->d_filename = m_strdup(b->d_filename);
+  b->description = m_strdup(b->description);
+
+  /* 
+   * we don't seem to need the HEADER structure currently.
+   * XXX - this may change in the future
+   */
+
+  if (b->hdr)
+    b->hdr = NULL;
+
+  /* copy parameters */
+  for (par = b->parameter, ppar = &b->parameter; par;
+       ppar = &(*ppar)->next, par = par->next) {
+    *ppar = parameter_new();
+    (*ppar)->attribute = m_strdup(par->attribute);
+    (*ppar)->value = m_strdup(par->value);
+  }
+
+  mutt_stamp_attachment (b);
+
+  return 0;
+}
+
 /* Attach all the body parts which can't be decoded. 
  * This code is shared by forwarding and replying. */
 
@@ -378,9 +442,9 @@ static void attach_forward_bodies (FILE * fp, HEADER * hdr,
   tmphdr->env = envelope_new();
   mutt_make_forward_subject (tmphdr->env, Context, parent);
 
-  mutt_mktemp (tmpbody);
-  if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL) {
-    mutt_error (_("Can't open temporary file %s."), tmpbody);
+  tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(Tempdir), NULL);
+  if (!tmpfp) {
+    mutt_error(_("Could not create temporary file"));
     return;
   }
 
@@ -478,21 +542,15 @@ static void attach_forward_bodies (FILE * fp, HEADER * hdr,
   }
 
   mutt_forward_trailer (tmpfp);
-
-  fclose (tmpfp);
-  tmpfp = NULL;
+  m_fclose(&tmpfp);
 
   /* now that we have the template, send it. */
   ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
   return;
 
 bail:
-
-  if (tmpfp) {
-    fclose (tmpfp);
-    mutt_unlink (tmpbody);
-  }
-
+  m_fclose(&tmpfp);
+  mutt_unlink(tmpbody);
   header_delete(&tmphdr);
 }
 
@@ -546,9 +604,9 @@ static void attach_forward_msgs (FILE * fp, HEADER * hdr __attribute__ ((unused)
 
     /* no MIME encapsulation */
 
-    mutt_mktemp (tmpbody);
-    if (!(tmpfp = safe_fopen (tmpbody, "w"))) {
-      mutt_error (_("Can't create %s."), tmpbody);
+    tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(Tempdir), NULL);
+    if (!tmpfp) {
+      mutt_error(_("Could not create temporary file"));
       header_delete(&tmphdr);
       return;
     }
@@ -586,7 +644,7 @@ static void attach_forward_msgs (FILE * fp, HEADER * hdr __attribute__ ((unused)
         }
       }
     }
-    fclose (tmpfp);
+    m_fclose(&tmpfp);
   }
   else if (rc == M_YES) {       /* do MIME encapsulation - we don't need to do much here */
     last = &tmphdr->content;
@@ -794,9 +852,9 @@ void mutt_attach_reply (FILE * fp, HEADER * hdr,
     return;
   }
 
-  mutt_mktemp (tmpbody);
-  if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL) {
-    mutt_error (_("Can't create %s."), tmpbody);
+  tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(Tempdir), NULL);
+  if (!tmpfp) {
+    mutt_error(_("Could not create temporary file"));
     header_delete(&tmphdr);
     return;
   }
@@ -856,12 +914,12 @@ void mutt_attach_reply (FILE * fp, HEADER * hdr,
         copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen,
                                       0) == NULL) {
       header_delete(&tmphdr);
-      fclose (tmpfp);
+      m_fclose(&tmpfp);
       return;
     }
   }
 
-  fclose (tmpfp);
+  m_fclose(&tmpfp);
 
   if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
     mutt_set_flag (Context, hdr, M_REPLIED, 1);