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.
18 #include "recvattach.h"
19 #include "mutt_menu.h"
20 #include "mutt_curses.h"
27 #include "mutt_crypt.h"
32 #include "lib/debug.h"
43 int mutt_get_tmp_attachment (BODY * a)
46 char tempfile[_POSIX_PATH_MAX];
47 rfc1524_entry *entry = rfc1524_new_entry ();
48 FILE *fpin = NULL, *fpout = NULL;
54 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
55 rfc1524_mailcap_lookup (a, type, entry, 0);
56 rfc1524_expand_filename (entry->nametemplate, a->filename,
57 tempfile, sizeof (tempfile));
59 rfc1524_free_entry (&entry);
61 if (stat (a->filename, &st) == -1)
64 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
65 mutt_copy_stream (fpin, fpout);
66 str_replace (&a->filename, tempfile);
69 if (a->stamp >= st.st_mtime)
70 mutt_stamp_attachment (a);
73 mutt_perror(fpin ? tempfile : a->filename);
80 return a->unlink ? 0 : -1;
84 /* return 1 if require full screen redraw, 0 otherwise */
85 int mutt_compose_attachment (BODY * a)
89 char newfile[_POSIX_PATH_MAX] = "";
90 rfc1524_entry *entry = rfc1524_new_entry ();
91 short unlink_newfile = 0;
94 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
95 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
96 if (entry->composecommand || entry->composetypecommand) {
98 if (entry->composetypecommand)
99 strfcpy (command, entry->composetypecommand, sizeof (command));
101 strfcpy (command, entry->composecommand, sizeof (command));
102 if (rfc1524_expand_filename (entry->nametemplate,
103 a->filename, newfile, sizeof (newfile))) {
104 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
105 if (safe_symlink (a->filename, newfile) == -1) {
106 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
114 strfcpy (newfile, a->filename, sizeof (newfile));
116 if (rfc1524_expand_command (a, newfile, type,
117 command, sizeof (command))) {
118 /* For now, editing requires a file, no piping */
119 mutt_error _("Mailcap compose entry requires %%s");
125 if ((r = mutt_system (command)) == -1)
126 mutt_error (_("Error running \"%s\"!"), command);
128 if (r != -1 && entry->composetypecommand) {
131 char tempfile[_POSIX_PATH_MAX];
133 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
134 mutt_perror (_("Failure to open file to parse headers."));
139 b = mutt_read_mime_header (fp, 0);
142 mutt_free_parameter (&a->parameter);
143 a->parameter = b->parameter;
146 if (b->description) {
147 mem_free (&a->description);
148 a->description = b->description;
149 b->description = NULL;
152 mem_free (&a->form_name);
153 a->form_name = b->form_name;
157 /* Remove headers by copying out data to another file, then
158 * copying the file back */
159 fseeko (fp, b->offset, 0);
160 mutt_mktemp (tempfile);
161 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
162 mutt_perror (_("Failure to open file to strip headers."));
166 mutt_copy_stream (fp, tfp);
169 mutt_unlink (a->filename);
170 if (mutt_rename_file (tempfile, a->filename) != 0) {
171 mutt_perror (_("Failure to rename file."));
183 rfc1524_free_entry (&entry);
184 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
196 rfc1524_free_entry (&entry);
201 * Currently, this only works for send mode, as it assumes that the
202 * BODY->filename actually contains the information. I'm not sure
203 * we want to deal with editing attachments we've already received,
204 * so this should be ok.
206 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
209 int mutt_edit_attachment (BODY * a)
212 char command[STRING];
213 char newfile[_POSIX_PATH_MAX] = "";
214 rfc1524_entry *entry = rfc1524_new_entry ();
215 short unlink_newfile = 0;
218 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
219 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
220 if (entry->editcommand) {
222 strfcpy (command, entry->editcommand, sizeof (command));
223 if (rfc1524_expand_filename (entry->nametemplate,
224 a->filename, newfile, sizeof (newfile))) {
225 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
226 if (safe_symlink (a->filename, newfile) == -1) {
227 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
235 strfcpy (newfile, a->filename, sizeof (newfile));
237 if (rfc1524_expand_command (a, newfile, type,
238 command, sizeof (command))) {
239 /* For now, editing requires a file, no piping */
240 mutt_error _("Mailcap Edit entry requires %%s");
245 if (mutt_system (command) == -1) {
246 mutt_error (_("Error running \"%s\"!"), command);
252 else if (a->type == TYPETEXT) {
253 /* On text, default to editor */
254 mutt_edit_file (NONULL (Editor), a->filename);
257 rfc1524_free_entry (&entry);
258 mutt_error (_("No mailcap edit entry for %s"), type);
269 rfc1524_free_entry (&entry);
274 /* for compatibility with metamail */
275 static int is_mmnoask (const char *buf)
277 char tmp[LONG_STRING], *p, *q;
280 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
281 if (str_cmp (p, "1") == 0)
284 strfcpy (tmp, p, sizeof (tmp));
287 while ((p = strtok (p, ",")) != NULL) {
288 if ((q = strrchr (p, '/')) != NULL) {
289 if (*(q + 1) == '*') {
290 if (ascii_strncasecmp (buf, p, q - p) == 0)
294 if (ascii_strcasecmp (buf, p) == 0)
300 if (buf[lng] == '/' && str_ncasecmp (buf, p, lng) == 0)
311 void mutt_check_lookup_list (BODY * b, char *type, int len)
313 LIST *t = MimeLookupList;
316 for (; t; t = t->next) {
317 i = str_len (t->data) - 1;
318 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
319 ascii_strncasecmp (type, t->data, i) == 0) ||
320 ascii_strcasecmp (type, t->data) == 0) {
325 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
326 snprintf (type, len, "%s/%s",
327 n == TYPEAUDIO ? "audio" :
328 n == TYPEAPPLICATION ? "application" :
329 n == TYPEIMAGE ? "image" :
330 n == TYPEMESSAGE ? "message" :
331 n == TYPEMODEL ? "model" :
332 n == TYPEMULTIPART ? "multipart" :
333 n == TYPETEXT ? "text" :
334 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
335 debug_print (1, ("\"%s\" -> %s\n", b->filename, type));
338 mem_free (&tmp.subtype);
340 mem_free (&tmp.xtype);
345 int mutt_is_autoview (BODY * b, const char *type)
347 LIST *t = AutoViewList;
348 char _type[SHORT_STRING];
352 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
354 strncpy (_type, type, sizeof (_type));
356 mutt_check_lookup_list (b, _type, sizeof (_type));
359 if (mutt_needs_mailcap (b)) {
360 if (option (OPTIMPLICITAUTOVIEW))
363 if (is_mmnoask (type))
367 for (; t; t = t->next) {
368 i = str_len (t->data) - 1;
369 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
370 ascii_strncasecmp (type, t->data, i) == 0) ||
371 ascii_strcasecmp (type, t->data) == 0)
378 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
379 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
380 ATTACHPTR ** idx, short idxlen)
382 char tempfile[_POSIX_PATH_MAX] = "";
383 char pagerfile[_POSIX_PATH_MAX] = "";
389 char command[STRING];
390 char descrip[STRING];
392 rfc1524_entry *entry = NULL;
394 int unlink_tempfile = 0;
396 is_message = mutt_is_message_type (a->type, a->subtype);
397 if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
398 !crypt_valid_passphrase (a->hdr->security))
400 use_mailcap = (flag == M_MAILCAP ||
401 (flag == M_REGULAR && mutt_needs_mailcap (a)));
402 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
405 entry = rfc1524_new_entry ();
406 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
407 if (flag == M_REGULAR) {
408 /* fallback to view as text */
409 rfc1524_free_entry (&entry);
410 mutt_error _("No matching mailcap entry found. Viewing as text.");
421 if (!entry->command) {
422 mutt_error _("MIME type not defined. Cannot view attachment.");
426 strfcpy (command, entry->command, sizeof (command));
429 fname = str_dup (a->filename);
430 mutt_sanitize_filename (fname, 1);
435 if (rfc1524_expand_filename (entry->nametemplate, fname,
436 tempfile, sizeof (tempfile))) {
437 if (fp == NULL && str_cmp (tempfile, a->filename)) {
438 /* send case: the file is already there */
439 if (safe_symlink (a->filename, tempfile) == -1) {
440 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
442 strfcpy (tempfile, a->filename, sizeof (tempfile));
450 else if (fp == NULL) /* send case */
451 strfcpy (tempfile, a->filename, sizeof (tempfile));
454 /* recv case: we need to save the attachment to a file */
456 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
460 use_pipe = rfc1524_expand_command (a, tempfile, type,
461 command, sizeof (command));
462 use_pager = entry->copiousoutput;
466 if (fp && !use_mailcap && a->filename) {
468 strfcpy (pagerfile, a->filename, sizeof (pagerfile));
469 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
472 mutt_mktemp (pagerfile);
477 int tempfd = -1, pagerfd = -1;
482 if (use_pager || use_pipe) {
484 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
486 mutt_perror ("open");
489 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
492 mutt_perror ("open");
496 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
497 use_pipe ? tempfd : -1,
498 use_pager ? pagerfd : -1,
506 mutt_error _("Cannot create filter");
513 snprintf (descrip, sizeof (descrip),
514 "---Command: %-20.20s Description: %s",
515 command, a->description);
517 snprintf (descrip, sizeof (descrip),
518 "---Command: %-30.30s Attachment: %s", command, type);
521 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
522 option (OPTWAITKEY))) && !use_pager)
523 mutt_any_key_to_continue (NULL);
530 /* interactive command */
531 if (mutt_system (command) ||
532 (entry->needsterminal && option (OPTWAITKEY)))
533 mutt_any_key_to_continue (NULL);
537 /* Don't use mailcap; the attachment is viewed in the pager */
539 if (flag == M_AS_TEXT) {
540 /* just let me see the raw data */
541 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
545 /* Use built-in handler */
546 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
547 * message in case of error */
548 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
549 unset_option (OPTVIEWATTACH);
552 unset_option (OPTVIEWATTACH);
556 strfcpy (descrip, a->description, sizeof (descrip));
557 else if (a->filename)
558 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
561 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
564 /* We only reach this point if there have been no errors */
569 memset (&info, 0, sizeof (info));
574 info.idxlen = idxlen;
577 rc = mutt_do_pager (descrip, pagerfile,
578 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE :
588 rfc1524_free_entry (&entry);
589 if (fp && tempfile[0])
590 mutt_unlink (tempfile);
591 else if (unlink_tempfile)
595 mutt_unlink (pagerfile);
600 /* returns 1 on success, 0 on error */
601 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
608 if (outfile && *outfile)
609 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
610 mutt_perror ("open");
621 memset (&s, 0, sizeof (STATE));
623 if (outfile && *outfile)
625 mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1);
627 thepid = mutt_create_filter (path, &s.fpout, NULL, NULL);
630 mutt_perror (_("Can't create filter"));
636 mutt_decode_attachment (b, &s);
637 safe_fclose (&s.fpout);
644 if ((ifp = fopen (b->filename, "r")) == NULL) {
645 mutt_perror ("fopen");
646 if (outfile && *outfile) {
653 if (outfile && *outfile)
654 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
656 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
659 mutt_perror (_("Can't create filter"));
665 mutt_copy_stream (ifp, ofp);
674 if (outfile && *outfile)
678 * check for error exit from child process
680 if (mutt_wait_filter (thepid) != 0)
683 if (rv == 0 || option (OPTWAITKEY))
684 mutt_any_key_to_continue (NULL);
688 static FILE *mutt_save_attachment_open (char *path, int flags)
690 if (flags == M_SAVE_APPEND)
691 return fopen (path, "a");
692 /* be sure not to change the following fopen to safe_fopen
693 * as safe_fopen returns w/ an error if path exists
695 if (flags == M_SAVE_OVERWRITE)
696 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
698 return safe_fopen (path, "w");
701 /* returns 0 on success, -1 on error */
702 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
711 m->encoding != ENCBASE64 &&
712 m->encoding != ENCQUOTEDPRINTABLE &&
713 mutt_is_message_type (m->type, m->subtype)) {
714 /* message type attachments are written to mail folders. */
716 char buf[HUGE_STRING];
724 hn->msgno = hdr->msgno; /* required for MH/maildir */
727 fseeko (fp, m->offset, 0);
728 if (fgets (buf, sizeof (buf), fp) == NULL)
730 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
733 mx_open_new_message (&ctx, hn,
734 is_from (buf, NULL, 0,
735 NULL) ? 0 : M_ADD_FROM)) == NULL) {
736 mx_close_mailbox (&ctx, NULL);
739 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
741 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
742 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
743 && mx_commit_message (msg, &ctx) == 0)
748 mx_close_message (&msg);
749 mx_close_mailbox (&ctx, NULL);
753 /* In recv mode, extract from folder and decode */
757 memset (&s, 0, sizeof (s));
758 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
759 mutt_perror ("fopen");
762 fseeko ((s.fpin = fp), m->offset, 0);
763 mutt_decode_attachment (m, &s);
765 if (fclose (s.fpout) != 0) {
766 mutt_perror ("fclose");
772 /* In send mode, just copy file */
776 if ((ofp = fopen (m->filename, "r")) == NULL) {
777 mutt_perror ("fopen");
781 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
782 mutt_perror ("fopen");
787 if (mutt_copy_stream (ofp, nfp) == -1) {
788 mutt_error _("Write fault!");
801 /* returns 0 on success, -1 on error */
802 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
803 int displaying, int flags)
806 unsigned int saved_encoding = 0;
807 BODY *saved_parts = NULL;
808 HEADER *saved_hdr = NULL;
810 memset (&s, 0, sizeof (s));
811 s.flags = displaying;
813 if (flags == M_SAVE_APPEND)
814 s.fpout = fopen (path, "a");
815 else if (flags == M_SAVE_OVERWRITE)
816 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
818 s.fpout = safe_fopen (path, "w");
820 if (s.fpout == NULL) {
821 mutt_perror ("fopen");
826 /* When called from the compose menu, the attachment isn't parsed,
827 * so we need to do it here. */
830 if (stat (m->filename, &st) == -1) {
831 mutt_perror ("stat");
836 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
837 mutt_perror ("fopen");
841 saved_encoding = m->encoding;
842 if (!is_multipart (m))
843 m->encoding = ENC8BIT;
845 m->length = st.st_size;
847 saved_parts = m->parts;
849 mutt_parse_part (s.fpin, m);
851 if (m->noconv || is_multipart (m))
852 s.flags |= M_CHARCONV;
856 s.flags |= M_CHARCONV;
859 mutt_body_handler (m, &s);
864 m->encoding = saved_encoding;
866 mutt_free_header (&m->hdr);
867 m->parts = saved_parts;
876 /* Ok, the difference between send and receive:
877 * recv: BODY->filename is a suggested name, and Context|HEADER points
878 * to the attachment in mailbox which is encooded
879 * send: BODY->filename points to the un-encoded file which contains the
883 int mutt_print_attachment (FILE * fp, BODY * a)
885 char newfile[_POSIX_PATH_MAX] = "";
889 short unlink_newfile = 0;
891 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
893 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
894 char command[_POSIX_PATH_MAX + STRING];
895 rfc1524_entry *entry;
898 debug_print (2, ("Using mailcap...\n"));
900 entry = rfc1524_new_entry ();
901 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
902 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
903 newfile, sizeof (newfile))) {
905 if (safe_symlink (a->filename, newfile) == -1) {
906 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
908 rfc1524_free_entry (&entry);
911 strfcpy (newfile, a->filename, sizeof (newfile));
918 /* in recv mode, save file to newfile first */
920 mutt_save_attachment (fp, a, newfile, 0, NULL);
922 strfcpy (command, entry->printcommand, sizeof (command));
924 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
928 /* interactive program */
930 if ((ifp = fopen (newfile, "r")) == NULL) {
931 mutt_perror ("fopen");
932 rfc1524_free_entry (&entry);
936 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
937 mutt_perror (_("Can't create filter"));
939 rfc1524_free_entry (&entry);
943 mutt_copy_stream (ifp, fpout);
944 safe_fclose (&fpout);
946 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
947 mutt_any_key_to_continue (NULL);
950 if (mutt_system (command) || option (OPTWAITKEY))
951 mutt_any_key_to_continue (NULL);
955 mutt_unlink (newfile);
956 else if (unlink_newfile)
959 rfc1524_free_entry (&entry);
963 if (!ascii_strcasecmp ("text/plain", type) ||
964 !ascii_strcasecmp ("application/postscript", type)) {
965 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
967 else if (mutt_can_decode (a)) {
968 /* decode and print */
975 mutt_mktemp (newfile);
976 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
978 debug_print (2, ("successfully decoded %s type attachment to %s\n",
981 if ((ifp = fopen (newfile, "r")) == NULL) {
982 mutt_perror ("fopen");
986 debug_print (2, ("successfully opened %s read-only\n", newfile));
990 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
991 mutt_perror (_("Can't create filter"));
996 debug_print (2, ("Filter created.\n"));
998 mutt_copy_stream (ifp, fpout);
1000 safe_fclose (&fpout);
1003 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1004 mutt_any_key_to_continue (NULL);
1009 safe_fclose (&fpout);
1010 mutt_unlink (newfile);
1014 mutt_error _("I don't know how to print that!");
1020 int mutt_attach_check (HEADER* hdr) {
1022 char buf[LONG_STRING];
1025 regmatch_t pmatch[1];
1027 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1028 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1031 while (!found && fgets (buf, sizeof (buf), fp)) {
1034 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,