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