2 * Copyright (C) 1996-2000 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., 59 Temple Place - Suite 330, Boston, MA 02111, 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
41 /* The command semantics include the following:
42 * %s is the filename that contains the mail body data
43 * %t is the content type, like text/plain
44 * %{parameter} is replaced by the parameter value from the content-type field
46 * Unsupported rfc1524 parameters: these would probably require some doing
47 * by mutt, and can probably just be done by piping the message to metamail
48 * %n is the integer number of sub-parts in the multipart
49 * %F is "content-type filename" repeated for each sub-part
51 * In addition, this function returns a 0 if the command works on a file,
52 * and 1 if the command works on a pipe.
54 int rfc1524_expand_command (BODY *a, char *filename, char *_type,
55 char *command, int clen)
59 char buf[LONG_STRING];
60 char type[LONG_STRING];
62 strfcpy (type, _type, sizeof (type));
64 if (option (OPTMAILCAPSANITIZE))
65 mutt_sanitize_filename (type, 0);
67 while (command[x] && x<clen && y<sizeof(buf))
69 if (command[x] == '\\') {
71 buf[y++] = command[x++];
73 else if (command[x] == '%')
76 if (command[x] == '{')
84 while (command[x] && command[x] != '}' && z<sizeof(param))
85 param[z++] = command[x++];
88 _pvalue = mutt_get_parameter (param, a->parameter);
89 strfcpy (pvalue, NONULL(_pvalue), sizeof (pvalue));
90 if (option (OPTMAILCAPSANITIZE))
91 mutt_sanitize_filename (pvalue, 0);
93 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
95 else if (command[x] == 's' && filename != NULL)
97 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
100 else if (command[x] == 't')
102 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
107 buf[y++] = command[x++];
110 strfcpy (command, buf, clen);
115 /* NUL terminates a rfc 1524 field,
116 * returns start of next field or NULL */
117 static char *get_field (char *s)
124 while ((ch = strpbrk (s, ";\\")) != NULL)
139 mutt_remove_trailing_ws (s);
143 static int get_field_text (char *field, char **entry,
144 char *type, char *filename, int line)
146 field = mutt_skip_whitespace (field);
152 field = mutt_skip_whitespace (field);
153 mutt_str_replace (entry, field);
159 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
160 type, filename, line);
165 static int rfc1524_mailcap_parse (BODY *a,
168 rfc1524_entry *entry,
184 /* rfc1524 mailcap file is of the format:
185 * base/type; command; extradefs
186 * type can be * for matching all
187 * base with no /type is an implicit wild
188 * command contains a %s for the filename to pass, default to pipe on stdin
189 * extradefs are of the form:
190 * def1="definition"; def2="define \;";
191 * line wraps with a \ at the end of the line
195 /* find length of basetype */
196 if ((ch = strchr (type, '/')) == NULL)
200 if ((fp = fopen (filename, "r")) != NULL)
202 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line)) != NULL)
204 /* ignore comments */
207 dprint (2, (debugfile, "mailcap entry: %s\n", buf));
210 ch = get_field (buf);
211 if (ascii_strcasecmp (buf, type) &&
212 (ascii_strncasecmp (buf, type, btlen) ||
213 (buf[btlen] != 0 && /* implicit wild */
214 mutt_strcmp (buf + btlen, "/*")))) /* wildsubtype */
217 /* next field is the viewcommand */
221 entry->command = safe_strdup (field);
223 /* parse the optional fields */
225 copiousoutput = FALSE;
226 composecommand = FALSE;
228 printcommand = FALSE;
234 dprint (2, (debugfile, "field: %s\n", field));
236 if (!ascii_strcasecmp (field, "needsterminal"))
239 entry->needsterminal = TRUE;
241 else if (!ascii_strcasecmp (field, "copiousoutput"))
243 copiousoutput = TRUE;
245 entry->copiousoutput = TRUE;
247 else if (!ascii_strncasecmp (field, "composetyped", 12))
249 /* this compare most occur before compose to match correctly */
250 if (get_field_text (field + 12, entry ? &entry->composetypecommand : NULL,
251 type, filename, line))
252 composecommand = TRUE;
254 else if (!ascii_strncasecmp (field, "compose", 7))
256 if (get_field_text (field + 7, entry ? &entry->composecommand : NULL,
257 type, filename, line))
258 composecommand = TRUE;
260 else if (!ascii_strncasecmp (field, "print", 5))
262 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
263 type, filename, line))
266 else if (!ascii_strncasecmp (field, "edit", 4))
268 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
269 type, filename, line))
272 else if (!ascii_strncasecmp (field, "nametemplate", 12))
274 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
275 type, filename, line);
277 else if (!ascii_strncasecmp (field, "x-convert", 9))
279 get_field_text (field + 9, entry ? &entry->convert : NULL,
280 type, filename, line);
282 else if (!ascii_strncasecmp (field, "test", 4))
285 * This routine executes the given test command to determine
286 * if this is the right entry.
288 char *test_command = NULL;
291 if (get_field_text (field + 4, &test_command, type, filename, line)
294 len = mutt_strlen (test_command) + STRING;
295 safe_realloc (&test_command, len);
296 rfc1524_expand_command (a, a->filename, type, test_command, len);
297 if (mutt_system (test_command))
299 /* a non-zero exit code means test failed */
302 FREE (&test_command);
307 if (opt == M_AUTOVIEW)
312 else if (opt == M_COMPOSE)
317 else if (opt == M_EDIT)
322 else if (opt == M_PRINT)
333 FREE (&entry->command);
334 FREE (&entry->composecommand);
335 FREE (&entry->composetypecommand);
336 FREE (&entry->editcommand);
337 FREE (&entry->printcommand);
338 FREE (&entry->nametemplate);
339 FREE (&entry->convert);
340 entry->needsterminal = 0;
341 entry->copiousoutput = 0;
344 } /* while (!found && (buf = mutt_read_line ())) */
346 } /* if ((fp = fopen ())) */
351 rfc1524_entry *rfc1524_new_entry(void)
353 return (rfc1524_entry *)safe_calloc(1, sizeof(rfc1524_entry));
356 void rfc1524_free_entry(rfc1524_entry **entry)
358 rfc1524_entry *p = *entry;
361 FREE (&p->testcommand);
362 FREE (&p->composecommand);
363 FREE (&p->composetypecommand);
364 FREE (&p->editcommand);
365 FREE (&p->printcommand);
366 FREE (&p->nametemplate);
371 * rfc1524_mailcap_lookup attempts to find the given type in the
372 * list of mailcap files. On success, this returns the entry information
373 * in *entry, and returns 1. On failure (not found), returns 0.
374 * If entry == NULL just return 1 if the given type is found.
376 int rfc1524_mailcap_lookup (BODY *a, char *type, rfc1524_entry *entry, int opt)
378 char path[_POSIX_PATH_MAX];
381 char *curr = MailcapPath;
383 /* rfc1524 specifies that a path of mailcap files should be searched.
385 * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
386 * and overriden by the MAILCAPS environment variable, and, just to be nice,
387 * we'll make it specifiable in .muttrc
391 mutt_error _("No mailcap path specified");
395 mutt_check_lookup_list (a, type, SHORT_STRING);
397 while (!found && *curr)
400 while (*curr && *curr != ':' && x < sizeof (path) - 1)
412 mutt_expand_path (path, sizeof (path));
414 dprint(2,(debugfile,"Checking mailcap file: %s\n",path));
415 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
419 mutt_error (_("mailcap entry for type %s not found"), type);
425 /* This routine will create a _temporary_ filename matching the
426 * name template given if this needs to be done.
428 * Please note that only the last path element of the
429 * template and/or the old file name will be used for the
430 * comparison and the temporary file name.
432 * Returns 0 if oldfile is fine as is.
433 * Returns 1 if newfile specified
436 static void strnfcpy(char *d, char *s, size_t siz, size_t len)
443 int rfc1524_expand_filename (char *nametemplate,
450 short lmatch = 0, rmatch = 0;
451 char left[_POSIX_PATH_MAX];
452 char right[_POSIX_PATH_MAX];
456 /* first, ignore leading path components.
459 if (nametemplate && (s = strrchr (nametemplate, '/')))
460 nametemplate = s + 1;
462 if (oldfile && (s = strrchr (oldfile, '/')))
468 strfcpy (newfile, oldfile, nflen);
472 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
474 else /* oldfile && nametemplate */
477 /* first, compare everything left from the "%s"
482 for(i = 0; nametemplate[i]; i++)
484 if(nametemplate[i] == '%' && nametemplate[i+1] == 's')
490 /* note that the following will _not_ read beyond oldfile's end. */
492 if(lmatch && nametemplate[i] != oldfile[i])
499 /* If we had a "%s", check the rest. */
501 /* now, for the right part: compare everything right from
502 * the "%s" to the final part of oldfile.
504 * The logic here is as follows:
506 * - We start reading from the end.
507 * - There must be a match _right_ from the "%s",
509 * - If there was a left hand match, this stuff
510 * must not be counted again. That's done by the
511 * condition (j >= (lmatch ? i : 0)).
516 for(r = 0, j = mutt_strlen(oldfile) - 1, k = mutt_strlen(nametemplate) - 1 ;
517 j >= (lmatch ? i : 0) && k >= i + 2;
520 if(nametemplate[k] != oldfile[j])
527 /* Now, check if we had a full match. */
532 if(lmatch) *left = 0;
533 else strnfcpy(left, nametemplate, sizeof(left), i);
535 if(rmatch) *right = 0;
536 else strfcpy(right, nametemplate + i + 2, sizeof(right));
538 snprintf(newfile, nflen, "%s%s%s", left, oldfile, right);
542 /* no "%s" in the name template. */
543 strfcpy(newfile, nametemplate, nflen);
547 mutt_adv_mktemp(newfile, nflen);
556 /* If rfc1524_expand_command() is used on a recv'd message, then
557 * the filename doesn't exist yet, but if its used while sending a message,
558 * then we need to rename the existing file.
560 * This function returns 0 on successful move, 1 on old file doesn't exist,
561 * 2 on new file already exists, and 3 on other failure.
564 /* note on access(2) use: No dangling symlink problems here due to
568 int _mutt_rename_file (char *oldfile, char *newfile, int overwrite)
572 if (access (oldfile, F_OK) != 0)
574 if (!overwrite && access (newfile, F_OK) == 0)
576 if ((ofp = fopen (oldfile,"r")) == NULL)
578 if ((nfp = safe_fopen (newfile,"w")) == NULL)
583 mutt_copy_stream (ofp,nfp);
586 mutt_unlink (oldfile);
590 int mutt_rename_file (char *oldfile, char *newfile)
592 return _mutt_rename_file (oldfile, newfile, 0);