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.
15 #include <lib-lib/mem.h>
20 #include "recvattach.h"
21 #include "mutt_menu.h"
22 #include "mutt_curses.h"
29 #include "mutt_crypt.h"
34 #include "lib/debug.h"
45 int mutt_get_tmp_attachment (BODY * a)
48 char tempfile[_POSIX_PATH_MAX];
49 rfc1524_entry *entry = rfc1524_new_entry ();
50 FILE *fpin = NULL, *fpout = NULL;
56 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
57 rfc1524_mailcap_lookup (a, type, entry, 0);
58 rfc1524_expand_filename (entry->nametemplate, a->filename,
59 tempfile, sizeof (tempfile));
61 rfc1524_free_entry (&entry);
63 if (stat (a->filename, &st) == -1)
66 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
67 mutt_copy_stream (fpin, fpout);
68 str_replace (&a->filename, tempfile);
71 if (a->stamp >= st.st_mtime)
72 mutt_stamp_attachment (a);
75 mutt_perror(fpin ? tempfile : a->filename);
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)) {
98 if (entry->composecommand || entry->composetypecommand) {
100 if (entry->composetypecommand)
101 strfcpy (command, entry->composetypecommand, sizeof (command));
103 strfcpy (command, entry->composecommand, sizeof (command));
104 if (rfc1524_expand_filename (entry->nametemplate,
105 a->filename, newfile, sizeof (newfile))) {
106 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
107 if (safe_symlink (a->filename, newfile) == -1) {
108 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
116 strfcpy (newfile, a->filename, sizeof (newfile));
118 if (rfc1524_expand_command (a, newfile, type,
119 command, sizeof (command))) {
120 /* For now, editing requires a file, no piping */
121 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) {
133 char tempfile[_POSIX_PATH_MAX];
135 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
136 mutt_perror (_("Failure to open file to parse headers."));
141 b = mutt_read_mime_header (fp, 0);
144 mutt_free_parameter (&a->parameter);
145 a->parameter = b->parameter;
148 if (b->description) {
149 p_delete(&a->description);
150 a->description = b->description;
151 b->description = NULL;
154 p_delete(&a->form_name);
155 a->form_name = b->form_name;
159 /* Remove headers by copying out data to another file, then
160 * copying the file back */
161 fseeko (fp, b->offset, 0);
162 mutt_mktemp (tempfile);
163 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
164 mutt_perror (_("Failure to open file to strip headers."));
168 mutt_copy_stream (fp, tfp);
171 mutt_unlink (a->filename);
172 if (mutt_rename_file (tempfile, a->filename) != 0) {
173 mutt_perror (_("Failure to rename file."));
185 rfc1524_free_entry (&entry);
186 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
198 rfc1524_free_entry (&entry);
203 * Currently, this only works for send mode, as it assumes that the
204 * BODY->filename actually contains the information. I'm not sure
205 * we want to deal with editing attachments we've already received,
206 * so this should be ok.
208 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
211 int mutt_edit_attachment (BODY * a)
214 char command[STRING];
215 char newfile[_POSIX_PATH_MAX] = "";
216 rfc1524_entry *entry = rfc1524_new_entry ();
217 short unlink_newfile = 0;
220 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
221 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
222 if (entry->editcommand) {
224 strfcpy (command, entry->editcommand, sizeof (command));
225 if (rfc1524_expand_filename (entry->nametemplate,
226 a->filename, newfile, sizeof (newfile))) {
227 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
228 if (safe_symlink (a->filename, newfile) == -1) {
229 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
237 strfcpy (newfile, a->filename, sizeof (newfile));
239 if (rfc1524_expand_command (a, newfile, type,
240 command, sizeof (command))) {
241 /* For now, editing requires a file, no piping */
242 mutt_error _("Mailcap Edit entry requires %%s");
247 if (mutt_system (command) == -1) {
248 mutt_error (_("Error running \"%s\"!"), command);
254 else if (a->type == TYPETEXT) {
255 /* On text, default to editor */
256 mutt_edit_file (NONULL (Editor), a->filename);
259 rfc1524_free_entry (&entry);
260 mutt_error (_("No mailcap edit entry for %s"), type);
271 rfc1524_free_entry (&entry);
276 /* for compatibility with metamail */
277 static int is_mmnoask (const char *buf)
279 char tmp[LONG_STRING], *p, *q;
282 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
283 if (str_cmp (p, "1") == 0)
286 strfcpy (tmp, p, sizeof (tmp));
289 while ((p = strtok (p, ",")) != NULL) {
290 if ((q = strrchr (p, '/')) != NULL) {
291 if (*(q + 1) == '*') {
292 if (ascii_strncasecmp (buf, p, q - p) == 0)
296 if (ascii_strcasecmp (buf, p) == 0)
302 if (buf[lng] == '/' && str_ncasecmp (buf, p, lng) == 0)
313 void mutt_check_lookup_list (BODY * b, char *type, int len)
315 LIST *t = MimeLookupList;
318 for (; t; t = t->next) {
319 i = str_len (t->data) - 1;
320 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
321 ascii_strncasecmp (type, t->data, i) == 0) ||
322 ascii_strcasecmp (type, t->data) == 0) {
327 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
328 snprintf (type, len, "%s/%s",
329 n == TYPEAUDIO ? "audio" :
330 n == TYPEAPPLICATION ? "application" :
331 n == TYPEIMAGE ? "image" :
332 n == TYPEMESSAGE ? "message" :
333 n == TYPEMODEL ? "model" :
334 n == TYPEMULTIPART ? "multipart" :
335 n == TYPETEXT ? "text" :
336 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
337 debug_print (1, ("\"%s\" -> %s\n", b->filename, type));
340 p_delete(&tmp.subtype);
342 p_delete(&tmp.xtype);
347 int mutt_is_autoview (BODY * b, const char *type)
349 LIST *t = AutoViewList;
350 char _type[SHORT_STRING];
354 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
356 strncpy (_type, type, sizeof (_type));
358 mutt_check_lookup_list (b, _type, sizeof (_type));
361 if (mutt_needs_mailcap (b)) {
362 if (option (OPTIMPLICITAUTOVIEW))
365 if (is_mmnoask (type))
369 for (; t; t = t->next) {
370 i = str_len (t->data) - 1;
371 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
372 ascii_strncasecmp (type, t->data, i) == 0) ||
373 ascii_strcasecmp (type, t->data) == 0)
380 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
381 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
382 ATTACHPTR ** idx, short idxlen)
384 char tempfile[_POSIX_PATH_MAX] = "";
385 char pagerfile[_POSIX_PATH_MAX] = "";
391 char command[STRING];
392 char descrip[STRING];
394 rfc1524_entry *entry = NULL;
396 int unlink_tempfile = 0;
398 is_message = mutt_is_message_type (a->type, a->subtype);
399 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
400 !crypt_valid_passphrase (a->hdr->security))
402 use_mailcap = (flag == M_MAILCAP ||
403 (flag == M_REGULAR && mutt_needs_mailcap (a)));
404 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
407 entry = rfc1524_new_entry ();
408 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
409 if (flag == M_REGULAR) {
410 /* fallback to view as text */
411 rfc1524_free_entry (&entry);
412 mutt_error _("No matching mailcap entry found. Viewing as text.");
423 if (!entry->command) {
424 mutt_error _("MIME type not defined. Cannot view attachment.");
428 strfcpy (command, entry->command, sizeof (command));
431 fname = str_dup (a->filename);
432 mutt_sanitize_filename (fname, 1);
437 if (rfc1524_expand_filename (entry->nametemplate, fname,
438 tempfile, sizeof (tempfile))) {
439 if (fp == NULL && str_cmp (tempfile, a->filename)) {
440 /* send case: the file is already there */
441 if (safe_symlink (a->filename, tempfile) == -1) {
442 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
444 strfcpy (tempfile, a->filename, sizeof (tempfile));
452 else if (fp == NULL) /* send case */
453 strfcpy (tempfile, a->filename, sizeof (tempfile));
456 /* recv case: we need to save the attachment to a file */
458 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
462 use_pipe = rfc1524_expand_command (a, tempfile, type,
463 command, sizeof (command));
464 use_pager = entry->copiousoutput;
468 if (fp && !use_mailcap && a->filename) {
470 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
471 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
474 mutt_mktemp (pagerfile);
479 int tempfd = -1, pagerfd = -1;
484 if (use_pager || use_pipe) {
486 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
488 mutt_perror ("open");
491 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
494 mutt_perror ("open");
498 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
499 use_pipe ? tempfd : -1,
500 use_pager ? pagerfd : -1,
508 mutt_error _("Cannot create filter");
515 snprintf (descrip, sizeof (descrip),
516 "---Command: %-20.20s Description: %s",
517 command, a->description);
519 snprintf (descrip, sizeof (descrip),
520 "---Command: %-30.30s Attachment: %s", command, type);
523 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
524 option (OPTWAITKEY))) && !use_pager)
525 mutt_any_key_to_continue (NULL);
532 /* interactive command */
533 if (mutt_system (command) ||
534 (entry->needsterminal && option (OPTWAITKEY)))
535 mutt_any_key_to_continue (NULL);
539 /* Don't use mailcap; the attachment is viewed in the pager */
541 if (flag == M_AS_TEXT) {
542 /* just let me see the raw data */
543 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
547 /* Use built-in handler */
548 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
549 * message in case of error */
550 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
551 unset_option (OPTVIEWATTACH);
554 unset_option (OPTVIEWATTACH);
558 strfcpy (descrip, a->description, sizeof (descrip));
559 else if (a->filename)
560 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
563 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
566 /* We only reach this point if there have been no errors */
571 memset (&info, 0, sizeof (info));
576 info.idxlen = idxlen;
579 rc = mutt_do_pager (descrip, pagerfile,
580 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE :
590 rfc1524_free_entry (&entry);
591 if (fp && tempfile[0])
592 mutt_unlink (tempfile);
593 else if (unlink_tempfile)
597 mutt_unlink (pagerfile);
602 /* returns 1 on success, 0 on error */
603 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
610 if (outfile && *outfile)
611 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
612 mutt_perror ("open");
623 memset (&s, 0, sizeof (STATE));
625 if (outfile && *outfile)
627 mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
629 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
632 mutt_perror (_("Can't create filter"));
638 mutt_decode_attachment (b, &s);
639 safe_fclose (&s.fpout);
646 if ((ifp = fopen (b->filename, "r")) == NULL) {
647 mutt_perror ("fopen");
648 if (outfile && *outfile) {
655 if (outfile && *outfile)
656 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
658 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
661 mutt_perror (_("Can't create filter"));
667 mutt_copy_stream (ifp, ofp);
676 if (outfile && *outfile)
680 * check for error exit from child process
682 if (mutt_wait_filter (thepid) != 0)
685 if (rv == 0 || option (OPTWAITKEY))
686 mutt_any_key_to_continue (NULL);
690 static FILE *mutt_save_attachment_open (char *path, int flags)
692 if (flags == M_SAVE_APPEND)
693 return fopen (path, "a");
694 /* be sure not to change the following fopen to safe_fopen
695 * as safe_fopen returns w/ an error if path exists
697 if (flags == M_SAVE_OVERWRITE)
698 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
700 return safe_fopen (path, "w");
703 /* returns 0 on success, -1 on error */
704 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
713 m->encoding != ENCBASE64 &&
714 m->encoding != ENCQUOTEDPRINTABLE &&
715 mutt_is_message_type (m->type, m->subtype)) {
716 /* message type attachments are written to mail folders. */
718 char buf[HUGE_STRING];
726 hn->msgno = hdr->msgno; /* required for MH/maildir */
729 fseeko (fp, m->offset, 0);
730 if (fgets (buf, sizeof (buf), fp) == NULL)
732 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
735 mx_open_new_message (&ctx, hn,
736 is_from (buf, NULL, 0,
737 NULL) ? 0 : M_ADD_FROM)) == NULL) {
738 mx_close_mailbox (&ctx, NULL);
741 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
743 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
744 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
745 && mx_commit_message (msg, &ctx) == 0)
750 mx_close_message (&msg);
751 mx_close_mailbox (&ctx, NULL);
755 /* In recv mode, extract from folder and decode */
759 memset (&s, 0, sizeof (s));
760 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
761 mutt_perror ("fopen");
764 fseeko ((s.fpin = fp), m->offset, 0);
765 mutt_decode_attachment (m, &s);
767 if (fclose (s.fpout) != 0) {
768 mutt_perror ("fclose");
774 /* In send mode, just copy file */
778 if ((ofp = fopen (m->filename, "r")) == NULL) {
779 mutt_perror ("fopen");
783 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
784 mutt_perror ("fopen");
789 if (mutt_copy_stream (ofp, nfp) == -1) {
790 mutt_error _("Write fault!");
803 /* returns 0 on success, -1 on error */
804 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
805 int displaying, int flags)
808 unsigned int saved_encoding = 0;
809 BODY *saved_parts = NULL;
810 HEADER *saved_hdr = NULL;
812 memset (&s, 0, sizeof (s));
813 s.flags = displaying;
815 if (flags == M_SAVE_APPEND)
816 s.fpout = fopen (path, "a");
817 else if (flags == M_SAVE_OVERWRITE)
818 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
820 s.fpout = safe_fopen (path, "w");
822 if (s.fpout == NULL) {
823 mutt_perror ("fopen");
828 /* When called from the compose menu, the attachment isn't parsed,
829 * so we need to do it here. */
832 if (stat (m->filename, &st) == -1) {
833 mutt_perror ("stat");
838 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
839 mutt_perror ("fopen");
843 saved_encoding = m->encoding;
844 if (!is_multipart (m))
845 m->encoding = ENC8BIT;
847 m->length = st.st_size;
849 saved_parts = m->parts;
851 mutt_parse_part (s.fpin, m);
853 if (m->noconv || is_multipart (m))
854 s.flags |= M_CHARCONV;
858 s.flags |= M_CHARCONV;
861 mutt_body_handler (m, &s);
866 m->encoding = saved_encoding;
868 mutt_free_header (&m->hdr);
869 m->parts = saved_parts;
878 /* Ok, the difference between send and receive:
879 * recv: BODY->filename is a suggested name, and Context|HEADER points
880 * to the attachment in mailbox which is encooded
881 * send: BODY->filename points to the un-encoded file which contains the
885 int mutt_print_attachment (FILE * fp, BODY * a)
887 char newfile[_POSIX_PATH_MAX] = "";
891 short unlink_newfile = 0;
893 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
895 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
896 char command[_POSIX_PATH_MAX + STRING];
897 rfc1524_entry *entry;
900 debug_print (2, ("Using mailcap...\n"));
902 entry = rfc1524_new_entry ();
903 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
904 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
905 newfile, sizeof (newfile))) {
907 if (safe_symlink (a->filename, newfile) == -1) {
908 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
910 rfc1524_free_entry (&entry);
913 strfcpy (newfile, a->filename, sizeof (newfile));
920 /* in recv mode, save file to newfile first */
922 mutt_save_attachment (fp, a, newfile, 0, NULL);
924 strfcpy (command, entry->printcommand, sizeof (command));
926 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
930 /* interactive program */
932 if ((ifp = fopen (newfile, "r")) == NULL) {
933 mutt_perror ("fopen");
934 rfc1524_free_entry (&entry);
938 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
939 mutt_perror (_("Can't create filter"));
941 rfc1524_free_entry (&entry);
945 mutt_copy_stream (ifp, fpout);
946 safe_fclose (&fpout);
948 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
949 mutt_any_key_to_continue (NULL);
952 if (mutt_system (command) || option (OPTWAITKEY))
953 mutt_any_key_to_continue (NULL);
957 mutt_unlink (newfile);
958 else if (unlink_newfile)
961 rfc1524_free_entry (&entry);
965 if (!ascii_strcasecmp ("text/plain", type) ||
966 !ascii_strcasecmp ("application/postscript", type)) {
967 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
969 else if (mutt_can_decode (a)) {
970 /* decode and print */
977 mutt_mktemp (newfile);
978 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
980 debug_print (2, ("successfully decoded %s type attachment to %s\n",
983 if ((ifp = fopen (newfile, "r")) == NULL) {
984 mutt_perror ("fopen");
988 debug_print (2, ("successfully opened %s read-only\n", newfile));
992 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
993 mutt_perror (_("Can't create filter"));
998 debug_print (2, ("Filter created.\n"));
1000 mutt_copy_stream (ifp, fpout);
1002 safe_fclose (&fpout);
1005 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1006 mutt_any_key_to_continue (NULL);
1011 safe_fclose (&fpout);
1012 mutt_unlink (newfile);
1016 mutt_error _("I don't know how to print that!");
1022 int mutt_attach_check (HEADER* hdr) {
1024 char buf[LONG_STRING];
1027 regmatch_t pmatch[1];
1029 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1030 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1033 while (!found && fgets (buf, sizeof (buf), fp)) {
1036 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,