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.
24 #include <lib-lib/mem.h>
25 #include <lib-lib/ascii.h>
26 #include <lib-lib/str.h>
27 #include <lib-lib/file.h>
28 #include <lib-lib/macros.h>
29 #include <lib-lib/debug.h>
31 #include <lib-sys/unix.h>
33 #include <lib-mime/mime.h>
35 #include <lib-ui/curses.h>
36 #include <lib-ui/menu.h>
40 #include "recvattach.h"
46 #include <lib-crypt/crypt.h>
48 int mutt_get_tmp_attachment (BODY * a)
51 char tempfile[_POSIX_PATH_MAX];
52 rfc1524_entry *entry = rfc1524_new_entry ();
53 FILE *fpin = NULL, *fpout = NULL;
59 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
60 rfc1524_mailcap_lookup (a, type, entry, 0);
61 rfc1524_expand_filename (entry->nametemplate, a->filename,
62 tempfile, sizeof (tempfile));
64 rfc1524_free_entry (&entry);
66 if (stat (a->filename, &st) == -1)
69 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
70 mutt_copy_stream (fpin, fpout);
71 m_strreplace(&a->filename, tempfile);
74 if (a->stamp >= st.st_mtime)
75 mutt_stamp_attachment (a);
78 mutt_perror(fpin ? tempfile : a->filename);
85 return a->unlink ? 0 : -1;
89 /* return 1 if require full screen redraw, 0 otherwise */
90 int mutt_compose_attachment (BODY * a)
94 char newfile[_POSIX_PATH_MAX] = "";
95 rfc1524_entry *entry = rfc1524_new_entry ();
96 short unlink_newfile = 0;
99 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
100 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
101 if (entry->composecommand || entry->composetypecommand) {
103 if (entry->composetypecommand)
104 m_strcpy(command, sizeof(command), entry->composetypecommand);
106 m_strcpy(command, sizeof(command), entry->composecommand);
107 if (rfc1524_expand_filename (entry->nametemplate,
108 a->filename, newfile, sizeof (newfile))) {
109 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
110 if (safe_symlink (a->filename, newfile) == -1) {
111 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
119 m_strcpy(newfile, sizeof(newfile), a->filename);
121 if (rfc1524_expand_command (a, newfile, type,
122 command, sizeof (command))) {
123 /* For now, editing requires a file, no piping */
124 mutt_error _("Mailcap compose entry requires %%s");
130 if ((r = mutt_system (command)) == -1)
131 mutt_error (_("Error running \"%s\"!"), command);
133 if (r != -1 && entry->composetypecommand) {
136 char tempfile[_POSIX_PATH_MAX];
138 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
139 mutt_perror (_("Failure to open file to parse headers."));
144 b = mutt_read_mime_header (fp, 0);
147 parameter_list_wipe(&a->parameter);
148 a->parameter = b->parameter;
151 if (b->description) {
152 p_delete(&a->description);
153 a->description = b->description;
154 b->description = NULL;
157 p_delete(&a->form_name);
158 a->form_name = b->form_name;
162 /* Remove headers by copying out data to another file, then
163 * copying the file back */
164 fseeko (fp, b->offset, 0);
165 mutt_mktemp (tempfile);
166 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
167 mutt_perror (_("Failure to open file to strip headers."));
171 mutt_copy_stream (fp, tfp);
174 mutt_unlink (a->filename);
175 if (mutt_rename_file (tempfile, a->filename) != 0) {
176 mutt_perror (_("Failure to rename file."));
188 rfc1524_free_entry (&entry);
189 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
201 rfc1524_free_entry (&entry);
206 * Currently, this only works for send mode, as it assumes that the
207 * BODY->filename actually contains the information. I'm not sure
208 * we want to deal with editing attachments we've already received,
209 * so this should be ok.
211 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
214 int mutt_edit_attachment (BODY * a)
217 char command[STRING];
218 char newfile[_POSIX_PATH_MAX] = "";
219 rfc1524_entry *entry = rfc1524_new_entry ();
220 short unlink_newfile = 0;
223 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
224 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
225 if (entry->editcommand) {
227 m_strcpy(command, sizeof(command), entry->editcommand);
228 if (rfc1524_expand_filename (entry->nametemplate,
229 a->filename, newfile, sizeof (newfile))) {
230 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
231 if (safe_symlink (a->filename, newfile) == -1) {
232 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
240 m_strcpy(newfile, sizeof(newfile), a->filename);
242 if (rfc1524_expand_command (a, newfile, type,
243 command, sizeof (command))) {
244 /* For now, editing requires a file, no piping */
245 mutt_error _("Mailcap Edit entry requires %%s");
250 if (mutt_system (command) == -1) {
251 mutt_error (_("Error running \"%s\"!"), command);
257 else if (a->type == TYPETEXT) {
258 /* On text, default to editor */
259 mutt_edit_file (NONULL (Editor), a->filename);
262 rfc1524_free_entry (&entry);
263 mutt_error (_("No mailcap edit entry for %s"), type);
274 rfc1524_free_entry (&entry);
279 /* for compatibility with metamail */
280 static int is_mmnoask (const char *buf)
282 char tmp[LONG_STRING], *p, *q;
285 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
286 if (m_strcmp(p, "1") == 0)
289 m_strcpy(tmp, sizeof(tmp), p);
292 while ((p = strtok (p, ",")) != NULL) {
293 if ((q = strrchr (p, '/')) != NULL) {
294 if (*(q + 1) == '*') {
295 if (ascii_strncasecmp (buf, p, q - p) == 0)
299 if (ascii_strcasecmp (buf, p) == 0)
305 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
316 void mutt_check_lookup_list (BODY * b, char *type, int len)
318 string_list_t *t = MimeLookupList;
321 for (; t; t = t->next) {
322 i = m_strlen(t->data) - 1;
323 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
324 ascii_strncasecmp (type, t->data, i) == 0) ||
325 ascii_strcasecmp (type, t->data) == 0) {
332 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
333 snprintf (type, len, "%s/%s",
334 n == TYPEAUDIO ? "audio" :
335 n == TYPEAPPLICATION ? "application" :
336 n == TYPEIMAGE ? "image" :
337 n == TYPEMESSAGE ? "message" :
338 n == TYPEMODEL ? "model" :
339 n == TYPEMULTIPART ? "multipart" :
340 n == TYPETEXT ? "text" :
341 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
342 debug_print (1, ("\"%s\" -> %s\n", b->filename, type));
345 p_delete(&tmp.subtype);
347 p_delete(&tmp.xtype);
352 int mutt_is_autoview (BODY * b, const char *type)
354 string_list_t *t = AutoViewList;
355 char _type[SHORT_STRING];
359 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
361 m_strcpy(_type, sizeof(_type), type);
363 mutt_check_lookup_list (b, _type, sizeof (_type));
366 if (mutt_needs_mailcap (b)) {
367 if (option (OPTIMPLICITAUTOVIEW))
370 if (is_mmnoask (type))
374 for (; t; t = t->next) {
375 i = m_strlen(t->data) - 1;
376 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
377 ascii_strncasecmp (type, t->data, i) == 0) ||
378 ascii_strcasecmp (type, t->data) == 0)
385 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
386 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
387 ATTACHPTR ** idx, short idxlen)
389 char tempfile[_POSIX_PATH_MAX] = "";
390 char pagerfile[_POSIX_PATH_MAX] = "";
396 char command[STRING];
397 char descrip[STRING];
399 rfc1524_entry *entry = NULL;
401 int unlink_tempfile = 0;
403 is_message = mutt_is_message_type (a->type, a->subtype);
404 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
405 !crypt_valid_passphrase (a->hdr->security))
407 use_mailcap = (flag == M_MAILCAP ||
408 (flag == M_REGULAR && mutt_needs_mailcap (a)));
409 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
412 entry = rfc1524_new_entry ();
413 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
414 if (flag == M_REGULAR) {
415 /* fallback to view as text */
416 rfc1524_free_entry (&entry);
417 mutt_error _("No matching mailcap entry found. Viewing as text.");
428 if (!entry->command) {
429 mutt_error _("MIME type not defined. Cannot view attachment.");
433 m_strcpy(command, sizeof(command), entry->command);
436 fname = m_strdup(a->filename);
437 mutt_sanitize_filename (fname, 1);
442 if (rfc1524_expand_filename (entry->nametemplate, fname,
443 tempfile, sizeof (tempfile))) {
444 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
445 /* send case: the file is already there */
446 if (safe_symlink (a->filename, tempfile) == -1) {
447 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
449 m_strcpy(tempfile, sizeof(tempfile), a->filename);
457 else if (fp == NULL) /* send case */
458 m_strcpy(tempfile, sizeof(tempfile), a->filename);
461 /* recv case: we need to save the attachment to a file */
463 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
467 use_pipe = rfc1524_expand_command (a, tempfile, type,
468 command, sizeof (command));
469 use_pager = entry->copiousoutput;
473 if (fp && !use_mailcap && a->filename) {
475 m_strcpy(pagerfile, sizeof(pagerfile), a->filename);
476 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
479 mutt_mktemp (pagerfile);
484 int tempfd = -1, pagerfd = -1;
489 if (use_pager || use_pipe) {
491 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
493 mutt_perror ("open");
496 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
499 mutt_perror ("open");
503 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
504 use_pipe ? tempfd : -1,
505 use_pager ? pagerfd : -1,
513 mutt_error _("Cannot create filter");
520 snprintf (descrip, sizeof (descrip),
521 "---Command: %-20.20s Description: %s",
522 command, a->description);
524 snprintf (descrip, sizeof (descrip),
525 "---Command: %-30.30s Attachment: %s", command, type);
528 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
529 option (OPTWAITKEY))) && !use_pager)
530 mutt_any_key_to_continue (NULL);
537 /* interactive command */
538 if (mutt_system (command) ||
539 (entry->needsterminal && option (OPTWAITKEY)))
540 mutt_any_key_to_continue (NULL);
544 /* Don't use mailcap; the attachment is viewed in the pager */
546 if (flag == M_AS_TEXT) {
547 /* just let me see the raw data */
548 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
552 /* Use built-in handler */
553 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
554 * message in case of error */
555 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
556 unset_option (OPTVIEWATTACH);
559 unset_option (OPTVIEWATTACH);
563 m_strcpy(descrip, sizeof(descrip), a->description);
564 else if (a->filename)
565 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
568 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
571 /* We only reach this point if there have been no errors */
581 info.idxlen = idxlen;
584 rc = mutt_do_pager(descrip, pagerfile,
585 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
595 rfc1524_free_entry (&entry);
596 if (fp && tempfile[0])
597 mutt_unlink (tempfile);
598 else if (unlink_tempfile)
602 mutt_unlink (pagerfile);
607 /* returns 1 on success, 0 on error */
608 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
615 if (outfile && *outfile)
616 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
617 mutt_perror ("open");
628 if (outfile && *outfile)
629 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
631 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
634 mutt_perror (_("Can't create filter"));
640 mutt_decode_attachment (b, &s);
641 safe_fclose (&s.fpout);
648 if ((ifp = fopen (b->filename, "r")) == NULL) {
649 mutt_perror ("fopen");
650 if (outfile && *outfile) {
657 if (outfile && *outfile)
658 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
660 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
663 mutt_perror (_("Can't create filter"));
669 mutt_copy_stream (ifp, ofp);
678 if (outfile && *outfile)
682 * check for error exit from child process
684 if (mutt_wait_filter (thepid) != 0)
687 if (rv == 0 || option (OPTWAITKEY))
688 mutt_any_key_to_continue (NULL);
692 static FILE *mutt_save_attachment_open (char *path, int flags)
694 if (flags == M_SAVE_APPEND)
695 return fopen (path, "a");
696 /* be sure not to change the following fopen to safe_fopen
697 * as safe_fopen returns w/ an error if path exists
699 if (flags == M_SAVE_OVERWRITE)
700 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
702 return safe_fopen (path, "w");
705 /* returns 0 on success, -1 on error */
706 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
715 m->encoding != ENCBASE64 &&
716 m->encoding != ENCQUOTEDPRINTABLE &&
717 mutt_is_message_type (m->type, m->subtype)) {
718 /* message type attachments are written to mail folders. */
720 char buf[HUGE_STRING];
728 hn->msgno = hdr->msgno; /* required for MH/maildir */
731 fseeko (fp, m->offset, 0);
732 if (fgets (buf, sizeof (buf), fp) == NULL)
734 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
737 mx_open_new_message (&ctx, hn,
738 is_from (buf, NULL, 0,
739 NULL) ? 0 : M_ADD_FROM)) == NULL) {
740 mx_close_mailbox (&ctx, NULL);
743 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
745 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
746 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
747 && mx_commit_message (msg, &ctx) == 0)
752 mx_close_message (&msg);
753 mx_close_mailbox (&ctx, NULL);
757 /* In recv mode, extract from folder and decode */
762 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
763 mutt_perror ("fopen");
766 fseeko ((s.fpin = fp), m->offset, 0);
767 mutt_decode_attachment (m, &s);
769 if (fclose (s.fpout) != 0) {
770 mutt_perror ("fclose");
776 /* In send mode, just copy file */
780 if ((ofp = fopen (m->filename, "r")) == NULL) {
781 mutt_perror ("fopen");
785 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
786 mutt_perror ("fopen");
791 if (mutt_copy_stream (ofp, nfp) == -1) {
792 mutt_error _("Write fault!");
805 /* returns 0 on success, -1 on error */
806 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
807 int displaying, int flags)
810 unsigned int saved_encoding = 0;
811 BODY *saved_parts = NULL;
812 HEADER *saved_hdr = NULL;
815 s.flags = displaying;
817 if (flags == M_SAVE_APPEND)
818 s.fpout = fopen (path, "a");
819 else if (flags == M_SAVE_OVERWRITE)
820 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
822 s.fpout = safe_fopen (path, "w");
824 if (s.fpout == NULL) {
825 mutt_perror ("fopen");
830 /* When called from the compose menu, the attachment isn't parsed,
831 * so we need to do it here. */
834 if (stat (m->filename, &st) == -1) {
835 mutt_perror ("stat");
840 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
841 mutt_perror ("fopen");
845 saved_encoding = m->encoding;
846 if (!is_multipart (m))
847 m->encoding = ENC8BIT;
849 m->length = st.st_size;
851 saved_parts = m->parts;
853 mutt_parse_part (s.fpin, m);
855 if (m->noconv || is_multipart (m))
856 s.flags |= M_CHARCONV;
860 s.flags |= M_CHARCONV;
863 mutt_body_handler (m, &s);
868 m->encoding = saved_encoding;
870 header_delete(&m->hdr);
871 m->parts = saved_parts;
880 /* Ok, the difference between send and receive:
881 * recv: BODY->filename is a suggested name, and Context|HEADER points
882 * to the attachment in mailbox which is encooded
883 * send: BODY->filename points to the un-encoded file which contains the
887 int mutt_print_attachment (FILE * fp, BODY * a)
889 char newfile[_POSIX_PATH_MAX] = "";
893 short unlink_newfile = 0;
895 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
897 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
898 char command[_POSIX_PATH_MAX + STRING];
899 rfc1524_entry *entry;
902 debug_print (2, ("Using mailcap...\n"));
904 entry = rfc1524_new_entry ();
905 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
906 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
907 newfile, sizeof (newfile))) {
909 if (safe_symlink (a->filename, newfile) == -1) {
910 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
912 rfc1524_free_entry (&entry);
915 m_strcpy(newfile, sizeof(newfile), a->filename);
922 /* in recv mode, save file to newfile first */
924 mutt_save_attachment (fp, a, newfile, 0, NULL);
926 m_strcpy(command, sizeof(command), entry->printcommand);
927 piped = rfc1524_expand_command(a, newfile, type, command,
932 /* interactive program */
934 if ((ifp = fopen (newfile, "r")) == NULL) {
935 mutt_perror ("fopen");
936 rfc1524_free_entry (&entry);
940 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
941 mutt_perror (_("Can't create filter"));
943 rfc1524_free_entry (&entry);
947 mutt_copy_stream (ifp, fpout);
948 safe_fclose (&fpout);
950 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
951 mutt_any_key_to_continue (NULL);
954 if (mutt_system (command) || option (OPTWAITKEY))
955 mutt_any_key_to_continue (NULL);
959 mutt_unlink (newfile);
960 else if (unlink_newfile)
963 rfc1524_free_entry (&entry);
967 if (!ascii_strcasecmp ("text/plain", type) ||
968 !ascii_strcasecmp ("application/postscript", type)) {
969 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
971 else if (mutt_can_decode (a)) {
972 /* decode and print */
979 mutt_mktemp (newfile);
980 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
982 debug_print (2, ("successfully decoded %s type attachment to %s\n",
985 if ((ifp = fopen (newfile, "r")) == NULL) {
986 mutt_perror ("fopen");
990 debug_print (2, ("successfully opened %s read-only\n", newfile));
994 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
995 mutt_perror (_("Can't create filter"));
1000 debug_print (2, ("Filter created.\n"));
1002 mutt_copy_stream (ifp, fpout);
1004 safe_fclose (&fpout);
1007 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1008 mutt_any_key_to_continue (NULL);
1013 safe_fclose (&fpout);
1014 mutt_unlink (newfile);
1018 mutt_error _("I don't know how to print that!");
1024 int mutt_attach_check (HEADER* hdr) {
1026 char buf[LONG_STRING];
1029 regmatch_t pmatch[1];
1031 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1032 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1035 while (!found && fgets (buf, sizeof (buf), fp)) {
1038 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,