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
24 #include <lib-lib/mem.h>
25 #include <lib-lib/str.h>
26 #include <lib-lib/ascii.h>
27 #include <lib-lib/macros.h>
28 #include <lib-lib/file.h>
34 #include "lib/debug.h"
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 m_strcpy(type, sizeof(type), _type);
68 if (option (OPTMAILCAPSANITIZE))
69 mutt_sanitize_filename (type, 0);
71 while (command[x] && x < clen && y < sizeof (buf)) {
72 if (command[x] == '\\') {
74 buf[y++] = command[x++];
76 else if (command[x] == '%') {
78 if (command[x] == '{') {
85 while (command[x] && command[x] != '}' && z < sizeof (param))
86 param[z++] = command[x++];
89 _pvalue = mutt_get_parameter (param, a->parameter);
90 m_strcpy(pvalue, sizeof(pvalue), NONULL(_pvalue));
91 if (option (OPTMAILCAPSANITIZE))
92 mutt_sanitize_filename (pvalue, 0);
94 y += mutt_quote_filename (buf + y, sizeof (buf) - y, pvalue);
96 else if (command[x] == 's' && filename != NULL) {
97 y += mutt_quote_filename (buf + y, sizeof (buf) - y, filename);
100 else if (command[x] == 't') {
101 y += mutt_quote_filename (buf + y, sizeof (buf) - y, type);
106 buf[y++] = command[x++];
109 m_strcpy(command, clen, buf);
114 /* NUL terminates a rfc 1524 field,
115 * returns start of next field or NULL */
116 static char *get_field (char *s)
123 while ((ch = strpbrk (s, ";\\")) != NULL) {
131 ch = vskipspaces(ch);
135 str_skip_trailws (s);
139 static int get_field_text (char *field, char **entry,
140 char *type, char *filename, int line)
142 field = vskipspaces(field);
145 field = vskipspaces(field + 1);
146 str_replace (entry, field);
151 mutt_error (_("Improperly formated entry for type %s in \"%s\" line %d"),
152 type, filename, line);
157 static int rfc1524_mailcap_parse (BODY * a,
159 char *type, rfc1524_entry * entry, int opt)
174 /* rfc1524 mailcap file is of the format:
175 * base/type; command; extradefs
176 * type can be * for matching all
177 * base with no /type is an implicit wild
178 * command contains a %s for the filename to pass, default to pipe on stdin
179 * extradefs are of the form:
180 * def1="definition"; def2="define \;";
181 * line wraps with a \ at the end of the line
185 /* find length of basetype */
186 if ((ch = strchr (type, '/')) == NULL)
190 if ((fp = fopen (filename, "r")) != NULL) {
191 while (!found && (buf = mutt_read_line (buf, &buflen, fp, &line)) != NULL) {
192 /* ignore comments */
195 debug_print (2, ("mailcap entry: %s\n", buf));
198 ch = get_field (buf);
199 if (ascii_strcasecmp (buf, type) && (ascii_strncasecmp (buf, type, btlen) || (buf[btlen] != 0 && /* implicit wild */
200 m_strcmp(buf + btlen, "/*")))) /* wildsubtype */
203 /* next field is the viewcommand */
207 entry->command = m_strdup(field);
209 /* parse the optional fields */
211 copiousoutput = FALSE;
212 composecommand = FALSE;
214 printcommand = FALSE;
219 debug_print (2, ("field: %s\n", field));
221 if (!ascii_strcasecmp (field, "needsterminal")) {
223 entry->needsterminal = TRUE;
225 else if (!ascii_strcasecmp (field, "copiousoutput")) {
226 copiousoutput = TRUE;
228 entry->copiousoutput = TRUE;
230 else if (!ascii_strncasecmp (field, "composetyped", 12)) {
231 /* this compare most occur before compose to match correctly */
233 (field + 12, entry ? &entry->composetypecommand : NULL, type,
235 composecommand = TRUE;
237 else if (!ascii_strncasecmp (field, "compose", 7)) {
239 (field + 7, entry ? &entry->composecommand : NULL, type,
241 composecommand = TRUE;
243 else if (!ascii_strncasecmp (field, "print", 5)) {
244 if (get_field_text (field + 5, entry ? &entry->printcommand : NULL,
245 type, filename, line))
248 else if (!ascii_strncasecmp (field, "edit", 4)) {
249 if (get_field_text (field + 4, entry ? &entry->editcommand : NULL,
250 type, filename, line))
253 else if (!ascii_strncasecmp (field, "nametemplate", 12)) {
254 get_field_text (field + 12, entry ? &entry->nametemplate : NULL,
255 type, filename, line);
257 else if (!ascii_strncasecmp (field, "x-convert", 9)) {
258 get_field_text (field + 9, entry ? &entry->convert : NULL,
259 type, filename, line);
261 else if (!ascii_strncasecmp (field, "test", 4)) {
263 * This routine executes the given test command to determine
264 * if this is the right entry.
266 char *test_command = NULL;
269 if (get_field_text (field + 4, &test_command, type, filename, line)
271 len = m_strlen(test_command) + STRING;
272 p_realloc(&test_command, len);
273 rfc1524_expand_command (a, a->filename, type, test_command, len);
274 if (mutt_system (test_command)) {
275 /* a non-zero exit code means test failed */
278 p_delete(&test_command);
283 if (opt == M_AUTOVIEW) {
287 else if (opt == M_COMPOSE) {
291 else if (opt == M_EDIT) {
295 else if (opt == M_PRINT) {
303 p_delete(&entry->command);
304 p_delete(&entry->composecommand);
305 p_delete(&entry->composetypecommand);
306 p_delete(&entry->editcommand);
307 p_delete(&entry->printcommand);
308 p_delete(&entry->nametemplate);
309 p_delete(&entry->convert);
310 entry->needsterminal = 0;
311 entry->copiousoutput = 0;
314 } /* while (!found && (buf = mutt_read_line ())) */
316 } /* if ((fp = fopen ())) */
321 rfc1524_entry *rfc1524_new_entry (void)
323 return p_new(rfc1524_entry, 1);
326 void rfc1524_free_entry (rfc1524_entry ** entry)
328 rfc1524_entry *p = *entry;
330 p_delete(&p->command);
331 p_delete(&p->testcommand);
332 p_delete(&p->composecommand);
333 p_delete(&p->composetypecommand);
334 p_delete(&p->editcommand);
335 p_delete(&p->printcommand);
336 p_delete(&p->nametemplate);
341 * rfc1524_mailcap_lookup attempts to find the given type in the
342 * list of mailcap files. On success, this returns the entry information
343 * in *entry, and returns 1. On failure (not found), returns 0.
344 * If entry == NULL just return 1 if the given type is found.
346 int rfc1524_mailcap_lookup (BODY * a, char *type, rfc1524_entry * entry,
349 char path[_POSIX_PATH_MAX];
352 char *curr = MailcapPath;
354 /* rfc1524 specifies that a path of mailcap files should be searched.
356 * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
357 * and overriden by the MAILCAPS environment variable, and, just to be nice,
358 * we'll make it specifiable in .muttrc
360 if (!curr || !*curr) {
361 mutt_error _("No mailcap path specified");
366 mutt_check_lookup_list (a, type, SHORT_STRING);
368 while (!found && *curr) {
370 while (*curr && *curr != ':' && x < sizeof (path) - 1) {
381 mutt_expand_path (path, sizeof (path));
383 debug_print (2, ("Checking mailcap file: %s\n", path));
384 found = rfc1524_mailcap_parse (a, path, type, entry, opt);
388 mutt_error (_("mailcap entry for type %s not found"), type);
394 /* This routine will create a _temporary_ filename matching the
395 * name template given if this needs to be done.
397 * Please note that only the last path element of the
398 * template and/or the old file name will be used for the
399 * comparison and the temporary file name.
401 * Returns 0 if oldfile is fine as is.
402 * Returns 1 if newfile specified
405 static void strnfcpy (char *d, char *s, size_t siz, size_t len)
412 int rfc1524_expand_filename (char *nametemplate,
413 char *oldfile, char *newfile, size_t nflen)
417 short lmatch = 0, rmatch = 0;
418 char left[_POSIX_PATH_MAX];
419 char right[_POSIX_PATH_MAX];
423 /* first, ignore leading path components.
426 if (nametemplate && (s = strrchr (nametemplate, '/')))
427 nametemplate = s + 1;
429 if (oldfile && (s = strrchr (oldfile, '/')))
434 m_strcpy(newfile, nflen, oldfile);
437 mutt_expand_fmt (newfile, nflen, nametemplate, "mutt");
439 else { /* oldfile && nametemplate */
442 /* first, compare everything left from the "%s"
448 for (i = 0; nametemplate[i]; i++) {
449 if (nametemplate[i] == '%' && nametemplate[i + 1] == 's') {
454 /* note that the following will _not_ read beyond oldfile's end. */
456 if (lmatch && nametemplate[i] != oldfile[i])
462 /* If we had a "%s", check the rest. */
464 /* now, for the right part: compare everything right from
465 * the "%s" to the final part of oldfile.
467 * The logic here is as follows:
469 * - We start reading from the end.
470 * - There must be a match _right_ from the "%s",
472 * - If there was a left hand match, this stuff
473 * must not be counted again. That's done by the
474 * condition (j >= (lmatch ? i : 0)).
479 for (r = 0, j = m_strlen(oldfile) - 1, k =
480 m_strlen(nametemplate) - 1;
481 j >= (lmatch ? i : 0) && k >= i + 2; j--, k--) {
482 if (nametemplate[k] != oldfile[j]) {
488 /* Now, check if we had a full match. */
496 strnfcpy (left, nametemplate, sizeof (left), i);
501 m_strcpy(right, sizeof(right), nametemplate + i + 2);
503 snprintf (newfile, nflen, "%s%s%s", left, oldfile, right);
506 /* no "%s" in the name template. */
507 m_strcpy(newfile, nflen, nametemplate);
511 mutt_adv_mktemp (NULL, newfile, nflen);
513 if (rmatch && lmatch)
520 /* If rfc1524_expand_command() is used on a recv'd message, then
521 * the filename doesn't exist yet, but if its used while sending a message,
522 * then we need to rename the existing file.
524 * This function returns 0 on successful move, 1 on old file doesn't exist,
525 * 2 on new file already exists, and 3 on other failure.
528 /* note on access(2) use: No dangling symlink problems here due to
532 int _mutt_rename_file (char *oldfile, char *newfile, int overwrite)
536 if (access (oldfile, F_OK) != 0)
538 if (!overwrite && access (newfile, F_OK) == 0)
540 if ((ofp = fopen (oldfile, "r")) == NULL)
542 if ((nfp = safe_fopen (newfile, "w")) == NULL) {
546 mutt_copy_stream (ofp, nfp);
549 mutt_unlink (oldfile);
553 int mutt_rename_file (char *oldfile, char *newfile)
555 return _mutt_rename_file (oldfile, newfile, 0);