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"))) { /* __FOPEN_CHECKED__ */
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);
64 return a->unlink ? 0 : -1;
68 /* return 1 if require full screen redraw, 0 otherwise */
69 int mutt_compose_attachment (BODY * a)
73 char newfile[_POSIX_PATH_MAX] = "";
74 rfc1524_entry *entry = rfc1524_entry_new();
75 short unlink_newfile = 0;
78 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
79 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
80 if (entry->composecommand || entry->composetypecommand) {
82 if (entry->composetypecommand)
83 m_strcpy(command, sizeof(command), entry->composetypecommand);
85 m_strcpy(command, sizeof(command), entry->composecommand);
86 if (rfc1524_expand_filename (entry->nametemplate,
87 a->filename, newfile, sizeof (newfile))) {
88 if (safe_symlink (a->filename, newfile) == -1) {
89 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
97 m_strcpy(newfile, sizeof(newfile), a->filename);
99 if (rfc1524_expand_command (a, newfile, type,
100 command, sizeof (command))) {
101 /* For now, editing requires a file, no piping */
102 mutt_error _("Mailcap compose entry requires %%s");
108 if ((r = mutt_system (command)) == -1)
109 mutt_error (_("Error running \"%s\"!"), command);
111 if (r != -1 && entry->composetypecommand) {
114 char tempfile[_POSIX_PATH_MAX];
116 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
117 mutt_perror (_("Failure to open file to parse headers."));
122 b = mutt_read_mime_header (fp, 0);
125 parameter_list_wipe(&a->parameter);
126 a->parameter = b->parameter;
129 if (b->description) {
130 p_delete(&a->description);
131 a->description = b->description;
132 b->description = NULL;
135 p_delete(&a->form_name);
136 a->form_name = b->form_name;
140 /* Remove headers by copying out data to another file, then
141 * copying the file back */
142 fseeko (fp, b->offset, 0);
143 mutt_mktemp (tempfile);
144 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
145 mutt_perror (_("Failure to open file to strip headers."));
149 mutt_copy_stream (fp, tfp);
152 mutt_unlink (a->filename);
153 if (mutt_rename_file (tempfile, a->filename) != 0) {
154 mutt_perror (_("Failure to rename file."));
166 rfc1524_entry_delete(&entry);
167 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
179 rfc1524_entry_delete(&entry);
184 * Currently, this only works for send mode, as it assumes that the
185 * BODY->filename actually contains the information. I'm not sure
186 * we want to deal with editing attachments we've already received,
187 * so this should be ok.
189 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
192 int mutt_edit_attachment (BODY * a)
195 char command[STRING];
196 char newfile[_POSIX_PATH_MAX] = "";
197 rfc1524_entry *entry = rfc1524_entry_new();
198 short unlink_newfile = 0;
201 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
202 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
203 if (entry->editcommand) {
205 m_strcpy(command, sizeof(command), entry->editcommand);
206 if (rfc1524_expand_filename (entry->nametemplate,
207 a->filename, newfile, sizeof (newfile))) {
208 if (safe_symlink (a->filename, newfile) == -1) {
209 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
217 m_strcpy(newfile, sizeof(newfile), a->filename);
219 if (rfc1524_expand_command (a, newfile, type,
220 command, sizeof (command))) {
221 /* For now, editing requires a file, no piping */
222 mutt_error _("Mailcap Edit entry requires %%s");
227 if (mutt_system (command) == -1) {
228 mutt_error (_("Error running \"%s\"!"), command);
234 else if (a->type == TYPETEXT) {
235 /* On text, default to editor */
236 mutt_edit_file (NONULL (Editor), a->filename);
239 rfc1524_entry_delete(&entry);
240 mutt_error (_("No mailcap edit entry for %s"), type);
251 rfc1524_entry_delete(&entry);
256 /* for compatibility with metamail */
257 static int is_mmnoask (const char *buf)
259 char tmp[LONG_STRING], *p, *q;
262 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
263 if (m_strcmp(p, "1") == 0)
266 m_strcpy(tmp, sizeof(tmp), p);
269 while ((p = strtok (p, ",")) != NULL) {
270 if ((q = strrchr (p, '/')) != NULL) {
271 if (*(q + 1) == '*') {
272 if (ascii_strncasecmp (buf, p, q - p) == 0)
276 if (ascii_strcasecmp (buf, p) == 0)
282 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
293 void mutt_check_lookup_list (BODY * b, char *type, int len)
295 string_list_t *t = MimeLookupList;
298 for (; t; t = t->next) {
299 i = m_strlen(t->data) - 1;
300 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
301 ascii_strncasecmp (type, t->data, i) == 0) ||
302 ascii_strcasecmp (type, t->data) == 0) {
309 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
310 snprintf (type, len, "%s/%s",
311 n == TYPEAUDIO ? "audio" :
312 n == TYPEAPPLICATION ? "application" :
313 n == TYPEIMAGE ? "image" :
314 n == TYPEMESSAGE ? "message" :
315 n == TYPEMODEL ? "model" :
316 n == TYPEMULTIPART ? "multipart" :
317 n == TYPETEXT ? "text" :
318 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
321 p_delete(&tmp.subtype);
323 p_delete(&tmp.xtype);
328 int mutt_is_autoview (BODY * b, const char *type)
330 string_list_t *t = AutoViewList;
331 char _type[SHORT_STRING];
335 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
337 m_strcpy(_type, sizeof(_type), type);
339 mutt_check_lookup_list (b, _type, sizeof (_type));
342 if (rfc1524_mailcap_isneeded(b)) {
343 if (option (OPTIMPLICITAUTOVIEW))
346 if (is_mmnoask (type))
350 for (; t; t = t->next) {
351 i = m_strlen(t->data) - 1;
352 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
353 ascii_strncasecmp (type, t->data, i) == 0) ||
354 ascii_strcasecmp (type, t->data) == 0)
361 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
362 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
363 ATTACHPTR ** idx, short idxlen)
365 char tempfile[_POSIX_PATH_MAX] = "";
366 char pagerfile[_POSIX_PATH_MAX] = "";
372 char command[STRING];
373 char descrip[STRING];
375 rfc1524_entry *entry = NULL;
377 int unlink_tempfile = 0;
379 is_message = mutt_is_message_type (a->type, a->subtype);
380 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
381 !crypt_valid_passphrase (a->hdr->security))
383 use_mailcap = (flag == M_MAILCAP ||
384 (flag == M_REGULAR && rfc1524_mailcap_isneeded(a)));
385 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
388 entry = rfc1524_entry_new();
389 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
390 if (flag == M_REGULAR) {
391 /* fallback to view as text */
392 rfc1524_entry_delete(&entry);
393 mutt_error _("No matching mailcap entry found. Viewing as text.");
404 if (!entry->command) {
405 mutt_error _("MIME type not defined. Cannot view attachment.");
409 m_strcpy(command, sizeof(command), entry->command);
412 fname = m_strdup(a->filename);
413 mutt_sanitize_filename (fname, 1);
418 if (rfc1524_expand_filename (entry->nametemplate, fname,
419 tempfile, sizeof (tempfile))) {
420 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
421 /* send case: the file is already there */
422 if (safe_symlink (a->filename, tempfile) == -1) {
423 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
425 m_strcpy(tempfile, sizeof(tempfile), a->filename);
433 else if (fp == NULL) /* send case */
434 m_strcpy(tempfile, sizeof(tempfile), a->filename);
437 /* recv case: we need to save the attachment to a file */
439 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
443 use_pipe = rfc1524_expand_command (a, tempfile, type,
444 command, sizeof (command));
445 use_pager = entry->copiousoutput;
449 if (fp && !use_mailcap && a->filename) {
451 m_strcpy(pagerfile, sizeof(pagerfile), a->filename);
452 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
455 mutt_mktemp (pagerfile);
460 int tempfd = -1, pagerfd = -1;
465 if (use_pager || use_pipe) {
467 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
469 mutt_perror ("open");
472 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
475 mutt_perror ("open");
479 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
480 use_pipe ? tempfd : -1,
481 use_pager ? pagerfd : -1,
489 mutt_error _("Cannot create filter");
496 snprintf (descrip, sizeof (descrip),
497 "---Command: %-20.20s Description: %s",
498 command, a->description);
500 snprintf (descrip, sizeof (descrip),
501 "---Command: %-30.30s Attachment: %s", command, type);
504 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
505 option (OPTWAITKEY))) && !use_pager)
506 mutt_any_key_to_continue (NULL);
513 /* interactive command */
514 if (mutt_system (command) ||
515 (entry->needsterminal && option (OPTWAITKEY)))
516 mutt_any_key_to_continue (NULL);
520 /* Don't use mailcap; the attachment is viewed in the pager */
522 if (flag == M_AS_TEXT) {
523 /* just let me see the raw data */
524 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
528 /* Use built-in handler */
529 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
530 * message in case of error */
531 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
532 unset_option (OPTVIEWATTACH);
535 unset_option (OPTVIEWATTACH);
539 m_strcpy(descrip, sizeof(descrip), a->description);
540 else if (a->filename)
541 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
544 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
547 /* We only reach this point if there have been no errors */
557 info.idxlen = idxlen;
560 rc = mutt_do_pager(descrip, pagerfile,
561 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
571 rfc1524_entry_delete(&entry);
572 if (fp && tempfile[0])
573 mutt_unlink (tempfile);
574 else if (unlink_tempfile)
578 mutt_unlink (pagerfile);
583 /* returns 1 on success, 0 on error */
584 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
591 if (outfile && *outfile)
592 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
593 mutt_perror ("open");
604 if (outfile && *outfile)
605 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
607 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
610 mutt_perror (_("Can't create filter"));
616 mutt_decode_attachment (b, &s);
617 safe_fclose (&s.fpout);
624 if ((ifp = fopen (b->filename, "r")) == NULL) {
625 mutt_perror ("fopen");
626 if (outfile && *outfile) {
633 if (outfile && *outfile)
634 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
636 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
639 mutt_perror (_("Can't create filter"));
645 mutt_copy_stream (ifp, ofp);
654 if (outfile && *outfile)
658 * check for error exit from child process
660 if (mutt_wait_filter (thepid) != 0)
663 if (rv == 0 || option (OPTWAITKEY))
664 mutt_any_key_to_continue (NULL);
668 static FILE *mutt_save_attachment_open (char *path, int flags)
670 if (flags == M_SAVE_APPEND)
671 return fopen (path, "a");
672 /* be sure not to change the following fopen to safe_fopen
673 * as safe_fopen returns w/ an error if path exists
675 if (flags == M_SAVE_OVERWRITE)
676 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
678 return safe_fopen (path, "w");
681 /* returns 0 on success, -1 on error */
682 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
691 m->encoding != ENCBASE64 &&
692 m->encoding != ENCQUOTEDPRINTABLE &&
693 mutt_is_message_type (m->type, m->subtype)) {
694 /* message type attachments are written to mail folders. */
696 char buf[HUGE_STRING];
704 hn->msgno = hdr->msgno; /* required for MH/maildir */
707 fseeko (fp, m->offset, 0);
708 if (fgets (buf, sizeof (buf), fp) == NULL)
710 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
713 mx_open_new_message (&ctx, hn,
714 is_from (buf, NULL, 0,
715 NULL) ? 0 : M_ADD_FROM)) == NULL) {
716 mx_close_mailbox (&ctx, NULL);
719 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
721 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
722 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
723 && mx_commit_message (msg, &ctx) == 0)
728 mx_close_message (&msg);
729 mx_close_mailbox (&ctx, NULL);
733 /* In recv mode, extract from folder and decode */
738 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
739 mutt_perror ("fopen");
742 fseeko ((s.fpin = fp), m->offset, 0);
743 mutt_decode_attachment (m, &s);
745 if (fclose (s.fpout) != 0) {
746 mutt_perror ("fclose");
752 /* In send mode, just copy file */
756 if ((ofp = fopen (m->filename, "r")) == NULL) {
757 mutt_perror ("fopen");
761 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
762 mutt_perror ("fopen");
767 if (mutt_copy_stream (ofp, nfp) == -1) {
768 mutt_error _("Write fault!");
781 /* returns 0 on success, -1 on error */
782 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
783 int displaying, int flags)
786 unsigned int saved_encoding = 0;
787 BODY *saved_parts = NULL;
788 HEADER *saved_hdr = NULL;
791 s.flags = displaying;
793 if (flags == M_SAVE_APPEND)
794 s.fpout = fopen (path, "a");
795 else if (flags == M_SAVE_OVERWRITE)
796 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
798 s.fpout = safe_fopen (path, "w");
800 if (s.fpout == NULL) {
801 mutt_perror ("fopen");
806 /* When called from the compose menu, the attachment isn't parsed,
807 * so we need to do it here. */
810 if (stat (m->filename, &st) == -1) {
811 mutt_perror ("stat");
816 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
817 mutt_perror ("fopen");
821 saved_encoding = m->encoding;
822 if (!is_multipart (m))
823 m->encoding = ENC8BIT;
825 m->length = st.st_size;
827 saved_parts = m->parts;
829 mutt_parse_part (s.fpin, m);
831 if (m->noconv || is_multipart (m))
832 s.flags |= M_CHARCONV;
836 s.flags |= M_CHARCONV;
839 mutt_body_handler (m, &s);
844 m->encoding = saved_encoding;
846 header_delete(&m->hdr);
847 m->parts = saved_parts;
856 /* Ok, the difference between send and receive:
857 * recv: BODY->filename is a suggested name, and Context|HEADER points
858 * to the attachment in mailbox which is encooded
859 * send: BODY->filename points to the un-encoded file which contains the
863 int mutt_print_attachment (FILE * fp, BODY * a)
865 char newfile[_POSIX_PATH_MAX] = "";
869 short unlink_newfile = 0;
871 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
873 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
874 char command[_POSIX_PATH_MAX + STRING];
875 rfc1524_entry *entry;
878 entry = rfc1524_entry_new();
879 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
880 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
881 newfile, sizeof (newfile))) {
883 if (safe_symlink (a->filename, newfile) == -1) {
884 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
886 rfc1524_entry_delete(&entry);
889 m_strcpy(newfile, sizeof(newfile), a->filename);
896 /* in recv mode, save file to newfile first */
898 mutt_save_attachment (fp, a, newfile, 0, NULL);
900 m_strcpy(command, sizeof(command), entry->printcommand);
901 piped = rfc1524_expand_command(a, newfile, type, command,
906 /* interactive program */
908 if ((ifp = fopen (newfile, "r")) == NULL) {
909 mutt_perror ("fopen");
910 rfc1524_entry_delete(&entry);
914 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
915 mutt_perror (_("Can't create filter"));
917 rfc1524_entry_delete(&entry);
921 mutt_copy_stream (ifp, fpout);
922 safe_fclose (&fpout);
924 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
925 mutt_any_key_to_continue (NULL);
928 if (mutt_system (command) || option (OPTWAITKEY))
929 mutt_any_key_to_continue (NULL);
933 mutt_unlink (newfile);
934 else if (unlink_newfile)
937 rfc1524_entry_delete(&entry);
941 if (!ascii_strcasecmp ("text/plain", type) ||
942 !ascii_strcasecmp ("application/postscript", type)) {
943 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
945 else if (mutt_can_decode (a)) {
946 /* decode and print */
953 mutt_mktemp (newfile);
954 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
956 if ((ifp = fopen (newfile, "r")) == NULL) {
957 mutt_perror ("fopen");
963 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
964 mutt_perror (_("Can't create filter"));
969 mutt_copy_stream (ifp, fpout);
971 safe_fclose (&fpout);
974 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
975 mutt_any_key_to_continue (NULL);
980 safe_fclose (&fpout);
981 mutt_unlink (newfile);
985 mutt_error _("I don't know how to print that!");
991 int mutt_attach_check (HEADER* hdr) {
993 char buf[LONG_STRING];
996 regmatch_t pmatch[1];
998 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
999 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1002 while (!found && fgets (buf, sizeof (buf), fp)) {
1005 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,