drop even more glue.
[apps/madmutt.git] / attach.c
1 /*
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>
5  *
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.
9  */
10
11 #include <lib-lib/lib-lib.h>
12
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>
18
19 #include "mutt.h"
20 #include "handler.h"
21 #include "recvattach.h"
22 #include "keymap.h"
23 #include "pager.h"
24 #include "copy.h"
25 #include <lib-crypt/crypt.h>
26
27 int mutt_get_tmp_attachment (BODY * a)
28 {
29   char type[STRING];
30   char tempfile[_POSIX_PATH_MAX];
31   rfc1524_entry *entry = rfc1524_entry_new();
32   FILE *fpin = NULL, *fpout = NULL;
33   struct stat st;
34
35   if (a->unlink)
36     return 0;
37
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));
42
43   rfc1524_entry_delete(&entry);
44
45   if (stat (a->filename, &st) == -1)
46     return -1;
47
48   if ((fpin = fopen (a->filename, "r")) && (fpout = safe_fopen (tempfile, "w"))) {
49     mutt_copy_stream (fpin, fpout);
50     m_strreplace(&a->filename, tempfile);
51     a->unlink = 1;
52
53     if (a->stamp >= st.st_mtime)
54       mutt_stamp_attachment (a);
55   } else {
56     mutt_perror(fpin ? tempfile : a->filename);
57   }
58
59   m_fclose(&fpin);
60   m_fclose(&fpout);
61
62   return a->unlink ? 0 : -1;
63 }
64
65
66 /* return 1 if require full screen redraw, 0 otherwise */
67 int mutt_compose_attachment (BODY * a)
68 {
69   char type[STRING];
70   char command[STRING];
71   char newfile[_POSIX_PATH_MAX] = "";
72   rfc1524_entry *entry = rfc1524_entry_new();
73   short unlink_newfile = 0;
74   int rc = 0;
75
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) {
79
80       if (entry->composetypecommand)
81         m_strcpy(command, sizeof(command), entry->composetypecommand);
82       else
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)
88               != M_YES)
89             goto bailout;
90         } else {
91           unlink_newfile = 1;
92         }
93       } else {
94         m_strcpy(newfile, sizeof(newfile), a->filename);
95       }
96
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");
101       } else {
102         int r;
103
104         mutt_endwin (NULL);
105         if ((r = mutt_system (command)) == -1)
106           mutt_error (_("Error running \"%s\"!"), command);
107
108         if (r != -1 && entry->composetypecommand) {
109           BODY *b;
110           FILE *fp, *tfp;
111
112           if ((fp = safe_fopen (a->filename, "r")) == NULL) {
113             mutt_perror (_("Failure to open file to parse headers."));
114
115             goto bailout;
116           }
117
118           b = mutt_read_mime_header (fp, 0);
119           if (b) {
120             char tempfile[_POSIX_PATH_MAX];
121
122             if (b->parameter) {
123               parameter_list_wipe(&a->parameter);
124               a->parameter = b->parameter;
125               b->parameter = NULL;
126             }
127             if (b->description) {
128               p_delete(&a->description);
129               a->description = b->description;
130               b->description = NULL;
131             }
132             if (b->form_name) {
133               p_delete(&a->form_name);
134               a->form_name = b->form_name;
135               b->form_name = NULL;
136             }
137
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(MCore.tmpdir), NULL);
142             if (!tfp) {
143               mutt_perror (_("Failure to open file to strip headers."));
144               goto bailout;
145             }
146             mutt_copy_stream (fp, tfp);
147             m_fclose(&fp);
148             m_fclose(&tfp);
149             mutt_unlink (a->filename);
150             if (mutt_rename_file (tempfile, a->filename) != 0) {
151               mutt_perror (_("Failure to rename file."));
152
153               goto bailout;
154             }
155
156             body_list_wipe(&b);
157           }
158         }
159       }
160     }
161   } else {
162     rfc1524_entry_delete(&entry);
163     mutt_message (_("No mailcap compose entry for %s, creating empty file."),
164                   type);
165     return 1;
166   }
167
168   rc = 1;
169
170 bailout:
171
172   if (unlink_newfile)
173     unlink (newfile);
174
175   rfc1524_entry_delete(&entry);
176   return rc;
177 }
178
179 /* 
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.
184  *
185  * Returns 1 if editor found, 0 if not (useful to tell calling menu to
186  * redraw)
187  */
188 int mutt_edit_attachment (BODY * a)
189 {
190   char type[STRING];
191   char command[STRING];
192   char newfile[_POSIX_PATH_MAX] = "";
193   rfc1524_entry *entry = rfc1524_entry_new();
194   short unlink_newfile = 0;
195   int rc = 0;
196
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) {
200
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)
206               != M_YES)
207             goto bailout;
208         } else {
209           unlink_newfile = 1;
210         }
211       } else {
212         m_strcpy(newfile, sizeof(newfile), a->filename);
213       }
214
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");
219         goto bailout;
220       } else {
221         mutt_endwin (NULL);
222         if (mutt_system (command) == -1) {
223           mutt_error (_("Error running \"%s\"!"), command);
224           goto bailout;
225         }
226       }
227     }
228   }
229   else if (a->type == TYPETEXT) {
230     /* On text, default to editor */
231     mutt_edit_file(a->filename);
232   } else {
233     rfc1524_entry_delete(&entry);
234     mutt_error (_("No mailcap edit entry for %s"), type);
235     return 0;
236   }
237
238   rc = 1;
239
240 bailout:
241
242   if (unlink_newfile)
243     unlink (newfile);
244
245   rfc1524_entry_delete(&entry);
246   return rc;
247 }
248
249
250 /* for compatibility with metamail */
251 static int is_mmnoask (const char *buf)
252 {
253   char tmp[LONG_STRING], *p, *q;
254   int lng;
255
256   if ((p = getenv ("MM_NOASK")) != NULL && *p) {
257     if (m_strcmp(p, "1") == 0)
258       return (1);
259
260     m_strcpy(tmp, sizeof(tmp), p);
261     p = tmp;
262
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)
267             return (1);
268         } else {
269           if (ascii_strcasecmp (buf, p) == 0)
270             return (1);
271         }
272       } else {
273         lng = m_strlen(p);
274         if (buf[lng] == '/' && m_strncasecmp(buf, p, lng) == 0)
275           return (1);
276       }
277
278       p = NULL;
279     }
280   }
281
282   return (0);
283 }
284
285 void mutt_check_lookup_list (BODY * b, char *type, int len)
286 {
287   string_list_t *t = MimeLookupList;
288   int i;
289
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) {
295
296       BODY tmp;
297       int n;
298
299       p_clear(&tmp, 1);
300
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);
311       }
312       if (tmp.subtype)
313         p_delete(&tmp.subtype);
314       if (tmp.xtype)
315         p_delete(&tmp.xtype);
316     }
317   }
318 }
319
320 int mutt_is_autoview (BODY * b, const char *type)
321 {
322   string_list_t *t = AutoViewList;
323   char _type[STRING];
324   int i;
325
326   if (!type)
327     snprintf(_type, sizeof(_type), "%s/%s", TYPE(b), b->subtype);
328   else
329     m_strcpy(_type, sizeof(_type), type);
330
331   mutt_check_lookup_list (b, _type, sizeof (_type));
332   type = _type;
333
334   if (rfc1524_mailcap_isneeded(b)) {
335     if (option (OPTIMPLICITAUTOVIEW))
336       return 1;
337
338     if (is_mmnoask (type))
339       return 1;
340   }
341
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)
347       return 1;
348   }
349
350   return 0;
351 }
352
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)
356 {
357   char tempfile[_POSIX_PATH_MAX] = "";
358   char pagerfile[_POSIX_PATH_MAX] = "";
359   int is_message;
360   int use_mailcap;
361   int use_pipe = 0;
362   int use_pager = 1;
363   char type[STRING];
364   char command[STRING];
365   char descrip[STRING];
366   char *fname;
367   rfc1524_entry *entry = NULL;
368   int rc = -1;
369   int unlink_tempfile = 0;
370   int pagerfd = -1;
371
372   is_message = mutt_is_message_type(a);
373   use_mailcap = (flag == M_MAILCAP ||
374                  (flag == M_REGULAR && rfc1524_mailcap_isneeded(a)));
375   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
376
377   if (use_mailcap) {
378     entry = rfc1524_entry_new();
379     if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
380       if (flag == M_REGULAR) {
381         /* fallback to view as text */
382         rfc1524_entry_delete(&entry);
383         mutt_error _("No matching mailcap entry found.  Viewing as text.");
384
385         flag = M_AS_TEXT;
386         use_mailcap = 0;
387       }
388       else
389         goto return_error;
390     }
391   }
392
393   if (use_mailcap) {
394     if (!entry->command) {
395       mutt_error _("MIME type not defined.  Cannot view attachment.");
396
397       goto return_error;
398     }
399     m_strcpy(command, sizeof(command), entry->command);
400
401     if (fp) {
402       fname = m_strdup(a->filename);
403       mutt_sanitize_filename (fname, 1);
404     }
405     else
406       fname = a->filename;
407
408     if (rfc1524_expand_filename (entry->nametemplate, fname,
409                                  tempfile, sizeof (tempfile))) {
410       if (fp == NULL && m_strcmp(tempfile, a->filename)) {
411         /* send case: the file is already there */
412         if (safe_symlink (a->filename, tempfile) == -1) {
413           if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
414               == M_YES)
415             m_strcpy(tempfile, sizeof(tempfile), a->filename);
416           else
417             goto return_error;
418         }
419         else
420           unlink_tempfile = 1;
421       }
422     }
423     else if (fp == NULL)        /* send case */
424       m_strcpy(tempfile, sizeof(tempfile), a->filename);
425
426     if (fp) {
427       /* recv case: we need to save the attachment to a file */
428       p_delete(&fname);
429       if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
430         goto return_error;
431     }
432
433     use_pipe = rfc1524_expand_command (a, tempfile, type,
434                                        command, sizeof (command));
435     use_pager = entry->copiousoutput;
436   }
437
438   if (use_pager) {
439     /* recv case */
440     pagerfd = m_tempfd(pagerfile, sizeof(pagerfile), NONULL(MCore.tmpdir),
441                        fp && !use_mailcap ? a->filename : NULL);
442   }
443
444   if (use_mailcap) {
445     pid_t thepid = 0;
446     int tempfd = -1;
447
448     if (!use_pager)
449       mutt_endwin (NULL);
450
451     if (use_pager || use_pipe) {
452       if (use_pager && pagerfd == -1) {
453         mutt_perror ("open");
454         goto return_error;
455       }
456       if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
457         if (pagerfd != -1)
458           close (pagerfd);
459         mutt_perror ("open");
460         goto return_error;
461       }
462
463       if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
464                                            use_pipe ? tempfd : -1,
465                                            use_pager ? pagerfd : -1,
466                                            -1)) == -1) {
467         if (pagerfd != -1)
468           close (pagerfd);
469
470         if (tempfd != -1)
471           close (tempfd);
472
473         mutt_error _("Cannot create filter");
474
475         goto return_error;
476       }
477
478       if (use_pager) {
479         if (a->description)
480           snprintf (descrip, sizeof (descrip),
481                     "---Command: %-20.20s Description: %s",
482                     command, a->description);
483         else
484           snprintf (descrip, sizeof (descrip),
485                     "---Command: %-30.30s Attachment: %s", command, type);
486       }
487
488       if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
489                                          option (OPTWAITKEY))) && !use_pager)
490         mutt_any_key_to_continue (NULL);
491
492       close (tempfd);
493       close (pagerfd);
494
495     }
496     else {
497       /* interactive command */
498       if (mutt_system (command) ||
499           (entry->needsterminal && option (OPTWAITKEY)))
500         mutt_any_key_to_continue (NULL);
501     }
502   }
503   else {
504     /* Don't use mailcap; the attachment is viewed in the pager */
505
506     if (flag == M_AS_TEXT) {
507       unlink(pagerfile);
508       close(pagerfd);
509       /* just let me see the raw data */
510       if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
511         goto return_error;
512     }
513     else {
514       /* Use built-in handler */
515       set_option (OPTVIEWATTACH);       /* disable the "use 'v' to view this part"
516                                          * message in case of error */
517       if (mutt_decode_save_attachment (fp, a, pagerfd, M_DISPLAY)) {
518         unset_option (OPTVIEWATTACH);
519         goto return_error;
520       }
521       unset_option (OPTVIEWATTACH);
522     }
523
524     if (a->description)
525       m_strcpy(descrip, sizeof(descrip), a->description);
526     else if (a->filename)
527       snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
528                 a->filename, type);
529     else
530       snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
531   }
532
533   /* We only reach this point if there have been no errors */
534
535   if (use_pager) {
536     pager_t info;
537     p_clear(&info, 1);
538
539     info.fp  = fp;
540     info.bdy = a;
541     info.ctx = Context;
542     info.idx = idx;
543     info.idxlen = idxlen;
544     info.hdr = hdr;
545
546     rc = mutt_do_pager(descrip, pagerfile,
547                        M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
548                        &info);
549     *pagerfile = '\0';
550   }
551   else
552     rc = 0;
553
554 return_error:
555
556   if (entry)
557     rfc1524_entry_delete(&entry);
558   if (fp && tempfile[0])
559     mutt_unlink (tempfile);
560   else if (unlink_tempfile)
561     unlink (tempfile);
562
563   if (pagerfile[0])
564     mutt_unlink (pagerfile);
565
566   return rc;
567 }
568
569 /* returns 1 on success, 0 on error */
570 int mutt_pipe_attachment(FILE * fp, BODY * b, const char *path, int out)
571 {
572   pid_t thepid;
573   int rv = 0;
574
575   mutt_endwin (NULL);
576
577   if (fp) {
578     /* recv case */
579     STATE s;
580     p_clear(&s, 1);
581
582     if (out >= 0)
583       thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
584     else
585       thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
586
587     if (thepid < 0) {
588       mutt_perror (_("Can't create filter"));
589
590       goto bail;
591     }
592
593     s.fpin = fp;
594     mutt_decode_attachment (b, &s);
595     m_fclose(&s.fpout);
596   } else {
597     /* send case */
598
599     FILE *ifp, *ofp;
600
601     if ((ifp = fopen (b->filename, "r")) == NULL) {
602       mutt_perror ("fopen");
603       return 0;
604     }
605
606     if (out >= 0)
607       thepid = mutt_create_filter_fd(path, &ofp, NULL, NULL, -1, out, -1);
608     else
609       thepid = mutt_create_filter(path, &ofp, NULL, NULL);
610
611     if (thepid < 0) {
612       mutt_perror (_("Can't create filter"));
613
614       m_fclose(&ifp);
615       goto bail;
616     }
617
618     mutt_copy_stream (ifp, ofp);
619     m_fclose(&ofp);
620     m_fclose(&ifp);
621   }
622
623   rv = 1;
624
625 bail:
626
627   if (out >= 0)
628     close(out);
629
630   /*
631    * check for error exit from child process
632    */
633   if (mutt_wait_filter(thepid) != 0)
634     rv = 0;
635
636   if (rv == 0 || option(OPTWAITKEY))
637     mutt_any_key_to_continue (NULL);
638   return rv;
639 }
640
641 static FILE *mutt_save_attachment_open (char *path, int flags)
642 {
643   if (flags == M_SAVE_APPEND)
644     return fopen (path, "a");
645   /* be sure not to change the following fopen to safe_fopen
646    * as safe_fopen returns w/ an error if path exists
647    */
648   if (flags == M_SAVE_OVERWRITE)
649     return fopen (path, "w");
650
651   return safe_fopen (path, "w");
652 }
653
654 /* returns 0 on success, -1 on error */
655 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
656                           HEADER * hdr)
657 {
658   if (fp) {
659
660     /* recv mode */
661
662     if (hdr &&
663         m->hdr &&
664         m->encoding != ENCBASE64 &&
665         m->encoding != ENCQUOTEDPRINTABLE &&
666         mutt_is_message_type(m)) {
667       /* message type attachments are written to mail folders. */
668
669       char buf[HUGE_STRING];
670       HEADER *hn;
671       CONTEXT ctx;
672       MESSAGE *msg;
673       int chflags = 0;
674       int r = -1;
675
676       hn = m->hdr;
677       hn->msgno = hdr->msgno;   /* required for MH/maildir */
678       hn->read = 1;
679
680       fseeko (fp, m->offset, 0);
681       if (fgets (buf, sizeof (buf), fp) == NULL)
682         return -1;
683       if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
684         return -1;
685       if ((msg =
686            mx_open_new_message (&ctx, hn,
687                                 is_from (buf, NULL, 0,
688                                          NULL) ? 0 : M_ADD_FROM)) == NULL) {
689         mx_close_mailbox (&ctx, NULL);
690         return -1;
691       }
692       if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
693         chflags = CH_FROM;
694       chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
695       if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
696           && mx_commit_message (msg, &ctx) == 0)
697         r = 0;
698       else
699         r = -1;
700
701       mx_close_message (&msg);
702       mx_close_mailbox (&ctx, NULL);
703       return r;
704     }
705     else {
706       /* In recv mode, extract from folder and decode */
707
708       STATE s;
709       p_clear(&s, 1);
710
711       if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
712         mutt_perror ("fopen");
713         return (-1);
714       }
715       fseeko ((s.fpin = fp), m->offset, 0);
716       mutt_decode_attachment (m, &s);
717
718       if (m_fclose(&s.fpout) != 0) {
719         mutt_perror ("fclose");
720         return (-1);
721       }
722     }
723   } else {
724     /* In send mode, just copy file */
725
726     FILE *ofp, *nfp;
727
728     if ((ofp = fopen (m->filename, "r")) == NULL) {
729       mutt_perror ("fopen");
730       return (-1);
731     }
732
733     if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
734       mutt_perror ("fopen");
735       m_fclose(&ofp);
736       return (-1);
737     }
738
739     if (mutt_copy_stream (ofp, nfp) == -1) {
740       mutt_error _("Write fault!");
741
742       m_fclose(&ofp);
743       m_fclose(&nfp);
744       return (-1);
745     }
746     m_fclose(&ofp);
747     m_fclose(&nfp);
748   }
749
750   return 0;
751 }
752
753 /* returns 0 on success, -1 on error */
754 int mutt_decode_save_attachment (FILE * fp, BODY * m, int pathfd,
755                                  int displaying)
756 {
757   STATE s;
758   unsigned int saved_encoding = 0;
759   BODY *saved_parts = NULL;
760   HEADER *saved_hdr = NULL;
761
762   p_clear(&s, 1);
763   s.flags = displaying;
764   s.fpout = fdopen (pathfd, "w");
765
766   if (s.fpout == NULL) {
767     close(pathfd);
768     mutt_perror ("fopen");
769     return (-1);
770   }
771
772   if (fp == NULL) {
773     /* When called from the compose menu, the attachment isn't parsed,
774      * so we need to do it here. */
775     struct stat st;
776
777     if (stat (m->filename, &st) == -1) {
778       mutt_perror ("stat");
779       m_fclose(&s.fpout);
780       return (-1);
781     }
782
783     if ((s.fpin = fopen (m->filename, "r")) == NULL) {
784       mutt_perror ("fopen");
785       return (-1);
786     }
787
788     saved_encoding = m->encoding;
789     if (!is_multipart (m))
790       m->encoding = ENC8BIT;
791
792     m->length = st.st_size;
793     m->offset = 0;
794     saved_parts = m->parts;
795     saved_hdr = m->hdr;
796     mutt_parse_part (s.fpin, m);
797
798     if (m->noconv || is_multipart (m))
799       s.flags |= M_CHARCONV;
800   }
801   else {
802     s.fpin = fp;
803     s.flags |= M_CHARCONV;
804   }
805
806   mutt_body_handler (m, &s);
807
808   m_fclose(&s.fpout);
809   if (fp == NULL) {
810     m->length = 0;
811     m->encoding = saved_encoding;
812     if (saved_parts) {
813       header_delete(&m->hdr);
814       m->parts = saved_parts;
815       m->hdr = saved_hdr;
816     }
817     m_fclose(&s.fpin);
818   }
819
820   return (0);
821 }
822
823 /* Ok, the difference between send and receive:
824  * recv: BODY->filename is a suggested name, and Context|HEADER points
825  *       to the attachment in mailbox which is encooded
826  * send: BODY->filename points to the un-encoded file which contains the 
827  *       attachment
828  */
829
830 int mutt_print_attachment (FILE * fp, BODY * a)
831 {
832   char newfile[_POSIX_PATH_MAX] = "";
833   char type[STRING];
834   pid_t thepid;
835   FILE *ifp, *fpout;
836   short unlink_newfile = 0;
837   int tok;
838
839   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
840
841   if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
842     char command[_POSIX_PATH_MAX + STRING];
843     rfc1524_entry *entry;
844     int piped = FALSE;
845
846     entry = rfc1524_entry_new();
847     rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
848     if (rfc1524_expand_filename (entry->nametemplate, a->filename,
849                                  newfile, sizeof (newfile))) {
850       if (!fp) {
851         if (safe_symlink (a->filename, newfile) == -1) {
852           if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
853               != M_YES) {
854             rfc1524_entry_delete(&entry);
855             return 0;
856           }
857           m_strcpy(newfile, sizeof(newfile), a->filename);
858         }
859         else
860           unlink_newfile = 1;
861       }
862     }
863
864     /* in recv mode, save file to newfile first */
865     if (fp)
866       mutt_save_attachment (fp, a, newfile, 0, NULL);
867
868     m_strcpy(command, sizeof(command), entry->printcommand);
869     piped = rfc1524_expand_command(a, newfile, type, command,
870                                    sizeof(command));
871
872     mutt_endwin (NULL);
873
874     /* interactive program */
875     if (piped) {
876       if ((ifp = fopen (newfile, "r")) == NULL) {
877         mutt_perror ("fopen");
878         rfc1524_entry_delete(&entry);
879         return (0);
880       }
881
882       if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
883         mutt_perror (_("Can't create filter"));
884
885         rfc1524_entry_delete(&entry);
886         m_fclose(&ifp);
887         return 0;
888       }
889       mutt_copy_stream (ifp, fpout);
890       m_fclose(&fpout);
891       m_fclose(&ifp);
892       if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
893         mutt_any_key_to_continue (NULL);
894     }
895     else {
896       if (mutt_system (command) || option (OPTWAITKEY))
897         mutt_any_key_to_continue (NULL);
898     }
899
900     if (fp)
901       mutt_unlink (newfile);
902     else if (unlink_newfile)
903       unlink (newfile);
904
905     rfc1524_entry_delete(&entry);
906     return (1);
907   }
908
909   tok = mime_which_token(type, -1);
910   if (tok == MIME_TEXT_PLAIN || tok == MIME_APPLICATION_POSTSCRIPT) {
911     return mutt_pipe_attachment(fp, a, NONULL (PrintCmd), -1);
912   }
913
914   if (mutt_can_decode (a)) {
915     /* decode and print */
916
917     int rc = 0;
918     int newfile_fd = 0;
919
920     ifp = NULL;
921     fpout = NULL;
922
923     newfile_fd = m_tempfd(newfile, sizeof(newfile), NONULL(MCore.tmpdir), NULL);
924     if (mutt_decode_save_attachment (fp, a, newfile_fd, M_PRINTING) == 0) {
925
926       if ((ifp = fopen(newfile, "r")) == NULL) {
927         mutt_perror ("fopen");
928         goto bail0;
929       }
930
931       mutt_endwin (NULL);
932       if ((thepid =
933            mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
934         mutt_perror (_("Can't create filter"));
935
936         goto bail0;
937       }
938
939       mutt_copy_stream(ifp, fpout);
940       m_fclose(&fpout);
941       m_fclose(&ifp);
942
943       if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
944         mutt_any_key_to_continue (NULL);
945       rc = 1;
946     }
947   bail0:
948     m_fclose(&ifp);
949     m_fclose(&fpout);
950     mutt_unlink (newfile);
951     return rc;
952   } else {
953     mutt_error _("I don't know how to print that!");
954
955     return 0;
956   }
957 }
958
959 int mutt_attach_check (HEADER* hdr) {
960   int found = 0;
961   char buf[LONG_STRING];
962   char *p = NULL;
963   FILE* fp = NULL;
964   regmatch_t pmatch[1];
965
966   if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
967       (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
968     return (0);
969
970   while (!found && fgets (buf, sizeof (buf), fp)) {
971     p = buf;
972     while (p && *p) {
973       if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,
974                   pmatch, 0) == 0) {
975         found = 1;
976         break;
977       }
978       p++;
979     }
980   }
981   m_fclose(&fp);
982
983   return (found);
984 }