2 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2004,2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_menu.h"
27 #include "mutt_curses.h"
35 #include "mutt_crypt.h"
46 int mutt_get_tmp_attachment (BODY *a)
49 char tempfile[_POSIX_PATH_MAX];
50 rfc1524_entry *entry = rfc1524_new_entry();
51 FILE *fpin = NULL, *fpout = NULL;
57 snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
58 rfc1524_mailcap_lookup(a, type, entry, 0);
59 rfc1524_expand_filename(entry->nametemplate, a->filename,
60 tempfile, sizeof(tempfile));
62 rfc1524_free_entry(&entry);
64 if(stat(a->filename, &st) == -1)
67 if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) /* __FOPEN_CHECKED__ */
69 mutt_copy_stream (fpin, fpout);
70 mutt_str_replace (&a->filename, tempfile);
73 if(a->stamp >= st.st_mtime)
74 mutt_stamp_attachment(a);
77 mutt_perror(fpin ? tempfile : a->filename);
79 if(fpin) safe_fclose (&fpin);
80 if(fpout) safe_fclose (&fpout);
82 return a->unlink ? 0 : -1;
86 /* return 1 if require full screen redraw, 0 otherwise */
87 int mutt_compose_attachment (BODY *a)
91 char newfile[_POSIX_PATH_MAX] = "";
92 rfc1524_entry *entry = rfc1524_new_entry ();
93 short unlink_newfile = 0;
96 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
97 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE))
99 if (entry->composecommand || entry->composetypecommand)
102 if (entry->composetypecommand)
103 strfcpy (command, entry->composetypecommand, sizeof (command));
105 strfcpy (command, entry->composecommand, sizeof (command));
106 if (rfc1524_expand_filename (entry->nametemplate,
107 a->filename, newfile, sizeof (newfile)))
109 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
110 a->filename, newfile));
111 if (safe_symlink (a->filename, newfile) == -1)
113 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
120 strfcpy(newfile, a->filename, sizeof(newfile));
122 if (rfc1524_expand_command (a, newfile, type,
123 command, sizeof (command)))
125 /* For now, editing requires a file, no piping */
126 mutt_error _("Mailcap compose entry requires %%s");
133 if ((r = mutt_system (command)) == -1)
134 mutt_error (_("Error running \"%s\"!"), command);
136 if (r != -1 && entry->composetypecommand)
140 char tempfile[_POSIX_PATH_MAX];
142 if ((fp = safe_fopen (a->filename, "r")) == NULL)
144 mutt_perror _("Failure to open file to parse headers.");
148 b = mutt_read_mime_header (fp, 0);
153 mutt_free_parameter (&a->parameter);
154 a->parameter = b->parameter;
157 if (b->description) {
158 FREE (&a->description);
159 a->description = b->description;
160 b->description = NULL;
164 FREE (&a->form_name);
165 a->form_name = b->form_name;
169 /* Remove headers by copying out data to another file, then
170 * copying the file back */
171 fseeko (fp, b->offset, 0);
172 mutt_mktemp (tempfile, sizeof (tempfile));
173 if ((tfp = safe_fopen (tempfile, "w")) == NULL)
175 mutt_perror _("Failure to open file to strip headers.");
178 mutt_copy_stream (fp, tfp);
181 mutt_unlink (a->filename);
182 if (mutt_rename_file (tempfile, a->filename) != 0)
184 mutt_perror _("Failure to rename file.");
196 rfc1524_free_entry (&entry);
197 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
209 rfc1524_free_entry (&entry);
214 * Currently, this only works for send mode, as it assumes that the
215 * BODY->filename actually contains the information. I'm not sure
216 * we want to deal with editing attachments we've already received,
217 * so this should be ok.
219 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
222 int mutt_edit_attachment (BODY *a)
225 char command[STRING];
226 char newfile[_POSIX_PATH_MAX] = "";
227 rfc1524_entry *entry = rfc1524_new_entry ();
228 short unlink_newfile = 0;
231 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
232 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT))
234 if (entry->editcommand)
237 strfcpy (command, entry->editcommand, sizeof (command));
238 if (rfc1524_expand_filename (entry->nametemplate,
239 a->filename, newfile, sizeof (newfile)))
241 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
242 a->filename, newfile));
243 if (safe_symlink (a->filename, newfile) == -1)
245 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
252 strfcpy(newfile, a->filename, sizeof(newfile));
254 if (rfc1524_expand_command (a, newfile, type,
255 command, sizeof (command)))
257 /* For now, editing requires a file, no piping */
258 mutt_error _("Mailcap Edit entry requires %%s");
264 if (mutt_system (command) == -1)
266 mutt_error (_("Error running \"%s\"!"), command);
272 else if (a->type == TYPETEXT)
274 /* On text, default to editor */
275 mutt_edit_file (NONULL (Editor), a->filename);
279 rfc1524_free_entry (&entry);
280 mutt_error (_("No mailcap edit entry for %s"),type);
291 rfc1524_free_entry (&entry);
296 /* for compatibility with metamail */
297 static int is_mmnoask (const char *buf)
299 char tmp[LONG_STRING], *p, *q;
302 if ((p = getenv ("MM_NOASK")) != NULL && *p)
304 if (mutt_strcmp (p, "1") == 0)
307 strfcpy (tmp, p, sizeof (tmp));
310 while ((p = strtok (p, ",")) != NULL)
312 if ((q = strrchr (p, '/')) != NULL)
316 if (ascii_strncasecmp (buf, p, q-p) == 0)
321 if (ascii_strcasecmp (buf, p) == 0)
327 lng = mutt_strlen (p);
328 if (buf[lng] == '/' && mutt_strncasecmp (buf, p, lng) == 0)
339 void mutt_check_lookup_list (BODY *b, char *type, int len)
341 LIST *t = MimeLookupList;
344 for (; t; t = t->next) {
345 i = mutt_strlen (t->data) - 1;
346 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
347 ascii_strncasecmp (type, t->data, i) == 0) ||
348 ascii_strcasecmp (type, t->data) == 0) {
352 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
353 snprintf (type, len, "%s/%s",
354 n == TYPEAUDIO ? "audio" :
355 n == TYPEAPPLICATION ? "application" :
356 n == TYPEIMAGE ? "image" :
357 n == TYPEMESSAGE ? "message" :
358 n == TYPEMODEL ? "model" :
359 n == TYPEMULTIPART ? "multipart" :
360 n == TYPETEXT ? "text" :
361 n == TYPEVIDEO ? "video" : "other",
363 dprint(1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n",
374 int mutt_is_autoview (BODY *b, const char *type)
376 LIST *t = AutoViewList;
377 char _type[SHORT_STRING];
381 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
383 strncpy (_type, type, sizeof(_type));
385 mutt_check_lookup_list (b, _type, sizeof(_type));
388 if (mutt_needs_mailcap (b))
390 if (option (OPTIMPLICITAUTOVIEW))
393 if (is_mmnoask (type))
397 for (; t; t = t->next) {
398 i = mutt_strlen (t->data) - 1;
399 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
400 ascii_strncasecmp (type, t->data, i) == 0) ||
401 ascii_strcasecmp (type, t->data) == 0)
408 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
409 int mutt_view_attachment (FILE *fp, BODY *a, int flag, HEADER *hdr,
410 ATTACHPTR **idx, short idxlen)
412 char tempfile[_POSIX_PATH_MAX] = "";
413 char pagerfile[_POSIX_PATH_MAX] = "";
419 char command[HUGE_STRING];
420 char descrip[STRING];
422 rfc1524_entry *entry = NULL;
424 int unlink_tempfile = 0;
426 is_message = mutt_is_message_type(a->type, a->subtype);
427 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
428 !crypt_valid_passphrase(a->hdr->security))
430 use_mailcap = (flag == M_MAILCAP ||
431 (flag == M_REGULAR && mutt_needs_mailcap (a)));
432 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
436 entry = rfc1524_new_entry ();
437 if (!rfc1524_mailcap_lookup (a, type, entry, 0))
439 if (flag == M_REGULAR)
441 /* fallback to view as text */
442 rfc1524_free_entry (&entry);
443 mutt_error _("No matching mailcap entry found. Viewing as text.");
456 mutt_error _("MIME type not defined. Cannot view attachment.");
459 strfcpy (command, entry->command, sizeof (command));
463 fname = safe_strdup (a->filename);
464 mutt_sanitize_filename (fname, 1);
469 if (rfc1524_expand_filename (entry->nametemplate, fname,
470 tempfile, sizeof (tempfile)))
472 if (fp == NULL && mutt_strcmp(tempfile, a->filename))
474 /* send case: the file is already there */
475 if (safe_symlink (a->filename, tempfile) == -1)
477 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) == M_YES)
478 strfcpy (tempfile, a->filename, sizeof (tempfile));
486 else if (fp == NULL) /* send case */
487 strfcpy (tempfile, a->filename, sizeof (tempfile));
491 /* recv case: we need to save the attachment to a file */
493 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
497 use_pipe = rfc1524_expand_command (a, tempfile, type,
498 command, sizeof (command));
499 use_pager = entry->copiousoutput;
504 if (fp && !use_mailcap && a->filename)
507 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
508 mutt_adv_mktemp (pagerfile, sizeof(pagerfile));
511 mutt_mktemp (pagerfile, sizeof (pagerfile));
517 int tempfd = -1, pagerfd = -1;
522 if (use_pager || use_pipe)
524 if (use_pager && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY)) == -1))
526 mutt_perror ("open");
529 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1))
533 mutt_perror ("open");
537 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
538 use_pipe ? tempfd : -1, use_pager ? pagerfd : -1, -1)) == -1)
546 mutt_error _("Cannot create filter");
553 snprintf (descrip, sizeof (descrip),
554 _("---Command: %-20.20s Description: %s"),
555 command, a->description);
557 snprintf (descrip, sizeof (descrip),
558 _("---Command: %-30.30s Attachment: %s"), command, type);
561 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
562 option (OPTWAITKEY))) && !use_pager)
563 mutt_any_key_to_continue (NULL);
572 /* interactive command */
573 if (mutt_system (command) ||
574 (entry->needsterminal && option (OPTWAITKEY)))
575 mutt_any_key_to_continue (NULL);
580 /* Don't use mailcap; the attachment is viewed in the pager */
582 if (flag == M_AS_TEXT)
584 /* just let me see the raw data */
585 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
590 /* Use built-in handler */
591 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
592 * message in case of error */
593 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0))
595 unset_option (OPTVIEWATTACH);
598 unset_option (OPTVIEWATTACH);
602 strfcpy (descrip, a->description, sizeof (descrip));
603 else if (a->filename)
604 snprintf (descrip, sizeof (descrip), _("---Attachment: %s: %s"),
607 snprintf (descrip, sizeof (descrip), _("---Attachment: %s"), type);
610 /* We only reach this point if there have been no errors */
616 memset (&info, 0, sizeof (info));
621 info.idxlen = idxlen;
624 rc = mutt_do_pager (descrip, pagerfile,
625 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0), &info);
634 rfc1524_free_entry (&entry);
635 if (fp && tempfile[0])
636 mutt_unlink (tempfile);
637 else if (unlink_tempfile)
641 mutt_unlink (pagerfile);
646 /* returns 1 on success, 0 on error */
647 int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile)
653 if (outfile && *outfile)
654 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0)
656 mutt_perror ("open");
668 memset (&s, 0, sizeof (STATE));
670 if (outfile && *outfile)
671 thepid = mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
673 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
677 mutt_perror _("Can't create filter");
682 mutt_decode_attachment (b, &s);
683 safe_fclose (&s.fpout);
691 if ((ifp = fopen (b->filename, "r")) == NULL)
693 mutt_perror ("fopen");
694 if (outfile && *outfile)
702 if (outfile && *outfile)
703 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
705 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
709 mutt_perror _("Can't create filter");
714 mutt_copy_stream (ifp, ofp);
723 if (outfile && *outfile)
727 * check for error exit from child process
729 if (mutt_wait_filter (thepid) != 0)
732 if (rv == 0 || option (OPTWAITKEY))
733 mutt_any_key_to_continue (NULL);
738 mutt_save_attachment_open (char *path, int flags)
740 if (flags == M_SAVE_APPEND)
741 return fopen (path, "a");
742 if (flags == M_SAVE_OVERWRITE)
743 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
745 return safe_fopen (path, "w");
748 /* returns 0 on success, -1 on error */
749 int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr)
758 m->encoding != ENCBASE64 &&
759 m->encoding != ENCQUOTEDPRINTABLE &&
760 mutt_is_message_type(m->type, m->subtype))
762 /* message type attachments are written to mail folders. */
764 char buf[HUGE_STRING];
772 hn->msgno = hdr->msgno; /* required for MH/maildir */
775 fseeko (fp, m->offset, 0);
776 if (fgets (buf, sizeof (buf), fp) == NULL)
778 if (mx_open_mailbox(path, M_APPEND | M_QUIET, &ctx) == NULL)
780 if ((msg = mx_open_new_message (&ctx, hn, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
782 mx_close_mailbox(&ctx, NULL);
785 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
786 chflags = CH_FROM | CH_UPDATE_LEN;
787 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
788 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
789 && mx_commit_message (msg, &ctx) == 0)
794 mx_close_message (&msg);
795 mx_close_mailbox (&ctx, NULL);
800 /* In recv mode, extract from folder and decode */
804 memset (&s, 0, sizeof (s));
805 s.flags |= M_CHARCONV;
807 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL)
809 mutt_perror ("fopen");
813 fseeko ((s.fpin = fp), m->offset, 0);
814 mutt_decode_attachment (m, &s);
816 if (fclose (s.fpout) != 0)
818 mutt_perror ("fclose");
826 /* In send mode, just copy file */
830 if ((ofp = fopen (m->filename, "r")) == NULL)
832 mutt_perror ("fopen");
836 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL)
838 mutt_perror ("fopen");
843 if (mutt_copy_stream (ofp, nfp) == -1)
845 mutt_error _("Write fault!");
857 /* returns 0 on success, -1 on error */
858 int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
859 int displaying, int flags)
862 unsigned int saved_encoding = 0;
863 BODY *saved_parts = NULL;
864 HEADER *saved_hdr = NULL;
866 memset (&s, 0, sizeof (s));
867 s.flags = displaying;
869 if (flags == M_SAVE_APPEND)
870 s.fpout = fopen (path, "a");
871 else if (flags == M_SAVE_OVERWRITE)
872 s.fpout = fopen (path, "w"); /* __FOPEN_CHECKED__ */
874 s.fpout = safe_fopen (path, "w");
878 mutt_perror ("fopen");
884 /* When called from the compose menu, the attachment isn't parsed,
885 * so we need to do it here. */
888 if (stat (m->filename, &st) == -1)
890 mutt_perror ("stat");
891 safe_fclose (&s.fpout);
895 if ((s.fpin = fopen (m->filename, "r")) == NULL)
897 mutt_perror ("fopen");
901 saved_encoding = m->encoding;
902 if (!is_multipart (m))
903 m->encoding = ENC8BIT;
905 m->length = st.st_size;
907 saved_parts = m->parts;
909 mutt_parse_part (s.fpin, m);
911 if (m->noconv || is_multipart (m))
912 s.flags |= M_CHARCONV;
917 s.flags |= M_CHARCONV;
920 mutt_body_handler (m, &s);
922 safe_fclose (&s.fpout);
926 m->encoding = saved_encoding;
929 mutt_free_header (&m->hdr);
930 m->parts = saved_parts;
933 safe_fclose (&s.fpin);
939 /* Ok, the difference between send and receive:
940 * recv: BODY->filename is a suggested name, and Context|HEADER points
941 * to the attachment in mailbox which is encoded
942 * send: BODY->filename points to the un-encoded file which contains the
946 int mutt_print_attachment (FILE *fp, BODY *a)
948 char newfile[_POSIX_PATH_MAX] = "";
952 short unlink_newfile = 0;
954 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
956 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT))
958 char command[_POSIX_PATH_MAX+STRING];
959 rfc1524_entry *entry;
962 dprint (2, (debugfile, "Using mailcap...\n"));
964 entry = rfc1524_new_entry ();
965 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
966 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
967 newfile, sizeof (newfile)))
971 if (safe_symlink(a->filename, newfile) == -1)
973 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
975 rfc1524_free_entry (&entry);
978 strfcpy (newfile, a->filename, sizeof (newfile));
985 /* in recv mode, save file to newfile first */
987 mutt_save_attachment (fp, a, newfile, 0, NULL);
989 strfcpy (command, entry->printcommand, sizeof (command));
990 piped = rfc1524_expand_command (a, newfile, type, command, sizeof (command));
994 /* interactive program */
997 if ((ifp = fopen (newfile, "r")) == NULL)
999 mutt_perror ("fopen");
1000 rfc1524_free_entry (&entry);
1004 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0)
1006 mutt_perror _("Can't create filter");
1007 rfc1524_free_entry (&entry);
1011 mutt_copy_stream (ifp, fpout);
1012 safe_fclose (&fpout);
1014 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
1015 mutt_any_key_to_continue (NULL);
1019 if (mutt_system (command) || option (OPTWAITKEY))
1020 mutt_any_key_to_continue (NULL);
1024 mutt_unlink (newfile);
1025 else if (unlink_newfile)
1028 rfc1524_free_entry (&entry);
1032 if (!ascii_strcasecmp ("text/plain", type) ||
1033 !ascii_strcasecmp ("application/postscript", type))
1035 return (mutt_pipe_attachment (fp, a, NONULL(PrintCmd), NULL));
1037 else if (mutt_can_decode (a))
1039 /* decode and print */
1046 mutt_mktemp (newfile, sizeof (newfile));
1047 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0)
1050 dprint (2, (debugfile, "successfully decoded %s type attachment to %s\n",
1053 if ((ifp = fopen (newfile, "r")) == NULL)
1055 mutt_perror ("fopen");
1059 dprint (2, (debugfile, "successfully opened %s read-only\n", newfile));
1062 if ((thepid = mutt_create_filter (NONULL(PrintCmd), &fpout, NULL, NULL)) < 0)
1064 mutt_perror _("Can't create filter");
1068 dprint (2, (debugfile, "Filter created.\n"));
1070 mutt_copy_stream (ifp, fpout);
1072 safe_fclose (&fpout);
1075 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1076 mutt_any_key_to_continue (NULL);
1081 safe_fclose (&fpout);
1082 mutt_unlink (newfile);
1087 mutt_error _("I don't know how to print that!");