/*
* Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
* Copyright (C) 1999-2000 Thomas Roessler <roessler@guug.de>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
#include "mutt.h"
#include "mutt_menu.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"
#ifdef HAVE_PGP
#include "pgp.h"
#endif
#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];
char tempfile[_POSIX_PATH_MAX];
rfc1524_entry *entry = rfc1524_new_entry();
FILE *fpin = NULL, *fpout = NULL;
struct stat st;
if(a->unlink)
return 0;
snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
rfc1524_mailcap_lookup(a, type, entry, 0);
rfc1524_expand_filename(entry->nametemplate, a->filename,
tempfile, sizeof(tempfile));
rfc1524_free_entry(&entry);
if(stat(a->filename, &st) == -1)
return -1;
if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) /* __FOPEN_CHECKED__ */
{
mutt_copy_stream (fpin, fpout);
mutt_str_replace (&a->filename, tempfile);
a->unlink = 1;
if(a->stamp >= st.st_mtime)
mutt_stamp_attachment(a);
}
else
mutt_perror(fpin ? tempfile : a->filename);
if(fpin) fclose(fpin);
if(fpout) fclose(fpout);
return a->unlink ? 0 : -1;
}
/* return 1 if require full screen redraw, 0 otherwise */
int mutt_compose_attachment (BODY *a)
{
char type[STRING];
char command[STRING];
char newfile[_POSIX_PATH_MAX] = "";
rfc1524_entry *entry = rfc1524_new_entry ();
short unlink_newfile = 0;
int rc = 0;
snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE))
{
if (entry->composecommand || entry->composetypecommand)
{
if (entry->composetypecommand)
strfcpy (command, entry->composetypecommand, sizeof (command));
else
strfcpy (command, entry->composecommand, sizeof (command));
if (rfc1524_expand_filename (entry->nametemplate,
a->filename, newfile, sizeof (newfile)))
{
dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
a->filename, newfile));
if (safe_symlink (a->filename, newfile) == -1)
{
if (!mutt_yesorno (_("Can't match nametemplate, continue?"), 1))
goto bailout;
}
unlink_newfile = 1;
}
else
strfcpy(newfile, a->filename, sizeof(newfile));
if (rfc1524_expand_command (a, newfile, type,
command, sizeof (command)))
{
/* For now, editing requires a file, no piping */
mutt_error _("Mailcap compose entry requires %%s");
}
else
{
int r;
mutt_endwin (NULL);
if ((r = mutt_system (command)) == -1)
mutt_error (_("Error running \"%s\"!"), command);
if (r != -1 && entry->composetypecommand)
{
BODY *b;