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) {
800 if (outrest
.s
!= NULL
)
802 if (inrest
.s
!= NULL
)
805 fseek(mb
.mb_itf
, start_off
, SEEK_SET
);
807 if (!(mtc
& (_MT_C_HASNUL
/*| _MT_C_CTRLCHAR XXX really? */))) {
808 /* In that special relaxed case we may very well wave through
809 * octet-streams full of control characters, as they do no harm
810 * TODO This should be part of m_mime_classifier_finalize() then! */
812 mtca
.mtca_all_len
- mtca
.mtca_all_bogus
< mtca
.mtca_all_len
>> 2)
815 mc
= MIME_TEXT_PLAIN
;
816 if (mce
& MIMECE_ALL_OVWR
)
817 mpp
->m_ct_type_plain
= "text/plain";
818 if (mce
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
819 mpp
->m_ct_type_usr_ovwr
= "text/plain";
826 static enum mime_handler_flags
827 a_mt_pipe_check(struct mime_handler
*mhp
){
828 enum mime_handler_flags rv_orig
, rv
;
832 rv_orig
= rv
= mhp
->mh_flags
;
834 /* Do we have any handler for this part? */
835 if(*(cp
= mhp
->mh_shell_cmd
) == '\0')
837 else if(*cp
++ != '@'){
840 }else if(*cp
== '\0'){
847 case '*': rv
|= MIME_HDL_COPIOUSOUTPUT
; ++cp
; goto jnextc
;
848 case '#': rv
|= MIME_HDL_NOQUOTE
; ++cp
; goto jnextc
;
849 case '&': rv
|= MIME_HDL_ASYNC
; ++cp
; goto jnextc
;
850 case '!': rv
|= MIME_HDL_NEEDSTERM
; ++cp
; goto jnextc
;
852 if(rv
& MIME_HDL_TMPF
)
853 rv
|= MIME_HDL_TMPF_UNLINK
;
858 rv
|= MIME_HDL_TMPF_FILL
;
867 mhp
->mh_shell_cmd
= cp
;
870 if(rv
& MIME_HDL_TMPF_FILL
)
874 if(rv
& MIME_HDL_ISQUOTE
){
875 if(rv
& MIME_HDL_NOQUOTE
)
878 /* Cannot fetch data back from asynchronous process */
879 if(rv
& MIME_HDL_ASYNC
)
882 /* TODO Can't use a "needsterminal" program for quoting */
883 if(rv
& MIME_HDL_NEEDSTERM
)
887 if(rv
& MIME_HDL_NEEDSTERM
){
888 if(rv
& MIME_HDL_COPIOUSOUTPUT
){
889 n_err(_("MIME type handlers: cannot use needsterminal and "
890 "copiousoutput together\n"));
893 if(rv
& MIME_HDL_ASYNC
){
894 n_err(_("MIME type handlers: cannot use needsterminal and "
895 "x-mailx-async together\n"));
899 /* needsterminal needs a terminal */
900 if(!(n_psonce
& n_PSO_INTERACTIVE
))
904 if(rv
& MIME_HDL_ASYNC
){
905 if(rv
& MIME_HDL_COPIOUSOUTPUT
){
906 n_err(_("MIME type handlers: cannot use x-mailx-async and "
907 "copiousoutput together\n"));
910 if(rv
& MIME_HDL_TMPF_UNLINK
){
911 n_err(_("MIME type handlers: cannot use x-mailx-async and "
912 "x-mailx-tmpfile-unlink together\n"));
917 /* TODO mailcap-only: TMPF_UNLINK): needs -tmpfile OR -tmpfile-fill */
931 struct n_string s
, *sp
;
939 sp
= n_string_creat_auto(&s
);
941 if(*(argv
= v
) == NULL
){
945 if(_mt_list
== NULL
){
946 fprintf(n_stdout
, _("# `mimetype': no mime.types(5) available\n"));
950 if((fp
= Ftmp(NULL
, "mimetype", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)
952 n_perr(_("tmpfile"), 0);
957 sp
= n_string_reserve(sp
, 63);
959 for(l
= 0, mtnp
= _mt_list
; mtnp
!= NULL
; ++l
, mtnp
= mtnp
->mt_next
){
962 sp
= n_string_trunc(sp
, 0);
964 switch(mtnp
->mt_flags
& a_MT__TM_MARKMASK
){
965 case a_MT_TM_PLAIN
: cp
= "@t "; break;
966 case a_MT_TM_SOUP_h
: cp
= "@h "; break;
967 case a_MT_TM_SOUP_H
: cp
= "@H "; break;
968 case a_MT_TM_QUIET
: cp
= "@q "; break;
969 default: cp
= NULL
; break;
972 sp
= n_string_push_cp(sp
, cp
);
974 if((mtnp
->mt_flags
& __MT_TMASK
) != _MT_OTHER
)
975 sp
= n_string_push_cp(sp
, _mt_typnames
[mtnp
->mt_flags
&__MT_TMASK
]);
977 sp
= n_string_push_buf(sp
, mtnp
->mt_line
, mtnp
->mt_mtlen
);
978 sp
= n_string_push_c(sp
, ' ');
979 sp
= n_string_push_c(sp
, ' ');
980 sp
= n_string_push_cp(sp
, &mtnp
->mt_line
[mtnp
->mt_mtlen
]);
982 fprintf(fp
, "wysh mimetype %s%s\n", n_string_cp(sp
),
983 ((n_poption
& n_PO_D_V
) == 0 ? n_empty
984 : (mtnp
->mt_flags
& _MT_USR
? " # user"
985 : (mtnp
->mt_flags
& _MT_SYS
? " # system"
986 : (mtnp
->mt_flags
& _MT_FSPEC
? " # f= file"
987 : (mtnp
->mt_flags
& _MT_CMD
? " # command" : " # built-in"))))));
990 page_or_print(fp
, l
);
993 for(; *argv
!= NULL
; ++argv
){
995 sp
= n_string_push_c(sp
, ' ');
996 sp
= n_string_push_cp(sp
, *argv
);
999 mtnp
= _mt_create(TRU1
, _MT_CMD
, n_string_cp(sp
), sp
->s_len
);
1001 mtnp
->mt_next
= _mt_list
;
1008 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1012 c_unmimetype(void *v
)
1015 struct mtnode
*lnp
, *mtnp
;
1019 /* Need to load that first as necessary */
1023 for (; *argv
!= NULL
; ++argv
) {
1024 if (!asccasecmp(*argv
, "reset")) {
1029 if (argv
[0][0] == '*' && argv
[0][1] == '\0') {
1031 while ((mtnp
= _mt_list
) != NULL
) {
1032 _mt_list
= mtnp
->mt_next
;
1038 for (match
= FAL0
, lnp
= NULL
, mtnp
= _mt_list
; mtnp
!= NULL
;) {
1043 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
1047 typ
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
1051 val
= ac_alloc(i
+ mtnp
->mt_mtlen
+1);
1052 memcpy(val
, typ
, i
);
1053 memcpy(val
+ i
, mtnp
->mt_line
, mtnp
->mt_mtlen
);
1054 val
[i
+= mtnp
->mt_mtlen
] = '\0';
1055 i
= asccasecmp(val
, *argv
);
1059 struct mtnode
*nnp
= mtnp
->mt_next
;
1068 lnp
= mtnp
, mtnp
= mtnp
->mt_next
;
1071 if (!(n_pstate
& n_PS_ROBOT
) || (n_poption
& n_PO_D_V
))
1072 n_err(_("No such MIME type: %s\n"), *argv
);
1077 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1081 n_mimetype_check_mtname(char const *name
)
1083 struct mtlookup mtl
;
1087 rv
= (_mt_by_mtname(&mtl
, name
) != NULL
);
1093 n_mimetype_classify_filename(char const *name
)
1095 struct mtlookup mtl
;
1098 _mt_by_filename(&mtl
, name
, TRU1
);
1100 return mtl
.mtl_result
;
1104 n_mimetype_classify_file(FILE *fp
, char const **contenttype
,
1105 char const **charset
, int *do_iconv
)
1107 /* TODO classify once only PLEASE PLEASE PLEASE */
1108 /* TODO message/rfc822 is special in that it may only be 7bit, 8bit or
1109 * TODO binary according to RFC 2046, 5.2.1
1110 * TODO The handling of which is a hack */
1112 enum mime_type_class mtc
;
1118 assert(ftell(fp
) == 0x0l
);
1122 if (*contenttype
== NULL
) {
1125 } else if (!ascncasecmp(*contenttype
, "text/", 5)) {
1126 mtc
= ok_blook(mime_allow_text_controls
)
1127 ? _MT_C_ISTXT
| _MT_C_ISTXTCOK
: _MT_C_ISTXT
;
1129 } else if (!asccasecmp(*contenttype
, "message/rfc822")) {
1137 menc
= mime_enc_target();
1139 if ((fpsz
= fsize(fp
)) == 0)
1142 char buf
[BUFFER_SIZE
];
1143 struct mt_class_arg mtca
;
1145 _mt_classify_init(&mtca
, mtc
);
1147 mtca
.mtca_len
= fread(buf
, sizeof(buf
[0]), n_NELEM(buf
), fp
);
1148 mtca
.mtca_buf
= buf
;
1149 if ((mtc
= _mt_classify_round(&mtca
)) & _MT_C_SUGGEST_DONE
)
1151 if (mtca
.mtca_len
== 0)
1154 /* TODO ferror(fp) ! */
1158 if (mtc
& _MT_C_HASNUL
) {
1160 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1161 * on request; else enforce what file(1)/libmagic(3) would suggest */
1162 if (mtc
& _MT_C_ISTXTCOK
)
1164 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1165 *contenttype
= "application/octet-stream";
1170 (_MT_C_LONGLINES
| _MT_C_CTRLCHAR
| _MT_C_NOTERMNL
| _MT_C_FROM_
)) {
1171 if (menc
!= MIMEE_B64
)
1175 if (mtc
& _MT_C_HIGHBIT
) {
1177 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1178 *do_iconv
= ((mtc
& _MT_C_HIGHBIT
) != 0);
1182 if (mtc
& _MT_C_NCTT
)
1183 *contenttype
= "text/plain";
1185 /* Not an attachment with specified charset? */
1187 if (*charset
== NULL
) /* TODO MIME/send: iter active? iter! else */
1188 *charset
= (mtc
& _MT_C_HIGHBIT
) ? charset_iter_or_fallback()
1189 : ok_vlook(charset_7bit
);
1191 /* TODO mime_type_file_classify() shouldn't return conversion */
1193 if (mtc
& _MT_C_FROM_1STLINE
) {
1194 n_err(_("Pre-v15 %s cannot handle message/rfc822 that "
1195 "indeed is a RFC 4155 MBOX!\n"
1196 " Forcing a content-type of application/mbox!\n"),
1198 *contenttype
= "application/mbox";
1201 c
= (menc
== MIMEE_7B
? CONV_7BIT
1202 : (menc
== MIMEE_8B
? CONV_8BIT
1203 /* May have only 7-bit, 8-bit and binary. Try to avoid latter */
1204 : ((mtc
& _MT_C_HASNUL
) ? CONV_NONE
1205 : ((mtc
& _MT_C_HIGHBIT
) ? CONV_8BIT
: CONV_7BIT
))));
1208 c
= (menc
== MIMEE_7B
? CONV_7BIT
1209 : (menc
== MIMEE_8B
? CONV_8BIT
1210 : (menc
== MIMEE_QP
? CONV_TOQP
: CONV_TOB64
)));
1216 n_mimetype_classify_part(struct mimepart
*mpp
, bool_t for_user_context
){
1217 /* TODO n_mimetype_classify_part() <-> m_mime_classifier_ with life cycle */
1218 struct mtlookup mtl
;
1219 enum mimecontent mc
;
1221 union {char const *cp
; ui32_t f
;} mce
;
1226 if ((ct
= mpp
->m_ct_type_plain
) == NULL
) /* TODO may not */
1229 if((mce
.cp
= ok_vlook(mime_counter_evidence
)) != NULL
&& *mce
.cp
!= '\0'){
1230 if((n_idec_ui32_cp(&mce
.f
, mce
.cp
, 0, NULL
1231 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1232 ) != n_IDEC_STATE_CONSUMED
){
1233 n_err(_("Invalid *mime-counter-evidence* value content\n"));
1236 mce
.f
|= MIMECE_SET
;
1237 is_os
= !asccasecmp(ct
, "application/octet-stream");
1239 if(mpp
->m_filename
!= NULL
&& (is_os
|| (mce
.f
& MIMECE_ALL_OVWR
))){
1240 if(_mt_by_filename(&mtl
, mpp
->m_filename
, TRU1
) == NULL
){
1242 goto jos_content_check
;
1243 }else if(is_os
|| asccasecmp(ct
, mtl
.mtl_result
)){
1244 if(mce
.f
& MIMECE_ALL_OVWR
)
1245 mpp
->m_ct_type_plain
= ct
= mtl
.mtl_result
;
1246 if(mce
.f
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
1247 mpp
->m_ct_type_usr_ovwr
= ct
= mtl
.mtl_result
;
1254 if(*ct
== '\0' || strchr(ct
, '/') == NULL
) /* For compat with non-MIME */
1256 else if(is_asccaseprefix("text/", ct
)){
1257 ct
+= sizeof("text/") -1;
1258 if(!asccasecmp(ct
, "plain"))
1259 mc
= MIME_TEXT_PLAIN
;
1260 else if(!asccasecmp(ct
, "html"))
1261 mc
= MIME_TEXT_HTML
;
1264 }else if(is_asccaseprefix("message/", ct
)){
1265 ct
+= sizeof("message/") -1;
1266 if(!asccasecmp(ct
, "rfc822"))
1270 }else if(is_asccaseprefix("multipart/", ct
)){
1273 enum mimecontent mt_mc
;
1275 {"alternative\0", MIME_ALTERNATIVE
},
1276 {"related", MIME_RELATED
},
1277 {"digest", MIME_DIGEST
},
1278 {"signed", MIME_SIGNED
},
1279 {"encrypted", MIME_ENCRYPTED
}
1282 for(ct
+= sizeof("multipart/") -1, mtap
= mta
;;)
1283 if(!asccasecmp(ct
, mtap
->mt_name
)){
1286 }else if(++mtap
== mta
+ n_NELEM(mta
)){
1290 }else if(is_asccaseprefix("application/", ct
)){
1292 goto jos_content_check
;
1293 ct
+= sizeof("application/") -1;
1294 if(!asccasecmp(ct
, "pkcs7-mime") || !asccasecmp(ct
, "x-pkcs7-mime"))
1302 if((mce
.f
& MIMECE_BIN_PARSE
) && mpp
->m_mime_enc
!= MIMEE_BIN
&&
1303 mpp
->m_charset
!= NULL
)
1304 mc
= _mt_classify_os_part(mce
.f
, mpp
, for_user_context
);
1308 FL
enum mime_handler_flags
1309 n_mimetype_handler(struct mime_handler
*mhp
, struct mimepart
const *mpp
,
1310 enum sendaction action
)
1313 #define __L (sizeof(__S) -1)
1314 struct mtlookup mtl
;
1316 enum mime_handler_flags rv
, xrv
;
1317 char const *es
, *cs
, *ccp
;
1321 memset(mhp
, 0, sizeof *mhp
);
1325 if (action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
1326 rv
|= MIME_HDL_ISQUOTE
;
1327 else if (action
!= SEND_TODISP
&& action
!= SEND_TODISP_ALL
&&
1328 action
!= SEND_TODISP_PARTS
)
1331 el
= ((es
= mpp
->m_filename
) != NULL
&& (es
= strrchr(es
, '.')) != NULL
&&
1332 *++es
!= '\0') ? strlen(es
) : 0;
1333 cl
= ((cs
= mpp
->m_ct_type_usr_ovwr
) != NULL
||
1334 (cs
= mpp
->m_ct_type_plain
) != NULL
) ? strlen(cs
) : 0;
1335 if ((l
= n_MAX(el
, cl
)) == 0) {
1336 /* TODO this should be done during parse time! */
1340 /* We don't pass the flags around, so ensure carrier is up-to-date */
1343 buf
= n_lofi_alloc(__L
+ l
+1);
1344 memcpy(buf
, __S
, __L
);
1346 /* File-extension handlers take precedence.
1347 * Yes, we really "fail" here for file extensions which clash MIME types */
1349 memcpy(buf
+ __L
, es
, el
+1);
1350 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1351 *cp
= lowerconv(*cp
);
1353 if ((mhp
->mh_shell_cmd
= ccp
= n_var_vlook(buf
, FAL0
)) != NULL
) {
1354 rv
= a_mt_pipe_check(mhp
);
1359 /* Then MIME Content-Type:, if any */
1363 memcpy(buf
+ __L
, cs
, cl
+1);
1364 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1365 *cp
= lowerconv(*cp
);
1367 if ((mhp
->mh_shell_cmd
= n_var_vlook(buf
, FAL0
)) != NULL
) {
1368 rv
= a_mt_pipe_check(mhp
);
1372 if (_mt_by_mtname(&mtl
, cs
) != NULL
)
1373 switch (mtl
.mtl_node
->mt_flags
& a_MT__TM_MARKMASK
) {
1374 #ifndef HAVE_FILTER_HTML_TAGSOUP
1375 case a_MT_TM_SOUP_H
:
1378 case a_MT_TM_SOUP_h
:
1379 #ifdef HAVE_FILTER_HTML_TAGSOUP
1380 case a_MT_TM_SOUP_H
:
1381 mhp
->mh_ptf
= &htmlflt_process_main
;
1382 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
=
1383 n_UNCONST(_("Built-in HTML tagsoup filter")));
1384 rv
^= MIME_HDL_NULL
| MIME_HDL_PTF
;
1389 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(_("Plain text")));
1390 rv
^= MIME_HDL_NULL
| MIME_HDL_TEXT
;
1394 mhp
->mh_msg
.s
= n_UNCONST(n_empty
);
1405 if((rv
&= MIME_HDL_TYPE_MASK
) == MIME_HDL_NULL
){
1406 if(mhp
->mh_msg
.s
== NULL
)
1407 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
1408 A_("[-- No MIME handler installed, or not applicable --]\n")));
1409 }else if(rv
== MIME_HDL_CMD
&& !(xrv
& MIME_HDL_COPIOUSOUTPUT
) &&
1410 action
!= SEND_TODISP_PARTS
){
1411 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
1412 _("[-- Use the command `mimeview' to display this --]\n")));
1413 xrv
&= ~MIME_HDL_TYPE_MASK
;
1414 xrv
|= (rv
= MIME_HDL_MSG
);
1416 mhp
->mh_flags
= xrv
;