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