/*
* Copyright (C) 1996-2000,2002,2013 Michael R. Elkins <me@mutt.org>
* Copyright (C) 1999-2004,2006 Thomas Roessler <roessler@does-not-exist.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "mutt.h"
#include "mutt_menu.h"
#include "attach.h"
#include "mutt_curses.h"
#include "keymap.h"
#include "rfc1524.h"
#include "mime.h"
#include "pager.h"
#include "mailbox.h"
#include "copy.h"
#include "mx.h"
#include "mutt_crypt.h"
#include "rfc3676.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int mutt_get_tmp_attachment (BODY *a)
{
char type[STRING];
BUFFER *tempfile = NULL;
rfc1524_entry *entry = NULL;
FILE *fpin = NULL, *fpout = NULL;
struct stat st;
if (a->unlink)
return 0;
tempfile = mutt_buffer_pool_get ();
entry = rfc1524_new_entry();
snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
rfc1524_mailcap_lookup(a, type, sizeof(type), entry, 0);
mutt_rfc1524_expand_filename (entry->nametemplate, a->filename, tempfile);
rfc1524_free_entry(&entry);
if (stat(a->filename, &st) == -1)
{
mutt_buffer_pool_release (&tempfile);
return -1;
}
if ((fpin = fopen(a->filename, "r")) && /* __FOPEN_CHECKED__ */
(fpout = safe_fopen(mutt_b2s (tempfile), "w")))
{
mutt_copy_stream (fpin, fpout);
mutt_str_replace (&a->filename, mutt_b2s (tempfile));
a->unlink = 1;
if (a->stamp >= st.st_mtime)
mutt_stamp_attachment(a);
}
else
mutt_perror(fpin ? mutt_b2s (tempfile) : a->filename);
if (fpin) safe_fclose (&fpin);
if (fpout) safe_fclose (&fpout);
mutt_buffer_pool_release (&tempfile);
return a->unlink ? 0 : -1;
}
/* return 1 if require full screen redraw, 0 otherwise */
int mutt_compose_attachment (BODY *a)
{
char type[STRING];
BUFFER *command = mutt_buffer_pool_get ();
BUFFER *newfile = mutt_buffer_pool_get ();
BUFFER *tempfile = mutt_buffer_pool_get ();
rfc1524_entry *entry = rfc1524_new_entry ();
short unlink_newfile = 0;
int rc = 0;
snprintf (type, sizeof (type), "%s/%s", TYPE (a), a-