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>
30 #include <lib-sys/unix.h>
32 #include <lib-mime/mime.h>
34 #include <lib-ui/curses.h>
35 #include <lib-ui/menu.h>
39 #include "recvattach.h"
45 #include <lib-crypt/crypt.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 if (safe_symlink (a->filename, newfile) == -1) {
109 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
117 m_strcpy(newfile, sizeof(newfile), a->filename);
119 if (rfc1524_expand_command (a, newfile, type,
120 command, sizeof (command))) {
121 /* For now, editing requires a file, no piping */
122 mutt_error _("Mailcap compose entry requires %%s");
128 if ((r = mutt_system (command)) == -1)
129 mutt_error (_("Error running \"%s\"!"), command);
131 if (r != -1 && entry->composetypecommand) {
134 char tempfile[_POSIX_PATH_MAX];
136 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
137 mutt_perror (_("Failure to open file to parse headers."));
142 b = mutt_read_mime_header (fp, 0);
145 parameter_list_wipe(&a->parameter);
146 a->parameter = b->parameter;
149 if (b->description) {
150 p_delete(&a->description);
151 a->description = b->description;
152 b->description = NULL;
155 p_delete(&a->form_name);
156 a->form_name = b->form_name;
160 /* Remove headers by copying out data to another file, then
161 * copying the file back */
162 fseeko (fp, b->offset, 0);
163 mutt_mktemp (tempfile);
164 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
165 mutt_perror (_("Failure to open file to strip headers."));
169 mutt_copy_stream (fp, tfp);
172 mutt_unlink (a->filename);
173 if (mutt_rename_file (tempfile, a->filename) != 0) {
174 mutt_perror (_("Failure to rename file."));
186 rfc1524_free_entry (&entry);
187 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
199 rfc1524_free_entry (&entry);
204 * Currently, this only works for send mode, as it assumes that the
205 * BODY->filename actually contains the information. I'm not sure
206 * we want to deal with editing attachments we've already received,
207 * so this should be ok.
209 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
212 int mutt_edit_attachment (BODY * a)
215 char command[STRING];
216 char newfile[_POSIX_PATH_MAX] = "";
217 rfc1524_entry *entry = rfc1524_new_entry ();
218 short unlink_newfile = 0;
221 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
222 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
223 if (entry->editcommand) {
225 m_strcpy(command, sizeof(command), entry->editcommand);
226 if (rfc1524_expand_filename (entry->nametemplate,
227 a->filename, newfile, sizeof (newfile))) {
228 if (safe_symlink (a->filename, newfile) == -1) {
229 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
237 m_strcpy(newfile, sizeof(newfile), a->filename);
239 if (rfc1524_expand_command (a, newfile, type,
240 command, sizeof (command))) {
241 /* For now, editing requires a file, no piping */
242 mutt_error _("Mailcap Edit entry requires %%s");
247 if (mutt_system (command) == -1) {
248 mutt_error (_("Error running \"%s\"!"), command);
254 else if (a->type == TYPETEXT) {
255 /* On text, default to editor */
256 mutt_edit_file (NONULL (Editor), a->filename);
259 rfc1524_free_entry (&entry);
260 mutt_error (_("No mailcap edit entry for %s"), type);
271 rfc1524_free_entry (&entry);
276 /* for compatibility with metamail */
277 static int is_mmnoask (const char *buf)
279 char tmp[LONG_STRING], *p, *q;
282 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
283 if (m_strcmp(p, "1") == 0)
286 m_strcpy(tmp, sizeof(tmp), p);
289 while ((p = strtok (p, ",")) != NULL) {
290 if ((q = strrchr (p, '/')) != NULL) {
291 if (*(q + 1) == '*') {
292 if (ascii_strncasecmp (buf, p, q - p) == 0)
296 if (ascii_strcasecmp (buf, p) == 0)
302 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
313 void mutt_check_lookup_list (BODY * b, char *type, int len)
315 string_list_t *t = MimeLookupList;
318 for (; t; t = t->next) {
319 i = m_strlen(t->data) - 1;
320 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
321 ascii_strncasecmp (type, t->data, i) == 0) ||
322 ascii_strcasecmp (type, t->data) == 0) {
329 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
330 snprintf (type, len, "%s/%s",
331 n == TYPEAUDIO ? "audio" :
332 n == TYPEAPPLICATION ? "application" :
333 n == TYPEIMAGE ? "image" :
334 n == TYPEMESSAGE ? "message" :
335 n == TYPEMODEL ? "model" :
336 n == TYPEMULTIPART ? "multipart" :
337 n == TYPETEXT ? "text" :
338 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
341 p_delete(&tmp.subtype);
343 p_delete(&tmp.xtype);
348 int mutt_is_autoview (BODY * b, const char *type)
350 string_list_t *t = AutoViewList;
351 char _type[SHORT_STRING];
355 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
357 m_strcpy(_type, sizeof(_type), type);
359 mutt_check_lookup_list (b, _type, sizeof (_type));
362 if (mutt_needs_mailcap (b)) {
363 if (option (OPTIMPLICITAUTOVIEW))
366 if (is_mmnoask (type))
370 for (; t; t = t->next) {
371 i = m_strlen(t->data) - 1;
372 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
373 ascii_strncasecmp (type, t->data, i) == 0) ||
374 ascii_strcasecmp (type, t->data) == 0)
381 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
382 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
383 ATTACHPTR ** idx, short idxlen)
385 char tempfile[_POSIX_PATH_MAX] = "";
386 char pagerfile[_POSIX_PATH_MAX] = "";
392 char command[STRING];
393 char descrip[STRING];
395 rfc1524_entry *entry = NULL;
397 int unlink_tempfile = 0;
399 is_message = mutt_is_message_type (a->type, a->subtype);
400 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
401 !crypt_valid_passphrase (a->hdr->security))
403 use_mailcap = (flag == M_MAILCAP ||
404 (flag == M_REGULAR && mutt_needs_mailcap (a)));
405 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
408 entry = rfc1524_new_entry ();
409 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
410 if (flag == M_REGULAR) {
411 /* fallback to view as text */
412 rfc1524_free_entry (&entry);
413 mutt_error _("No matching mailcap entry found. Viewing as text.");
424 if (!entry->command) {
425 mutt_error _("MIME type not defined. Cannot view attachment.");
429 m_strcpy(command, sizeof(command), entry->command);
432 fname = m_strdup(a->filename);
433 mutt_sanitize_filename (fname, 1);
438 if (rfc1524_expand_filename (entry->nametemplate, fname,
439 tempfile, sizeof (tempfile))) {
440 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
441 /* send case: the file is already there */
442 if (safe_symlink (a->filename, tempfile) == -1) {
443 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
445 m_strcpy(tempfile, sizeof(tempfile), a->filename);
453 else if (fp == NULL) /* send case */
454 m_strcpy(tempfile, sizeof(tempfile), a->filename);
457 /* recv case: we need to save the attachment to a file */
459 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
463 use_pipe = rfc1524_expand_command (a, tempfile, type,
464 command, sizeof (command));
465 use_pager = entry->copiousoutput;
469 if (fp && !use_mailcap && a->filename) {
471 m_strcpy(pagerfile, sizeof(pagerfile), a->filename);
472 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
475 mutt_mktemp (pagerfile);
480 int tempfd = -1, pagerfd = -1;
485 if (use_pager || use_pipe) {
487 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
489 mutt_perror ("open");
492 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
495 mutt_perror ("open");
499 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
500 use_pipe ? tempfd : -1,
501 use_pager ? pagerfd : -1,
509 mutt_error _("Cannot create filter");
516 snprintf (descrip, sizeof (descrip),
517 "---Command: %-20.20s Description: %s",
518 command, a->description);
520 snprintf (descrip, sizeof (descrip),
521 "---Command: %-30.30s Attachment: %s", command, type);
524 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
525 option (OPTWAITKEY))) && !use_pager)
526 mutt_any_key_to_continue (NULL);
533 /* interactive command */
534 if (mutt_system (command) ||
535 (entry->needsterminal && option (OPTWAITKEY)))
536 mutt_any_key_to_continue (NULL);
540 /* Don't use mailcap; the attachment is viewed in the pager */
542 if (flag == M_AS_TEXT) {
543 /* just let me see the raw data */
544 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
548 /* Use built-in handler */
549 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
550 * message in case of error */
551 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
552 unset_option (OPTVIEWATTACH);
555 unset_option (OPTVIEWATTACH);
559 m_strcpy(descrip, sizeof(descrip), a->description);
560 else if (a->filename)
561 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
564 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
567 /* We only reach this point if there have been no errors */
577 info.idxlen = idxlen;
580 rc = mutt_do_pager(descrip, pagerfile,
581 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
591 rfc1524_free_entry (&entry);
592 if (fp && tempfile[0])
593 mutt_unlink (tempfile);
594 else if (unlink_tempfile)
598 mutt_unlink (pagerfile);
603 /* returns 1 on success, 0 on error */
604 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
611 if (outfile && *outfile)
612 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
613 mutt_perror ("open");
624 if (outfile && *outfile)
625 thepid = 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 */
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;
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 header_delete(&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 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 m_strcpy(newfile, sizeof(newfile), a->filename);
916 /* in recv mode, save file to newfile first */
918 mutt_save_attachment (fp, a, newfile, 0, NULL);
920 m_strcpy(command, sizeof(command), entry->printcommand);
921 piped = rfc1524_expand_command(a, newfile, type, 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 if ((ifp = fopen (newfile, "r")) == NULL) {
977 mutt_perror ("fopen");
983 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
984 mutt_perror (_("Can't create filter"));
989 mutt_copy_stream (ifp, fpout);
991 safe_fclose (&fpout);
994 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
995 mutt_any_key_to_continue (NULL);
1000 safe_fclose (&fpout);
1001 mutt_unlink (newfile);
1005 mutt_error _("I don't know how to print that!");
1011 int mutt_attach_check (HEADER* hdr) {
1013 char buf[LONG_STRING];
1016 regmatch_t pmatch[1];
1018 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1019 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1022 while (!found && fgets (buf, sizeof (buf), fp)) {
1025 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,