From: Ken Hornstein Date: Fri, 24 Jan 2014 17:38:03 +0000 (-0500) Subject: In my over-eager trimming I didn't realize the fallback code that X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/1828086b128c40e32a58354356e5ef3e4a3d2a0b?ds=sidebyside;hp=-c In my over-eager trimming I didn't realize the fallback code that used the mhshow-suffix configuration information to determine the MIME type was removed; put that back into mime_type(). --- 1828086b128c40e32a58354356e5ef3e4a3d2a0b diff --git a/uip/attach.c b/uip/attach.c index 345c72a7..540ddd93 100644 --- a/uip/attach.c +++ b/uip/attach.c @@ -21,6 +21,8 @@ static char *get_file_info(const char *, const char *); char * mime_type(const char *file_name) { char *content_type = NULL; /* mime content type */ + char *p; + static int loaded_defaults = 0; #ifdef MIMETYPEPROC char *mimetype; @@ -44,10 +46,63 @@ mime_type(const char *file_name) { content_type = strdup(mimetype); #endif /* MIMEENCODINGPROC */ } -#else /* MIMETYPEPROC */ - NMH_UNUSED(file_name); #endif /* MIMETYPEPROC */ + /* + * If we didn't get the MIME type from the contents (or we don't support + * the necessary command) then use the mhshow suffix. + */ + + if (content_type == NULL) { + struct node *np; /* Content scan node pointer */ + FILE *fp; /* File pointer for mhn.defaults */ + + if (! loaded_defaults && + (fp = fopen(p = etcpath("mhn.defaults"), "r"))) { + loaded_defaults = 1; + readconfig(NULL, fp, p, 0); + fclose(fp); + } + + if ((p = strrchr(file_name, '.')) != NULL) { + for (np = m_defs; np; np = np->n_next) { + if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0 && + strcasecmp(p, np->n_field ? np->n_field : "") == 0) { + content_type = strdup(np->n_name + 14); + break; + } + } + } + + /* + * If we didn't match any filename extension, try to infer the + * content type. If we have binary, assume application/octet-stream; + * otherwise, assume text/plain. + */ + + if (content_type == NULL) { + FILE *fp; + int binary = 0, c; + + if (!(fp = fopen(file_name, "r"))) { + advise (NULL, "unable to access file \"%s\"", file_name); + return NULL; + } + + while ((c = getc(fp)) != EOF) { + if (! isascii(c)) { + binary = 1; + break; + } + } + + fclose(fp); + + content_type = + strdup(binary ? "application/octet-stream" : "text/plain"); + } + } + return content_type; }