1 /*
2 * Copyright (C) 1996-2000,2003 Michael R. Elkins <me@mutt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 /*
20 * rfc1524 defines a format for the Multimedia Mail Configuration, which
21 * is the standard mailcap file format under Unix which specifies what
22 * external programs should be used to view/compose/edit multimedia files
23 * based on content type.
24 *
25 * This file contains various functions for implementing a fair subset of
26 * rfc1524.
27 */
28
29 #if HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include "mutt.h"
34 #include "rfc1524.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 */
rfc1524_expand_command(BODY * a,char * filename,char * _type,char * command,int clen)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 strfcpy (type, _type, sizeof (type));
67
68 if (option (OPTMAILCAPSANITIZE))
69 mutt_sanitize_filename (type, 0);
70
71 while (x < clen && command[x] && y < sizeof (buf) - 1)
72 {
73 if (command[x] == '\\')
74 {
75 x++;
76 buf[y++] = command[x++];
77 }
78 else if (command[x] == '%')
79 {
80 x++;
81 if (command[x] == '{')
82 {
83 char param[STRING];
84 char pvalue[STRING];
85 char *_pvalue;
86 int z = 0;
87
88 x++;
89 while (command[x] && command[x] != '}' && z < sizeof (param) - 1)
90 param[z++] = command[x++];
91 param[z] = '\0';
92
93 _pvalue = mutt_get_parameter (param, a->parameter);
94 strfcpy (pvalue, NONULL(_pvalue), sizeof (pvalue));
95 if (option (OPTMAILCAPSANITIZE))
96 mutt_sanitize_filename (pvalue, 0);
97
98 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
99 }
100 else if (command[x] == 's' && filename != NULL)
101 {
102 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
103 needspipe = FALSE;
104 }
105 else if (command[x] == 't')
106 {
107 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
108 }
109 x++;
110 }
111 else
112 buf[y++] = command[x++];
113 }
114 buf[y] = '\0';
115 strfcpy (command, buf, clen);
116
117 return needspipe;
118 }
119
120 /* NUL terminates a rfc 1524 field,
121 * returns start of next field or NULL */
get_field(char * s)122 static char *get_field (char *s)
123 {
124 char *ch;
125
126 if (!s)
127 return NULL;
128
129 while ((ch = strpbrk (s, ";\\")) != NULL)
130 {
131 if (*ch == '\\')
132 {
133 s = ch + 1;
134 if (*s)
135 s++;
136 }
137 else
138 {
139 *ch++ = 0;
140 SKIPWS (ch);
141 break;
142 }
143 }
144 mutt_remove_trailing_ws (s);
145 return ch;
146 }
147
get_field_text(char * field,char ** entry,char * type,char * filename,int line)148 static int get_field_text (char *field, char **entry,
149 char *type, char *filename, int line)
150 {
151 field = mutt_skip_whitespace (field);
152 if (*field == '=')
153 {
154 if (entry)
155 {
156 field++;
157 field = mutt_skip_whitespace (field);
158 mutt_str_replace (entry, field);
159 }
160 return 1;
161 }
162 else
163 {
164 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
165 type, filename, line);
166 return 0;
167 }
168 }
169
rfc1524_mailcap_parse(BODY * a,char * filename,char * type,rfc1524_entry * entry,int opt)170 static int rfc1524_mailcap_parse (BODY *a,
171 char *filename,
172 char *type,
173 rfc1524_entry *entry,
174 int opt)
175 {
176 FILE *fp;
177 char *buf = NULL;
178 size_t buflen;
179 char *ch;
180 char *field;
181 int found = FALSE;
182 int copiousoutput;
183 int composecommand;
184 int editcommand;
185 int printcommand;
186 int btlen;
187 int line = 0;
188
189 /* rfc1524 mailcap file is of the format:
190 * base/type; command; extradefs
191 * type can be * for matching all
192 * base with no /type is an implicit wild
193 * command contains a %s for the filename to pass, default to pipe on stdin
194 * extradefs are of the form:
195 * def1="definition"; def2="define \;";
196 * line wraps with a \ at the end of the line
197 * # for comments
198 */
199
200 /* find length of basetype */
201 if ((ch = strchr (type, '/')) == NULL)
202 return FALSE;
203 btlen = ch - type;
204
205 if ((fp = fopen (filename, "r")) != NULL)
206 {
207 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line, M_CONT)) != NULL)
208 {
209 /* ignore comments */
210 if (*buf == '#')
211 continue;
212 dprint (2, (debugfile, "mailcap entry: %s\n", buf));
213
214 /* check type */
215 ch = get_field (buf);
216 if (ascii_strcasecmp (buf, type) &&
217 (ascii_strncasecmp (buf, type, btlen) ||
218 (buf[btlen] != 0 && /* implicit wild */
219 mutt_strcmp (buf + btlen, "/*")))) /* wildsubtype */
220 continue;
221
222 /* next field is the viewcommand */
223 field = ch;
224 ch = get_field (ch);
225 if (entry)
226 entry->command = safe_strdup (field);
227
228 /* parse the optional fields */
229 found = TRUE;
230 copiousoutput = FALSE;
231 composecommand = FALSE;
232 editcommand = FALSE;
233 printcommand = FALSE;
234
235 while (ch)
236 {
237 field = ch;
238 ch = get_field (ch);
239 dprint (2, (debugfile, "field: %s\n", field));
240
241 if (!ascii_strcasecmp (field, "needsterminal"))
242 {
243 if (entry)
244 entry->needsterminal = TRUE;
245 }
246 else if (!ascii_strcasecmp (field, "copiousoutput"))
247 {
248 copiousoutput = TRUE;
249 if (entry)
250 entry->copiousoutput = TRUE;
251 }
252 else if (!ascii_strncasecmp (field, "composetyped", 12))
253 {
254 /* this compare most occur before compose to match correctly */
255 if (get_field_text (field + 12, entry ? &entry->composetypecommand : NULL,
256 type, filename, line))
257 composecommand = TRUE;
258 }
259 else if (!ascii_strncasecmp (field, "compose", 7))
260 {
261 if (get_field_text (field + 7, entry ? &entry->composecommand : NULL,
262 type, filename, line))
263 composecommand = TRUE;
264 }
265 else if (!ascii_strncasecmp (field, "print", 5))
266 {
267 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
268 type, filename, line))
269 printcommand = TRUE;
270 }
271 else if (!ascii_strncasecmp (field, "edit", 4))
272 {
273 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
274 type, filename, line))
275 editcommand = TRUE;
276 }
277 else if (!ascii_strncasecmp (field, "nametemplate", 12))
278 {
279 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
280 type, filename, line);
281 }
282 else if (!ascii_strncasecmp (field, "x-convert", 9))
283 {
284 get_field_text (field + 9, entry ? &entry->convert : NULL,
285 type, filename, line);
286 }
287 else if (!ascii_strncasecmp (field, "test", 4))
288 {
289 /*
290 * This routine executes the given test command to determine
291 * if this is the right entry.
292 */
293 char *test_command = NULL;
294 size_t len;
295
296 if (get_field_text (field + 4, &test_command, type, filename, line)
297 && test_command)
298 {
299 len = mutt_strlen (test_command) + STRING;
300 safe_realloc (&test_command, len);
301 rfc1524_expand_command (a, a->filename, type, test_command, len);
302 if (mutt_system (test_command))
303 {
304 /* a non-zero exit code means test failed */
305 found = FALSE;
306 }
307 FREE (&test_command);
308 }
309 }
310 } /* while (ch) */
311
312 if (opt == M_AUTOVIEW)
313 {
314 if (!copiousoutput)
315 found = FALSE;
316 }
317 else if (opt == M_COMPOSE)
318 {
319 if (!composecommand)
320 found = FALSE;
321 }
322 else if (opt == M_EDIT)
323 {
324 if (!editcommand)
325 found = FALSE;
326 }
327 else if (opt == M_PRINT)
328 {
329 if (!printcommand)
330 found = FALSE;
331 }
332
333 if (!found)
334 {
335 /* reset */
336 if (entry)
337 {
338 FREE (&entry->command);
339 FREE (&entry->composecommand);
340 FREE (&entry->composetypecommand);
341 FREE (&entry->editcommand);
342 FREE (&entry->printcommand);
343 FREE (&entry->nametemplate);
344 FREE (&entry->convert);
345 entry->needsterminal = 0;
346 entry->copiousoutput = 0;
347 }
348 }
349 } /* while (!found && (buf = mutt_read_line ())) */
350 safe_fclose (&fp);
351 } /* if ((fp = fopen ())) */
352 FREE (&buf);
353 return found;
354 }
355
rfc1524_new_entry(void)356 rfc1524_entry *rfc1524_new_entry(void)
357 {
358 return (rfc1524_entry *)safe_calloc(1, sizeof(rfc1524_entry));
359 }
360
rfc1524_free_entry(rfc1524_entry ** entry)361 void rfc1524_free_entry(rfc1524_entry **entry)
362 {
363 rfc1524_entry *p = *entry;
364
365 FREE (&p->command);
366 FREE (&p->testcommand);
367 FREE (&p->composecommand);
368 FREE (&p->composetypecommand);
369 FREE (&p->editcommand);
370 FREE (&p->printcommand);
371 FREE (&p->nametemplate);
372 FREE (entry); /* __FREE_CHECKED__ */
373 }
374
375 /*
376 * rfc1524_mailcap_lookup attempts to find the given type in the
377 * list of mailcap files. On success, this returns the entry information
378 * in *entry, and returns 1. On failure (not found), returns 0.
379 * If entry == NULL just return 1 if the given type is found.
380 */
rfc1524_mailcap_lookup(BODY * a,char * type,rfc1524_entry * entry,int opt)381 int rfc1524_mailcap_lookup (BODY *a, char *type, rfc1524_entry *entry, int opt)
382 {
383 char path[_POSIX_PATH_MAX];
384 int x;
385 int found = FALSE;
386 char *curr = MailcapPath;
387
388 /* rfc1524 specifies that a path of mailcap files should be searched.
389 * joy. They say
390 * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
391 * and overridden by the MAILCAPS environment variable, and, just to be nice,
392 * we'll make it specifiable in .muttrc
393 */
394 if (!curr || !*curr)
395 {
396 mutt_error _("No mailcap path specified");
397 return 0;
398 }
399
400 mutt_check_lookup_list (a, type, SHORT_STRING);
401
402 while (!found && *curr)
403 {
404 x = 0;
405 while (*curr && *curr != ':' && x < sizeof (path) - 1)
406 {
407 path[x++] = *curr;
408 curr++;
409 }
410 if (*curr)
411 curr++;
412
413 if (!x)
414 continue;
415
416 path[x] = '\0';
417 mutt_expand_path (path, sizeof (path));
418
419 dprint(2,(debugfile,"Checking mailcap file: %s\n",path));
420 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
421 }
422
423 if (entry && !found)
424 mutt_error (_("mailcap entry for type %s not found"), type);
425
426 return found;
427 }
428
429
430 /* This routine will create a _temporary_ filename matching the
431 * name template given if this needs to be done.
432 *
433 * Please note that only the last path element of the
434 * template and/or the old file name will be used for the
435 * comparison and the temporary file name.
436 *
437 * Returns 0 if oldfile is fine as is.
438 * Returns 1 if newfile specified
439 */
440
strnfcpy(char * d,char * s,size_t siz,size_t len)441 static void strnfcpy(char *d, char *s, size_t siz, size_t len)
442 {
443 if(len > siz)
444 len = siz - 1;
445 strfcpy(d, s, len);
446 }
447
rfc1524_expand_filename(char * nametemplate,char * oldfile,char * newfile,size_t nflen)448 int rfc1524_expand_filename (char *nametemplate,
449 char *oldfile,
450 char *newfile,
451 size_t nflen)
452 {
453 int i, j, k, ps, r;
454 char *s;
455 short lmatch = 0, rmatch = 0;
456 char left[_POSIX_PATH_MAX];
457 char right[_POSIX_PATH_MAX];
458
459 newfile[0] = 0;
460
461 /* first, ignore leading path components.
462 */
463
464 if (nametemplate && (s = strrchr (nametemplate, '/')))
465 nametemplate = s + 1;
466
467 if (oldfile && (s = strrchr (oldfile, '/')))
468 oldfile = s + 1;
469
470 if (!nametemplate)
471 {
472 if (oldfile)
473 strfcpy (newfile, oldfile, nflen);
474 }
475 else if (!oldfile)
476 {
477 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
478 }
479 else /* oldfile && nametemplate */
480 {
481
482 /* first, compare everything left from the "%s"
483 * (if there is one).
484 */
485
486 lmatch = 1; ps = 0;
487 for(i = 0; nametemplate[i]; i++)
488 {
489 if(nametemplate[i] == '%' && nametemplate[i+1] == 's')
490 {
491 ps = 1;
492 break;
493 }
494
495 /* note that the following will _not_ read beyond oldfile's end. */
496
497 if(lmatch && nametemplate[i] != oldfile[i])
498 lmatch = 0;
499 }
500
501 if(ps)
502 {
503
504 /* If we had a "%s", check the rest. */
505
506 /* now, for the right part: compare everything right from
507 * the "%s" to the final part of oldfile.
508 *
509 * The logic here is as follows:
510 *
511 * - We start reading from the end.
512 * - There must be a match _right_ from the "%s",
513 * thus the i + 2.
514 * - If there was a left hand match, this stuff
515 * must not be counted again. That's done by the
516 * condition (j >= (lmatch ? i : 0)).
517 */
518
519 rmatch = 1;
520
521 for(r = 0, j = mutt_strlen(oldfile) - 1, k = mutt_strlen(nametemplate) - 1 ;
522 j >= (lmatch ? i : 0) && k >= i + 2;
523 j--, k--)
524 {
525 if(nametemplate[k] != oldfile[j])
526 {
527 rmatch = 0;
528 break;
529 }
530 }
531
532 /* Now, check if we had a full match. */
533
534 if(k >= i + 2)
535 rmatch = 0;
536
537 if(lmatch) *left = 0;
538 else strnfcpy(left, nametemplate, sizeof(left), i);
539
540 if(rmatch) *right = 0;
541 else strfcpy(right, nametemplate + i + 2, sizeof(right));
542
543 snprintf(newfile, nflen, "%s%s%s", left, oldfile, right);
544 }
545 else
546 {
547 /* no "%s" in the name template. */
548 strfcpy(newfile, nametemplate, nflen);
549 }
550 }
551
552 mutt_adv_mktemp(newfile, nflen);
553
554 if(rmatch && lmatch)
555 return 0;
556 else
557 return 1;
558
559 }
560
561 /* If rfc1524_expand_command() is used on a recv'd message, then
562 * the filename doesn't exist yet, but if its used while sending a message,
563 * then we need to rename the existing file.
564 *
565 * This function returns 0 on successful move, 1 on old file doesn't exist,
566 * 2 on new file already exists, and 3 on other failure.
567 */
568
569 /* note on access(2) use: No dangling symlink problems here due to
570 * safe_fopen().
571 */
572
mutt_rename_file(char * oldfile,char * newfile)573 int mutt_rename_file (char *oldfile, char *newfile)
574 {
575 FILE *ofp, *nfp;
576
577 if (access (oldfile, F_OK) != 0)
578 return 1;
579 if (access (newfile, F_OK) == 0)
580 return 2;
581 if ((ofp = fopen (oldfile,"r")) == NULL)
582 return 3;
583 if ((nfp = safe_fopen (newfile,"w")) == NULL)
584 {
585 safe_fclose (&ofp);
586 return 3;
587 }
588 mutt_copy_stream (ofp,nfp);
589 safe_fclose (&nfp);
590 safe_fclose (&ofp);
591 mutt_unlink (oldfile);
592 return 0;
593 }
594