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 - 2017 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
{
52 _MT_C_CLEAN
= 0, /* Plain RFC 5322 message */
53 _MT_C_NCTT
= 1<<0, /* *contenttype == NULL */
54 _MT_C_ISTXT
= 1<<1, /* *contenttype =~ text\/ */
55 _MT_C_ISTXTCOK
= 1<<2, /* _ISTXT + *mime-allow-text-controls* */
56 _MT_C_HIGHBIT
= 1<<3, /* Not 7bit clean */
57 _MT_C_LONGLINES
= 1<<4, /* MIME_LINELEN_LIMIT exceed. */
58 _MT_C_CTRLCHAR
= 1<<5, /* Control characters seen */
59 _MT_C_HASNUL
= 1<<6, /* Contains \0 characters */
60 _MT_C_NOTERMNL
= 1<<7, /* Lacks a final newline */
61 _MT_C_FROM_
= 1<<8, /* ^From_ seen */
62 _MT_C_FROM_1STLINE
= 1<<9, /* From_ line seen */
63 _MT_C_SUGGEST_DONE
= 1<<16, /* Inspector suggests to stop further parse */
64 _MT_C__1STLINE
= 1<<17 /* .. */
74 struct mtnode
*mt_next
;
76 ui32_t mt_mtlen
; /* Length of MIME type string, rest thereafter */
77 /* C99 forbids flexible arrays in union, so unfortunately we waste a pointer
78 * that could already store character data here */
85 struct mtnode
const *mtl_node
;
86 char *mtl_result
; /* If requested, salloc()ed MIME type */
95 enum mime_type_class mtca_mtc
;
98 static struct mtbltin
const _mt_bltin
[] = {
99 #include <gen-mime-types.h>
102 static char const _mt_typnames
[][16] = {
103 "application/", "audio/", "image/",
104 "message/", "multipart/", "text/",
107 n_CTAV(_MT_APPLICATION
== 0 && _MT_AUDIO
== 1 && _MT_IMAGE
== 2 &&
108 _MT_MESSAGE
== 3 && _MT_MULTIPART
== 4 && _MT_TEXT
== 5 &&
112 static bool_t _mt_is_init
;
113 static struct mtnode
*_mt_list
;
115 /* Initialize MIME type list in order */
116 static void _mt_init(void);
117 static bool_t
__mt_load_file(ui32_t orflags
,
118 char const *file
, char **line
, size_t *linesize
);
120 /* Create (prepend) a new MIME type; cmdcalled results in a bit more verbosity
122 static struct mtnode
* _mt_create(bool_t cmdcalled
, ui32_t orflags
,
123 char const *line
, size_t len
);
125 /* Try to find MIME type by X (after zeroing mtlp), return NULL if not found;
126 * if with_result >mtl_result will be created upon success for the former */
127 static struct mtlookup
* _mt_by_filename(struct mtlookup
*mtlp
,
128 char const *name
, bool_t with_result
);
129 static struct mtlookup
* _mt_by_mtname(struct mtlookup
*mtlp
,
132 /* In-depth inspection of raw content: call _round() repeatedly, last time with
133 * a 0 length buffer, finally check .mtca_mtc for result.
134 * No further call is needed if _round() return includes _MT_C_SUGGEST_DONE,
135 * as the resulting classification is unambiguous */
136 SINLINE
struct mt_class_arg
* _mt_classify_init(struct mt_class_arg
*mtcap
,
137 enum mime_type_class initval
);
138 static enum mime_type_class
_mt_classify_round(struct mt_class_arg
*mtcap
);
140 /* We need an in-depth inspection of an application/octet-stream part */
141 static enum mimecontent
_mt_classify_os_part(ui32_t mce
, struct mimepart
*mpp
);
143 /* Check whether a *pipe-XY* handler is applicable, and adjust flags according
144 * to the defined trigger characters; upon entry MIME_HDL_NULL is set, and that
145 * isn't changed if mhp doesn't apply */
146 static enum mime_handler_flags
a_mt_pipe_check(struct mime_handler
*mhp
);
152 char c
, *line
; /* TODO line pool (below) */
155 char const *srcs_arr
[10], *ccp
, **srcs
;
161 /* Always load our built-ins */
162 for (tail
= NULL
, i
= 0; i
< n_NELEM(_mt_bltin
); ++i
) {
163 struct mtbltin
const *mtbp
= _mt_bltin
+ i
;
164 struct mtnode
*mtnp
= smalloc(sizeof *mtnp
);
167 tail
->mt_next
= mtnp
;
171 mtnp
->mt_next
= NULL
;
172 mtnp
->mt_flags
= mtbp
->mtb_flags
;
173 mtnp
->mt_mtlen
= mtbp
->mtb_mtlen
;
174 mtnp
->mt_line
= mtbp
->mtb_line
;
177 /* Decide which files sources have to be loaded */
178 if ((ccp
= ok_vlook(mimetypes_load_control
)) == NULL
)
180 else if (*ccp
== '\0')
184 srcs
[-1] = srcs
[-2] = NULL
;
186 if (strchr(ccp
, '=') != NULL
) {
189 while ((ccp
= n_strsep(&line
, ',', TRU1
)) != NULL
) {
190 switch ((c
= *ccp
)) {
192 srcs_arr
[1] = VAL_MIME_TYPES_SYS
;
196 srcs_arr
[0] = VAL_MIME_TYPES_USR
;
202 if (*++ccp
== '=' && *++ccp
!= '\0') {
203 if (PTR2SIZE(srcs
- srcs_arr
) < n_NELEM(srcs_arr
))
206 n_err(_("*mimetypes-load-control*: too many sources, "
207 "skipping %s\n"), n_shexp_quote_cp(ccp
, FAL0
));
215 } else for (i
= 0; (c
= ccp
[i
]) != '\0'; ++i
)
217 case 'S': case 's': srcs_arr
[1] = VAL_MIME_TYPES_SYS
; break;
218 case 'U': case 'u': srcs_arr
[0] = VAL_MIME_TYPES_USR
; break;
221 n_err(_("*mimetypes-load-control*: unsupported content: %s\n"), ccp
);
225 /* Load all file-based sources in the desired order */
228 for (j
= 0, i
= (ui32_t
)PTR2SIZE(srcs
- srcs_arr
), srcs
= srcs_arr
;
229 i
> 0; ++j
, ++srcs
, --i
)
232 else if (!__mt_load_file((j
== 0 ? _MT_USR
233 : (j
== 1 ? _MT_SYS
: _MT_FSPEC
)), *srcs
, &line
, &linesize
)) {
234 if ((n_poption
& n_PO_D_V
) || j
> 1)
235 n_err(_("*mimetypes-load-control*: cannot open or load %s\n"),
236 n_shexp_quote_cp(*srcs
, FAL0
));
246 __mt_load_file(ui32_t orflags
, char const *file
, char **line
, size_t *linesize
)
250 struct mtnode
*head
, *tail
, *mtnp
;
254 if ((cp
= fexpand(file
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
||
255 (fp
= Fopen(cp
, "r")) == NULL
) {
260 for (head
= tail
= NULL
; fgetline(line
, linesize
, NULL
, &len
, fp
, 0) != 0;)
261 if ((mtnp
= _mt_create(FAL0
, orflags
, *line
, len
)) != NULL
) {
265 tail
->mt_next
= mtnp
;
269 tail
->mt_next
= _mt_list
;
279 static struct mtnode
*
280 _mt_create(bool_t cmdcalled
, ui32_t orflags
, char const *line
, size_t len
)
283 char const *typ
, *subtyp
;
289 /* Drop anything after a comment first TODO v15: only when read from file */
290 if ((typ
= memchr(line
, '#', len
)) != NULL
)
291 len
= PTR2SIZE(typ
- line
);
293 /* Then trim any trailing whitespace from line (including NL/CR) */
297 work
.s
= n_UNCONST(line
);
299 line
= n_str_trim(&work
, n_STR_TRIM_BOTH
)->s
;
304 /* (But wait - is there a type marker?) */
306 if(!(orflags
& (_MT_USR
| _MT_SYS
)) && *typ
== '@'){
310 orflags
|= a_MT_TM_PLAIN
;
317 else if(len
> 4 && typ
[2] == '@' && typ
[3] == ' '){
318 n_OBSOLETE("`mimetype': the trailing \"@\" in \"type-marker\" "
325 default: goto jeinval
;
326 case 't': orflags
|= a_MT_TM_PLAIN
; break;
327 case 'h': orflags
|= a_MT_TM_SOUP_h
; break;
328 case 'H': orflags
|= a_MT_TM_SOUP_H
; break;
329 case 'q': orflags
|= a_MT_TM_QUIET
; break;
338 while (len
> 0 && !blankchar(*line
))
340 /* Ignore empty lines and even incomplete specifications (only MIME type)
341 * because this is quite common in mime.types(5) files */
342 if (len
== 0 || (tlen
= PTR2SIZE(line
- typ
)) == 0) {
343 if (cmdcalled
|| (orflags
& _MT_FSPEC
)) {
345 line
= _("(no value)");
348 n_err(_("Empty MIME type or no extensions given: %.*s\n"),
354 if ((subtyp
= memchr(typ
, '/', tlen
)) == NULL
|| subtyp
[1] == '\0' ||
355 spacechar(subtyp
[1])) {
357 if(cmdcalled
|| (orflags
& _MT_FSPEC
) || (n_poption
& n_PO_D_V
))
358 n_err(_("%s MIME type: %.*s\n"),
359 (cmdcalled
? _("Invalid") : _("mime.types(5): invalid")),
365 /* Map to mime_type */
366 tlen
= PTR2SIZE(subtyp
- typ
);
367 for (i
= __MT_TMIN
;;) {
368 if (!ascncasecmp(_mt_typnames
[i
], typ
, tlen
)) {
370 tlen
= PTR2SIZE(line
- subtyp
);
374 if (++i
== __MT_TMAX
) {
375 orflags
|= _MT_OTHER
;
376 tlen
= PTR2SIZE(line
- typ
);
381 /* Strip leading whitespace from the list of extensions;
382 * trailing WS has already been trimmed away above.
383 * Be silent on slots which define a mimetype without any value */
384 while (len
> 0 && blankchar(*line
))
390 mtnp
= smalloc(sizeof(*mtnp
) + tlen
+ len
+1);
391 mtnp
->mt_next
= NULL
;
392 mtnp
->mt_flags
= orflags
;
393 mtnp
->mt_mtlen
= (ui32_t
)tlen
;
394 { char *l
= (char*)(mtnp
+ 1);
396 memcpy(l
, typ
, tlen
);
397 memcpy(l
+ tlen
, line
, len
);
407 static struct mtlookup
*
408 _mt_by_filename(struct mtlookup
*mtlp
, char const *name
, bool_t with_result
)
412 char const *ext
, *cp
;
415 memset(mtlp
, 0, sizeof *mtlp
);
417 if ((nlen
= strlen(name
)) == 0) /* TODO name should be a URI */
419 /* We need a period TODO we should support names like README etc. */
420 for (i
= nlen
; name
[--i
] != '.';)
421 if (i
== 0 || name
[i
] == '/') /* XXX no magics */
423 /* While here, basename() it */
424 while (i
> 0 && name
[i
- 1] != '/')
428 mtlp
->mtl_name
= name
;
429 mtlp
->mtl_nlen
= nlen
;
434 /* ..all the MIME types */
435 for (mtnp
= _mt_list
; mtnp
!= NULL
; mtnp
= mtnp
->mt_next
)
436 for (ext
= mtnp
->mt_line
+ mtnp
->mt_mtlen
;; ext
= cp
) {
438 while (whitechar(*cp
))
441 while (!whitechar(*cp
) && *cp
!= '\0')
444 if ((i
= PTR2SIZE(cp
- ext
)) == 0)
446 /* Don't allow neither of ".txt" or "txt" to match "txt" */
447 else if (i
+ 1 >= nlen
|| name
[(j
= nlen
- i
) - 1] != '.' ||
448 ascncasecmp(name
+ j
, ext
, i
))
452 mtlp
->mtl_node
= mtnp
;
457 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
461 name
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
465 mtlp
->mtl_result
= salloc(i
+ j
+1);
467 memcpy(mtlp
->mtl_result
, name
, j
);
468 memcpy(mtlp
->mtl_result
+ j
, mtnp
->mt_line
, i
);
469 mtlp
->mtl_result
[j
+= i
] = '\0';
479 static struct mtlookup
*
480 _mt_by_mtname(struct mtlookup
*mtlp
, char const *mtname
)
487 memset(mtlp
, 0, sizeof *mtlp
);
489 if ((mtlp
->mtl_nlen
= nlen
= strlen(mtlp
->mtl_name
= mtname
)) == 0)
495 /* ..all the MIME types */
496 for (mtnp
= _mt_list
; mtnp
!= NULL
; mtnp
= mtnp
->mt_next
) {
497 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
501 cp
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
506 if (i
+ j
== mtlp
->mtl_nlen
) {
507 char *xmt
= ac_alloc(i
+ j
+1);
510 memcpy(xmt
+ j
, mtnp
->mt_line
, i
);
512 i
= asccasecmp(mtname
, xmt
);
517 mtlp
->mtl_node
= mtnp
;
529 SINLINE
struct mt_class_arg
*
530 _mt_classify_init(struct mt_class_arg
* mtcap
, enum mime_type_class initval
)
533 memset(mtcap
, 0, sizeof *mtcap
);
534 /*mtcap->mtca_lastc =*/ mtcap
->mtca_c
= EOF
;
535 mtcap
->mtca_mtc
= initval
| _MT_C__1STLINE
;
540 static enum mime_type_class
541 _mt_classify_round(struct mt_class_arg
*mtcap
) /* TODO dig UTF-8 for !text/!! */
543 /* TODO BTW., after the MIME/send layer rewrite we could use a MIME
544 * TODO boundary of "=-=-=" if we would add a B_ in EQ spirit to F_,
545 * TODO and report that state to the outer world */
547 #define F_SIZEOF (sizeof(F_) -1)
548 char f_buf
[F_SIZEOF
], *f_p
= f_buf
;
553 enum mime_type_class mtc
;
556 buf
= mtcap
->mtca_buf
;
557 blen
= mtcap
->mtca_len
;
558 curlen
= mtcap
->mtca_curlen
;
560 /*lastc = mtcap->mtca_lastc;*/
561 mtc
= mtcap
->mtca_mtc
;
565 /* Real EOF, or only current buffer end? */
566 if(mtcap
->mtca_len
== 0){
581 if (!(mtc
& _MT_C_ISTXTCOK
)) {
582 mtc
|= _MT_C_SUGGEST_DONE
;
587 if (c
== '\n' || c
== EOF
) {
588 mtc
&= ~_MT_C__1STLINE
;
589 if (curlen
>= MIME_LINELEN_LIMIT
)
590 mtc
|= _MT_C_LONGLINES
;
598 /* A bit hairy is handling of \r=\x0D=CR.
600 * Control characters other than TAB, or CR and LF as parts of CRLF
601 * pairs, must not appear. \r alone does not force _CTRLCHAR below since
602 * we cannot peek the next character. Thus right here, inspect the last
603 * seen character for if its \r and set _CTRLCHAR in a delayed fashion */
604 /*else*/ if (lastc
== '\r')
605 mtc
|= _MT_C_CTRLCHAR
;
607 /* Control character? XXX this is all ASCII here */
608 if (c
< 0x20 || c
== 0x7F) {
609 /* RFC 2045, 6.7, as above ... */
610 if (c
!= '\t' && c
!= '\r')
611 mtc
|= _MT_C_CTRLCHAR
;
612 /* If there is a escape sequence in reverse solidus notation defined
613 * for this in ANSI X3.159-1989 (ANSI C89), don't treat it as a control
614 * for real. I.e., \a=\x07=BEL, \b=\x08=BS, \t=\x09=HT. Don't follow
615 * libmagic(1) in respect to \v=\x0B=VT. \f=\x0C=NP; do ignore
617 if ((c
>= '\x07' && c
<= '\x0D') || c
== '\x1B')
619 mtc
|= _MT_C_HASNUL
; /* Force base64 */
620 if (!(mtc
& _MT_C_ISTXTCOK
)) {
621 mtc
|= _MT_C_SUGGEST_DONE
;
624 } else if ((ui8_t
)c
& 0x80) {
625 mtc
|= _MT_C_HIGHBIT
;
626 /* TODO count chars with HIGHBIT? libmagic?
627 * TODO try encode part - base64 if bails? */
628 if (!(mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))) { /* TODO _NCTT?? */
629 mtc
|= _MT_C_HASNUL
/* Force base64 */ | _MT_C_SUGGEST_DONE
;
632 } else if (!(mtc
& _MT_C_FROM_
) && UICMP(z
, curlen
, <, F_SIZEOF
)) {
634 if (UICMP(z
, curlen
, ==, F_SIZEOF
- 1) &&
635 PTR2SIZE(f_p
- f_buf
) == F_SIZEOF
&&
636 !memcmp(f_buf
, F_
, F_SIZEOF
)){
638 if (mtc
& _MT_C__1STLINE
)
639 mtc
|= _MT_C_FROM_1STLINE
;
643 if (c
== EOF
&& lastc
!= '\n')
644 mtc
|= _MT_C_NOTERMNL
;
646 mtcap
->mtca_curlen
= curlen
;
647 /*mtcap->mtca_lastc = lastc*/;
649 mtcap
->mtca_mtc
= mtc
;
656 static enum mimecontent
657 _mt_classify_os_part(ui32_t mce
, struct mimepart
*mpp
)
659 struct str in
= {NULL
, 0}, outrest
, inrest
, dec
;
660 struct mt_class_arg mtca
;
662 enum mime_type_class mtc
;
670 assert(mpp
->m_mime_enc
!= MIMEE_BIN
);
672 outrest
= inrest
= dec
= in
;
677 /* TODO v15-compat Note we actually bypass our usual file handling by
678 * TODO directly using fseek() on mb.mb_itf -- the v15 rewrite will change
679 * TODO all of this, and until then doing it like this is the only option
680 * TODO to integrate nicely into whoever calls us */
681 start_off
= ftell(mb
.mb_itf
);
682 if ((ibuf
= setinput(&mb
, (struct message
*)mpp
, NEED_BODY
)) == NULL
) {
684 fseek(mb
.mb_itf
, start_off
, SEEK_SET
);
689 /* Skip part headers */
690 for (lc
= '\0'; cnt
> 0; lc
= c
, --cnt
)
691 if ((c
= getc(ibuf
)) == EOF
|| (c
== '\n' && lc
== '\n'))
693 if (cnt
== 0 || ferror(ibuf
))
696 /* So now let's inspect the part content, decoding content-transfer-encoding
697 * along the way TODO this should simply be "mime_factory_create(MPP)"! */
698 _mt_classify_init(&mtca
, _MT_C_ISTXT
);
703 c
= (--cnt
== 0) ? EOF
: getc(ibuf
);
704 if ((dobuf
= (c
== '\n'))) {
705 /* Ignore empty lines */
708 } else if ((dobuf
= (c
== EOF
))) {
709 if (lsz
== 0 && outrest
.l
== 0)
714 in
.s
= srealloc(in
.s
, lsz
+= LINESIZE
);
716 in
.s
[in
.l
++] = (char)c
;
721 switch (mpp
->m_mime_enc
) {
723 if (!b64_decode_part(&dec
, &in
, &outrest
,
724 (did_inrest
? NULL
: &inrest
))) {
725 mtca
.mtca_mtc
= _MT_C_HASNUL
;
726 goto jstopit
; /* break;break; */
731 if (!qp_decode_part(&dec
, &in
, &outrest
, &inrest
)) {
732 mtca
.mtca_mtc
= _MT_C_HASNUL
;
733 goto jstopit
; /* break;break; */
735 if (dec
.l
== 0 && c
!= EOF
) {
741 /* Temporarily switch those two buffers.. */
748 mtca
.mtca_buf
= dec
.s
;
749 mtca
.mtca_len
= (ssize_t
)dec
.l
;
750 if ((mtc
= _mt_classify_round(&mtca
)) & _MT_C_SUGGEST_DONE
) {
757 /* ..and restore switched */
765 if ((in
.l
= inrest
.l
) > 0) {
777 if (outrest
.s
!= NULL
)
779 if (inrest
.s
!= NULL
)
782 fseek(mb
.mb_itf
, start_off
, SEEK_SET
);
784 if (!(mtc
& (_MT_C_HASNUL
/*| _MT_C_CTRLCHAR XXX really? */))) {
785 mc
= MIME_TEXT_PLAIN
;
786 if (mce
& MIMECE_ALL_OVWR
)
787 mpp
->m_ct_type_plain
= "text/plain";
788 if (mce
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
789 mpp
->m_ct_type_usr_ovwr
= "text/plain";
796 static enum mime_handler_flags
797 a_mt_pipe_check(struct mime_handler
*mhp
){
798 enum mime_handler_flags rv_orig
, rv
;
802 rv_orig
= rv
= mhp
->mh_flags
;
804 /* Do we have any handler for this part? */
805 if(*(cp
= mhp
->mh_shell_cmd
) == '\0')
807 else if(*cp
++ != '@'){
810 }else if(*cp
== '\0'){
817 case '*': rv
|= MIME_HDL_COPIOUSOUTPUT
; ++cp
; goto jnextc
;
818 case '#': rv
|= MIME_HDL_NOQUOTE
; ++cp
; goto jnextc
;
819 case '&': rv
|= MIME_HDL_ASYNC
; ++cp
; goto jnextc
;
820 case '!': rv
|= MIME_HDL_NEEDSTERM
; ++cp
; goto jnextc
;
822 if(rv
& MIME_HDL_TMPF
)
823 rv
|= MIME_HDL_TMPF_UNLINK
;
828 rv
|= MIME_HDL_TMPF_FILL
;
837 mhp
->mh_shell_cmd
= cp
;
840 if(rv
& MIME_HDL_TMPF_FILL
)
844 if(rv
& MIME_HDL_ISQUOTE
){
845 if(rv
& MIME_HDL_NOQUOTE
)
848 /* Cannot fetch data back from asynchronous process */
849 if(rv
& MIME_HDL_ASYNC
)
852 /* TODO Can't use a "needsterminal" program for quoting */
853 if(rv
& MIME_HDL_NEEDSTERM
)
857 if(rv
& MIME_HDL_NEEDSTERM
){
858 if(rv
& MIME_HDL_COPIOUSOUTPUT
){
859 n_err(_("MIME type handlers: cannot use needsterminal and "
860 "copiousoutput together\n"));
863 if(rv
& MIME_HDL_ASYNC
){
864 n_err(_("MIME type handlers: cannot use needsterminal and "
865 "x-mailx-async together\n"));
869 /* needsterminal needs a terminal */
870 if(!(n_psonce
& n_PSO_INTERACTIVE
))
874 if(rv
& MIME_HDL_ASYNC
){
875 if(rv
& MIME_HDL_COPIOUSOUTPUT
){
876 n_err(_("MIME type handlers: cannot use x-mailx-async and "
877 "copiousoutput together\n"));
880 if(rv
& MIME_HDL_TMPF_UNLINK
){
881 n_err(_("MIME type handlers: cannot use x-mailx-async and "
882 "x-mailx-tmpfile-unlink together\n"));
887 /* TODO mailcap-only: TMPF_UNLINK): needs -tmpfile OR -tmpfile-fill */
901 struct n_string s
, *sp
;
909 sp
= n_string_creat_auto(&s
);
911 if(*(argv
= v
) == NULL
){
915 if(_mt_list
== NULL
){
916 fprintf(n_stdout
, _("# `mimetype': no mime.types(5) available\n"));
920 if((fp
= Ftmp(NULL
, "mimetype", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)
922 n_perr(_("tmpfile"), 0);
927 sp
= n_string_reserve(sp
, 63);
929 for(l
= 0, mtnp
= _mt_list
; mtnp
!= NULL
; ++l
, mtnp
= mtnp
->mt_next
){
932 sp
= n_string_trunc(sp
, 0);
934 switch(mtnp
->mt_flags
& a_MT__TM_MARKMASK
){
935 case a_MT_TM_PLAIN
: cp
= "@t "; break;
936 case a_MT_TM_SOUP_h
: cp
= "@h "; break;
937 case a_MT_TM_SOUP_H
: cp
= "@H "; break;
938 case a_MT_TM_QUIET
: cp
= "@q "; break;
939 default: cp
= NULL
; break;
942 sp
= n_string_push_cp(sp
, cp
);
944 if((mtnp
->mt_flags
& __MT_TMASK
) != _MT_OTHER
)
945 sp
= n_string_push_cp(sp
, _mt_typnames
[mtnp
->mt_flags
&__MT_TMASK
]);
947 sp
= n_string_push_buf(sp
, mtnp
->mt_line
, mtnp
->mt_mtlen
);
948 sp
= n_string_push_c(sp
, ' ');
949 sp
= n_string_push_c(sp
, ' ');
950 sp
= n_string_push_cp(sp
, &mtnp
->mt_line
[mtnp
->mt_mtlen
]);
952 fprintf(fp
, "wysh mimetype %s%s\n", n_string_cp(sp
),
953 ((n_poption
& n_PO_D_V
) == 0 ? n_empty
954 : (mtnp
->mt_flags
& _MT_USR
? " # user"
955 : (mtnp
->mt_flags
& _MT_SYS
? " # system"
956 : (mtnp
->mt_flags
& _MT_FSPEC
? " # f= file"
957 : (mtnp
->mt_flags
& _MT_CMD
? " # command" : " # built-in"))))));
960 page_or_print(fp
, l
);
963 for(; *argv
!= NULL
; ++argv
){
965 sp
= n_string_push_c(sp
, ' ');
966 sp
= n_string_push_cp(sp
, *argv
);
969 mtnp
= _mt_create(TRU1
, _MT_CMD
, n_string_cp(sp
), sp
->s_len
);
971 mtnp
->mt_next
= _mt_list
;
978 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
982 c_unmimetype(void *v
)
985 struct mtnode
*lnp
, *mtnp
;
989 /* Need to load that first as necessary */
993 for (; *argv
!= NULL
; ++argv
) {
994 if (!asccasecmp(*argv
, "reset")) {
999 if (argv
[0][0] == '*' && argv
[0][1] == '\0') {
1001 while ((mtnp
= _mt_list
) != NULL
) {
1002 _mt_list
= mtnp
->mt_next
;
1008 for (match
= FAL0
, lnp
= NULL
, mtnp
= _mt_list
; mtnp
!= NULL
;) {
1013 if ((mtnp
->mt_flags
& __MT_TMASK
) == _MT_OTHER
) {
1017 typ
= _mt_typnames
[mtnp
->mt_flags
& __MT_TMASK
];
1021 val
= ac_alloc(i
+ mtnp
->mt_mtlen
+1);
1022 memcpy(val
, typ
, i
);
1023 memcpy(val
+ i
, mtnp
->mt_line
, mtnp
->mt_mtlen
);
1024 val
[i
+= mtnp
->mt_mtlen
] = '\0';
1025 i
= asccasecmp(val
, *argv
);
1029 struct mtnode
*nnp
= mtnp
->mt_next
;
1038 lnp
= mtnp
, mtnp
= mtnp
->mt_next
;
1041 if (!(n_pstate
& n_PS_ROBOT
) || (n_poption
& n_PO_D_V
))
1042 n_err(_("No such MIME type: %s\n"), *argv
);
1047 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1051 n_mimetype_check_mtname(char const *name
)
1053 struct mtlookup mtl
;
1057 rv
= (_mt_by_mtname(&mtl
, name
) != NULL
);
1063 n_mimetype_classify_filename(char const *name
)
1065 struct mtlookup mtl
;
1068 _mt_by_filename(&mtl
, name
, TRU1
);
1070 return mtl
.mtl_result
;
1074 n_mimetype_classify_file(FILE *fp
, char const **contenttype
,
1075 char const **charset
, int *do_iconv
)
1077 /* TODO classify once only PLEASE PLEASE PLEASE */
1078 /* TODO message/rfc822 is special in that it may only be 7bit, 8bit or
1079 * TODO binary according to RFC 2046, 5.2.1
1080 * TODO The handling of which is a hack */
1082 enum mime_type_class mtc
;
1088 assert(ftell(fp
) == 0x0l
);
1092 if (*contenttype
== NULL
) {
1095 } else if (!ascncasecmp(*contenttype
, "text/", 5)) {
1096 mtc
= ok_blook(mime_allow_text_controls
)
1097 ? _MT_C_ISTXT
| _MT_C_ISTXTCOK
: _MT_C_ISTXT
;
1099 } else if (!asccasecmp(*contenttype
, "message/rfc822")) {
1107 menc
= mime_enc_target();
1109 if ((fpsz
= fsize(fp
)) == 0)
1112 char buf
[BUFFER_SIZE
];
1113 struct mt_class_arg mtca
;
1115 _mt_classify_init(&mtca
, mtc
);
1117 mtca
.mtca_len
= fread(buf
, sizeof(buf
[0]), n_NELEM(buf
), fp
);
1118 mtca
.mtca_buf
= buf
;
1119 if ((mtc
= _mt_classify_round(&mtca
)) & _MT_C_SUGGEST_DONE
)
1121 if (mtca
.mtca_len
== 0)
1124 /* TODO ferror(fp) ! */
1128 if (mtc
& _MT_C_HASNUL
) {
1130 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1131 * on request; else enforce what file(1)/libmagic(3) would suggest */
1132 if (mtc
& _MT_C_ISTXTCOK
)
1134 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1135 *contenttype
= "application/octet-stream";
1136 /* if (*charset == NULL)
1137 * *charset = "binary";*/
1142 (_MT_C_LONGLINES
| _MT_C_CTRLCHAR
| _MT_C_NOTERMNL
| _MT_C_FROM_
)) {
1143 if (menc
!= MIMEE_B64
)
1147 if (mtc
& _MT_C_HIGHBIT
) {
1149 if (mtc
& (_MT_C_NCTT
| _MT_C_ISTXT
))
1150 *do_iconv
= ((mtc
& _MT_C_HIGHBIT
) != 0);
1154 if (mtc
& _MT_C_NCTT
)
1155 *contenttype
= "text/plain";
1157 /* Not an attachment with specified charset? */
1159 if (*charset
== NULL
) /* TODO MIME/send: iter active? iter! else */
1160 *charset
= (mtc
& _MT_C_HIGHBIT
) ? charset_iter_or_fallback()
1161 : ok_vlook(charset_7bit
);
1163 /* TODO mime_type_file_classify() shouldn't return conversion */
1165 if (mtc
& _MT_C_FROM_1STLINE
) {
1166 n_err(_("Pre-v15 %s cannot handle message/rfc822 that "
1167 "indeed is a RFC 4155 MBOX!\n"
1168 " Forcing a content-type of application/mbox!\n"),
1170 *contenttype
= "application/mbox";
1173 c
= (menc
== MIMEE_7B
? CONV_7BIT
1174 : (menc
== MIMEE_8B
? CONV_8BIT
1175 /* May have only 7-bit, 8-bit and binary. Try to avoid latter */
1176 : ((mtc
& _MT_C_HASNUL
) ? CONV_NONE
1177 : ((mtc
& _MT_C_HIGHBIT
) ? CONV_8BIT
: CONV_7BIT
))));
1180 c
= (menc
== MIMEE_7B
? CONV_7BIT
1181 : (menc
== MIMEE_8B
? CONV_8BIT
1182 : (menc
== MIMEE_QP
? CONV_TOQP
: CONV_TOB64
)));
1188 n_mimetype_classify_part(struct mimepart
*mpp
) /* FIXME charset=binary ??? */
1190 struct mtlookup mtl
;
1191 enum mimecontent mc
;
1193 union {char const *cp
; ui32_t f
;} mce
;
1198 if ((ct
= mpp
->m_ct_type_plain
) == NULL
) /* TODO may not */
1201 if((mce
.cp
= ok_vlook(mime_counter_evidence
)) != NULL
&& *mce
.cp
!= '\0'){
1202 if((n_idec_ui32_cp(&mce
.f
, mce
.cp
, 0, NULL
1203 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1204 ) != n_IDEC_STATE_CONSUMED
){
1205 n_err(_("Invalid *mime-counter-evidence* value content\n"));
1208 mce
.f
|= MIMECE_SET
;
1209 is_os
= !asccasecmp(ct
, "application/octet-stream");
1211 if(mpp
->m_filename
!= NULL
&& (is_os
|| (mce
.f
& MIMECE_ALL_OVWR
))){
1212 if(_mt_by_filename(&mtl
, mpp
->m_filename
, TRU1
) == NULL
){
1214 goto jos_content_check
;
1215 }else if(is_os
|| asccasecmp(ct
, mtl
.mtl_result
)){
1216 if(mce
.f
& MIMECE_ALL_OVWR
)
1217 mpp
->m_ct_type_plain
= ct
= mtl
.mtl_result
;
1218 if(mce
.f
& (MIMECE_BIN_OVWR
| MIMECE_ALL_OVWR
))
1219 mpp
->m_ct_type_usr_ovwr
= ct
= mtl
.mtl_result
;
1226 if(*ct
== '\0' || strchr(ct
, '/') == NULL
) /* For compat with non-MIME */
1228 else if(is_asccaseprefix("text/", ct
)){
1229 ct
+= sizeof("text/") -1;
1230 if(!asccasecmp(ct
, "plain"))
1231 mc
= MIME_TEXT_PLAIN
;
1232 else if(!asccasecmp(ct
, "html"))
1233 mc
= MIME_TEXT_HTML
;
1236 }else if(is_asccaseprefix("message/", ct
)){
1237 ct
+= sizeof("message/") -1;
1238 if(!asccasecmp(ct
, "rfc822"))
1242 }else if(is_asccaseprefix("multipart/", ct
)){
1245 enum mimecontent mt_mc
;
1247 {"alternative\0", MIME_ALTERNATIVE
},
1248 {"related", MIME_RELATED
},
1249 {"digest", MIME_DIGEST
},
1250 {"signed", MIME_SIGNED
},
1251 {"encrypted", MIME_ENCRYPTED
}
1254 for(ct
+= sizeof("multipart/") -1, mtap
= mta
;;)
1255 if(!asccasecmp(ct
, mtap
->mt_name
)){
1258 }else if(++mtap
== mta
+ n_NELEM(mta
)){
1262 }else if(is_asccaseprefix("application/", ct
)){
1264 goto jos_content_check
;
1265 ct
+= sizeof("application/") -1;
1266 if(!asccasecmp(ct
, "pkcs7-mime") || !asccasecmp(ct
, "x-pkcs7-mime"))
1274 if((mce
.f
& MIMECE_BIN_PARSE
) && mpp
->m_mime_enc
!= MIMEE_BIN
&&
1275 mpp
->m_charset
!= NULL
&& asccasecmp(mpp
->m_charset
, "binary"))
1276 mc
= _mt_classify_os_part(mce
.f
, mpp
);
1280 FL
enum mime_handler_flags
1281 n_mimetype_handler(struct mime_handler
*mhp
, struct mimepart
const *mpp
,
1282 enum sendaction action
)
1285 #define __L (sizeof(__S) -1)
1286 struct mtlookup mtl
;
1288 enum mime_handler_flags rv
, xrv
;
1289 char const *es
, *cs
, *ccp
;
1293 memset(mhp
, 0, sizeof *mhp
);
1297 if (action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
1298 rv
|= MIME_HDL_ISQUOTE
;
1299 else if (action
!= SEND_TODISP
&& action
!= SEND_TODISP_ALL
&&
1300 action
!= SEND_TODISP_PARTS
)
1303 el
= ((es
= mpp
->m_filename
) != NULL
&& (es
= strrchr(es
, '.')) != NULL
&&
1304 *++es
!= '\0') ? strlen(es
) : 0;
1305 cl
= ((cs
= mpp
->m_ct_type_usr_ovwr
) != NULL
||
1306 (cs
= mpp
->m_ct_type_plain
) != NULL
) ? strlen(cs
) : 0;
1307 if ((l
= n_MAX(el
, cl
)) == 0) {
1308 /* TODO this should be done during parse time! */
1312 /* We don't pass the flags around, so ensure carrier is up-to-date */
1315 buf
= n_lofi_alloc(__L
+ l
+1);
1316 memcpy(buf
, __S
, __L
);
1318 /* File-extension handlers take precedence.
1319 * Yes, we really "fail" here for file extensions which clash MIME types */
1321 memcpy(buf
+ __L
, es
, el
+1);
1322 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1323 *cp
= lowerconv(*cp
);
1325 if ((mhp
->mh_shell_cmd
= ccp
= n_var_vlook(buf
, FAL0
)) != NULL
) {
1326 rv
= a_mt_pipe_check(mhp
);
1331 /* Then MIME Content-Type:, if any */
1335 memcpy(buf
+ __L
, cs
, cl
+1);
1336 for (cp
= buf
+ __L
; *cp
!= '\0'; ++cp
)
1337 *cp
= lowerconv(*cp
);
1339 if ((mhp
->mh_shell_cmd
= n_var_vlook(buf
, FAL0
)) != NULL
) {
1340 rv
= a_mt_pipe_check(mhp
);
1344 if (_mt_by_mtname(&mtl
, cs
) != NULL
)
1345 switch (mtl
.mtl_node
->mt_flags
& a_MT__TM_MARKMASK
) {
1346 #ifndef HAVE_FILTER_HTML_TAGSOUP
1347 case a_MT_TM_SOUP_H
:
1350 case a_MT_TM_SOUP_h
:
1351 #ifdef HAVE_FILTER_HTML_TAGSOUP
1352 case a_MT_TM_SOUP_H
:
1353 mhp
->mh_ptf
= &htmlflt_process_main
;
1354 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
=
1355 n_UNCONST(_("Built-in HTML tagsoup filter")));
1356 rv
^= MIME_HDL_NULL
| MIME_HDL_PTF
;
1361 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(_("Plain text")));
1362 rv
^= MIME_HDL_NULL
| MIME_HDL_TEXT
;
1366 mhp
->mh_msg
.s
= n_UNCONST(n_empty
);
1377 if((rv
&= MIME_HDL_TYPE_MASK
) == MIME_HDL_NULL
){
1378 if(mhp
->mh_msg
.s
== NULL
)
1379 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
1380 _("[-- No MIME handler installed, or not applicable --]\n")));
1381 }else if(rv
== MIME_HDL_CMD
&& !(xrv
& MIME_HDL_COPIOUSOUTPUT
) &&
1382 action
!= SEND_TODISP_PARTS
){
1383 mhp
->mh_msg
.l
= strlen(mhp
->mh_msg
.s
= n_UNCONST(
1384 _("[-- Use the command `mimeview' to display this --]\n")));
1385 xrv
&= ~MIME_HDL_TYPE_MASK
;
1386 xrv
|= (rv
= MIME_HDL_MSG
);
1388 mhp
->mh_flags
= xrv
;