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 (m_strcmp(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 && m_strcmp(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 */
575 info.idxlen = idxlen;
578 rc = mutt_do_pager(descrip, pagerfile,
579 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
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 if (outfile && *outfile)
623 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
625 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
628 mutt_perror (_("Can't create filter"));
634 mutt_decode_attachment (b, &s);
635 safe_fclose (&s.fpout);
642 if ((ifp = fopen (b->filename, "r")) == NULL) {
643 mutt_perror ("fopen");
644 if (outfile && *outfile) {
651 if (outfile && *outfile)
652 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
654 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
657 mutt_perror (_("Can't create filter"));
663 mutt_copy_stream (ifp, ofp);
672 if (outfile && *outfile)
676 * check for error exit from child process
678 if (mutt_wait_filter (thepid) != 0)
681 if (rv == 0 || option (OPTWAITKEY))
682 mutt_any_key_to_continue (NULL);
686 static FILE *mutt_save_attachment_open (char *path, int flags)
688 if (flags == M_SAVE_APPEND)
689 return fopen (path, "a");
690 /* be sure not to change the following fopen to safe_fopen
691 * as safe_fopen returns w/ an error if path exists
693 if (flags == M_SAVE_OVERWRITE)
694 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
696 return safe_fopen (path, "w");
699 /* returns 0 on success, -1 on error */
700 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
709 m->encoding != ENCBASE64 &&
710 m->encoding != ENCQUOTEDPRINTABLE &&
711 mutt_is_message_type (m->type, m->subtype)) {
712 /* message type attachments are written to mail folders. */
714 char buf[HUGE_STRING];
722 hn->msgno = hdr->msgno; /* required for MH/maildir */
725 fseeko (fp, m->offset, 0);
726 if (fgets (buf, sizeof (buf), fp) == NULL)
728 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
731 mx_open_new_message (&ctx, hn,
732 is_from (buf, NULL, 0,
733 NULL) ? 0 : M_ADD_FROM)) == NULL) {
734 mx_close_mailbox (&ctx, NULL);
737 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
739 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
740 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
741 && mx_commit_message (msg, &ctx) == 0)
746 mx_close_message (&msg);
747 mx_close_mailbox (&ctx, NULL);
751 /* In recv mode, extract from folder and decode */
756 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
757 mutt_perror ("fopen");
760 fseeko ((s.fpin = fp), m->offset, 0);
761 mutt_decode_attachment (m, &s);
763 if (fclose (s.fpout) != 0) {
764 mutt_perror ("fclose");
770 /* In send mode, just copy file */
774 if ((ofp = fopen (m->filename, "r")) == NULL) {
775 mutt_perror ("fopen");
779 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
780 mutt_perror ("fopen");
785 if (mutt_copy_stream (ofp, nfp) == -1) {
786 mutt_error _("Write fault!");
799 /* returns 0 on success, -1 on error */
800 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
801 int displaying, int flags)
804 unsigned int saved_encoding = 0;
805 BODY *saved_parts = NULL;
806 HEADER *saved_hdr = NULL;
809 s.flags = displaying;
811 if (flags == M_SAVE_APPEND)
812 s.fpout = fopen (path, "a");
813 else if (flags == M_SAVE_OVERWRITE)
814 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
816 s.fpout = safe_fopen (path, "w");
818 if (s.fpout == NULL) {
819 mutt_perror ("fopen");
824 /* When called from the compose menu, the attachment isn't parsed,
825 * so we need to do it here. */
828 if (stat (m->filename, &st) == -1) {
829 mutt_perror ("stat");
834 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
835 mutt_perror ("fopen");
839 saved_encoding = m->encoding;
840 if (!is_multipart (m))
841 m->encoding = ENC8BIT;
843 m->length = st.st_size;
845 saved_parts = m->parts;
847 mutt_parse_part (s.fpin, m);
849 if (m->noconv || is_multipart (m))
850 s.flags |= M_CHARCONV;
854 s.flags |= M_CHARCONV;
857 mutt_body_handler (m, &s);
862 m->encoding = saved_encoding;
864 mutt_free_header (&m->hdr);
865 m->parts = saved_parts;
874 /* Ok, the difference between send and receive:
875 * recv: BODY->filename is a suggested name, and Context|HEADER points
876 * to the attachment in mailbox which is encooded
877 * send: BODY->filename points to the un-encoded file which contains the
881 int mutt_print_attachment (FILE * fp, BODY * a)
883 char newfile[_POSIX_PATH_MAX] = "";
887 short unlink_newfile = 0;
889 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
891 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
892 char command[_POSIX_PATH_MAX + STRING];
893 rfc1524_entry *entry;
896 debug_print (2, ("Using mailcap...\n"));
898 entry = rfc1524_new_entry ();
899 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
900 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
901 newfile, sizeof (newfile))) {
903 if (safe_symlink (a->filename, newfile) == -1) {
904 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
906 rfc1524_free_entry (&entry);
909 strfcpy (newfile, a->filename, sizeof (newfile));
916 /* in recv mode, save file to newfile first */
918 mutt_save_attachment (fp, a, newfile, 0, NULL);
920 strfcpy (command, entry->printcommand, sizeof (command));
922 rfc1524_expand_command (a, newfile, type, command, sizeof (command));
926 /* interactive program */
928 if ((ifp = fopen (newfile, "r")) == NULL) {
929 mutt_perror ("fopen");
930 rfc1524_free_entry (&entry);
934 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
935 mutt_perror (_("Can't create filter"));
937 rfc1524_free_entry (&entry);
941 mutt_copy_stream (ifp, fpout);
942 safe_fclose (&fpout);
944 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
945 mutt_any_key_to_continue (NULL);
948 if (mutt_system (command) || option (OPTWAITKEY))
949 mutt_any_key_to_continue (NULL);
953 mutt_unlink (newfile);
954 else if (unlink_newfile)
957 rfc1524_free_entry (&entry);
961 if (!ascii_strcasecmp ("text/plain", type) ||
962 !ascii_strcasecmp ("application/postscript", type)) {
963 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
965 else if (mutt_can_decode (a)) {
966 /* decode and print */
973 mutt_mktemp (newfile);
974 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
976 debug_print (2, ("successfully decoded %s type attachment to %s\n",
979 if ((ifp = fopen (newfile, "r")) == NULL) {
980 mutt_perror ("fopen");
984 debug_print (2, ("successfully opened %s read-only\n", newfile));
988 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
989 mutt_perror (_("Can't create filter"));
994 debug_print (2, ("Filter created.\n"));
996 mutt_copy_stream (ifp, fpout);
998 safe_fclose (&fpout);
1001 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
1002 mutt_any_key_to_continue (NULL);
1007 safe_fclose (&fpout);
1008 mutt_unlink (newfile);
1012 mutt_error _("I don't know how to print that!");
1018 int mutt_attach_check (HEADER* hdr) {
1020 char buf[LONG_STRING];
1023 regmatch_t pmatch[1];
1025 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1026 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1029 while (!found && fgets (buf, sizeof (buf), fp)) {
1032 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,