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