]> granicus.if.org Git - mutt/commitdiff
Add option to run command to query attachment mime type. (closes #2933) (closes ...
authorKevin McCarthy <kevin@8t8.us>
Sat, 19 Aug 2017 15:33:57 +0000 (08:33 -0700)
committerKevin McCarthy <kevin@8t8.us>
Sat, 19 Aug 2017 15:33:57 +0000 (08:33 -0700)
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.

doc/manual.xml.head
globals.h
init.h
mutt.h
sendlib.c

index 1767f980828fc6be7e95d12c58e428f13cfcbcd1..b4975d97097a80326606317f37775cd87fdfed09 100644 (file)
@@ -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
index 5654f28cefa1af2eed91b9bc2a341522ddcf9df9..9634691a48dcf986e701199238b8a35e27c9593a 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -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
diff --git a/init.h b/init.h
index d7bb5e7fc45034b8c578655bb45221dc25c02e37..035752f61667b6d2b2d903451f4e1b929c9691fd 100644 (file)
--- a/init.h
+++ b/init.h
@@ -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" },
   /*
diff --git a/mutt.h b/mutt.h
index 465f70557228704aa5f03b5dfec4ce09eeab92c3..54a807fab03bb9731451472853c15f8c1487d6ad 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -423,6 +423,7 @@ enum
   OPTMETOO,
   OPTMHPURGE,
   OPTMIMEFORWDECODE,
+  OPTMIMETYPEQUERYFIRST,
   OPTNARROWTREE,
   OPTPAGERSTOP,
   OPTPIPEDECODE,
index 75d270b2754fae36c96c6d0be81aefd257cb5ff3..d09b8ce068585c682321fd926a91fdc88170c694 100644 (file)
--- a/sendlib.c
+++ b/sendlib.c
@@ -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)
   {