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
40 /* The command semantics include the following:
41 * %s is the filename that contains the mail body data
42 * %t is the content type, like text/plain
43 * %{parameter} is replaced by the parameter value from the content-type field
45 * Unsupported rfc1524 parameters: these would probably require some doing
46 * by mutt, and can probably just be done by piping the message to metamail
47 * %n is the integer number of sub-parts in the multipart
48 * %F is "content-type filename" repeated for each sub-part
50 * In addition, this function returns a 0 if the command works on a file,
51 * and 1 if the command works on a pipe.
53 int rfc1524_expand_command (BODY * a, char *filename, char *_type,
54 char *command, int clen)
58 char buf[LONG_STRING];
59 char type[LONG_STRING];
61 strfcpy (type, _type, sizeof (type));
63 if (option (OPTMAILCAPSANITIZE))
64 mutt_sanitize_filename (type, 0);
66 while (command[x] && x < clen && y < sizeof (buf)) {
67 if (command[x] == '\\') {
69 buf[y++] = command[x++];
71 else if (command[x] == '%') {
73 if (command[x] == '{') {
80 while (command[x] && command[x] != '}' && z < sizeof (param))
81 param[z++] = command[x++];
84 _pvalue = mutt_get_parameter (param, a->parameter);
85 strfcpy (pvalue, NONULL (_pvalue), sizeof (pvalue));
86 if (option (OPTMAILCAPSANITIZE))
87 mutt_sanitize_filename (pvalue, 0);
89 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
91 else if (command[x] == 's' && filename != NULL) {
92 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
95 else if (command[x] == 't') {
96 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
101 buf[y++] = command[x++];
104 strfcpy (command, buf, clen);
109 /* NUL terminates a rfc 1524 field,
110 * returns start of next field or NULL */
111 static char *get_field (char *s)
118 while ((ch = strpbrk (s, ";\\")) != NULL) {
130 mutt_remove_trailing_ws (s);
134 static int get_field_text (char *field, char **entry,
135 char *type, char *filename, int line)
137 field = mutt_skip_whitespace (field);
141 field = mutt_skip_whitespace (field);
142 mutt_str_replace (entry, field);
147 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
148 type, filename, line);
153 static int rfc1524_mailcap_parse (BODY * a,
155 char *type, rfc1524_entry * entry, int opt)
170 /* rfc1524 mailcap file is of the format:
171 * base/type; command; extradefs
172 * type can be * for matching all
173 * base with no /type is an implicit wild
174 * command contains a %s for the filename to pass, default to pipe on stdin
175 * extradefs are of the form:
176 * def1="definition"; def2="define \;";
177 * line wraps with a \ at the end of the line
181 /* find length of basetype */
182 if ((ch = strchr (type, '/')) == NULL)
186 if ((fp = fopen (filename, "r")) != NULL) {
187 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line)) != NULL) {
188 /* ignore comments */
191 dprint (2, (debugfile, "mailcap entry: %s\n", buf));
194 ch = get_field (buf);
195 if (ascii_strcasecmp (buf, type) && (ascii_strncasecmp (buf, type, btlen) || (buf[btlen] != 0 && /* implicit wild */
196 mutt_strcmp (buf + btlen, "/*")))) /* wildsubtype */
199 /* next field is the viewcommand */
203 entry->command = safe_strdup (field);
205 /* parse the optional fields */
207 copiousoutput = FALSE;
208 composecommand = FALSE;
210 printcommand = FALSE;
215 dprint (2, (debugfile, "field: %s\n", field));
217 if (!ascii_strcasecmp (field, "needsterminal")) {
219 entry->needsterminal = TRUE;
221 else if (!ascii_strcasecmp (field, "copiousoutput")) {
222 copiousoutput = TRUE;
224 entry->copiousoutput = TRUE;
226 else if (!ascii_strncasecmp (field, "composetyped", 12)) {
227 /* this compare most occur before compose to match correctly */
229 (field + 12, entry ? &entry->composetypecommand : NULL, type,
231 composecommand = TRUE;
233 else if (!ascii_strncasecmp (field, "compose", 7)) {
235 (field + 7, entry ? &entry->composecommand : NULL, type,
237 composecommand = TRUE;
239 else if (!ascii_strncasecmp (field, "print", 5)) {
240 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
241 type, filename, line))
244 else if (!ascii_strncasecmp (field, "edit", 4)) {
245 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
246 type, filename, line))
249 else if (!ascii_strncasecmp (field, "nametemplate", 12)) {
250 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
251 type, filename, line);
253 else if (!ascii_strncasecmp (field, "x-convert", 9)) {
254 get_field_text (field + 9, entry ? &entry->convert : NULL,
255 type, filename, line);
257 else if (!ascii_strncasecmp (field, "test", 4)) {
259 * This routine executes the given test command to determine
260 * if this is the right entry.
262 char *test_command = NULL;
265 if (get_field_text (field + 4, &test_command, type, filename, line)
267 len = mutt_strlen (test_command) + STRING;
268 safe_realloc (&test_command, len);
269 rfc1524_expand_command (a, a->filename, type, test_command, len);
270 if (mutt_system (test_command)) {
271 /* a non-zero exit code means test failed */
274 FREE (&test_command);
279 if (opt == M_AUTOVIEW) {
283 else if (opt == M_COMPOSE) {
287 else if (opt == M_EDIT) {
291 else if (opt == M_PRINT) {
299 FREE (&entry->command);
300 FREE (&entry->composecommand);
301 FREE (&entry->composetypecommand);
302 FREE (&entry->editcommand);
303 FREE (&entry->printcommand);
304 FREE (&entry->nametemplate);
305 FREE (&entry->convert);
306 entry->needsterminal = 0;
307 entry->copiousoutput = 0;
310 } /* while (!found && (buf = mutt_read_line ())) */
312 } /* if ((fp = fopen ())) */
317 rfc1524_entry *rfc1524_new_entry (void)
319 return (rfc1524_entry *) safe_calloc (1, sizeof (rfc1524_entry));
322 void rfc1524_free_entry (rfc1524_entry ** entry)
324 rfc1524_entry *p = *entry;
327 FREE (&p->testcommand);
328 FREE (&p->composecommand);
329 FREE (&p->composetypecommand);
330 FREE (&p->editcommand);
331 FREE (&p->printcommand);
332 FREE (&p->nametemplate);
337 * rfc1524_mailcap_lookup attempts to find the given type in the
338 * list of mailcap files. On success, this returns the entry information
339 * in *entry, and returns 1. On failure (not found), returns 0.
340 * If entry == NULL just return 1 if the given type is found.
342 int rfc1524_mailcap_lookup (BODY * a, char *type, rfc1524_entry * entry,
345 char path[_POSIX_PATH_MAX];
348 char *curr = MailcapPath;
350 /* rfc1524 specifies that a path of mailcap files should be searched.
352 * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
353 * and overriden by the MAILCAPS environment variable, and, just to be nice,
354 * we'll make it specifiable in .muttrc
356 if (!curr || !*curr) {
357 mutt_error _("No mailcap path specified");
362 mutt_check_lookup_list (a, type, SHORT_STRING);
364 while (!found && *curr) {
366 while (*curr && *curr != ':' && x < sizeof (path) - 1) {
377 mutt_expand_path (path, sizeof (path));
379 dprint (2, (debugfile, "Checking mailcap file: %s\n", path));
380 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
384 mutt_error (_("mailcap entry for type %s not found"), type);
390 /* This routine will create a _temporary_ filename matching the
391 * name template given if this needs to be done.
393 * Please note that only the last path element of the
394 * template and/or the old file name will be used for the
395 * comparison and the temporary file name.
397 * Returns 0 if oldfile is fine as is.
398 * Returns 1 if newfile specified
401 static void strnfcpy (char *d, char *s, size_t siz, size_t len)
408 int rfc1524_expand_filename (char *nametemplate,
409 char *oldfile, char *newfile, size_t nflen)
413 short lmatch = 0, rmatch = 0;
414 char left[_POSIX_PATH_MAX];
415 char right[_POSIX_PATH_MAX];
419 /* first, ignore leading path components.
422 if (nametemplate && (s = strrchr (nametemplate, '/')))
423 nametemplate = s + 1;
425 if (oldfile && (s = strrchr (oldfile, '/')))
430 strfcpy (newfile, oldfile, nflen);
433 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
435 else { /* oldfile && nametemplate */
438 /* first, compare everything left from the "%s"
444 for (i = 0; nametemplate[i]; i++) {
445 if (nametemplate[i] == '%' && nametemplate[i + 1] == 's') {
450 /* note that the following will _not_ read beyond oldfile's end. */
452 if (lmatch && nametemplate[i] != oldfile[i])
458 /* If we had a "%s", check the rest. */
460 /* now, for the right part: compare everything right from
461 * the "%s" to the final part of oldfile.
463 * The logic here is as follows:
465 * - We start reading from the end.
466 * - There must be a match _right_ from the "%s",
468 * - If there was a left hand match, this stuff
469 * must not be counted again. That's done by the
470 * condition (j >= (lmatch ? i : 0)).
475 for (r = 0, j = mutt_strlen (oldfile) - 1, k =
476 mutt_strlen (nametemplate) - 1;
477 j >= (lmatch ? i : 0) && k >= i + 2; j--, k--) {
478 if (nametemplate[k] != oldfile[j]) {
484 /* Now, check if we had a full match. */
492 strnfcpy (left, nametemplate, sizeof (left), i);
497 strfcpy (right, nametemplate + i + 2, sizeof (right));
499 snprintf (newfile, nflen, "%s%s%s", left, oldfile, right);
502 /* no "%s" in the name template. */
503 strfcpy (newfile, nametemplate, nflen);
507 mutt_adv_mktemp (newfile, nflen);
509 if (rmatch && lmatch)
516 /* If rfc1524_expand_command() is used on a recv'd message, then
517 * the filename doesn't exist yet, but if its used while sending a message,
518 * then we need to rename the existing file.
520 * This function returns 0 on successful move, 1 on old file doesn't exist,
521 * 2 on new file already exists, and 3 on other failure.
524 /* note on access(2) use: No dangling symlink problems here due to
528 int _mutt_rename_file (char *oldfile, char *newfile, int overwrite)
532 if (access (oldfile, F_OK) != 0)
534 if (!overwrite && access (newfile, F_OK) == 0)
536 if ((ofp = fopen (oldfile, "r")) == NULL)
538 if ((nfp = safe_fopen (newfile, "w")) == NULL) {
542 mutt_copy_stream (ofp, nfp);
545 mutt_unlink (oldfile);
549 int mutt_rename_file (char *oldfile, char *newfile)
551 return _mutt_rename_file (oldfile, newfile, 0);