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);
56 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)
94 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");
105 if ((r = mutt_system (command)) == -1)
106 mutt_error (_("Error running \"%s\"!"), command);
108 if (r != -1 && entry->composetypecommand) {
112 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
113 mutt_perror (_("Failure to open file to parse headers."));
118 b = mutt_read_mime_header (fp, 0);
120 char tempfile[_POSIX_PATH_MAX];
123 parameter_list_wipe(&a->parameter);
124 a->parameter = b->parameter;
127 if (b->description) {
128 p_delete(&a->description);
129 a->description = b->description;
130 b->description = NULL;
133 p_delete(&a->form_name);
134 a->form_name = b->form_name;
138 /* Remove headers by copying out data to another file, then
139 * copying the file back */
140 fseeko (fp, b->offset, 0);
141 tfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
143 mutt_perror (_("Failure to open file to strip headers."));
146 mutt_copy_stream (fp, tfp);
149 mutt_unlink (a->filename);
150 if (mutt_rename_file (tempfile, a->filename) != 0) {
151 mutt_perror (_("Failure to rename file."));
162 rfc1524_entry_delete(&entry);
163 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
175 rfc1524_entry_delete(&entry);
180 * Currently, this only works for send mode, as it assumes that the
181 * BODY->filename actually contains the information. I'm not sure
182 * we want to deal with editing attachments we've already received,
183 * so this should be ok.
185 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
188 int mutt_edit_attachment (BODY * a)
191 char command[STRING];
192 char newfile[_POSIX_PATH_MAX] = "";
193 rfc1524_entry *entry = rfc1524_entry_new();
194 short unlink_newfile = 0;
197 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
198 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
199 if (entry->editcommand) {
201 m_strcpy(command, sizeof(command), entry->editcommand);
202 if (rfc1524_expand_filename (entry->nametemplate,
203 a->filename, newfile, sizeof (newfile))) {
204 if (safe_symlink (a->filename, newfile) == -1) {
205 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
212 m_strcpy(newfile, sizeof(newfile), a->filename);
215 if (rfc1524_expand_command (a, newfile, type,
216 command, sizeof (command))) {
217 /* For now, editing requires a file, no piping */
218 mutt_error _("Mailcap Edit entry requires %%s");
222 if (mutt_system (command) == -1) {
223 mutt_error (_("Error running \"%s\"!"), command);
229 else if (a->type == TYPETEXT) {
230 /* On text, default to editor */
231 mutt_edit_file (NONULL (Editor), a->filename);
233 rfc1524_entry_delete(&entry);
234 mutt_error (_("No mailcap edit entry for %s"), type);
245 rfc1524_entry_delete(&entry);
250 /* for compatibility with metamail */
251 static int is_mmnoask (const char *buf)
253 char tmp[LONG_STRING], *p, *q;
256 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
257 if (m_strcmp(p, "1") == 0)
260 m_strcpy(tmp, sizeof(tmp), p);
263 while ((p = strtok (p, ",")) != NULL) {
264 if ((q = strrchr (p, '/')) != NULL) {
265 if (*(q + 1) == '*') {
266 if (ascii_strncasecmp (buf, p, q - p) == 0)
269 if (ascii_strcasecmp (buf, p) == 0)
274 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
285 void mutt_check_lookup_list (BODY * b, char *type, int len)
287 string_list_t *t = MimeLookupList;
290 for (; t; t = t->next) {
291 i = m_strlen(t->data) - 1;
292 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
293 ascii_strncasecmp (type, t->data, i) == 0) ||
294 ascii_strcasecmp (type, t->data) == 0) {
301 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
302 snprintf (type, len, "%s/%s",
303 n == TYPEAUDIO ? "audio" :
304 n == TYPEAPPLICATION ? "application" :
305 n == TYPEIMAGE ? "image" :
306 n == TYPEMESSAGE ? "message" :
307 n == TYPEMODEL ? "model" :
308 n == TYPEMULTIPART ? "multipart" :
309 n == TYPETEXT ? "text" :
310 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
313 p_delete(&tmp.subtype);
315 p_delete(&tmp.xtype);
320 int mutt_is_autoview (BODY * b, const char *type)
322 string_list_t *t = AutoViewList;
327 snprintf(_type, sizeof(_type), "%s/%s", TYPE(b), b->subtype);
329 m_strcpy(_type, sizeof(_type), type);
331 mutt_check_lookup_list (b, _type, sizeof (_type));
334 if (rfc1524_mailcap_isneeded(b)) {
335 if (option (OPTIMPLICITAUTOVIEW))
338 if (is_mmnoask (type))
342 for (; t; t = t->next) {
343 i = m_strlen(t->data) - 1;
344 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
345 ascii_strncasecmp (type, t->data, i) == 0) ||
346 ascii_strcasecmp (type, t->data) == 0)
353 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
354 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
355 ATTACHPTR ** idx, short idxlen)
357 char tempfile[_POSIX_PATH_MAX] = "";
358 char pagerfile[_POSIX_PATH_MAX] = "";
364 char command[STRING];
365 char descrip[STRING];
367 rfc1524_entry *entry = NULL;
369 int unlink_tempfile = 0;
372 is_message = mutt_is_message_type(a);
373 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
374 !crypt_valid_passphrase (a->hdr->security))
376 use_mailcap = (flag == M_MAILCAP ||
377 (flag == M_REGULAR && rfc1524_mailcap_isneeded(a)));
378 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
381 entry = rfc1524_entry_new();
382 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
383 if (flag == M_REGULAR) {
384 /* fallback to view as text */
385 rfc1524_entry_delete(&entry);
386 mutt_error _("No matching mailcap entry found. Viewing as text.");
397 if (!entry->command) {
398 mutt_error _("MIME type not defined. Cannot view attachment.");
402 m_strcpy(command, sizeof(command), entry->command);
405 fname = m_strdup(a->filename);
406 mutt_sanitize_filename (fname, 1);
411 if (rfc1524_expand_filename (entry->nametemplate, fname,
412 tempfile, sizeof (tempfile))) {
413 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
414 /* send case: the file is already there */
415 if (safe_symlink (a->filename, tempfile) == -1) {
416 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
418 m_strcpy(tempfile, sizeof(tempfile), a->filename);
426 else if (fp == NULL) /* send case */
427 m_strcpy(tempfile, sizeof(tempfile), a->filename);
430 /* recv case: we need to save the attachment to a file */
432 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
436 use_pipe = rfc1524_expand_command (a, tempfile, type,
437 command, sizeof (command));
438 use_pager = entry->copiousoutput;
443 pagerfd = m_tempfd(pagerfile, sizeof(pagerfile), NONULL(Tempdir),
444 fp && !use_mailcap ? a->filename : NULL);
454 if (use_pager || use_pipe) {
455 if (use_pager && pagerfd == -1) {
456 mutt_perror ("open");
459 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
462 mutt_perror ("open");
466 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
467 use_pipe ? tempfd : -1,
468 use_pager ? pagerfd : -1,
476 mutt_error _("Cannot create filter");
483 snprintf (descrip, sizeof (descrip),
484 "---Command: %-20.20s Description: %s",
485 command, a->description);
487 snprintf (descrip, sizeof (descrip),
488 "---Command: %-30.30s Attachment: %s", command, type);
491 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
492 option (OPTWAITKEY))) && !use_pager)
493 mutt_any_key_to_continue (NULL);
500 /* interactive command */
501 if (mutt_system (command) ||
502 (entry->needsterminal && option (OPTWAITKEY)))
503 mutt_any_key_to_continue (NULL);
507 /* Don't use mailcap; the attachment is viewed in the pager */
509 if (flag == M_AS_TEXT) {
512 /* just let me see the raw data */
513 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
517 /* Use built-in handler */
518 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
519 * message in case of error */
520 if (mutt_decode_save_attachment (fp, a, pagerfd, M_DISPLAY)) {
521 unset_option (OPTVIEWATTACH);
524 unset_option (OPTVIEWATTACH);
528 m_strcpy(descrip, sizeof(descrip), a->description);
529 else if (a->filename)
530 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
533 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
536 /* We only reach this point if there have been no errors */
546 info.idxlen = idxlen;
549 rc = mutt_do_pager(descrip, pagerfile,
550 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
560 rfc1524_entry_delete(&entry);
561 if (fp && tempfile[0])
562 mutt_unlink (tempfile);
563 else if (unlink_tempfile)
567 mutt_unlink (pagerfile);
572 /* returns 1 on success, 0 on error */
573 int mutt_pipe_attachment(FILE * fp, BODY * b, const char *path, int out)
586 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
588 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
591 mutt_perror (_("Can't create filter"));
597 mutt_decode_attachment (b, &s);
604 if ((ifp = fopen (b->filename, "r")) == NULL) {
605 mutt_perror ("fopen");
610 thepid = mutt_create_filter_fd(path, &ofp, NULL, NULL, -1, out, -1);
612 thepid = mutt_create_filter(path, &ofp, NULL, NULL);
615 mutt_perror (_("Can't create filter"));
621 mutt_copy_stream (ifp, ofp);
634 * check for error exit from child process
636 if (mutt_wait_filter(thepid) != 0)
639 if (rv == 0 || option(OPTWAITKEY))
640 mutt_any_key_to_continue (NULL);
644 static FILE *mutt_save_attachment_open (char *path, int flags)
646 if (flags == M_SAVE_APPEND)
647 return fopen (path, "a");
648 /* be sure not to change the following fopen to safe_fopen
649 * as safe_fopen returns w/ an error if path exists
651 if (flags == M_SAVE_OVERWRITE)
652 return fopen (path, "w");
654 return safe_fopen (path, "w");
657 /* returns 0 on success, -1 on error */
658 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
667 m->encoding != ENCBASE64 &&
668 m->encoding != ENCQUOTEDPRINTABLE &&
669 mutt_is_message_type(m)) {
670 /* message type attachments are written to mail folders. */
672 char buf[HUGE_STRING];
680 hn->msgno = hdr->msgno; /* required for MH/maildir */
683 fseeko (fp, m->offset, 0);
684 if (fgets (buf, sizeof (buf), fp) == NULL)
686 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
689 mx_open_new_message (&ctx, hn,
690 is_from (buf, NULL, 0,
691 NULL) ? 0 : M_ADD_FROM)) == NULL) {
692 mx_close_mailbox (&ctx, NULL);
695 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
697 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
698 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
699 && mx_commit_message (msg, &ctx) == 0)
704 mx_close_message (&msg);
705 mx_close_mailbox (&ctx, NULL);
709 /* In recv mode, extract from folder and decode */
714 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
715 mutt_perror ("fopen");
718 fseeko ((s.fpin = fp), m->offset, 0);
719 mutt_decode_attachment (m, &s);
721 if (m_fclose(&s.fpout) != 0) {
722 mutt_perror ("fclose");
727 /* In send mode, just copy file */
731 if ((ofp = fopen (m->filename, "r")) == NULL) {
732 mutt_perror ("fopen");
736 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
737 mutt_perror ("fopen");
742 if (mutt_copy_stream (ofp, nfp) == -1) {
743 mutt_error _("Write fault!");
756 /* returns 0 on success, -1 on error */
757 int mutt_decode_save_attachment (FILE * fp, BODY * m, int pathfd,
761 unsigned int saved_encoding = 0;
762 BODY *saved_parts = NULL;
763 HEADER *saved_hdr = NULL;
766 s.flags = displaying;
767 s.fpout = fdopen (pathfd, "w");
769 if (s.fpout == NULL) {
771 mutt_perror ("fopen");
776 /* When called from the compose menu, the attachment isn't parsed,
777 * so we need to do it here. */
780 if (stat (m->filename, &st) == -1) {
781 mutt_perror ("stat");
786 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
787 mutt_perror ("fopen");
791 saved_encoding = m->encoding;
792 if (!is_multipart (m))
793 m->encoding = ENC8BIT;
795 m->length = st.st_size;
797 saved_parts = m->parts;
799 mutt_parse_part (s.fpin, m);
801 if (m->noconv || is_multipart (m))
802 s.flags |= M_CHARCONV;
806 s.flags |= M_CHARCONV;
809 mutt_body_handler (m, &s);
814 m->encoding = saved_encoding;
816 header_delete(&m->hdr);
817 m->parts = saved_parts;
826 /* Ok, the difference between send and receive:
827 * recv: BODY->filename is a suggested name, and Context|HEADER points
828 * to the attachment in mailbox which is encooded
829 * send: BODY->filename points to the un-encoded file which contains the
833 int mutt_print_attachment (FILE * fp, BODY * a)
835 char newfile[_POSIX_PATH_MAX] = "";
839 short unlink_newfile = 0;
842 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
844 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
845 char command[_POSIX_PATH_MAX + STRING];
846 rfc1524_entry *entry;
849 entry = rfc1524_entry_new();
850 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
851 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
852 newfile, sizeof (newfile))) {
854 if (safe_symlink (a->filename, newfile) == -1) {
855 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
857 rfc1524_entry_delete(&entry);
860 m_strcpy(newfile, sizeof(newfile), a->filename);
867 /* in recv mode, save file to newfile first */
869 mutt_save_attachment (fp, a, newfile, 0, NULL);
871 m_strcpy(command, sizeof(command), entry->printcommand);
872 piped = rfc1524_expand_command(a, newfile, type, command,
877 /* interactive program */
879 if ((ifp = fopen (newfile, "r")) == NULL) {
880 mutt_perror ("fopen");
881 rfc1524_entry_delete(&entry);
885 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
886 mutt_perror (_("Can't create filter"));
888 rfc1524_entry_delete(&entry);
892 mutt_copy_stream (ifp, fpout);
895 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
896 mutt_any_key_to_continue (NULL);
899 if (mutt_system (command) || option (OPTWAITKEY))
900 mutt_any_key_to_continue (NULL);
904 mutt_unlink (newfile);
905 else if (unlink_newfile)
908 rfc1524_entry_delete(&entry);
912 tok = mime_which_token(type, -1);
913 if (tok == MIME_TEXT_PLAIN || tok == MIME_APPLICATION_POSTSCRIPT) {
914 return mutt_pipe_attachment(fp, a, NONULL (PrintCmd), -1);
917 if (mutt_can_decode (a)) {
918 /* decode and print */
926 newfile_fd = m_tempfd(newfile, sizeof(newfile), NONULL(Tempdir), NULL);
927 if (mutt_decode_save_attachment (fp, a, newfile_fd, M_PRINTING) == 0) {
929 if ((ifp = fopen(newfile, "r")) == NULL) {
930 mutt_perror ("fopen");
936 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
937 mutt_perror (_("Can't create filter"));
942 mutt_copy_stream(ifp, fpout);
946 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
947 mutt_any_key_to_continue (NULL);
953 mutt_unlink (newfile);
956 mutt_error _("I don't know how to print that!");
962 int mutt_attach_check (HEADER* hdr) {
964 char buf[LONG_STRING];
967 regmatch_t pmatch[1];
969 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
970 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
973 while (!found && fgets (buf, sizeof (buf), fp)) {
976 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,