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 (command[x] && x<clen && y<sizeof(buf))
73 if (command[x] == '\\') {
75 buf[y++] = command[x++];
77 else if (command[x] == '%')
80 if (command[x] == '{')
88 while (command[x] && command[x] != '}' && z<sizeof(param))
89 param[z++] = command[x++];
92 _pvalue = mutt_get_parameter (param, a->parameter);
93 strfcpy (pvalue, NONULL(_pvalue), sizeof (pvalue));
94 if (option (OPTMAILCAPSANITIZE))
95 mutt_sanitize_filename (pvalue, 0);
97 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
99 else if (command[x] == 's' && filename != NULL)
101 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
104 else if (command[x] == 't')
106 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
111 buf[y++] = command[x++];
114 strfcpy (command, buf, clen);
119 /* NUL terminates a rfc 1524 field,
120 * returns start of next field or NULL */
121 static char *get_field (char *s)
128 while ((ch = strpbrk (s, ";\\")) != NULL)
143 mutt_remove_trailing_ws (s);
147 static int get_field_text (char *field, char **entry,
148 char *type, char *filename, int line)
150 field = mutt_skip_whitespace (field);
156 field = mutt_skip_whitespace (field);
157 mutt_str_replace (entry, field);
163 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
164 type, filename, line);
169 static int rfc1524_mailcap_parse (BODY *a,
172 rfc1524_entry *entry,
188 /* rfc1524 mailcap file is of the format:
189 * base/type; command; extradefs
190 * type can be * for matching all
191 * base with no /type is an implicit wild
192 * command contains a %s for the filename to pass, default to pipe on stdin
193 * extradefs are of the form:
194 * def1="definition"; def2="define \;";
195 * line wraps with a \ at the end of the line
199 /* find length of basetype */
200 if ((ch = strchr (type, '/')) == NULL)
204 if ((fp = fopen (filename, "r")) != NULL)
206 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line)) != NULL)
208 /* ignore comments */
211 dprint (2, (debugfile, "mailcap entry: %s\n", buf));
214 ch = get_field (buf);
215 if (ascii_strcasecmp (buf, type) &&
216 (ascii_strncasecmp (buf, type, btlen) ||
217 (buf[btlen] != 0 && /* implicit wild */
218 mutt_strcmp (buf + btlen, "/*")))) /* wildsubtype */
221 /* next field is the viewcommand */
225 entry->command = safe_strdup (field);
227 /* parse the optional fields */
229 copiousoutput = FALSE;
230 composecommand = FALSE;
232 printcommand = FALSE;
238 dprint (2, (debugfile, "field: %s\n", field));
240 if (!ascii_strcasecmp (field, "needsterminal"))
243 entry->needsterminal = TRUE;
245 else if (!ascii_strcasecmp (field, "copiousoutput"))
247 copiousoutput = TRUE;
249 entry->copiousoutput = TRUE;
251 else if (!ascii_strncasecmp (field, "composetyped", 12))
253 /* this compare most occur before compose to match correctly */
254 if (get_field_text (field + 12, entry ? &entry->composetypecommand : NULL,
255 type, filename, line))
256 composecommand = TRUE;
258 else if (!ascii_strncasecmp (field, "compose", 7))
260 if (get_field_text (field + 7, entry ? &entry->composecommand : NULL,
261 type, filename, line))
262 composecommand = TRUE;
264 else if (!ascii_strncasecmp (field, "print", 5))
266 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
267 type, filename, line))
270 else if (!ascii_strncasecmp (field, "edit", 4))
272 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
273 type, filename, line))
276 else if (!ascii_strncasecmp (field, "nametemplate", 12))
278 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
279 type, filename, line);
281 else if (!ascii_strncasecmp (field, "x-convert", 9))
283 get_field_text (field + 9, entry ? &entry->convert : NULL,
284 type, filename, line);
286 else if (!ascii_strncasecmp (field, "test", 4))
289 * This routine executes the given test command to determine
290 * if this is the right entry.
292 char *test_command = NULL;
295 if (get_field_text (field + 4, &test_command, type, filename, line)
298 len = mutt_strlen (test_command) + STRING;
299 safe_realloc (&test_command, len);
300 rfc1524_expand_command (a, a->filename, type, test_command, len);
301 if (mutt_system (test_command))
303 /* a non-zero exit code means test failed */
306 FREE (&test_command);
311 if (opt == M_AUTOVIEW)
316 else if (opt == M_COMPOSE)
321 else if (opt == M_EDIT)
326 else if (opt == M_PRINT)
337 FREE (&entry->command);
338 FREE (&entry->composecommand);
339 FREE (&entry->composetypecommand);
340 FREE (&entry->editcommand);
341 FREE (&entry->printcommand);
342 FREE (&entry->nametemplate);
343 FREE (&entry->convert);
344 entry->needsterminal = 0;
345 entry->copiousoutput = 0;
348 } /* while (!found && (buf = mutt_read_line ())) */
350 } /* if ((fp = fopen ())) */
355 rfc1524_entry *rfc1524_new_entry(void)
357 return (rfc1524_entry *)safe_calloc(1, sizeof(rfc1524_entry));
360 void rfc1524_free_entry(rfc1524_entry **entry)
362 rfc1524_entry *p = *entry;
365 FREE (&p->testcommand);
366 FREE (&p->composecommand);
367 FREE (&p->composetypecommand);
368 FREE (&p->editcommand);
369 FREE (&p->printcommand);
370 FREE (&p->nametemplate);
371 FREE (entry); /* __FREE_CHECKED__ */
375 * rfc1524_mailcap_lookup attempts to find the given type in the
376 * list of mailcap files. On success, this returns the entry information
377 * in *entry, and returns 1. On failure (not found), returns 0.
378 * If entry == NULL just return 1 if the given type is found.
380 int rfc1524_mailcap_lookup (BODY *a, char *type, rfc1524_entry *entry, int opt)
382 char path[_POSIX_PATH_MAX];
385 char *curr = MailcapPath;
387 /* rfc1524 specifies that a path of mailcap files should be searched.
389 * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
390 * and overriden by the MAILCAPS environment variable, and, just to be nice,
391 * we'll make it specifiable in .muttrc
395 mutt_error _("No mailcap path specified");
399 mutt_check_lookup_list (a, type, SHORT_STRING);
401 while (!found && *curr)
404 while (*curr && *curr != ':' && x < sizeof (path) - 1)
416 mutt_expand_path (path, sizeof (path));
418 dprint(2,(debugfile,"Checking mailcap file: %s\n",path));
419 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
423 mutt_error (_("mailcap entry for type %s not found"), type);
429 /* This routine will create a _temporary_ filename matching the
430 * name template given if this needs to be done.
432 * Please note that only the last path element of the
433 * template and/or the old file name will be used for the
434 * comparison and the temporary file name.
436 * Returns 0 if oldfile is fine as is.
437 * Returns 1 if newfile specified
440 static void strnfcpy(char *d, char *s, size_t siz, size_t len)
447 int rfc1524_expand_filename (char *nametemplate,
454 short lmatch = 0, rmatch = 0;
455 char left[_POSIX_PATH_MAX];
456 char right[_POSIX_PATH_MAX];
460 /* first, ignore leading path components.
463 if (nametemplate && (s = strrchr (nametemplate, '/')))
464 nametemplate = s + 1;
466 if (oldfile && (s = strrchr (oldfile, '/')))
472 strfcpy (newfile, oldfile, nflen);
476 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
478 else /* oldfile && nametemplate */
481 /* first, compare everything left from the "%s"
486 for(i = 0; nametemplate[i]; i++)
488 if(nametemplate[i] == '%' && nametemplate[i+1] == 's')
494 /* note that the following will _not_ read beyond oldfile's end. */
496 if(lmatch && nametemplate[i] != oldfile[i])
503 /* If we had a "%s", check the rest. */
505 /* now, for the right part: compare everything right from
506 * the "%s" to the final part of oldfile.
508 * The logic here is as follows:
510 * - We start reading from the end.
511 * - There must be a match _right_ from the "%s",
513 * - If there was a left hand match, this stuff
514 * must not be counted again. That's done by the
515 * condition (j >= (lmatch ? i : 0)).
520 for(r = 0, j = mutt_strlen(oldfile) - 1, k = mutt_strlen(nametemplate) - 1 ;
521 j >= (lmatch ? i : 0) && k >= i + 2;
524 if(nametemplate[k] != oldfile[j])
531 /* Now, check if we had a full match. */
536 if(lmatch) *left = 0;
537 else strnfcpy(left, nametemplate, sizeof(left), i);
539 if(rmatch) *right = 0;
540 else strfcpy(right, nametemplate + i + 2, sizeof(right));
542 snprintf(newfile, nflen, "%s%s%s", left, oldfile, right);
546 /* no "%s" in the name template. */
547 strfcpy(newfile, nametemplate, nflen);
551 mutt_adv_mktemp(newfile, nflen);
560 /* If rfc1524_expand_command() is used on a recv'd message, then
561 * the filename doesn't exist yet, but if its used while sending a message,
562 * then we need to rename the existing file.
564 * This function returns 0 on successful move, 1 on old file doesn't exist,
565 * 2 on new file already exists, and 3 on other failure.
568 /* note on access(2) use: No dangling symlink problems here due to
572 int mutt_rename_file (char *oldfile, char *newfile)
576 if (access (oldfile, F_OK) != 0)
578 if (access (newfile, F_OK) == 0)
580 if ((ofp = fopen (oldfile,"r")) == NULL)
582 if ((nfp = safe_fopen (newfile,"w")) == NULL)
587 mutt_copy_stream (ofp,nfp);
590 mutt_unlink (oldfile);