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/file.h>
19 #include <lib-lib/macros.h>
21 #include <lib-mime/mime.h>
23 #include <lib-ui/curses.h>
27 #include "recvattach.h"
28 #include "mutt_menu.h"
34 #include <lib-crypt/crypt.h>
36 #include "lib/debug.h"
47 int mutt_get_tmp_attachment (BODY * a)
50 char tempfile[_POSIX_PATH_MAX];
51 rfc1524_entry *entry = rfc1524_new_entry ();
52 FILE *fpin = NULL, *fpout = NULL;
58 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
59 rfc1524_mailcap_lookup (a, type, entry, 0);
60 rfc1524_expand_filename (entry->nametemplate, a->filename,
61 tempfile, sizeof (tempfile));
63 rfc1524_free_entry (&entry);
65 if (stat (a->filename, &st) == -1)
68 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
69 mutt_copy_stream (fpin, fpout);
70 m_strreplace(&a->filename, tempfile);
73 if (a->stamp >= st.st_mtime)
74 mutt_stamp_attachment (a);
77 mutt_perror(fpin ? tempfile : a->filename);
84 return a->unlink ? 0 : -1;
88 /* return 1 if require full screen redraw, 0 otherwise */
89 int mutt_compose_attachment (BODY * a)
93 char newfile[_POSIX_PATH_MAX] = "";
94 rfc1524_entry *entry = rfc1524_new_entry ();
95 short unlink_newfile = 0;
98 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
99 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
100 if (entry->composecommand || entry->composetypecommand) {
102 if (entry->composetypecommand)
103 m_strcpy(command, sizeof(command), entry->composetypecommand);
105 m_strcpy(command, sizeof(command), entry->composecommand);
106 if (rfc1524_expand_filename (entry->nametemplate,
107 a->filename, newfile, sizeof (newfile))) {
108 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
109 if (safe_symlink (a->filename, newfile) == -1) {
110 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
118 m_strcpy(newfile, sizeof(newfile), a->filename);
120 if (rfc1524_expand_command (a, newfile, type,
121 command, sizeof (command))) {
122 /* For now, editing requires a file, no piping */
123 mutt_error _("Mailcap compose entry requires %%s");
129 if ((r = mutt_system (command)) == -1)
130 mutt_error (_("Error running \"%s\"!"), command);
132 if (r != -1 && entry->composetypecommand) {
135 char tempfile[_POSIX_PATH_MAX];
137 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
138 mutt_perror (_("Failure to open file to parse headers."));
143 b = mutt_read_mime_header (fp, 0);
146 parameter_delete(&a->parameter);
147 a->parameter = b->parameter;
150 if (b->description) {
151 p_delete(&a->description);
152 a->description = b->description;
153 b->description = NULL;
156 p_delete(&a->form_name);
157 a->form_name = b->form_name;
161 /* Remove headers by copying out data to another file, then
162 * copying the file back */
163 fseeko (fp, b->offset, 0);
164 mutt_mktemp (tempfile);
165 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
166 mutt_perror (_("Failure to open file to strip headers."));
170 mutt_copy_stream (fp, tfp);
173 mutt_unlink (a->filename);
174 if (mutt_rename_file (tempfile, a->filename) != 0) {
175 mutt_perror (_("Failure to rename file."));
187 rfc1524_free_entry (&entry);
188 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
200 rfc1524_free_entry (&entry);
205 * Currently, this only works for send mode, as it assumes that the
206 * BODY->filename actually contains the information. I'm not sure
207 * we want to deal with editing attachments we've already received,
208 * so this should be ok.
210 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
213 int mutt_edit_attachment (BODY * a)
216 char command[STRING];
217 char newfile[_POSIX_PATH_MAX] = "";
218 rfc1524_entry *entry = rfc1524_new_entry ();
219 short unlink_newfile = 0;
222 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
223 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
224 if (entry->editcommand) {
226 m_strcpy(command, sizeof(command), entry->editcommand);
227 if (rfc1524_expand_filename (entry->nametemplate,
228 a->filename, newfile, sizeof (newfile))) {
229 debug_print (1, ("oldfile: %s\t newfile: %s\n", a->filename, newfile));
230 if (safe_symlink (a->filename, newfile) == -1) {
231 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
239 m_strcpy(newfile, sizeof(newfile), a->filename);
241 if (rfc1524_expand_command (a, newfile, type,
242 command, sizeof (command))) {
243 /* For now, editing requires a file, no piping */
244 mutt_error _("Mailcap Edit entry requires %%s");
249 if (mutt_system (command) == -1) {
250 mutt_error (_("Error running \"%s\"!"), command);
256 else if (a->type == TYPETEXT) {
257 /* On text, default to editor */
258 mutt_edit_file (NONULL (Editor), a->filename);
261 rfc1524_free_entry (&entry);
262 mutt_error (_("No mailcap edit entry for %s"), type);
273 rfc1524_free_entry (&entry);
278 /* for compatibility with metamail */
279 static int is_mmnoask (const char *buf)
281 char tmp[LONG_STRING], *p, *q;
284 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
285 if (m_strcmp(p, "1") == 0)
288 m_strcpy(tmp, sizeof(tmp), p);
291 while ((p = strtok (p, ",")) != NULL) {
292 if ((q = strrchr (p, '/')) != NULL) {
293 if (*(q + 1) == '*') {
294 if (ascii_strncasecmp (buf, p, q - p) == 0)
298 if (ascii_strcasecmp (buf, p) == 0)
304 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
315 void mutt_check_lookup_list (BODY * b, char *type, int len)
317 LIST *t = MimeLookupList;
320 for (; t; t = t->next) {
321 i = m_strlen(t->data) - 1;
322 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
323 ascii_strncasecmp (type, t->data, i) == 0) ||
324 ascii_strcasecmp (type, t->data) == 0) {
331 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
332 snprintf (type, len, "%s/%s",
333 n == TYPEAUDIO ? "audio" :
334 n == TYPEAPPLICATION ? "application" :
335 n == TYPEIMAGE ? "image" :
336 n == TYPEMESSAGE ? "message" :
337 n == TYPEMODEL ? "model" :
338 n == TYPEMULTIPART ? "multipart" :
339 n == TYPETEXT ? "text" :
340 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
341 debug_print (1, ("\"%s\" -> %s\n", b->filename, type));
344 p_delete(&tmp.subtype);
346 p_delete(&tmp.xtype);
351 int mutt_is_autoview (BODY * b, const char *type)
353 LIST *t = AutoViewList;
354 char _type[SHORT_STRING];
358 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
360 m_strcpy(_type, sizeof(_type), type);
362 mutt_check_lookup_list (b, _type, sizeof (_type));
365 if (mutt_needs_mailcap (b)) {
366 if (option (OPTIMPLICITAUTOVIEW))
369 if (is_mmnoask (type))
373 for (; t; t = t->next) {
374 i = m_strlen(t->data) - 1;
375 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
376 ascii_strncasecmp (type, t->data, i) == 0) ||
377 ascii_strcasecmp (type, t->data) == 0)
384 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
385 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
386 ATTACHPTR ** idx, short idxlen)
388 char tempfile[_POSIX_PATH_MAX] = "";
389 char pagerfile[_POSIX_PATH_MAX] = "";
395 char command[STRING];
396 char descrip[STRING];
398 rfc1524_entry *entry = NULL;
400 int unlink_tempfile = 0;
402 is_message = mutt_is_message_type (a->type, a->subtype);
403 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
404 !crypt_valid_passphrase (a->hdr->security))
406 use_mailcap = (flag == M_MAILCAP ||
407 (flag == M_REGULAR && mutt_needs_mailcap (a)));
408 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
411 entry = rfc1524_new_entry ();
412 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
413 if (flag == M_REGULAR) {
414 /* fallback to view as text */
415 rfc1524_free_entry (&entry);
416 mutt_error _("No matching mailcap entry found. Viewing as text.");
427 if (!entry->command) {
428 mutt_error _("MIME type not defined. Cannot view attachment.");
432 m_strcpy(command, sizeof(command), entry->command);
435 fname = m_strdup(a->filename);
436 mutt_sanitize_filename (fname, 1);
441 if (rfc1524_expand_filename (entry->nametemplate, fname,
442 tempfile, sizeof (tempfile))) {
443 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
444 /* send case: the file is already there */
445 if (safe_symlink (a->filename, tempfile) == -1) {
446 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
448 m_strcpy(tempfile, sizeof(tempfile), a->filename);
456 else if (fp == NULL) /* send case */
457 m_strcpy(tempfile, sizeof(tempfile), a->filename);
460 /* recv case: we need to save the attachment to a file */
462 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
466 use_pipe = rfc1524_expand_command (a, tempfile, type,
467 command, sizeof (command));
468 use_pager = entry->copiousoutput;
472 if (fp && !use_mailcap && a->filename) {
474 m_strcpy(pagerfile, sizeof(pagerfile), a->filename);
475 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
478 mutt_mktemp (pagerfile);
483 int tempfd = -1, pagerfd = -1;
488 if (use_pager || use_pipe) {
490 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
492 mutt_perror ("open");
495 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
498 mutt_perror ("open");
502 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
503 use_pipe ? tempfd : -1,
504 use_pager ? pagerfd : -1,
512 mutt_error _("Cannot create filter");
519 snprintf (descrip, sizeof (descrip),
520 "---Command: %-20.20s Description: %s",
521 command, a->description);
523 snprintf (descrip, sizeof (descrip),
524 "---Command: %-30.30s Attachment: %s", command, type);
527 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
528 option (OPTWAITKEY))) && !use_pager)
529 mutt_any_key_to_continue (NULL);
536 /* interactive command */
537 if (mutt_system (command) ||
538 (entry->needsterminal && option (OPTWAITKEY)))
539 mutt_any_key_to_continue (NULL);
543 /* Don't use mailcap; the attachment is viewed in the pager */
545 if (flag == M_AS_TEXT) {
546 /* just let me see the raw data */
547 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
551 /* Use built-in handler */
552 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
553 * message in case of error */
554 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
555 unset_option (OPTVIEWATTACH);
558 unset_option (OPTVIEWATTACH);
562 m_strcpy(descrip, sizeof(descrip), a->description);
563 else if (a->filename)
564 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
567 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
570 /* We only reach this point if there have been no errors */
580 info.idxlen = idxlen;
583 rc = mutt_do_pager(descrip, pagerfile,
584 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
594 rfc1524_free_entry (&entry);
595 if (fp && tempfile[0])
596 mutt_unlink (tempfile);
597 else if (unlink_tempfile)
601 mutt_unlink (pagerfile);
606 /* returns 1 on success, 0 on error */
607 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
614 if (outfile && *outfile)
615 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
616 mutt_perror ("open");
627 if (outfile && *outfile)
628 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
630 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
633 mutt_perror (_("Can't create filter"));
639 mutt_decode_attachment (b, &s);
640 safe_fclose (&s.fpout);
647 if ((ifp = fopen (b->filename, "r")) == NULL) {
648 mutt_perror ("fopen");
649 if (outfile && *outfile) {
656 if (outfile && *outfile)
657 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
659 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
662 mutt_perror (_("Can't create filter"));
668 mutt_copy_stream (ifp, ofp);
677 if (outfile && *outfile)
681 * check for error exit from child process
683 if (mutt_wait_filter (thepid) != 0)
686 if (rv == 0 || option (OPTWAITKEY))
687 mutt_any_key_to_continue (NULL);
691 static FILE *mutt_save_attachment_open (char *path, int flags)
693 if (flags == M_SAVE_APPEND)
694 return fopen (path, "a");
695 /* be sure not to change the following fopen to safe_fopen
696 * as safe_fopen returns w/ an error if path exists
698 if (flags == M_SAVE_OVERWRITE)
699 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
701 return safe_fopen (path, "w");
704 /* returns 0 on success, -1 on error */
705 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
714 m->encoding != ENCBASE64 &&
715 m->encoding != ENCQUOTEDPRINTABLE &&
716 mutt_is_message_type (m->type, m->subtype)) {
717 /* message type attachments are written to mail folders. */
719 char buf[HUGE_STRING];
727 hn->msgno = hdr->msgno; /* required for MH/maildir */
730 fseeko (fp, m->offset, 0);
731 if (fgets (buf, sizeof (buf), fp) == NULL)
733 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
736 mx_open_new_message (&ctx, hn,
737 is_from (buf, NULL, 0,
738 NULL) ? 0 : M_ADD_FROM)) == NULL) {
739 mx_close_mailbox (&ctx, NULL);
742 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
744 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
745 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
746 && mx_commit_message (msg, &ctx) == 0)
751 mx_close_message (&msg);
752 mx_close_mailbox (&ctx, NULL);
756 /* In recv mode, extract from folder and decode */
761 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
762 mutt_perror ("fopen");
765 fseeko ((s.fpin = fp), m->offset, 0);
766 mutt_decode_attachment (m, &s);
768 if (fclose (s.fpout) != 0) {
769 mutt_perror ("fclose");
775 /* In send mode, just copy file */
779 if ((ofp = fopen (m->filename, "r")) == NULL) {
780 mutt_perror ("fopen");
784 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
785 mutt_perror ("fopen");
790 if (mutt_copy_stream (ofp, nfp) == -1) {
791 mutt_error _("Write fault!");
804 /* returns 0 on success, -1 on error */
805 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
806 int displaying, int flags)
809 unsigned int saved_encoding = 0;
810 BODY *saved_parts = NULL;
811 HEADER *saved_hdr = NULL;
814 s.flags = displaying;
816 if (flags == M_SAVE_APPEND)
817 s.fpout = fopen (path, "a");
818 else if (flags == M_SAVE_OVERWRITE)
819 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
821 s.fpout = safe_fopen (path, "w");
823 if (s.fpout == NULL) {
824 mutt_perror ("fopen");
829 /* When called from the compose menu, the attachment isn't parsed,
830 * so we need to do it here. */
833 if (stat (m->filename, &st) == -1) {
834 mutt_perror ("stat");
839 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
840 mutt_perror ("fopen");
844 saved_encoding = m->encoding;
845 if (!is_multipart (m))
846 m->encoding = ENC8BIT;
848 m->length = st.st_size;
850 saved_parts = m->parts;
852 mutt_parse_part (s.fpin, m);
854 if (m->noconv || is_multipart (m))
855 s.flags |= M_CHARCONV;
859 s.flags |= M_CHARCONV;
862 mutt_body_handler (m, &s);
867 m->encoding = saved_encoding;
869 header_delete(&m->hdr);
870 m->parts = saved_parts;
879 /* Ok, the difference between send and receive:
880 * recv: BODY->filename is a suggested name, and Context|HEADER points
881 * to the attachment in mailbox which is encooded
882 * send: BODY->filename points to the un-encoded file which contains the
886 int mutt_print_attachment (FILE * fp, BODY * a)
888 char newfile[_POSIX_PATH_MAX] = "";
892 short unlink_newfile = 0;
894 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
896 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
897 char command[_POSIX_PATH_MAX + STRING];
898 rfc1524_entry *entry;
901 debug_print (2, ("Using mailcap...\n"));
903 entry = rfc1524_new_entry ();
904 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
905 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
906 newfile, sizeof (newfile))) {
908 if (safe_symlink (a->filename, newfile) == -1) {
909 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
911 rfc1524_free_entry (&entry);
914 m_strcpy(newfile, sizeof(newfile), a->filename);
921 /* in recv mode, save file to newfile first */
923 mutt_save_attachment (fp, a, newfile, 0, NULL);
925 m_strcpy(command, sizeof(command), entry->printcommand);
926 piped = rfc1524_expand_command(a, newfile, type, command,
931 /* interactive program */
933 if ((ifp = fopen (newfile, "r")) == NULL) {
934 mutt_perror ("fopen");
935 rfc1524_free_entry (&entry);
939 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
940 mutt_perror (_("Can't create filter"));
942 rfc1524_free_entry (&entry);
946 mutt_copy_stream (ifp, fpout);
947 safe_fclose (&fpout);
949 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
950 mutt_any_key_to_continue (NULL);
953 if (mutt_system (command) || option (OPTWAITKEY))
954 mutt_any_key_to_continue (NULL);
958 mutt_unlink (newfile);
959 else if (unlink_newfile)
962 rfc1524_free_entry (&entry);
966 if (!ascii_strcasecmp ("text/plain", type) ||
967 !ascii_strcasecmp ("application/postscript", type)) {
968 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
970 else if (mutt_can_decode (a)) {
971 /* decode and print */
978 mutt_mktemp (newfile);
979 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
981 debug_print (2, ("successfully decoded %s type attachment to %s\n",
984 if ((ifp = fopen (newfile, "r")) == NULL) {
985 mutt_perror ("fopen");
989 debug_print (2, ("successfully opened %s read-only\n", newfile));
993 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
994 mutt_perror (_("Can't create filter"));
999 debug_print (2, ("Filter created.\n"));
1001 mutt_copy_stream (ifp, fpout);
1003 safe_fclose (&fpout);
1006 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1007 mutt_any_key_to_continue (NULL);
1012 safe_fclose (&fpout);
1013 mutt_unlink (newfile);
1017 mutt_error _("I don't know how to print that!");
1023 int mutt_attach_check (HEADER* hdr) {
1025 char buf[LONG_STRING];
1028 regmatch_t pmatch[1];
1030 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1031 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1034 while (!found && fgets (buf, sizeof (buf), fp)) {
1037 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,