more strings simplifications.
[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(Tempdir), 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 (NONULL (Editor), 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   if (is_message && a->hdr && (a->hdr->security & ENCRYPT) &&
374       !crypt_valid_passphrase (a->hdr->security))
375     return (rc);
376   use_mailcap = (flag == M_MAILCAP ||
377                  (flag == M_REGULAR && rfc1524_mailcap_isneeded(a)));
378   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
379
380   if (use_mailcap) {
381     entry = rfc1524_entry_new();
382     if (!rfc1524_mailcap_lookup (a, type, entry, 0)) {
383       if (flag == M_REGULAR) {
384         /* fallback to view as text */
385         rfc1524_entry_delete(&entry);
386         mutt_error _("No matching mailcap entry found.  Viewing as text.");
387
388         flag = M_AS_TEXT;
389         use_mailcap = 0;
390       }
391       else
392         goto return_error;
393     }
394   }
395
396   if (use_mailcap) {
397     if (!entry->command) {
398       mutt_error _("MIME type not defined.  Cannot view attachment.");
399
400       goto return_error;
401     }
402     m_strcpy(command, sizeof(command), entry->command);
403
404     if (fp) {
405       fname = m_strdup(a->filename);
406       mutt_sanitize_filename (fname, 1);
407     }
408     else
409       fname = a->filename;
410
411     if (rfc1524_expand_filename (entry->nametemplate, fname,
412                                  tempfile, sizeof (tempfile))) {
413       if (fp == NULL && m_strcmp(tempfile, a->filename)) {
414         /* send case: the file is already there */
415         if (safe_symlink (a->filename, tempfile) == -1) {
416           if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
417               == M_YES)
418             m_strcpy(tempfile, sizeof(tempfile), a->filename);
419           else
420             goto return_error;
421         }
422         else
423           unlink_tempfile = 1;
424       }
425     }
426     else if (fp == NULL)        /* send case */
427       m_strcpy(tempfile, sizeof(tempfile), a->filename);
428
429     if (fp) {
430       /* recv case: we need to save the attachment to a file */
431       p_delete(&fname);
432       if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1)
433         goto return_error;
434     }
435
436     use_pipe = rfc1524_expand_command (a, tempfile, type,
437                                        command, sizeof (command));
438     use_pager = entry->copiousoutput;
439   }
440
441   if (use_pager) {
442     /* recv case */
443     pagerfd = m_tempfd(pagerfile, sizeof(pagerfile), NONULL(Tempdir),
444                        fp && !use_mailcap ? a->filename : NULL);
445   }
446
447   if (use_mailcap) {
448     pid_t thepid = 0;
449     int tempfd = -1;
450
451     if (!use_pager)
452       mutt_endwin (NULL);
453
454     if (use_pager || use_pipe) {
455       if (use_pager && pagerfd == -1) {
456         mutt_perror ("open");
457         goto return_error;
458       }
459       if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) {
460         if (pagerfd != -1)
461           close (pagerfd);
462         mutt_perror ("open");
463         goto return_error;
464       }
465
466       if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL,
467                                            use_pipe ? tempfd : -1,
468                                            use_pager ? pagerfd : -1,
469                                            -1)) == -1) {
470         if (pagerfd != -1)
471           close (pagerfd);
472
473         if (tempfd != -1)
474           close (tempfd);
475
476         mutt_error _("Cannot create filter");
477
478         goto return_error;
479       }
480
481       if (use_pager) {
482         if (a->description)
483           snprintf (descrip, sizeof (descrip),
484                     "---Command: %-20.20s Description: %s",
485                     command, a->description);
486         else
487           snprintf (descrip, sizeof (descrip),
488                     "---Command: %-30.30s Attachment: %s", command, type);
489       }
490
491       if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
492                                          option (OPTWAITKEY))) && !use_pager)
493         mutt_any_key_to_continue (NULL);
494
495       close (tempfd);
496       close (pagerfd);
497
498     }
499     else {
500       /* interactive command */
501       if (mutt_system (command) ||
502           (entry->needsterminal && option (OPTWAITKEY)))
503         mutt_any_key_to_continue (NULL);
504     }
505   }
506   else {
507     /* Don't use mailcap; the attachment is viewed in the pager */
508
509     if (flag == M_AS_TEXT) {
510       /* just let me see the raw data */
511       if (mutt_save_attachment (fp, a, pagerfile, 0, NULL))
512         goto return_error;
513     }
514     else {
515       /* Use built-in handler */
516       set_option (OPTVIEWATTACH);       /* disable the "use 'v' to view this part"
517                                          * message in case of error */
518       if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) {
519         unset_option (OPTVIEWATTACH);
520         goto return_error;
521       }
522       unset_option (OPTVIEWATTACH);
523     }
524
525     if (a->description)
526       m_strcpy(descrip, sizeof(descrip), a->description);
527     else if (a->filename)
528       snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s",
529                 a->filename, type);
530     else
531       snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
532   }
533
534   /* We only reach this point if there have been no errors */
535
536   if (use_pager) {
537     pager_t info;
538     p_clear(&info, 1);
539
540     info.fp  = fp;
541     info.bdy = a;
542     info.ctx = Context;
543     info.idx = idx;
544     info.idxlen = idxlen;
545     info.hdr = hdr;
546
547     rc = mutt_do_pager(descrip, pagerfile,
548                        M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0),
549                        &info);
550     *pagerfile = '\0';
551   }
552   else
553     rc = 0;
554
555 return_error:
556
557   if (entry)
558     rfc1524_entry_delete(&entry);
559   if (fp && tempfile[0])
560     mutt_unlink (tempfile);
561   else if (unlink_tempfile)
562     unlink (tempfile);
563
564   if (pagerfile[0])
565     mutt_unlink (pagerfile);
566
567   return rc;
568 }
569
570 /* returns 1 on success, 0 on error */
571 int mutt_pipe_attachment (FILE * fp, BODY * b, const char *path,
572                           char *outfile)
573 {
574   pid_t thepid;
575   int out = -1;
576   int rv = 0;
577
578   if (outfile && *outfile)
579     if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) {
580       mutt_perror ("open");
581       return 0;
582     }
583
584   mutt_endwin (NULL);
585
586   if (fp) {
587     /* recv case */
588     STATE s;
589     p_clear(&s, 1);
590
591     if (outfile && *outfile)
592       thepid = mutt_create_filter_fd(path, &s.fpout, NULL, NULL, -1, out, -1);
593     else
594       thepid = mutt_create_filter(path, &s.fpout, NULL, NULL);
595
596     if (thepid < 0) {
597       mutt_perror (_("Can't create filter"));
598
599       goto bail;
600     }
601
602     s.fpin = fp;
603     mutt_decode_attachment (b, &s);
604     m_fclose(&s.fpout);
605   }
606   else {
607     /* send case */
608
609     FILE *ifp, *ofp;
610
611     if ((ifp = fopen (b->filename, "r")) == NULL) {
612       mutt_perror ("fopen");
613       if (outfile && *outfile) {
614         close (out);
615         unlink (outfile);
616       }
617       return 0;
618     }
619
620     if (outfile && *outfile)
621       thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1);
622     else
623       thepid = mutt_create_filter (path, &ofp, NULL, NULL);
624
625     if (thepid < 0) {
626       mutt_perror (_("Can't create filter"));
627
628       m_fclose(&ifp);
629       goto bail;
630     }
631
632     mutt_copy_stream (ifp, ofp);
633     m_fclose(&ofp);
634     m_fclose(&ifp);
635   }
636
637   rv = 1;
638
639 bail:
640
641   if (outfile && *outfile)
642     close (out);
643
644   /*
645    * check for error exit from child process
646    */
647   if (mutt_wait_filter (thepid) != 0)
648     rv = 0;
649
650   if (rv == 0 || option (OPTWAITKEY))
651     mutt_any_key_to_continue (NULL);
652   return rv;
653 }
654
655 static FILE *mutt_save_attachment_open (char *path, int flags)
656 {
657   if (flags == M_SAVE_APPEND)
658     return fopen (path, "a");
659   /* be sure not to change the following fopen to safe_fopen
660    * as safe_fopen returns w/ an error if path exists
661    */
662   if (flags == M_SAVE_OVERWRITE)
663     return fopen (path, "w");
664
665   return safe_fopen (path, "w");
666 }
667
668 /* returns 0 on success, -1 on error */
669 int mutt_save_attachment (FILE * fp, BODY * m, char *path, int flags,
670                           HEADER * hdr)
671 {
672   if (fp) {
673
674     /* recv mode */
675
676     if (hdr &&
677         m->hdr &&
678         m->encoding != ENCBASE64 &&
679         m->encoding != ENCQUOTEDPRINTABLE &&
680         mutt_is_message_type(m)) {
681       /* message type attachments are written to mail folders. */
682
683       char buf[HUGE_STRING];
684       HEADER *hn;
685       CONTEXT ctx;
686       MESSAGE *msg;
687       int chflags = 0;
688       int r = -1;
689
690       hn = m->hdr;
691       hn->msgno = hdr->msgno;   /* required for MH/maildir */
692       hn->read = 1;
693
694       fseeko (fp, m->offset, 0);
695       if (fgets (buf, sizeof (buf), fp) == NULL)
696         return -1;
697       if (mx_open_mailbox (path, M_APPEND | M_QUIET, &ctx) == NULL)
698         return -1;
699       if ((msg =
700            mx_open_new_message (&ctx, hn,
701                                 is_from (buf, NULL, 0,
702                                          NULL) ? 0 : M_ADD_FROM)) == NULL) {
703         mx_close_mailbox (&ctx, NULL);
704         return -1;
705       }
706       if (ctx.magic == M_MBOX || ctx.magic == M_MMDF)
707         chflags = CH_FROM;
708       chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE);
709       if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0
710           && mx_commit_message (msg, &ctx) == 0)
711         r = 0;
712       else
713         r = -1;
714
715       mx_close_message (&msg);
716       mx_close_mailbox (&ctx, NULL);
717       return r;
718     }
719     else {
720       /* In recv mode, extract from folder and decode */
721
722       STATE s;
723       p_clear(&s, 1);
724
725       if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) {
726         mutt_perror ("fopen");
727         return (-1);
728       }
729       fseeko ((s.fpin = fp), m->offset, 0);
730       mutt_decode_attachment (m, &s);
731
732       if (m_fclose(&s.fpout) != 0) {
733         mutt_perror ("fclose");
734         return (-1);
735       }
736     }
737   } else {
738     /* In send mode, just copy file */
739
740     FILE *ofp, *nfp;
741
742     if ((ofp = fopen (m->filename, "r")) == NULL) {
743       mutt_perror ("fopen");
744       return (-1);
745     }
746
747     if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) {
748       mutt_perror ("fopen");
749       m_fclose(&ofp);
750       return (-1);
751     }
752
753     if (mutt_copy_stream (ofp, nfp) == -1) {
754       mutt_error _("Write fault!");
755
756       m_fclose(&ofp);
757       m_fclose(&nfp);
758       return (-1);
759     }
760     m_fclose(&ofp);
761     m_fclose(&nfp);
762   }
763
764   return 0;
765 }
766
767 /* returns 0 on success, -1 on error */
768 int mutt_decode_save_attachment (FILE * fp, BODY * m, char *path,
769                                  int displaying, int flags)
770 {
771   STATE s;
772   unsigned int saved_encoding = 0;
773   BODY *saved_parts = NULL;
774   HEADER *saved_hdr = NULL;
775
776   p_clear(&s, 1);
777   s.flags = displaying;
778
779   if (flags == M_SAVE_APPEND)
780     s.fpout = fopen (path, "a");
781   else
782     s.fpout = safe_fopen (path, "w");
783
784   if (s.fpout == NULL) {
785     mutt_perror ("fopen");
786     return (-1);
787   }
788
789   if (fp == NULL) {
790     /* When called from the compose menu, the attachment isn't parsed,
791      * so we need to do it here. */
792     struct stat st;
793
794     if (stat (m->filename, &st) == -1) {
795       mutt_perror ("stat");
796       m_fclose(&s.fpout);
797       return (-1);
798     }
799
800     if ((s.fpin = fopen (m->filename, "r")) == NULL) {
801       mutt_perror ("fopen");
802       return (-1);
803     }
804
805     saved_encoding = m->encoding;
806     if (!is_multipart (m))
807       m->encoding = ENC8BIT;
808
809     m->length = st.st_size;
810     m->offset = 0;
811     saved_parts = m->parts;
812     saved_hdr = m->hdr;
813     mutt_parse_part (s.fpin, m);
814
815     if (m->noconv || is_multipart (m))
816       s.flags |= M_CHARCONV;
817   }
818   else {
819     s.fpin = fp;
820     s.flags |= M_CHARCONV;
821   }
822
823   mutt_body_handler (m, &s);
824
825   m_fclose(&s.fpout);
826   if (fp == NULL) {
827     m->length = 0;
828     m->encoding = saved_encoding;
829     if (saved_parts) {
830       header_delete(&m->hdr);
831       m->parts = saved_parts;
832       m->hdr = saved_hdr;
833     }
834     m_fclose(&s.fpin);
835   }
836
837   return (0);
838 }
839
840 /* Ok, the difference between send and receive:
841  * recv: BODY->filename is a suggested name, and Context|HEADER points
842  *       to the attachment in mailbox which is encooded
843  * send: BODY->filename points to the un-encoded file which contains the 
844  *       attachment
845  */
846
847 int mutt_print_attachment (FILE * fp, BODY * a)
848 {
849   char newfile[_POSIX_PATH_MAX] = "";
850   char type[STRING];
851   pid_t thepid;
852   FILE *ifp, *fpout;
853   short unlink_newfile = 0;
854   int tok;
855
856   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
857
858   if (rfc1524_mailcap_lookup (a, type, NULL, M_PRINT)) {
859     char command[_POSIX_PATH_MAX + STRING];
860     rfc1524_entry *entry;
861     int piped = FALSE;
862
863     entry = rfc1524_entry_new();
864     rfc1524_mailcap_lookup (a, type, entry, M_PRINT);
865     if (rfc1524_expand_filename (entry->nametemplate, a->filename,
866                                  newfile, sizeof (newfile))) {
867       if (!fp) {
868         if (safe_symlink (a->filename, newfile) == -1) {
869           if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES)
870               != M_YES) {
871             rfc1524_entry_delete(&entry);
872             return 0;
873           }
874           m_strcpy(newfile, sizeof(newfile), a->filename);
875         }
876         else
877           unlink_newfile = 1;
878       }
879     }
880
881     /* in recv mode, save file to newfile first */
882     if (fp)
883       mutt_save_attachment (fp, a, newfile, 0, NULL);
884
885     m_strcpy(command, sizeof(command), entry->printcommand);
886     piped = rfc1524_expand_command(a, newfile, type, command,
887                                    sizeof(command));
888
889     mutt_endwin (NULL);
890
891     /* interactive program */
892     if (piped) {
893       if ((ifp = fopen (newfile, "r")) == NULL) {
894         mutt_perror ("fopen");
895         rfc1524_entry_delete(&entry);
896         return (0);
897       }
898
899       if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) {
900         mutt_perror (_("Can't create filter"));
901
902         rfc1524_entry_delete(&entry);
903         m_fclose(&ifp);
904         return 0;
905       }
906       mutt_copy_stream (ifp, fpout);
907       m_fclose(&fpout);
908       m_fclose(&ifp);
909       if (mutt_wait_filter (thepid) || option (OPTWAITKEY))
910         mutt_any_key_to_continue (NULL);
911     }
912     else {
913       if (mutt_system (command) || option (OPTWAITKEY))
914         mutt_any_key_to_continue (NULL);
915     }
916
917     if (fp)
918       mutt_unlink (newfile);
919     else if (unlink_newfile)
920       unlink (newfile);
921
922     rfc1524_entry_delete(&entry);
923     return (1);
924   }
925
926   tok = mime_which_token(type, -1);
927   if (tok == MIME_TEXT_PLAIN || tok == MIME_APPLICATION_POSTSCRIPT) {
928     return (mutt_pipe_attachment (fp, a, NONULL (PrintCmd), NULL));
929   }
930   else if (mutt_can_decode (a)) {
931     /* decode and print */
932
933     int rc = 0;
934
935     ifp = NULL;
936     fpout = NULL;
937
938     mutt_mktemp (newfile);
939     if (mutt_decode_save_attachment (fp, a, newfile, M_PRINTING, 0) == 0) {
940
941       if ((ifp = fopen (newfile, "r")) == NULL) {
942         mutt_perror ("fopen");
943         goto bail0;
944       }
945
946       mutt_endwin (NULL);
947       if ((thepid =
948            mutt_create_filter (NONULL (PrintCmd), &fpout, NULL, NULL)) < 0) {
949         mutt_perror (_("Can't create filter"));
950
951         goto bail0;
952       }
953
954       mutt_copy_stream (ifp, fpout);
955
956       m_fclose(&fpout);
957       m_fclose(&ifp);
958
959       if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY))
960         mutt_any_key_to_continue (NULL);
961       rc = 1;
962     }
963   bail0:
964     m_fclose(&ifp);
965     m_fclose(&fpout);
966     mutt_unlink (newfile);
967     return rc;
968   }
969   else {
970     mutt_error _("I don't know how to print that!");
971
972     return 0;
973   }
974 }
975
976 int mutt_attach_check (HEADER* hdr) {
977   int found = 0;
978   char buf[LONG_STRING];
979   char *p = NULL;
980   FILE* fp = NULL;
981   regmatch_t pmatch[1];
982
983   if (!hdr || !hdr->content || !((regex_t*) AttachRemindRegexp.rx) ||
984       (fp = safe_fopen (hdr->content->filename, "r")) == NULL)
985     return (0);
986
987   while (!found && fgets (buf, sizeof (buf), fp)) {
988     p = buf;
989     while (p && *p) {
990       if (regexec ((regex_t*) AttachRemindRegexp.rx, p, 1,
991                   pmatch, 0) == 0) {
992         found = 1;
993         break;
994       }
995       p++;
996     }
997   }
998   m_fclose(&fp);
999
1000   return (found);
1001 }