begin to rework mailcap parsing a "bit".
[apps/madmutt.git] / lib-mime / rfc1524.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * Copyright notice from original mutt:
21  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
22  *
23  * This file is part of mutt-ng, see http://www.muttng.org/.
24  * It's licensed under the GNU General Public License,
25  * please see the file GPL in the top level source directory.
26  */
27
28 /* 
29  * rfc1524 defines a format for the Multimedia Mail Configuration, which
30  * is the standard mailcap file format under Unix which specifies what 
31  * external programs should be used to view/compose/edit multimedia files
32  * based on content type.
33  *
34  * This file contains various functions for implementing a fair subset of 
35  * rfc1524.
36  */
37
38 #if HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <string.h>
43 #include <stdlib.h>
44 #include <ctype.h>
45
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48 #include <errno.h>
49 #include <unistd.h>
50
51 #include <lib-lib/mem.h>
52 #include <lib-lib/str.h>
53 #include <lib-lib/ascii.h>
54 #include <lib-lib/macros.h>
55 #include <lib-lib/file.h>
56
57 #include <lib-sys/unix.h>
58
59 #include "mutt.h"
60 #include "mime.h"
61 #include "attach.h"
62
63 /* The command semantics include the following:
64  * %s is the filename that contains the mail body data
65  * %t is the content type, like text/plain
66  * %{parameter} is replaced by the parameter value from the content-type field
67  * \% is %
68  * Unsupported rfc1524 parameters: these would probably require some doing
69  * by mutt, and can probably just be done by piping the message to metamail
70  * %n is the integer number of sub-parts in the multipart
71  * %F is "content-type filename" repeated for each sub-part
72  *
73  * In addition, this function returns a 0 if the command works on a file,
74  * and 1 if the command works on a pipe.
75  */
76 int rfc1524_expand_command(BODY *a, const char *filename, const char *mtype,
77                            char *command, int clen)
78 {
79     int x = 0, y = 0;
80     int needspipe = TRUE;
81     char buf[LONG_STRING];
82     char type[LONG_STRING];
83
84     m_strcpy(type, sizeof(type), mtype);
85
86     if (option(OPTMAILCAPSANITIZE))
87         mutt_sanitize_filename(type, 0);
88
89     while (command[x] && x < clen && y < ssizeof(buf)) {
90         switch (command[x]) {
91           case '\\':
92             x++;
93             buf[y++] = command[x++];
94             break;
95
96           case '%':
97             x++;
98             if (command[x] == '{') {
99                 char param[STRING];
100                 char pval[STRING];
101                 int z = 0;
102
103                 x++;
104                 while (command[x] && command[x] != '}' && z < ssizeof (param))
105                     param[z++] = command[x++];
106                 param[z] = '\0';
107
108                 m_strcpy(pval, sizeof(pval),
109                          mutt_get_parameter(param, a->parameter));
110
111                 if (option(OPTMAILCAPSANITIZE))
112                     mutt_sanitize_filename(pval, 0);
113
114                 y += mutt_quote_filename(buf + y, sizeof(buf) - y, pval);
115             }
116
117             if (command[x] == 's' && filename) {
118                 y += mutt_quote_filename(buf + y, sizeof(buf) - y, filename);
119                 needspipe = FALSE;
120             }
121
122             if (command[x] == 't') {
123                 y += mutt_quote_filename(buf + y, sizeof(buf) - y, type);
124             }
125             x++;
126             break;
127
128           default:
129             buf[y++] = command[x++];
130             break;
131         }
132     }
133
134     buf[y] = '\0';
135     m_strcpy(command, clen, buf);
136
137     return needspipe;
138 }
139
140 static char *parse_field(char *p, char **field, char **value)
141 {
142     p = vskipspaces(p);
143     *field = p;
144     *value = NULL;
145
146     for (;;) {
147         switch (*p) {
148           case ';':
149             *p++ = '\0';
150           case '\0':
151             m_strrtrim(*field);
152             m_strrtrim(*value);
153             return p;
154
155           case '\\':
156             p += 1 + (p[1] != 0);
157             break;
158
159           case '=':
160             if (!*value) {
161                 *p++ = '\0';
162                 p = *value = vskipspaces(p);
163                 continue;
164             }
165             /* falltrhrough */
166
167           default:
168             p++;
169             break;
170         }
171     }
172 }
173
174 static inline void
175 parse_field_error(const char *type, const char *filename, int line)
176 {
177     mutt_error(_("Improperly formated entry for type %s in \"%s\" line %d"),
178                type, filename, line);
179 }
180
181 /* rfc1524 mailcap file is of the format:
182  * base/type; command; extradefs
183  * type can be * for matching all
184  * base with no /type is an implicit wild
185  * command contains a %s for the filename to pass, default to pipe on stdin
186  * extradefs are of the form:
187  *  def1="definition"; def2="define \;";
188  * line wraps with a \ at the end of the line
189  * # for comments
190  */
191 static int
192 rfc1524_mailcap_parse(BODY *a, const char *filename, const char *type,
193                       rfc1524_entry *entry, int opt)
194 {
195     FILE *fp;
196     char *buf = NULL;
197     ssize_t buflen;
198     char *ch;
199     char *field, *value;
200     int found = FALSE;
201     int copiousoutput;
202     int composecommand;
203     int editcommand;
204     int printcommand;
205     int btlen;
206     int line = 0;
207
208     /* find length of basetype */
209     if ((ch = strchr (type, '/')) == NULL)
210         return FALSE;
211     btlen = ch - type;
212
213     fp = fopen(filename, "r");
214     if (!fp)
215         goto error;
216
217     while (!found && (buf = mutt_read_line(buf, &buflen, fp, &line)) != NULL) {
218         /* ignore comments */
219         if (*buf == '#')
220             continue;
221
222         /* check type */
223         ch = parse_field(buf, &field, &value);
224         if (ascii_strcasecmp(field, type)
225             && (ascii_strncasecmp(field, type, btlen)
226                 || (buf[btlen] != 0 && m_strcmp(buf + btlen, "/*"))))
227             continue;
228
229         /* next field is the viewcommand */
230         ch = parse_field(ch, &field, &value);
231         if (entry)
232             entry->command = m_strdup(field);
233
234         /* parse the optional fields */
235         found = TRUE;
236         copiousoutput = FALSE;
237         composecommand = FALSE;
238         editcommand = FALSE;
239         printcommand = FALSE;
240
241         while (*ch) {
242             ch = parse_field(ch, &field, &value);
243
244             switch (mime_which_token(field, -1)) {
245 #define DO_CASE(token, field, expr)                                             \
246               case token:                                                       \
247                 if (value) {                                                    \
248                     if (entry)                                                  \
249                         m_strreplace(&entry->field, value);                     \
250                     expr;                                                       \
251                 } else {                                                        \
252                     parse_field_error(type, filename, line);                    \
253                 }                                                               \
254                 break;
255
256               case MIME_NEEDSTERMINAL:
257                 if (entry)
258                     entry->needsterminal = TRUE;
259                 break;
260
261               case MIME_COPIOUSOUTPUT:
262                 copiousoutput = TRUE;
263                 if (entry)
264                     entry->copiousoutput = TRUE;
265                 break;
266
267                 DO_CASE(MIME_COMPOSETYPED, composecommand, composecommand = TRUE);
268                 DO_CASE(MIME_COMPOSE, composecommand, composecommand = TRUE);
269                 DO_CASE(MIME_PRINT, printcommand, printcommand = TRUE);
270                 DO_CASE(MIME_EDIT, editcommand, editcommand = TRUE);
271                 DO_CASE(MIME_NAMETEMPLATE, nametemplate, );
272                 DO_CASE(MIME_X_CONVERT, convert, );
273
274               case MIME_TEST:
275                 /*
276                  * This routine executes the given test command to determine
277                  * if this is the right entry.  a non-zero exit code means
278                  * test failed
279                  */
280                 if (value) {
281                     ssize_t len   = m_strlen(value) + STRING;
282                     char *testcmd = p_new(char, len);
283
284                     strcpy(testcmd, value);
285                     rfc1524_expand_command(a, a->filename, type, testcmd, len);
286                     found = !mutt_system(testcmd);
287                     p_delete(&testcmd);
288                 } else {
289                     parse_field_error(type, filename, line);
290                 }
291                 break;
292
293               default:
294                 break;
295 #undef DO_CASE
296             }
297         }
298
299         if (opt == M_AUTOVIEW) {
300             if (!copiousoutput)
301                 found = FALSE;
302         }
303         else if (opt == M_COMPOSE) {
304             if (!composecommand)
305                 found = FALSE;
306         }
307         else if (opt == M_EDIT) {
308             if (!editcommand)
309                 found = FALSE;
310         }
311         else if (opt == M_PRINT) {
312             if (!printcommand)
313                 found = FALSE;
314         }
315
316         if (!found && entry) {
317             rfc1524_entry_wipe(entry);
318             rfc1524_entry_init(entry);
319         }
320     }                           /* while (!found && (buf = mutt_read_line ())) */
321     fclose (fp);
322
323   error:
324     p_delete(&buf);
325     return found;
326 }
327
328
329 /************** READ MARK **********************/
330
331 /*
332  * rfc1524_mailcap_lookup attempts to find the given type in the
333  * list of mailcap files.  On success, this returns the entry information
334  * in *entry, and returns 1.  On failure (not found), returns 0.
335  * If entry == NULL just return 1 if the given type is found.
336  */
337 int rfc1524_mailcap_lookup (BODY * a, char *type, rfc1524_entry * entry,
338                             int opt)
339 {
340   char path[_POSIX_PATH_MAX];
341   int x;
342   int found = FALSE;
343   char *curr = MailcapPath;
344
345   /* rfc1524 specifies that a path of mailcap files should be searched.
346    * joy.  They say 
347    * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
348    * and overriden by the MAILCAPS environment variable, and, just to be nice, 
349    * we'll make it specifiable in .muttrc
350    */
351   if (!curr || !*curr) {
352     mutt_error _("No mailcap path specified");
353
354     return 0;
355   }
356
357   mutt_check_lookup_list (a, type, SHORT_STRING);
358
359   while (!found && *curr) {
360     x = 0;
361     while (*curr && *curr != ':' && x < ssizeof (path) - 1) {
362       path[x++] = *curr;
363       curr++;
364     }
365     if (*curr)
366       curr++;
367
368     if (!x)
369       continue;
370
371     path[x] = '\0';
372     mutt_expand_path (path, sizeof (path));
373
374     found = rfc1524_mailcap_parse (a, path, type, entry, opt);
375   }
376
377   if (entry && !found)
378     mutt_error (_("mailcap entry for type %s not found"), type);
379
380   return found;
381 }
382
383
384 /* This routine will create a _temporary_ filename matching the
385  * name template given if this needs to be done.
386  * 
387  * Please note that only the last path element of the
388  * template and/or the old file name will be used for the
389  * comparison and the temporary file name.
390  * 
391  * Returns 0 if oldfile is fine as is.
392  * Returns 1 if newfile specified
393  */
394
395 int rfc1524_expand_filename (char *nametemplate,
396                              char *oldfile, char *newfile, ssize_t nflen)
397 {
398   int i, j, k, ps, r;
399   char *s;
400   short lmatch = 0, rmatch = 0;
401   char left[_POSIX_PATH_MAX];
402   char right[_POSIX_PATH_MAX];
403
404   newfile[0] = 0;
405
406   /* first, ignore leading path components.
407    */
408
409   if (nametemplate && (s = strrchr (nametemplate, '/')))
410     nametemplate = s + 1;
411
412   if (oldfile && (s = strrchr (oldfile, '/')))
413     oldfile = s + 1;
414
415   if (!nametemplate) {
416     if (oldfile)
417       m_strcpy(newfile, nflen, oldfile);
418   }
419   else if (!oldfile) {
420     mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
421   }
422   else {                        /* oldfile && nametemplate */
423
424
425     /* first, compare everything left from the "%s" 
426      * (if there is one).
427      */
428
429     lmatch = 1;
430     ps = 0;
431     for (i = 0; nametemplate[i]; i++) {
432       if (nametemplate[i] == '%' && nametemplate[i + 1] == 's') {
433         ps = 1;
434         break;
435       }
436
437       /* note that the following will _not_ read beyond oldfile's end. */
438
439       if (lmatch && nametemplate[i] != oldfile[i])
440         lmatch = 0;
441     }
442
443     if (ps) {
444
445       /* If we had a "%s", check the rest. */
446
447       /* now, for the right part: compare everything right from 
448        * the "%s" to the final part of oldfile.
449        * 
450        * The logic here is as follows:
451        * 
452        * - We start reading from the end.
453        * - There must be a match _right_ from the "%s",
454        *   thus the i + 2.  
455        * - If there was a left hand match, this stuff
456        *   must not be counted again.  That's done by the
457        *   condition (j >= (lmatch ? i : 0)).
458        */
459
460       rmatch = 1;
461
462       for (r = 0, j = m_strlen(oldfile) - 1, k =
463            m_strlen(nametemplate) - 1;
464            j >= (lmatch ? i : 0) && k >= i + 2; j--, k--) {
465         if (nametemplate[k] != oldfile[j]) {
466           rmatch = 0;
467           break;
468         }
469       }
470
471       /* Now, check if we had a full match. */
472
473       if (k >= i + 2)
474         rmatch = 0;
475
476       if (lmatch)
477         *left = 0;
478       else
479         m_strncpy(left, sizeof(left), nametemplate, i);
480
481       if (rmatch)
482         *right = 0;
483       else
484         m_strcpy(right, sizeof(right), nametemplate + i + 2);
485
486       snprintf (newfile, nflen, "%s%s%s", left, oldfile, right);
487     }
488     else {
489       /* no "%s" in the name template. */
490       m_strcpy(newfile, nflen, nametemplate);
491     }
492   }
493
494   mutt_adv_mktemp (NULL, newfile, nflen);
495
496   return !(rmatch && lmatch);
497 }
498