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