2 * Copyright (C) 1996-2000,2003 Michael R. Elkins <me@mutt.org>
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.
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.
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.
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.
25 * This file contains various functions for implementing a fair subset of
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
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
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.
58 int rfc1524_expand_command (BODY *a, char *filename, char *_type,
59 char *command, int clen)
63 char buf[LONG_STRING];
64 char type[LONG_STRING];
66 strfcpy (type, _type, sizeof (type));
68 if (option (OPTMAILCAPSANITIZE))
69 mutt_sanitize_filename (type, 0);
71 while (x < clen && command[x] && y < sizeof (buf) - 1)
73 if (command[x] == '\\')
76 buf[y++] = command[x++];
78 else if (command[x] == '%')
81 if (command[x] == '{')
89 while (command[x] && command[x] != '}' && z < sizeof (param) - 1)
90 param[z++] = command[x++];
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);
98 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
100 else if (command[x] == 's' && filename != NULL)
102 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
105 else if (command[x] == 't')
107 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
112 buf[y++] = command[x++];
115 strfcpy (command, buf, clen);
120 /* NUL terminates a rfc 1524 field,
121 * returns start of next field or NULL */
122 static char *get_field (char *s)
129 while ((ch = strpbrk (s, ";\\")) != NULL)
144 mutt_remove_trailing_ws (s);
148 static int get_field_text (char *field, char **entry,
149 char *type, char *filename, int line)
151 field = mutt_skip_whitespace (field);
157 field = mutt_skip_whitespace (field);
158 mutt_str_replace (entry, field);
164 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
165 type, filename, line);
170 static int rfc1524_mailcap_parse (BODY *a,
173 rfc1524_entry *entry,
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
200 /* find length of basetype */
201 if ((ch = strchr (type, '/')) == NULL)
205 if ((fp = fopen (filename, "r")) != NULL)
207 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line, M_CONT)) != NULL)
209 /* ignore comments */
212 dprint (2, (debugfile, "mailcap entry: %s\n", buf));
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 */
222 /* next field is the viewcommand */
226 entry->command = safe_strdup (field);
228 /* parse the optional fields */
230 copiousoutput = FALSE;
231 composecommand = FALSE;
233 printcommand = FALSE;
239 dprint (2, (debugfile, "field: %s\n", field));
241 if (!ascii_strcasecmp (field, "needsterminal"))
244 entry->needsterminal = TRUE;
246 else if (!ascii_strcasecmp (field, "copiousoutput"))
248 copiousoutput = TRUE;
250 entry->copiousoutput = TRUE;
252 else if (!ascii_strncasecmp (field, "composetyped", 12))
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;
259 else if (!ascii_strncasecmp (field, "compose", 7))
261 if (get_field_text (field + 7, entry ? &entry->composecommand : NULL,
262 type, filename, line))
263 composecommand = TRUE;
265 else if (!ascii_strncasecmp (field, "print", 5))
267 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
268 type, filename, line))
271 else if (!ascii_strncasecmp (field, "edit", 4))
273 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
274 type, filename, line))
277 else if (!ascii_strncasecmp (field, "nametemplate", 12))
279 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
280 type, filename, line);
282 else if (!ascii_strncasecmp (field, "x-convert", 9))
284 get_field_text (field + 9, entry ? &entry->convert : NULL,
285 type, filename, line);
287 else if (!ascii_strncasecmp (field, "test", 4))
290 * This routine executes the given test command to determine
291 * if this is the right entry.
293 char *test_command = NULL;
296 if (get_field_text (field + 4, &test_command, type, filename, line)
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))
304 /* a non-zero exit code means test failed */
307 FREE (&test_command);
312 if (opt == M_AUTOVIEW)
317 else if (opt == M_COMPOSE)
322 else if (opt == M_EDIT)
327 else if (opt == M_PRINT)
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;
349 } /* while (!found && (buf = mutt_read_line ())) */
351 } /* if ((fp = fopen ())) */
356 rfc1524_entry *rfc1524_new_entry(void)
358 return (rfc1524_entry *)safe_calloc(1, sizeof(rfc1524_entry));
361 void rfc1524_free_entry(rfc1524_entry **entry)
363 rfc1524_entry *p = *entry;
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__ */
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.
381 int rfc1524_mailcap_lookup (BODY *a, char *type, rfc1524_entry *entry, int opt)
383 char path[_POSIX_PATH_MAX];
386 char *curr = MailcapPath;
388 /* rfc1524 specifies that a path of mailcap files should be searched.
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
396 mutt_error _("No mailcap path specified");
400 mutt_check_lookup_list (a, type, SHORT_STRING);
402 while (!found && *curr)
405 while (*curr && *curr != ':' && x < sizeof (path) - 1)
417 mutt_expand_path (path, sizeof (path));
419 dprint(2,(debugfile,"Checking mailcap file: %s\n",path));
420 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
424 mutt_error (_("mailcap entry for type %s not found"), type);
430 /* This routine will create a _temporary_ filename matching the
431 * name template given if this needs to be done.
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.
437 * Returns 0 if oldfile is fine as is.
438 * Returns 1 if newfile specified
441 static void strnfcpy(char *d, char *s, size_t siz, size_t len)
448 int rfc1524_expand_filename (char *nametemplate,
455 short lmatch = 0, rmatch = 0;
456 char left[_POSIX_PATH_MAX];
457 char right[_POSIX_PATH_MAX];
461 /* first, ignore leading path components.
464 if (nametemplate && (s = strrchr (nametemplate, '/')))
465 nametemplate = s + 1;
467 if (oldfile && (s = strrchr (oldfile, '/')))
473 strfcpy (newfile, oldfile, nflen);
477 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
479 else /* oldfile && nametemplate */
482 /* first, compare everything left from the "%s"
487 for(i = 0; nametemplate[i]; i++)
489 if(nametemplate[i] == '%' && nametemplate[i+1] == 's')
495 /* note that the following will _not_ read beyond oldfile's end. */
497 if(lmatch && nametemplate[i] != oldfile[i])
504 /* If we had a "%s", check the rest. */
506 /* now, for the right part: compare everything right from
507 * the "%s" to the final part of oldfile.
509 * The logic here is as follows:
511 * - We start reading from the end.
512 * - There must be a match _right_ from the "%s",
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)).
521 for(r = 0, j = mutt_strlen(oldfile) - 1, k = mutt_strlen(nametemplate) - 1 ;
522 j >= (lmatch ? i : 0) && k >= i + 2;
525 if(nametemplate[k] != oldfile[j])
532 /* Now, check if we had a full match. */
537 if(lmatch) *left = 0;
538 else strnfcpy(left, nametemplate, sizeof(left), i);
540 if(rmatch) *right = 0;
541 else strfcpy(right, nametemplate + i + 2, sizeof(right));
543 snprintf(newfile, nflen, "%s%s%s", left, oldfile, right);
547 /* no "%s" in the name template. */
548 strfcpy(newfile, nametemplate, nflen);
552 mutt_adv_mktemp(newfile, nflen);
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.
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.
569 /* note on access(2) use: No dangling symlink problems here due to
573 int mutt_rename_file (char *oldfile, char *newfile)
577 if (access (oldfile, F_OK) != 0)
579 if (access (newfile, F_OK) == 0)
581 if ((ofp = fopen (oldfile,"r")) == NULL)
583 if ((nfp = safe_fopen (newfile,"w")) == NULL)
588 mutt_copy_stream (ofp,nfp);
591 mutt_unlink (oldfile);