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/lib-lib.h>
26 #include <lib-sys/unix.h>
28 #include <lib-mime/mime.h>
30 #include <lib-ui/curses.h>
31 #include <lib-ui/menu.h>
35 #include "recvattach.h"
40 #include <lib-crypt/crypt.h>
42 int mutt_get_tmp_attachment (BODY * a)
45 char tempfile[_POSIX_PATH_MAX];
46 rfc1524_entry *entry = rfc1524_entry_new();
47 FILE *fpin = NULL, *fpout = NULL;
53 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
54 rfc1524_mailcap_lookup (a, type, entry, 0);
55 rfc1524_expand_filename (entry->nametemplate, a->filename,
56 tempfile, sizeof (tempfile));
58 rfc1524_entry_delete(&entry);
60 if (stat (a->filename, &st) == -1)
63 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
64 mutt_copy_stream (fpin, fpout);
65 m_strreplace(&a->filename, tempfile);
68 if (a->stamp >= st.st_mtime)
69 mutt_stamp_attachment (a);
72 mutt_perror(fpin ? tempfile : a->filename);
79 return a->unlink ? 0 : -1;
83 /* return 1 if require full screen redraw, 0 otherwise */
84 int mutt_compose_attachment (BODY * a)
88 char newfile[_POSIX_PATH_MAX] = "";
89 rfc1524_entry *entry = rfc1524_entry_new();
90 short unlink_newfile = 0;
93 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
94 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
95 if (entry->composecommand || entry->composetypecommand) {
97 if (entry->composetypecommand)
98 m_strcpy(command, sizeof(command), entry->composetypecommand);
100 m_strcpy(command, sizeof(command), entry->composecommand);
101 if (rfc1524_expand_filename (entry->nametemplate,
102 a->filename, newfile, sizeof (newfile))) {
103 if (safe_symlink (a->filename, newfile) == -1) {
104 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
112 m_strcpy(newfile, sizeof(newfile), a->filename);
114 if (rfc1524_expand_command (a, newfile, type,
115 command, sizeof (command))) {
116 /* For now, editing requires a file, no piping */
117 mutt_error _("Mailcap compose entry requires %%s");
123 if ((r = mutt_system (command)) == -1)
124 mutt_error (_("Error running \"%s\"!"), command);
126 if (r != -1 && entry->composetypecommand) {
129 char tempfile[_POSIX_PATH_MAX];
131 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
132 mutt_perror (_("Failure to open file to parse headers."));
137 b = mutt_read_mime_header (fp, 0);
140 parameter_list_wipe(&a->parameter);
141 a->parameter = b->parameter;
144 if (b->description) {
145 p_delete(&a->description);
146 a->description = b->description;
147 b->description = NULL;
150 p_delete(&a->form_name);
151 a->form_name = b->form_name;
155 /* Remove headers by copying out data to another file, then
156 * copying the file back */
157 fseeko (fp, b->offset, 0);
158 mutt_mktemp (tempfile);
159 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
160 mutt_perror (_("Failure to open file to strip headers."));
164 mutt_copy_stream (fp, tfp);
167 mutt_unlink (a->filename);
168 if (mutt_rename_file (tempfile, a->filename) != 0) {
169 mutt_perror (_("Failure to rename file."));
181 rfc1524_entry_delete(&entry);
182 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
194 rfc1524_entry_delete(&entry);
199 * Currently, this only works for send mode, as it assumes that the
200 * BODY->filename actually contains the information. I'm not sure
201 * we want to deal with editing attachments we've already received,
202 * so this should be ok.
204 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
207 int mutt_edit_attachment (BODY * a)
210 char command[STRING];
211 char newfile[_POSIX_PATH_MAX] = "";
212 rfc1524_entry *entry = rfc1524_entry_new();
213 short unlink_newfile = 0;
216 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
217 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
218 if (entry->editcommand) {
220 m_strcpy(command, sizeof(command), entry->editcommand);
221 if (rfc1524_expand_filename (entry->nametemplate,
222 a->filename, newfile, sizeof (newfile))) {
223 if (safe_symlink (a->filename, newfile) == -1) {
224 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
232 m_strcpy(newfile, sizeof(newfile), a->filename);
234 if (rfc1524_expand_command (a, newfile, type,
235 command, sizeof (command))) {
236 /* For now, editing requires a file, no piping */
237 mutt_error _("Mailcap Edit entry requires %%s");
242 if (mutt_system (command) == -1) {
243 mutt_error (_("Error running \"%s\"!"), command);
249 else if (a->type == TYPETEXT) {
250 /* On text, default to editor */
251 mutt_edit_file (NONULL (Editor), a->filename);
254 rfc1524_entry_delete(&entry);
255 mutt_error (_("No mailcap edit entry for %s"), type);
266 rfc1524_entry_delete(&entry);
271 /* for compatibility with metamail */
272 static int is_mmnoask (const char *buf)
274 char tmp[LONG_STRING], *p, *q;
277 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
278 if (m_strcmp(p, "1") == 0)
281 m_strcpy(tmp, sizeof(tmp), p);
284 while ((p = strtok (p, ",")) != NULL) {
285 if ((q = strrchr (p, '/')) != NULL) {
286 if (*(q + 1) == '*') {
287 if (ascii_strncasecmp (buf, p, q - p) == 0)
291 if (ascii_strcasecmp (buf, p) == 0)
297 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
308 void mutt_check_lookup_list (BODY * b, char *type, int len)
310 string_list_t *t = MimeLookupList;
313 for (; t; t = t->next) {
314 i = m_strlen(t->data) - 1;
315 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
316 ascii_strncasecmp (type, t->data, i) == 0) ||
317 ascii_strcasecmp (type, t->data) == 0) {
324 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
325 snprintf (type, len, "%s/%s",
326 n == TYPEAUDIO ? "audio" :
327 n == TYPEAPPLICATION ? "application" :
328 n == TYPEIMAGE ? "image" :
329 n == TYPEMESSAGE ? "message" :
330 n == TYPEMODEL ? "model" :
331 n == TYPEMULTIPART ? "multipart" :
332 n == TYPETEXT ? "text" :
333 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
336 p_delete(&tmp.subtype);
338 p_delete(&tmp.xtype);
343 int mutt_is_autoview (BODY * b, const char *type)
345 string_list_t *t = AutoViewList;
346 char _type[SHORT_STRING];
350 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
352 m_strcpy(_type, sizeof(_type), type);
354 mutt_check_lookup_list (b, _type, sizeof (_type));
357 if (rfc1524_mailcap_isneeded(b)) {
358 if (option (OPTIMPLICITAUTOVIEW))
361 if (is_mmnoask (type))
365 for (; t; t = t->next) {
366 i = m_strlen(t->data) - 1;
367 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
368 ascii_strncasecmp (type, t->data, i) == 0) ||
369 ascii_strcasecmp (type, t->data) == 0)
376 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
377 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
378 ATTACHPTR ** idx, short idxlen)
380 char tempfile[_POSIX_PATH_MAX] = "";
381 char pagerfile[_POSIX_PATH_MAX] = "";
387 char command[STRING];
388 char descrip[STRING];
390 rfc1524_entry *entry = NULL;
392 int unlink_tempfile = 0;
394 is_message = mutt_is_message_type (a->type, a->subtype);
395 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
396 !crypt_valid_passphrase (a->hdr->security))
398 use_mailcap = (flag == M_MAILCAP ||
399 (flag == M_REGULAR && rfc1524_mailcap_isneeded(a)));
400 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
403 entry = rfc1524_entry_new();
404 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
405 if (flag == M_REGULAR) {
406 /* fallback to view as text */
407 rfc1524_entry_delete(&entry);
408 mutt_error _("No matching mailcap entry found. Viewing as text.");
419 if (!entry->command) {
420 mutt_error _("MIME type not defined. Cannot view attachment.");
424 m_strcpy(command, sizeof(command), entry->command);
427 fname = m_strdup(a->filename);
428 mutt_sanitize_filename (fname, 1);
433 if (rfc1524_expand_filename (entry->nametemplate, fname,
434 tempfile, sizeof (tempfile))) {
435 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
436 /* send case: the file is already there */
437 if (safe_symlink (a->filename, tempfile) == -1) {
438 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
440 m_strcpy(tempfile, sizeof(tempfile), a->filename);
448 else if (fp == NULL) /* send case */
449 m_strcpy(tempfile, sizeof(tempfile), a->filename);
452 /* recv case: we need to save the attachment to a file */
454 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
458 use_pipe = rfc1524_expand_command (a, tempfile, type,
459 command, sizeof (command));
460 use_pager = entry->copiousoutput;
464 if (fp && !use_mailcap && a->filename) {
466 m_strcpy(pagerfile, sizeof(pagerfile), a->filename);
467 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
470 mutt_mktemp (pagerfile);
475 int tempfd = -1, pagerfd = -1;
480 if (use_pager || use_pipe) {
482 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
484 mutt_perror ("open");
487 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
490 mutt_perror ("open");
494 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
495 use_pipe ? tempfd : -1,
496 use_pager ? pagerfd : -1,
504 mutt_error _("Cannot create filter");
511 snprintf (descrip, sizeof (descrip),
512 "---Command: %-20.20s Description: %s",
513 command, a->description);
515 snprintf (descrip, sizeof (descrip),
516 "---Command: %-30.30s Attachment: %s", command, type);
519 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
520 option (OPTWAITKEY))) && !use_pager)
521 mutt_any_key_to_continue (NULL);
528 /* interactive command */
529 if (mutt_system (command) ||
530 (entry->needsterminal && option (OPTWAITKEY)))
531 mutt_any_key_to_continue (NULL);
535 /* Don't use mailcap; the attachment is viewed in the pager */
537 if (flag == M_AS_TEXT) {
538 /* just let me see the raw data */
539 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
543 /* Use built-in handler */
544 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
545 * message in case of error */
546 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
547 unset_option (OPTVIEWATTACH);
550 unset_option (OPTVIEWATTACH);
554 m_strcpy(descrip, sizeof(descrip), a->description);
555 else if (a->filename)
556 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
559 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
562 /* We only reach this point if there have been no errors */
572 info.idxlen = idxlen;
575 rc = mutt_do_pager(descrip, pagerfile,
576 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
586 rfc1524_entry_delete(&entry);
587 if (fp && tempfile[0])
588 mutt_unlink (tempfile);
589 else if (unlink_tempfile)
593 mutt_unlink (pagerfile);
598 /* returns 1 on success, 0 on error */
599 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
606 if (outfile && *outfile)
607 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
608 mutt_perror ("open");
619 if (outfile && *outfile)
620 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
622 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
625 mutt_perror (_("Can't create filter"));
631 mutt_decode_attachment (b, &s);
632 safe_fclose (&s.fpout);
639 if ((ifp = fopen (b->filename, "r")) == NULL) {
640 mutt_perror ("fopen");
641 if (outfile && *outfile) {
648 if (outfile && *outfile)
649 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
651 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
654 mutt_perror (_("Can't create filter"));
660 mutt_copy_stream (ifp, ofp);
669 if (outfile && *outfile)
673 * check for error exit from child process
675 if (mutt_wait_filter (thepid) != 0)
678 if (rv == 0 || option (OPTWAITKEY))
679 mutt_any_key_to_continue (NULL);
683 static FILE *mutt_save_attachment_open (char *path, int flags)
685 if (flags == M_SAVE_APPEND)
686 return fopen (path, "a");
687 /* be sure not to change the following fopen to safe_fopen
688 * as safe_fopen returns w/ an error if path exists
690 if (flags == M_SAVE_OVERWRITE)
691 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
693 return safe_fopen (path, "w");
696 /* returns 0 on success, -1 on error */
697 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
706 m->encoding != ENCBASE64 &&
707 m->encoding != ENCQUOTEDPRINTABLE &&
708 mutt_is_message_type (m->type, m->subtype)) {
709 /* message type attachments are written to mail folders. */
711 char buf[HUGE_STRING];
719 hn->msgno = hdr->msgno; /* required for MH/maildir */
722 fseeko (fp, m->offset, 0);
723 if (fgets (buf, sizeof (buf), fp) == NULL)
725 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
728 mx_open_new_message (&ctx, hn,
729 is_from (buf, NULL, 0,
730 NULL) ? 0 : M_ADD_FROM)) == NULL) {
731 mx_close_mailbox (&ctx, NULL);
734 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
736 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
737 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
738 && mx_commit_message (msg, &ctx) == 0)
743 mx_close_message (&msg);
744 mx_close_mailbox (&ctx, NULL);
748 /* In recv mode, extract from folder and decode */
753 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
754 mutt_perror ("fopen");
757 fseeko ((s.fpin = fp), m->offset, 0);
758 mutt_decode_attachment (m, &s);
760 if (fclose (s.fpout) != 0) {
761 mutt_perror ("fclose");
767 /* In send mode, just copy file */
771 if ((ofp = fopen (m->filename, "r")) == NULL) {
772 mutt_perror ("fopen");
776 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
777 mutt_perror ("fopen");
782 if (mutt_copy_stream (ofp, nfp) == -1) {
783 mutt_error _("Write fault!");
796 /* returns 0 on success, -1 on error */
797 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
798 int displaying, int flags)
801 unsigned int saved_encoding = 0;
802 BODY *saved_parts = NULL;
803 HEADER *saved_hdr = NULL;
806 s.flags = displaying;
808 if (flags == M_SAVE_APPEND)
809 s.fpout = fopen (path, "a");
810 else if (flags == M_SAVE_OVERWRITE)
811 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
813 s.fpout = safe_fopen (path, "w");
815 if (s.fpout == NULL) {
816 mutt_perror ("fopen");
821 /* When called from the compose menu, the attachment isn't parsed,
822 * so we need to do it here. */
825 if (stat (m->filename, &st) == -1) {
826 mutt_perror ("stat");
831 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
832 mutt_perror ("fopen");
836 saved_encoding = m->encoding;
837 if (!is_multipart (m))
838 m->encoding = ENC8BIT;
840 m->length = st.st_size;
842 saved_parts = m->parts;
844 mutt_parse_part (s.fpin, m);
846 if (m->noconv || is_multipart (m))
847 s.flags |= M_CHARCONV;
851 s.flags |= M_CHARCONV;
854 mutt_body_handler (m, &s);
859 m->encoding = saved_encoding;
861 header_delete(&m->hdr);
862 m->parts = saved_parts;
871 /* Ok, the difference between send and receive:
872 * recv: BODY->filename is a suggested name, and Context|HEADER points
873 * to the attachment in mailbox which is encooded
874 * send: BODY->filename points to the un-encoded file which contains the
878 int mutt_print_attachment (FILE * fp, BODY * a)
880 char newfile[_POSIX_PATH_MAX] = "";
884 short unlink_newfile = 0;
886 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
888 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
889 char command[_POSIX_PATH_MAX + STRING];
890 rfc1524_entry *entry;
893 entry = rfc1524_entry_new();
894 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
895 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
896 newfile, sizeof (newfile))) {
898 if (safe_symlink (a->filename, newfile) == -1) {
899 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
901 rfc1524_entry_delete(&entry);
904 m_strcpy(newfile, sizeof(newfile), a->filename);
911 /* in recv mode, save file to newfile first */
913 mutt_save_attachment (fp, a, newfile, 0, NULL);
915 m_strcpy(command, sizeof(command), entry->printcommand);
916 piped = rfc1524_expand_command(a, newfile, type, command,
921 /* interactive program */
923 if ((ifp = fopen (newfile, "r")) == NULL) {
924 mutt_perror ("fopen");
925 rfc1524_entry_delete(&entry);
929 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
930 mutt_perror (_("Can't create filter"));
932 rfc1524_entry_delete(&entry);
936 mutt_copy_stream (ifp, fpout);
937 safe_fclose (&fpout);
939 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
940 mutt_any_key_to_continue (NULL);
943 if (mutt_system (command) || option (OPTWAITKEY))
944 mutt_any_key_to_continue (NULL);
948 mutt_unlink (newfile);
949 else if (unlink_newfile)
952 rfc1524_entry_delete(&entry);
956 if (!ascii_strcasecmp ("text/plain", type) ||
957 !ascii_strcasecmp ("application/postscript", type)) {
958 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
960 else if (mutt_can_decode (a)) {
961 /* decode and print */
968 mutt_mktemp (newfile);
969 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
971 if ((ifp = fopen (newfile, "r")) == NULL) {
972 mutt_perror ("fopen");
978 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
979 mutt_perror (_("Can't create filter"));
984 mutt_copy_stream (ifp, fpout);
986 safe_fclose (&fpout);
989 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
990 mutt_any_key_to_continue (NULL);
995 safe_fclose (&fpout);
996 mutt_unlink (newfile);
1000 mutt_error _("I don't know how to print that!");
1006 int mutt_attach_check (HEADER* hdr) {
1008 char buf[LONG_STRING];
1011 regmatch_t pmatch[1];
1013 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1014 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1017 while (!found && fgets (buf, sizeof (buf), fp)) {
1020 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,