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