]> diplodocus.org Git - nmh/blob - uip/attach.c
540ddd9321bbe4f8ccc4060f1efb0049bfeee7c4
[nmh] / uip / attach.c
1 /*
2 * attach.c -- routines to help attach files via whatnow
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <h/tws.h>
12
13 #ifdef MIMETYPEPROC
14 static char *get_file_info(const char *, const char *);
15 #endif /* MIMETYPEPROC */
16
17 /*
18 * Try to use external command to determine mime type, and possibly
19 * encoding. Caller is responsible for free'ing returned memory.
20 */
21 char *
22 mime_type(const char *file_name) {
23 char *content_type = NULL; /* mime content type */
24 char *p;
25 static int loaded_defaults = 0;
26
27 #ifdef MIMETYPEPROC
28 char *mimetype;
29
30 if ((mimetype = get_file_info(MIMETYPEPROC, file_name))) {
31 #ifdef MIMEENCODINGPROC
32 /* Try to append charset for text content. */
33 char *mimeencoding;
34
35 if (strncasecmp(mimetype, "text", 4) == 0) {
36 if ((mimeencoding = get_file_info(MIMEENCODINGPROC, file_name))) {
37 content_type = concat(mimetype, "; charset=", mimeencoding,
38 NULL);
39 } else {
40 content_type = strdup(mimetype);
41 }
42 } else {
43 content_type = strdup(mimetype);
44 }
45 #else /* MIMEENCODINGPROC */
46 content_type = strdup(mimetype);
47 #endif /* MIMEENCODINGPROC */
48 }
49 #endif /* MIMETYPEPROC */
50
51 /*
52 * If we didn't get the MIME type from the contents (or we don't support
53 * the necessary command) then use the mhshow suffix.
54 */
55
56 if (content_type == NULL) {
57 struct node *np; /* Content scan node pointer */
58 FILE *fp; /* File pointer for mhn.defaults */
59
60 if (! loaded_defaults &&
61 (fp = fopen(p = etcpath("mhn.defaults"), "r"))) {
62 loaded_defaults = 1;
63 readconfig(NULL, fp, p, 0);
64 fclose(fp);
65 }
66
67 if ((p = strrchr(file_name, '.')) != NULL) {
68 for (np = m_defs; np; np = np->n_next) {
69 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0 &&
70 strcasecmp(p, np->n_field ? np->n_field : "") == 0) {
71 content_type = strdup(np->n_name + 14);
72 break;
73 }
74 }
75 }
76
77 /*
78 * If we didn't match any filename extension, try to infer the
79 * content type. If we have binary, assume application/octet-stream;
80 * otherwise, assume text/plain.
81 */
82
83 if (content_type == NULL) {
84 FILE *fp;
85 int binary = 0, c;
86
87 if (!(fp = fopen(file_name, "r"))) {
88 advise (NULL, "unable to access file \"%s\"", file_name);
89 return NULL;
90 }
91
92 while ((c = getc(fp)) != EOF) {
93 if (! isascii(c)) {
94 binary = 1;
95 break;
96 }
97 }
98
99 fclose(fp);
100
101 content_type =
102 strdup(binary ? "application/octet-stream" : "text/plain");
103 }
104 }
105
106 return content_type;
107 }
108
109
110 #ifdef MIMETYPEPROC
111 /*
112 * Get information using proc about a file.
113 */
114 static char *
115 get_file_info(const char *proc, const char *file_name) {
116 char *cmd, *cp;
117 char *quotec = "'";
118
119 if ((cp = strchr(file_name, '\''))) {
120 /* file_name contains a single quote. */
121 if (strchr(file_name, '"')) {
122 advise(NULL, "filenames containing both single and double quotes "
123 "are unsupported for attachment");
124 return NULL;
125 } else {
126 quotec = "\"";
127 }
128 }
129
130 cmd = concat(proc, " ", quotec, file_name, quotec, NULL);
131 if ((cmd = concat(proc, " ", quotec, file_name, quotec, NULL))) {
132 FILE *fp;
133
134 if ((fp = popen(cmd, "r")) != NULL) {
135 char buf[BUFSIZ >= 2048 ? BUFSIZ : 2048];
136
137 buf[0] = '\0';
138 if (fgets(buf, sizeof buf, fp)) {
139 char *eol;
140
141 /* Skip leading <filename>:<whitespace>, if present. */
142 if ((cp = strchr(buf, ':')) != NULL) {
143 ++cp;
144 while (*cp && isblank((unsigned char) *cp)) {
145 ++cp;
146 }
147 } else {
148 cp = buf;
149 }
150
151 /* Truncate at newline (LF or CR), if present. */
152 if ((eol = strpbrk(cp, "\n\r")) != NULL) {
153 *eol = '\0';
154 }
155 } else if (buf[0] == '\0') {
156 /* This can happen on Cygwin if the popen()
157 mysteriously fails. Return NULL so that the caller
158 will use another method to determine the info. */
159 free (cp);
160 cp = NULL;
161 }
162
163 (void) pclose(fp);
164 } else {
165 advise(NULL, "no output from %s", cmd);
166 }
167
168 free(cmd);
169 } else {
170 advise(NULL, "concat with \"%s\" failed, out of memory?", proc);
171 }
172
173 return cp ? strdup(cp) : NULL;
174 }
175 #endif /* MIMETYPEPROC */