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"
36 int mutt_get_tmp_attachment (BODY * a)
39 char tempfile[_POSIX_PATH_MAX];
40 rfc1524_entry *entry = rfc1524_new_entry ();
41 FILE *fpin = NULL, *fpout = NULL;
47 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
48 rfc1524_mailcap_lookup (a, type, entry, 0);
49 rfc1524_expand_filename (entry->nametemplate, a->filename,
50 tempfile, sizeof (tempfile));
52 rfc1524_free_entry (&entry);
54 if (stat (a->filename, &st) == -1)
57 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
58 mutt_copy_stream (fpin, fpout);
59 mutt_str_replace (&a->filename, tempfile);
62 if (a->stamp >= st.st_mtime)
63 mutt_stamp_attachment (a);
66 mutt_perror (fpin ? tempfile : a->filename);
73 return a->unlink ? 0 : -1;
77 /* return 1 if require full screen redraw, 0 otherwise */
78 int mutt_compose_attachment (BODY * a)
82 char newfile[_POSIX_PATH_MAX] = "";
83 rfc1524_entry *entry = rfc1524_new_entry ();
84 short unlink_newfile = 0;
87 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
88 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
89 if (entry->composecommand || entry->composetypecommand) {
91 if (entry->composetypecommand)
92 strfcpy (command, entry->composetypecommand, sizeof (command));
94 strfcpy (command, entry->composecommand, sizeof (command));
95 if (rfc1524_expand_filename (entry->nametemplate,
96 a->filename, newfile, sizeof (newfile))) {
97 dprint (1, (debugfile, "oldfile: %s\t newfile: %s\n",
98 a->filename, newfile));
99 if (safe_symlink (a->filename, newfile) == -1) {
100 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
108 strfcpy (newfile, a->filename, sizeof (newfile));
110 if (rfc1524_expand_command (a, newfile, type,
111 command, sizeof (command))) {
112 /* For now, editing requires a file, no piping */
113 mutt_error _("Mailcap compose entry requires %%s");
119 if ((r = mutt_system (command)) == -1)
120 mutt_error (_("Error running \"%s\"!"), command);
122 if (r != -1 && entry->composetypecommand) {
125 char tempfile[_POSIX_PATH_MAX];
127 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
128 mutt_perror _("Failure to open file to parse headers.");
133 b = mutt_read_mime_header (fp, 0);
136 mutt_free_parameter (&a->parameter);
137 a->parameter = b->parameter;
140 if (b->description) {
141 FREE (&a->description);
142 a->description = b->description;
143 b->description = NULL;
146 FREE (&a->form_name);
147 a->form_name = b->form_name;
151 /* Remove headers by copying out data to another file, then
152 * copying the file back */
153 fseek (fp, b->offset, 0);
154 mutt_mktemp (tempfile);
155 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
156 mutt_perror _("Failure to open file to strip headers.");
160 mutt_copy_stream (fp, tfp);
163 mutt_unlink (a->filename);
164 if (mutt_rename_file (tempfile, a->filename) != 0) {
165 mutt_perror _("Failure to rename file.");
177 rfc1524_free_entry (&entry);
178 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
190 rfc1524_free_entry (&entry);
195 * Currently, this only works for send mode, as it assumes that the
196 * BODY->filename actually contains the information. I'm not sure
197 * we want to deal with editing attachments we've already received,
198 * so this should be ok.
200 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
203 int mutt_edit_attachment (BODY * a)
206 char command[STRING];
207 char newfile[_POSIX_PATH_MAX] = "";
208 rfc1524_entry *entry = rfc1524_new_entry ();
209 short unlink_newfile = 0;
212 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
213 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
214 if (entry->editcommand) {
216 strfcpy (command, entry->editcommand, sizeof (command));
217 if (rfc1524_expand_filename (entry->nametemplate,
218 a->filename, newfile, sizeof (newfile))) {
219 dprint (1, (debugfile, "oldfile: %s\t newfile: %s\n",
220 a->filename, newfile));
221 if (safe_symlink (a->filename, newfile) == -1) {
222 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
230 strfcpy (newfile, a->filename, sizeof (newfile));
232 if (rfc1524_expand_command (a, newfile, type,
233 command, sizeof (command))) {
234 /* For now, editing requires a file, no piping */
235 mutt_error _("Mailcap Edit entry requires %%s");
239 if (mutt_system (command) == -1)
240 mutt_error (_("Error running \"%s\"!"), command);
244 else if (a->type == TYPETEXT) {
245 /* On text, default to editor */
246 mutt_edit_file (NONULL (Editor), newfile);
249 rfc1524_free_entry (&entry);
250 mutt_error (_("No mailcap edit entry for %s"), type);
261 rfc1524_free_entry (&entry);
266 /* for compatibility with metamail */
267 static int is_mmnoask (const char *buf)
269 char tmp[LONG_STRING], *p, *q;
272 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
273 if (mutt_strcmp (p, "1") == 0)
276 strfcpy (tmp, p, sizeof (tmp));
279 while ((p = strtok (p, ",")) != NULL) {
280 if ((q = strrchr (p, '/')) != NULL) {
281 if (*(q + 1) == '*') {
282 if (ascii_strncasecmp (buf, p, q - p) == 0)
286 if (ascii_strcasecmp (buf, p) == 0)
291 lng = mutt_strlen (p);
292 if (buf[lng] == '/' && mutt_strncasecmp (buf, p, lng) == 0)
303 void mutt_check_lookup_list (BODY * b, char *type, int len)
305 LIST *t = MimeLookupList;
308 for (; t; t = t->next) {
309 i = mutt_strlen (t->data) - 1;
310 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
311 ascii_strncasecmp (type, t->data, i) == 0) ||
312 ascii_strcasecmp (type, t->data) == 0) {
317 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
318 snprintf (type, len, "%s/%s",
319 n == TYPEAUDIO ? "audio" :
320 n == TYPEAPPLICATION ? "application" :
321 n == TYPEIMAGE ? "image" :
322 n == TYPEMESSAGE ? "message" :
323 n == TYPEMODEL ? "model" :
324 n == TYPEMULTIPART ? "multipart" :
325 n == TYPETEXT ? "text" :
326 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
327 dprint (1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n",
338 int mutt_is_autoview (BODY * b, const char *type)
340 LIST *t = AutoViewList;
341 char _type[SHORT_STRING];
345 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
347 strncpy (_type, type, sizeof (_type));
349 mutt_check_lookup_list (b, _type, sizeof (_type));
352 if (mutt_needs_mailcap (b)) {
353 if (option (OPTIMPLICITAUTOVIEW))
356 if (is_mmnoask (type))
360 for (; t; t = t->next) {
361 i = mutt_strlen (t->data) - 1;
362 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
363 ascii_strncasecmp (type, t->data, i) == 0) ||
364 ascii_strcasecmp (type, t->data) == 0)
371 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
372 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
373 ATTACHPTR ** idx, short idxlen)
375 char tempfile[_POSIX_PATH_MAX] = "";
376 char pagerfile[_POSIX_PATH_MAX] = "";
382 char command[STRING];
383 char descrip[STRING];
385 rfc1524_entry *entry = NULL;
387 int unlink_tempfile = 0;
389 is_message = mutt_is_message_type (a->type, a->subtype);
390 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
391 !crypt_valid_passphrase (a->hdr->security))
393 use_mailcap = (flag == M_MAILCAP ||
394 (flag == M_REGULAR && mutt_needs_mailcap (a)));
395 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
398 entry = rfc1524_new_entry ();
399 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
400 if (flag == M_REGULAR) {
401 /* fallback to view as text */
402 rfc1524_free_entry (&entry);
403 mutt_error _("No matching mailcap entry found. Viewing as text.");
414 if (!entry->command) {
415 mutt_error _("MIME type not defined. Cannot view attachment.");
419 strfcpy (command, entry->command, sizeof (command));
422 fname = safe_strdup (a->filename);
423 mutt_sanitize_filename (fname, 1);
428 if (rfc1524_expand_filename (entry->nametemplate, fname,
429 tempfile, sizeof (tempfile))) {
430 if (fp == NULL && mutt_strcmp (tempfile, a->filename)) {
431 /* send case: the file is already there */
432 if (safe_symlink (a->filename, tempfile) == -1) {
433 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
435 strfcpy (tempfile, a->filename, sizeof (tempfile));
443 else if (fp == NULL) /* send case */
444 strfcpy (tempfile, a->filename, sizeof (tempfile));
447 /* recv case: we need to save the attachment to a file */
449 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
453 use_pipe = rfc1524_expand_command (a, tempfile, type,
454 command, sizeof (command));
455 use_pager = entry->copiousoutput;
459 if (fp && !use_mailcap && a->filename) {
461 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
462 mutt_adv_mktemp (pagerfile, sizeof (pagerfile));
465 mutt_mktemp (pagerfile);
470 int tempfd = -1, pagerfd = -1;
475 if (use_pager || use_pipe) {
477 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
479 mutt_perror ("open");
482 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
485 mutt_perror ("open");
489 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
490 use_pipe ? tempfd : -1,
491 use_pager ? pagerfd : -1,
499 mutt_error _("Cannot create filter");
506 snprintf (descrip, sizeof (descrip),
507 "---Command: %-20.20s Description: %s",
508 command, a->description);
510 snprintf (descrip, sizeof (descrip),
511 "---Command: %-30.30s Attachment: %s", command, type);
514 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
515 option (OPTWAITKEY))) && !use_pager)
516 mutt_any_key_to_continue (NULL);
523 /* interactive command */
524 if (mutt_system (command) ||
525 (entry->needsterminal && option (OPTWAITKEY)))
526 mutt_any_key_to_continue (NULL);
530 /* Don't use mailcap; the attachment is viewed in the pager */
532 if (flag == M_AS_TEXT) {
533 /* just let me see the raw data */
534 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
538 /* Use built-in handler */
539 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
540 * message in case of error */
541 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
542 unset_option (OPTVIEWATTACH);
545 unset_option (OPTVIEWATTACH);
549 strfcpy (descrip, a->description, sizeof (descrip));
550 else if (a->filename)
551 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
554 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
557 /* We only reach this point if there have been no errors */
562 memset (&info, 0, sizeof (info));
567 info.idxlen = idxlen;
570 rc = mutt_do_pager (descrip, pagerfile,
571 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE :
581 rfc1524_free_entry (&entry);
582 if (fp && tempfile[0])
583 mutt_unlink (tempfile);
584 else if (unlink_tempfile)
588 mutt_unlink (pagerfile);
593 /* returns 1 on success, 0 on error */
594 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
601 if (outfile && *outfile)
602 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
603 mutt_perror ("open");
614 memset (&s, 0, sizeof (STATE));
616 if (outfile && *outfile)
618 mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
620 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
623 mutt_perror _("Can't create filter");
629 mutt_decode_attachment (b, &s);
630 safe_fclose (&s.fpout);
637 if ((ifp = fopen (b->filename, "r")) == NULL) {
638 mutt_perror ("fopen");
639 if (outfile && *outfile) {
646 if (outfile && *outfile)
647 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
649 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
652 mutt_perror _("Can't create filter");
658 mutt_copy_stream (ifp, ofp);
667 if (outfile && *outfile)
671 * check for error exit from child process
673 if (mutt_wait_filter (thepid) != 0)
676 if (rv == 0 || option (OPTWAITKEY))
677 mutt_any_key_to_continue (NULL);
681 static FILE *mutt_save_attachment_open (char *path, int flags)
683 if (flags == M_SAVE_APPEND)
684 return fopen (path, "a");
685 /* be sure not to change the following fopen to safe_fopen
686 * as safe_fopen returns w/ an error if path exists
688 if (flags == M_SAVE_OVERWRITE)
689 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
691 return safe_fopen (path, "w");
694 /* returns 0 on success, -1 on error */
695 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
704 m->encoding != ENCBASE64 &&
705 m->encoding != ENCQUOTEDPRINTABLE &&
706 mutt_is_message_type (m->type, m->subtype)) {
707 /* message type attachments are written to mail folders. */
709 char buf[HUGE_STRING];
717 hn->msgno = hdr->msgno; /* required for MH/maildir */
720 fseek (fp, m->offset, 0);
721 if (fgets (buf, sizeof (buf), fp) == NULL)
723 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
726 mx_open_new_message (&ctx, hn,
727 is_from (buf, NULL, 0,
728 NULL) ? 0 : M_ADD_FROM)) == NULL) {
729 mx_close_mailbox (&ctx, NULL);
732 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
734 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
735 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
736 && mx_commit_message (msg, &ctx) == 0)
741 mx_close_message (&msg);
742 mx_close_mailbox (&ctx, NULL);
746 /* In recv mode, extract from folder and decode */
750 memset (&s, 0, sizeof (s));
751 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
752 mutt_perror ("fopen");
755 fseek ((s.fpin = fp), m->offset, 0);
756 mutt_decode_attachment (m, &s);
758 if (fclose (s.fpout) != 0) {
759 mutt_perror ("fclose");
765 /* In send mode, just copy file */
769 if ((ofp = fopen (m->filename, "r")) == NULL) {
770 mutt_perror ("fopen");
774 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
775 mutt_perror ("fopen");
780 if (mutt_copy_stream (ofp, nfp) == -1) {
781 mutt_error _("Write fault!");
794 /* returns 0 on success, -1 on error */
795 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
796 int displaying, int flags)
799 unsigned int saved_encoding = 0;
800 BODY *saved_parts = NULL;
801 HEADER *saved_hdr = NULL;
803 memset (&s, 0, sizeof (s));
804 s.flags = displaying;
806 if (flags == M_SAVE_APPEND)
807 s.fpout = fopen (path, "a");
808 else if (flags == M_SAVE_OVERWRITE)
809 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
811 s.fpout = safe_fopen (path, "w");
813 if (s.fpout == NULL) {
814 mutt_perror ("fopen");
819 /* When called from the compose menu, the attachment isn't parsed,
820 * so we need to do it here. */
823 if (stat (m->filename, &st) == -1) {
824 mutt_perror ("stat");
829 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
830 mutt_perror ("fopen");
834 saved_encoding = m->encoding;
835 if (!is_multipart (m))
836 m->encoding = ENC8BIT;
838 m->length = st.st_size;
840 saved_parts = m->parts;
842 mutt_parse_part (s.fpin, m);
844 if (m->noconv || is_multipart (m))
845 s.flags |= M_CHARCONV;
849 s.flags |= M_CHARCONV;
852 mutt_body_handler (m, &s);
857 m->encoding = saved_encoding;
859 mutt_free_header (&m->hdr);
860 m->parts = saved_parts;
869 /* Ok, the difference between send and receive:
870 * recv: BODY->filename is a suggested name, and Context|HEADER points
871 * to the attachment in mailbox which is encooded
872 * send: BODY->filename points to the un-encoded file which contains the
876 int mutt_print_attachment (FILE * fp, BODY * a)
878 char newfile[_POSIX_PATH_MAX] = "";
882 short unlink_newfile = 0;
884 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
886 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
887 char command[_POSIX_PATH_MAX + STRING];
888 rfc1524_entry *entry;
891 dprint (2, (debugfile, "Using mailcap...\n"));
893 entry = rfc1524_new_entry ();
894 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
895 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
896 newfile, sizeof (newfile))) {
898 if (safe_symlink (a->filename, newfile) == -1) {
899 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
901 rfc1524_free_entry (&entry);
904 strfcpy (newfile, a->filename, sizeof (newfile));
911 /* in recv mode, save file to newfile first */
913 mutt_save_attachment (fp, a, newfile, 0, NULL);
915 strfcpy (command, entry->printcommand, sizeof (command));
917 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
921 /* interactive program */
923 if ((ifp = fopen (newfile, "r")) == NULL) {
924 mutt_perror ("fopen");
925 rfc1524_free_entry (&entry);
929 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
930 mutt_perror _("Can't create filter");
932 rfc1524_free_entry (&entry);
936 mutt_copy_stream (ifp, fpout);
937 safe_fclose (&fpout);
939 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
940 mutt_any_key_to_continue (NULL);
943 if (mutt_system (command) || option (OPTWAITKEY))
944 mutt_any_key_to_continue (NULL);
948 mutt_unlink (newfile);
949 else if (unlink_newfile)
952 rfc1524_free_entry (&entry);
956 if (!ascii_strcasecmp ("text/plain", type) ||
957 !ascii_strcasecmp ("application/postscript", type)) {
958 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
960 else if (mutt_can_decode (a)) {
961 /* decode and print */
968 mutt_mktemp (newfile);
969 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
972 (debugfile, "successfully decoded %s type attachment to %s\n",
975 if ((ifp = fopen (newfile, "r")) == NULL) {
976 mutt_perror ("fopen");
980 dprint (2, (debugfile, "successfully opened %s read-only\n", newfile));
984 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
985 mutt_perror _("Can't create filter");
990 dprint (2, (debugfile, "Filter created.\n"));
992 mutt_copy_stream (ifp, fpout);
994 safe_fclose (&fpout);
997 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
998 mutt_any_key_to_continue (NULL);
1003 safe_fclose (&fpout);
1004 mutt_unlink (newfile);
1008 mutt_error _("I don't know how to print that!");