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.
11 #include <lib-lib/lib-lib.h>
13 #include <lib-sys/unix.h>
14 #include <lib-mime/mime.h>
15 #include <lib-ui/curses.h>
16 #include <lib-ui/menu.h>
17 #include <lib-mx/mx.h>
21 #include "recvattach.h"
25 #include <lib-crypt/crypt.h>
27 int mutt_get_tmp_attachment (BODY * a)
30 char tempfile[_POSIX_PATH_MAX];
31 rfc1524_entry *entry = rfc1524_entry_new();
32 FILE *fpin = NULL, *fpout = NULL;
38 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
39 rfc1524_mailcap_lookup (a, type, entry, 0);
40 rfc1524_expand_filename (entry->nametemplate, a->filename,
41 tempfile, sizeof (tempfile));
43 rfc1524_entry_delete(&entry);
45 if (stat (a->filename, &st) == -1)
48 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) {
49 mutt_copy_stream (fpin, fpout);
50 m_strreplace(&a->filename, tempfile);
53 if (a->stamp >= st.st_mtime)
54 mutt_stamp_attachment (a);
57 mutt_perror(fpin ? tempfile : a->filename);
62 return a->unlink ? 0 : -1;
66 /* return 1 if require full screen redraw, 0 otherwise */
67 int mutt_compose_attachment (BODY * a)
71 char newfile[_POSIX_PATH_MAX] = "";
72 rfc1524_entry *entry = rfc1524_entry_new();
73 short unlink_newfile = 0;
76 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
77 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
78 if (entry->composecommand || entry->composetypecommand) {
80 if (entry->composetypecommand)
81 m_strcpy(command, sizeof(command), entry->composetypecommand);
83 m_strcpy(command, sizeof(command), entry->composecommand);
84 if (rfc1524_expand_filename (entry->nametemplate,
85 a->filename, newfile, sizeof (newfile))) {
86 if (safe_symlink (a->filename, newfile) == -1) {
87 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
95 m_strcpy(newfile, sizeof(newfile), a->filename);
97 if (rfc1524_expand_command (a, newfile, type,
98 command, sizeof (command))) {
99 /* For now, editing requires a file, no piping */
100 mutt_error _("Mailcap compose entry requires %%s");
106 if ((r = mutt_system (command)) == -1)
107 mutt_error (_("Error running \"%s\"!"), command);
109 if (r != -1 && entry->composetypecommand) {
113 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
114 mutt_perror (_("Failure to open file to parse headers."));
119 b = mutt_read_mime_header (fp, 0);
121 char tempfile[_POSIX_PATH_MAX];
124 parameter_list_wipe(&a->parameter);
125 a->parameter = b->parameter;
128 if (b->description) {
129 p_delete(&a->description);
130 a->description = b->description;
131 b->description = NULL;
134 p_delete(&a->form_name);
135 a->form_name = b->form_name;
139 /* Remove headers by copying out data to another file, then
140 * copying the file back */
141 fseeko (fp, b->offset, 0);
142 tfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
144 mutt_perror (_("Failure to open file to strip headers."));
147 mutt_copy_stream (fp, tfp);
150 mutt_unlink (a->filename);
151 if (mutt_rename_file (tempfile, a->filename) != 0) {
152 mutt_perror (_("Failure to rename file."));
164 rfc1524_entry_delete(&entry);
165 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
177 rfc1524_entry_delete(&entry);
182 * Currently, this only works for send mode, as it assumes that the
183 * BODY->filename actually contains the information. I'm not sure
184 * we want to deal with editing attachments we've already received,
185 * so this should be ok.
187 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
190 int mutt_edit_attachment (BODY * a)
193 char command[STRING];
194 char newfile[_POSIX_PATH_MAX] = "";
195 rfc1524_entry *entry = rfc1524_entry_new();
196 short unlink_newfile = 0;
199 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
200 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
201 if (entry->editcommand) {
203 m_strcpy(command, sizeof(command), entry->editcommand);
204 if (rfc1524_expand_filename (entry->nametemplate,
205 a->filename, newfile, sizeof (newfile))) {
206 if (safe_symlink (a->filename, newfile) == -1) {
207 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
215 m_strcpy(newfile, sizeof(newfile), a->filename);
217 if (rfc1524_expand_command (a, newfile, type,
218 command, sizeof (command))) {
219 /* For now, editing requires a file, no piping */
220 mutt_error _("Mailcap Edit entry requires %%s");
225 if (mutt_system (command) == -1) {
226 mutt_error (_("Error running \"%s\"!"), command);
232 else if (a->type == TYPETEXT) {
233 /* On text, default to editor */
234 mutt_edit_file (NONULL (Editor), a->filename);
237 rfc1524_entry_delete(&entry);
238 mutt_error (_("No mailcap edit entry for %s"), type);
249 rfc1524_entry_delete(&entry);
254 /* for compatibility with metamail */
255 static int is_mmnoask (const char *buf)
257 char tmp[LONG_STRING], *p, *q;
260 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
261 if (m_strcmp(p, "1") == 0)
264 m_strcpy(tmp, sizeof(tmp), p);
267 while ((p = strtok (p, ",")) != NULL) {
268 if ((q = strrchr (p, '/')) != NULL) {
269 if (*(q + 1) == '*') {
270 if (ascii_strncasecmp (buf, p, q - p) == 0)
274 if (ascii_strcasecmp (buf, p) == 0)
280 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
291 void mutt_check_lookup_list (BODY * b, char *type, int len)
293 string_list_t *t = MimeLookupList;
296 for (; t; t = t->next) {
297 i = m_strlen(t->data) - 1;
298 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
299 ascii_strncasecmp (type, t->data, i) == 0) ||
300 ascii_strcasecmp (type, t->data) == 0) {
307 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
308 snprintf (type, len, "%s/%s",
309 n == TYPEAUDIO ? "audio" :
310 n == TYPEAPPLICATION ? "application" :
311 n == TYPEIMAGE ? "image" :
312 n == TYPEMESSAGE ? "message" :
313 n == TYPEMODEL ? "model" :
314 n == TYPEMULTIPART ? "multipart" :
315 n == TYPETEXT ? "text" :
316 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
319 p_delete(&tmp.subtype);
321 p_delete(&tmp.xtype);
326 int mutt_is_autoview (BODY * b, const char *type)
328 string_list_t *t = AutoViewList;
329 char _type[SHORT_STRING];
333 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
335 m_strcpy(_type, sizeof(_type), type);
337 mutt_check_lookup_list (b, _type, sizeof (_type));
340 if (rfc1524_mailcap_isneeded(b)) {
341 if (option (OPTIMPLICITAUTOVIEW))
344 if (is_mmnoask (type))
348 for (; t; t = t->next) {
349 i = m_strlen(t->data) - 1;
350 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
351 ascii_strncasecmp (type, t->data, i) == 0) ||
352 ascii_strcasecmp (type, t->data) == 0)
359 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
360 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
361 ATTACHPTR ** idx, short idxlen)
363 char tempfile[_POSIX_PATH_MAX] = "";
364 char pagerfile[_POSIX_PATH_MAX] = "";
370 char command[STRING];
371 char descrip[STRING];
373 rfc1524_entry *entry = NULL;
375 int unlink_tempfile = 0;
378 is_message = mutt_is_message_type(a);
379 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
380 !crypt_valid_passphrase (a->hdr->security))
382 use_mailcap = (flag == M_MAILCAP ||
383 (flag == M_REGULAR && rfc1524_mailcap_isneeded(a)));
384 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
387 entry = rfc1524_entry_new();
388 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
389 if (flag == M_REGULAR) {
390 /* fallback to view as text */
391 rfc1524_entry_delete(&entry);
392 mutt_error _("No matching mailcap entry found. Viewing as text.");
403 if (!entry->command) {
404 mutt_error _("MIME type not defined. Cannot view attachment.");
408 m_strcpy(command, sizeof(command), entry->command);
411 fname = m_strdup(a->filename);
412 mutt_sanitize_filename (fname, 1);
417 if (rfc1524_expand_filename (entry->nametemplate, fname,
418 tempfile, sizeof (tempfile))) {
419 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
420 /* send case: the file is already there */
421 if (safe_symlink (a->filename, tempfile) == -1) {
422 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
424 m_strcpy(tempfile, sizeof(tempfile), a->filename);
432 else if (fp == NULL) /* send case */
433 m_strcpy(tempfile, sizeof(tempfile), a->filename);
436 /* recv case: we need to save the attachment to a file */
438 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
442 use_pipe = rfc1524_expand_command (a, tempfile, type,
443 command, sizeof (command));
444 use_pager = entry->copiousoutput;
449 pagerfd = m_tempfd(pagerfile, sizeof(pagerfile), NONULL(Tempdir),
450 fp && !use_mailcap ? a->filename : NULL);
460 if (use_pager || use_pipe) {
461 if (use_pager && pagerfd == -1) {
462 mutt_perror ("open");
465 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
468 mutt_perror ("open");
472 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
473 use_pipe ? tempfd : -1,
474 use_pager ? pagerfd : -1,
482 mutt_error _("Cannot create filter");
489 snprintf (descrip, sizeof (descrip),
490 "---Command: %-20.20s Description: %s",
491 command, a->description);
493 snprintf (descrip, sizeof (descrip),
494 "---Command: %-30.30s Attachment: %s", command, type);
497 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
498 option (OPTWAITKEY))) && !use_pager)
499 mutt_any_key_to_continue (NULL);
506 /* interactive command */
507 if (mutt_system (command) ||
508 (entry->needsterminal && option (OPTWAITKEY)))
509 mutt_any_key_to_continue (NULL);
513 /* Don't use mailcap; the attachment is viewed in the pager */
515 if (flag == M_AS_TEXT) {
516 /* just let me see the raw data */
517 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
521 /* Use built-in handler */
522 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
523 * message in case of error */
524 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
525 unset_option (OPTVIEWATTACH);
528 unset_option (OPTVIEWATTACH);
532 m_strcpy(descrip, sizeof(descrip), a->description);
533 else if (a->filename)
534 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
537 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
540 /* We only reach this point if there have been no errors */
550 info.idxlen = idxlen;
553 rc = mutt_do_pager(descrip, pagerfile,
554 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
564 rfc1524_entry_delete(&entry);
565 if (fp && tempfile[0])
566 mutt_unlink (tempfile);
567 else if (unlink_tempfile)
571 mutt_unlink (pagerfile);
576 /* returns 1 on success, 0 on error */
577 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
584 if (outfile && *outfile)
585 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
586 mutt_perror ("open");
597 if (outfile && *outfile)
598 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
600 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
603 mutt_perror (_("Can't create filter"));
609 mutt_decode_attachment (b, &s);
617 if ((ifp = fopen (b->filename, "r")) == NULL) {
618 mutt_perror ("fopen");
619 if (outfile && *outfile) {
626 if (outfile && *outfile)
627 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
629 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
632 mutt_perror (_("Can't create filter"));
638 mutt_copy_stream (ifp, ofp);
647 if (outfile && *outfile)
651 * check for error exit from child process
653 if (mutt_wait_filter (thepid) != 0)
656 if (rv == 0 || option (OPTWAITKEY))
657 mutt_any_key_to_continue (NULL);
661 static FILE *mutt_save_attachment_open (char *path, int flags)
663 if (flags == M_SAVE_APPEND)
664 return fopen (path, "a");
665 /* be sure not to change the following fopen to safe_fopen
666 * as safe_fopen returns w/ an error if path exists
668 if (flags == M_SAVE_OVERWRITE)
669 return fopen (path, "w");
671 return safe_fopen (path, "w");
674 /* returns 0 on success, -1 on error */
675 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
684 m->encoding != ENCBASE64 &&
685 m->encoding != ENCQUOTEDPRINTABLE &&
686 mutt_is_message_type(m)) {
687 /* message type attachments are written to mail folders. */
689 char buf[HUGE_STRING];
697 hn->msgno = hdr->msgno; /* required for MH/maildir */
700 fseeko (fp, m->offset, 0);
701 if (fgets (buf, sizeof (buf), fp) == NULL)
703 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
706 mx_open_new_message (&ctx, hn,
707 is_from (buf, NULL, 0,
708 NULL) ? 0 : M_ADD_FROM)) == NULL) {
709 mx_close_mailbox (&ctx, NULL);
712 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
714 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
715 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
716 && mx_commit_message (msg, &ctx) == 0)
721 mx_close_message (&msg);
722 mx_close_mailbox (&ctx, NULL);
726 /* In recv mode, extract from folder and decode */
731 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
732 mutt_perror ("fopen");
735 fseeko ((s.fpin = fp), m->offset, 0);
736 mutt_decode_attachment (m, &s);
738 if (m_fclose(&s.fpout) != 0) {
739 mutt_perror ("fclose");
744 /* In send mode, just copy file */
748 if ((ofp = fopen (m->filename, "r")) == NULL) {
749 mutt_perror ("fopen");
753 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
754 mutt_perror ("fopen");
759 if (mutt_copy_stream (ofp, nfp) == -1) {
760 mutt_error _("Write fault!");
773 /* returns 0 on success, -1 on error */
774 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
775 int displaying, int flags)
778 unsigned int saved_encoding = 0;
779 BODY *saved_parts = NULL;
780 HEADER *saved_hdr = NULL;
783 s.flags = displaying;
785 if (flags == M_SAVE_APPEND)
786 s.fpout = fopen (path, "a");
788 s.fpout = safe_fopen (path, "w");
790 if (s.fpout == NULL) {
791 mutt_perror ("fopen");
796 /* When called from the compose menu, the attachment isn't parsed,
797 * so we need to do it here. */
800 if (stat (m->filename, &st) == -1) {
801 mutt_perror ("stat");
806 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
807 mutt_perror ("fopen");
811 saved_encoding = m->encoding;
812 if (!is_multipart (m))
813 m->encoding = ENC8BIT;
815 m->length = st.st_size;
817 saved_parts = m->parts;
819 mutt_parse_part (s.fpin, m);
821 if (m->noconv || is_multipart (m))
822 s.flags |= M_CHARCONV;
826 s.flags |= M_CHARCONV;
829 mutt_body_handler (m, &s);
834 m->encoding = saved_encoding;
836 header_delete(&m->hdr);
837 m->parts = saved_parts;
846 /* Ok, the difference between send and receive:
847 * recv: BODY->filename is a suggested name, and Context|HEADER points
848 * to the attachment in mailbox which is encooded
849 * send: BODY->filename points to the un-encoded file which contains the
853 int mutt_print_attachment (FILE * fp, BODY * a)
855 char newfile[_POSIX_PATH_MAX] = "";
859 short unlink_newfile = 0;
861 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
863 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
864 char command[_POSIX_PATH_MAX + STRING];
865 rfc1524_entry *entry;
868 entry = rfc1524_entry_new();
869 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
870 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
871 newfile, sizeof (newfile))) {
873 if (safe_symlink (a->filename, newfile) == -1) {
874 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
876 rfc1524_entry_delete(&entry);
879 m_strcpy(newfile, sizeof(newfile), a->filename);
886 /* in recv mode, save file to newfile first */
888 mutt_save_attachment (fp, a, newfile, 0, NULL);
890 m_strcpy(command, sizeof(command), entry->printcommand);
891 piped = rfc1524_expand_command(a, newfile, type, command,
896 /* interactive program */
898 if ((ifp = fopen (newfile, "r")) == NULL) {
899 mutt_perror ("fopen");
900 rfc1524_entry_delete(&entry);
904 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
905 mutt_perror (_("Can't create filter"));
907 rfc1524_entry_delete(&entry);
911 mutt_copy_stream (ifp, fpout);
914 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
915 mutt_any_key_to_continue (NULL);
918 if (mutt_system (command) || option (OPTWAITKEY))
919 mutt_any_key_to_continue (NULL);
923 mutt_unlink (newfile);
924 else if (unlink_newfile)
927 rfc1524_entry_delete(&entry);
931 if (!ascii_strcasecmp ("text/plain", type) ||
932 !ascii_strcasecmp ("application/postscript", type)) {
933 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
935 else if (mutt_can_decode (a)) {
936 /* decode and print */
943 mutt_mktemp (newfile);
944 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
946 if ((ifp = fopen (newfile, "r")) == NULL) {
947 mutt_perror ("fopen");
953 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
954 mutt_perror (_("Can't create filter"));
959 mutt_copy_stream (ifp, fpout);
964 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
965 mutt_any_key_to_continue (NULL);
971 mutt_unlink (newfile);
975 mutt_error _("I don't know how to print that!");
981 int mutt_attach_check (HEADER* hdr) {
983 char buf[LONG_STRING];
986 regmatch_t pmatch[1];
988 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
989 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
992 while (!found && fgets (buf, sizeof (buf), fp)) {
995 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,