Nico Golde:
[apps/madmutt.git] / rfc1524.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */ 
18
19 /* 
20  * rfc1524 defines a format for the Multimedia Mail Configuration, which
21  * is the standard mailcap file format under Unix which specifies what 
22  * external programs should be used to view/compose/edit multimedia files
23  * based on content type.
24  *
25  * This file contains various functions for implementing a fair subset of 
26  * rfc1524.
27  */
28
29 #if HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include "mutt.h"
34 #include "rfc1524.h"
35
36 #include <string.h>
37 #include <stdlib.h>
38 #include <ctype.h>
39
40 #include <sys/stat.h>
41 #include <sys/wait.h>
42 #include <errno.h>
43 #include <unistd.h>
44
45 /* The command semantics include the following:
46  * %s is the filename that contains the mail body data
47  * %t is the content type, like text/plain
48  * %{parameter} is replaced by the parameter value from the content-type field
49  * \% is %
50  * Unsupported rfc1524 parameters: these would probably require some doing
51  * by mutt, and can probably just be done by piping the message to metamail
52  * %n is the integer number of sub-parts in the multipart
53  * %F is "content-type filename" repeated for each sub-part
54  *
55  * In addition, this function returns a 0 if the command works on a file,
56  * and 1 if the command works on a pipe.
57  */
58 int rfc1524_expand_command (BODY *a, char *filename, char *_type,
59     char *command, int clen)
60 {
61   int x=0,y=0;
62   int needspipe = TRUE;
63   char buf[LONG_STRING];
64   char type[LONG_STRING];
65   
66   strfcpy (type, _type, sizeof (type));
67   
68   if (option (OPTMAILCAPSANITIZE))
69     mutt_sanitize_filename (type, 0);
70
71   while (command[x] && x<clen && y<sizeof(buf)) 
72   {
73     if (command[x] == '\\') {
74       x++;
75       buf[y++] = command[x++];
76     }
77     else if (command[x] == '%') 
78     {
79       x++;
80       if (command[x] == '{') 
81       {
82         char param[STRING];
83         char pvalue[STRING];
84         char *_pvalue;
85         int z = 0;
86
87         x++;
88         while (command[x] && command[x] != '}' && z<sizeof(param))
89           param[z++] = command[x++];
90         param[z] = '\0';
91         
92         _pvalue = mutt_get_parameter (param, a->parameter);
93         strfcpy (pvalue, NONULL(_pvalue), sizeof (pvalue));
94         if (option (OPTMAILCAPSANITIZE))
95           mutt_sanitize_filename (pvalue, 0);
96         
97         y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
98       }
99       else if (command[x] == 's' && filename != NULL)
100       {
101         y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
102         needspipe = FALSE;
103       }
104       else if (command[x] == 't')
105       {
106         y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
107       }
108       x++;
109     }
110     else
111       buf[y++] = command[x++];
112   }
113   buf[y] = '\0';
114   strfcpy (command, buf, clen);
115
116   return needspipe;
117 }
118
119 /* NUL terminates a rfc 1524 field,
120  * returns start of next field or NULL */
121 static char *get_field (char *s)
122 {
123   char *ch;
124
125   if (!s)
126     return NULL;
127
128   while ((ch = strpbrk (s, ";\\")) != NULL)
129   {
130     if (*ch == '\\')
131     {
132       s = ch + 1;
133       if (*s)
134         s++;
135     }
136     else
137     {
138       *ch++ = 0;
139       SKIPWS (ch);
140       break;
141     }
142   }
143   mutt_remove_trailing_ws (s);
144   return ch;
145 }
146
147 static int get_field_text (char *field, char **entry,
148                            char *type, char *filename, int line)
149 {
150   field = mutt_skip_whitespace (field);
151   if (*field == '=')
152   {
153     if (entry)
154     {
155       field++;
156       field = mutt_skip_whitespace (field);
157       mutt_str_replace (entry, field);
158     }
159     return 1;
160   }
161   else 
162   {
163     mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
164                 type, filename, line);
165     return 0;
166   }
167 }
168
169 static int rfc1524_mailcap_parse (BODY *a,
170                                   char *filename,
171                                   char *type, 
172                                   rfc1524_entry *entry,
173                                   int opt)
174 {
175   FILE *fp;
176   char *buf = NULL;
177   size_t buflen;
178   char *ch;
179   char *field;
180   int found = FALSE;
181   int copiousoutput;
182   int composecommand;
183   int editcommand;
184   int printcommand;
185   int btlen;
186   int line = 0;
187
188   /* rfc1524 mailcap file is of the format:
189    * base/type; command; extradefs
190    * type can be * for matching all
191    * base with no /type is an implicit wild
192    * command contains a %s for the filename to pass, default to pipe on stdin
193    * extradefs are of the form:
194    *  def1="definition"; def2="define \;";
195    * line wraps with a \ at the end of the line
196    * # for comments
197    */
198
199   /* find length of basetype */
200   if ((ch = strchr (type, '/')) == NULL)
201     return FALSE;
202   btlen = ch - type;
203
204   if ((fp = fopen (filename, "r")) != NULL)
205   {
206     while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line)) != NULL)
207     {
208       /* ignore comments */
209       if (*buf == '#')
210         continue;
211       dprint (2, (debugfile, "mailcap entry: %s\n", buf));
212
213       /* check type */
214       ch = get_field (buf);
215       if (ascii_strcasecmp (buf, type) &&
216           (ascii_strncasecmp (buf, type, btlen) ||
217            (buf[btlen] != 0 &&                  /* implicit wild */
218             mutt_strcmp (buf + btlen, "/*"))))  /* wildsubtype */
219         continue;
220
221       /* next field is the viewcommand */
222       field = ch;
223       ch = get_field (ch);
224       if (entry)
225         entry->command = safe_strdup (field);
226
227       /* parse the optional fields */
228       found = TRUE;
229       copiousoutput = FALSE;
230       composecommand = FALSE;
231       editcommand = FALSE;
232       printcommand = FALSE;
233
234       while (ch)
235       {
236         field = ch;
237         ch = get_field (ch);
238         dprint (2, (debugfile, "field: %s\n", field));
239
240         if (!ascii_strcasecmp (field, "needsterminal"))
241         {
242           if (entry)
243             entry->needsterminal = TRUE;
244         }
245         else if (!ascii_strcasecmp (field, "copiousoutput"))
246         {
247           copiousoutput = TRUE;
248           if (entry)
249             entry->copiousoutput = TRUE;
250         }
251         else if (!ascii_strncasecmp (field, "composetyped", 12))
252         {
253           /* this compare most occur before compose to match correctly */
254           if (get_field_text (field + 12, entry ? &entry->composetypecommand : NULL,
255                               type, filename, line))
256             composecommand = TRUE;
257         }
258         else if (!ascii_strncasecmp (field, "compose", 7))
259         {
260           if (get_field_text (field + 7, entry ? &entry->composecommand : NULL,
261                               type, filename, line))
262             composecommand = TRUE;
263         }
264         else if (!ascii_strncasecmp (field, "print", 5))
265         {
266           if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
267                               type, filename, line))
268             printcommand = TRUE;
269         }
270         else if (!ascii_strncasecmp (field, "edit", 4))
271         {
272           if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
273                               type, filename, line))
274             editcommand = TRUE;
275         }
276         else if (!ascii_strncasecmp (field, "nametemplate", 12))
277         {
278           get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
279                           type, filename, line);
280         }
281         else if (!ascii_strncasecmp (field, "x-convert", 9))
282         {
283           get_field_text (field + 9, entry ? &entry->convert : NULL,
284                           type, filename, line);
285         }
286         else if (!ascii_strncasecmp (field, "test", 4))
287         {
288           /* 
289            * This routine executes the given test command to determine
290            * if this is the right entry.
291            */
292           char *test_command = NULL;
293           size_t len;
294
295           if (get_field_text (field + 4, &test_command, type, filename, line)
296               && test_command)
297           {
298             len = mutt_strlen (test_command) + STRING;
299             safe_realloc (&test_command, len);
300             rfc1524_expand_command (a, a->filename, type, test_command, len);
301             if (mutt_system (test_command))
302             {
303               /* a non-zero exit code means test failed */
304               found = FALSE;
305             }
306             FREE (&test_command);
307           }
308         }
309       } /* while (ch) */
310
311       if (opt == M_AUTOVIEW)
312       {
313         if (!copiousoutput)
314           found = FALSE;
315       }
316       else if (opt == M_COMPOSE)
317       {
318         if (!composecommand)
319           found = FALSE;
320       }
321       else if (opt == M_EDIT)
322       {
323         if (!editcommand)
324           found = FALSE;
325       }
326       else if (opt == M_PRINT)
327       {
328         if (!printcommand)
329           found = FALSE;
330       }
331       
332       if (!found)
333       {
334         /* reset */
335         if (entry)
336         {
337           FREE (&entry->command);
338           FREE (&entry->composecommand);
339           FREE (&entry->composetypecommand);
340           FREE (&entry->editcommand);
341           FREE (&entry->printcommand);
342           FREE (&entry->nametemplate);
343           FREE (&entry->convert);
344           entry->needsterminal = 0;
345           entry->copiousoutput = 0;
346         }
347       }
348     } /* while (!found && (buf = mutt_read_line ())) */
349     fclose (fp);
350   } /* if ((fp = fopen ())) */
351   FREE (&buf);
352   return found;
353 }
354
355 rfc1524_entry *rfc1524_new_entry(void)
356 {
357   return (rfc1524_entry *)safe_calloc(1, sizeof(rfc1524_entry));
358 }
359
360 void rfc1524_free_entry(rfc1524_entry **entry)
361 {
362   rfc1524_entry *p = *entry;
363
364   FREE (&p->command);
365   FREE (&p->testcommand);
366   FREE (&p->composecommand);
367   FREE (&p->composetypecommand);
368   FREE (&p->editcommand);
369   FREE (&p->printcommand);
370   FREE (&p->nametemplate);
371   FREE (entry);
372 }
373
374 /*
375  * rfc1524_mailcap_lookup attempts to find the given type in the
376  * list of mailcap files.  On success, this returns the entry information
377  * in *entry, and returns 1.  On failure (not found), returns 0.
378  * If entry == NULL just return 1 if the given type is found.
379  */
380 int rfc1524_mailcap_lookup (BODY *a, char *type, rfc1524_entry *entry, int opt)
381 {
382   char path[_POSIX_PATH_MAX];
383   int x;
384   int found = FALSE;
385   char *curr = MailcapPath;
386
387   /* rfc1524 specifies that a path of mailcap files should be searched.
388    * joy.  They say 
389    * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
390    * and overriden by the MAILCAPS environment variable, and, just to be nice, 
391    * we'll make it specifiable in .muttrc
392    */
393   if (!curr || !*curr)
394   {
395     mutt_error _("No mailcap path specified");
396     return 0;
397   }
398
399   mutt_check_lookup_list (a, type, SHORT_STRING);
400
401   while (!found && *curr)
402   {
403     x = 0;
404     while (*curr && *curr != ':' && x < sizeof (path) - 1)
405     {
406       path[x++] = *curr;
407       curr++;
408     }
409     if (*curr)
410       curr++;
411
412     if (!x)
413       continue;
414     
415     path[x] = '\0';
416     mutt_expand_path (path, sizeof (path));
417
418     dprint(2,(debugfile,"Checking mailcap file: %s\n",path));
419     found = rfc1524_mailcap_parse (a, path, type, entry, opt);
420   }
421
422   if (entry && !found)
423     mutt_error (_("mailcap entry for type %s not found"), type);
424
425   return found;
426 }
427
428
429 /* This routine will create a _temporary_ filename matching the
430  * name template given if this needs to be done.
431  * 
432  * Please note that only the last path element of the
433  * template and/or the old file name will be used for the
434  * comparison and the temporary file name.
435  * 
436  * Returns 0 if oldfile is fine as is.
437  * Returns 1 if newfile specified
438  */
439
440 static void strnfcpy(char *d, char *s, size_t siz, size_t len)
441 {
442   if(len > siz)
443     len = siz - 1;
444   strfcpy(d, s, len);
445 }
446
447 int rfc1524_expand_filename (char *nametemplate,
448                              char *oldfile, 
449                              char *newfile,
450                              size_t nflen)
451 {
452   int i, j, k, ps, r;
453   char *s;
454   short lmatch = 0, rmatch = 0; 
455   char left[_POSIX_PATH_MAX];
456   char right[_POSIX_PATH_MAX];
457   
458   newfile[0] = 0;
459
460   /* first, ignore leading path components.
461    */
462   
463   if (nametemplate && (s = strrchr (nametemplate, '/')))
464     nametemplate = s + 1;
465
466   if (oldfile && (s = strrchr (oldfile, '/')))
467     oldfile = s + 1;
468     
469   if (!nametemplate)
470   {
471     if (oldfile)
472       strfcpy (newfile, oldfile, nflen);
473   }
474   else if (!oldfile)
475   {
476     mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
477   }
478   else /* oldfile && nametemplate */
479   {
480
481     /* first, compare everything left from the "%s" 
482      * (if there is one).
483      */
484     
485     lmatch = 1; ps = 0;
486     for(i = 0; nametemplate[i]; i++)
487     {
488       if(nametemplate[i] == '%' && nametemplate[i+1] == 's')
489       { 
490         ps = 1;
491         break;
492       }
493
494       /* note that the following will _not_ read beyond oldfile's end. */
495
496       if(lmatch && nametemplate[i] != oldfile[i])
497         lmatch = 0;
498     }
499
500     if(ps)
501     {
502       
503       /* If we had a "%s", check the rest. */
504       
505       /* now, for the right part: compare everything right from 
506        * the "%s" to the final part of oldfile.
507        * 
508        * The logic here is as follows:
509        * 
510        * - We start reading from the end.
511        * - There must be a match _right_ from the "%s",
512        *   thus the i + 2.  
513        * - If there was a left hand match, this stuff
514        *   must not be counted again.  That's done by the
515        *   condition (j >= (lmatch ? i : 0)).
516        */
517       
518       rmatch = 1;
519
520       for(r = 0, j = mutt_strlen(oldfile) - 1, k = mutt_strlen(nametemplate) - 1 ;
521           j >= (lmatch ? i : 0) && k >= i + 2;
522           j--, k--)
523       {
524         if(nametemplate[k] != oldfile[j])
525         {
526           rmatch = 0;
527           break;
528         }
529       }
530       
531       /* Now, check if we had a full match. */
532       
533       if(k >= i + 2)
534         rmatch = 0;
535       
536       if(lmatch) *left = 0;
537       else strnfcpy(left, nametemplate, sizeof(left), i);
538       
539       if(rmatch) *right = 0;
540       else strfcpy(right, nametemplate + i + 2, sizeof(right));
541       
542       snprintf(newfile, nflen, "%s%s%s", left, oldfile, right);
543     }
544     else
545     {
546       /* no "%s" in the name template. */
547       strfcpy(newfile, nametemplate, nflen);
548     }
549   }
550   
551   mutt_adv_mktemp(newfile, nflen);
552
553   if(rmatch && lmatch)
554     return 0;
555   else 
556     return 1;
557   
558 }
559
560 /* If rfc1524_expand_command() is used on a recv'd message, then
561  * the filename doesn't exist yet, but if its used while sending a message,
562  * then we need to rename the existing file.
563  *
564  * This function returns 0 on successful move, 1 on old file doesn't exist,
565  * 2 on new file already exists, and 3 on other failure.
566  */
567
568 /* note on access(2) use: No dangling symlink problems here due to
569  * safe_fopen().
570  */
571
572 int _mutt_rename_file (char *oldfile, char *newfile, int overwrite)
573 {
574   FILE *ofp, *nfp;
575
576   if (access (oldfile, F_OK) != 0)
577     return 1;
578   if (!overwrite && access (newfile, F_OK) == 0)
579     return 2;
580   if ((ofp = fopen (oldfile,"r")) == NULL)
581     return 3;
582   if ((nfp = safe_fopen (newfile,"w")) == NULL)
583   {
584     fclose(ofp);
585     return 3;
586   }
587   mutt_copy_stream (ofp,nfp);
588   fclose (nfp);
589   fclose (ofp);
590   mutt_unlink (oldfile);
591   return 0;
592 }
593
594 int mutt_rename_file (char *oldfile, char *newfile)
595 {
596   return _mutt_rename_file (oldfile, newfile, 0);
597 }