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"
25 #include "mutt_crypt.h"
40 int mutt_get_tmp_attachment (BODY * a)
43 char tempfile[_POSIX_PATH_MAX];
44 rfc1524_entry *entry = rfc1524_new_entry ();
45 FILE *fpin = NULL, *fpout = NULL;
51 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
52 rfc1524_mailcap_lookup (a, type, entry, 0);
53 rfc1524_expand_filename (entry->nametemplate, a->filename,
54 tempfile, sizeof (tempfile));
56 rfc1524_free_entry (&entry);
58 if (stat (a->filename, &st) == -1)
61 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
62 mutt_copy_stream (fpin, fpout);
63 mutt_str_replace (&a->filename, tempfile);
66 if (a->stamp >= st.st_mtime)
67 mutt_stamp_attachment (a);
70 mutt_perror (fpin ? tempfile : a->filename);
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)) {
93 if (entry->composecommand || entry->composetypecommand) {
95 if (entry->composetypecommand)
96 strfcpy (command, entry->composetypecommand, sizeof (command));
98 strfcpy (command, entry->composecommand, sizeof (command));
99 if (rfc1524_expand_filename (entry->nametemplate,
100 a->filename, newfile, sizeof (newfile))) {
101 dprint (1, (debugfile, "oldfile: %s\t newfile: %s\n",
102 a->filename, newfile));
103 if (safe_symlink (a->filename, newfile) == -1) {
104 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
112 strfcpy (newfile, a->filename, sizeof (newfile));
114 if (rfc1524_expand_command (a, newfile, type,
115 command, sizeof (command))) {
116 /* For now, editing requires a file, no piping */
117 mutt_error _("Mailcap compose entry requires %%s");
123 if ((r = mutt_system (command)) == -1)
124 mutt_error (_("Error running \"%s\"!"), command);
126 if (r != -1 && entry->composetypecommand) {
129 char tempfile[_POSIX_PATH_MAX];
131 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
132 mutt_perror _("Failure to open file to parse headers.");
137 b = mutt_read_mime_header (fp, 0);
140 mutt_free_parameter (&a->parameter);
141 a->parameter = b->parameter;
144 if (b->description) {
145 FREE (&a->description);
146 a->description = b->description;
147 b->description = NULL;
150 FREE (&a->form_name);
151 a->form_name = b->form_name;
155 /* Remove headers by copying out data to another file, then
156 * copying the file back */
157 fseek (fp, b->offset, 0);
158 mutt_mktemp (tempfile);
159 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
160 mutt_perror _("Failure to open file to strip headers.");
164 mutt_copy_stream (fp, tfp);
167 mutt_unlink (a->filename);
168 if (mutt_rename_file (tempfile, a->filename) != 0) {
169 mutt_perror _("Failure to rename file.");
181 rfc1524_free_entry (&entry);
182 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
194 rfc1524_free_entry (&entry);
199 * Currently, this only works for send mode, as it assumes that the
200 * BODY->filename actually contains the information. I'm not sure
201 * we want to deal with editing attachments we've already received,
202 * so this should be ok.
204 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
207 int mutt_edit_attachment (BODY * a)
210 char command[STRING];
211 char newfile[_POSIX_PATH_MAX] = "";
212 rfc1524_entry *entry = rfc1524_new_entry ();
213 short unlink_newfile = 0;
216 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
217 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
218 if (entry->editcommand) {
220 strfcpy (command, entry->editcommand, sizeof (command));
221 if (rfc1524_expand_filename (entry->nametemplate,
222 a->filename, newfile, sizeof (newfile))) {
223 dprint (1, (debugfile, "oldfile: %s\t newfile: %s\n",
224 a->filename, newfile));
225 if (safe_symlink (a->filename, newfile) == -1) {
226 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
234 strfcpy (newfile, a->filename, sizeof (newfile));
236 if (rfc1524_expand_command (a, newfile, type,
237 command, sizeof (command))) {
238 /* For now, editing requires a file, no piping */
239 mutt_error _("Mailcap Edit entry requires %%s");
243 if (mutt_system (command) == -1)
244 mutt_error (_("Error running \"%s\"!"), command);
248 else if (a->type == TYPETEXT) {
249 /* On text, default to editor */
250 mutt_edit_file (NONULL (Editor), newfile);
253 rfc1524_free_entry (&entry);
254 mutt_error (_("No mailcap edit entry for %s"), type);
265 rfc1524_free_entry (&entry);
270 /* for compatibility with metamail */
271 static int is_mmnoask (const char *buf)
273 char tmp[LONG_STRING], *p, *q;
276 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
277 if (mutt_strcmp (p, "1") == 0)
280 strfcpy (tmp, p, sizeof (tmp));
283 while ((p = strtok (p, ",")) != NULL) {
284 if ((q = strrchr (p, '/')) != NULL) {
285 if (*(q + 1) == '*') {
286 if (ascii_strncasecmp (buf, p, q - p) == 0)
290 if (ascii_strcasecmp (buf, p) == 0)
295 lng = mutt_strlen (p);
296 if (buf[lng] == '/' && mutt_strncasecmp (buf, p, lng) == 0)
307 void mutt_check_lookup_list (BODY * b, char *type, int len)
309 LIST *t = MimeLookupList;
312 for (; t; t = t->next) {
313 i = mutt_strlen (t->data) - 1;
314 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
315 ascii_strncasecmp (type, t->data, i) == 0) ||
316 ascii_strcasecmp (type, t->data) == 0) {
321 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
322 snprintf (type, len, "%s/%s",
323 n == TYPEAUDIO ? "audio" :
324 n == TYPEAPPLICATION ? "application" :
325 n == TYPEIMAGE ? "image" :
326 n == TYPEMESSAGE ? "message" :
327 n == TYPEMODEL ? "model" :
328 n == TYPEMULTIPART ? "multipart" :
329 n == TYPETEXT ? "text" :
330 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
331 dprint (1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n",
342 int mutt_is_autoview (BODY * b, const char *type)
344 LIST *t = AutoViewList;
345 char _type[SHORT_STRING];
349 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
351 strncpy (_type, type, sizeof (_type));
353 mutt_check_lookup_list (b, _type, sizeof (_type));
356 if (mutt_needs_mailcap (b)) {
357 if (option (OPTIMPLICITAUTOVIEW))
360 if (is_mmnoask (type))
364 for (; t; t = t->next) {
365 i = mutt_strlen (t->data) - 1;
366 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
367 ascii_strncasecmp (type, t->data, i) == 0) ||
368 ascii_strcasecmp (type, t->data) == 0)
375 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
376 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
377 ATTACHPTR ** idx, short idxlen)
379 char tempfile[_POSIX_PATH_MAX] = "";
380 char pagerfile[_POSIX_PATH_MAX] = "";
386 char command[STRING];
387 char descrip[STRING];
389 rfc1524_entry *entry = NULL;
391 int unlink_tempfile = 0;
393 is_message = mutt_is_message_type (a->type, a->subtype);
394 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
395 !crypt_valid_passphrase (a->hdr->security))
397 use_mailcap = (flag == M_MAILCAP ||
398 (flag == M_REGULAR && mutt_needs_mailcap (a)));
399 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
402 entry = rfc1524_new_entry ();
403 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
404 if (flag == M_REGULAR) {
405 /* fallback to view as text */
406 rfc1524_free_entry (&entry);
407 mutt_error _("No matching mailcap entry found. Viewing as text.");
418 if (!entry->command) {
419 mutt_error _("MIME type not defined. Cannot view attachment.");
423 strfcpy (command, entry->command, sizeof (command));
426 fname = safe_strdup (a->filename);
427 mutt_sanitize_filename (fname, 1);
432 if (rfc1524_expand_filename (entry->nametemplate, fname,
433 tempfile, sizeof (tempfile))) {
434 if (fp == NULL && mutt_strcmp (tempfile, a->filename)) {
435 /* send case: the file is already there */
436 if (safe_symlink (a->filename, tempfile) == -1) {
437 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
439 strfcpy (tempfile, a->filename, sizeof (tempfile));
447 else if (fp == NULL) /* send case */
448 strfcpy (tempfile, a->filename, sizeof (tempfile));
451 /* recv case: we need to save the attachment to a file */
453 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
457 use_pipe = rfc1524_expand_command (a, tempfile, type,
458 command, sizeof (command));
459 use_pager = entry->copiousoutput;
463 if (fp && !use_mailcap && a->filename) {
465 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
466 mutt_adv_mktemp (pagerfile, sizeof (pagerfile));
469 mutt_mktemp (pagerfile);
474 int tempfd = -1, pagerfd = -1;
479 if (use_pager || use_pipe) {
481 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
483 mutt_perror ("open");
486 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
489 mutt_perror ("open");
493 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
494 use_pipe ? tempfd : -1,
495 use_pager ? pagerfd : -1,
503 mutt_error _("Cannot create filter");
510 snprintf (descrip, sizeof (descrip),
511 "---Command: %-20.20s Description: %s",
512 command, a->description);
514 snprintf (descrip, sizeof (descrip),
515 "---Command: %-30.30s Attachment: %s", command, type);
518 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
519 option (OPTWAITKEY))) && !use_pager)
520 mutt_any_key_to_continue (NULL);
527 /* interactive command */
528 if (mutt_system (command) ||
529 (entry->needsterminal && option (OPTWAITKEY)))
530 mutt_any_key_to_continue (NULL);
534 /* Don't use mailcap; the attachment is viewed in the pager */
536 if (flag == M_AS_TEXT) {
537 /* just let me see the raw data */
538 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
542 /* Use built-in handler */
543 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
544 * message in case of error */
545 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
546 unset_option (OPTVIEWATTACH);
549 unset_option (OPTVIEWATTACH);
553 strfcpy (descrip, a->description, sizeof (descrip));
554 else if (a->filename)
555 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
558 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
561 /* We only reach this point if there have been no errors */
566 memset (&info, 0, sizeof (info));
571 info.idxlen = idxlen;
574 rc = mutt_do_pager (descrip, pagerfile,
575 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE :
585 rfc1524_free_entry (&entry);
586 if (fp && tempfile[0])
587 mutt_unlink (tempfile);
588 else if (unlink_tempfile)
592 mutt_unlink (pagerfile);
597 /* returns 1 on success, 0 on error */
598 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
605 if (outfile && *outfile)
606 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
607 mutt_perror ("open");
618 memset (&s, 0, sizeof (STATE));
620 if (outfile && *outfile)
622 mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
624 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
627 mutt_perror _("Can't create filter");
633 mutt_decode_attachment (b, &s);
634 safe_fclose (&s.fpout);
641 if ((ifp = fopen (b->filename, "r")) == NULL) {
642 mutt_perror ("fopen");
643 if (outfile && *outfile) {
650 if (outfile && *outfile)
651 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
653 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
656 mutt_perror _("Can't create filter");
662 mutt_copy_stream (ifp, ofp);
671 if (outfile && *outfile)
675 * check for error exit from child process
677 if (mutt_wait_filter (thepid) != 0)
680 if (rv == 0 || option (OPTWAITKEY))
681 mutt_any_key_to_continue (NULL);
685 static FILE *mutt_save_attachment_open (char *path, int flags)
687 if (flags == M_SAVE_APPEND)
688 return fopen (path, "a");
689 /* be sure not to change the following fopen to safe_fopen
690 * as safe_fopen returns w/ an error if path exists
692 if (flags == M_SAVE_OVERWRITE)
693 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
695 return safe_fopen (path, "w");
698 /* returns 0 on success, -1 on error */
699 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
708 m->encoding != ENCBASE64 &&
709 m->encoding != ENCQUOTEDPRINTABLE &&
710 mutt_is_message_type (m->type, m->subtype)) {
711 /* message type attachments are written to mail folders. */
713 char buf[HUGE_STRING];
721 hn->msgno = hdr->msgno; /* required for MH/maildir */
724 fseek (fp, m->offset, 0);
725 if (fgets (buf, sizeof (buf), fp) == NULL)
727 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
730 mx_open_new_message (&ctx, hn,
731 is_from (buf, NULL, 0,
732 NULL) ? 0 : M_ADD_FROM)) == NULL) {
733 mx_close_mailbox (&ctx, NULL);
736 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
738 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
739 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
740 && mx_commit_message (msg, &ctx) == 0)
745 mx_close_message (&msg);
746 mx_close_mailbox (&ctx, NULL);
750 /* In recv mode, extract from folder and decode */
754 memset (&s, 0, sizeof (s));
755 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
756 mutt_perror ("fopen");
759 fseek ((s.fpin = fp), m->offset, 0);
760 mutt_decode_attachment (m, &s);
762 if (fclose (s.fpout) != 0) {
763 mutt_perror ("fclose");
769 /* In send mode, just copy file */
773 if ((ofp = fopen (m->filename, "r")) == NULL) {
774 mutt_perror ("fopen");
778 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
779 mutt_perror ("fopen");
784 if (mutt_copy_stream (ofp, nfp) == -1) {
785 mutt_error _("Write fault!");
798 /* returns 0 on success, -1 on error */
799 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
800 int displaying, int flags)
803 unsigned int saved_encoding = 0;
804 BODY *saved_parts = NULL;
805 HEADER *saved_hdr = NULL;
807 memset (&s, 0, sizeof (s));
808 s.flags = displaying;
810 if (flags == M_SAVE_APPEND)
811 s.fpout = fopen (path, "a");
812 else if (flags == M_SAVE_OVERWRITE)
813 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
815 s.fpout = safe_fopen (path, "w");
817 if (s.fpout == NULL) {
818 mutt_perror ("fopen");
823 /* When called from the compose menu, the attachment isn't parsed,
824 * so we need to do it here. */
827 if (stat (m->filename, &st) == -1) {
828 mutt_perror ("stat");
833 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
834 mutt_perror ("fopen");
838 saved_encoding = m->encoding;
839 if (!is_multipart (m))
840 m->encoding = ENC8BIT;
842 m->length = st.st_size;
844 saved_parts = m->parts;
846 mutt_parse_part (s.fpin, m);
848 if (m->noconv || is_multipart (m))
849 s.flags |= M_CHARCONV;
853 s.flags |= M_CHARCONV;
856 mutt_body_handler (m, &s);
861 m->encoding = saved_encoding;
863 mutt_free_header (&m->hdr);
864 m->parts = saved_parts;
873 /* Ok, the difference between send and receive:
874 * recv: BODY->filename is a suggested name, and Context|HEADER points
875 * to the attachment in mailbox which is encooded
876 * send: BODY->filename points to the un-encoded file which contains the
880 int mutt_print_attachment (FILE * fp, BODY * a)
882 char newfile[_POSIX_PATH_MAX] = "";
886 short unlink_newfile = 0;
888 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
890 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
891 char command[_POSIX_PATH_MAX + STRING];
892 rfc1524_entry *entry;
895 dprint (2, (debugfile, "Using mailcap...\n"));
897 entry = rfc1524_new_entry ();
898 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
899 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
900 newfile, sizeof (newfile))) {
902 if (safe_symlink (a->filename, newfile) == -1) {
903 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
905 rfc1524_free_entry (&entry);
908 strfcpy (newfile, a->filename, sizeof (newfile));
915 /* in recv mode, save file to newfile first */
917 mutt_save_attachment (fp, a, newfile, 0, NULL);
919 strfcpy (command, entry->printcommand, sizeof (command));
921 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
925 /* interactive program */
927 if ((ifp = fopen (newfile, "r")) == NULL) {
928 mutt_perror ("fopen");
929 rfc1524_free_entry (&entry);
933 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
934 mutt_perror _("Can't create filter");
936 rfc1524_free_entry (&entry);
940 mutt_copy_stream (ifp, fpout);
941 safe_fclose (&fpout);
943 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
944 mutt_any_key_to_continue (NULL);
947 if (mutt_system (command) || option (OPTWAITKEY))
948 mutt_any_key_to_continue (NULL);
952 mutt_unlink (newfile);
953 else if (unlink_newfile)
956 rfc1524_free_entry (&entry);
960 if (!ascii_strcasecmp ("text/plain", type) ||
961 !ascii_strcasecmp ("application/postscript", type)) {
962 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
964 else if (mutt_can_decode (a)) {
965 /* decode and print */
972 mutt_mktemp (newfile);
973 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
976 (debugfile, "successfully decoded %s type attachment to %s\n",
979 if ((ifp = fopen (newfile, "r")) == NULL) {
980 mutt_perror ("fopen");
984 dprint (2, (debugfile, "successfully opened %s read-only\n", newfile));
988 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
989 mutt_perror _("Can't create filter");
994 dprint (2, (debugfile, "Filter created.\n"));
996 mutt_copy_stream (ifp, fpout);
998 safe_fclose (&fpout);
1001 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1002 mutt_any_key_to_continue (NULL);
1007 safe_fclose (&fpout);
1008 mutt_unlink (newfile);
1012 mutt_error _("I don't know how to print that!");