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
36 /* The command semantics include the following:
37 * %s is the filename that contains the mail body data
38 * %t is the content type, like text/plain
39 * %{parameter} is replaced by the parameter value from the content-type field
41 * Unsupported rfc1524 parameters: these would probably require some doing
42 * by mutt, and can probably just be done by piping the message to metamail
43 * %n is the integer number of sub-parts in the multipart
44 * %F is "content-type filename" repeated for each sub-part
46 * In addition, this function returns a 0 if the command works on a file,
47 * and 1 if the command works on a pipe.
49 int rfc1524_expand_command (BODY * a, char *filename, char *_type,
50 char *command, int clen)
54 char buf[LONG_STRING];
55 char type[LONG_STRING];
57 strfcpy (type, _type, sizeof (type));
59 if (option (OPTMAILCAPSANITIZE))
60 mutt_sanitize_filename (type, 0);
62 while (command[x] && x < clen && y < sizeof (buf)) {
63 if (command[x] == '\\') {
65 buf[y++] = command[x++];
67 else if (command[x] == '%') {
69 if (command[x] == '{') {
76 while (command[x] && command[x] != '}' && z < sizeof (param))
77 param[z++] = command[x++];
80 _pvalue = mutt_get_parameter (param, a->parameter);
81 strfcpy (pvalue, NONULL (_pvalue), sizeof (pvalue));
82 if (option (OPTMAILCAPSANITIZE))
83 mutt_sanitize_filename (pvalue, 0);
85 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
87 else if (command[x] == 's' && filename != NULL) {
88 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
91 else if (command[x] == 't') {
92 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
97 buf[y++] = command[x++];
100 strfcpy (command, buf, clen);
105 /* NUL terminates a rfc 1524 field,
106 * returns start of next field or NULL */
107 static char *get_field (char *s)
114 while ((ch = strpbrk (s, ";\\")) != NULL) {
126 mutt_remove_trailing_ws (s);
130 static int get_field_text (char *field, char **entry,
131 char *type, char *filename, int line)
133 field = mutt_skip_whitespace (field);
137 field = mutt_skip_whitespace (field);
138 mutt_str_replace (entry, field);
143 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
144 type, filename, line);
149 static int rfc1524_mailcap_parse (BODY * a,
151 char *type, rfc1524_entry * entry, int opt)
166 /* rfc1524 mailcap file is of the format:
167 * base/type; command; extradefs
168 * type can be * for matching all
169 * base with no /type is an implicit wild
170 * command contains a %s for the filename to pass, default to pipe on stdin
171 * extradefs are of the form:
172 * def1="definition"; def2="define \;";
173 * line wraps with a \ at the end of the line
177 /* find length of basetype */
178 if ((ch = strchr (type, '/')) == NULL)
182 if ((fp = fopen (filename, "r")) != NULL) {
183 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line)) != NULL) {
184 /* ignore comments */
187 dprint (2, (debugfile, "mailcap entry: %s\n", buf));
190 ch = get_field (buf);
191 if (ascii_strcasecmp (buf, type) && (ascii_strncasecmp (buf, type, btlen) || (buf[btlen] != 0 && /* implicit wild */
192 mutt_strcmp (buf + btlen, "/*")))) /* wildsubtype */
195 /* next field is the viewcommand */
199 entry->command = safe_strdup (field);
201 /* parse the optional fields */
203 copiousoutput = FALSE;
204 composecommand = FALSE;
206 printcommand = FALSE;
211 dprint (2, (debugfile, "field: %s\n", field));
213 if (!ascii_strcasecmp (field, "needsterminal")) {
215 entry->needsterminal = TRUE;
217 else if (!ascii_strcasecmp (field, "copiousoutput")) {
218 copiousoutput = TRUE;
220 entry->copiousoutput = TRUE;
222 else if (!ascii_strncasecmp (field, "composetyped", 12)) {
223 /* this compare most occur before compose to match correctly */
225 (field + 12, entry ? &entry->composetypecommand : NULL, type,
227 composecommand = TRUE;
229 else if (!ascii_strncasecmp (field, "compose", 7)) {
231 (field + 7, entry ? &entry->composecommand : NULL, type,
233 composecommand = TRUE;
235 else if (!ascii_strncasecmp (field, "print", 5)) {
236 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
237 type, filename, line))
240 else if (!ascii_strncasecmp (field, "edit", 4)) {
241 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
242 type, filename, line))
245 else if (!ascii_strncasecmp (field, "nametemplate", 12)) {
246 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
247 type, filename, line);
249 else if (!ascii_strncasecmp (field, "x-convert", 9)) {
250 get_field_text (field + 9, entry ? &entry->convert : NULL,
251 type, filename, line);
253 else if (!ascii_strncasecmp (field, "test", 4)) {
255 * This routine executes the given test command to determine
256 * if this is the right entry.
258 char *test_command = NULL;
261 if (get_field_text (field + 4, &test_command, type, filename, line)
263 len = mutt_strlen (test_command) + STRING;
264 safe_realloc (&test_command, len);
265 rfc1524_expand_command (a, a->filename, type, test_command, len);
266 if (mutt_system (test_command)) {
267 /* a non-zero exit code means test failed */
270 FREE (&test_command);
275 if (opt == M_AUTOVIEW) {
279 else if (opt == M_COMPOSE) {
283 else if (opt == M_EDIT) {
287 else if (opt == M_PRINT) {
295 FREE (&entry->command);
296 FREE (&entry->composecommand);
297 FREE (&entry->composetypecommand);
298 FREE (&entry->editcommand);
299 FREE (&entry->printcommand);
300 FREE (&entry->nametemplate);
301 FREE (&entry->convert);
302 entry->needsterminal = 0;
303 entry->copiousoutput = 0;
306 } /* while (!found && (buf = mutt_read_line ())) */
308 } /* if ((fp = fopen ())) */
313 rfc1524_entry *rfc1524_new_entry (void)
315 return (rfc1524_entry *) safe_calloc (1, sizeof (rfc1524_entry));
318 void rfc1524_free_entry (rfc1524_entry ** entry)
320 rfc1524_entry *p = *entry;
323 FREE (&p->testcommand);
324 FREE (&p->composecommand);
325 FREE (&p->composetypecommand);
326 FREE (&p->editcommand);
327 FREE (&p->printcommand);
328 FREE (&p->nametemplate);
333 * rfc1524_mailcap_lookup attempts to find the given type in the
334 * list of mailcap files. On success, this returns the entry information
335 * in *entry, and returns 1. On failure (not found), returns 0.
336 * If entry == NULL just return 1 if the given type is found.
338 int rfc1524_mailcap_lookup (BODY * a, char *type, rfc1524_entry * entry,
341 char path[_POSIX_PATH_MAX];
344 char *curr = MailcapPath;
346 /* rfc1524 specifies that a path of mailcap files should be searched.
348 * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
349 * and overriden by the MAILCAPS environment variable, and, just to be nice,
350 * we'll make it specifiable in .muttrc
352 if (!curr || !*curr) {
353 mutt_error _("No mailcap path specified");
358 mutt_check_lookup_list (a, type, SHORT_STRING);
360 while (!found && *curr) {
362 while (*curr && *curr != ':' && x < sizeof (path) - 1) {
373 mutt_expand_path (path, sizeof (path));
375 dprint (2, (debugfile, "Checking mailcap file: %s\n", path));
376 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
380 mutt_error (_("mailcap entry for type %s not found"), type);
386 /* This routine will create a _temporary_ filename matching the
387 * name template given if this needs to be done.
389 * Please note that only the last path element of the
390 * template and/or the old file name will be used for the
391 * comparison and the temporary file name.
393 * Returns 0 if oldfile is fine as is.
394 * Returns 1 if newfile specified
397 static void strnfcpy (char *d, char *s, size_t siz, size_t len)
404 int rfc1524_expand_filename (char *nametemplate,
405 char *oldfile, char *newfile, size_t nflen)
409 short lmatch = 0, rmatch = 0;
410 char left[_POSIX_PATH_MAX];
411 char right[_POSIX_PATH_MAX];
415 /* first, ignore leading path components.
418 if (nametemplate && (s = strrchr (nametemplate, '/')))
419 nametemplate = s + 1;
421 if (oldfile && (s = strrchr (oldfile, '/')))
426 strfcpy (newfile, oldfile, nflen);
429 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
431 else { /* oldfile && nametemplate */
434 /* first, compare everything left from the "%s"
440 for (i = 0; nametemplate[i]; i++) {
441 if (nametemplate[i] == '%' && nametemplate[i + 1] == 's') {
446 /* note that the following will _not_ read beyond oldfile's end. */
448 if (lmatch && nametemplate[i] != oldfile[i])
454 /* If we had a "%s", check the rest. */
456 /* now, for the right part: compare everything right from
457 * the "%s" to the final part of oldfile.
459 * The logic here is as follows:
461 * - We start reading from the end.
462 * - There must be a match _right_ from the "%s",
464 * - If there was a left hand match, this stuff
465 * must not be counted again. That's done by the
466 * condition (j >= (lmatch ? i : 0)).
471 for (r = 0, j = mutt_strlen (oldfile) - 1, k =
472 mutt_strlen (nametemplate) - 1;
473 j >= (lmatch ? i : 0) && k >= i + 2; j--, k--) {
474 if (nametemplate[k] != oldfile[j]) {
480 /* Now, check if we had a full match. */
488 strnfcpy (left, nametemplate, sizeof (left), i);
493 strfcpy (right, nametemplate + i + 2, sizeof (right));
495 snprintf (newfile, nflen, "%s%s%s", left, oldfile, right);
498 /* no "%s" in the name template. */
499 strfcpy (newfile, nametemplate, nflen);
503 mutt_adv_mktemp (newfile, nflen);
505 if (rmatch && lmatch)
512 /* If rfc1524_expand_command() is used on a recv'd message, then
513 * the filename doesn't exist yet, but if its used while sending a message,
514 * then we need to rename the existing file.
516 * This function returns 0 on successful move, 1 on old file doesn't exist,
517 * 2 on new file already exists, and 3 on other failure.
520 /* note on access(2) use: No dangling symlink problems here due to
524 int _mutt_rename_file (char *oldfile, char *newfile, int overwrite)
528 if (access (oldfile, F_OK) != 0)
530 if (!overwrite && access (newfile, F_OK) == 0)
532 if ((ofp = fopen (oldfile, "r")) == NULL)
534 if ((nfp = safe_fopen (newfile, "w")) == NULL) {
538 mutt_copy_stream (ofp, nfp);
541 mutt_unlink (oldfile);
545 int mutt_rename_file (char *oldfile, char *newfile)
547 return _mutt_rename_file (oldfile, newfile, 0);