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