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);
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->type, a->subtype);
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;
448 if (fp && !use_mailcap && a->filename) {
450 m_strcpy(pagerfile, sizeof(pagerfile), a->filename);
451 pagerfd = m_tempfd(pagerfile, sizeof(pagerfile), NONULL(Tempdir), pagerfile);
453 pagerfd = m_tempfd(pagerfile, sizeof(pagerfile), NONULL(Tempdir), NULL);
464 if (use_pager || use_pipe) {
465 if (use_pager && pagerfd == -1) {
466 mutt_perror ("open");
469 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
472 mutt_perror ("open");
476 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
477 use_pipe ? tempfd : -1,
478 use_pager ? pagerfd : -1,
486 mutt_error _("Cannot create filter");
493 snprintf (descrip, sizeof (descrip),
494 "---Command: %-20.20s Description: %s",
495 command, a->description);
497 snprintf (descrip, sizeof (descrip),
498 "---Command: %-30.30s Attachment: %s", command, type);
501 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
502 option (OPTWAITKEY))) && !use_pager)
503 mutt_any_key_to_continue (NULL);
510 /* interactive command */
511 if (mutt_system (command) ||
512 (entry->needsterminal && option (OPTWAITKEY)))
513 mutt_any_key_to_continue (NULL);
517 /* Don't use mailcap; the attachment is viewed in the pager */
519 if (flag == M_AS_TEXT) {
520 /* just let me see the raw data */
521 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
525 /* Use built-in handler */
526 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
527 * message in case of error */
528 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
529 unset_option (OPTVIEWATTACH);
532 unset_option (OPTVIEWATTACH);
536 m_strcpy(descrip, sizeof(descrip), a->description);
537 else if (a->filename)
538 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
541 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
544 /* We only reach this point if there have been no errors */
554 info.idxlen = idxlen;
557 rc = mutt_do_pager(descrip, pagerfile,
558 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
568 rfc1524_entry_delete(&entry);
569 if (fp && tempfile[0])
570 mutt_unlink (tempfile);
571 else if (unlink_tempfile)
575 mutt_unlink (pagerfile);
580 /* returns 1 on success, 0 on error */
581 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
588 if (outfile && *outfile)
589 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
590 mutt_perror ("open");
601 if (outfile && *outfile)
602 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
604 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
607 mutt_perror (_("Can't create filter"));
613 mutt_decode_attachment (b, &s);
621 if ((ifp = fopen (b->filename, "r")) == NULL) {
622 mutt_perror ("fopen");
623 if (outfile && *outfile) {
630 if (outfile && *outfile)
631 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
633 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
636 mutt_perror (_("Can't create filter"));
642 mutt_copy_stream (ifp, ofp);
651 if (outfile && *outfile)
655 * check for error exit from child process
657 if (mutt_wait_filter (thepid) != 0)
660 if (rv == 0 || option (OPTWAITKEY))
661 mutt_any_key_to_continue (NULL);
665 static FILE *mutt_save_attachment_open (char *path, int flags)
667 if (flags == M_SAVE_APPEND)
668 return fopen (path, "a");
669 /* be sure not to change the following fopen to safe_fopen
670 * as safe_fopen returns w/ an error if path exists
672 if (flags == M_SAVE_OVERWRITE)
673 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
675 return safe_fopen (path, "w");
678 /* returns 0 on success, -1 on error */
679 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
688 m->encoding != ENCBASE64 &&
689 m->encoding != ENCQUOTEDPRINTABLE &&
690 mutt_is_message_type (m->type, m->subtype)) {
691 /* message type attachments are written to mail folders. */
693 char buf[HUGE_STRING];
701 hn->msgno = hdr->msgno; /* required for MH/maildir */
704 fseeko (fp, m->offset, 0);
705 if (fgets (buf, sizeof (buf), fp) == NULL)
707 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
710 mx_open_new_message (&ctx, hn,
711 is_from (buf, NULL, 0,
712 NULL) ? 0 : M_ADD_FROM)) == NULL) {
713 mx_close_mailbox (&ctx, NULL);
716 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
718 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
719 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
720 && mx_commit_message (msg, &ctx) == 0)
725 mx_close_message (&msg);
726 mx_close_mailbox (&ctx, NULL);
730 /* In recv mode, extract from folder and decode */
735 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
736 mutt_perror ("fopen");
739 fseeko ((s.fpin = fp), m->offset, 0);
740 mutt_decode_attachment (m, &s);
742 if (m_fclose(&s.fpout) != 0) {
743 mutt_perror ("fclose");
748 /* In send mode, just copy file */
752 if ((ofp = fopen (m->filename, "r")) == NULL) {
753 mutt_perror ("fopen");
757 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
758 mutt_perror ("fopen");
763 if (mutt_copy_stream (ofp, nfp) == -1) {
764 mutt_error _("Write fault!");
777 /* returns 0 on success, -1 on error */
778 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
779 int displaying, int flags)
782 unsigned int saved_encoding = 0;
783 BODY *saved_parts = NULL;
784 HEADER *saved_hdr = NULL;
787 s.flags = displaying;
789 if (flags == M_SAVE_APPEND)
790 s.fpout = fopen (path, "a");
791 else if (flags == M_SAVE_OVERWRITE)
792 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
794 s.fpout = safe_fopen (path, "w");
796 if (s.fpout == NULL) {
797 mutt_perror ("fopen");
802 /* When called from the compose menu, the attachment isn't parsed,
803 * so we need to do it here. */
806 if (stat (m->filename, &st) == -1) {
807 mutt_perror ("stat");
812 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
813 mutt_perror ("fopen");
817 saved_encoding = m->encoding;
818 if (!is_multipart (m))
819 m->encoding = ENC8BIT;
821 m->length = st.st_size;
823 saved_parts = m->parts;
825 mutt_parse_part (s.fpin, m);
827 if (m->noconv || is_multipart (m))
828 s.flags |= M_CHARCONV;
832 s.flags |= M_CHARCONV;
835 mutt_body_handler (m, &s);
840 m->encoding = saved_encoding;
842 header_delete(&m->hdr);
843 m->parts = saved_parts;
852 /* Ok, the difference between send and receive:
853 * recv: BODY->filename is a suggested name, and Context|HEADER points
854 * to the attachment in mailbox which is encooded
855 * send: BODY->filename points to the un-encoded file which contains the
859 int mutt_print_attachment (FILE * fp, BODY * a)
861 char newfile[_POSIX_PATH_MAX] = "";
865 short unlink_newfile = 0;
867 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
869 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
870 char command[_POSIX_PATH_MAX + STRING];
871 rfc1524_entry *entry;
874 entry = rfc1524_entry_new();
875 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
876 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
877 newfile, sizeof (newfile))) {
879 if (safe_symlink (a->filename, newfile) == -1) {
880 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
882 rfc1524_entry_delete(&entry);
885 m_strcpy(newfile, sizeof(newfile), a->filename);
892 /* in recv mode, save file to newfile first */
894 mutt_save_attachment (fp, a, newfile, 0, NULL);
896 m_strcpy(command, sizeof(command), entry->printcommand);
897 piped = rfc1524_expand_command(a, newfile, type, command,
902 /* interactive program */
904 if ((ifp = fopen (newfile, "r")) == NULL) {
905 mutt_perror ("fopen");
906 rfc1524_entry_delete(&entry);
910 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
911 mutt_perror (_("Can't create filter"));
913 rfc1524_entry_delete(&entry);
917 mutt_copy_stream (ifp, fpout);
920 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
921 mutt_any_key_to_continue (NULL);
924 if (mutt_system (command) || option (OPTWAITKEY))
925 mutt_any_key_to_continue (NULL);
929 mutt_unlink (newfile);
930 else if (unlink_newfile)
933 rfc1524_entry_delete(&entry);
937 if (!ascii_strcasecmp ("text/plain", type) ||
938 !ascii_strcasecmp ("application/postscript", type)) {
939 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
941 else if (mutt_can_decode (a)) {
942 /* decode and print */
949 mutt_mktemp (newfile);
950 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
952 if ((ifp = fopen (newfile, "r")) == NULL) {
953 mutt_perror ("fopen");
959 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
960 mutt_perror (_("Can't create filter"));
965 mutt_copy_stream (ifp, fpout);
970 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
971 mutt_any_key_to_continue (NULL);
977 mutt_unlink (newfile);
981 mutt_error _("I don't know how to print that!");
987 int mutt_attach_check (HEADER* hdr) {
989 char buf[LONG_STRING];
992 regmatch_t pmatch[1];
994 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
995 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
998 while (!found && fgets (buf, sizeof (buf), fp)) {
1001 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,