2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
16 #include "mutt_menu.h"
17 #include "mutt_curses.h"
24 #include "mutt_crypt.h"
39 int mutt_get_tmp_attachment (BODY * a)
42 char tempfile[_POSIX_PATH_MAX];
43 rfc1524_entry *entry = rfc1524_new_entry ();
44 FILE *fpin = NULL, *fpout = NULL;
50 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
51 rfc1524_mailcap_lookup (a, type, entry, 0);
52 rfc1524_expand_filename (entry->nametemplate, a->filename,
53 tempfile, sizeof (tempfile));
55 rfc1524_free_entry (&entry);
57 if (stat (a->filename, &st) == -1)
60 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
61 mutt_copy_stream (fpin, fpout);
62 str_replace (&a->filename, tempfile);
65 if (a->stamp >= st.st_mtime)
66 mutt_stamp_attachment (a);
69 mutt_perror(fpin ? tempfile : a->filename);
76 return a->unlink ? 0 : -1;
80 /* return 1 if require full screen redraw, 0 otherwise */
81 int mutt_compose_attachment (BODY * a)
85 char newfile[_POSIX_PATH_MAX] = "";
86 rfc1524_entry *entry = rfc1524_new_entry ();
87 short unlink_newfile = 0;
90 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
91 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
92 if (entry->composecommand || entry->composetypecommand) {
94 if (entry->composetypecommand)
95 strfcpy (command, entry->composetypecommand, sizeof (command));
97 strfcpy (command, entry->composecommand, sizeof (command));
98 if (rfc1524_expand_filename (entry->nametemplate,
99 a->filename, newfile, sizeof (newfile))) {
100 dprint (1, (debugfile, "oldfile: %s\t newfile: %s\n",
101 a->filename, newfile));
102 if (safe_symlink (a->filename, newfile) == -1) {
103 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
111 strfcpy (newfile, a->filename, sizeof (newfile));
113 if (rfc1524_expand_command (a, newfile, type,
114 command, sizeof (command))) {
115 /* For now, editing requires a file, no piping */
116 mutt_error _("Mailcap compose entry requires %%s");
122 if ((r = mutt_system (command)) == -1)
123 mutt_error (_("Error running \"%s\"!"), command);
125 if (r != -1 && entry->composetypecommand) {
128 char tempfile[_POSIX_PATH_MAX];
130 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
131 mutt_perror (_("Failure to open file to parse headers."));
136 b = mutt_read_mime_header (fp, 0);
139 mutt_free_parameter (&a->parameter);
140 a->parameter = b->parameter;
143 if (b->description) {
144 FREE (&a->description);
145 a->description = b->description;
146 b->description = NULL;
149 FREE (&a->form_name);
150 a->form_name = b->form_name;
154 /* Remove headers by copying out data to another file, then
155 * copying the file back */
156 fseek (fp, b->offset, 0);
157 mutt_mktemp (tempfile);
158 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
159 mutt_perror (_("Failure to open file to strip headers."));
163 mutt_copy_stream (fp, tfp);
166 mutt_unlink (a->filename);
167 if (mutt_rename_file (tempfile, a->filename) != 0) {
168 mutt_perror (_("Failure to rename file."));
180 rfc1524_free_entry (&entry);
181 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
193 rfc1524_free_entry (&entry);
198 * Currently, this only works for send mode, as it assumes that the
199 * BODY->filename actually contains the information. I'm not sure
200 * we want to deal with editing attachments we've already received,
201 * so this should be ok.
203 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
206 int mutt_edit_attachment (BODY * a)
209 char command[STRING];
210 char newfile[_POSIX_PATH_MAX] = "";
211 rfc1524_entry *entry = rfc1524_new_entry ();
212 short unlink_newfile = 0;
215 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
216 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
217 if (entry->editcommand) {
219 strfcpy (command, entry->editcommand, sizeof (command));
220 if (rfc1524_expand_filename (entry->nametemplate,
221 a->filename, newfile, sizeof (newfile))) {
222 dprint (1, (debugfile, "oldfile: %s\t newfile: %s\n",
223 a->filename, newfile));
224 if (safe_symlink (a->filename, newfile) == -1) {
225 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
233 strfcpy (newfile, a->filename, sizeof (newfile));
235 if (rfc1524_expand_command (a, newfile, type,
236 command, sizeof (command))) {
237 /* For now, editing requires a file, no piping */
238 mutt_error _("Mailcap Edit entry requires %%s");
242 if (mutt_system (command) == -1)
243 mutt_error (_("Error running \"%s\"!"), command);
247 else if (a->type == TYPETEXT) {
248 /* On text, default to editor */
249 mutt_edit_file (NONULL (Editor), newfile);
252 rfc1524_free_entry (&entry);
253 mutt_error (_("No mailcap edit entry for %s"), type);
264 rfc1524_free_entry (&entry);
269 /* for compatibility with metamail */
270 static int is_mmnoask (const char *buf)
272 char tmp[LONG_STRING], *p, *q;
275 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
276 if (safe_strcmp (p, "1") == 0)
279 strfcpy (tmp, p, sizeof (tmp));
282 while ((p = strtok (p, ",")) != NULL) {
283 if ((q = strrchr (p, '/')) != NULL) {
284 if (*(q + 1) == '*') {
285 if (ascii_strncasecmp (buf, p, q - p) == 0)
289 if (ascii_strcasecmp (buf, p) == 0)
294 lng = safe_strlen (p);
295 if (buf[lng] == '/' && safe_strncasecmp (buf, p, lng) == 0)
306 void mutt_check_lookup_list (BODY * b, char *type, int len)
308 LIST *t = MimeLookupList;
311 for (; t; t = t->next) {
312 i = safe_strlen (t->data) - 1;
313 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
314 ascii_strncasecmp (type, t->data, i) == 0) ||
315 ascii_strcasecmp (type, t->data) == 0) {
320 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
321 snprintf (type, len, "%s/%s",
322 n == TYPEAUDIO ? "audio" :
323 n == TYPEAPPLICATION ? "application" :
324 n == TYPEIMAGE ? "image" :
325 n == TYPEMESSAGE ? "message" :
326 n == TYPEMODEL ? "model" :
327 n == TYPEMULTIPART ? "multipart" :
328 n == TYPETEXT ? "text" :
329 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
330 dprint (1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n",
341 int mutt_is_autoview (BODY * b, const char *type)
343 LIST *t = AutoViewList;
344 char _type[SHORT_STRING];
348 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
350 strncpy (_type, type, sizeof (_type));
352 mutt_check_lookup_list (b, _type, sizeof (_type));
355 if (mutt_needs_mailcap (b)) {
356 if (option (OPTIMPLICITAUTOVIEW))
359 if (is_mmnoask (type))
363 for (; t; t = t->next) {
364 i = safe_strlen (t->data) - 1;
365 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
366 ascii_strncasecmp (type, t->data, i) == 0) ||
367 ascii_strcasecmp (type, t->data) == 0)
374 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
375 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
376 ATTACHPTR ** idx, short idxlen)
378 char tempfile[_POSIX_PATH_MAX] = "";
379 char pagerfile[_POSIX_PATH_MAX] = "";
385 char command[STRING];
386 char descrip[STRING];
388 rfc1524_entry *entry = NULL;
390 int unlink_tempfile = 0;
392 is_message = mutt_is_message_type (a->type, a->subtype);
393 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
394 !crypt_valid_passphrase (a->hdr->security))
396 use_mailcap = (flag == M_MAILCAP ||
397 (flag == M_REGULAR && mutt_needs_mailcap (a)));
398 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
401 entry = rfc1524_new_entry ();
402 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
403 if (flag == M_REGULAR) {
404 /* fallback to view as text */
405 rfc1524_free_entry (&entry);
406 mutt_error _("No matching mailcap entry found. Viewing as text.");
417 if (!entry->command) {
418 mutt_error _("MIME type not defined. Cannot view attachment.");
422 strfcpy (command, entry->command, sizeof (command));
425 fname = safe_strdup (a->filename);
426 mutt_sanitize_filename (fname, 1);
431 if (rfc1524_expand_filename (entry->nametemplate, fname,
432 tempfile, sizeof (tempfile))) {
433 if (fp == NULL && safe_strcmp (tempfile, a->filename)) {
434 /* send case: the file is already there */
435 if (safe_symlink (a->filename, tempfile) == -1) {
436 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
438 strfcpy (tempfile, a->filename, sizeof (tempfile));
446 else if (fp == NULL) /* send case */
447 strfcpy (tempfile, a->filename, sizeof (tempfile));
450 /* recv case: we need to save the attachment to a file */
452 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
456 use_pipe = rfc1524_expand_command (a, tempfile, type,
457 command, sizeof (command));
458 use_pager = entry->copiousoutput;
462 if (fp && !use_mailcap && a->filename) {
464 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
465 mutt_adv_mktemp (pagerfile, sizeof (pagerfile));
468 mutt_mktemp (pagerfile);
473 int tempfd = -1, pagerfd = -1;
478 if (use_pager || use_pipe) {
480 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
482 mutt_perror ("open");
485 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
488 mutt_perror ("open");
492 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
493 use_pipe ? tempfd : -1,
494 use_pager ? pagerfd : -1,
502 mutt_error _("Cannot create filter");
509 snprintf (descrip, sizeof (descrip),
510 "---Command: %-20.20s Description: %s",
511 command, a->description);
513 snprintf (descrip, sizeof (descrip),
514 "---Command: %-30.30s Attachment: %s", command, type);
517 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
518 option (OPTWAITKEY))) && !use_pager)
519 mutt_any_key_to_continue (NULL);
526 /* interactive command */
527 if (mutt_system (command) ||
528 (entry->needsterminal && option (OPTWAITKEY)))
529 mutt_any_key_to_continue (NULL);
533 /* Don't use mailcap; the attachment is viewed in the pager */
535 if (flag == M_AS_TEXT) {
536 /* just let me see the raw data */
537 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
541 /* Use built-in handler */
542 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
543 * message in case of error */
544 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
545 unset_option (OPTVIEWATTACH);
548 unset_option (OPTVIEWATTACH);
552 strfcpy (descrip, a->description, sizeof (descrip));
553 else if (a->filename)
554 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
557 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
560 /* We only reach this point if there have been no errors */
565 memset (&info, 0, sizeof (info));
570 info.idxlen = idxlen;
573 rc = mutt_do_pager (descrip, pagerfile,
574 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE :
584 rfc1524_free_entry (&entry);
585 if (fp && tempfile[0])
586 mutt_unlink (tempfile);
587 else if (unlink_tempfile)
591 mutt_unlink (pagerfile);
596 /* returns 1 on success, 0 on error */
597 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
604 if (outfile && *outfile)
605 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
606 mutt_perror ("open");
617 memset (&s, 0, sizeof (STATE));
619 if (outfile && *outfile)
621 mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
623 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
626 mutt_perror (_("Can't create filter"));
632 mutt_decode_attachment (b, &s);
633 safe_fclose (&s.fpout);
640 if ((ifp = fopen (b->filename, "r")) == NULL) {
641 mutt_perror ("fopen");
642 if (outfile && *outfile) {
649 if (outfile && *outfile)
650 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
652 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
655 mutt_perror (_("Can't create filter"));
661 mutt_copy_stream (ifp, ofp);
670 if (outfile && *outfile)
674 * check for error exit from child process
676 if (mutt_wait_filter (thepid) != 0)
679 if (rv == 0 || option (OPTWAITKEY))
680 mutt_any_key_to_continue (NULL);
684 static FILE *mutt_save_attachment_open (char *path, int flags)
686 if (flags == M_SAVE_APPEND)
687 return fopen (path, "a");
688 /* be sure not to change the following fopen to safe_fopen
689 * as safe_fopen returns w/ an error if path exists
691 if (flags == M_SAVE_OVERWRITE)
692 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
694 return safe_fopen (path, "w");
697 /* returns 0 on success, -1 on error */
698 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
707 m->encoding != ENCBASE64 &&
708 m->encoding != ENCQUOTEDPRINTABLE &&
709 mutt_is_message_type (m->type, m->subtype)) {
710 /* message type attachments are written to mail folders. */
712 char buf[HUGE_STRING];
720 hn->msgno = hdr->msgno; /* required for MH/maildir */
723 fseek (fp, m->offset, 0);
724 if (fgets (buf, sizeof (buf), fp) == NULL)
726 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
729 mx_open_new_message (&ctx, hn,
730 is_from (buf, NULL, 0,
731 NULL) ? 0 : M_ADD_FROM)) == NULL) {
732 mx_close_mailbox (&ctx, NULL);
735 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
737 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
738 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
739 && mx_commit_message (msg, &ctx) == 0)
744 mx_close_message (&msg);
745 mx_close_mailbox (&ctx, NULL);
749 /* In recv mode, extract from folder and decode */
753 memset (&s, 0, sizeof (s));
754 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
755 mutt_perror ("fopen");
758 fseek ((s.fpin = fp), m->offset, 0);
759 mutt_decode_attachment (m, &s);
761 if (fclose (s.fpout) != 0) {
762 mutt_perror ("fclose");
768 /* In send mode, just copy file */
772 if ((ofp = fopen (m->filename, "r")) == NULL) {
773 mutt_perror ("fopen");
777 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
778 mutt_perror ("fopen");
783 if (mutt_copy_stream (ofp, nfp) == -1) {
784 mutt_error _("Write fault!");
797 /* returns 0 on success, -1 on error */
798 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
799 int displaying, int flags)
802 unsigned int saved_encoding = 0;
803 BODY *saved_parts = NULL;
804 HEADER *saved_hdr = NULL;
806 memset (&s, 0, sizeof (s));
807 s.flags = displaying;
809 if (flags == M_SAVE_APPEND)
810 s.fpout = fopen (path, "a");
811 else if (flags == M_SAVE_OVERWRITE)
812 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
814 s.fpout = safe_fopen (path, "w");
816 if (s.fpout == NULL) {
817 mutt_perror ("fopen");
822 /* When called from the compose menu, the attachment isn't parsed,
823 * so we need to do it here. */
826 if (stat (m->filename, &st) == -1) {
827 mutt_perror ("stat");
832 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
833 mutt_perror ("fopen");
837 saved_encoding = m->encoding;
838 if (!is_multipart (m))
839 m->encoding = ENC8BIT;
841 m->length = st.st_size;
843 saved_parts = m->parts;
845 mutt_parse_part (s.fpin, m);
847 if (m->noconv || is_multipart (m))
848 s.flags |= M_CHARCONV;
852 s.flags |= M_CHARCONV;
855 mutt_body_handler (m, &s);
860 m->encoding = saved_encoding;
862 mutt_free_header (&m->hdr);
863 m->parts = saved_parts;
872 /* Ok, the difference between send and receive:
873 * recv: BODY->filename is a suggested name, and Context|HEADER points
874 * to the attachment in mailbox which is encooded
875 * send: BODY->filename points to the un-encoded file which contains the
879 int mutt_print_attachment (FILE * fp, BODY * a)
881 char newfile[_POSIX_PATH_MAX] = "";
885 short unlink_newfile = 0;
887 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
889 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
890 char command[_POSIX_PATH_MAX + STRING];
891 rfc1524_entry *entry;
894 dprint (2, (debugfile, "Using mailcap...\n"));
896 entry = rfc1524_new_entry ();
897 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
898 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
899 newfile, sizeof (newfile))) {
901 if (safe_symlink (a->filename, newfile) == -1) {
902 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
904 rfc1524_free_entry (&entry);
907 strfcpy (newfile, a->filename, sizeof (newfile));
914 /* in recv mode, save file to newfile first */
916 mutt_save_attachment (fp, a, newfile, 0, NULL);
918 strfcpy (command, entry->printcommand, sizeof (command));
920 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
924 /* interactive program */
926 if ((ifp = fopen (newfile, "r")) == NULL) {
927 mutt_perror ("fopen");
928 rfc1524_free_entry (&entry);
932 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
933 mutt_perror (_("Can't create filter"));
935 rfc1524_free_entry (&entry);
939 mutt_copy_stream (ifp, fpout);
940 safe_fclose (&fpout);
942 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
943 mutt_any_key_to_continue (NULL);
946 if (mutt_system (command) || option (OPTWAITKEY))
947 mutt_any_key_to_continue (NULL);
951 mutt_unlink (newfile);
952 else if (unlink_newfile)
955 rfc1524_free_entry (&entry);
959 if (!ascii_strcasecmp ("text/plain", type) ||
960 !ascii_strcasecmp ("application/postscript", type)) {
961 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
963 else if (mutt_can_decode (a)) {
964 /* decode and print */
971 mutt_mktemp (newfile);
972 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
975 (debugfile, "successfully decoded %s type attachment to %s\n",
978 if ((ifp = fopen (newfile, "r")) == NULL) {
979 mutt_perror ("fopen");
983 dprint (2, (debugfile, "successfully opened %s read-only\n", newfile));
987 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
988 mutt_perror (_("Can't create filter"));
993 dprint (2, (debugfile, "Filter created.\n"));
995 mutt_copy_stream (ifp, fpout);
997 safe_fclose (&fpout);
1000 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1001 mutt_any_key_to_continue (NULL);
1006 safe_fclose (&fpout);
1007 mutt_unlink (newfile);
1011 mutt_error _("I don't know how to print that!");