Commit 3cd69e87 authored by Kevin J. McCarthy's avatar Kevin J. McCarthy

Add option to run command to query attachment mime type. (closes #2933) (closes #3959)

Add $mime_type_query_command to specify a command to run if the
attachment extension is not in the mime.types file.

Add $mime_type_query_first to allow the query command to be run before
the mime.types lookup.
parent dc4c148e
......@@ -6748,9 +6748,11 @@ to use.
<para>
If Mutt can not determine the MIME type by the extension of the file you
attach, it will look at the file. If the file is free of binary
information, Mutt will assume that the file is plain text, and mark it
as <literal>text/plain</literal>. If the file contains binary
attach, it will run the command specified in
<link linkend="mime-type-query-command">$mime_type_query_command</link>.
If that command is not specified, Mutt will look at the file. If the file
is free of binary information, Mutt will assume that the file is plain text,
and mark it as <literal>text/plain</literal>. If the file contains binary
information, then Mutt will mark it as
<literal>application/octet-stream</literal>. You can change the MIME
type that Mutt assigns to an attachment by using the
......
......@@ -84,6 +84,7 @@ WHERE char *MarkMacroPrefix;
WHERE char *MhFlagged;
WHERE char *MhReplied;
WHERE char *MhUnseen;
WHERE char *MimeTypeQueryCmd;
WHERE char *MsgFmt;
#ifdef USE_SOCKET
......
......@@ -1720,6 +1720,32 @@ struct option_t MuttVars[] = {
** menu, attachments which cannot be decoded in a reasonable manner will
** be attached to the newly composed message if this option is \fIset\fP.
*/
{ "mime_type_query_command", DT_STR, R_NONE, UL &MimeTypeQueryCmd, UL "" },
/*
** .pp
** This specifies a command to run, to determine the mime type of a
** new attachment when composing a message. Unless
** $$mime_type_query_first is set, this will only be run if the
** attachment's extension is not found in the mime.types file.
** .pp
** The string may contain a ``%s'', which will be substituted with the
** attachment filename. Mutt will add quotes around the string substituted
** for ``%s'' automatically according to shell quoting rules, so you should
** avoid adding your own. If no ``%s'' is found in the string, Mutt will
** append the attachment filename to the end of the string.
** .pp
** The command should output a single line containing the
** attachment's mime type.
** .pp
** Suggested values are ``xdg-mime query filetype'' or
** ``file -bi''.
*/
{ "mime_type_query_first", DT_BOOL, R_NONE, OPTMIMETYPEQUERYFIRST, 0 },
/*
** .pp
** When \fIset\fP, the $$mime_type_query_command will be run before the
** mime.types lookup.
*/
#ifdef MIXMASTER
{ "mix_entry_format", DT_STR, R_NONE, UL &MixEntryFormat, UL "%4n %c %-16s %a" },
/*
......
......@@ -423,6 +423,7 @@ enum
OPTMETOO,
OPTMHPURGE,
OPTMIMEFORWDECODE,
OPTMIMETYPEQUERYFIRST,
OPTNARROWTREE,
OPTPAGERSTOP,
OPTPIPEDECODE,
......
......@@ -1356,6 +1356,35 @@ BODY *mutt_make_message_attach (CONTEXT *ctx, HEADER *hdr, int attach_msg)
return (body);
}
static void run_mime_type_query (BODY *att)
{
FILE *fp, *fperr;
char cmd[HUGE_STRING];
char *buf = NULL;
size_t buflen;
int dummy = 0;
pid_t thepid;
mutt_expand_file_fmt (cmd, sizeof(cmd), MimeTypeQueryCmd, att->filename);
if ((thepid = mutt_create_filter (cmd, NULL, &fp, &fperr)) < 0)
{
mutt_error (_("Error running \"%s\"!"), cmd);
return;
}
if ((buf = mutt_read_line (buf, &buflen, fp, &dummy, 0)) != NULL)
{
if (strchr(buf, '/'))
mutt_parse_content_type (buf, att);
FREE (&buf);
}
safe_fclose (&fp);
safe_fclose (&fperr);
mutt_wait_filter (thepid);
}
BODY *mutt_make_file_attach (const char *path)
{
BODY *att;
......@@ -1364,10 +1393,20 @@ BODY *mutt_make_file_attach (const char *path)
att = mutt_new_body ();
att->filename = safe_strdup (path);
if (MimeTypeQueryCmd && *MimeTypeQueryCmd &&
option (OPTMIMETYPEQUERYFIRST))
run_mime_type_query (att);
/* Attempt to determine the appropriate content-type based on the filename
* suffix.
*/
mutt_lookup_mime_type (att, path);
if (!att->subtype)
mutt_lookup_mime_type (att, path);
if (!att->subtype &&
MimeTypeQueryCmd && *MimeTypeQueryCmd &&
!option (OPTMIMETYPEQUERYFIRST))
run_mime_type_query (att);
if ((info = mutt_get_content_info (path, att)) == NULL)
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment