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 - 2018 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
= 1u<< 8, /* Via `mimetype' (not struct mtbltin) */
40 _MT_USR
= 1u<< 9, /* VAL_MIME_TYPES_USR */
41 _MT_SYS
= 1u<<10, /* VAL_MIME_TYPES_SYS */
42 _MT_FSPEC
= 1u<<11, /* Loaded via f= *mimetypes-load-control* spec. */
44 a_MT_TM_PLAIN
= 1u<<16, /* Without pipe handler display as text */
45 a_MT_TM_SOUP_h
= 2u<<16, /* Ditto, but HTML tagsoup parser if possible */
46 a_MT_TM_SOUP_H
= 3u<<16, /* HTML tagsoup parser, else NOT plain text */
47 a_MT_TM_QUIET
= 4u<<16, /* No "no mime handler available" message */
48 a_MT__TM_MARKMASK
= 7u<<16
51 enum mime_type_class
{
53 _MT_C_CLEAN
= _MT_C_NONE
, /* Plain RFC 5322 message */
54 _MT_C_DEEP_INSPECT
= 1u<<0, /* Always test all the file */
55 _MT_C_NCTT
= 1u<<1, /* *contenttype == NULL */
56 _MT_C_ISTXT
= 1u<<2, /* *contenttype =~ text\/ */
57 _MT_C_ISTXTCOK
= 1u<<3, /* _ISTXT + *mime-allow-text-controls* */
58 _MT_C_HIGHBIT
= 1u<<4, /* Not 7bit clean */
59 _MT_C_LONGLINES
= 1u<<5, /* MIME_LINELEN_LIMIT exceed. */
60 _MT_C_CTRLCHAR
= 1u<<6, /* Control characters seen */
61 _MT_C_HASNUL
= 1u<<7, /* Contains \0 characters */
62 _MT_C_NOTERMNL
= 1u<<8, /* Lacks a final newline */
63 _MT_C_FROM_
= 1u<<9, /* ^From_ seen */
64 _MT_C_FROM_1STLINE
= 1u<<10, /* From_ line seen */
65 _MT_C_SUGGEST_DONE
= 1u<<16, /* Inspector suggests to stop further parse */
66 _MT_C__1STLINE
= 1u<<17 /* .. */
76 struct mtnode
*mt_next
;
78 ui32_t mt_mtlen
; /* Length of MIME type string, rest thereafter */
79 /* C99 forbids flexible arrays in union, so unfortunately we waste a pointer
80 * that could already store character data here */
87 struct mtnode
const *mtl_node
;
88 char *mtl_result
; /* If requested, salloc()ed MIME type */
94 ssize_t mtca_curlnlen
;
98 enum mime_type_class mtca_mtc
;
100 ui64_t mtca_all_highbit
; /* TODO not yet interpreted */
101 ui64_t mtca_all_bogus
;
104 static struct mtbltin
const _mt_bltin
[] = {
105 #include <gen-mime-types.h>
108 static char const _mt_typnames
[][16] = {
109 "application/", "audio/", "image/",
110 "message/", "multipart/", "text/",
113 n_CTAV(_MT_APPLICATION
== 0 && _MT_AUDIO
== 1 && _MT_IMAGE
== 2 &&
114 _MT_MESSAGE
== 3 && _MT_MULTIPART
== 4 && _MT_TEXT
== 5 &&
118 static bool_t _mt_is_init
;
119 static struct mtnode
*_mt_list
;
121 /* Initialize MIME type list in order */
122 static void _mt_init(void);
123 static bool_t
__mt_load_file(ui32_t orflags
,
124 char const *file
, char **line
, size_t *linesize
);
126 /* Create (prepend) a new MIME type; cmdcalled results in a bit more verbosity
128 static struct mtnode
* _mt_create(bool_t cmdcalled
, ui32_t orflags
,
129 char const *line
, size_t len
);
131 /* Try to find MIME type by X (after zeroing mtlp), return NULL if not found;
132 * if with_result >mtl_result will be created upon success for the former */
133 static struct mtlookup
* _mt_by_filename(struct mtlookup
*mtlp
,
134 char const *name
, bool_t with_result
);
135 static struct mtlookup
* _mt_by_mtname(struct mtlookup
*mtlp
,
138 /* In-depth inspection of raw content: call _round() repeatedly, last time with
139 * a 0 length buffer, finally check .mtca_mtc for result.
140 * No further call is needed if _round() return includes _MT_C_SUGGEST_DONE,
141 * as the resulting classification is unambiguous */
142 n_INLINE
struct mt_class_arg
* _mt_classify_init(struct mt_class_arg
*mtcap
,
143 enum mime_type_class initval
);
144 static enum mime_type_class
_mt_classify_round(struct mt_class_arg
*mtcap
);
146 /* We need an in-depth inspection of an application/octet-stream part */
147 static enum mimecontent
_mt_classify_os_part(ui32_t mce
, struct mimepart
*mpp
,
148 bool_t deep_inspect
);
150 /* Check whether a *pipe-XY* handler is applicable, and adjust flags according
151 * to the defined trigger characters; upon entry MIME_HDL_NULL is set, and that
152 * isn't changed if mhp doesn't apply */
153 static enum mime_handler_flags
a_mt_pipe_check(struct mime_handler
*mhp
);
159 char c
, *line
; /* TODO line pool (below) */
162 char const *srcs_arr
[10], *ccp
, **srcs
;
168 /* Always load our built-ins */
169 for (tail
= NULL
, i
= 0; i
< n_NELEM(_mt_bltin
); ++i
) {
170 struct mtbltin
const *mtbp
= _mt_bltin
+ i
;
171 struct mtnode
*mtnp
= smalloc(sizeof *mtnp
);
174 tail
->mt_next
= mtnp
;
178 mtnp
->mt_next
= NULL
;
179 mtnp
->mt_flags
= mtbp
->mtb_flags
;
180 mtnp
->mt_mtlen
= mtbp
->mtb_mtlen
;
181 mtnp
->mt_line
= mtbp
->mtb_line
;
184 /* Decide which files sources have to be loaded */
185 if ((ccp
= ok_vlook(mimetypes_load_control
)) == NULL
)
187 else if (*ccp
== '\0')
191 srcs
[-1] = srcs
[-2] = NULL
;
193 if (strchr(ccp
, '=') != NULL
) {
196 while ((ccp
= n_strsep(&line
, ',', TRU1
)) != NULL
) {
197 switch ((c
= *ccp
)) {
199 srcs_arr
[1] = VAL_MIME_TYPES_SYS
;
203 srcs_arr
[0] = VAL_MIME_TYPES_USR
;
209 if (*++ccp
== '=' && *++ccp
!= '\0') {
210 if (PTR2SIZE(srcs
- srcs_arr
) < n_NELEM(srcs_arr
))
213 n_err(_("*mimetypes-load-control*: too many sources, "
214 "skipping %s\n"), n_shexp_quote_cp(ccp
, FAL0
));
222 } else for (i
= 0; (c
= ccp
[i
]) != '\0'; ++i
)
224 case 'S': case 's': srcs_arr
[1] = VAL_MIME_TYPES_SYS
; break;
225 case 'U': case 'u': srcs_arr
[0] = VAL_MIME_TYPES_USR
; break;
228 n_err(_("*mimetypes-load-control*: unsupported content: %s\n"), ccp
);
232 /* Load all file-based sources in the desired order */
235 for (j
= 0, i
= (ui32_t
)PTR2SIZE(srcs
- srcs_arr
), srcs
= srcs_arr
;
236 i
> 0; ++j
, ++srcs
, --i
)
239 else if (!__mt_load_file((j
== 0 ? _MT_USR
240 : (j
== 1 ? _MT_SYS
: _MT_FSPEC
)), *srcs
, &line
, &linesize
)) {
241 if ((n_poption
& n_PO_D_V
) || j
> 1)
242 n_err(_("*mimetypes-load-control*: cannot open or load %s\n"),
243 n_shexp_quote_cp(*srcs
, FAL0
));
253 __mt_load_file(ui32_t orflags
, char const *file
, char **line
, size_t *linesize
)
257 struct mtnode
*head
, *tail
, *mtnp
;
261 if ((cp
= fexpand(file
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
||
262 (fp
= Fopen(cp
, "r")) == NULL
) {
267 for (head
= tail
= NULL
; fgetline(line
, linesize
, NULL
, &len
, fp
, 0) != 0;)
268 if ((mtnp
= _mt_create(FAL0
, orflags
, *line
, len
)) != NULL
) {
272 tail
->mt_next
= mtnp
;
276 tail
->mt_next
= _mt_list
;
286 static struct mtnode
*
287 _mt_create(bool_t cmdcalled
, ui32_t orflags
, char const *line
, size_t len
)
290 char const *typ
, *subtyp
;
296 /* Drop anything after a comment first TODO v15: only when read from file */
297 if ((typ
= memchr(line
, '#', len
)) != NULL
)
298 len
= PTR2SIZE(typ
- line
);
300 /* Then trim any trailing whitespace from line (including NL/CR) */
304 work
.s
= n_UNCONST(line
);
306 line
= n_str_trim(&work
, n_STR_TRIM_BOTH
)->s
;
311 /* (But wait - is there a type marker?) */
313 if(!(orflags
& (_MT_USR
| _MT_SYS
)) && *typ
== '@'){
317 orflags
|= a_MT_TM_PLAIN
;
324 else if(len
> 4 && typ
[2] == '@' && typ
[3] == ' '){
325 n_OBSOLETE("`mimetype': the trailing \"@\" in \"type-marker\" "
332 default: goto jeinval
;
333 case 't': orflags
|= a_MT_TM_PLAIN
; break;
334 case 'h': orflags
|= a_MT_TM_SOUP_h
; break;
335 case 'H': orflags
|= a_MT_TM_SOUP_H
; break;
336 case 'q': orflags
|= a_MT_TM_QUIET
; break;
345 while (len
> 0 && !blankchar(*line
))
347 /* Ignore empty lines and even incomplete specifications (only MIME type)
348 * because this is quite common in mime.types(5) files */
349 if (len
== 0 || (tlen
= PTR2SIZE(line
- typ
)) == 0) {
350 if (cmdcalled
|| (orflags
& _MT_FSPEC
)) {
352 line
= _("(no value)");
355 n_err(_("Empty MIME type or no extensions given: %.*s\n"),
361 if ((subtyp
= memchr(typ
, '/', tlen
)) == NULL
|| subtyp
[1] == '\0' ||
362 spacechar(subtyp
[1])) {
364 if(cmdcalled
|| (orflags
& _MT_FSPEC
) || (n_poption
& n_PO_D_V
))
365 n_err(_("%s MIME type: %.*s\n"),
366 (cmdcalled
? _("Invalid") : _("mime.types(5): invalid")),
372 /* Map to mime_type */
373 tlen
= PTR2SIZE(subtyp
- typ
);
374 for (i
= __MT_TMIN
;;) {
375 if (!ascncasecmp(_mt_typnames
[i
], typ
, tlen
)) {
377 tlen
= PTR2SIZE(line
- subtyp
);
381 if (++i
== __MT_TMAX
) {
382 orflags
|= _MT_OTHER
;
383 tlen
= PTR2SIZE(line
- typ
);
388 /* Strip leading whitespace from the list of extensions;
389 * trailing WS has already been trimmed away above.
390 * Be silent on slots which define a mimetype without any value */
391 while (len
> 0 && blankchar(*line
))
397 mtnp
= smalloc(sizeof(*mtnp
) + tlen
+ len
+1);
398 mtnp
->mt_next
= NULL
;
399 mtnp
->mt_flags
= orflags
;
400 mtnp
->mt_mtlen
= (ui32_t
)tlen
;
401 { char *l
= (char*)(mtnp
+ 1);
403 memcpy(l
, typ
, tlen
);
404 memcpy(l
+ tlen
, line
, len
);
414 static struct mtlookup
*
415 _mt_by_filename(struct mtlookup
*mtlp
, char const *name
, bool_t with_result
)
419 char const *ext
, *cp
;
422 memset(mtlp
, 0, sizeof *mtlp
);
424 if ((nlen
= strlen(name
)) == 0) /* TODO name should be a URI */
426 /* We need a period TODO we should support names like README etc. */
427 for (i
= nlen
; name
[--i
] != '.';)
428 if (i
== 0 || name
[i
] == '/') /* XXX no magics */
430 /* While here, basename() it */
431 while (i
> 0 && name
[i
- 1] != '/')
435 mtlp
->mtl_name
= name
;
436 mtlp
->mtl_nlen
= nlen
;
441 /* ..all the MIME types */
442 for (mtnp
= _mt_list
; mtnp
!= NULL
; mtnp
= mtnp
->mt_next
)
443 for (ext
= mtnp
->mt_line
+ mtnp
->mt_mtlen
;; ext
= cp
) {
445 while (whitechar(*cp
))
448 while (!whitechar(*cp
) && *cp
!= '\0')
451 if ((i
= PTR2SIZE(cp
- ext
)) == 0)
453 /* Don't allow neither of ".txt" or "txt" to match "txt" */
454 else if (i
+ 1 >= nlen
|| name
[(j
= nlen
- i
) - 1] != '.' ||
455 ascncasecmp(name
+ j
, ext
, i
))
459 mtlp
->mtl_node
= mtnp
;
464 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
468 name
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
472 mtlp
->mtl_result
= salloc(i
+ j
+1);
474 memcpy(mtlp
->mtl_result
, name
, j
);
475 memcpy(mtlp
->mtl_result
+ j
, mtnp
->mt_line
, i
);
476 mtlp
->mtl_result
[j
+= i
] = '\0';
486 static struct mtlookup
*
487 _mt_by_mtname(struct mtlookup
*mtlp
, char const *mtname
)
494 memset(mtlp
, 0, sizeof *mtlp
);
496 if ((mtlp
->mtl_nlen
= nlen
= strlen(mtlp
->mtl_name
= mtname
)) == 0)
502 /* ..all the MIME types */
503 for (mtnp
= _mt_list
; mtnp
!= NULL
; mtnp
= mtnp
->mt_next
) {
504 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
508 cp
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
513 if (i
+ j
== mtlp
->mtl_nlen
) {
514 char *xmt
= ac_alloc(i
+ j
+1);
517 memcpy(xmt
+ j
, mtnp
->mt_line
, i
);
519 i
= asccasecmp(mtname
, xmt
);
524 mtlp
->mtl_node
= mtnp
;
536 n_INLINE
struct mt_class_arg
*
537 _mt_classify_init(struct mt_class_arg
* mtcap
, enum mime_type_class initval
)
540 memset(mtcap
, 0, sizeof *mtcap
);
541 /*mtcap->mtca_lastc =*/ mtcap
->mtca_c
= EOF
;
542 mtcap
->mtca_mtc
= initval
| _MT_C__1STLINE
;
547 static enum mime_type_class
548 _mt_classify_round(struct mt_class_arg
*mtcap
) /* TODO dig UTF-8 for !text/!! */
550 /* TODO BTW., after the MIME/send layer rewrite we could use a MIME
551 * TODO boundary of "=-=-=" if we would add a B_ in EQ spirit to F_,
552 * TODO and report that state to the outer world */
554 #define F_SIZEOF (sizeof(F_) -1)
555 char f_buf
[F_SIZEOF
], *f_p
= f_buf
;
561 enum mime_type_class mtc
;
564 buf
= mtcap
->mtca_buf
;
565 blen
= mtcap
->mtca_len
;
566 curlnlen
= mtcap
->mtca_curlnlen
;
567 alllen
= mtcap
->mtca_all_len
;
569 /*lastc = mtcap->mtca_lastc;*/
570 mtc
= mtcap
->mtca_mtc
;
572 for (;; ++curlnlen
) {
574 /* Real EOF, or only current buffer end? */
575 if(mtcap
->mtca_len
== 0){
591 if (!(mtc
& _MT_C_ISTXTCOK
)) {
592 mtc
|= _MT_C_SUGGEST_DONE
;
597 if (c
== '\n' || c
== EOF
) {
598 mtc
&= ~_MT_C__1STLINE
;
599 if (curlnlen
>= MIME_LINELEN_LIMIT
)
600 mtc
|= _MT_C_LONGLINES
;
607 /* A bit hairy is handling of \r=\x0D=CR.
609 * Control characters other than TAB, or CR and LF as parts of CRLF
610 * pairs, must not appear. \r alone does not force _CTRLCHAR below since
611 * we cannot peek the next character. Thus right here, inspect the last
612 * seen character for if its \r and set _CTRLCHAR in a delayed fashion */
613 /*else*/ if (lastc
== '\r')
614 mtc
|= _MT_C_CTRLCHAR
;
616 /* Control character? XXX this is all ASCII here */
617 if (c
< 0x20 || c
== 0x7F) {
618 /* RFC 2045, 6.7, as above ... */
619 if (c
!= '\t' && c
!= '\r')
620 mtc
|= _MT_C_CTRLCHAR
;
622 /* If there is a escape sequence in reverse solidus notation defined
623 * for this in ANSI X3.159-1989 (ANSI C89), don't treat it as a control
624 * for real. I.e., \a=\x07=BEL, \b=\x08=BS, \t=\x09=HT. Don't follow
625 * libmagic(1) in respect to \v=\x0B=VT. \f=\x0C=NP; do ignore
627 if ((c
>= '\x07' && c
<= '\x0D') || c
== '\x1B')
630 /* As a special case, if we are going for displaying data to the user
631 * or quoting a message then simply continue this, in the end, in case
632 * we get there, we will decide upon the all_len/all_bogus ratio
633 * whether this is usable plain text or not */
634 ++mtcap
->mtca_all_bogus
;
635 if(mtc
& _MT_C_DEEP_INSPECT
)
638 mtc
|= _MT_C_HASNUL
; /* Force base64 */
639 if (!(mtc
& _MT_C_ISTXTCOK
)) {
640 mtc
|= _MT_C_SUGGEST_DONE
;
643 } else if ((ui8_t
)c
& 0x80) {
644 mtc
|= _MT_C_HIGHBIT
;
645 ++mtcap
->mtca_all_highbit
;
646 if (!(mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))) { /* TODO _NCTT?? */
647 mtc
|= _MT_C_HASNUL
/* Force base64 */ | _MT_C_SUGGEST_DONE
;
650 } else if (!(mtc
& _MT_C_FROM_
) && UICMP(z
, curlnlen
, <, F_SIZEOF
)) {
652 if (UICMP(z
, curlnlen
, ==, F_SIZEOF
- 1) &&
653 PTR2SIZE(f_p
- f_buf
) == F_SIZEOF
&&
654 !memcmp(f_buf
, F_
, F_SIZEOF
)){
656 if (mtc
& _MT_C__1STLINE
)
657 mtc
|= _MT_C_FROM_1STLINE
;
661 if (c
== EOF
&& lastc
!= '\n')
662 mtc
|= _MT_C_NOTERMNL
;
664 mtcap
->mtca_curlnlen
= curlnlen
;
665 /*mtcap->mtca_lastc = lastc*/;
667 mtcap
->mtca_mtc
= mtc
;
668 mtcap
->mtca_all_len
= alllen
;
675 static enum mimecontent
676 _mt_classify_os_part(ui32_t mce
, struct mimepart
*mpp
, bool_t deep_inspect
)
678 struct str in
= {NULL
, 0}, outrest
, inrest
, dec
;
679 struct mt_class_arg mtca
;
681 enum mime_type_class mtc
;
689 assert(mpp
->m_mime_enc
!= MIMEE_BIN
);
691 outrest
= inrest
= dec
= in
;
696 /* TODO v15-compat Note we actually bypass our usual file handling by
697 * TODO directly using fseek() on mb.mb_itf -- the v15 rewrite will change
698 * TODO all of this, and until then doing it like this is the only option
699 * TODO to integrate nicely into whoever calls us */
700 start_off
= ftell(mb
.mb_itf
);
701 if ((ibuf
= setinput(&mb
, (struct message
*)mpp
, NEED_BODY
)) == NULL
) {
703 fseek(mb
.mb_itf
, start_off
, SEEK_SET
);
708 /* Skip part headers */
709 for (lc
= '\0'; cnt
> 0; lc
= c
, --cnt
)
710 if ((c
= getc(ibuf
)) == EOF
|| (c
== '\n' && lc
== '\n'))
712 if (cnt
== 0 || ferror(ibuf
))
715 /* So now let's inspect the part content, decoding content-transfer-encoding
716 * along the way TODO this should simply be "mime_factory_create(MPP)"!
717 * TODO In fact m_mime_classifier_(setup|call|call_part|finalize)() and the
718 * TODO state(s) (the _MT_C states) should become reported to the outer
719 * TODO world like that (see MIME boundary TODO around here) */
720 _mt_classify_init(&mtca
, (_MT_C_ISTXT
|
721 (deep_inspect
? _MT_C_DEEP_INSPECT
: _MT_C_NONE
)));
726 c
= (--cnt
== 0) ? EOF
: getc(ibuf
);
727 if ((dobuf
= (c
== '\n'))) {
728 /* Ignore empty lines */
731 } else if ((dobuf
= (c
== EOF
))) {
732 if (lsz
== 0 && outrest
.l
== 0)
737 in
.s
= srealloc(in
.s
, lsz
+= LINESIZE
);
739 in
.s
[in
.l
++] = (char)c
;
744 switch (mpp
->m_mime_enc
) {
746 if (!b64_decode_part(&dec
, &in
, &outrest
,
747 (did_inrest
? NULL
: &inrest
))) {
748 mtca
.mtca_mtc
= _MT_C_HASNUL
;
749 goto jstopit
; /* break;break; */
754 if (!qp_decode_part(&dec
, &in
, &outrest
, &inrest
)) {
755 mtca
.mtca_mtc
= _MT_C_HASNUL
;
756 goto jstopit
; /* break;break; */
758 if (dec
.l
== 0 && c
!= EOF
) {
764 /* Temporarily switch those two buffers.. */
771 mtca
.mtca_buf
= dec
.s
;
772 mtca
.mtca_len
= (ssize_t
)dec
.l
;
773 if ((mtc
= _mt_classify_round(&mtca
)) & _MT_C_SUGGEST_DONE
) {
780 /* ..and restore switched */
788 if ((in
.l
= inrest
.l
) > 0) {
801 if (outrest
.s
!= NULL
)
803 if (inrest
.s
!= NULL
)
806 fseek(mb
.mb_itf
, start_off
, SEEK_SET
);
808 if (!(mtc
& (_MT_C_HASNUL
/*| _MT_C_CTRLCHAR XXX really? */))) {
809 /* In that special relaxed case we may very well wave through
810 * octet-streams full of control characters, as they do no harm
811 * TODO This should be part of m_mime_classifier_finalize() then! */
813 mtca
.mtca_all_len
- mtca
.mtca_all_bogus
< mtca
.mtca_all_len
>> 2)
816 mc
= MIME_TEXT_PLAIN
;
817 if (mce
& MIMECE_ALL_OVWR
)
818 mpp
->m_ct_type_plain
= "text/plain";
819 if (mce
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
820 mpp
->m_ct_type_usr_ovwr
= "text/plain";
827 static enum mime_handler_flags
828 a_mt_pipe_check(struct mime_handler
*mhp
){
829 enum mime_handler_flags rv_orig
, rv
;
833 rv_orig
= rv
= mhp
->mh_flags
;
835 /* Do we have any handler for this part? */
836 if(*(cp
= mhp
->mh_shell_cmd
) == '\0')
838 else if(*cp
++ != '@'){
841 }else if(*cp
== '\0'){
848 case '*': rv
|= MIME_HDL_COPIOUSOUTPUT
; ++cp
; goto jnextc
;
849 case '#': rv
|= MIME_HDL_NOQUOTE
; ++cp
; goto jnextc
;
850 case '&': rv
|= MIME_HDL_ASYNC
; ++cp
; goto jnextc
;
851 case '!': rv
|= MIME_HDL_NEEDSTERM
; ++cp
; goto jnextc
;
853 if(rv
& MIME_HDL_TMPF
)
854 rv
|= MIME_HDL_TMPF_UNLINK
;
859 rv
|= MIME_HDL_TMPF_FILL
;
868 mhp
->mh_shell_cmd
= cp
;
871 if(rv
& MIME_HDL_TMPF_FILL
)
875 if(rv
& MIME_HDL_ISQUOTE
){
876 if(rv
& MIME_HDL_NOQUOTE
)
879 /* Cannot fetch data back from asynchronous process */
880 if(rv
& MIME_HDL_ASYNC
)
883 /* TODO Can't use a "needsterminal" program for quoting */
884 if(rv
& MIME_HDL_NEEDSTERM
)
888 if(rv
& MIME_HDL_NEEDSTERM
){
889 if(rv
& MIME_HDL_COPIOUSOUTPUT
){
890 n_err(_("MIME type handlers: cannot use needsterminal and "
891 "copiousoutput together\n"));
894 if(rv
& MIME_HDL_ASYNC
){
895 n_err(_("MIME type handlers: cannot use needsterminal and "
896 "x-mailx-async together\n"));
900 /* needsterminal needs a terminal */
901 if(!(n_psonce
& n_PSO_INTERACTIVE
))
905 if(rv
& MIME_HDL_ASYNC
){
906 if(rv
& MIME_HDL_COPIOUSOUTPUT
){
907 n_err(_("MIME type handlers: cannot use x-mailx-async and "
908 "copiousoutput together\n"));
911 if(rv
& MIME_HDL_TMPF_UNLINK
){
912 n_err(_("MIME type handlers: cannot use x-mailx-async and "
913 "x-mailx-tmpfile-unlink together\n"));
918 /* TODO mailcap-only: TMPF_UNLINK): needs -tmpfile OR -tmpfile-fill */
932 struct n_string s
, *sp
;
940 sp
= n_string_creat_auto(&s
);
942 if(*(argv
= v
) == NULL
){
946 if(_mt_list
== NULL
){
947 fprintf(n_stdout
, _("# `mimetype': no mime.types(5) available\n"));
951 if((fp
= Ftmp(NULL
, "mimetype", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)
953 n_perr(_("tmpfile"), 0);
958 sp
= n_string_reserve(sp
, 63);
960 for(l
= 0, mtnp
= _mt_list
; mtnp
!= NULL
; ++l
, mtnp
= mtnp
->mt_next
){
963 sp
= n_string_trunc(sp
, 0);
965 switch(mtnp
->mt_flags
& a_MT__TM_MARKMASK
){
966 case a_MT_TM_PLAIN
: cp
= "@t "; break;
967 case a_MT_TM_SOUP_h
: cp
= "@h "; break;
968 case a_MT_TM_SOUP_H
: cp
= "@H "; break;
969 case a_MT_TM_QUIET
: cp
= "@q "; break;
970 default: cp
= NULL
; break;
973 sp
= n_string_push_cp(sp
, cp
);
975 if((mtnp
->mt_flags
& __MT_TMASK
) != _MT_OTHER
)
976 sp
= n_string_push_cp(sp
, _mt_typnames
[mtnp
->mt_flags
&__MT_TMASK
]);
978 sp
= n_string_push_buf(sp
, mtnp
->mt_line
, mtnp
->mt_mtlen
);
979 sp
= n_string_push_c(sp
, ' ');
980 sp
= n_string_push_c(sp
, ' ');
981 sp
= n_string_push_cp(sp
, &mtnp
->mt_line
[mtnp
->mt_mtlen
]);
983 fprintf(fp
, "wysh mimetype %s%s\n", n_string_cp(sp
),
984 ((n_poption
& n_PO_D_V
) == 0 ? n_empty
985 : (mtnp
->mt_flags
& _MT_USR
? " # user"
986 : (mtnp
->mt_flags
& _MT_SYS
? " # system"
987 : (mtnp
->mt_flags
& _MT_FSPEC
? " # f= file"
988 : (mtnp
->mt_flags
& _MT_CMD
? " # command" : " # built-in"))))));
991 page_or_print(fp
, l
);
994 for(; *argv
!= NULL
; ++argv
){
996 sp
= n_string_push_c(sp
, ' ');
997 sp
= n_string_push_cp(sp
, *argv
);
1000 mtnp
= _mt_create(TRU1
, _MT_CMD
, n_string_cp(sp
), sp
->s_len
);
1002 mtnp
->mt_next
= _mt_list
;
1009 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1013 c_unmimetype(void *v
)
1016 struct mtnode
*lnp
, *mtnp
;
1020 /* Need to load that first as necessary */
1024 for (; *argv
!= NULL
; ++argv
) {
1025 if (!asccasecmp(*argv
, "reset")) {
1030 if (argv
[0][0] == '*' && argv
[0][1] == '\0') {
1032 while ((mtnp
= _mt_list
) != NULL
) {
1033 _mt_list
= mtnp
->mt_next
;
1039 for (match
= FAL0
, lnp
= NULL
, mtnp
= _mt_list
; mtnp
!= NULL
;) {
1044 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
1048 typ
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
1052 val
= ac_alloc(i
+ mtnp
->mt_mtlen
+1);
1053 memcpy(val
, typ
, i
);
1054 memcpy(val
+ i
, mtnp
->mt_line
, mtnp
->mt_mtlen
);
1055 val
[i
+= mtnp
->mt_mtlen
] = '\0';
1056 i
= asccasecmp(val
, *argv
);
1060 struct mtnode
*nnp
= mtnp
->mt_next
;
1069 lnp
= mtnp
, mtnp
= mtnp
->mt_next
;
1072 if (!(n_pstate
& n_PS_ROBOT
) || (n_poption
& n_PO_D_V
))
1073 n_err(_("No such MIME type: %s\n"), *argv
);
1078 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1082 n_mimetype_check_mtname(char const *name
)
1084 struct mtlookup mtl
;
1088 rv
= (_mt_by_mtname(&mtl
, name
) != NULL
);
1094 n_mimetype_classify_filename(char const *name
)
1096 struct mtlookup mtl
;
1099 _mt_by_filename(&mtl
, name
, TRU1
);
1101 return mtl
.mtl_result
;
1105 n_mimetype_classify_file(FILE *fp
, char const **contenttype
,
1106 char const **charset
, int *do_iconv
)
1108 /* TODO classify once only PLEASE PLEASE PLEASE */
1109 /* TODO message/rfc822 is special in that it may only be 7bit, 8bit or
1110 * TODO binary according to RFC 2046, 5.2.1
1111 * TODO The handling of which is a hack */
1113 enum mime_type_class mtc
;
1119 assert(ftell(fp
) == 0x0l
);
1123 if (*contenttype
== NULL
) {
1126 } else if (!ascncasecmp(*contenttype
, "text/", 5)) {
1127 mtc
= ok_blook(mime_allow_text_controls
)
1128 ? _MT_C_ISTXT
| _MT_C_ISTXTCOK
: _MT_C_ISTXT
;
1130 } else if (!asccasecmp(*contenttype
, "message/rfc822")) {
1138 menc
= mime_enc_target();
1140 if ((fpsz
= fsize(fp
)) == 0)
1143 char buf
[BUFFER_SIZE
];
1144 struct mt_class_arg mtca
;
1146 _mt_classify_init(&mtca
, mtc
);
1148 mtca
.mtca_len
= fread(buf
, sizeof(buf
[0]), n_NELEM(buf
), fp
);
1149 mtca
.mtca_buf
= buf
;
1150 if ((mtc
= _mt_classify_round(&mtca
)) & _MT_C_SUGGEST_DONE
)
1152 if (mtca
.mtca_len
== 0)
1155 /* TODO ferror(fp) ! */
1159 if (mtc
& _MT_C_HASNUL
) {
1161 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1162 * on request; else enforce what file(1)/libmagic(3) would suggest */
1163 if (mtc
& _MT_C_ISTXTCOK
)
1165 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1166 *contenttype
= "application/octet-stream";
1171 (_MT_C_LONGLINES
| _MT_C_CTRLCHAR
| _MT_C_NOTERMNL
| _MT_C_FROM_
)) {
1172 if (menc
!= MIMEE_B64
)
1176 if (mtc
& _MT_C_HIGHBIT
) {
1178 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1179 *do_iconv
= ((mtc
& _MT_C_HIGHBIT
) != 0);
1183 if (mtc
& _MT_C_NCTT
)
1184 *contenttype
= "text/plain";
1186 /* Not an attachment with specified charset? */
1188 if (*charset
== NULL
) /* TODO MIME/send: iter active? iter! else */
1189 *charset
= (mtc
& _MT_C_HIGHBIT
) ? charset_iter_or_fallback()
1190 : ok_vlook(charset_7bit
);
1192 /* TODO mime_type_file_classify() shouldn't return conversion */
1194 if (mtc
& _MT_C_FROM_1STLINE
) {
1195 n_err(_("Pre-v15 %s cannot handle message/rfc822 that "
1196 "indeed is a RFC 4155 MBOX!\n"
1197 " Forcing a content-type of application/mbox!\n"),
1199 *contenttype
= "application/mbox";
1202 c
= (menc
== MIMEE_7B
? CONV_7BIT
1203 : (menc
== MIMEE_8B
? CONV_8BIT
1204 /* May have only 7-bit, 8-bit and binary. Try to avoid latter */
1205 : ((mtc
& _MT_C_HASNUL
) ? CONV_NONE
1206 : ((mtc
& _MT_C_HIGHBIT
) ? CONV_8BIT
: CONV_7BIT
))));
1209 c
= (menc
== MIMEE_7B
? CONV_7BIT
1210 : (menc
== MIMEE_8B
? CONV_8BIT
1211 : (menc
== MIMEE_QP
? CONV_TOQP
: CONV_TOB64
)));
1217 n_mimetype_classify_part(struct mimepart
*mpp
, bool_t for_user_context
){
1218 /* TODO n_mimetype_classify_part() <-> m_mime_classifier_ with life cycle */
1219 struct mtlookup mtl
;
1220 enum mimecontent mc
;
1222 union {char const *cp
; ui32_t f
;} mce
;
1227 if ((ct
= mpp
->m_ct_type_plain
) == NULL
) /* TODO may not */
1230 if((mce
.cp
= ok_vlook(mime_counter_evidence
)) != NULL
&& *mce
.cp
!= '\0'){
1231 if((n_idec_ui32_cp(&mce
.f
, mce
.cp
, 0, NULL
1232 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1233 ) != n_IDEC_STATE_CONSUMED
){
1234 n_err(_("Invalid *mime-counter-evidence* value content\n"));
1237 mce
.f
|= MIMECE_SET
;
1238 is_os
= !asccasecmp(ct
, "application/octet-stream");
1240 if(mpp
->m_filename
!= NULL
&& (is_os
|| (mce
.f
& MIMECE_ALL_OVWR
))){
1241 if(_mt_by_filename(&mtl
, mpp
->m_filename
, TRU1
) == NULL
){
1243 goto jos_content_check
;
1244 }else if(is_os
|| asccasecmp(ct
, mtl
.mtl_result
)){
1245 if(mce
.f
& MIMECE_ALL_OVWR
)
1246 mpp
->m_ct_type_plain
= ct
= mtl
.mtl_result
;
1247 if(mce
.f
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
1248 mpp
->m_ct_type_usr_ovwr
= ct
= mtl
.mtl_result
;
1255 if(*ct
== '\0' || strchr(ct
, '/') == NULL
) /* For compat with non-MIME */
1257 else if(is_asccaseprefix("text/", ct
)){
1258 ct
+= sizeof("text/") -1;
1259 if(!asccasecmp(ct
, "plain"))
1260 mc
= MIME_TEXT_PLAIN
;
1261 else if(!asccasecmp(ct
, "html"))
1262 mc
= MIME_TEXT_HTML
;
1265 }else if(is_asccaseprefix("message/", ct
)){
1266 ct
+= sizeof("message/") -1;
1267 if(!asccasecmp(ct
, "rfc822"))
1271 }else if(is_asccaseprefix("multipart/", ct
)){
1274 enum mimecontent mt_mc
;
1276 {"alternative\0", MIME_ALTERNATIVE
},
1277 {"related", MIME_RELATED
},
1278 {"digest", MIME_DIGEST
},
1279 {"signed", MIME_SIGNED
},
1280 {"encrypted", MIME_ENCRYPTED
}
1283 for(ct
+= sizeof("multipart/") -1, mtap
= mta
;;)
1284 if(!asccasecmp(ct
, mtap
->mt_name
)){
1287 }else if(++mtap
== mta
+ n_NELEM(mta
)){
1291 }else if(is_asccaseprefix("application/", ct
)){
1293 goto jos_content_check
;
1294 ct
+= sizeof("application/") -1;
1295 if(!asccasecmp(ct
, "pkcs7-mime") || !asccasecmp(ct
, "x-pkcs7-mime"))
1303 if((mce
.f
& MIMECE_BIN_PARSE
) && mpp
->m_mime_enc
!= MIMEE_BIN
&&
1304 mpp
->m_charset
!= NULL
)
1305 mc
= _mt_classify_os_part(mce
.f
, mpp
, for_user_context
);
1309 FL
enum mime_handler_flags
1310 n_mimetype_handler(struct mime_handler
*mhp
, struct mimepart
const *mpp
,
1311 enum sendaction action
)
1314 #define __L (sizeof(__S) -1)
1315 struct mtlookup mtl
;
1317 enum mime_handler_flags rv
, xrv
;
1318 char const *es
, *cs
, *ccp
;
1322 memset(mhp
, 0, sizeof *mhp
);
1326 if (action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
1327 rv
|= MIME_HDL_ISQUOTE
;
1328 else if (action
!= SEND_TODISP
&& action
!= SEND_TODISP_ALL
&&
1329 action
!= SEND_TODISP_PARTS
)
1332 el
= ((es
= mpp
->m_filename
) != NULL
&& (es
= strrchr(es
, '.')) != NULL
&&
1333 *++es
!= '\0') ? strlen(es
) : 0;
1334 cl
= ((cs
= mpp
->m_ct_type_usr_ovwr
) != NULL
||
1335 (cs
= mpp
->m_ct_type_plain
) != NULL
) ? strlen(cs
) : 0;
1336 if ((l
= n_MAX(el
, cl
)) == 0) {
1337 /* TODO this should be done during parse time! */
1341 /* We don't pass the flags around, so ensure carrier is up-to-date */
1344 buf
= n_lofi_alloc(__L
+ l
+1);
1345 memcpy(buf
, __S
, __L
);
1347 /* File-extension handlers take precedence.
1348 * Yes, we really "fail" here for file extensions which clash MIME types */
1350 memcpy(buf
+ __L
, es
, el
+1);
1351 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1352 *cp
= lowerconv(*cp
);
1354 if ((mhp
->mh_shell_cmd
= ccp
= n_var_vlook(buf
, FAL0
)) != NULL
) {
1355 rv
= a_mt_pipe_check(mhp
);
1360 /* Then MIME Content-Type:, if any */
1364 memcpy(buf
+ __L
, cs
, cl
+1);
1365 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1366 *cp
= lowerconv(*cp
);
1368 if ((mhp
->mh_shell_cmd
= n_var_vlook(buf
, FAL0
)) != NULL
) {
1369 rv
= a_mt_pipe_check(mhp
);
1373 if (_mt_by_mtname(&mtl
, cs
) != NULL
)
1374 switch (mtl
.mtl_node
->mt_flags
& a_MT__TM_MARKMASK
) {
1375 #ifndef HAVE_FILTER_HTML_TAGSOUP
1376 case a_MT_TM_SOUP_H
:
1379 case a_MT_TM_SOUP_h
:
1380 #ifdef HAVE_FILTER_HTML_TAGSOUP
1381 case a_MT_TM_SOUP_H
:
1382 mhp
->mh_ptf
= &htmlflt_process_main
;
1383 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
=
1384 n_UNCONST(_("Built-in HTML tagsoup filter")));
1385 rv
^= MIME_HDL_NULL
| MIME_HDL_PTF
;
1390 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(_("Plain text")));
1391 rv
^= MIME_HDL_NULL
| MIME_HDL_TEXT
;
1395 mhp
->mh_msg
.s
= n_UNCONST(n_empty
);
1406 if((rv
&= MIME_HDL_TYPE_MASK
) == MIME_HDL_NULL
){
1407 if(mhp
->mh_msg
.s
== NULL
)
1408 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
1409 A_("[-- No MIME handler installed, or not applicable --]\n")));
1410 }else if(rv
== MIME_HDL_CMD
&& !(xrv
& MIME_HDL_COPIOUSOUTPUT
) &&
1411 action
!= SEND_TODISP_PARTS
){
1412 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
1413 _("[-- Use the command `mimeview' to display this --]\n")));
1414 xrv
&= ~MIME_HDL_TYPE_MASK
;
1415 xrv
|= (rv
= MIME_HDL_MSG
);
1417 mhp
->mh_flags
= xrv
;