2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 #include "mutt_menu.h"
22 #include "mutt_curses.h"
30 #include "mutt_crypt.h"
41 int mutt_get_tmp_attachment (BODY *a)
44 char tempfile[_POSIX_PATH_MAX];
45 rfc1524_entry *entry = rfc1524_new_entry();
46 FILE *fpin = NULL, *fpout = NULL;
52 snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
53 rfc1524_mailcap_lookup(a, type, entry, 0);
54 rfc1524_expand_filename(entry->nametemplate, a->filename,
55 tempfile, sizeof(tempfile));
57 rfc1524_free_entry(&entry);
59 if(stat(a->filename, &st) == -1)
62 if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) /* __FOPEN_CHECKED__ */
64 mutt_copy_stream (fpin, fpout);
65 mutt_str_replace (&a->filename, tempfile);
68 if(a->stamp >= st.st_mtime)
69 mutt_stamp_attachment(a);
72 mutt_perror(fpin ? tempfile : a->filename);
74 if(fpin) fclose(fpin);
75 if(fpout) fclose(fpout);
77 return a->unlink ? 0 : -1;
81 /* return 1 if require full screen redraw, 0 otherwise */
82 int mutt_compose_attachment (BODY *a)
86 char newfile[_POSIX_PATH_MAX] = "";
87 rfc1524_entry *entry = rfc1524_new_entry ();
88 short unlink_newfile = 0;
91 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
92 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE))
94 if (entry->composecommand || entry->composetypecommand)
97 if (entry->composetypecommand)
98 strfcpy (command, entry->composetypecommand, sizeof (command));
100 strfcpy (command, entry->composecommand, sizeof (command));
101 if (rfc1524_expand_filename (entry->nametemplate,
102 a->filename, newfile, sizeof (newfile)))
104 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
105 a->filename, newfile));
106 if (safe_symlink (a->filename, newfile) == -1)
108 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
115 strfcpy(newfile, a->filename, sizeof(newfile));
117 if (rfc1524_expand_command (a, newfile, type,
118 command, sizeof (command)))
120 /* For now, editing requires a file, no piping */
121 mutt_error _("Mailcap compose entry requires %%s");
128 if ((r = mutt_system (command)) == -1)
129 mutt_error (_("Error running \"%s\"!"), command);
131 if (r != -1 && entry->composetypecommand)
135 char tempfile[_POSIX_PATH_MAX];
137 if ((fp = safe_fopen (a->filename, "r")) == NULL)
139 mutt_perror _("Failure to open file to parse headers.");
143 b = mutt_read_mime_header (fp, 0);
148 mutt_free_parameter (&a->parameter);
149 a->parameter = b->parameter;
152 if (b->description) {
153 FREE (&a->description);
154 a->description = b->description;
155 b->description = NULL;
159 FREE (&a->form_name);
160 a->form_name = b->form_name;
164 /* Remove headers by copying out data to another file, then
165 * copying the file back */
166 fseek (fp, b->offset, 0);
167 mutt_mktemp (tempfile);
168 if ((tfp = safe_fopen (tempfile, "w")) == NULL)
170 mutt_perror _("Failure to open file to strip headers.");
173 mutt_copy_stream (fp, tfp);
176 mutt_unlink (a->filename);
177 if (mutt_rename_file (tempfile, a->filename) != 0)
179 mutt_perror _("Failure to rename file.");
191 rfc1524_free_entry (&entry);
192 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
204 rfc1524_free_entry (&entry);
209 * Currently, this only works for send mode, as it assumes that the
210 * BODY->filename actually contains the information. I'm not sure
211 * we want to deal with editing attachments we've already received,
212 * so this should be ok.
214 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
217 int mutt_edit_attachment (BODY *a)
220 char command[STRING];
221 char newfile[_POSIX_PATH_MAX] = "";
222 rfc1524_entry *entry = rfc1524_new_entry ();
223 short unlink_newfile = 0;
226 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
227 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT))
229 if (entry->editcommand)
232 strfcpy (command, entry->editcommand, sizeof (command));
233 if (rfc1524_expand_filename (entry->nametemplate,
234 a->filename, newfile, sizeof (newfile)))
236 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
237 a->filename, newfile));
238 if (safe_symlink (a->filename, newfile) == -1)
240 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
247 strfcpy(newfile, a->filename, sizeof(newfile));
249 if (rfc1524_expand_command (a, newfile, type,
250 command, sizeof (command)))
252 /* For now, editing requires a file, no piping */
253 mutt_error _("Mailcap Edit entry requires %%s");
258 if (mutt_system (command) == -1)
259 mutt_error (_("Error running \"%s\"!"), command);
263 else if (a->type == TYPETEXT)
265 /* On text, default to editor */
266 mutt_edit_file (NONULL (Editor), newfile);
270 rfc1524_free_entry (&entry);
271 mutt_error (_("No mailcap edit entry for %s"),type);
282 rfc1524_free_entry (&entry);
287 /* for compatibility with metamail */
288 static int is_mmnoask (const char *buf)
290 char tmp[LONG_STRING], *p, *q;
293 if ((p = getenv ("MM_NOASK")) != NULL && *p)
295 if (mutt_strcmp (p, "1") == 0)
298 strfcpy (tmp, p, sizeof (tmp));
301 while ((p = strtok (p, ",")) != NULL)
303 if ((q = strrchr (p, '/')) != NULL)
307 if (ascii_strncasecmp (buf, p, q-p) == 0)
312 if (ascii_strcasecmp (buf, p) == 0)
318 lng = mutt_strlen (p);
319 if (buf[lng] == '/' && mutt_strncasecmp (buf, p, lng) == 0)
330 void mutt_check_lookup_list (BODY *b, char *type, int len)
332 LIST *t = MimeLookupList;
335 for (; t; t = t->next) {
336 i = mutt_strlen (t->data) - 1;
337 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
338 ascii_strncasecmp (type, t->data, i) == 0) ||
339 ascii_strcasecmp (type, t->data) == 0) {
343 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
344 snprintf (type, len, "%s/%s",
345 n == TYPEAUDIO ? "audio" :
346 n == TYPEAPPLICATION ? "application" :
347 n == TYPEIMAGE ? "image" :
348 n == TYPEMESSAGE ? "message" :
349 n == TYPEMODEL ? "model" :
350 n == TYPEMULTIPART ? "multipart" :
351 n == TYPETEXT ? "text" :
352 n == TYPEVIDEO ? "video" : "other",
354 dprint(1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n",
365 int mutt_is_autoview (BODY *b, const char *type)
367 LIST *t = AutoViewList;
368 char _type[SHORT_STRING];
372 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
374 strncpy (_type, type, sizeof(_type));
376 mutt_check_lookup_list (b, _type, sizeof(_type));
379 if (mutt_needs_mailcap (b))
381 if (option (OPTIMPLICITAUTOVIEW))
384 if (is_mmnoask (type))
388 for (; t; t = t->next) {
389 i = mutt_strlen (t->data) - 1;
390 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
391 ascii_strncasecmp (type, t->data, i) == 0) ||
392 ascii_strcasecmp (type, t->data) == 0)
399 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
400 int mutt_view_attachment (FILE *fp, BODY *a, int flag, HEADER *hdr,
401 ATTACHPTR **idx, short idxlen)
403 char tempfile[_POSIX_PATH_MAX] = "";
404 char pagerfile[_POSIX_PATH_MAX] = "";
410 char command[STRING];
411 char descrip[STRING];
413 rfc1524_entry *entry = NULL;
415 int unlink_tempfile = 0;
417 is_message = mutt_is_message_type(a->type, a->subtype);
418 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
419 !crypt_valid_passphrase(a->hdr->security))
421 use_mailcap = (flag == M_MAILCAP ||
422 (flag == M_REGULAR && mutt_needs_mailcap (a)));
423 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
427 entry = rfc1524_new_entry ();
428 if (!rfc1524_mailcap_lookup (a, type, entry, 0))
430 if (flag == M_REGULAR)
432 /* fallback to view as text */
433 rfc1524_free_entry (&entry);
434 mutt_error _("No matching mailcap entry found. Viewing as text.");
447 mutt_error _("MIME type not defined. Cannot view attachment.");
450 strfcpy (command, entry->command, sizeof (command));
454 fname = safe_strdup (a->filename);
455 mutt_sanitize_filename (fname, 1);
460 if (rfc1524_expand_filename (entry->nametemplate, fname,
461 tempfile, sizeof (tempfile)))
463 if (fp == NULL && mutt_strcmp(tempfile, a->filename))
465 /* send case: the file is already there */
466 if (safe_symlink (a->filename, tempfile) == -1)
468 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) == M_YES)
469 strfcpy (tempfile, a->filename, sizeof (tempfile));
477 else if (fp == NULL) /* send case */
478 strfcpy (tempfile, a->filename, sizeof (tempfile));
482 /* recv case: we need to save the attachment to a file */
484 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
488 use_pipe = rfc1524_expand_command (a, tempfile, type,
489 command, sizeof (command));
490 use_pager = entry->copiousoutput;
495 if (fp && !use_mailcap && a->filename)
498 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
499 mutt_adv_mktemp (pagerfile, sizeof(pagerfile));
502 mutt_mktemp (pagerfile);
508 int tempfd = -1, pagerfd = -1;
513 if (use_pager || use_pipe)
515 if (use_pager && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY)) == -1))
517 mutt_perror ("open");
520 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1))
524 mutt_perror ("open");
528 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
529 use_pipe ? tempfd : -1, use_pager ? pagerfd : -1, -1)) == -1)
537 mutt_error _("Cannot create filter");
544 snprintf (descrip, sizeof (descrip),
545 "---Command: %-20.20s Description: %s",
546 command, a->description);
548 snprintf (descrip, sizeof (descrip),
549 "---Command: %-30.30s Attachment: %s", command, type);
552 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
553 option (OPTWAITKEY))) && !use_pager)
554 mutt_any_key_to_continue (NULL);
562 /* interactive command */
563 if (mutt_system (command) ||
564 (entry->needsterminal && option (OPTWAITKEY)))
565 mutt_any_key_to_continue (NULL);
570 /* Don't use mailcap; the attachment is viewed in the pager */
572 if (flag == M_AS_TEXT)
574 /* just let me see the raw data */
575 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
580 /* Use built-in handler */
581 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
582 * message in case of error */
583 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0))
585 unset_option (OPTVIEWATTACH);
588 unset_option (OPTVIEWATTACH);
592 strfcpy (descrip, a->description, sizeof (descrip));
593 else if (a->filename)
594 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
597 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
600 /* We only reach this point if there have been no errors */
606 memset (&info, 0, sizeof (info));
611 info.idxlen = idxlen;
614 rc = mutt_do_pager (descrip, pagerfile,
615 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0), &info);
624 rfc1524_free_entry (&entry);
625 if (fp && tempfile[0])
626 mutt_unlink (tempfile);
627 else if (unlink_tempfile)
631 mutt_unlink (pagerfile);
636 /* returns 1 on success, 0 on error */
637 int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile)
643 if (outfile && *outfile)
644 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0)
646 mutt_perror ("open");
658 memset (&s, 0, sizeof (STATE));
660 if (outfile && *outfile)
661 thepid = mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
663 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
667 mutt_perror _("Can't create filter");
672 mutt_decode_attachment (b, &s);
673 safe_fclose (&s.fpout);
681 if ((ifp = fopen (b->filename, "r")) == NULL)
683 mutt_perror ("fopen");
684 if (outfile && *outfile)
692 if (outfile && *outfile)
693 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
695 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
699 mutt_perror _("Can't create filter");
704 mutt_copy_stream (ifp, ofp);
713 if (outfile && *outfile)
717 * check for error exit from child process
719 if (mutt_wait_filter (thepid) != 0)
722 if (rv == 0 || option (OPTWAITKEY))
723 mutt_any_key_to_continue (NULL);
728 mutt_save_attachment_open (char *path, int flags)
730 if (flags == M_SAVE_APPEND)
731 return fopen (path, "a");
732 if (flags == M_SAVE_OVERWRITE)
733 return safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
735 return safe_fopen (path, "w");
738 /* returns 0 on success, -1 on error */
739 int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr)
748 m->encoding != ENCBASE64 &&
749 m->encoding != ENCQUOTEDPRINTABLE &&
750 mutt_is_message_type(m->type, m->subtype))
752 /* message type attachments are written to mail folders. */
754 char buf[HUGE_STRING];
762 hn->msgno = hdr->msgno; /* required for MH/maildir */
765 fseek (fp, m->offset, 0);
766 if (fgets (buf, sizeof (buf), fp) == NULL)
768 if (mx_open_mailbox(path, M_APPEND | M_QUIET, &ctx) == NULL)
770 if ((msg = mx_open_new_message (&ctx, hn, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
772 mx_close_mailbox(&ctx, NULL);
775 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
777 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
778 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
779 && mx_commit_message (msg, &ctx) == 0)
784 mx_close_message (&msg);
785 mx_close_mailbox (&ctx, NULL);
790 /* In recv mode, extract from folder and decode */
794 memset (&s, 0, sizeof (s));
795 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL)
797 mutt_perror ("fopen");
800 fseek ((s.fpin = fp), m->offset, 0);
801 mutt_decode_attachment (m, &s);
803 if (fclose (s.fpout) != 0)
805 mutt_perror ("fclose");
812 /* In send mode, just copy file */
816 if ((ofp = fopen (m->filename, "r")) == NULL)
818 mutt_perror ("fopen");
822 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL)
824 mutt_perror ("fopen");
829 if (mutt_copy_stream (ofp, nfp) == -1)
831 mutt_error _("Write fault!");
843 /* returns 0 on success, -1 on error */
844 int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
845 int displaying, int flags)
848 unsigned int saved_encoding = 0;
849 BODY *saved_parts = NULL;
850 HEADER *saved_hdr = NULL;
852 memset (&s, 0, sizeof (s));
853 s.flags = displaying;
855 if (flags == M_SAVE_APPEND)
856 s.fpout = fopen (path, "a");
857 else if (flags == M_SAVE_OVERWRITE)
858 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
860 s.fpout = safe_fopen (path, "w");
864 mutt_perror ("fopen");
870 /* When called from the compose menu, the attachment isn't parsed,
871 * so we need to do it here. */
874 if (stat (m->filename, &st) == -1)
876 mutt_perror ("stat");
881 if ((s.fpin = fopen (m->filename, "r")) == NULL)
883 mutt_perror ("fopen");
887 saved_encoding = m->encoding;
888 if (!is_multipart (m))
889 m->encoding = ENC8BIT;
891 m->length = st.st_size;
893 saved_parts = m->parts;
895 mutt_parse_part (s.fpin, m);
897 if (m->noconv || is_multipart (m))
898 s.flags |= M_CHARCONV;
903 s.flags |= M_CHARCONV;
906 mutt_body_handler (m, &s);
912 m->encoding = saved_encoding;
915 mutt_free_header (&m->hdr);
916 m->parts = saved_parts;
925 /* Ok, the difference between send and receive:
926 * recv: BODY->filename is a suggested name, and Context|HEADER points
927 * to the attachment in mailbox which is encooded
928 * send: BODY->filename points to the un-encoded file which contains the
932 int mutt_print_attachment (FILE *fp, BODY *a)
934 char newfile[_POSIX_PATH_MAX] = "";
938 short unlink_newfile = 0;
940 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
942 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT))
944 char command[_POSIX_PATH_MAX+STRING];
945 rfc1524_entry *entry;
948 dprint (2, (debugfile, "Using mailcap...\n"));
950 entry = rfc1524_new_entry ();
951 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
952 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
953 newfile, sizeof (newfile)))
957 if (safe_symlink(a->filename, newfile) == -1)
959 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
961 rfc1524_free_entry (&entry);
964 strfcpy (newfile, a->filename, sizeof (newfile));
971 /* in recv mode, save file to newfile first */
973 mutt_save_attachment (fp, a, newfile, 0, NULL);
975 strfcpy (command, entry->printcommand, sizeof (command));
976 piped = rfc1524_expand_command (a, newfile, type, command, sizeof (command));
980 /* interactive program */
983 if ((ifp = fopen (newfile, "r")) == NULL)
985 mutt_perror ("fopen");
986 rfc1524_free_entry (&entry);
990 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0)
992 mutt_perror _("Can't create filter");
993 rfc1524_free_entry (&entry);
997 mutt_copy_stream (ifp, fpout);
998 safe_fclose (&fpout);
1000 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
1001 mutt_any_key_to_continue (NULL);
1005 if (mutt_system (command) || option (OPTWAITKEY))
1006 mutt_any_key_to_continue (NULL);
1010 mutt_unlink (newfile);
1011 else if (unlink_newfile)
1014 rfc1524_free_entry (&entry);
1018 if (!ascii_strcasecmp ("text/plain", type) ||
1019 !ascii_strcasecmp ("application/postscript", type))
1021 return (mutt_pipe_attachment (fp, a, NONULL(PrintCmd), NULL));
1023 else if (mutt_can_decode (a))
1025 /* decode and print */
1032 mutt_mktemp (newfile);
1033 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0)
1036 dprint (2, (debugfile, "successfully decoded %s type attachment to %s\n",
1039 if ((ifp = fopen (newfile, "r")) == NULL)
1041 mutt_perror ("fopen");
1045 dprint (2, (debugfile, "successfully opened %s read-only\n", newfile));
1048 if ((thepid = mutt_create_filter (NONULL(PrintCmd), &fpout, NULL, NULL)) < 0)
1050 mutt_perror _("Can't create filter");
1054 dprint (2, (debugfile, "Filter created.\n"));
1056 mutt_copy_stream (ifp, fpout);
1058 safe_fclose (&fpout);
1061 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1062 mutt_any_key_to_continue (NULL);
1067 safe_fclose (&fpout);
1068 mutt_unlink (newfile);
1073 mutt_error _("I don't know how to print that!");