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"
29 #include "lib/debug.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 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 debug_print (1, ("oldfile: %s\t newfile: %s\n", 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 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
223 if (safe_symlink (a->filename, newfile) == -1) {
224 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
232 strfcpy (newfile, a->filename, sizeof (newfile));
234 if (rfc1524_expand_command (a, newfile, type,
235 command, sizeof (command))) {
236 /* For now, editing requires a file, no piping */
237 mutt_error _("Mailcap Edit entry requires %%s");
241 if (mutt_system (command) == -1)
242 mutt_error (_("Error running \"%s\"!"), command);
246 else if (a->type == TYPETEXT) {
247 /* On text, default to editor */
248 mutt_edit_file (NONULL (Editor), newfile);
251 rfc1524_free_entry (&entry);
252 mutt_error (_("No mailcap edit entry for %s"), type);
263 rfc1524_free_entry (&entry);
268 /* for compatibility with metamail */
269 static int is_mmnoask (const char *buf)
271 char tmp[LONG_STRING], *p, *q;
274 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
275 if (mutt_strcmp (p, "1") == 0)
278 strfcpy (tmp, p, sizeof (tmp));
281 while ((p = strtok (p, ",")) != NULL) {
282 if ((q = strrchr (p, '/')) != NULL) {
283 if (*(q + 1) == '*') {
284 if (ascii_strncasecmp (buf, p, q - p) == 0)
288 if (ascii_strcasecmp (buf, p) == 0)
293 lng = mutt_strlen (p);
294 if (buf[lng] == '/' && safe_strncasecmp (buf, p, lng) == 0)
305 void mutt_check_lookup_list (BODY * b, char *type, int len)
307 LIST *t = MimeLookupList;
310 for (; t; t = t->next) {
311 i = mutt_strlen (t->data) - 1;
312 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
313 ascii_strncasecmp (type, t->data, i) == 0) ||
314 ascii_strcasecmp (type, t->data) == 0) {
319 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
320 snprintf (type, len, "%s/%s",
321 n == TYPEAUDIO ? "audio" :
322 n == TYPEAPPLICATION ? "application" :
323 n == TYPEIMAGE ? "image" :
324 n == TYPEMESSAGE ? "message" :
325 n == TYPEMODEL ? "model" :
326 n == TYPEMULTIPART ? "multipart" :
327 n == TYPETEXT ? "text" :
328 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
329 debug_print (1, ("\"%s\" -> %s\n", b->filename, type));
339 int mutt_is_autoview (BODY * b, const char *type)
341 LIST *t = AutoViewList;
342 char _type[SHORT_STRING];
346 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
348 strncpy (_type, type, sizeof (_type));
350 mutt_check_lookup_list (b, _type, sizeof (_type));
353 if (mutt_needs_mailcap (b)) {
354 if (option (OPTIMPLICITAUTOVIEW))
357 if (is_mmnoask (type))
361 for (; t; t = t->next) {
362 i = mutt_strlen (t->data) - 1;
363 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
364 ascii_strncasecmp (type, t->data, i) == 0) ||
365 ascii_strcasecmp (type, t->data) == 0)
372 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
373 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
374 ATTACHPTR ** idx, short idxlen)
376 char tempfile[_POSIX_PATH_MAX] = "";
377 char pagerfile[_POSIX_PATH_MAX] = "";
383 char command[STRING];
384 char descrip[STRING];
386 rfc1524_entry *entry = NULL;
388 int unlink_tempfile = 0;
390 is_message = mutt_is_message_type (a->type, a->subtype);
391 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
392 !crypt_valid_passphrase (a->hdr->security))
394 use_mailcap = (flag == M_MAILCAP ||
395 (flag == M_REGULAR && mutt_needs_mailcap (a)));
396 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
399 entry = rfc1524_new_entry ();
400 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
401 if (flag == M_REGULAR) {
402 /* fallback to view as text */
403 rfc1524_free_entry (&entry);
404 mutt_error _("No matching mailcap entry found. Viewing as text.");
415 if (!entry->command) {
416 mutt_error _("MIME type not defined. Cannot view attachment.");
420 strfcpy (command, entry->command, sizeof (command));
423 fname = safe_strdup (a->filename);
424 mutt_sanitize_filename (fname, 1);
429 if (rfc1524_expand_filename (entry->nametemplate, fname,
430 tempfile, sizeof (tempfile))) {
431 if (fp == NULL && mutt_strcmp (tempfile, a->filename)) {
432 /* send case: the file is already there */
433 if (safe_symlink (a->filename, tempfile) == -1) {
434 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
436 strfcpy (tempfile, a->filename, sizeof (tempfile));
444 else if (fp == NULL) /* send case */
445 strfcpy (tempfile, a->filename, sizeof (tempfile));
448 /* recv case: we need to save the attachment to a file */
450 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
454 use_pipe = rfc1524_expand_command (a, tempfile, type,
455 command, sizeof (command));
456 use_pager = entry->copiousoutput;
460 if (fp && !use_mailcap && a->filename) {
462 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
463 mutt_adv_mktemp (pagerfile, sizeof (pagerfile));
466 mutt_mktemp (pagerfile);
471 int tempfd = -1, pagerfd = -1;
476 if (use_pager || use_pipe) {
478 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
480 mutt_perror ("open");
483 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
486 mutt_perror ("open");
490 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
491 use_pipe ? tempfd : -1,
492 use_pager ? pagerfd : -1,
500 mutt_error _("Cannot create filter");
507 snprintf (descrip, sizeof (descrip),
508 "---Command: %-20.20s Description: %s",
509 command, a->description);
511 snprintf (descrip, sizeof (descrip),
512 "---Command: %-30.30s Attachment: %s", command, type);
515 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
516 option (OPTWAITKEY))) && !use_pager)
517 mutt_any_key_to_continue (NULL);
524 /* interactive command */
525 if (mutt_system (command) ||
526 (entry->needsterminal && option (OPTWAITKEY)))
527 mutt_any_key_to_continue (NULL);
531 /* Don't use mailcap; the attachment is viewed in the pager */
533 if (flag == M_AS_TEXT) {
534 /* just let me see the raw data */
535 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
539 /* Use built-in handler */
540 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
541 * message in case of error */
542 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
543 unset_option (OPTVIEWATTACH);
546 unset_option (OPTVIEWATTACH);
550 strfcpy (descrip, a->description, sizeof (descrip));
551 else if (a->filename)
552 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
555 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
558 /* We only reach this point if there have been no errors */
563 memset (&info, 0, sizeof (info));
568 info.idxlen = idxlen;
571 rc = mutt_do_pager (descrip, pagerfile,
572 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE :
582 rfc1524_free_entry (&entry);
583 if (fp && tempfile[0])
584 mutt_unlink (tempfile);
585 else if (unlink_tempfile)
589 mutt_unlink (pagerfile);
594 /* returns 1 on success, 0 on error */
595 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
602 if (outfile && *outfile)
603 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
604 mutt_perror ("open");
615 memset (&s, 0, sizeof (STATE));
617 if (outfile && *outfile)
619 mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
621 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
624 mutt_perror (_("Can't create filter"));
630 mutt_decode_attachment (b, &s);
631 safe_fclose (&s.fpout);
638 if ((ifp = fopen (b->filename, "r")) == NULL) {
639 mutt_perror ("fopen");
640 if (outfile && *outfile) {
647 if (outfile && *outfile)
648 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
650 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
653 mutt_perror (_("Can't create filter"));
659 mutt_copy_stream (ifp, ofp);
668 if (outfile && *outfile)
672 * check for error exit from child process
674 if (mutt_wait_filter (thepid) != 0)
677 if (rv == 0 || option (OPTWAITKEY))
678 mutt_any_key_to_continue (NULL);
682 static FILE *mutt_save_attachment_open (char *path, int flags)
684 if (flags == M_SAVE_APPEND)
685 return fopen (path, "a");
686 /* be sure not to change the following fopen to safe_fopen
687 * as safe_fopen returns w/ an error if path exists
689 if (flags == M_SAVE_OVERWRITE)
690 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
692 return safe_fopen (path, "w");
695 /* returns 0 on success, -1 on error */
696 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
705 m->encoding != ENCBASE64 &&
706 m->encoding != ENCQUOTEDPRINTABLE &&
707 mutt_is_message_type (m->type, m->subtype)) {
708 /* message type attachments are written to mail folders. */
710 char buf[HUGE_STRING];
718 hn->msgno = hdr->msgno; /* required for MH/maildir */
721 fseek (fp, m->offset, 0);
722 if (fgets (buf, sizeof (buf), fp) == NULL)
724 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
727 mx_open_new_message (&ctx, hn,
728 is_from (buf, NULL, 0,
729 NULL) ? 0 : M_ADD_FROM)) == NULL) {
730 mx_close_mailbox (&ctx, NULL);
733 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
735 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
736 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
737 && mx_commit_message (msg, &ctx) == 0)
742 mx_close_message (&msg);
743 mx_close_mailbox (&ctx, NULL);
747 /* In recv mode, extract from folder and decode */
751 memset (&s, 0, sizeof (s));
752 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
753 mutt_perror ("fopen");
756 fseek ((s.fpin = fp), m->offset, 0);
757 mutt_decode_attachment (m, &s);
759 if (fclose (s.fpout) != 0) {
760 mutt_perror ("fclose");
766 /* In send mode, just copy file */
770 if ((ofp = fopen (m->filename, "r")) == NULL) {
771 mutt_perror ("fopen");
775 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
776 mutt_perror ("fopen");
781 if (mutt_copy_stream (ofp, nfp) == -1) {
782 mutt_error _("Write fault!");
795 /* returns 0 on success, -1 on error */
796 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
797 int displaying, int flags)
800 unsigned int saved_encoding = 0;
801 BODY *saved_parts = NULL;
802 HEADER *saved_hdr = NULL;
804 memset (&s, 0, sizeof (s));
805 s.flags = displaying;
807 if (flags == M_SAVE_APPEND)
808 s.fpout = fopen (path, "a");
809 else if (flags == M_SAVE_OVERWRITE)
810 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
812 s.fpout = safe_fopen (path, "w");
814 if (s.fpout == NULL) {
815 mutt_perror ("fopen");
820 /* When called from the compose menu, the attachment isn't parsed,
821 * so we need to do it here. */
824 if (stat (m->filename, &st) == -1) {
825 mutt_perror ("stat");
830 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
831 mutt_perror ("fopen");
835 saved_encoding = m->encoding;
836 if (!is_multipart (m))
837 m->encoding = ENC8BIT;
839 m->length = st.st_size;
841 saved_parts = m->parts;
843 mutt_parse_part (s.fpin, m);
845 if (m->noconv || is_multipart (m))
846 s.flags |= M_CHARCONV;
850 s.flags |= M_CHARCONV;
853 mutt_body_handler (m, &s);
858 m->encoding = saved_encoding;
860 mutt_free_header (&m->hdr);
861 m->parts = saved_parts;
870 /* Ok, the difference between send and receive:
871 * recv: BODY->filename is a suggested name, and Context|HEADER points
872 * to the attachment in mailbox which is encooded
873 * send: BODY->filename points to the un-encoded file which contains the
877 int mutt_print_attachment (FILE * fp, BODY * a)
879 char newfile[_POSIX_PATH_MAX] = "";
883 short unlink_newfile = 0;
885 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
887 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
888 char command[_POSIX_PATH_MAX + STRING];
889 rfc1524_entry *entry;
892 debug_print (2, ("Using mailcap...\n"));
894 entry = rfc1524_new_entry ();
895 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
896 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
897 newfile, sizeof (newfile))) {
899 if (safe_symlink (a->filename, newfile) == -1) {
900 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
902 rfc1524_free_entry (&entry);
905 strfcpy (newfile, a->filename, sizeof (newfile));
912 /* in recv mode, save file to newfile first */
914 mutt_save_attachment (fp, a, newfile, 0, NULL);
916 strfcpy (command, entry->printcommand, sizeof (command));
918 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
922 /* interactive program */
924 if ((ifp = fopen (newfile, "r")) == NULL) {
925 mutt_perror ("fopen");
926 rfc1524_free_entry (&entry);
930 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
931 mutt_perror (_("Can't create filter"));
933 rfc1524_free_entry (&entry);
937 mutt_copy_stream (ifp, fpout);
938 safe_fclose (&fpout);
940 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
941 mutt_any_key_to_continue (NULL);
944 if (mutt_system (command) || option (OPTWAITKEY))
945 mutt_any_key_to_continue (NULL);
949 mutt_unlink (newfile);
950 else if (unlink_newfile)
953 rfc1524_free_entry (&entry);
957 if (!ascii_strcasecmp ("text/plain", type) ||
958 !ascii_strcasecmp ("application/postscript", type)) {
959 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
961 else if (mutt_can_decode (a)) {
962 /* decode and print */
969 mutt_mktemp (newfile);
970 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
972 debug_print (2, ("successfully decoded %s type attachment to %s\n",
975 if ((ifp = fopen (newfile, "r")) == NULL) {
976 mutt_perror ("fopen");
980 debug_print (2, ("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 debug_print (2, ("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!");