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>
16 #include <lib-lib/ascii.h>
17 #include <lib-lib/str.h>
18 #include <lib-lib/macros.h>
22 #include "recvattach.h"
23 #include "mutt_menu.h"
24 #include "mutt_curses.h"
31 #include "mutt_crypt.h"
33 #include "lib/debug.h"
44 int mutt_get_tmp_attachment (BODY * a)
47 char tempfile[_POSIX_PATH_MAX];
48 rfc1524_entry *entry = rfc1524_new_entry ();
49 FILE *fpin = NULL, *fpout = NULL;
55 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
56 rfc1524_mailcap_lookup (a, type, entry, 0);
57 rfc1524_expand_filename (entry->nametemplate, a->filename,
58 tempfile, sizeof (tempfile));
60 rfc1524_free_entry (&entry);
62 if (stat (a->filename, &st) == -1)
65 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
66 mutt_copy_stream (fpin, fpout);
67 str_replace (&a->filename, tempfile);
70 if (a->stamp >= st.st_mtime)
71 mutt_stamp_attachment (a);
74 mutt_perror(fpin ? tempfile : a->filename);
81 return a->unlink ? 0 : -1;
85 /* return 1 if require full screen redraw, 0 otherwise */
86 int mutt_compose_attachment (BODY * a)
90 char newfile[_POSIX_PATH_MAX] = "";
91 rfc1524_entry *entry = rfc1524_new_entry ();
92 short unlink_newfile = 0;
95 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
96 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
97 if (entry->composecommand || entry->composetypecommand) {
99 if (entry->composetypecommand)
100 strfcpy (command, entry->composetypecommand, sizeof (command));
102 strfcpy (command, entry->composecommand, sizeof (command));
103 if (rfc1524_expand_filename (entry->nametemplate,
104 a->filename, newfile, sizeof (newfile))) {
105 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
106 if (safe_symlink (a->filename, newfile) == -1) {
107 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
115 strfcpy (newfile, a->filename, sizeof (newfile));
117 if (rfc1524_expand_command (a, newfile, type,
118 command, sizeof (command))) {
119 /* For now, editing requires a file, no piping */
120 mutt_error _("Mailcap compose entry requires %%s");
126 if ((r = mutt_system (command)) == -1)
127 mutt_error (_("Error running \"%s\"!"), command);
129 if (r != -1 && entry->composetypecommand) {
132 char tempfile[_POSIX_PATH_MAX];
134 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
135 mutt_perror (_("Failure to open file to parse headers."));
140 b = mutt_read_mime_header (fp, 0);
143 mutt_free_parameter (&a->parameter);
144 a->parameter = b->parameter;
147 if (b->description) {
148 p_delete(&a->description);
149 a->description = b->description;
150 b->description = NULL;
153 p_delete(&a->form_name);
154 a->form_name = b->form_name;
158 /* Remove headers by copying out data to another file, then
159 * copying the file back */
160 fseeko (fp, b->offset, 0);
161 mutt_mktemp (tempfile);
162 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
163 mutt_perror (_("Failure to open file to strip headers."));
167 mutt_copy_stream (fp, tfp);
170 mutt_unlink (a->filename);
171 if (mutt_rename_file (tempfile, a->filename) != 0) {
172 mutt_perror (_("Failure to rename file."));
184 rfc1524_free_entry (&entry);
185 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
197 rfc1524_free_entry (&entry);
202 * Currently, this only works for send mode, as it assumes that the
203 * BODY->filename actually contains the information. I'm not sure
204 * we want to deal with editing attachments we've already received,
205 * so this should be ok.
207 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
210 int mutt_edit_attachment (BODY * a)
213 char command[STRING];
214 char newfile[_POSIX_PATH_MAX] = "";
215 rfc1524_entry *entry = rfc1524_new_entry ();
216 short unlink_newfile = 0;
219 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
220 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
221 if (entry->editcommand) {
223 strfcpy (command, entry->editcommand, sizeof (command));
224 if (rfc1524_expand_filename (entry->nametemplate,
225 a->filename, newfile, sizeof (newfile))) {
226 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
227 if (safe_symlink (a->filename, newfile) == -1) {
228 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
236 strfcpy (newfile, a->filename, sizeof (newfile));
238 if (rfc1524_expand_command (a, newfile, type,
239 command, sizeof (command))) {
240 /* For now, editing requires a file, no piping */
241 mutt_error _("Mailcap Edit entry requires %%s");
246 if (mutt_system (command) == -1) {
247 mutt_error (_("Error running \"%s\"!"), command);
253 else if (a->type == TYPETEXT) {
254 /* On text, default to editor */
255 mutt_edit_file (NONULL (Editor), a->filename);
258 rfc1524_free_entry (&entry);
259 mutt_error (_("No mailcap edit entry for %s"), type);
270 rfc1524_free_entry (&entry);
275 /* for compatibility with metamail */
276 static int is_mmnoask (const char *buf)
278 char tmp[LONG_STRING], *p, *q;
281 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
282 if (str_cmp (p, "1") == 0)
285 strfcpy (tmp, p, sizeof (tmp));
288 while ((p = strtok (p, ",")) != NULL) {
289 if ((q = strrchr (p, '/')) != NULL) {
290 if (*(q + 1) == '*') {
291 if (ascii_strncasecmp (buf, p, q - p) == 0)
295 if (ascii_strcasecmp (buf, p) == 0)
301 if (buf[lng] == '/' && str_ncasecmp (buf, p, lng) == 0)
312 void mutt_check_lookup_list (BODY * b, char *type, int len)
314 LIST *t = MimeLookupList;
317 for (; t; t = t->next) {
318 i = m_strlen(t->data) - 1;
319 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
320 ascii_strncasecmp (type, t->data, i) == 0) ||
321 ascii_strcasecmp (type, t->data) == 0) {
326 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
327 snprintf (type, len, "%s/%s",
328 n == TYPEAUDIO ? "audio" :
329 n == TYPEAPPLICATION ? "application" :
330 n == TYPEIMAGE ? "image" :
331 n == TYPEMESSAGE ? "message" :
332 n == TYPEMODEL ? "model" :
333 n == TYPEMULTIPART ? "multipart" :
334 n == TYPETEXT ? "text" :
335 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
336 debug_print (1, ("\"%s\" -> %s\n", b->filename, type));
339 p_delete(&tmp.subtype);
341 p_delete(&tmp.xtype);
346 int mutt_is_autoview (BODY * b, const char *type)
348 LIST *t = AutoViewList;
349 char _type[SHORT_STRING];
353 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
355 strncpy (_type, type, sizeof (_type));
357 mutt_check_lookup_list (b, _type, sizeof (_type));
360 if (mutt_needs_mailcap (b)) {
361 if (option (OPTIMPLICITAUTOVIEW))
364 if (is_mmnoask (type))
368 for (; t; t = t->next) {
369 i = m_strlen(t->data) - 1;
370 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
371 ascii_strncasecmp (type, t->data, i) == 0) ||
372 ascii_strcasecmp (type, t->data) == 0)
379 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
380 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
381 ATTACHPTR ** idx, short idxlen)
383 char tempfile[_POSIX_PATH_MAX] = "";
384 char pagerfile[_POSIX_PATH_MAX] = "";
390 char command[STRING];
391 char descrip[STRING];
393 rfc1524_entry *entry = NULL;
395 int unlink_tempfile = 0;
397 is_message = mutt_is_message_type (a->type, a->subtype);
398 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
399 !crypt_valid_passphrase (a->hdr->security))
401 use_mailcap = (flag == M_MAILCAP ||
402 (flag == M_REGULAR && mutt_needs_mailcap (a)));
403 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
406 entry = rfc1524_new_entry ();
407 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
408 if (flag == M_REGULAR) {
409 /* fallback to view as text */
410 rfc1524_free_entry (&entry);
411 mutt_error _("No matching mailcap entry found. Viewing as text.");
422 if (!entry->command) {
423 mutt_error _("MIME type not defined. Cannot view attachment.");
427 strfcpy (command, entry->command, sizeof (command));
430 fname = m_strdup(a->filename);
431 mutt_sanitize_filename (fname, 1);
436 if (rfc1524_expand_filename (entry->nametemplate, fname,
437 tempfile, sizeof (tempfile))) {
438 if (fp == NULL && str_cmp (tempfile, a->filename)) {
439 /* send case: the file is already there */
440 if (safe_symlink (a->filename, tempfile) == -1) {
441 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
443 strfcpy (tempfile, a->filename, sizeof (tempfile));
451 else if (fp == NULL) /* send case */
452 strfcpy (tempfile, a->filename, sizeof (tempfile));
455 /* recv case: we need to save the attachment to a file */
457 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
461 use_pipe = rfc1524_expand_command (a, tempfile, type,
462 command, sizeof (command));
463 use_pager = entry->copiousoutput;
467 if (fp && !use_mailcap && a->filename) {
469 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
470 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
473 mutt_mktemp (pagerfile);
478 int tempfd = -1, pagerfd = -1;
483 if (use_pager || use_pipe) {
485 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
487 mutt_perror ("open");
490 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
493 mutt_perror ("open");
497 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
498 use_pipe ? tempfd : -1,
499 use_pager ? pagerfd : -1,
507 mutt_error _("Cannot create filter");
514 snprintf (descrip, sizeof (descrip),
515 "---Command: %-20.20s Description: %s",
516 command, a->description);
518 snprintf (descrip, sizeof (descrip),
519 "---Command: %-30.30s Attachment: %s", command, type);
522 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
523 option (OPTWAITKEY))) && !use_pager)
524 mutt_any_key_to_continue (NULL);
531 /* interactive command */
532 if (mutt_system (command) ||
533 (entry->needsterminal && option (OPTWAITKEY)))
534 mutt_any_key_to_continue (NULL);
538 /* Don't use mailcap; the attachment is viewed in the pager */
540 if (flag == M_AS_TEXT) {
541 /* just let me see the raw data */
542 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
546 /* Use built-in handler */
547 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
548 * message in case of error */
549 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
550 unset_option (OPTVIEWATTACH);
553 unset_option (OPTVIEWATTACH);
557 strfcpy (descrip, a->description, sizeof (descrip));
558 else if (a->filename)
559 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
562 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
565 /* We only reach this point if there have been no errors */
570 memset (&info, 0, sizeof (info));
575 info.idxlen = idxlen;
578 rc = mutt_do_pager (descrip, pagerfile,
579 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE :
589 rfc1524_free_entry (&entry);
590 if (fp && tempfile[0])
591 mutt_unlink (tempfile);
592 else if (unlink_tempfile)
596 mutt_unlink (pagerfile);
601 /* returns 1 on success, 0 on error */
602 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
609 if (outfile && *outfile)
610 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
611 mutt_perror ("open");
622 memset (&s, 0, sizeof (STATE));
624 if (outfile && *outfile)
626 mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
628 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
631 mutt_perror (_("Can't create filter"));
637 mutt_decode_attachment (b, &s);
638 safe_fclose (&s.fpout);
645 if ((ifp = fopen (b->filename, "r")) == NULL) {
646 mutt_perror ("fopen");
647 if (outfile && *outfile) {
654 if (outfile && *outfile)
655 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
657 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
660 mutt_perror (_("Can't create filter"));
666 mutt_copy_stream (ifp, ofp);
675 if (outfile && *outfile)
679 * check for error exit from child process
681 if (mutt_wait_filter (thepid) != 0)
684 if (rv == 0 || option (OPTWAITKEY))
685 mutt_any_key_to_continue (NULL);
689 static FILE *mutt_save_attachment_open (char *path, int flags)
691 if (flags == M_SAVE_APPEND)
692 return fopen (path, "a");
693 /* be sure not to change the following fopen to safe_fopen
694 * as safe_fopen returns w/ an error if path exists
696 if (flags == M_SAVE_OVERWRITE)
697 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
699 return safe_fopen (path, "w");
702 /* returns 0 on success, -1 on error */
703 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
712 m->encoding != ENCBASE64 &&
713 m->encoding != ENCQUOTEDPRINTABLE &&
714 mutt_is_message_type (m->type, m->subtype)) {
715 /* message type attachments are written to mail folders. */
717 char buf[HUGE_STRING];
725 hn->msgno = hdr->msgno; /* required for MH/maildir */
728 fseeko (fp, m->offset, 0);
729 if (fgets (buf, sizeof (buf), fp) == NULL)
731 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
734 mx_open_new_message (&ctx, hn,
735 is_from (buf, NULL, 0,
736 NULL) ? 0 : M_ADD_FROM)) == NULL) {
737 mx_close_mailbox (&ctx, NULL);
740 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
742 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
743 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
744 && mx_commit_message (msg, &ctx) == 0)
749 mx_close_message (&msg);
750 mx_close_mailbox (&ctx, NULL);
754 /* In recv mode, extract from folder and decode */
758 memset (&s, 0, sizeof (s));
759 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
760 mutt_perror ("fopen");
763 fseeko ((s.fpin = fp), m->offset, 0);
764 mutt_decode_attachment (m, &s);
766 if (fclose (s.fpout) != 0) {
767 mutt_perror ("fclose");
773 /* In send mode, just copy file */
777 if ((ofp = fopen (m->filename, "r")) == NULL) {
778 mutt_perror ("fopen");
782 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
783 mutt_perror ("fopen");
788 if (mutt_copy_stream (ofp, nfp) == -1) {
789 mutt_error _("Write fault!");
802 /* returns 0 on success, -1 on error */
803 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
804 int displaying, int flags)
807 unsigned int saved_encoding = 0;
808 BODY *saved_parts = NULL;
809 HEADER *saved_hdr = NULL;
811 memset (&s, 0, sizeof (s));
812 s.flags = displaying;
814 if (flags == M_SAVE_APPEND)
815 s.fpout = fopen (path, "a");
816 else if (flags == M_SAVE_OVERWRITE)
817 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
819 s.fpout = safe_fopen (path, "w");
821 if (s.fpout == NULL) {
822 mutt_perror ("fopen");
827 /* When called from the compose menu, the attachment isn't parsed,
828 * so we need to do it here. */
831 if (stat (m->filename, &st) == -1) {
832 mutt_perror ("stat");
837 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
838 mutt_perror ("fopen");
842 saved_encoding = m->encoding;
843 if (!is_multipart (m))
844 m->encoding = ENC8BIT;
846 m->length = st.st_size;
848 saved_parts = m->parts;
850 mutt_parse_part (s.fpin, m);
852 if (m->noconv || is_multipart (m))
853 s.flags |= M_CHARCONV;
857 s.flags |= M_CHARCONV;
860 mutt_body_handler (m, &s);
865 m->encoding = saved_encoding;
867 mutt_free_header (&m->hdr);
868 m->parts = saved_parts;
877 /* Ok, the difference between send and receive:
878 * recv: BODY->filename is a suggested name, and Context|HEADER points
879 * to the attachment in mailbox which is encooded
880 * send: BODY->filename points to the un-encoded file which contains the
884 int mutt_print_attachment (FILE * fp, BODY * a)
886 char newfile[_POSIX_PATH_MAX] = "";
890 short unlink_newfile = 0;
892 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
894 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
895 char command[_POSIX_PATH_MAX + STRING];
896 rfc1524_entry *entry;
899 debug_print (2, ("Using mailcap...\n"));
901 entry = rfc1524_new_entry ();
902 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
903 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
904 newfile, sizeof (newfile))) {
906 if (safe_symlink (a->filename, newfile) == -1) {
907 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
909 rfc1524_free_entry (&entry);
912 strfcpy (newfile, a->filename, sizeof (newfile));
919 /* in recv mode, save file to newfile first */
921 mutt_save_attachment (fp, a, newfile, 0, NULL);
923 strfcpy (command, entry->printcommand, sizeof (command));
925 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
929 /* interactive program */
931 if ((ifp = fopen (newfile, "r")) == NULL) {
932 mutt_perror ("fopen");
933 rfc1524_free_entry (&entry);
937 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
938 mutt_perror (_("Can't create filter"));
940 rfc1524_free_entry (&entry);
944 mutt_copy_stream (ifp, fpout);
945 safe_fclose (&fpout);
947 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
948 mutt_any_key_to_continue (NULL);
951 if (mutt_system (command) || option (OPTWAITKEY))
952 mutt_any_key_to_continue (NULL);
956 mutt_unlink (newfile);
957 else if (unlink_newfile)
960 rfc1524_free_entry (&entry);
964 if (!ascii_strcasecmp ("text/plain", type) ||
965 !ascii_strcasecmp ("application/postscript", type)) {
966 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
968 else if (mutt_can_decode (a)) {
969 /* decode and print */
976 mutt_mktemp (newfile);
977 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
979 debug_print (2, ("successfully decoded %s type attachment to %s\n",
982 if ((ifp = fopen (newfile, "r")) == NULL) {
983 mutt_perror ("fopen");
987 debug_print (2, ("successfully opened %s read-only\n", newfile));
991 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
992 mutt_perror (_("Can't create filter"));
997 debug_print (2, ("Filter created.\n"));
999 mutt_copy_stream (ifp, fpout);
1001 safe_fclose (&fpout);
1004 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1005 mutt_any_key_to_continue (NULL);
1010 safe_fclose (&fpout);
1011 mutt_unlink (newfile);
1015 mutt_error _("I don't know how to print that!");
1021 int mutt_attach_check (HEADER* hdr) {
1023 char buf[LONG_STRING];
1026 regmatch_t pmatch[1];
1028 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1029 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1032 while (!found && fgets (buf, sizeof (buf), fp)) {
1035 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,