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 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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.
20 #define n_FILE mime_types
22 #ifndef HAVE_AMALGAMATION
36 __MT_TMAX
= _MT_OTHER
,
39 _MT_CMD
= 1<< 8, /* Via `mimetype' (not struct mtbltin) */
40 _MT_USR
= 1<< 9, /* MIME_TYPES_USR */
41 _MT_SYS
= 1<<10, /* MIME_TYPES_SYS */
42 _MT_FSPEC
= 1<<11, /* Loaded via f= *mimetypes-load-control* spec. */
44 _MT_PLAIN
= 1<<16, /* Without pipe handler display as text */
45 _MT_SOUP_h
= 2<<16, /* Ditto, but HTML tagsoup parser if possible */
46 _MT_SOUP_H
= 3<<16, /* HTML tagsoup parser, else NOT plain text */
47 __MT_MARKMASK
= _MT_SOUP_H
50 enum mime_type_class
{
51 _MT_C_CLEAN
= 0, /* Plain RFC 5322 message */
52 _MT_C_NCTT
= 1<<0, /* *contenttype == NULL */
53 _MT_C_ISTXT
= 1<<1, /* *contenttype =~ text\/ */
54 _MT_C_ISTXTCOK
= 1<<2, /* _ISTXT + *mime-allow-text-controls* */
55 _MT_C_HIGHBIT
= 1<<3, /* Not 7bit clean */
56 _MT_C_LONGLINES
= 1<<4, /* MIME_LINELEN_LIMIT exceed. */
57 _MT_C_CTRLCHAR
= 1<<5, /* Control characters seen */
58 _MT_C_HASNUL
= 1<<6, /* Contains \0 characters */
59 _MT_C_NOTERMNL
= 1<<7, /* Lacks a final newline */
60 _MT_C_FROM_
= 1<<8, /* ^From_ seen */
61 _MT_C_SUGGEST_DONE
= 1<<16 /* Inspector suggests to stop further parse */
71 struct mtnode
*mt_next
;
73 ui32_t mt_mtlen
; /* Length of MIME type string, rest thereafter */
74 /* C99 forbids flexible arrays in union, so unfortunately we waste a pointer
75 * that could already store character data here */
82 struct mtnode
const *mtl_node
;
83 char *mtl_result
; /* If requested, salloc()ed MIME type */
92 enum mime_type_class mtca_mtc
;
95 static struct mtbltin
const _mt_bltin
[] = {
96 #include "mime_types.h"
99 static char const _mt_typnames
[][16] = {
100 "application/", "audio/", "image/",
101 "message/", "multipart/", "text/",
104 n_CTAV(_MT_APPLICATION
== 0 && _MT_AUDIO
== 1 && _MT_IMAGE
== 2 &&
105 _MT_MESSAGE
== 3 && _MT_MULTIPART
== 4 && _MT_TEXT
== 5 &&
109 static bool_t _mt_is_init
;
110 static struct mtnode
*_mt_list
;
112 /* Initialize MIME type list in order */
113 static void _mt_init(void);
114 static bool_t
__mt_load_file(ui32_t orflags
,
115 char const *file
, char **line
, size_t *linesize
);
117 /* Create (prepend) a new MIME type; cmdcalled results in a bit more verbosity
119 static struct mtnode
* _mt_create(bool_t cmdcalled
, ui32_t orflags
,
120 char const *line
, size_t len
);
122 /* Try to find MIME type by X (after zeroing mtlp), return NULL if not found;
123 * if with_result >mtl_result will be created upon success for the former */
124 static struct mtlookup
* _mt_by_filename(struct mtlookup
*mtlp
,
125 char const *name
, bool_t with_result
);
126 static struct mtlookup
* _mt_by_mtname(struct mtlookup
*mtlp
,
129 /* In-depth inspection of raw content: call _round() repeatedly, last time with
130 * a 0 length buffer, finally check .mtca_mtc for result.
131 * No further call is needed if _round() return includes _MT_C_SUGGEST_DONE,
132 * as the resulting classification is unambiguous */
133 SINLINE
struct mt_class_arg
* _mt_classify_init(struct mt_class_arg
*mtcap
,
134 enum mime_type_class initval
);
135 static enum mime_type_class
_mt_classify_round(struct mt_class_arg
*mtcap
);
137 /* We need an in-depth inspection of an application/octet-stream part */
138 static enum mimecontent
_mt_classify_os_part(ui32_t mce
, struct mimepart
*mpp
);
140 /* Check whether a *pipe-XY* handler is applicable, and adjust flags according
141 * to the defined trigger characters; upon entry MIME_HDL_NULL is set, and that
142 * isn't changed if mhp doesn't apply */
143 static enum mime_handler_flags
_mt_pipe_check(struct mime_handler
*mhp
);
149 char c
, *line
; /* TODO line pool (below) */
152 char const *srcs_arr
[10], *ccp
, **srcs
;
158 /* Always load our builtins */
159 for (tail
= NULL
, i
= 0; i
< n_NELEM(_mt_bltin
); ++i
) {
160 struct mtbltin
const *mtbp
= _mt_bltin
+ i
;
161 struct mtnode
*mtnp
= smalloc(sizeof *mtnp
);
164 tail
->mt_next
= mtnp
;
168 mtnp
->mt_next
= NULL
;
169 mtnp
->mt_flags
= mtbp
->mtb_flags
;
170 mtnp
->mt_mtlen
= mtbp
->mtb_mtlen
;
171 mtnp
->mt_line
= mtbp
->mtb_line
;
174 /* Decide which files sources have to be loaded */
175 if ((ccp
= ok_vlook(mimetypes_load_control
)) == NULL
)
177 else if (*ccp
== '\0')
181 srcs
[-1] = srcs
[-2] = NULL
;
183 if (strchr(ccp
, '=') != NULL
) {
186 while ((ccp
= n_strsep(&line
, ',', TRU1
)) != NULL
) {
187 switch ((c
= *ccp
)) {
189 srcs_arr
[1] = MIME_TYPES_SYS
;
193 srcs_arr
[0] = MIME_TYPES_USR
;
199 if (*++ccp
== '=' && *++ccp
!= '\0') {
200 if (PTR2SIZE(srcs
- srcs_arr
) < n_NELEM(srcs_arr
))
203 n_err(_("*mimetypes-load-control*: too many sources, "
204 "skipping %s\n"), n_shexp_quote_cp(ccp
, FAL0
));
212 } else for (i
= 0; (c
= ccp
[i
]) != '\0'; ++i
)
214 case 'S': case 's': srcs_arr
[1] = MIME_TYPES_SYS
; break;
215 case 'U': case 'u': srcs_arr
[0] = MIME_TYPES_USR
; break;
218 n_err(_("*mimetypes-load-control*: unsupported content: %s\n"), ccp
);
222 /* Load all file-based sources in the desired order */
225 for (j
= 0, i
= (ui32_t
)PTR2SIZE(srcs
- srcs_arr
), srcs
= srcs_arr
;
226 i
> 0; ++j
, ++srcs
, --i
)
229 else if (!__mt_load_file((j
== 0 ? _MT_USR
230 : (j
== 1 ? _MT_SYS
: _MT_FSPEC
)), *srcs
, &line
, &linesize
)) {
231 if ((options
& OPT_D_V
) || j
> 1)
232 n_err(_("*mimetypes-load-control*: can't open or load %s\n"),
233 n_shexp_quote_cp(*srcs
, FAL0
));
243 __mt_load_file(ui32_t orflags
, char const *file
, char **line
, size_t *linesize
)
247 struct mtnode
*head
, *tail
, *mtnp
;
251 if ((cp
= file_expand(file
)) == NULL
|| (fp
= Fopen(cp
, "r")) == NULL
) {
256 for (head
= tail
= NULL
; fgetline(line
, linesize
, NULL
, &len
, fp
, 0) != 0;)
257 if ((mtnp
= _mt_create(FAL0
, orflags
, *line
, len
)) != NULL
) {
261 tail
->mt_next
= mtnp
;
265 tail
->mt_next
= _mt_list
;
275 static struct mtnode
*
276 _mt_create(bool_t cmdcalled
, ui32_t orflags
, char const *line
, size_t len
)
278 struct mtnode
*mtnp
= NULL
;
279 char const *typ
, *subtyp
;
283 /* Drop anything after a comment first */
284 if ((typ
= memchr(line
, '#', len
)) != NULL
)
285 len
= PTR2SIZE(typ
- line
);
287 /* Then trim any trailing whitespace from line (including NL/CR) */
288 while (len
> 0 && spacechar(line
[len
- 1]))
291 /* Isolate MIME type, trim any whitespace from it */
292 while (len
> 0 && blankchar(*line
))
296 /* (But wait - is there a type marker?) */
297 if (!(orflags
& (_MT_USR
| _MT_SYS
)) && *typ
== '@') {
301 orflags
|= _MT_PLAIN
;
305 } else if (len
> 4 && typ
[2] == '@' && typ
[3] == ' ') {
307 case 't': orflags
|= _MT_PLAIN
; goto jexttypmar
;
308 case 'h': orflags
|= _MT_SOUP_h
; goto jexttypmar
;
309 case 'H': orflags
|= _MT_SOUP_H
;
322 while (len
> 0 && !blankchar(*line
))
324 /* Ignore empty lines and even incomplete specifications (only MIME type)
325 * because this is quite common in mime.types(5) files */
326 if (len
== 0 || (tlen
= PTR2SIZE(line
- typ
)) == 0) {
328 n_err(_("Empty MIME type or no extensions given: %s\n"),
329 (len
== 0 ? _("(no value)") : line
));
333 if ((subtyp
= memchr(typ
, '/', tlen
)) == NULL
) {
335 if (cmdcalled
|| (options
& OPT_D_V
))
336 n_err(_("%s MIME type: %s\n"),
337 (cmdcalled
? _("Invalid") : _("mime.types(5): invalid")), typ
);
342 /* Map to mime_type */
343 tlen
= PTR2SIZE(subtyp
- typ
);
344 for (i
= __MT_TMIN
;;) {
345 if (!ascncasecmp(_mt_typnames
[i
], typ
, tlen
)) {
347 tlen
= PTR2SIZE(line
- subtyp
);
351 if (++i
== __MT_TMAX
) {
352 orflags
|= _MT_OTHER
;
353 tlen
= PTR2SIZE(line
- typ
);
358 /* Strip leading whitespace from the list of extensions;
359 * trailing WS has already been trimmed away above.
360 * Be silent on slots which define a mimetype without any value */
361 while (len
> 0 && blankchar(*line
))
367 mtnp
= smalloc(sizeof(*mtnp
) + tlen
+ len
+1);
368 mtnp
->mt_next
= NULL
;
369 mtnp
->mt_flags
= orflags
;
370 mtnp
->mt_mtlen
= (ui32_t
)tlen
;
371 { char *l
= (char*)(mtnp
+ 1);
373 memcpy(l
, typ
, tlen
);
374 memcpy(l
+ tlen
, line
, len
);
384 static struct mtlookup
*
385 _mt_by_filename(struct mtlookup
*mtlp
, char const *name
, bool_t with_result
)
389 char const *ext
, *cp
;
392 memset(mtlp
, 0, sizeof *mtlp
);
394 if ((nlen
= strlen(name
)) == 0) /* TODO name should be a URI */
396 /* We need a period TODO we should support names like README etc. */
397 for (i
= nlen
; name
[--i
] != '.';)
398 if (i
== 0 || name
[i
] == '/') /* XXX no magics */
400 /* While here, basename() it */
401 while (i
> 0 && name
[i
- 1] != '/')
405 mtlp
->mtl_name
= name
;
406 mtlp
->mtl_nlen
= nlen
;
411 /* ..all the MIME types */
412 for (mtnp
= _mt_list
; mtnp
!= NULL
; mtnp
= mtnp
->mt_next
)
413 for (ext
= mtnp
->mt_line
+ mtnp
->mt_mtlen
;; ext
= cp
) {
415 while (whitechar(*cp
))
418 while (!whitechar(*cp
) && *cp
!= '\0')
421 if ((i
= PTR2SIZE(cp
- ext
)) == 0)
423 /* Don't allow neither of ".txt" or "txt" to match "txt" */
424 else if (i
+ 1 >= nlen
|| name
[(j
= nlen
- i
) - 1] != '.' ||
425 ascncasecmp(name
+ j
, ext
, i
))
429 mtlp
->mtl_node
= mtnp
;
434 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
438 name
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
442 mtlp
->mtl_result
= salloc(i
+ j
+1);
444 memcpy(mtlp
->mtl_result
, name
, j
);
445 memcpy(mtlp
->mtl_result
+ j
, mtnp
->mt_line
, i
);
446 mtlp
->mtl_result
[j
+= i
] = '\0';
456 static struct mtlookup
*
457 _mt_by_mtname(struct mtlookup
*mtlp
, char const *mtname
)
464 memset(mtlp
, 0, sizeof *mtlp
);
466 if ((mtlp
->mtl_nlen
= nlen
= strlen(mtlp
->mtl_name
= mtname
)) == 0)
472 /* ..all the MIME types */
473 for (mtnp
= _mt_list
; mtnp
!= NULL
; mtnp
= mtnp
->mt_next
) {
474 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
478 cp
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
483 if (i
+ j
== mtlp
->mtl_nlen
) {
484 char *xmt
= ac_alloc(i
+ j
+1);
487 memcpy(xmt
+ j
, mtnp
->mt_line
, i
);
489 i
= asccasecmp(mtname
, xmt
);
494 mtlp
->mtl_node
= mtnp
;
506 SINLINE
struct mt_class_arg
*
507 _mt_classify_init(struct mt_class_arg
* mtcap
, enum mime_type_class initval
)
510 memset(mtcap
, 0, sizeof *mtcap
);
511 mtcap
->mtca_lastc
= mtcap
->mtca_c
= EOF
;
512 mtcap
->mtca_mtc
= initval
;
517 static enum mime_type_class
518 _mt_classify_round(struct mt_class_arg
*mtcap
)
520 /* TODO BTW., after the MIME/send layer rewrite we could use a MIME
521 * TODO boundary of "=-=-=" if we would add a B_ in EQ spirit to F_,
522 * TODO and report that state to the outer world */
524 #define F_SIZEOF (sizeof(F_) -1)
525 char f_buf
[F_SIZEOF
], *f_p
= f_buf
;
530 enum mime_type_class mtc
;
533 buf
= mtcap
->mtca_buf
;
534 blen
= mtcap
->mtca_len
;
535 curlen
= mtcap
->mtca_curlen
;
537 lastc
= mtcap
->mtca_lastc
;
538 mtc
= mtcap
->mtca_mtc
;
543 /* Real EOF, or only current buffer end? */
544 if (mtcap
->mtca_len
== 0)
554 if (!(mtc
& _MT_C_ISTXTCOK
)) {
555 mtc
|= _MT_C_SUGGEST_DONE
;
560 if (c
== '\n' || c
== EOF
) {
561 if (curlen
>= MIME_LINELEN_LIMIT
)
562 mtc
|= _MT_C_LONGLINES
;
570 /* A bit hairy is handling of \r=\x0D=CR.
572 * Control characters other than TAB, or CR and LF as parts of CRLF
573 * pairs, must not appear. \r alone does not force _CTRLCHAR below since
574 * we cannot peek the next character. Thus right here, inspect the last
575 * seen character for if its \r and set _CTRLCHAR in a delayed fashion */
576 /*else*/ if (lastc
== '\r')
577 mtc
|= _MT_C_CTRLCHAR
;
579 /* Control character? XXX this is all ASCII here */
580 if (c
< 0x20 || c
== 0x7F) {
581 /* RFC 2045, 6.7, as above ... */
582 if (c
!= '\t' && c
!= '\r')
583 mtc
|= _MT_C_CTRLCHAR
;
584 /* If there is a escape sequence in backslash notation defined for
585 * this in ANSI X3.159-1989 (ANSI C89), don't treat it as a control
586 * for real. I.e., \a=\x07=BEL, \b=\x08=BS, \t=\x09=HT. Don't follow
587 * libmagic(1) in respect to \v=\x0B=VT. \f=\x0C=NP; do ignore
589 if ((c
>= '\x07' && c
<= '\x0D') || c
== '\x1B')
591 mtc
|= _MT_C_HASNUL
; /* Force base64 */
592 if (!(mtc
& _MT_C_ISTXTCOK
)) {
593 mtc
|= _MT_C_SUGGEST_DONE
;
596 } else if ((ui8_t
)c
& 0x80) {
597 mtc
|= _MT_C_HIGHBIT
;
598 /* TODO count chars with HIGHBIT? libmagic?
599 * TODO try encode part - base64 if bails? */
600 if (!(mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))) { /* TODO _NCTT?? */
601 mtc
|= _MT_C_HASNUL
/* Force base64 */ | _MT_C_SUGGEST_DONE
;
604 } else if (!(mtc
& _MT_C_FROM_
) && UICMP(z
, curlen
, <, F_SIZEOF
)) {
606 if (UICMP(z
, curlen
, ==, F_SIZEOF
- 1) &&
607 PTR2SIZE(f_p
- f_buf
) == F_SIZEOF
&&
608 !memcmp(f_buf
, F_
, F_SIZEOF
))
612 if (c
== EOF
&& lastc
!= '\n')
613 mtc
|= _MT_C_NOTERMNL
;
615 mtcap
->mtca_curlen
= curlen
;
616 mtcap
->mtca_lastc
= lastc
;
618 mtcap
->mtca_mtc
= mtc
;
625 static enum mimecontent
626 _mt_classify_os_part(ui32_t mce
, struct mimepart
*mpp
)
628 struct str in
= {NULL
, 0}, outrest
, inrest
, dec
;
629 struct mt_class_arg mtca
;
631 enum mime_type_class mtc
;
639 assert(mpp
->m_mime_enc
!= MIMEE_BIN
);
641 outrest
= inrest
= dec
= in
;
645 /* TODO v15-compat Note we actually bypass our usual file handling by
646 * TODO directly using fseek() on mb.mb_itf -- the v15 rewrite will change
647 * TODO all of this, and until then doing it like this is the only option
648 * TODO to integrate nicely into whoever calls us */
649 start_off
= ftell(mb
.mb_itf
);
650 if ((ibuf
= setinput(&mb
, (struct message
*)mpp
, NEED_BODY
)) == NULL
) {
652 fseek(mb
.mb_itf
, start_off
, SEEK_SET
);
657 /* Skip part headers */
658 for (lc
= '\0'; cnt
> 0; lc
= c
, --cnt
)
659 if ((c
= getc(ibuf
)) == EOF
|| (c
== '\n' && lc
== '\n'))
661 if (cnt
== 0 || ferror(ibuf
))
664 /* So now let's inspect the part content, decoding content-transfer-encoding
665 * along the way TODO this should simply be "mime_factory_create(MPP)"! */
666 _mt_classify_init(&mtca
, _MT_C_ISTXT
);
671 c
= (--cnt
== 0) ? EOF
: getc(ibuf
);
672 if ((dobuf
= (c
== '\n'))) {
673 /* Ignore empty lines */
676 } else if ((dobuf
= (c
== EOF
))) {
677 if (lsz
== 0 && outrest
.l
== 0)
682 in
.s
= srealloc(in
.s
, lsz
+= LINESIZE
);
684 in
.s
[in
.l
++] = (char)c
;
689 switch (mpp
->m_mime_enc
) {
691 if (!b64_decode_text(&dec
, &in
, &outrest
,
692 (did_inrest
? NULL
: &inrest
))) {
693 mtca
.mtca_mtc
= _MT_C_HASNUL
;
694 goto jstopit
; /* break;break; */
699 if (!qp_decode_text(&dec
, &in
, &outrest
, &inrest
)) {
700 mtca
.mtca_mtc
= _MT_C_HASNUL
;
701 goto jstopit
; /* break;break; */
703 if (dec
.l
== 0 && c
!= EOF
) {
709 /* Temporarily switch those two buffers.. */
716 mtca
.mtca_buf
= dec
.s
;
717 mtca
.mtca_len
= (ssize_t
)dec
.l
;
718 if ((mtc
= _mt_classify_round(&mtca
)) & _MT_C_SUGGEST_DONE
) {
725 /* ..and restore switched */
733 if ((in
.l
= inrest
.l
) > 0) {
745 if (outrest
.s
!= NULL
)
747 if (inrest
.s
!= NULL
)
750 fseek(mb
.mb_itf
, start_off
, SEEK_SET
);
752 if (!(mtc
& (_MT_C_HASNUL
| _MT_C_CTRLCHAR
))) {
753 mc
= MIME_TEXT_PLAIN
;
754 if (mce
& MIMECE_ALL_OVWR
)
755 mpp
->m_ct_type_plain
= "text/plain";
756 if (mce
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
757 mpp
->m_ct_type_usr_ovwr
= "text/plain";
764 static enum mime_handler_flags
765 _mt_pipe_check(struct mime_handler
*mhp
)
767 enum mime_handler_flags rv_orig
, rv
;
771 rv_orig
= rv
= mhp
->mh_flags
;
773 /* Do we have any handler for this part? */
774 if (*(cp
= mhp
->mh_shell_cmd
) == '\0')
776 else if (*cp
++ != '@') {
779 } else if (*cp
== '\0') {
786 case '*': rv
|= MIME_HDL_ALWAYS
; ++cp
; goto jnextc
;
787 case '#': rv
|= MIME_HDL_NOQUOTE
; ++cp
; goto jnextc
;
788 case '&': rv
|= MIME_HDL_ASYNC
; ++cp
; goto jnextc
;
789 case '!': rv
|= MIME_HDL_NEEDSTERM
; ++cp
; goto jnextc
;
791 if (rv
& MIME_HDL_TMPF
)
792 rv
|= MIME_HDL_TMPF_UNLINK
;
797 rv
|= MIME_HDL_TMPF_FILL
;
806 mhp
->mh_shell_cmd
= cp
;
809 if (rv
& MIME_HDL_TMPF_FILL
)
813 if (rv
& MIME_HDL_ISQUOTE
) {
814 if (rv
& MIME_HDL_NOQUOTE
)
817 /* Cannot fetch data back from asynchronous process */
818 if (rv
& MIME_HDL_ASYNC
)
821 /* TODO Can't use a "needsterminal" program for quoting */
822 if (rv
& MIME_HDL_NEEDSTERM
)
826 if (rv
& MIME_HDL_NEEDSTERM
) {
827 if (rv
& MIME_HDL_ASYNC
) {
828 n_err(_("MIME type handlers: can't use needsterminal and "
829 "x-nail-async together\n"));
833 /* needsterminal needs a terminal */
834 if (!(options
& OPT_INTERACTIVE
))
838 if (!(rv
& MIME_HDL_ALWAYS
) && !(pstate
& PS_MSGLIST_DIRECT
)) {
839 /* Viewing multiple messages in one go, don't block system */
840 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
841 _("[-- Directly address message only for display --]\n")));
870 if (_mt_list
== NULL
) {
871 printf(_("*mimetypes-load-control*: no mime.types(5) available\n"));
875 if ((fp
= Ftmp(NULL
, "mimelist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
877 n_perr(_("tmpfile"), 0);
882 for (l
= 0, mtnp
= _mt_list
; mtnp
!= NULL
; ++l
, mtnp
= mtnp
->mt_next
) {
883 char const *tmark
, *typ
;
885 switch (mtnp
->mt_flags
& __MT_MARKMASK
) {
886 case _MT_PLAIN
: tmark
= "/t"; break;
887 case _MT_SOUP_h
: tmark
= "/h"; break;
888 case _MT_SOUP_H
: tmark
= "/H"; break;
889 default: tmark
= " "; break;
891 typ
= ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
)
892 ? n_empty
: _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
894 fprintf(fp
, "%c%s %s%.*s %s\n",
895 (mtnp
->mt_flags
& _MT_USR
? 'U'
896 : (mtnp
->mt_flags
& _MT_SYS
? 'S'
897 : (mtnp
->mt_flags
& _MT_FSPEC
? 'F'
898 : (mtnp
->mt_flags
& _MT_CMD
? 'C' : 'B')))),
899 tmark
, typ
, (int)mtnp
->mt_mtlen
, mtnp
->mt_line
,
900 mtnp
->mt_line
+ mtnp
->mt_mtlen
);
903 page_or_print(fp
, l
);
906 for (; *argv
!= NULL
; ++argv
) {
907 mtnp
= _mt_create(TRU1
, _MT_CMD
, *argv
, strlen(*argv
));
909 mtnp
->mt_next
= _mt_list
;
917 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
921 c_unmimetype(void *v
)
924 struct mtnode
*lnp
, *mtnp
;
928 /* Need to load that first as necessary */
932 for (; *argv
!= NULL
; ++argv
) {
933 if (!asccasecmp(*argv
, "reset")) {
938 if (argv
[0][0] == '*' && argv
[0][1] == '\0') {
940 while ((mtnp
= _mt_list
) != NULL
) {
941 _mt_list
= mtnp
->mt_next
;
947 for (match
= FAL0
, lnp
= NULL
, mtnp
= _mt_list
; mtnp
!= NULL
;) {
952 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
956 typ
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
960 val
= ac_alloc(i
+ mtnp
->mt_mtlen
+1);
962 memcpy(val
+ i
, mtnp
->mt_line
, mtnp
->mt_mtlen
);
963 val
[i
+= mtnp
->mt_mtlen
] = '\0';
964 i
= asccasecmp(val
, *argv
);
968 struct mtnode
*nnp
= mtnp
->mt_next
;
977 lnp
= mtnp
, mtnp
= mtnp
->mt_next
;
980 if (!(pstate
& PS_ROBOT
) || (options
& OPT_D_V
))
981 n_err(_("No such MIME type: %s\n"), *argv
);
986 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
990 mime_type_check_mtname(char const *name
)
996 rv
= (_mt_by_mtname(&mtl
, name
) != NULL
);
1002 mime_type_classify_filename(char const *name
)
1004 struct mtlookup mtl
;
1007 _mt_by_filename(&mtl
, name
, TRU1
);
1009 return mtl
.mtl_result
;
1013 mime_type_classify_file(FILE *fp
, char const **contenttype
,
1014 char const **charset
, int *do_iconv
)
1016 /* TODO classify once only PLEASE PLEASE PLEASE */
1017 enum mime_type_class mtc
;
1022 assert(ftell(fp
) == 0x0l
);
1026 if (*contenttype
== NULL
)
1028 else if (!ascncasecmp(*contenttype
, "text/", 5))
1029 mtc
= ok_blook(mime_allow_text_controls
)
1030 ? _MT_C_ISTXT
| _MT_C_ISTXTCOK
: _MT_C_ISTXT
;
1034 menc
= mime_enc_target();
1036 if ((fpsz
= fsize(fp
)) == 0)
1039 char buf
[BUFFER_SIZE
];
1040 struct mt_class_arg mtca
;
1042 _mt_classify_init(&mtca
, mtc
);
1044 mtca
.mtca_len
= fread(buf
, sizeof(buf
[0]), n_NELEM(buf
), fp
);
1045 mtca
.mtca_buf
= buf
;
1046 if ((mtc
= _mt_classify_round(&mtca
)) & _MT_C_SUGGEST_DONE
)
1048 if (mtca
.mtca_len
== 0)
1051 /* TODO ferror(fp) ! */
1055 if (mtc
& _MT_C_HASNUL
) {
1057 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1058 * on request; else enforce what file(1)/libmagic(3) would suggest */
1059 if (mtc
& _MT_C_ISTXTCOK
)
1061 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1062 *contenttype
= "application/octet-stream";
1063 if (*charset
== NULL
)
1064 *charset
= "binary";
1069 (_MT_C_LONGLINES
| _MT_C_CTRLCHAR
| _MT_C_NOTERMNL
| _MT_C_FROM_
)) {
1070 if (menc
!= MIMEE_B64
)
1074 if (mtc
& _MT_C_HIGHBIT
) {
1076 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1077 *do_iconv
= ((mtc
& _MT_C_HIGHBIT
) != 0);
1081 if (mtc
& _MT_C_NCTT
)
1082 *contenttype
= "text/plain";
1084 /* Not an attachment with specified charset? */
1086 if (*charset
== NULL
) /* TODO MIME/send: iter active? iter! else */
1087 *charset
= (mtc
& _MT_C_HIGHBIT
) ? charset_iter_or_fallback()
1088 : charset_get_7bit();
1091 /* TODO mime_type_file_classify() shouldn't return conversion */
1092 return (menc
== MIMEE_7B
? CONV_7BIT
:
1093 (menc
== MIMEE_8B
? CONV_8BIT
:
1094 (menc
== MIMEE_QP
? CONV_TOQP
: CONV_TOB64
)));
1098 mime_type_classify_part(struct mimepart
*mpp
) /* FIXME charset=binary ??? */
1100 struct mtlookup mtl
;
1101 enum mimecontent mc
;
1103 union {char const *cp
; ui32_t f
;} mce
;
1108 if ((ct
= mpp
->m_ct_type_plain
) == NULL
) /* TODO may not */
1111 if ((mce
.cp
= ok_vlook(mime_counter_evidence
)) != NULL
) {
1115 ul
= strtoul(mce
.cp
, &eptr
, 0); /* XXX strtol */
1116 if (*mce
.cp
== '\0')
1118 else if (*eptr
!= '\0' || (ui64_t
)ul
>= UI32_MAX
) {
1119 n_err(_("Can't parse *mime-counter-evidence* value: %s\n"), mce
.cp
);
1122 mce
.f
= (ui32_t
)ul
| MIMECE_SET
;
1123 is_os
= !asccasecmp(ct
, "application/octet-stream");
1125 if (mpp
->m_filename
!= NULL
&& (is_os
|| (mce
.f
& MIMECE_ALL_OVWR
))) {
1126 if (_mt_by_filename(&mtl
, mpp
->m_filename
, TRU1
) == NULL
) {
1128 goto jos_content_check
;
1129 } else if (is_os
|| asccasecmp(ct
, mtl
.mtl_result
)) {
1130 if (mce
.f
& MIMECE_ALL_OVWR
)
1131 mpp
->m_ct_type_plain
= ct
= mtl
.mtl_result
;
1132 if (mce
.f
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
1133 mpp
->m_ct_type_usr_ovwr
= ct
= mtl
.mtl_result
;
1140 if (strchr(ct
, '/') == NULL
) /* For compatibility with non-MIME */
1142 else if (is_asccaseprefix(ct
, "text/")) {
1143 ct
+= sizeof("text/") -1;
1144 if (!asccasecmp(ct
, "plain"))
1145 mc
= MIME_TEXT_PLAIN
;
1146 else if (!asccasecmp(ct
, "html"))
1147 mc
= MIME_TEXT_HTML
;
1150 } else if (is_asccaseprefix(ct
, "message/")) {
1151 ct
+= sizeof("message/") -1;
1152 if (!asccasecmp(ct
, "rfc822"))
1156 } else if (!ascncasecmp(ct
, "multipart/", 10)) {
1157 ct
+= sizeof("multipart/") -1;
1158 if (!asccasecmp(ct
, "alternative"))
1159 mc
= MIME_ALTERNATIVE
;
1160 else if (!asccasecmp(ct
, "related"))
1162 else if (!asccasecmp(ct
, "digest"))
1166 } else if (is_asccaseprefix(ct
, "application/")) {
1168 goto jos_content_check
;
1169 ct
+= sizeof("application/") -1;
1170 if (!asccasecmp(ct
, "pkcs7-mime") || !asccasecmp(ct
, "x-pkcs7-mime"))
1178 if ((mce
.f
& MIMECE_BIN_PARSE
) && mpp
->m_mime_enc
!= MIMEE_BIN
&&
1179 mpp
->m_charset
!= NULL
&& asccasecmp(mpp
->m_charset
, "binary"))
1180 mc
= _mt_classify_os_part(mce
.f
, mpp
);
1184 FL
enum mime_handler_flags
1185 mime_type_handler(struct mime_handler
*mhp
, struct mimepart
const *mpp
,
1186 enum sendaction action
)
1189 #define __L (sizeof(__S) -1)
1190 struct mtlookup mtl
;
1192 enum mime_handler_flags rv
;
1193 char const *es
, *cs
, *ccp
;
1197 memset(mhp
, 0, sizeof *mhp
);
1201 if (action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
1202 rv
|= MIME_HDL_ISQUOTE
;
1203 else if (action
!= SEND_TODISP
&& action
!= SEND_TODISP_ALL
)
1206 el
= ((es
= mpp
->m_filename
) != NULL
&& (es
= strrchr(es
, '.')) != NULL
&&
1207 *++es
!= '\0') ? strlen(es
) : 0;
1208 cl
= ((cs
= mpp
->m_ct_type_usr_ovwr
) != NULL
||
1209 (cs
= mpp
->m_ct_type_plain
) != NULL
) ? strlen(cs
) : 0;
1210 if ((l
= n_MAX(el
, cl
)) == 0) {
1211 /* TODO this should be done during parse time! */
1215 /* We don't pass the flags around, so ensure carrier is up-to-date */
1218 buf
= ac_alloc(__L
+ l
+1);
1219 memcpy(buf
, __S
, __L
);
1221 /* File-extension handlers take precedence.
1222 * Yes, we really "fail" here for file extensions which clash MIME types */
1224 memcpy(buf
+ __L
, es
, el
+1);
1225 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1226 *cp
= lowerconv(*cp
);
1228 if ((mhp
->mh_shell_cmd
= ccp
= vok_vlook(buf
)) != NULL
) {
1229 rv
= _mt_pipe_check(mhp
);
1234 /* Then MIME Content-Type:, if any */
1238 memcpy(buf
+ __L
, cs
, cl
+1);
1239 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1240 *cp
= lowerconv(*cp
);
1242 if ((mhp
->mh_shell_cmd
= vok_vlook(buf
)) != NULL
) {
1243 rv
= _mt_pipe_check(mhp
);
1247 if (_mt_by_mtname(&mtl
, cs
) != NULL
)
1248 switch (mtl
.mtl_node
->mt_flags
& __MT_MARKMASK
) {
1249 #ifndef HAVE_FILTER_HTML_TAGSOUP
1254 #ifdef HAVE_FILTER_HTML_TAGSOUP
1256 mhp
->mh_ptf
= &htmlflt_process_main
;
1257 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
=
1258 n_UNCONST(_("Builtin HTML tagsoup filter")));
1259 rv
^= MIME_HDL_NULL
| MIME_HDL_PTF
;
1264 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(_("Plain text")));
1265 rv
^= MIME_HDL_NULL
| MIME_HDL_TEXT
;
1276 if ((rv
&= MIME_HDL_TYPE_MASK
) == MIME_HDL_NULL
)
1277 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
1278 _("[-- No MIME handler installed or none applicable --]")));