2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
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.
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.
16 * This file contains various functions for implementing a fair subset of
30 #include "lib/debug.h"
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)) {
68 if (command[x] == '\\') {
70 buf[y++] = command[x++];
72 else if (command[x] == '%') {
74 if (command[x] == '{') {
81 while (command[x] && command[x] != '}' && z < sizeof (param))
82 param[z++] = command[x++];
85 _pvalue = mutt_get_parameter (param, a->parameter);
86 strfcpy (pvalue, NONULL (_pvalue), sizeof (pvalue));
87 if (option (OPTMAILCAPSANITIZE))
88 mutt_sanitize_filename (pvalue, 0);
90 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
92 else if (command[x] == 's' && filename != NULL) {
93 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
96 else if (command[x] == 't') {
97 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
102 buf[y++] = command[x++];
105 strfcpy (command, buf, clen);
110 /* NUL terminates a rfc 1524 field,
111 * returns start of next field or NULL */
112 static char *get_field (char *s)
119 while ((ch = strpbrk (s, ";\\")) != NULL) {
131 str_skip_trailws (s);
135 static int get_field_text (char *field, char **entry,
136 char *type, char *filename, int line)
138 field = str_skip_initws (field);
142 field = str_skip_initws (field);
143 str_replace (entry, field);
148 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
149 type, filename, line);
154 static int rfc1524_mailcap_parse (BODY * a,
156 char *type, rfc1524_entry * entry, int opt)
171 /* rfc1524 mailcap file is of the format:
172 * base/type; command; extradefs
173 * type can be * for matching all
174 * base with no /type is an implicit wild
175 * command contains a %s for the filename to pass, default to pipe on stdin
176 * extradefs are of the form:
177 * def1="definition"; def2="define \;";
178 * line wraps with a \ at the end of the line
182 /* find length of basetype */
183 if ((ch = strchr (type, '/')) == NULL)
187 if ((fp = fopen (filename, "r")) != NULL) {
188 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line)) != NULL) {
189 /* ignore comments */
192 debug_print (2, ("mailcap entry: %s\n", buf));
195 ch = get_field (buf);
196 if (ascii_strcasecmp (buf, type) && (ascii_strncasecmp (buf, type, btlen) || (buf[btlen] != 0 && /* implicit wild */
197 mutt_strcmp (buf + btlen, "/*")))) /* wildsubtype */
200 /* next field is the viewcommand */
204 entry->command = safe_strdup (field);
206 /* parse the optional fields */
208 copiousoutput = FALSE;
209 composecommand = FALSE;
211 printcommand = FALSE;
216 debug_print (2, ("field: %s\n", field));
218 if (!ascii_strcasecmp (field, "needsterminal")) {
220 entry->needsterminal = TRUE;
222 else if (!ascii_strcasecmp (field, "copiousoutput")) {
223 copiousoutput = TRUE;
225 entry->copiousoutput = TRUE;
227 else if (!ascii_strncasecmp (field, "composetyped", 12)) {
228 /* this compare most occur before compose to match correctly */
230 (field + 12, entry ? &entry->composetypecommand : NULL, type,
232 composecommand = TRUE;
234 else if (!ascii_strncasecmp (field, "compose", 7)) {
236 (field + 7, entry ? &entry->composecommand : NULL, type,
238 composecommand = TRUE;
240 else if (!ascii_strncasecmp (field, "print", 5)) {
241 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
242 type, filename, line))
245 else if (!ascii_strncasecmp (field, "edit", 4)) {
246 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
247 type, filename, line))
250 else if (!ascii_strncasecmp (field, "nametemplate", 12)) {
251 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
252 type, filename, line);
254 else if (!ascii_strncasecmp (field, "x-convert", 9)) {
255 get_field_text (field + 9, entry ? &entry->convert : NULL,
256 type, filename, line);
258 else if (!ascii_strncasecmp (field, "test", 4)) {
260 * This routine executes the given test command to determine
261 * if this is the right entry.
263 char *test_command = NULL;
266 if (get_field_text (field + 4, &test_command, type, filename, line)
268 len = mutt_strlen (test_command) + STRING;
269 safe_realloc (&test_command, len);
270 rfc1524_expand_command (a, a->filename, type, test_command, len);
271 if (mutt_system (test_command)) {
272 /* a non-zero exit code means test failed */
275 FREE (&test_command);
280 if (opt == M_AUTOVIEW) {
284 else if (opt == M_COMPOSE) {
288 else if (opt == M_EDIT) {
292 else if (opt == M_PRINT) {
300 FREE (&entry->command);
301 FREE (&entry->composecommand);
302 FREE (&entry->composetypecommand);
303 FREE (&entry->editcommand);
304 FREE (&entry->printcommand);
305 FREE (&entry->nametemplate);
306 FREE (&entry->convert);
307 entry->needsterminal = 0;
308 entry->copiousoutput = 0;
311 } /* while (!found && (buf = mutt_read_line ())) */
313 } /* if ((fp = fopen ())) */
318 rfc1524_entry *rfc1524_new_entry (void)
320 return (rfc1524_entry *) safe_calloc (1, sizeof (rfc1524_entry));
323 void rfc1524_free_entry (rfc1524_entry ** entry)
325 rfc1524_entry *p = *entry;
328 FREE (&p->testcommand);
329 FREE (&p->composecommand);
330 FREE (&p->composetypecommand);
331 FREE (&p->editcommand);
332 FREE (&p->printcommand);
333 FREE (&p->nametemplate);
338 * rfc1524_mailcap_lookup attempts to find the given type in the
339 * list of mailcap files. On success, this returns the entry information
340 * in *entry, and returns 1. On failure (not found), returns 0.
341 * If entry == NULL just return 1 if the given type is found.
343 int rfc1524_mailcap_lookup (BODY * a, char *type, rfc1524_entry * entry,
346 char path[_POSIX_PATH_MAX];
349 char *curr = MailcapPath;
351 /* rfc1524 specifies that a path of mailcap files should be searched.
353 * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
354 * and overriden by the MAILCAPS environment variable, and, just to be nice,
355 * we'll make it specifiable in .muttrc
357 if (!curr || !*curr) {
358 mutt_error _("No mailcap path specified");
363 mutt_check_lookup_list (a, type, SHORT_STRING);
365 while (!found && *curr) {
367 while (*curr && *curr != ':' && x < sizeof (path) - 1) {
378 mutt_expand_path (path, sizeof (path));
380 debug_print (2, ("Checking mailcap file: %s\n", path));
381 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
385 mutt_error (_("mailcap entry for type %s not found"), type);
391 /* This routine will create a _temporary_ filename matching the
392 * name template given if this needs to be done.
394 * Please note that only the last path element of the
395 * template and/or the old file name will be used for the
396 * comparison and the temporary file name.
398 * Returns 0 if oldfile is fine as is.
399 * Returns 1 if newfile specified
402 static void strnfcpy (char *d, char *s, size_t siz, size_t len)
409 int rfc1524_expand_filename (char *nametemplate,
410 char *oldfile, char *newfile, size_t nflen)
414 short lmatch = 0, rmatch = 0;
415 char left[_POSIX_PATH_MAX];
416 char right[_POSIX_PATH_MAX];
420 /* first, ignore leading path components.
423 if (nametemplate && (s = strrchr (nametemplate, '/')))
424 nametemplate = s + 1;
426 if (oldfile && (s = strrchr (oldfile, '/')))
431 strfcpy (newfile, oldfile, nflen);
434 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
436 else { /* oldfile && nametemplate */
439 /* first, compare everything left from the "%s"
445 for (i = 0; nametemplate[i]; i++) {
446 if (nametemplate[i] == '%' && nametemplate[i + 1] == 's') {
451 /* note that the following will _not_ read beyond oldfile's end. */
453 if (lmatch && nametemplate[i] != oldfile[i])
459 /* If we had a "%s", check the rest. */
461 /* now, for the right part: compare everything right from
462 * the "%s" to the final part of oldfile.
464 * The logic here is as follows:
466 * - We start reading from the end.
467 * - There must be a match _right_ from the "%s",
469 * - If there was a left hand match, this stuff
470 * must not be counted again. That's done by the
471 * condition (j >= (lmatch ? i : 0)).
476 for (r = 0, j = mutt_strlen (oldfile) - 1, k =
477 mutt_strlen (nametemplate) - 1;
478 j >= (lmatch ? i : 0) && k >= i + 2; j--, k--) {
479 if (nametemplate[k] != oldfile[j]) {
485 /* Now, check if we had a full match. */
493 strnfcpy (left, nametemplate, sizeof (left), i);
498 strfcpy (right, nametemplate + i + 2, sizeof (right));
500 snprintf (newfile, nflen, "%s%s%s", left, oldfile, right);
503 /* no "%s" in the name template. */
504 strfcpy (newfile, nametemplate, nflen);
508 mutt_adv_mktemp (newfile, nflen);
510 if (rmatch && lmatch)
517 /* If rfc1524_expand_command() is used on a recv'd message, then
518 * the filename doesn't exist yet, but if its used while sending a message,
519 * then we need to rename the existing file.
521 * This function returns 0 on successful move, 1 on old file doesn't exist,
522 * 2 on new file already exists, and 3 on other failure.
525 /* note on access(2) use: No dangling symlink problems here due to
529 int _mutt_rename_file (char *oldfile, char *newfile, int overwrite)
533 if (access (oldfile, F_OK) != 0)
535 if (!overwrite && access (newfile, F_OK) == 0)
537 if ((ofp = fopen (oldfile, "r")) == NULL)
539 if ((nfp = safe_fopen (newfile, "w")) == NULL) {
543 mutt_copy_stream (ofp, nfp);
546 mutt_unlink (oldfile);
550 int mutt_rename_file (char *oldfile, char *newfile)
552 return _mutt_rename_file (oldfile, newfile, 0);