Drop `features', change `version', adjust make system..
[s-mailx.git] / mime_types.c
blob4cd71ce421b06f287642fdb82f4043256334a2f1
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ `(un)?mimetype' and other mime.types(5) related facilities.
3 *@ "Keep in sync with" ./mime.types.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #undef n_FILE
20 #define n_FILE mime_types
22 #ifndef HAVE_AMALGAMATION
23 # include "nail.h"
24 #endif
26 enum mime_type {
27 _MT_APPLICATION,
28 _MT_AUDIO,
29 _MT_IMAGE,
30 _MT_MESSAGE,
31 _MT_MULTIPART,
32 _MT_TEXT,
33 _MT_VIDEO,
34 _MT_OTHER,
35 __MT_TMIN = 0,
36 __MT_TMAX = _MT_OTHER,
37 __MT_TMASK = 0x07,
39 _MT_LOADED = 1<< 8, /* Not struct mtbltin */
40 _MT_USR = 1<< 9, /* MIME_TYPES_USR */
41 _MT_SYS = 1<<10, /* MIME_TYPES_SYS */
43 _MT_PLAIN = 1<<16, /* Without pipe handler display as text */
44 _MT_SOUP_h = 2<<16, /* Ditto, but HTML tagsoup parser if possible */
45 _MT_SOUP_H = 3<<16, /* HTML tagsoup parser, else NOT plain text */
46 __MT_MARKMASK = _MT_SOUP_H
49 enum mime_type_class {
50 _MT_C_CLEAN = 0, /* Plain RFC 5322 message */
51 _MT_C_NCTT = 1<<0, /* *contenttype == NULL */
52 _MT_C_ISTXT = 1<<1, /* *contenttype =~ text\/ */
53 _MT_C_ISTXTCOK = 1<<2, /* _ISTXT + *mime-allow-text-controls* */
54 _MT_C_HIGHBIT = 1<<3, /* Not 7bit clean */
55 _MT_C_LONGLINES = 1<<4, /* MIME_LINELEN_LIMIT exceed. */
56 _MT_C_CTRLCHAR = 1<<5, /* Control characters seen */
57 _MT_C_HASNUL = 1<<6, /* Contains \0 characters */
58 _MT_C_NOTERMNL = 1<<7, /* Lacks a final newline */
59 _MT_C_FROM_ = 1<<8, /* ^From_ seen */
60 _MT_C_SUGGEST_DONE = 1<<16 /* Inspector suggests to stop further parse */
63 struct mtbltin {
64 ui32_t mtb_flags;
65 ui32_t mtb_mtlen;
66 char const *mtb_line;
69 struct mtnode {
70 struct mtnode *mt_next;
71 ui32_t mt_flags;
72 ui32_t mt_mtlen; /* Length of MIME type string, rest thereafter */
73 /* C99 forbids flexible arrays in union, so unfortunately we waste a pointer
74 * that could already store character data here */
75 char const *mt_line;
78 struct mtlookup {
79 char const *mtl_name;
80 size_t mtl_nlen;
81 struct mtnode const *mtl_node;
82 char *mtl_result; /* If requested, salloc()ed MIME type */
85 struct mt_class_arg {
86 char const *mtca_buf;
87 size_t mtca_len;
88 ssize_t mtca_curlen;
89 char mtca_lastc;
90 char mtca_c;
91 enum mime_type_class mtca_mtc;
94 static struct mtbltin const _mt_bltin[] = {
95 #include "mime_types.h"
98 static char const _mt_typnames[][16] = {
99 "application/", "audio/", "image/",
100 "message/", "multipart/", "text/",
101 "video/"
103 CTA(_MT_APPLICATION == 0 && _MT_AUDIO == 1 && _MT_IMAGE == 2 &&
104 _MT_MESSAGE == 3 && _MT_MULTIPART == 4 && _MT_TEXT == 5 &&
105 _MT_VIDEO == 6);
107 /* */
108 static bool_t _mt_is_init;
109 static struct mtnode *_mt_list;
111 /* Initialize MIME type list in order */
112 static void _mt_init(void);
113 static bool_t __mt_load_file(ui32_t orflags,
114 char const *file, char **line, size_t *linesize);
116 /* Create (prepend) a new MIME type; cmdcalled results in a bit more verbosity
117 * for `mimetype' */
118 static struct mtnode * _mt_create(bool_t cmdcalled, ui32_t orflags,
119 char const *line, size_t len);
121 /* Try to find MIME type by X (after zeroing mtlp), return NULL if not found;
122 * if with_result >mtl_result will be created upon success for the former */
123 static struct mtlookup * _mt_by_filename(struct mtlookup *mtlp,
124 char const *name, bool_t with_result);
125 static struct mtlookup * _mt_by_mtname(struct mtlookup *mtlp,
126 char const *mtname);
128 /* In-depth inspection of raw content: call _round() repeatedly, last time with
129 * a 0 length buffer, finally check .mtca_mtc for result.
130 * No further call is needed if _round() return includes _MT_C_SUGGEST_DONE,
131 * as the resulting classification is unambiguous */
132 SINLINE struct mt_class_arg * _mt_classify_init(struct mt_class_arg *mtcap,
133 enum mime_type_class initval);
134 static enum mime_type_class _mt_classify_round(struct mt_class_arg *mtcap);
136 /* We need an in-depth inspection of an application/octet-stream part */
137 static enum mimecontent _mt_classify_os_part(ui32_t mce, struct mimepart *mpp);
139 /* Check whether a *pipe-XY* handler is applicable, and adjust flags according
140 * to the defined trigger characters; upon entry MIME_HDL_NULL is set, and that
141 * isn't changed if mhp doesn't apply */
142 static enum mime_handler_flags _mt_pipe_check(struct mime_handler *mhp);
144 static void
145 _mt_init(void)
147 struct mtnode *tail;
148 char c, *line; /* TODO line pool (below) */
149 size_t linesize;
150 ui32_t i, j;
151 char const *srcs_arr[10], *ccp, **srcs;
152 NYD_ENTER;
154 /*if (_mt_is_init)
155 * goto jleave;*/
157 /* Always load our builtins */
158 for (tail = NULL, i = 0; i < NELEM(_mt_bltin); ++i) {
159 struct mtbltin const *mtbp = _mt_bltin + i;
160 struct mtnode *mtnp = smalloc(sizeof *mtnp);
162 if (tail != NULL)
163 tail->mt_next = mtnp;
164 else
165 _mt_list = mtnp;
166 tail = mtnp;
167 mtnp->mt_next = NULL;
168 mtnp->mt_flags = mtbp->mtb_flags;
169 mtnp->mt_mtlen = mtbp->mtb_mtlen;
170 mtnp->mt_line = mtbp->mtb_line;
173 /* Decide which files sources have to be loaded */
174 if ((ccp = ok_vlook(mimetypes_load_control)) == NULL)
175 ccp = "US";
176 else if (*ccp == '\0')
177 goto jleave;
179 srcs = srcs_arr + 2;
180 srcs[-1] = srcs[-2] = NULL;
182 if (strchr(ccp, '=') != NULL) {
183 line = savestr(ccp);
185 while ((ccp = n_strsep(&line, ',', TRU1)) != NULL) {
186 switch ((c = *ccp)) {
187 case 'S': case 's':
188 srcs_arr[1] = MIME_TYPES_SYS;
189 if (0) {
190 /* FALLTHRU */
191 case 'U': case 'u':
192 srcs_arr[0] = MIME_TYPES_USR;
194 if (ccp[1] != '\0')
195 goto jecontent;
196 break;
197 case 'F': case 'f':
198 if (*++ccp == '=' && *++ccp != '\0') {
199 if (PTR2SIZE(srcs - srcs_arr) < NELEM(srcs_arr))
200 *srcs++ = ccp;
201 else
202 n_err(_("*mimetypes-load-control*: too many sources, "
203 "skipping %s\n"), n_shell_quote_cp(ccp, FAL0));
204 continue;
206 /* FALLTHRU */
207 default:
208 goto jecontent;
211 } else for (i = 0; (c = ccp[i]) != '\0'; ++i)
212 switch (c) {
213 case 'S': case 's': srcs_arr[1] = MIME_TYPES_SYS; break;
214 case 'U': case 'u': srcs_arr[0] = MIME_TYPES_USR; break;
215 default:
216 jecontent:
217 n_err(_("*mimetypes-load-control*: unsupported content: %s\n"), ccp);
218 goto jleave;
221 /* Load all file-based sources in the desired order */
222 line = NULL;
223 linesize = 0;
224 for (j = 0, i = (ui32_t)PTR2SIZE(srcs - srcs_arr), srcs = srcs_arr;
225 i > 0; ++j, ++srcs, --i)
226 if (*srcs == NULL)
227 continue;
228 else if (!__mt_load_file((j == 0 ? _MT_USR : (j == 1 ? _MT_SYS : 0)),
229 *srcs, &line, &linesize)) {
230 if ((options & OPT_D_V) || j > 1)
231 n_err(_("*mimetypes-load-control*: can't open or load %s\n"),
232 n_shell_quote_cp(*srcs, FAL0));
234 if (line != NULL)
235 free(line);
236 jleave:
237 _mt_is_init = TRU1;
238 NYD_LEAVE;
241 static bool_t
242 __mt_load_file(ui32_t orflags, char const *file, char **line, size_t *linesize)
244 char const *cp;
245 FILE *fp;
246 struct mtnode *head, *tail, *mtnp;
247 size_t len;
248 NYD_ENTER;
250 if ((cp = file_expand(file)) == NULL || (fp = Fopen(cp, "r")) == NULL) {
251 cp = NULL;
252 goto jleave;
255 for (head = tail = NULL; fgetline(line, linesize, NULL, &len, fp, 0) != 0;)
256 if ((mtnp = _mt_create(FAL0, orflags, *line, len)) != NULL) {
257 if (head == NULL)
258 head = tail = mtnp;
259 else
260 tail->mt_next = mtnp;
261 tail = mtnp;
263 if (head != NULL) {
264 tail->mt_next = _mt_list;
265 _mt_list = head;
268 Fclose(fp);
269 jleave:
270 NYD_LEAVE;
271 return (cp != NULL);
274 static struct mtnode *
275 _mt_create(bool_t cmdcalled, ui32_t orflags, char const *line, size_t len)
277 struct mtnode *mtnp = NULL;
278 char const *typ, *subtyp;
279 size_t tlen, i;
280 NYD_ENTER;
282 /* Drop anything after a comment first */
283 if ((typ = memchr(line, '#', len)) != NULL)
284 len = PTR2SIZE(typ - line);
286 /* Then trim any trailing whitespace from line (including NL/CR) */
287 while (len > 0 && spacechar(line[len - 1]))
288 --len;
290 /* Isolate MIME type, trim any whitespace from it */
291 while (len > 0 && blankchar(*line))
292 ++line, --len;
293 typ = line;
295 /* (But wait - is there a type marker?) */
296 if (!(orflags & (_MT_USR | _MT_SYS)) && *typ == '@') {
297 if (len < 2)
298 goto jeinval;
299 if (typ[1] == ' ') {
300 orflags |= _MT_PLAIN;
301 typ += 2;
302 len -= 2;
303 line += 2;
304 } else if (len > 4 && typ[2] == '@' && typ[3] == ' ') {
305 switch (typ[1]) {
306 case 't': orflags |= _MT_PLAIN; goto jexttypmar;
307 case 'h': orflags |= _MT_SOUP_h; goto jexttypmar;
308 case 'H': orflags |= _MT_SOUP_H;
309 jexttypmar:
310 typ += 4;
311 len -= 4;
312 line += 4;
313 break;
314 default:
315 goto jeinval;
317 } else
318 goto jeinval;
321 while (len > 0 && !blankchar(*line))
322 ++line, --len;
323 /* Ignore empty lines and even incomplete specifications (only MIME type)
324 * because this is quite common in mime.types(5) files */
325 if (len == 0 || (tlen = PTR2SIZE(line - typ)) == 0) {
326 if (cmdcalled)
327 n_err(_("Empty MIME type or no extensions given: %s\n"),
328 (len == 0 ? _("(no value)") : line));
329 goto jleave;
332 if ((subtyp = memchr(typ, '/', tlen)) == NULL) {
333 jeinval:
334 if (cmdcalled || (options & OPT_D_V))
335 n_err(_("%s MIME type: %s\n"),
336 (cmdcalled ? _("Invalid") : _("mime.types(5): invalid")), typ);
337 goto jleave;
339 ++subtyp;
341 /* Map to mime_type */
342 tlen = PTR2SIZE(subtyp - typ);
343 for (i = __MT_TMIN;;) {
344 if (!ascncasecmp(_mt_typnames[i], typ, tlen)) {
345 orflags |= i;
346 tlen = PTR2SIZE(line - subtyp);
347 typ = subtyp;
348 break;
350 if (++i == __MT_TMAX) {
351 orflags |= _MT_OTHER;
352 tlen = PTR2SIZE(line - typ);
353 break;
357 /* Strip leading whitespace from the list of extensions;
358 * trailing WS has already been trimmed away above.
359 * Be silent on slots which define a mimetype without any value */
360 while (len > 0 && blankchar(*line))
361 ++line, --len;
362 if (len == 0)
363 goto jleave;
365 /* */
366 mtnp = smalloc(sizeof(*mtnp) + tlen + len +1);
367 mtnp->mt_next = NULL;
368 mtnp->mt_flags = (orflags |= _MT_LOADED);
369 mtnp->mt_mtlen = (ui32_t)tlen;
370 { char *l = (char*)(mtnp + 1);
371 mtnp->mt_line = l;
372 memcpy(l, typ, tlen);
373 memcpy(l + tlen, line, len);
374 tlen += len;
375 l[tlen] = '\0';
378 jleave:
379 NYD_LEAVE;
380 return mtnp;
383 static struct mtlookup *
384 _mt_by_filename(struct mtlookup *mtlp, char const *name, bool_t with_result)
386 struct mtnode *mtnp;
387 size_t nlen, i, j;
388 char const *ext, *cp;
389 NYD2_ENTER;
391 memset(mtlp, 0, sizeof *mtlp);
393 if ((mtlp->mtl_nlen = nlen = strlen(mtlp->mtl_name = name)) == 0 ||
394 memchr(name, '.', nlen) == NULL)
395 goto jnull_leave;
397 if (!_mt_is_init)
398 _mt_init();
400 /* ..all the MIME types */
401 for (mtnp = _mt_list; mtnp != NULL; mtnp = mtnp->mt_next)
402 for (ext = mtnp->mt_line + mtnp->mt_mtlen;; ext = cp) {
403 cp = ext;
404 while (whitechar(*cp))
405 ++cp;
406 ext = cp;
407 while (!whitechar(*cp) && *cp != '\0')
408 ++cp;
410 if ((i = PTR2SIZE(cp - ext)) == 0)
411 break;
412 /* Don't allow neither of ".txt" or "txt" to match "txt" */
413 else if (i + 1 >= nlen || name[(j = nlen - i) - 1] != '.' ||
414 ascncasecmp(name + j, ext, i))
415 continue;
417 /* Found it */
418 mtlp->mtl_node = mtnp;
420 if (!with_result)
421 goto jleave;
423 if ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER) {
424 name = "";
425 j = 0;
426 } else {
427 name = _mt_typnames[mtnp->mt_flags & __MT_TMASK];
428 j = strlen(name);
430 i = mtnp->mt_mtlen;
431 mtlp->mtl_result = salloc(i + j +1);
432 if (j > 0)
433 memcpy(mtlp->mtl_result, name, j);
434 memcpy(mtlp->mtl_result + j, mtnp->mt_line, i);
435 mtlp->mtl_result[j += i] = '\0';
436 goto jleave;
438 jnull_leave:
439 mtlp = NULL;
440 jleave:
441 NYD2_LEAVE;
442 return mtlp;
445 static struct mtlookup *
446 _mt_by_mtname(struct mtlookup *mtlp, char const *mtname)
448 struct mtnode *mtnp;
449 size_t nlen, i, j;
450 char const *cp;
451 NYD2_ENTER;
453 memset(mtlp, 0, sizeof *mtlp);
455 if ((mtlp->mtl_nlen = nlen = strlen(mtlp->mtl_name = mtname)) == 0)
456 goto jnull_leave;
458 if (!_mt_is_init)
459 _mt_init();
461 /* ..all the MIME types */
462 for (mtnp = _mt_list; mtnp != NULL; mtnp = mtnp->mt_next) {
463 if ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER) {
464 cp = "";
465 j = 0;
466 } else {
467 cp = _mt_typnames[mtnp->mt_flags & __MT_TMASK];
468 j = strlen(cp);
470 i = mtnp->mt_mtlen;
472 if (i + j == mtlp->mtl_nlen) {
473 char *xmt = ac_alloc(i + j +1);
474 if (j > 0)
475 memcpy(xmt, cp, j);
476 memcpy(xmt + j, mtnp->mt_line, i);
477 xmt[j += i] = '\0';
478 i = asccasecmp(mtname, xmt);
479 ac_free(xmt);
481 if (!i) {
482 /* Found it */
483 mtlp->mtl_node = mtnp;
484 goto jleave;
488 jnull_leave:
489 mtlp = NULL;
490 jleave:
491 NYD2_LEAVE;
492 return mtlp;
495 SINLINE struct mt_class_arg *
496 _mt_classify_init(struct mt_class_arg * mtcap, enum mime_type_class initval)
498 NYD2_ENTER;
499 memset(mtcap, 0, sizeof *mtcap);
500 mtcap->mtca_lastc = mtcap->mtca_c = EOF;
501 mtcap->mtca_mtc = initval;
502 NYD2_LEAVE;
503 return mtcap;
506 static enum mime_type_class
507 _mt_classify_round(struct mt_class_arg *mtcap)
509 /* TODO BTW., after the MIME/send layer rewrite we could use a MIME
510 * TODO boundary of "=-=-=" if we would add a B_ in EQ spirit to F_,
511 * TODO and report that state to the outer world */
512 #define F_ "From "
513 #define F_SIZEOF (sizeof(F_) -1)
514 char f_buf[F_SIZEOF], *f_p = f_buf;
515 char const *buf;
516 size_t blen;
517 ssize_t curlen;
518 int c, lastc;
519 enum mime_type_class mtc;
520 NYD2_ENTER;
522 buf = mtcap->mtca_buf;
523 blen = mtcap->mtca_len;
524 curlen = mtcap->mtca_curlen;
525 c = mtcap->mtca_c;
526 lastc = mtcap->mtca_lastc;
527 mtc = mtcap->mtca_mtc;
529 for (;; ++curlen) {
530 lastc = c;
531 if (blen == 0) {
532 /* Real EOF, or only current buffer end? */
533 if (mtcap->mtca_len == 0)
534 c = EOF;
535 else
536 break;
537 } else
538 c = (uc_i)*buf++;
539 --blen;
541 if (c == '\0') {
542 mtc |= _MT_C_HASNUL;
543 if (!(mtc & _MT_C_ISTXTCOK)) {
544 mtc |= _MT_C_SUGGEST_DONE;
545 break;
547 continue;
549 if (c == '\n' || c == EOF) {
550 if (curlen >= MIME_LINELEN_LIMIT)
551 mtc |= _MT_C_LONGLINES;
552 if (c == EOF) {
553 break;
555 f_p = f_buf;
556 curlen = -1;
557 continue;
559 /* A bit hairy is handling of \r=\x0D=CR.
560 * RFC 2045, 6.7:
561 * Control characters other than TAB, or CR and LF as parts of CRLF
562 * pairs, must not appear. \r alone does not force _CTRLCHAR below since
563 * we cannot peek the next character. Thus right here, inspect the last
564 * seen character for if its \r and set _CTRLCHAR in a delayed fashion */
565 /*else*/ if (lastc == '\r')
566 mtc |= _MT_C_CTRLCHAR;
568 /* Control character? XXX this is all ASCII here */
569 if (c < 0x20 || c == 0x7F) {
570 /* RFC 2045, 6.7, as above ... */
571 if (c != '\t' && c != '\r')
572 mtc |= _MT_C_CTRLCHAR;
573 /* If there is a escape sequence in backslash notation defined for
574 * this in ANSI X3.159-1989 (ANSI C89), don't treat it as a control
575 * for real. I.e., \a=\x07=BEL, \b=\x08=BS, \t=\x09=HT. Don't follow
576 * libmagic(1) in respect to \v=\x0B=VT. \f=\x0C=NP; do ignore
577 * \e=\x1B=ESC */
578 if ((c >= '\x07' && c <= '\x0D') || c == '\x1B')
579 continue;
580 mtc |= _MT_C_HASNUL; /* Force base64 */
581 if (!(mtc & _MT_C_ISTXTCOK)) {
582 mtc |= _MT_C_SUGGEST_DONE;
583 break;
585 } else if ((ui8_t)c & 0x80) {
586 mtc |= _MT_C_HIGHBIT;
587 /* TODO count chars with HIGHBIT? libmagic?
588 * TODO try encode part - base64 if bails? */
589 if (!(mtc & (_MT_C_NCTT | _MT_C_ISTXT))) { /* TODO _NCTT?? */
590 mtc |= _MT_C_HASNUL /* Force base64 */ | _MT_C_SUGGEST_DONE;
591 break;
593 } else if (!(mtc & _MT_C_FROM_) && UICMP(z, curlen, <, F_SIZEOF)) {
594 *f_p++ = (char)c;
595 if (UICMP(z, curlen, ==, F_SIZEOF - 1) &&
596 PTR2SIZE(f_p - f_buf) == F_SIZEOF &&
597 !memcmp(f_buf, F_, F_SIZEOF))
598 mtc |= _MT_C_FROM_;
601 if (c == EOF && lastc != '\n')
602 mtc |= _MT_C_NOTERMNL;
604 mtcap->mtca_curlen = curlen;
605 mtcap->mtca_lastc = lastc;
606 mtcap->mtca_c = c;
607 mtcap->mtca_mtc = mtc;
608 NYD2_LEAVE;
609 return mtc;
610 #undef F_
611 #undef F_SIZEOF
614 static enum mimecontent
615 _mt_classify_os_part(ui32_t mce, struct mimepart *mpp)
617 struct str in = {NULL, 0}, rest = {NULL, 0}, dec = {NULL, 0};
618 struct mt_class_arg mtca;
619 enum mime_type_class mtc;
620 int lc, c;
621 size_t cnt, lsz;
622 FILE *ibuf;
623 off_t start_off;
624 enum mimecontent mc;
625 NYD2_ENTER;
627 assert(mpp->m_mime_enc != MIMEE_BIN);
629 mc = MIME_UNKNOWN;
630 UNINIT(mtc, 0);
632 /* TODO v15-compat Note we actually bypass our usual file handling by
633 * TODO directly using fseek() on mb.mb_itf -- the v15 rewrite will change
634 * TODO all of this, and until then doing it like this is the only option
635 * TODO to integrate nicely into whoever calls us */
636 start_off = ftell(mb.mb_itf);
637 if ((ibuf = setinput(&mb, (struct message*)mpp, NEED_BODY)) == NULL) {
638 jos_leave:
639 fseek(mb.mb_itf, start_off, SEEK_SET);
640 goto jleave;
642 cnt = mpp->m_size;
644 /* Skip part headers */
645 for (lc = '\0'; cnt > 0; lc = c, --cnt)
646 if ((c = getc(ibuf)) == EOF || (c == '\n' && lc == '\n'))
647 break;
648 if (cnt == 0 || ferror(ibuf))
649 goto jos_leave;
651 /* So now let's inspect the part content, decoding content-transfer-encoding
652 * along the way TODO this should simply be "mime_factory_create(MPP)"! */
653 _mt_classify_init(&mtca, _MT_C_ISTXT);
655 for (lsz = 0;;) {
656 bool_t dobuf;
658 c = (--cnt == 0) ? EOF : getc(ibuf);
659 if ((dobuf = (c == '\n'))) {
660 /* Ignore empty lines */
661 if (lsz == 0)
662 continue;
663 } else if ((dobuf = (c == EOF))) {
664 if (lsz == 0 && rest.l == 0)
665 break;
668 if (in.l + 1 >= lsz)
669 in.s = srealloc(in.s, lsz += LINESIZE);
670 if (c != EOF)
671 in.s[in.l++] = (char)c;
672 if (!dobuf)
673 continue;
675 jdobuf:
676 switch (mpp->m_mime_enc) {
677 case MIMEE_B64:
678 if (b64_decode(&dec, &in, &rest) == STOP) {
679 mtca.mtca_mtc = _MT_C_HASNUL;
680 goto jstopit; /* break;break; */
682 break;
683 case MIMEE_QP:
684 /* Drin */
685 if (qp_decode(&dec, &in, &rest) == STOP) {
686 mtca.mtca_mtc = _MT_C_HASNUL;
687 goto jstopit; /* break;break; */
689 if (dec.l == 0 && c != EOF) {
690 in.l = 0;
691 continue;
693 break;
694 default:
695 /* Temporarily switch those two buffers.. */
696 dec = in;
697 in.s = NULL;
698 in.l = 0;
699 break;
702 mtca.mtca_buf = dec.s;
703 mtca.mtca_len = (ssize_t)dec.l;
704 if ((mtc = _mt_classify_round(&mtca)) & _MT_C_SUGGEST_DONE) {
705 mtc = _MT_C_HASNUL;
706 break;
709 if (c == EOF)
710 break;
711 /* ..and restore switched */
712 if (in.s == NULL) {
713 in = dec;
714 dec.s = NULL;
716 in.l = dec.l = 0;
718 if (rest.l > 0) {
719 in.l = 0;
720 goto jdobuf;
722 jstopit:
723 if (in.s != NULL)
724 free(in.s);
725 if (dec.s != NULL)
726 free(dec.s);
727 if (rest.s != NULL)
728 free(rest.s);
730 fseek(mb.mb_itf, start_off, SEEK_SET);
732 if (!(mtc & (_MT_C_HASNUL | _MT_C_CTRLCHAR))) {
733 mc = MIME_TEXT_PLAIN;
734 if (mce & MIMECE_ALL_OVWR)
735 mpp->m_ct_type_plain = "text/plain";
736 if (mce & (MIMECE_BIN_OVWR | MIMECE_ALL_OVWR))
737 mpp->m_ct_type_usr_ovwr = "text/plain";
739 jleave:
740 NYD2_LEAVE;
741 return mc;
744 static enum mime_handler_flags
745 _mt_pipe_check(struct mime_handler *mhp)
747 enum mime_handler_flags rv_orig, rv;
748 char const *cp;
749 NYD2_ENTER;
751 rv_orig = rv = mhp->mh_flags;
753 /* Do we have any handler for this part? */
754 if (*(cp = mhp->mh_shell_cmd) == '\0')
755 goto jleave;
756 else if (*cp++ != '@') {
757 rv |= MIME_HDL_CMD;
758 goto jleave;
759 } else if (*cp == '\0') {
760 rv |= MIME_HDL_TEXT;
761 goto jleave;
764 jnextc:
765 switch (*cp) {
766 case '*': rv |= MIME_HDL_ALWAYS; ++cp; goto jnextc;
767 case '#': rv |= MIME_HDL_NOQUOTE; ++cp; goto jnextc;
768 case '&': rv |= MIME_HDL_ASYNC; ++cp; goto jnextc;
769 case '!': rv |= MIME_HDL_NEEDSTERM; ++cp; goto jnextc;
770 case '+':
771 if (rv & MIME_HDL_TMPF)
772 rv |= MIME_HDL_TMPF_UNLINK;
773 rv |= MIME_HDL_TMPF;
774 ++cp;
775 goto jnextc;
776 case '=':
777 rv |= MIME_HDL_TMPF_FILL;
778 ++cp;
779 goto jnextc;
780 case '@':
781 ++cp;
782 /* FALLTHRU */
783 default:
784 break;
786 mhp->mh_shell_cmd = cp;
788 /* Implications */
789 if (rv & MIME_HDL_TMPF_FILL)
790 rv |= MIME_HDL_TMPF;
792 /* Exceptions */
793 if (rv & MIME_HDL_ISQUOTE) {
794 if (rv & MIME_HDL_NOQUOTE)
795 goto jerr;
797 /* Cannot fetch data back from asynchronous process */
798 if (rv & MIME_HDL_ASYNC)
799 goto jerr;
801 /* TODO Can't use a "needsterminal" program for quoting */
802 if (rv & MIME_HDL_NEEDSTERM)
803 goto jerr;
806 if (rv & MIME_HDL_NEEDSTERM) {
807 if (rv & MIME_HDL_ASYNC) {
808 n_err(_("MIME type handlers: can't use needsterminal and "
809 "x-nail-async together\n"));
810 goto jerr;
813 /* needsterminal needs a terminal */
814 if (!(options & OPT_INTERACTIVE))
815 goto jerr;
818 if (!(rv & MIME_HDL_ALWAYS) && !(pstate & PS_MSGLIST_DIRECT)) {
819 /* Viewing multiple messages in one go, don't block system */
820 mhp->mh_msg.l = strlen(mhp->mh_msg.s = UNCONST(
821 _("[-- Directly address message only for display --]\n")));
822 rv |= MIME_HDL_MSG;
823 goto jleave;
826 rv |= MIME_HDL_CMD;
827 jleave:
828 mhp->mh_flags = rv;
829 NYD2_LEAVE;
830 return rv;
831 jerr:
832 rv = rv_orig;
833 goto jleave;
836 FL int
837 c_mimetype(void *v)
839 char **argv = v;
840 struct mtnode *mtnp;
841 NYD_ENTER;
843 if (!_mt_is_init)
844 _mt_init();
846 if (*argv == NULL) {
847 FILE *fp;
848 size_t l;
850 if (_mt_list == NULL) {
851 printf(_("*mimetypes-load-control*: no mime.types(5) available\n"));
852 goto jleave;
855 if ((fp = Ftmp(NULL, "mimelist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
856 NULL) {
857 n_perr(_("tmpfile"), 0);
858 v = NULL;
859 goto jleave;
862 for (l = 0, mtnp = _mt_list; mtnp != NULL; ++l, mtnp = mtnp->mt_next) {
863 char const *tmark, *typ;
865 switch (mtnp->mt_flags & __MT_MARKMASK) {
866 case _MT_PLAIN: tmark = "/t"; break;
867 case _MT_SOUP_h: tmark = "/h"; break;
868 case _MT_SOUP_H: tmark = "/H"; break;
869 default: tmark = " "; break;
871 typ = ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER)
872 ? "" : _mt_typnames[mtnp->mt_flags & __MT_TMASK];
874 fprintf(fp, "%c%s %s%.*s <%s>\n",
875 (mtnp->mt_flags & _MT_USR ? 'U'
876 : (mtnp->mt_flags & _MT_SYS ? 'S'
877 : (mtnp->mt_flags & _MT_LOADED ? 'F' : 'B'))),
878 tmark, typ, (int)mtnp->mt_mtlen, mtnp->mt_line,
879 mtnp->mt_line + mtnp->mt_mtlen);
882 page_or_print(fp, l);
883 Fclose(fp);
884 } else {
885 for (; *argv != NULL; ++argv) {
886 mtnp = _mt_create(TRU1, _MT_LOADED, *argv, strlen(*argv));
887 if (mtnp != NULL) {
888 mtnp->mt_next = _mt_list;
889 _mt_list = mtnp;
890 } else
891 v = NULL;
894 jleave:
895 NYD_LEAVE;
896 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
899 FL int
900 c_unmimetype(void *v)
902 char **argv = v;
903 struct mtnode *lnp, *mtnp;
904 bool_t match;
905 NYD_ENTER;
907 /* Need to load that first as necessary */
908 if (!_mt_is_init)
909 _mt_init();
911 for (; *argv != NULL; ++argv) {
912 if (!asccasecmp(*argv, "reset")) {
913 _mt_is_init = FAL0;
914 goto jdelall;
917 if (argv[0][0] == '*' && argv[0][1] == '\0') {
918 jdelall:
919 while ((mtnp = _mt_list) != NULL) {
920 _mt_list = mtnp->mt_next;
921 free(mtnp);
923 continue;
926 for (match = FAL0, lnp = NULL, mtnp = _mt_list; mtnp != NULL;) {
927 char const *typ;
928 char *val;
929 size_t i;
931 if ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER) {
932 typ = "";
933 i = 0;
934 } else {
935 typ = _mt_typnames[mtnp->mt_flags & __MT_TMASK];
936 i = strlen(typ);
939 val = ac_alloc(i + mtnp->mt_mtlen +1);
940 memcpy(val, typ, i);
941 memcpy(val + i, mtnp->mt_line, mtnp->mt_mtlen);
942 val[i += mtnp->mt_mtlen] = '\0';
943 i = asccasecmp(val, *argv);
944 ac_free(val);
946 if (!i) {
947 struct mtnode *nnp = mtnp->mt_next;
948 if (lnp == NULL)
949 _mt_list = nnp;
950 else
951 lnp->mt_next = nnp;
952 free(mtnp);
953 mtnp = nnp;
954 match = TRU1;
955 } else
956 lnp = mtnp, mtnp = mtnp->mt_next;
958 if (!match) {
959 if (!(pstate & PS_ROBOT) || (options & OPT_D_V))
960 n_err(_("No such MIME type: %s\n"), *argv);
961 v = NULL;
964 NYD_LEAVE;
965 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
968 FL bool_t
969 mime_type_check_mtname(char const *name)
971 struct mtlookup mtl;
972 bool_t rv;
973 NYD_ENTER;
975 rv = (_mt_by_mtname(&mtl, name) != NULL);
976 NYD_LEAVE;
977 return rv;
980 FL char *
981 mime_type_classify_filename(char const *name)
983 struct mtlookup mtl;
984 NYD_ENTER;
986 _mt_by_filename(&mtl, name, TRU1);
987 NYD_LEAVE;
988 return mtl.mtl_result;
991 FL enum conversion
992 mime_type_classify_file(FILE *fp, char const **contenttype,
993 char const **charset, int *do_iconv)
995 /* TODO classify once only PLEASE PLEASE PLEASE */
996 enum mime_type_class mtc;
997 enum mime_enc menc;
998 off_t fpsz;
999 NYD_ENTER;
1001 assert(ftell(fp) == 0x0l);
1003 *do_iconv = 0;
1005 if (*contenttype == NULL)
1006 mtc = _MT_C_NCTT;
1007 else if (!ascncasecmp(*contenttype, "text/", 5))
1008 mtc = ok_blook(mime_allow_text_controls)
1009 ? _MT_C_ISTXT | _MT_C_ISTXTCOK : _MT_C_ISTXT;
1010 else
1011 mtc = _MT_C_CLEAN;
1013 menc = mime_enc_target();
1015 if ((fpsz = fsize(fp)) == 0)
1016 goto j7bit;
1017 else {
1018 char buf[BUFFER_SIZE];
1019 struct mt_class_arg mtca;
1021 _mt_classify_init(&mtca, mtc);
1022 for (;;) {
1023 mtca.mtca_len = fread(buf, sizeof(buf[0]), NELEM(buf), fp);
1024 mtca.mtca_buf = buf;
1025 if ((mtc = _mt_classify_round(&mtca)) & _MT_C_SUGGEST_DONE)
1026 break;
1027 if (mtca.mtca_len == 0)
1028 break;
1030 /* TODO ferror(fp) ! */
1031 rewind(fp);
1034 if (mtc & _MT_C_HASNUL) {
1035 menc = MIMEE_B64;
1036 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1037 * on request; else enforce what file(1)/libmagic(3) would suggest */
1038 if (mtc & _MT_C_ISTXTCOK)
1039 goto jcharset;
1040 if (mtc & (_MT_C_NCTT | _MT_C_ISTXT))
1041 *contenttype = "application/octet-stream";
1042 if (*charset == NULL)
1043 *charset = "binary";
1044 goto jleave;
1047 if (mtc &
1048 (_MT_C_LONGLINES | _MT_C_CTRLCHAR | _MT_C_NOTERMNL | _MT_C_FROM_)) {
1049 if (menc != MIMEE_B64)
1050 menc = MIMEE_QP;
1051 goto jstepi;
1053 if (mtc & _MT_C_HIGHBIT) {
1054 jstepi:
1055 if (mtc & (_MT_C_NCTT | _MT_C_ISTXT))
1056 *do_iconv = ((mtc & _MT_C_HIGHBIT) != 0);
1057 } else
1058 j7bit:
1059 menc = MIMEE_7B;
1060 if (mtc & _MT_C_NCTT)
1061 *contenttype = "text/plain";
1063 /* Not an attachment with specified charset? */
1064 jcharset:
1065 if (*charset == NULL) /* TODO MIME/send: iter active? iter! else */
1066 *charset = (mtc & _MT_C_HIGHBIT) ? charset_iter_or_fallback()
1067 : charset_get_7bit();
1068 jleave:
1069 NYD_LEAVE;
1070 /* TODO mime_type_file_classify() shouldn't return conversion */
1071 return (menc == MIMEE_7B ? CONV_7BIT :
1072 (menc == MIMEE_8B ? CONV_8BIT :
1073 (menc == MIMEE_QP ? CONV_TOQP : CONV_TOB64)));
1076 FL enum mimecontent
1077 mime_type_classify_part(struct mimepart *mpp) /* FIXME charset=binary ??? */
1079 struct mtlookup mtl;
1080 enum mimecontent mc;
1081 char const *ct;
1082 union {char const *cp; ui32_t f;} mce;
1083 bool_t is_os;
1084 NYD_ENTER;
1086 mc = MIME_UNKNOWN;
1087 if ((ct = mpp->m_ct_type_plain) == NULL) /* TODO may not */
1088 ct = "";
1090 if ((mce.cp = ok_vlook(mime_counter_evidence)) != NULL) {
1091 char *eptr;
1092 ul_i ul;
1094 ul = strtoul(mce.cp, &eptr, 0); /* XXX strtol */
1095 if (*mce.cp == '\0')
1096 is_os = FAL0;
1097 else if (*eptr != '\0' || (ui64_t)ul >= UI32_MAX) {
1098 n_err(_("Can't parse *mime-counter-evidence* value: %s\n"), mce.cp);
1099 is_os = FAL0;
1100 } else {
1101 mce.f = (ui32_t)ul | MIMECE_SET;
1102 is_os = !asccasecmp(ct, "application/octet-stream");
1104 if (mpp->m_filename != NULL && (is_os || (mce.f & MIMECE_ALL_OVWR))) {
1105 if (_mt_by_filename(&mtl, mpp->m_filename, TRU1) == NULL) {
1106 if (is_os)
1107 goto jos_content_check;
1108 } else if (is_os || asccasecmp(ct, mtl.mtl_result)) {
1109 if (mce.f & MIMECE_ALL_OVWR)
1110 mpp->m_ct_type_plain = ct = mtl.mtl_result;
1111 if (mce.f & (MIMECE_BIN_OVWR | MIMECE_ALL_OVWR))
1112 mpp->m_ct_type_usr_ovwr = ct = mtl.mtl_result;
1116 } else
1117 is_os = FAL0;
1119 if (strchr(ct, '/') == NULL) /* For compatibility with non-MIME */
1120 mc = MIME_TEXT;
1121 else if (is_asccaseprefix(ct, "text/")) {
1122 ct += sizeof("text/") -1;
1123 if (!asccasecmp(ct, "plain"))
1124 mc = MIME_TEXT_PLAIN;
1125 else if (!asccasecmp(ct, "html"))
1126 mc = MIME_TEXT_HTML;
1127 else
1128 mc = MIME_TEXT;
1129 } else if (is_asccaseprefix(ct, "message/")) {
1130 ct += sizeof("message/") -1;
1131 if (!asccasecmp(ct, "rfc822"))
1132 mc = MIME_822;
1133 else
1134 mc = MIME_MESSAGE;
1135 } else if (!ascncasecmp(ct, "multipart/", 10)) {
1136 ct += sizeof("multipart/") -1;
1137 if (!asccasecmp(ct, "alternative"))
1138 mc = MIME_ALTERNATIVE;
1139 else if (!asccasecmp(ct, "related"))
1140 mc = MIME_RELATED;
1141 else if (!asccasecmp(ct, "digest"))
1142 mc = MIME_DIGEST;
1143 else
1144 mc = MIME_MULTI;
1145 } else if (is_asccaseprefix(ct, "application/")) {
1146 if (is_os)
1147 goto jos_content_check;
1148 ct += sizeof("application/") -1;
1149 if (!asccasecmp(ct, "pkcs7-mime") || !asccasecmp(ct, "x-pkcs7-mime"))
1150 mc = MIME_PKCS7;
1152 jleave:
1153 NYD_LEAVE;
1154 return mc;
1156 jos_content_check:
1157 if ((mce.f & MIMECE_BIN_PARSE) && mpp->m_mime_enc != MIMEE_BIN &&
1158 mpp->m_charset != NULL && asccasecmp(mpp->m_charset, "binary"))
1159 mc = _mt_classify_os_part(mce.f, mpp);
1160 goto jleave;
1163 FL enum mime_handler_flags
1164 mime_type_handler(struct mime_handler *mhp, struct mimepart const *mpp,
1165 enum sendaction action)
1167 #define __S "pipe-"
1168 #define __L (sizeof(__S) -1)
1169 struct mtlookup mtl;
1170 char *buf, *cp;
1171 enum mime_handler_flags rv;
1172 char const *es, *cs, *ccp;
1173 size_t el, cl, l;
1174 NYD_ENTER;
1176 memset(mhp, 0, sizeof *mhp);
1177 buf = NULL;
1179 rv = MIME_HDL_NULL;
1180 if (action == SEND_QUOTE || action == SEND_QUOTE_ALL)
1181 rv |= MIME_HDL_ISQUOTE;
1182 else if (action != SEND_TODISP && action != SEND_TODISP_ALL)
1183 goto jleave;
1185 el = ((es = mpp->m_filename) != NULL && (es = strrchr(es, '.')) != NULL &&
1186 *++es != '\0') ? strlen(es) : 0;
1187 cl = ((cs = mpp->m_ct_type_usr_ovwr) != NULL ||
1188 (cs = mpp->m_ct_type_plain) != NULL) ? strlen(cs) : 0;
1189 if ((l = MAX(el, cl)) == 0) {
1190 /* TODO this should be done during parse time! */
1191 goto jleave;
1194 /* We don't pass the flags around, so ensure carrier is up-to-date */
1195 mhp->mh_flags = rv;
1197 buf = ac_alloc(__L + l +1);
1198 memcpy(buf, __S, __L);
1200 /* File-extension handlers take precedence.
1201 * Yes, we really "fail" here for file extensions which clash MIME types */
1202 if (el > 0) {
1203 memcpy(buf + __L, es, el +1);
1204 for (cp = buf + __L; *cp != '\0'; ++cp)
1205 *cp = lowerconv(*cp);
1207 if ((mhp->mh_shell_cmd = ccp = vok_vlook(buf)) != NULL) {
1208 rv = _mt_pipe_check(mhp);
1209 goto jleave;
1213 /* Then MIME Content-Type:, if any */
1214 if (cl == 0)
1215 goto jleave;
1217 memcpy(buf + __L, cs, cl +1);
1218 for (cp = buf + __L; *cp != '\0'; ++cp)
1219 *cp = lowerconv(*cp);
1221 if ((mhp->mh_shell_cmd = vok_vlook(buf)) != NULL) {
1222 rv = _mt_pipe_check(mhp);
1223 goto jleave;
1226 if (_mt_by_mtname(&mtl, cs) != NULL)
1227 switch (mtl.mtl_node->mt_flags & __MT_MARKMASK) {
1228 #ifndef HAVE_FILTER_HTML_TAGSOUP
1229 case _MT_SOUP_H:
1230 break;
1231 #endif
1232 case _MT_SOUP_h:
1233 #ifdef HAVE_FILTER_HTML_TAGSOUP
1234 case _MT_SOUP_H:
1235 mhp->mh_ptf = &htmlflt_process_main;
1236 mhp->mh_msg.l = strlen(mhp->mh_msg.s =
1237 UNCONST(_("Builtin HTML tagsoup filter")));
1238 rv ^= MIME_HDL_NULL | MIME_HDL_PTF;
1239 goto jleave;
1240 #endif
1241 /* FALLTHRU */
1242 case _MT_PLAIN:
1243 mhp->mh_msg.l = strlen(mhp->mh_msg.s = UNCONST(_("Plain text view")));
1244 rv ^= MIME_HDL_NULL | MIME_HDL_TEXT;
1245 goto jleave;
1246 default:
1247 break;
1250 jleave:
1251 if (buf != NULL)
1252 ac_free(buf);
1254 mhp->mh_flags = rv;
1255 if ((rv &= MIME_HDL_TYPE_MASK) == MIME_HDL_NULL)
1256 mhp->mh_msg.l = strlen(mhp->mh_msg.s = UNCONST(
1257 _("[-- No MIME handler installed or none applicable --]")));
1258 NYD_LEAVE;
1259 return rv;
1260 #undef __L
1261 #undef __S
1264 /* s-it-mode */