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