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