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>
15 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/menu.h>
22 #include "recvattach.h"
27 #include <lib-crypt/crypt.h>
29 int mutt_get_tmp_attachment (BODY * a)
32 char tempfile[_POSIX_PATH_MAX];
33 rfc1524_entry *entry = rfc1524_entry_new();
34 FILE *fpin = NULL, *fpout = NULL;
40 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
41 rfc1524_mailcap_lookup (a, type, entry, 0);
42 rfc1524_expand_filename (entry->nametemplate, a->filename,
43 tempfile, sizeof (tempfile));
45 rfc1524_entry_delete(&entry);
47 if (stat (a->filename, &st) == -1)
50 if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) { /* __FOPEN_CHECKED__ */
51 mutt_copy_stream (fpin, fpout);
52 m_strreplace(&a->filename, tempfile);
55 if (a->stamp >= st.st_mtime)
56 mutt_stamp_attachment (a);
59 mutt_perror(fpin ? tempfile : a->filename);
66 return a->unlink ? 0 : -1;
70 /* return 1 if require full screen redraw, 0 otherwise */
71 int mutt_compose_attachment (BODY * a)
75 char newfile[_POSIX_PATH_MAX] = "";
76 rfc1524_entry *entry = rfc1524_entry_new();
77 short unlink_newfile = 0;
80 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
81 if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) {
82 if (entry->composecommand || entry->composetypecommand) {
84 if (entry->composetypecommand)
85 m_strcpy(command, sizeof(command), entry->composetypecommand);
87 m_strcpy(command, sizeof(command), entry->composecommand);
88 if (rfc1524_expand_filename (entry->nametemplate,
89 a->filename, newfile, sizeof (newfile))) {
90 if (safe_symlink (a->filename, newfile) == -1) {
91 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
99 m_strcpy(newfile, sizeof(newfile), a->filename);
101 if (rfc1524_expand_command (a, newfile, type,
102 command, sizeof (command))) {
103 /* For now, editing requires a file, no piping */
104 mutt_error _("Mailcap compose entry requires %%s");
110 if ((r = mutt_system (command)) == -1)
111 mutt_error (_("Error running \"%s\"!"), command);
113 if (r != -1 && entry->composetypecommand) {
116 char tempfile[_POSIX_PATH_MAX];
118 if ((fp = safe_fopen (a->filename, "r")) == NULL) {
119 mutt_perror (_("Failure to open file to parse headers."));
124 b = mutt_read_mime_header (fp, 0);
127 parameter_list_wipe(&a->parameter);
128 a->parameter = b->parameter;
131 if (b->description) {
132 p_delete(&a->description);
133 a->description = b->description;
134 b->description = NULL;
137 p_delete(&a->form_name);
138 a->form_name = b->form_name;
142 /* Remove headers by copying out data to another file, then
143 * copying the file back */
144 fseeko (fp, b->offset, 0);
145 mutt_mktemp (tempfile);
146 if ((tfp = safe_fopen (tempfile, "w")) == NULL) {
147 mutt_perror (_("Failure to open file to strip headers."));
151 mutt_copy_stream (fp, tfp);
154 mutt_unlink (a->filename);
155 if (mutt_rename_file (tempfile, a->filename) != 0) {
156 mutt_perror (_("Failure to rename file."));
168 rfc1524_entry_delete(&entry);
169 mutt_message (_("No mailcap compose entry for %s, creating empty file."),
181 rfc1524_entry_delete(&entry);
186 * Currently, this only works for send mode, as it assumes that the
187 * BODY->filename actually contains the information. I'm not sure
188 * we want to deal with editing attachments we've already received,
189 * so this should be ok.
191 * Returns 1 if editor found, 0 if not (useful to tell calling menu to
194 int mutt_edit_attachment (BODY * a)
197 char command[STRING];
198 char newfile[_POSIX_PATH_MAX] = "";
199 rfc1524_entry *entry = rfc1524_entry_new();
200 short unlink_newfile = 0;
203 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
204 if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) {
205 if (entry->editcommand) {
207 m_strcpy(command, sizeof(command), entry->editcommand);
208 if (rfc1524_expand_filename (entry->nametemplate,
209 a->filename, newfile, sizeof (newfile))) {
210 if (safe_symlink (a->filename, newfile) == -1) {
211 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
219 m_strcpy(newfile, sizeof(newfile), a->filename);
221 if (rfc1524_expand_command (a, newfile, type,
222 command, sizeof (command))) {
223 /* For now, editing requires a file, no piping */
224 mutt_error _("Mailcap Edit entry requires %%s");
229 if (mutt_system (command) == -1) {
230 mutt_error (_("Error running \"%s\"!"), command);
236 else if (a->type == TYPETEXT) {
237 /* On text, default to editor */
238 mutt_edit_file (NONULL (Editor), a->filename);
241 rfc1524_entry_delete(&entry);
242 mutt_error (_("No mailcap edit entry for %s"), type);
253 rfc1524_entry_delete(&entry);
258 /* for compatibility with metamail */
259 static int is_mmnoask (const char *buf)
261 char tmp[LONG_STRING], *p, *q;
264 if ((p = getenv ("MM_NOASK")) != NULL && *p) {
265 if (m_strcmp(p, "1") == 0)
268 m_strcpy(tmp, sizeof(tmp), p);
271 while ((p = strtok (p, ",")) != NULL) {
272 if ((q = strrchr (p, '/')) != NULL) {
273 if (*(q + 1) == '*') {
274 if (ascii_strncasecmp (buf, p, q - p) == 0)
278 if (ascii_strcasecmp (buf, p) == 0)
284 if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
295 void mutt_check_lookup_list (BODY * b, char *type, int len)
297 string_list_t *t = MimeLookupList;
300 for (; t; t = t->next) {
301 i = m_strlen(t->data) - 1;
302 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
303 ascii_strncasecmp (type, t->data, i) == 0) ||
304 ascii_strcasecmp (type, t->data) == 0) {
311 if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) {
312 snprintf (type, len, "%s/%s",
313 n == TYPEAUDIO ? "audio" :
314 n == TYPEAPPLICATION ? "application" :
315 n == TYPEIMAGE ? "image" :
316 n == TYPEMESSAGE ? "message" :
317 n == TYPEMODEL ? "model" :
318 n == TYPEMULTIPART ? "multipart" :
319 n == TYPETEXT ? "text" :
320 n == TYPEVIDEO ? "video" : "other", tmp.subtype);
323 p_delete(&tmp.subtype);
325 p_delete(&tmp.xtype);
330 int mutt_is_autoview (BODY * b, const char *type)
332 string_list_t *t = AutoViewList;
333 char _type[SHORT_STRING];
337 snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype);
339 m_strcpy(_type, sizeof(_type), type);
341 mutt_check_lookup_list (b, _type, sizeof (_type));
344 if (rfc1524_mailcap_isneeded(b)) {
345 if (option (OPTIMPLICITAUTOVIEW))
348 if (is_mmnoask (type))
352 for (; t; t = t->next) {
353 i = m_strlen(t->data) - 1;
354 if ((i > 0 && t->data[i - 1] == '/' && t->data[i] == '*' &&
355 ascii_strncasecmp (type, t->data, i) == 0) ||
356 ascii_strcasecmp (type, t->data) == 0)
363 /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */
364 int mutt_view_attachment (FILE * fp, BODY * a, int flag, HEADER * hdr,
365 ATTACHPTR ** idx, short idxlen)
367 char tempfile[_POSIX_PATH_MAX] = "";
368 char pagerfile[_POSIX_PATH_MAX] = "";
374 char command[STRING];
375 char descrip[STRING];
377 rfc1524_entry *entry = NULL;
379 int unlink_tempfile = 0;
381 is_message = mutt_is_message_type (a->type, a->subtype);
382 if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
383 !crypt_valid_passphrase (a->hdr->security))
385 use_mailcap = (flag == M_MAILCAP ||
386 (flag == M_REGULAR && rfc1524_mailcap_isneeded(a)));
387 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
390 entry = rfc1524_entry_new();
391 if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
392 if (flag == M_REGULAR) {
393 /* fallback to view as text */
394 rfc1524_entry_delete(&entry);
395 mutt_error _("No matching mailcap entry found. Viewing as text.");
406 if (!entry->command) {
407 mutt_error _("MIME type not defined. Cannot view attachment.");
411 m_strcpy(command, sizeof(command), entry->command);
414 fname = m_strdup(a->filename);
415 mutt_sanitize_filename (fname, 1);
420 if (rfc1524_expand_filename (entry->nametemplate, fname,
421 tempfile, sizeof (tempfile))) {
422 if (fp == NULL && m_strcmp(tempfile, a->filename)) {
423 /* send case: the file is already there */
424 if (safe_symlink (a->filename, tempfile) == -1) {
425 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
427 m_strcpy(tempfile, sizeof(tempfile), a->filename);
435 else if (fp == NULL) /* send case */
436 m_strcpy(tempfile, sizeof(tempfile), a->filename);
439 /* recv case: we need to save the attachment to a file */
441 if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
445 use_pipe = rfc1524_expand_command (a, tempfile, type,
446 command, sizeof (command));
447 use_pager = entry->copiousoutput;
451 if (fp && !use_mailcap && a->filename) {
453 m_strcpy(pagerfile, sizeof(pagerfile), a->filename);
454 mutt_adv_mktemp (NULL, pagerfile, sizeof (pagerfile));
457 mutt_mktemp (pagerfile);
462 int tempfd = -1, pagerfd = -1;
467 if (use_pager || use_pipe) {
469 && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY))
471 mutt_perror ("open");
474 if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
477 mutt_perror ("open");
481 if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
482 use_pipe ? tempfd : -1,
483 use_pager ? pagerfd : -1,
491 mutt_error _("Cannot create filter");
498 snprintf (descrip, sizeof (descrip),
499 "---Command: %-20.20s Description: %s",
500 command, a->description);
502 snprintf (descrip, sizeof (descrip),
503 "---Command: %-30.30s Attachment: %s", command, type);
506 if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
507 option (OPTWAITKEY))) && !use_pager)
508 mutt_any_key_to_continue (NULL);
515 /* interactive command */
516 if (mutt_system (command) ||
517 (entry->needsterminal && option (OPTWAITKEY)))
518 mutt_any_key_to_continue (NULL);
522 /* Don't use mailcap; the attachment is viewed in the pager */
524 if (flag == M_AS_TEXT) {
525 /* just let me see the raw data */
526 if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
530 /* Use built-in handler */
531 set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
532 * message in case of error */
533 if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
534 unset_option (OPTVIEWATTACH);
537 unset_option (OPTVIEWATTACH);
541 m_strcpy(descrip, sizeof(descrip), a->description);
542 else if (a->filename)
543 snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
546 snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
549 /* We only reach this point if there have been no errors */
559 info.idxlen = idxlen;
562 rc = mutt_do_pager(descrip, pagerfile,
563 M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
573 rfc1524_entry_delete(&entry);
574 if (fp && tempfile[0])
575 mutt_unlink (tempfile);
576 else if (unlink_tempfile)
580 mutt_unlink (pagerfile);
585 /* returns 1 on success, 0 on error */
586 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
593 if (outfile && *outfile)
594 if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
595 mutt_perror ("open");
606 if (outfile && *outfile)
607 thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
609 thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
612 mutt_perror (_("Can't create filter"));
618 mutt_decode_attachment (b, &s);
619 safe_fclose (&s.fpout);
626 if ((ifp = fopen (b->filename, "r")) == NULL) {
627 mutt_perror ("fopen");
628 if (outfile && *outfile) {
635 if (outfile && *outfile)
636 thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
638 thepid = mutt_create_filter (path, &ofp, NULL, NULL);
641 mutt_perror (_("Can't create filter"));
647 mutt_copy_stream (ifp, ofp);
656 if (outfile && *outfile)
660 * check for error exit from child process
662 if (mutt_wait_filter (thepid) != 0)
665 if (rv == 0 || option (OPTWAITKEY))
666 mutt_any_key_to_continue (NULL);
670 static FILE *mutt_save_attachment_open (char *path, int flags)
672 if (flags == M_SAVE_APPEND)
673 return fopen (path, "a");
674 /* be sure not to change the following fopen to safe_fopen
675 * as safe_fopen returns w/ an error if path exists
677 if (flags == M_SAVE_OVERWRITE)
678 return fopen (path, "w"); /* __FOPEN_CHECKED__ */
680 return safe_fopen (path, "w");
683 /* returns 0 on success, -1 on error */
684 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
693 m->encoding != ENCBASE64 &&
694 m->encoding != ENCQUOTEDPRINTABLE &&
695 mutt_is_message_type (m->type, m->subtype)) {
696 /* message type attachments are written to mail folders. */
698 char buf[HUGE_STRING];
706 hn->msgno = hdr->msgno; /* required for MH/maildir */
709 fseeko (fp, m->offset, 0);
710 if (fgets (buf, sizeof (buf), fp) == NULL)
712 if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
715 mx_open_new_message (&ctx, hn,
716 is_from (buf, NULL, 0,
717 NULL) ? 0 : M_ADD_FROM)) == NULL) {
718 mx_close_mailbox (&ctx, NULL);
721 if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
723 chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
724 if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
725 && mx_commit_message (msg, &ctx) == 0)
730 mx_close_message (&msg);
731 mx_close_mailbox (&ctx, NULL);
735 /* In recv mode, extract from folder and decode */
740 if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
741 mutt_perror ("fopen");
744 fseeko ((s.fpin = fp), m->offset, 0);
745 mutt_decode_attachment (m, &s);
747 if (fclose (s.fpout) != 0) {
748 mutt_perror ("fclose");
754 /* In send mode, just copy file */
758 if ((ofp = fopen (m->filename, "r")) == NULL) {
759 mutt_perror ("fopen");
763 if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
764 mutt_perror ("fopen");
769 if (mutt_copy_stream (ofp, nfp) == -1) {
770 mutt_error _("Write fault!");
783 /* returns 0 on success, -1 on error */
784 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
785 int displaying, int flags)
788 unsigned int saved_encoding = 0;
789 BODY *saved_parts = NULL;
790 HEADER *saved_hdr = NULL;
793 s.flags = displaying;
795 if (flags == M_SAVE_APPEND)
796 s.fpout = fopen (path, "a");
797 else if (flags == M_SAVE_OVERWRITE)
798 s.fpout = safe_fopen (path, "w"); /* __FOPEN_CHECKED__ */
800 s.fpout = safe_fopen (path, "w");
802 if (s.fpout == NULL) {
803 mutt_perror ("fopen");
808 /* When called from the compose menu, the attachment isn't parsed,
809 * so we need to do it here. */
812 if (stat (m->filename, &st) == -1) {
813 mutt_perror ("stat");
818 if ((s.fpin = fopen (m->filename, "r")) == NULL) {
819 mutt_perror ("fopen");
823 saved_encoding = m->encoding;
824 if (!is_multipart (m))
825 m->encoding = ENC8BIT;
827 m->length = st.st_size;
829 saved_parts = m->parts;
831 mutt_parse_part (s.fpin, m);
833 if (m->noconv || is_multipart (m))
834 s.flags |= M_CHARCONV;
838 s.flags |= M_CHARCONV;
841 mutt_body_handler (m, &s);
846 m->encoding = saved_encoding;
848 header_delete(&m->hdr);
849 m->parts = saved_parts;
858 /* Ok, the difference between send and receive:
859 * recv: BODY->filename is a suggested name, and Context|HEADER points
860 * to the attachment in mailbox which is encooded
861 * send: BODY->filename points to the un-encoded file which contains the
865 int mutt_print_attachment (FILE * fp, BODY * a)
867 char newfile[_POSIX_PATH_MAX] = "";
871 short unlink_newfile = 0;
873 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
875 if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
876 char command[_POSIX_PATH_MAX + STRING];
877 rfc1524_entry *entry;
880 entry = rfc1524_entry_new();
881 rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
882 if (rfc1524_expand_filename (entry->nametemplate, a->filename,
883 newfile, sizeof (newfile))) {
885 if (safe_symlink (a->filename, newfile) == -1) {
886 if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
888 rfc1524_entry_delete(&entry);
891 m_strcpy(newfile, sizeof(newfile), a->filename);
898 /* in recv mode, save file to newfile first */
900 mutt_save_attachment (fp, a, newfile, 0, NULL);
902 m_strcpy(command, sizeof(command), entry->printcommand);
903 piped = rfc1524_expand_command(a, newfile, type, command,
908 /* interactive program */
910 if ((ifp = fopen (newfile, "r")) == NULL) {
911 mutt_perror ("fopen");
912 rfc1524_entry_delete(&entry);
916 if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
917 mutt_perror (_("Can't create filter"));
919 rfc1524_entry_delete(&entry);
923 mutt_copy_stream (ifp, fpout);
924 safe_fclose (&fpout);
926 if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
927 mutt_any_key_to_continue (NULL);
930 if (mutt_system (command) || option (OPTWAITKEY))
931 mutt_any_key_to_continue (NULL);
935 mutt_unlink (newfile);
936 else if (unlink_newfile)
939 rfc1524_entry_delete(&entry);
943 if (!ascii_strcasecmp ("text/plain", type) ||
944 !ascii_strcasecmp ("application/postscript", type)) {
945 return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
947 else if (mutt_can_decode (a)) {
948 /* decode and print */
955 mutt_mktemp (newfile);
956 if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
958 if ((ifp = fopen (newfile, "r")) == NULL) {
959 mutt_perror ("fopen");
965 mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
966 mutt_perror (_("Can't create filter"));
971 mutt_copy_stream (ifp, fpout);
973 safe_fclose (&fpout);
976 if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
977 mutt_any_key_to_continue (NULL);
982 safe_fclose (&fpout);
983 mutt_unlink (newfile);
987 mutt_error _("I don't know how to print that!");
993 int mutt_attach_check (HEADER* hdr) {
995 char buf[LONG_STRING];
998 regmatch_t pmatch[1];
1000 if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
1001 (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
1004 while (!found && fgets (buf, sizeof (buf), fp)) {
1007 if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,