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