2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 #include "mutt_menu.h"
22 #include "mutt_curses.h"
30 #include "mutt_crypt.h"
41 int mutt_get_tmp_attachment (BODY *a)
44 char tempfile[_POSIX_PATH_MAX];
45 rfc1524_entry *entry = rfc1524_new_entry();
46 FILE *fpin = NULL, *fpout = NULL;
52 snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
53 rfc1524_mailcap_lookup(a, type, entry, 0);
54 rfc1524_expand_filename(entry->nametemplate, a->filename,
55 tempfile, sizeof(tempfile));
57 rfc1524_free_entry(&entry);
59 if(stat(a->filename, &st) == -1)
62 if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) /* __FOPEN_CHECKED__ */
64 mutt_copy_stream (fpin, fpout);
65 mutt_str_replace (&a->filename, tempfile);
68 if(a->stamp >= st.st_mtime)
69 mutt_stamp_attachment(a);
72 mutt_perror(fpin ? tempfile : a->filename);
74 if(fpin) fclose(fpin);
75 if(fpout) fclose(fpout);
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))
94 if (entry->composecommand || entry->composetypecommand)
97 if (entry->composetypecommand)
98 strfcpy (command, entry->composetypecommand, sizeof (command));
100 strfcpy (command, entry->composecommand, sizeof (command));
101 if (rfc1524_expand_filename (entry->nametemplate,
102 a->filename, newfile, sizeof (newfile)))
104 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
105 a->filename, newfile));
106 if (safe_symlink (a->filename, newfile) == -1)
108 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
114 strfcpy(newfile, a->filename, sizeof(newfile));
116 if (rfc1524_expand_command (a, newfile, type,
117 command, sizeof (command)))
119 /* For now, editing requires a file, no piping */
120 mutt_error _("Mailcap compose entry requires %%s");
127 if ((r = mutt_system (command)) == -1)
128 mutt_error (_("Error running \"%s\"!"), command);
130 if (r != -1 && entry->composetypecommand)
134 char tempfile[_POSIX_PATH_MAX];
136 if ((fp = safe_fopen (a->filename, "r")) == NULL)
138 mutt_perror _("Failure to open file to parse headers.");
142 b = mutt_read_mime_header (fp, 0);
147 mutt_free_parameter (&a->parameter);
148 a->parameter = b->parameter;
151 if (b->description) {
152 FREE (&a->description);
153 a->description = b->description;
154 b->description = NULL;
158 FREE (&a->form_name);
159 a->form_name = b->form_name;
163 /* Remove headers by copying out data to another file, then
164 * copying the file back */
165 fseek (fp, b->offset, 0);
166 mutt_mktemp (tempfile);
167 if ((tfp = safe_fopen (tempfile, "w")) == NULL)
169 mutt_perror _("Failure to open file to strip headers.");
172 mutt_copy_stream (fp, tfp);
175 mutt_unlink (a->filename);
176 mutt_rename_file (tempfile, a->filename);
186 rfc1524_free_entry (&entry);
187 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
199 rfc1524_free_entry (&entry);
204 * Currently, this only works for send mode, as it assumes that the
205 * BODY->filename actually contains the information. I'm not sure
206 * we want to deal with editing attachments we've already received,
207 * so this should be ok.
209 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
212 int mutt_edit_attachment (BODY *a)
215 char command[STRING];
216 char newfile[_POSIX_PATH_MAX] = "";
217 rfc1524_entry *entry = rfc1524_new_entry ();
218 short unlink_newfile = 0;
221 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
222 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT))
224 if (entry->editcommand)
227 strfcpy (command, entry->editcommand, sizeof (command));
228 if (rfc1524_expand_filename (entry->nametemplate,
229 a->filename, newfile, sizeof (newfile)))
231 dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
232 a->filename, newfile));
233 if (safe_symlink (a->filename, newfile) == -1)
235 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
241 strfcpy(newfile, a->filename, sizeof(newfile));
243 if (rfc1524_expand_command (a, newfile, type,
244 command, sizeof (command)))
246 /* For now, editing requires a file, no piping */
247 mutt_error _("Mailcap Edit entry requires %%s");
252 if (mutt_system (command) == -1)
253 mutt_error (_("Error running \"%s\"!"), command);
257 else if (a->type == TYPETEXT)
259 /* On text, default to editor */
260 mutt_edit_file (NONULL (Editor), newfile);
264 rfc1524_free_entry (&entry);
265 mutt_error (_("No mailcap edit entry for %s"),type);
276 rfc1524_free_entry (&entry);
281 /* for compatibility with metamail */
282 static int is_mmnoask (const char *buf)
284 char tmp[LONG_STRING], *p, *q;
287 if ((p = getenv ("MM_NOASK")) != NULL && *p)
289 if (mutt_strcmp (p, "1") == 0)
292 strfcpy (tmp, p, sizeof (tmp));
295 while ((p = strtok (p, ",")) != NULL)
297 if ((q = strrchr (p, '/')) != NULL)
301 if (ascii_strncasecmp (buf, p, q-p) == 0)
306 if (ascii_strcasecmp (buf, p) == 0)
312 lng = mutt_strlen (p);
313 if (buf[lng] == '/' && mutt_strncasecmp (buf, p, lng) == 0)
324 void mutt_check_lookup_list (BODY *b, char *type, int len)
326 LIST *t = MimeLookupList;
329 for (; t; t = t->next) {
330 i = mutt_strlen (t->data) - 1;
331 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
332 ascii_strncasecmp (type, t->data, i) == 0) ||
333 ascii_strcasecmp (type, t->data) == 0) {
337 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
338 snprintf (type, len, "%s/%s",
339 n == TYPEAUDIO ? "audio" :
340 n == TYPEAPPLICATION ? "application" :
341 n == TYPEIMAGE ? "image" :
342 n == TYPEMESSAGE ? "message" :
343 n == TYPEMODEL ? "model" :
344 n == TYPEMULTIPART ? "multipart" :
345 n == TYPETEXT ? "text" :
346 n == TYPEVIDEO ? "video" : "other",
348 dprint(1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n",
359 int mutt_is_autoview (BODY *b, const char *type)
361 LIST *t = AutoViewList;
362 char _type[SHORT_STRING];
366 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
368 strncpy (_type, type, sizeof(_type));
370 mutt_check_lookup_list (b, _type, sizeof(_type));
373 if (mutt_needs_mailcap (b))
375 if (option (OPTIMPLICITAUTOVIEW))
378 if (is_mmnoask (type))
382 for (; t; t = t->next) {
383 i = mutt_strlen (t->data) - 1;
384 if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
385 ascii_strncasecmp (type, t->data, i) == 0) ||
386 ascii_strcasecmp (type, t->data) == 0)
393 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
394 int mutt_view_attachment (FILE *fp, BODY *a, int flag, HEADER *hdr,
395 ATTACHPTR **idx, short idxlen)
397 char tempfile[_POSIX_PATH_MAX] = "";
398 char pagerfile[_POSIX_PATH_MAX] = "";
404 char command[STRING];
405 char descrip[STRING];
407 rfc1524_entry *entry = NULL;
409 int unlink_tempfile = 0;
411 is_message = mutt_is_message_type(a->type, a->subtype);
412 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
413 !crypt_valid_passphrase(a->hdr->security))
415 use_mailcap = (flag == M_MAILCAP ||
416 (flag == M_REGULAR && mutt_needs_mailcap (a)));
417 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
421 entry = rfc1524_new_entry ();
422 if (!rfc1524_mailcap_lookup (a, type, entry, 0))
424 if (flag == M_REGULAR)
426 /* fallback to view as text */
427 rfc1524_free_entry (&entry);
428 mutt_error _("No matching mailcap entry found. Viewing as text.");
441 mutt_error _("MIME type not defined. Cannot view attachment.");
444 strfcpy (command, entry->command, sizeof (command));
448 fname = safe_strdup (a->filename);
449 mutt_sanitize_filename (fname, 1);
454 if (rfc1524_expand_filename (entry->nametemplate, fname,
455 tempfile, sizeof (tempfile)))
457 if (fp == NULL && mutt_strcmp(tempfile, a->filename))
459 /* send case: the file is already there */
460 if (safe_symlink (a->filename, tempfile) == -1)
462 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) == M_YES)
463 strfcpy (tempfile, a->filename, sizeof (tempfile));
471 else if (fp == NULL) /* send case */
472 strfcpy (tempfile, a->filename, sizeof (tempfile));
476 /* recv case: we need to save the attachment to a file */
478 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
482 use_pipe = rfc1524_expand_command (a, tempfile, type,
483 command, sizeof (command));
484 use_pager = entry->copiousoutput;
489 if (fp && !use_mailcap && a->filename)
492 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
493 mutt_adv_mktemp (pagerfile, sizeof(pagerfile));
496 mutt_mktemp (pagerfile);
502 int tempfd = -1, pagerfd = -1;
507 if (use_pager || use_pipe)
509 if (use_pager && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY)) == -1))
511 mutt_perror ("open");
514 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1))
518 mutt_perror ("open");
522 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
523 use_pipe ? tempfd : -1, use_pager ? pagerfd : -1, -1)) == -1)
531 mutt_error _("Cannot create filter");
538 snprintf (descrip, sizeof (descrip),
539 "---Command: %-20.20s Description: %s",
540 command, a->description);
542 snprintf (descrip, sizeof (descrip),
543 "---Command: %-30.30s Attachment: %s", command, type);
546 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
547 option (OPTWAITKEY))) && !use_pager)
548 mutt_any_key_to_continue (NULL);
556 /* interactive command */
557 if (mutt_system (command) ||
558 (entry->needsterminal && option (OPTWAITKEY)))
559 mutt_any_key_to_continue (NULL);
564 /* Don't use mailcap; the attachment is viewed in the pager */
566 if (flag == M_AS_TEXT)
568 /* just let me see the raw data */
569 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
574 /* Use built-in handler */
575 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
576 * message in case of error */
577 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0))
579 unset_option (OPTVIEWATTACH);
582 unset_option (OPTVIEWATTACH);
586 strfcpy (descrip, a->description, sizeof (descrip));
587 else if (a->filename)
588 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
591 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
594 /* We only reach this point if there have been no errors */
600 memset (&info, 0, sizeof (info));
605 info.idxlen = idxlen;
608 rc = mutt_do_pager (descrip, pagerfile,
609 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0), &info);
617 rfc1524_free_entry (&entry);
618 if (fp && tempfile[0])
619 mutt_unlink (tempfile);
620 else if (unlink_tempfile)
624 mutt_unlink (pagerfile);
629 /* returns 1 on success, 0 on error */
630 int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile)
636 if (outfile && *outfile)
637 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0)
639 mutt_perror ("open");
651 memset (&s, 0, sizeof (STATE));
653 if (outfile && *outfile)
654 thepid = mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
656 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
660 mutt_perror _("Can't create filter");
665 mutt_decode_attachment (b, &s);
666 safe_fclose (&s.fpout);
674 if ((ifp = fopen (b->filename, "r")) == NULL)
676 mutt_perror ("fopen");
677 if (outfile && *outfile)
685 if (outfile && *outfile)
686 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
688 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
692 mutt_perror _("Can't create filter");
697 mutt_copy_stream (ifp, ofp);
706 if (outfile && *outfile)
710 * check for error exit from child process
712 if (mutt_wait_filter (thepid) != 0)
715 if (rv == 0 || option (OPTWAITKEY))
716 mutt_any_key_to_continue (NULL);
721 mutt_save_attachment_open (char *path, int flags)
723 if (flags == M_SAVE_APPEND)
724 return fopen (path, "a");
725 if (flags == M_SAVE_OVERWRITE)
726 return safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
728 return safe_fopen (path, "w");
731 /* returns 0 on success, -1 on error */
732 int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr)
741 m->encoding != ENCBASE64 &&
742 m->encoding != ENCQUOTEDPRINTABLE &&
743 mutt_is_message_type(m->type, m->subtype))
745 /* message type attachments are written to mail folders. */
747 char buf[HUGE_STRING];
755 hn->msgno = hdr->msgno; /* required for MH/maildir */
758 fseek (fp, m->offset, 0);
759 if (fgets (buf, sizeof (buf), fp) == NULL)
761 if (mx_open_mailbox(path, M_APPEND | M_QUIET, &ctx) == NULL)
763 if ((msg = mx_open_new_message (&ctx, hn, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL)
765 mx_close_mailbox(&ctx, NULL);
768 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
770 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
771 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
772 && mx_commit_message (msg, &ctx) == 0)
777 mx_close_message (&msg);
778 mx_close_mailbox (&ctx, NULL);
783 /* In recv mode, extract from folder and decode */
787 memset (&s, 0, sizeof (s));
788 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL)
790 mutt_perror ("fopen");
793 fseek ((s.fpin = fp), m->offset, 0);
794 mutt_decode_attachment (m, &s);
796 if (fclose (s.fpout) != 0)
798 mutt_perror ("fclose");
805 /* In send mode, just copy file */
809 if ((ofp = fopen (m->filename, "r")) == NULL)
811 mutt_perror ("fopen");
815 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL)
817 mutt_perror ("fopen");
822 if (mutt_copy_stream (ofp, nfp) == -1)
824 mutt_error _("Write fault!");
836 /* returns 0 on success, -1 on error */
837 int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
838 int displaying, int flags)
841 unsigned int saved_encoding = 0;
842 BODY *saved_parts = NULL;
843 HEADER *saved_hdr = NULL;
845 memset (&s, 0, sizeof (s));
846 s.flags = displaying;
848 if (flags == M_SAVE_APPEND)
849 s.fpout = fopen (path, "a");
850 else if (flags == M_SAVE_OVERWRITE)
851 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
853 s.fpout = safe_fopen (path, "w");
857 mutt_perror ("fopen");
863 /* When called from the compose menu, the attachment isn't parsed,
864 * so we need to do it here. */
867 if (stat (m->filename, &st) == -1)
869 mutt_perror ("stat");
874 if ((s.fpin = fopen (m->filename, "r")) == NULL)
876 mutt_perror ("fopen");
880 saved_encoding = m->encoding;
881 if (!is_multipart (m))
882 m->encoding = ENC8BIT;
884 m->length = st.st_size;
886 saved_parts = m->parts;
888 mutt_parse_part (s.fpin, m);
890 if (m->noconv || is_multipart (m))
891 s.flags |= M_CHARCONV;
896 s.flags |= M_CHARCONV;
899 mutt_body_handler (m, &s);
905 m->encoding = saved_encoding;
908 mutt_free_header (&m->hdr);
909 m->parts = saved_parts;
918 /* Ok, the difference between send and receive:
919 * recv: BODY->filename is a suggested name, and Context|HEADER points
920 * to the attachment in mailbox which is encooded
921 * send: BODY->filename points to the un-encoded file which contains the
925 int mutt_print_attachment (FILE *fp, BODY *a)
927 char newfile[_POSIX_PATH_MAX] = "";
931 short unlink_newfile = 0;
933 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
935 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT))
937 char command[_POSIX_PATH_MAX+STRING];
938 rfc1524_entry *entry;
941 dprint (2, (debugfile, "Using mailcap...\n"));
943 entry = rfc1524_new_entry ();
944 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
945 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
946 newfile, sizeof (newfile)))
950 if (safe_symlink(a->filename, newfile) == -1)
952 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES)
954 rfc1524_free_entry (&entry);
957 strfcpy (newfile, a->filename, sizeof (newfile));
964 /* in recv mode, save file to newfile first */
966 mutt_save_attachment (fp, a, newfile, 0, NULL);
968 strfcpy (command, entry->printcommand, sizeof (command));
969 piped = rfc1524_expand_command (a, newfile, type, command, sizeof (command));
973 /* interactive program */
976 if ((ifp = fopen (newfile, "r")) == NULL)
978 mutt_perror ("fopen");
979 rfc1524_free_entry (&entry);
983 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0)
985 mutt_perror _("Can't create filter");
986 rfc1524_free_entry (&entry);
990 mutt_copy_stream (ifp, fpout);
991 safe_fclose (&fpout);
993 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
994 mutt_any_key_to_continue (NULL);
998 if (mutt_system (command) || option (OPTWAITKEY))
999 mutt_any_key_to_continue (NULL);
1003 mutt_unlink (newfile);
1004 else if (unlink_newfile)
1007 rfc1524_free_entry (&entry);
1011 if (!ascii_strcasecmp ("text/plain", type) ||
1012 !ascii_strcasecmp ("application/postscript", type))
1014 return (mutt_pipe_attachment (fp, a, NONULL(PrintCmd), NULL));
1016 else if (mutt_can_decode (a))
1018 /* decode and print */
1025 mutt_mktemp (newfile);
1026 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0)
1029 dprint (2, (debugfile, "successfully decoded %s type attachment to %s\n",
1032 if ((ifp = fopen (newfile, "r")) == NULL)
1034 mutt_perror ("fopen");
1038 dprint (2, (debugfile, "successfully opened %s read-only\n", newfile));
1041 if ((thepid = mutt_create_filter (NONULL(PrintCmd), &fpout, NULL, NULL)) < 0)
1043 mutt_perror _("Can't create filter");
1047 dprint (2, (debugfile, "Filter created.\n"));
1049 mutt_copy_stream (ifp, fpout);
1051 safe_fclose (&fpout);
1054 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1055 mutt_any_key_to_continue (NULL);
1060 safe_fclose (&fpout);
1061 mutt_unlink (newfile);
1066 mutt_error _("I don't know how to print that!");