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.
25 #include "mutt_menu.h"
26 #include "mutt_curses.h"
34 #include "mutt_crypt.h"
45 int mutt_get_tmp_attachment (BODY *a)
48 char tempfile[_POSIX_PATH_MAX];
49 rfc1524_entry *entry = rfc1524_new_entry();
50 FILE *fpin = NULL, *fpout = NULL;
56 snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
57 rfc1524_mailcap_lookup(a, type, entry, 0);
58 rfc1524_expand_filename(entry->nametemplate, a->filename,
59 tempfile, sizeof(tempfile));
61 rfc1524_free_entry(&entry);
63 if(stat(a->filename, &st) == -1)
66 if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) /* __FOPEN_CHECKED__ */
68 mutt_copy_stream (fpin, fpout);
69 mutt_str_replace (&a->filename, tempfile);
72 if(a->stamp >= st.st_mtime)
73 mutt_stamp_attachment(a);
76 mutt_perror(fpin ? tempfile : a->filename);
78 if(fpin) fclose(fpin);
79 if(fpout) fclose(fpout);
81 return a->unlink ? 0 : -1;
85 /* return 1 if require full screen redraw, 0 otherwise */
86 int mutt_compose_attachment (BODY *a)
90 char newfile[_POSIX_PATH_MAX] = "";
91 rfc1524_entry *entry = rfc1524_new_entry ();
92 short unlink_newfile = 0;
95 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
96 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE))
98 if (entry->composecommand || entry->composetypecommand)
101 if (entry->composetypecommand)
102 strfcpy (command, entry->composetypecommand, sizeof (command));
104 strfcpy (command, entry->composecommand, sizeof (command));
105 if (rfc1524_expand_filename (entry->nametemplate,
106 a->filename, newfile, sizeof (newfile)))
108 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
109 a->filename, newfile));
110 if (safe_symlink (a->filename, newfile) == -1)
112 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
119 strfcpy(newfile, a->filename, sizeof(newfile));
121 if (rfc1524_expand_command (a, newfile, type,
122 command, sizeof (command)))
124 /* For now, editing requires a file, no piping */
125 mutt_error _("Mailcap compose entry requires %%s");
132 if ((r = mutt_system (command)) == -1)
133 mutt_error (_("Error running \"%s\"!"), command);
135 if (r != -1 && entry->composetypecommand)
139 char tempfile[_POSIX_PATH_MAX];
141 if ((fp = safe_fopen (a->filename, "r")) == NULL)
143 mutt_perror _("Failure to open file to parse headers.");
147 b = mutt_read_mime_header (fp, 0);
152 mutt_free_parameter (&a->parameter);
153 a->parameter = b->parameter;
156 if (b->description) {
157 FREE (&a->description);
158 a->description = b->description;
159 b->description = NULL;
163 FREE (&a->form_name);
164 a->form_name = b->form_name;
168 /* Remove headers by copying out data to another file, then
169 * copying the file back */
170 fseek (fp, b->offset, 0);
171 mutt_mktemp (tempfile);
172 if ((tfp = safe_fopen (tempfile, "w")) == NULL)
174 mutt_perror _("Failure to open file to strip headers.");
177 mutt_copy_stream (fp, tfp);
180 mutt_unlink (a->filename);
181 if (mutt_rename_file (tempfile, a->filename) != 0)
183 mutt_perror _("Failure to rename file.");
195 rfc1524_free_entry (&entry);
196 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
208 rfc1524_free_entry (&entry);
213 * Currently, this only works for send mode, as it assumes that the
214 * BODY->filename actually contains the information. I'm not sure
215 * we want to deal with editing attachments we've already received,
216 * so this should be ok.
218 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
221 int mutt_edit_attachment (BODY *a)
224 char command[STRING];
225 char newfile[_POSIX_PATH_MAX] = "";
226 rfc1524_entry *entry = rfc1524_new_entry ();
227 short unlink_newfile = 0;
230 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
231 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT))
233 if (entry->editcommand)
236 strfcpy (command, entry->editcommand, sizeof (command));
237 if (rfc1524_expand_filename (entry->nametemplate,
238 a->filename, newfile, sizeof (newfile)))
240 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
241 a->filename, newfile));
242 if (safe_symlink (a->filename, newfile) == -1)
244 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
251 strfcpy(newfile, a->filename, sizeof(newfile));
253 if (rfc1524_expand_command (a, newfile, type,
254 command, sizeof (command)))
256 /* For now, editing requires a file, no piping */
257 mutt_error _("Mailcap Edit entry requires %%s");
262 if (mutt_system (command) == -1)
263 mutt_error (_("Error running \"%s\"!"), command);
267 else if (a->type == TYPETEXT)
269 /* On text, default to editor */
270 mutt_edit_file (NONULL (Editor), newfile);
274 rfc1524_free_entry (&entry);
275 mutt_error (_("No mailcap edit entry for %s"),type);
286 rfc1524_free_entry (&entry);
291 /* for compatibility with metamail */
292 static int is_mmnoask (const char *buf)
294 char tmp[LONG_STRING], *p, *q;
297 if ((p = getenv ("MM_NOASK")) != NULL && *p)
299 if (mutt_strcmp (p, "1") == 0)
302 strfcpy (tmp, p, sizeof (tmp));
305 while ((p = strtok (p, ",")) != NULL)
307 if ((q = strrchr (p, '/')) != NULL)
311 if (ascii_strncasecmp (buf, p, q-p) == 0)
316 if (ascii_strcasecmp (buf, p) == 0)
322 lng = mutt_strlen (p);
323 if (buf[lng] == '/' && mutt_strncasecmp (buf, p, lng) == 0)
334 void mutt_check_lookup_list (BODY *b, char *type, int len)
336 LIST *t = MimeLookupList;
339 for (; t; t = t->next) {
340 i = mutt_strlen (t->data) - 1;
341 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
342 ascii_strncasecmp (type, t->data, i) == 0) ||
343 ascii_strcasecmp (type, t->data) == 0) {
347 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
348 snprintf (type, len, "%s/%s",
349 n == TYPEAUDIO ? "audio" :
350 n == TYPEAPPLICATION ? "application" :
351 n == TYPEIMAGE ? "image" :
352 n == TYPEMESSAGE ? "message" :
353 n == TYPEMODEL ? "model" :
354 n == TYPEMULTIPART ? "multipart" :
355 n == TYPETEXT ? "text" :
356 n == TYPEVIDEO ? "video" : "other",
358 dprint(1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n",
369 int mutt_is_autoview (BODY *b, const char *type)
371 LIST *t = AutoViewList;
372 char _type[SHORT_STRING];
376 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
378 strncpy (_type, type, sizeof(_type));
380 mutt_check_lookup_list (b, _type, sizeof(_type));
383 if (mutt_needs_mailcap (b))
385 if (option (OPTIMPLICITAUTOVIEW))
388 if (is_mmnoask (type))
392 for (; t; t = t->next) {
393 i = mutt_strlen (t->data) - 1;
394 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
395 ascii_strncasecmp (type, t->data, i) == 0) ||
396 ascii_strcasecmp (type, t->data) == 0)
403 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
404 int mutt_view_attachment (FILE *fp, BODY *a, int flag, HEADER *hdr,
405 ATTACHPTR **idx, short idxlen)
407 char tempfile[_POSIX_PATH_MAX] = "";
408 char pagerfile[_POSIX_PATH_MAX] = "";
414 char command[STRING];
415 char descrip[STRING];
417 rfc1524_entry *entry = NULL;
419 int unlink_tempfile = 0;
421 is_message = mutt_is_message_type(a->type, a->subtype);
422 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
423 !crypt_valid_passphrase(a->hdr->security))
425 use_mailcap = (flag == M_MAILCAP ||
426 (flag == M_REGULAR && mutt_needs_mailcap (a)));
427 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
431 entry = rfc1524_new_entry ();
432 if (!rfc1524_mailcap_lookup (a, type, entry, 0))
434 if (flag == M_REGULAR)
436 /* fallback to view as text */
437 rfc1524_free_entry (&entry);
438 mutt_error _("No matching mailcap entry found. Viewing as text.");
451 mutt_error _("MIME type not defined. Cannot view attachment.");
454 strfcpy (command, entry->command, sizeof (command));
458 fname = safe_strdup (a->filename);
459 mutt_sanitize_filename (fname, 1);
464 if (rfc1524_expand_filename (entry->nametemplate, fname,
465 tempfile, sizeof (tempfile)))
467 if (fp == NULL && mutt_strcmp(tempfile, a->filename))
469 /* send case: the file is already there */
470 if (safe_symlink (a->filename, tempfile) == -1)
472 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) == M_YES)
473 strfcpy (tempfile, a->filename, sizeof (tempfile));
481 else if (fp == NULL) /* send case */
482 strfcpy (tempfile, a->filename, sizeof (tempfile));
486 /* recv case: we need to save the attachment to a file */
488 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
492 use_pipe = rfc1524_expand_command (a, tempfile, type,
493 command, sizeof (command));
494 use_pager = entry->copiousoutput;
499 if (fp && !use_mailcap && a->filename)
502 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
503 mutt_adv_mktemp (pagerfile, sizeof(pagerfile));
506 mutt_mktemp (pagerfile);
512 int tempfd = -1, pagerfd = -1;
517 if (use_pager || use_pipe)
519 if (use_pager && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY)) == -1))
521 mutt_perror ("open");
524 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1))
528 mutt_perror ("open");
532 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
533 use_pipe ? tempfd : -1, use_pager ? pagerfd : -1, -1)) == -1)
541 mutt_error _("Cannot create filter");
548 snprintf (descrip, sizeof (descrip),
549 "---Command: %-20.20s Description: %s",
550 command, a->description);
552 snprintf (descrip, sizeof (descrip),
553 "---Command: %-30.30s Attachment: %s", command, type);
556 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
557 option (OPTWAITKEY))) && !use_pager)
558 mutt_any_key_to_continue (NULL);
566 /* interactive command */
567 if (mutt_system (command) ||
568 (entry->needsterminal && option (OPTWAITKEY)))
569 mutt_any_key_to_continue (NULL);
574 /* Don't use mailcap; the attachment is viewed in the pager */
576 if (flag == M_AS_TEXT)
578 /* just let me see the raw data */
579 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
584 /* Use built-in handler */
585 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
586 * message in case of error */
587 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0))
589 unset_option (OPTVIEWATTACH);
592 unset_option (OPTVIEWATTACH);
596 strfcpy (descrip, a->description, sizeof (descrip));
597 else if (a->filename)
598 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
601 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
604 /* We only reach this point if there have been no errors */
610 memset (&info, 0, sizeof (info));
615 info.idxlen = idxlen;
618 rc = mutt_do_pager (descrip, pagerfile,
619 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0), &info);
628 rfc1524_free_entry (&entry);
629 if (fp && tempfile[0])
630 mutt_unlink (tempfile);
631 else if (unlink_tempfile)
635 mutt_unlink (pagerfile);
640 /* returns 1 on success, 0 on error */
641 int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile)
647 if (outfile && *outfile)
648 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0)
650 mutt_perror ("open");
662 memset (&s, 0, sizeof (STATE));
664 if (outfile && *outfile)
665 thepid = mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
667 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
671 mutt_perror _("Can't create filter");
676 mutt_decode_attachment (b, &s);
677 safe_fclose (&s.fpout);
685 if ((ifp = fopen (b->filename, "r")) == NULL)
687 mutt_perror ("fopen");
688 if (outfile && *outfile)
696 if (outfile && *outfile)
697 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
699 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
703 mutt_perror _("Can't create filter");
708 mutt_copy_stream (ifp, ofp);
717 if (outfile && *outfile)
721 * check for error exit from child process
723 if (mutt_wait_filter (thepid) != 0)
726 if (rv == 0 || option (OPTWAITKEY))
727 mutt_any_key_to_continue (NULL);
732 mutt_save_attachment_open (char *path, int flags)
734 if (flags == M_SAVE_APPEND)
735 return fopen (path, "a");
736 /* be sure not to change the following fopen to safe_fopen
737 * as safe_fopen returns w/ an error if path exists
739 if (flags == M_SAVE_OVERWRITE)
740 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
742 return safe_fopen (path, "w");
745 /* returns 0 on success, -1 on error */
746 int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr)
755 m->encoding != ENCBASE64 &&
756 m->encoding != ENCQUOTEDPRINTABLE &&
757 mutt_is_message_type(m->type, m->subtype))
759 /* message type attachments are written to mail folders. */
761 char buf[HUGE_STRING];
769 hn->msgno = hdr->msgno; /* required for MH/maildir */
772 fseek (fp, m->offset, 0);
773 if (fgets (buf, sizeof (buf), fp) == NULL)
775 if (mx_open_mailbox(path, M_APPEND | M_QUIET, &ctx) == NULL)
777 if ((msg = mx_open_new_message (&ctx, hn, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
779 mx_close_mailbox(&ctx, NULL);
782 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
784 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
785 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
786 && mx_commit_message (msg, &ctx) == 0)
791 mx_close_message (&msg);
792 mx_close_mailbox (&ctx, NULL);
797 /* In recv mode, extract from folder and decode */
801 memset (&s, 0, sizeof (s));
802 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL)
804 mutt_perror ("fopen");
807 fseek ((s.fpin = fp), m->offset, 0);
808 mutt_decode_attachment (m, &s);
810 if (fclose (s.fpout) != 0)
812 mutt_perror ("fclose");
819 /* In send mode, just copy file */
823 if ((ofp = fopen (m->filename, "r")) == NULL)
825 mutt_perror ("fopen");
829 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL)
831 mutt_perror ("fopen");
836 if (mutt_copy_stream (ofp, nfp) == -1)
838 mutt_error _("Write fault!");
850 /* returns 0 on success, -1 on error */
851 int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
852 int displaying, int flags)
855 unsigned int saved_encoding = 0;
856 BODY *saved_parts = NULL;
857 HEADER *saved_hdr = NULL;
859 memset (&s, 0, sizeof (s));
860 s.flags = displaying;
862 if (flags == M_SAVE_APPEND)
863 s.fpout = fopen (path, "a");
864 else if (flags == M_SAVE_OVERWRITE)
865 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
867 s.fpout = safe_fopen (path, "w");
871 mutt_perror ("fopen");
877 /* When called from the compose menu, the attachment isn't parsed,
878 * so we need to do it here. */
881 if (stat (m->filename, &st) == -1)
883 mutt_perror ("stat");
888 if ((s.fpin = fopen (m->filename, "r")) == NULL)
890 mutt_perror ("fopen");
894 saved_encoding = m->encoding;
895 if (!is_multipart (m))
896 m->encoding = ENC8BIT;
898 m->length = st.st_size;
900 saved_parts = m->parts;
902 mutt_parse_part (s.fpin, m);
904 if (m->noconv || is_multipart (m))
905 s.flags |= M_CHARCONV;
910 s.flags |= M_CHARCONV;
913 mutt_body_handler (m, &s);
919 m->encoding = saved_encoding;
922 mutt_free_header (&m->hdr);
923 m->parts = saved_parts;
932 /* Ok, the difference between send and receive:
933 * recv: BODY->filename is a suggested name, and Context|HEADER points
934 * to the attachment in mailbox which is encooded
935 * send: BODY->filename points to the un-encoded file which contains the
939 int mutt_print_attachment (FILE *fp, BODY *a)
941 char newfile[_POSIX_PATH_MAX] = "";
945 short unlink_newfile = 0;
947 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
949 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT))
951 char command[_POSIX_PATH_MAX+STRING];
952 rfc1524_entry *entry;
955 dprint (2, (debugfile, "Using mailcap...\n"));
957 entry = rfc1524_new_entry ();
958 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
959 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
960 newfile, sizeof (newfile)))
964 if (safe_symlink(a->filename, newfile) == -1)
966 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
968 rfc1524_free_entry (&entry);
971 strfcpy (newfile, a->filename, sizeof (newfile));
978 /* in recv mode, save file to newfile first */
980 mutt_save_attachment (fp, a, newfile, 0, NULL);
982 strfcpy (command, entry->printcommand, sizeof (command));
983 piped = rfc1524_expand_command (a, newfile, type, command, sizeof (command));
987 /* interactive program */
990 if ((ifp = fopen (newfile, "r")) == NULL)
992 mutt_perror ("fopen");
993 rfc1524_free_entry (&entry);
997 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0)
999 mutt_perror _("Can't create filter");
1000 rfc1524_free_entry (&entry);
1004 mutt_copy_stream (ifp, fpout);
1005 safe_fclose (&fpout);
1007 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
1008 mutt_any_key_to_continue (NULL);
1012 if (mutt_system (command) || option (OPTWAITKEY))
1013 mutt_any_key_to_continue (NULL);
1017 mutt_unlink (newfile);
1018 else if (unlink_newfile)
1021 rfc1524_free_entry (&entry);
1025 if (!ascii_strcasecmp ("text/plain", type) ||
1026 !ascii_strcasecmp ("application/postscript", type))
1028 return (mutt_pipe_attachment (fp, a, NONULL(PrintCmd), NULL));
1030 else if (mutt_can_decode (a))
1032 /* decode and print */
1039 mutt_mktemp (newfile);
1040 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0)
1043 dprint (2, (debugfile, "successfully decoded %s type attachment to %s\n",
1046 if ((ifp = fopen (newfile, "r")) == NULL)
1048 mutt_perror ("fopen");
1052 dprint (2, (debugfile, "successfully opened %s read-only\n", newfile));
1055 if ((thepid = mutt_create_filter (NONULL(PrintCmd), &fpout, NULL, NULL)) < 0)
1057 mutt_perror _("Can't create filter");
1061 dprint (2, (debugfile, "Filter created.\n"));
1063 mutt_copy_stream (ifp, fpout);
1065 safe_fclose (&fpout);
1068 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1069 mutt_any_key_to_continue (NULL);
1074 safe_fclose (&fpout);
1075 mutt_unlink (newfile);
1080 mutt_error _("I don't know how to print that!");