/*
* Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
*
* 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., 675 Mass Ave, Cambridge, MA 02139, 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 _PGPPATH
#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")))
{
mutt_copy_stream (fpin, fpout);
FREE(&a->filename);
a->filename = safe_strdup(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
{
endwin ();
mutt_system (command);
if (entry->composetypecommand)
{
BODY *b;
FILE *fp, *tfp;
char tempfile[_POSIX_PATH_MAX];
if ((fp = safe_fopen (a->filename, "r")) == NULL)
{
mutt_perror ("Failure to open file to parse headers.");
goto bailout;
}
b = mutt_read_mime_header (fp, 0);
if (b)
{
if (b->parameter)
{
mutt_free_parameter (&a->parameter);
a->parameter = b->parameter;
b->parameter = NULL;
}
if (b->description) {
safe_free ((void **) &a->description);
a->description = b->description;
b->description = NULL;
}
if (b->form_name)
{
safe_free ((void **) &a->form_name);
a->form_name = b->form_name;
b->form_name = NULL