1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ MIME support functions.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
44 #define _CHARSET() ((_cs_iter != NULL) ? _cs_iter : charset_get_8bit())
47 struct mtnode
*mt_next
;
48 size_t mt_mtlen
; /* Length of MIME type string */
49 char mt_line
[VFIELD_SIZE(8)];
52 static char const * const _mt_sources
[] = {
53 /* XXX Order fixed due to *mimetypes-load-control* handling! */
54 MIME_TYPES_USR
, MIME_TYPES_SYS
, NULL
56 * const _mt_bltin
[] = {
57 #include "mime_types.h"
61 struct mtnode
*_mt_list
;
62 char *_cs_iter_base
, *_cs_iter
;
64 /* Initialize MIME type list */
65 static void _mt_init(void);
66 static void __mt_add_line(char const *line
, struct mtnode
**tail
);
68 /* Is 7-bit enough? */
70 static bool_t
_has_highbit(char const *s
);
71 static bool_t
_name_highbit(struct name
*np
);
74 /* Get the conversion that matches *encoding* */
75 static enum conversion
_conversion_by_encoding(void);
77 /* fwrite(3) while checking for displayability */
78 static ssize_t
_fwrite_td(struct str
const *input
, enum tdflags flags
,
79 struct str
*rest
, struct quoteflt
*qf
);
81 static size_t delctrl(char *cp
, size_t sz
);
83 static int is_this_enc(char const *line
, char const *encoding
);
85 /* Convert header fields to RFC 1522 format and write to the file fo */
86 static size_t mime_write_tohdr(struct str
*in
, FILE *fo
);
88 /* Write len characters of the passed string to the passed file, doing charset
89 * and header conversion */
90 static size_t convhdra(char const *str
, size_t len
, FILE *fp
);
92 /* Write an address to a header field */
93 static size_t mime_write_tohdr_a(struct str
*in
, FILE *f
);
95 /* Append to buf, handling resizing */
96 static void addstr(char **buf
, size_t *sz
, size_t *pos
,
97 char const *str
, size_t len
);
99 static void addconv(char **buf
, size_t *sz
, size_t *pos
,
100 char const *str
, size_t len
);
105 struct mtnode
*tail
= NULL
;
106 char *line
= NULL
; /* TODO line pool */
109 char const *ccp
, * const *srcs
;
113 if ((ccp
= ok_vlook(mimetypes_load_control
)) == NULL
)
115 else for (idx_ok
= 0; *ccp
!= '\0'; ++ccp
)
126 /* XXX bad *mimetypes-load-control*; log error? */
130 for (idx
= 1, srcs
= _mt_sources
; *srcs
!= NULL
; idx
<<= 1, ++srcs
) {
131 if (!(idx
& idx_ok
) || (ccp
= file_expand(*srcs
)) == NULL
)
133 if ((fp
= Fopen(ccp
, "r")) == NULL
) {
134 /*fprintf(stderr, tr(176, "Cannot open %s\n"), fn);*/
137 while (fgetline(&line
, &linesize
, NULL
, NULL
, fp
, 0))
138 __mt_add_line(line
, &tail
);
144 for (srcs
= _mt_bltin
; *srcs
!= NULL
; ++srcs
)
145 __mt_add_line(*srcs
, &tail
);
150 __mt_add_line(char const *line
, struct mtnode
**tail
) /* XXX diag? dups!*/
157 if (!alphachar(*line
))
161 while (!blankchar(*line
) && *line
!= '\0')
165 tlen
= PTR2SIZE(line
- typ
);
167 while (blankchar(*line
) && *line
!= '\0')
173 if (line
[elen
- 1] == '\n') {
174 if (--elen
> 0 && line
[elen
- 1] == '\r')
180 mtn
= smalloc(sizeof(struct mtnode
) -
181 VFIELD_SIZEOF(struct mtnode
, mt_line
) + tlen
+ 1 + elen
+1);
183 (*tail
)->mt_next
= mtn
;
188 mtn
->mt_mtlen
= tlen
;
189 memcpy(mtn
->mt_line
, typ
, tlen
);
190 mtn
->mt_line
[tlen
] = '\0';
192 memcpy(mtn
->mt_line
+ tlen
, line
, elen
);
194 mtn
->mt_line
[tlen
] = '\0';
201 _has_highbit(char const *s
)
208 if ((ui8_t
)*s
& 0x80)
210 while (*s
++ != '\0');
219 _name_highbit(struct name
*np
)
225 if (_has_highbit(np
->n_name
) || _has_highbit(np
->n_fullname
))
234 #endif /* HAVE_ICONV */
236 static enum conversion
237 _conversion_by_encoding(void)
243 if ((cp
= ok_vlook(encoding
)) == NULL
)
244 ret
= MIME_DEFAULT_ENCODING
;
245 else if (!strcmp(cp
, "quoted-printable"))
247 else if (!strcmp(cp
, "8bit"))
249 else if (!strcmp(cp
, "base64"))
252 fprintf(stderr
, tr(177, "Warning: invalid encoding %s, using base64\n"),
261 _fwrite_td(struct str
const *input
, enum tdflags flags
, struct str
*rest
,
264 /* TODO note: after send/MIME layer rewrite we will have a string pool
265 * TODO so that memory allocation count drops down massively; for now,
266 * TODO v14.* that is, we pay a lot & heavily depend on the allocator */
267 /* TODO well if we get a broken pipe here, and it happens to
268 * TODO happen pretty easy when sleeping in a full pipe buffer,
269 * TODO then the current codebase performs longjump away;
270 * TODO this leaves memory leaks behind ('think up to 3 per,
271 * TODO dep. upon alloca availability). For this to be fixed
272 * TODO we either need to get rid of the longjmp()s (tm) or
273 * TODO the storage must come from the outside or be tracked
274 * TODO in a carrier struct. Best both. But storage reuse
275 * TODO would be a bigbig win besides */
276 /* *input* _may_ point to non-modifyable buffer; but even then it only
277 * needs to be dup'ed away if we have to transform the content */
288 if ((flags
& TD_ICONV
) && iconvd
!= (iconv_t
)-1) {
291 if (rest
!= NULL
&& rest
->l
> 0) {
292 in
.l
= rest
->l
+ input
->l
;
293 in
.s
= buf
= smalloc(in
.l
+1);
294 memcpy(in
.s
, rest
->s
, rest
->l
);
295 memcpy(in
.s
+ rest
->l
, input
->s
, input
->l
);
299 if (n_iconv_str(iconvd
, &out
, &in
, &in
, TRU1
) != 0 && rest
!= NULL
&&
301 /* Incomplete multibyte at EOF is special */
302 if (flags
& _TD_EOF
) {
303 out
.s
= srealloc(out
.s
, out
.l
+ 4);
304 /* TODO 0xFFFD out.s[out.l++] = '[';*/
305 out
.s
[out
.l
++] = '?'; /* TODO 0xFFFD !!! */
306 /* TODO 0xFFFD out.s[out.l++] = ']';*/
308 n_str_add(rest
, &in
);
312 flags
&= ~_TD_BUFCOPY
;
320 makeprint(&in
, &out
);
321 else if (flags
& _TD_BUFCOPY
)
322 n_str_dup(&out
, &in
);
325 if (flags
& TD_DELCTRL
)
326 out
.l
= delctrl(out
.s
, out
.l
);
328 rv
= quoteflt_push(qf
, out
.s
, out
.l
);
332 if (in
.s
!= input
->s
)
339 delctrl(char *cp
, size_t sz
)
345 if (!cntrlchar(cp
[x
]))
354 is_this_enc(char const *line
, char const *encoding
)
356 int rv
, c
, quoted
= 0;
362 while (*line
!= '\0' && *encoding
)
363 if ((c
= *line
++, lowerconv(c
) != *encoding
++))
366 if (quoted
&& *line
== '"')
368 if (*line
== '\0' || whitechar(*line
))
377 mime_write_tohdr(struct str
*in
, FILE *fo
) /* TODO rewrite - FAST! */
379 struct str cin
, cout
;
380 char buf
[B64_LINESIZE
+1]; /* (No CR/LF used) */
381 char const *charset7
, *charset
, *upper
, *wbeg
, *wend
, *lastspc
,
383 size_t sz
= 0, col
= 0, quoteany
, wr
, charsetlen
,
384 maxcol
= 65 /* there is the header field's name, too */;
385 bool_t highbit
, mustquote
, broken
;
388 charset7
= charset_get_7bit();
389 charset
= _CHARSET();
390 wr
= strlen(charset7
);
391 charsetlen
= strlen(charset
);
392 charsetlen
= MAX(charsetlen
, wr
);
393 upper
= in
->s
+ in
->l
;
395 /* xxx note this results in too much hits since =/? force quoting even
396 * xxx if they don't form =? etc. */
397 quoteany
= mime_cte_mustquote(in
->s
, in
->l
, TRU1
);
401 for (wbeg
= in
->s
; wbeg
< upper
; ++wbeg
)
402 if ((ui8_t
)*wbeg
& 0x80)
405 if (quoteany
<< 1 > in
->l
) {
406 /* Print the entire field in base64 */
407 for (wbeg
= in
->s
; wbeg
< upper
; wbeg
= wend
) {
409 cin
.s
= UNCONST(wbeg
);
411 cin
.l
= PTR2SIZE(wend
- wbeg
);
412 if (cin
.l
* 4/3 + 7 + charsetlen
< maxcol
- col
) {
415 wr
= fprintf(fo
, "=?%s?B?%s?=", (highbit
? charset
: charset7
),
416 b64_encode(&cout
, &cin
, B64_BUF
)->s
);
420 fwrite("\n ", sizeof(char), 2, fo
);
438 /* Print the field word-wise in quoted-printable */
440 for (wbeg
= in
->s
; wbeg
< upper
; wbeg
= wend
) {
442 while (wbeg
< upper
&& whitechar(*wbeg
)) {
443 lastspc
= lastspc
? lastspc
: wbeg
;
450 while (lastspc
< wbeg
) {
451 putc(*lastspc
&0377, fo
);
461 for (wend
= wbeg
; wend
< upper
&& !whitechar(*wend
); ++wend
)
462 if ((ui8_t
)*wend
& 0x80)
464 mustquote
= (mime_cte_mustquote(wbeg
, PTR2SIZE(wend
- wbeg
), TRU1
)
467 if (mustquote
|| broken
||
468 (PTR2SIZE(wend
- wbeg
) >= 76-5 && quoteany
)) {
469 for (cout
.s
= NULL
;;) {
470 cin
.s
= UNCONST(lastwordend
? lastwordend
: wbeg
);
471 cin
.l
= PTR2SIZE(wend
- cin
.s
);
472 qp_encode(&cout
, &cin
, QP_ISHEAD
);
473 wr
= cout
.l
+ charsetlen
+ 7;
475 if (col
<= maxcol
&& wr
<= maxcol
- col
) {
477 /* TODO because we included the WS in the encoded str,
479 * TODO RFC: "any 'linear-white-space' that separates
480 * TODO a pair of adjacent 'encoded-word's is ignored" */
485 fprintf(fo
, "=?%s?Q?%.*s?=",
486 (highbit
? charset
: charset7
), (int)cout
.l
, cout
.s
);
490 } else if (col
> 1) {
491 /* TODO assuming SP separator, ignore *lastspc* !?? */
493 if (lastspc
!= NULL
) {
518 if (col
&& PTR2SIZE(wend
- wbeg
) > maxcol
- col
) {
523 if (lastspc
== NULL
) {
528 maxcol
-= PTR2SIZE(wbeg
- lastspc
);
531 while (lastspc
< wbeg
) {
532 putc(*lastspc
&0377, fo
);
536 wr
= fwrite(wbeg
, sizeof *wbeg
, PTR2SIZE(wend
- wbeg
), fo
);
548 convhdra(char const *str
, size_t len
, FILE *fp
)
557 cin
.s
= UNCONST(str
);
561 if (iconvd
!= (iconv_t
)-1) {
563 if (n_iconv_str(iconvd
, &ciconv
, &cin
, NULL
, FAL0
) != 0)
568 ret
= mime_write_tohdr(&cin
, fp
);
571 if (ciconv
.s
!= NULL
)
579 mime_write_tohdr_a(struct str
*in
, FILE *f
)
581 char const *cp
, *lastcp
;
587 if ((cp
= routeaddr(in
->s
)) != NULL
&& cp
> lastcp
) {
588 sz
+= convhdra(lastcp
, PTR2SIZE(cp
- lastcp
), f
);
592 for ( ; *cp
!= '\0'; ++cp
) {
595 sz
+= fwrite(lastcp
, 1, PTR2SIZE(cp
- lastcp
+ 1), f
);
597 cp
= skip_comment(cp
);
599 sz
+= convhdra(lastcp
, PTR2SIZE(cp
- lastcp
), f
);
606 if (*cp
== '\\' && cp
[1] != '\0')
613 sz
+= fwrite(lastcp
, 1, PTR2SIZE(cp
- lastcp
), f
);
619 addstr(char **buf
, size_t *sz
, size_t *pos
, char const *str
, size_t len
)
622 *buf
= srealloc(*buf
, *sz
+= len
);
623 memcpy(&(*buf
)[*pos
], str
, len
);
629 addconv(char **buf
, size_t *sz
, size_t *pos
, char const *str
, size_t len
)
636 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
637 addstr(buf
, sz
, pos
, out
.s
, out
.l
);
643 charset_get_7bit(void)
648 if ((t
= ok_vlook(charset_7bit
)) == NULL
)
655 charset_get_8bit(void)
660 if ((t
= ok_vlook(CHARSET_8BIT_OKEY
)) == NULL
)
672 if ((t
= ok_vlook(ttycharset
)) == NULL
)
679 charset_iter_reset(char const *a_charset_to_try_first
)
682 size_t sarrl
[3], len
;
686 sarr
[0] = a_charset_to_try_first
;
688 if ((sarr
[1] = ok_vlook(sendcharsets
)) == NULL
&&
689 ok_blook(sendcharsets_else_ttycharset
))
690 sarr
[1] = charset_get_lc();
692 sarr
[2] = charset_get_8bit();
694 sarrl
[2] = len
= strlen(sarr
[2]);
696 if ((cp
= UNCONST(sarr
[1])) != NULL
)
697 len
+= (sarrl
[1] = strlen(cp
));
700 if ((cp
= UNCONST(sarr
[0])) != NULL
)
701 len
+= (sarrl
[0] = strlen(cp
));
706 _cs_iter_base
= cp
= salloc(len
+ 1 + 1 +1);
709 if ((len
= sarrl
[0]) != 0) {
710 memcpy(cp
, sarr
[0], len
);
714 if ((len
= sarrl
[1]) != 0) {
715 memcpy(cp
, sarr
[1], len
);
721 memcpy(cp
, sarr
[2], len
);
728 charset_iter_next(void)
733 rv
= _cs_iter
= n_strsep(&_cs_iter_base
, ',', TRU1
);
739 charset_iter_current(void)
750 charset_iter_recurse(char *outer_storage
[2]) /* TODO LEGACY FUN, REMOVE */
753 outer_storage
[0] = _cs_iter_base
;
754 outer_storage
[1] = _cs_iter
;
759 charset_iter_restore(char *outer_storage
[2]) /* TODO LEGACY FUN, REMOVE */
762 _cs_iter_base
= outer_storage
[0];
763 _cs_iter
= outer_storage
[1];
769 need_hdrconv(struct header
*hp
, enum gfield w
)
771 char const *ret
= NULL
;
775 if (hp
->h_from
!= NULL
) {
776 if (_name_highbit(hp
->h_from
))
778 } else if (_has_highbit(myaddrs(NULL
)))
780 if (hp
->h_organization
) {
781 if (_has_highbit(hp
->h_organization
))
783 } else if (_has_highbit(ok_vlook(ORGANIZATION
)))
786 if (_name_highbit(hp
->h_replyto
))
788 } else if (_has_highbit(ok_vlook(replyto
)))
791 if (_name_highbit(hp
->h_sender
))
793 } else if (_has_highbit(ok_vlook(sender
)))
796 if ((w
& GTO
) && _name_highbit(hp
->h_to
))
798 if ((w
& GCC
) && _name_highbit(hp
->h_cc
))
800 if ((w
& GBCC
) && _name_highbit(hp
->h_bcc
))
802 if ((w
& GSUBJECT
) && _has_highbit(hp
->h_subject
))
808 #endif /* HAVE_ICONV */
816 if (is_this_enc(p
, "7bit"))
818 else if (is_this_enc(p
, "8bit"))
820 else if (is_this_enc(p
, "base64"))
822 else if (is_this_enc(p
, "binary"))
824 else if (is_this_enc(p
, "quoted-printable"))
833 mime_getparam(char const *param
, char *h
)
835 char *p
= h
, *q
, *rv
= NULL
;
841 if (!whitechar(*p
)) {
843 while (*p
&& (*p
!= ';' || c
== '\\')) {
844 c
= (c
== '\\') ? '\0' : *p
;
852 while (whitechar(*p
))
854 if (!ascncasecmp(p
, param
, sz
)) {
856 while (whitechar(*p
))
862 while (*p
!= '\0' && (*p
!= ';' || c
== '\\')) {
863 if (*p
== '"' && c
!= '\\') {
865 while (*p
!= '\0' && (*p
!= '"' || c
== '\\')) {
866 c
= (c
== '\\') ? '\0' : *p
;
871 c
= (c
== '\\') ? '\0' : *p
;
878 while (whitechar(*p
))
884 if ((q
= strchr(p
, '"')) == NULL
)
887 while (*q
!= '\0' && !whitechar(*q
) && *q
!= ';')
890 sz
= PTR2SIZE(q
- p
);
891 rv
= salloc(q
- p
+1);
900 mime_get_boundary(char *h
, size_t *len
)
906 if ((p
= mime_getparam("boundary", h
)) != NULL
) {
910 q
= salloc(sz
+ 2 +1);
912 memcpy(q
+ 2, p
, sz
);
913 *(q
+ sz
+ 2) = '\0';
920 mime_create_boundary(void)
926 snprintf(bp
, 48, "=_%011lu=-%s=_", (ul_it
)time_current
.tc_time
,
927 getrandstring(47 - (11 + 6)));
933 mime_classify_file(FILE *fp
, char const **contenttype
, char const **charset
,
936 /* TODO classify once only PLEASE PLEASE PLEASE */
937 /* TODO BTW., after the MIME/send layer rewrite we could use a MIME
938 * TODO boundary of "=-=-=" if we would add a B_ in EQ spirit to F_,
939 * TODO and report that state to the outer world */
941 #define F_SIZEOF (sizeof(F_) -1)
943 char f_buf
[F_SIZEOF
], *f_p
= f_buf
;
945 _CLEAN
= 0, /* Plain RFC 2822 message */
946 _NCTT
= 1<<0, /* *contenttype == NULL */
947 _ISTXT
= 1<<1, /* *contenttype =~ text/ */
948 _ISTXTCOK
= 1<<2, /* _ISTXT + *mime-allow-text-controls* */
949 _HIGHBIT
= 1<<3, /* Not 7bit clean */
950 _LONGLINES
= 1<<4, /* MIME_LINELEN_LIMIT exceed. */
951 _CTRLCHAR
= 1<<5, /* Control characters seen */
952 _HASNUL
= 1<<6, /* Contains \0 characters */
953 _NOTERMNL
= 1<<7, /* Lacks a final newline */
954 _TRAILWS
= 1<<8, /* Blanks before NL */
955 _FROM_
= 1<<9 /* ^From_ seen */
957 enum conversion convert
;
962 assert(ftell(fp
) == 0x0l
);
966 if (*contenttype
== NULL
)
968 else if (!ascncasecmp(*contenttype
, "text/", 5))
969 ctt
= ok_blook(mime_allow_text_controls
) ? _ISTXT
| _ISTXTCOK
: _ISTXT
;
970 convert
= _conversion_by_encoding();
975 /* We have to inspect the file content */
976 for (curlen
= 0, c
= EOF
;; ++curlen
) {
982 if (!(ctt
& _ISTXTCOK
))
986 if (c
== '\n' || c
== EOF
) {
987 if (curlen
>= MIME_LINELEN_LIMIT
)
991 if (blankchar(lastc
))
997 /* A bit hairy is handling of \r=\x0D=CR.
999 * Control characters other than TAB, or CR and LF as parts of CRLF
1000 * pairs, must not appear. \r alone does not force _CTRLCHAR below since
1001 * we cannot peek the next character. Thus right here, inspect the last
1002 * seen character for if its \r and set _CTRLCHAR in a delayed fashion */
1003 /*else*/ if (lastc
== '\r')
1006 /* Control character? XXX this is all ASCII here */
1007 if (c
< 0x20 || c
== 0x7F) {
1008 /* RFC 2045, 6.7, as above ... */
1009 if (c
!= '\t' && c
!= '\r')
1011 /* If there is a escape sequence in backslash notation defined for
1012 * this in ANSI X3.159-1989 (ANSI C89), don't treat it as a control
1013 * for real. I.e., \a=\x07=BEL, \b=\x08=BS, \t=\x09=HT. Don't follow
1014 * libmagic(1) in respect to \v=\x0B=VT. \f=\x0C=NP; do ignore
1016 if ((c
>= '\x07' && c
<= '\x0D') || c
== '\x1B')
1018 ctt
|= _HASNUL
; /* Force base64 */
1019 if (!(ctt
& _ISTXTCOK
))
1021 } else if ((ui8_t
)c
& 0x80) {
1023 /* TODO count chars with HIGHBIT? libmagic?
1024 * TODO try encode part - base64 if bails? */
1025 if (!(ctt
& (_NCTT
| _ISTXT
))) { /* TODO _NCTT?? */
1026 ctt
|= _HASNUL
; /* Force base64 */
1029 } else if (!(ctt
& _FROM_
) && UICMP(z
, curlen
, <, F_SIZEOF
)) {
1031 if (UICMP(z
, curlen
, ==, F_SIZEOF
- 1) &&
1032 PTR2SIZE(f_p
- f_buf
) == F_SIZEOF
&&
1033 !memcmp(f_buf
, F_
, F_SIZEOF
))
1041 if (ctt
& _HASNUL
) {
1042 convert
= CONV_TOB64
;
1043 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1044 * on request; else enforce what file(1)/libmagic(3) would suggest */
1045 if (ctt
& _ISTXTCOK
)
1047 if (ctt
& (_NCTT
| _ISTXT
))
1048 *contenttype
= "application/octet-stream";
1049 if (*charset
== NULL
)
1050 *charset
= "binary";
1054 if (ctt
& (_LONGLINES
| _CTRLCHAR
| _NOTERMNL
| _TRAILWS
| _FROM_
)) {
1055 convert
= CONV_TOQP
;
1058 if (ctt
& _HIGHBIT
) {
1060 if (ctt
& (_NCTT
| _ISTXT
))
1061 *do_iconv
= ((ctt
& _HIGHBIT
) != 0);
1064 convert
= CONV_7BIT
;
1066 *contenttype
= "text/plain";
1068 /* Not an attachment with specified charset? */
1070 if (*charset
== NULL
)
1071 *charset
= (ctt
& _HIGHBIT
) ? _CHARSET() : charset_get_7bit();
1080 mime_classify_content_of_part(struct mimepart
const *mip
)
1082 enum mimecontent mc
;
1087 ct
= mip
->m_ct_type_plain
;
1089 if (!asccasecmp(ct
, "application/octet-stream") &&
1090 mip
->m_filename
!= NULL
&& ok_blook(mime_counter_evidence
)) {
1091 ct
= mime_classify_content_type_by_fileext(mip
->m_filename
);
1093 /* TODO how about let *mime-counter-evidence* have
1094 * TODO a value, and if set, saving the attachment in
1095 * TODO a temporary file that mime_classify_file() can
1096 * TODO examine, and using MIME_TEXT if that gives us
1097 * TODO something that seems to be human readable?! */
1100 if (strchr(ct
, '/') == NULL
) /* For compatibility with non-MIME */
1102 else if (!asccasecmp(ct
, "text/plain"))
1103 mc
= MIME_TEXT_PLAIN
;
1104 else if (!asccasecmp(ct
, "text/html"))
1105 mc
= MIME_TEXT_HTML
;
1106 else if (!ascncasecmp(ct
, "text/", 5))
1108 else if (!asccasecmp(ct
, "message/rfc822"))
1110 else if (!ascncasecmp(ct
, "message/", 8))
1112 else if (!asccasecmp(ct
, "multipart/alternative"))
1113 mc
= MIME_ALTERNATIVE
;
1114 else if (!asccasecmp(ct
, "multipart/digest"))
1116 else if (!ascncasecmp(ct
, "multipart/", 10))
1118 else if (!asccasecmp(ct
, "application/x-pkcs7-mime") ||
1119 !asccasecmp(ct
, "application/pkcs7-mime"))
1127 mime_classify_content_type_by_fileext(char const *name
)
1129 char *content
= NULL
;
1134 if ((name
= strrchr(name
, '.')) == NULL
|| *++name
== '\0')
1137 if (_mt_list
== NULL
)
1140 nlen
= strlen(name
);
1141 for (mtn
= _mt_list
; mtn
!= NULL
; mtn
= mtn
->mt_next
) {
1142 char const *ext
= mtn
->mt_line
+ mtn
->mt_mtlen
+ 1,
1145 while (!whitechar(*cp
) && *cp
!= '\0')
1147 /* Better to do case-insensitive comparison on extension, since the
1148 * RFC doesn't specify case of attribute values? */
1149 if (nlen
== PTR2SIZE(cp
- ext
) && !ascncasecmp(name
, ext
, nlen
)) {
1150 content
= savestrbuf(mtn
->mt_line
, mtn
->mt_mtlen
);
1153 while (whitechar(*cp
) && *cp
!= '\0')
1156 } while (*ext
!= '\0');
1164 c_mimetypes(void *v
)
1172 if (argv
[1] != NULL
)
1174 if (!asccasecmp(*argv
, "show"))
1176 if (!asccasecmp(*argv
, "clear"))
1179 fprintf(stderr
, "Synopsis: mimetypes: %s\n",
1180 tr(418, "Either <show> (default) or <clear> the mime.types cache"));
1184 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1190 if (_mt_list
== NULL
)
1193 if ((fp
= Ftmp(NULL
, "mimelist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1200 for (l
= 0, mtn
= _mt_list
; mtn
!= NULL
; ++l
, mtn
= mtn
->mt_next
)
1201 fprintf(fp
, "%s\t%s\n", mtn
->mt_line
, mtn
->mt_line
+ mtn
->mt_mtlen
+ 1);
1203 page_or_print(fp
, l
);
1209 while ((mtn
= _mt_list
) != NULL
) {
1210 _mt_list
= mtn
->mt_next
;
1217 mime_fromhdr(struct str
const *in
, struct str
*out
, enum tdflags flags
)
1219 /* TODO mime_fromhdr(): is called with strings that contain newlines;
1220 * TODO this is the usual newline problem all around the codebase;
1221 * TODO i.e., if we strip it, then the display misses it ;>
1222 * TODO this is why it is so messy and why S-nail v14.2 plus additional
1223 * TODO patch for v14.5.2 (and maybe even v14.5.3 subminor) occurred, and
1224 * TODO why our display reflects what is contained in the message: the 1:1
1225 * TODO relationship of message content and display!
1226 * TODO instead a header line should be decoded to what it is (a single
1227 * TODO line that is) and it should be objective to the backend wether
1228 * TODO it'll be folded to fit onto the display or not, e.g., for search
1229 * TODO purposes etc. then the only condition we have to honour in here
1230 * TODO is that whitespace in between multiple adjacent MIME encoded words
1231 * TODO á la RFC 2047 is discarded; i.e.: this function should deal with
1232 * TODO RFC 2047 and be renamed: mime_fromhdr() -> mime_rfc2047_decode() */
1233 struct str cin
, cout
;
1234 char *p
, *op
, *upper
, *cs
, *cbeg
;
1235 ui32_t convert
, lastenc
, lastoutl
;
1238 iconv_t fhicd
= (iconv_t
)-1;
1244 *(out
->s
= smalloc(1)) = '\0';
1250 tcs
= charset_get_lc();
1254 lastenc
= lastoutl
= 0;
1258 if (*p
== '=' && *(p
+ 1) == '?') {
1261 while (p
< upper
&& *p
!= '?')
1262 ++p
; /* strip charset */
1265 cs
= salloc(PTR2SIZE(++p
- cbeg
));
1266 memcpy(cs
, cbeg
, PTR2SIZE(p
- cbeg
- 1));
1267 cs
[p
- cbeg
- 1] = '\0';
1269 if (fhicd
!= (iconv_t
)-1)
1270 n_iconv_close(fhicd
);
1271 fhicd
= asccasecmp(cs
, tcs
) ? n_iconv_open(tcs
, cs
) : (iconv_t
)-1;
1275 convert
= CONV_FROMB64
;
1278 convert
= CONV_FROMQP
;
1280 default: /* invalid, ignore */
1288 if (PTRCMP(p
+ 1, >=, upper
))
1290 if (*p
++ == '?' && *p
== '=')
1299 if (convert
== CONV_FROMB64
) {
1300 /* XXX Take care for, and strip LF from
1301 * XXX [Invalid Base64 encoding ignored] */
1302 if (b64_decode(&cout
, &cin
, NULL
) == STOP
&&
1303 cout
.s
[cout
.l
- 1] == '\n')
1306 qp_decode(&cout
, &cin
, NULL
);
1310 if ((flags
& TD_ICONV
) && fhicd
!= (iconv_t
)-1) {
1311 cin
.s
= NULL
, cin
.l
= 0; /* XXX string pool ! */
1312 convert
= n_iconv_str(fhicd
, &cin
, &cout
, NULL
, TRU1
);
1313 out
= n_str_add(out
, &cin
);
1314 if (convert
) /* EINVAL at EOS */
1315 out
= n_str_add_buf(out
, "?", 1);
1319 out
= n_str_add(out
, &cout
);
1320 lastenc
= lastoutl
= out
->l
;
1327 onlyws
= (lastenc
> 0);
1331 if (op
[0] == '=' && (PTRCMP(op
+ 1, ==, upper
) || op
[1] == '?'))
1333 if (onlyws
&& !blankchar(*op
))
1337 out
= n_str_add_buf(out
, p
, PTR2SIZE(op
- p
));
1339 if (!onlyws
|| lastoutl
!= lastenc
)
1344 out
->s
[out
->l
] = '\0';
1346 if (flags
& TD_ISPR
) {
1347 makeprint(out
, &cout
);
1351 if (flags
& TD_DELCTRL
)
1352 out
->l
= delctrl(out
->s
, out
->l
);
1354 if (fhicd
!= (iconv_t
)-1)
1355 n_iconv_close(fhicd
);
1363 mime_fromaddr(char const *name
)
1365 char const *cp
, *lastcp
;
1367 size_t ressz
= 1, rescur
= 0;
1372 if (*name
== '\0') {
1373 res
= savestr(name
);
1377 if ((cp
= routeaddr(name
)) != NULL
&& cp
> name
) {
1378 addconv(&res
, &ressz
, &rescur
, name
, PTR2SIZE(cp
- name
));
1383 for ( ; *cp
; ++cp
) {
1386 addstr(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
+ 1));
1388 cp
= skip_comment(cp
);
1390 addconv(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
));
1397 if (*cp
== '\\' && cp
[1] != '\0')
1404 addstr(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
));
1405 /* TODO rescur==0: inserted to silence Coverity ...; check that */
1420 xmime_write(char const *ptr
, size_t size
, FILE *f
, enum conversion convert
,
1421 enum tdflags dflags
, struct str
*rest
)
1424 struct quoteflt
*qf
;
1427 quoteflt_reset(qf
= quoteflt_dummy(), f
);
1428 rv
= mime_write(ptr
, size
, f
, convert
, dflags
, qf
, rest
);
1429 assert(quoteflt_flush(qf
) == 0);
1435 mime_write(char const *ptr
, size_t size
, FILE *f
,
1436 enum conversion convert
, enum tdflags dflags
,
1437 struct quoteflt
*qf
, struct str
*rest
)
1439 /* TODO note: after send/MIME layer rewrite we will have a string pool
1440 * TODO so that memory allocation count drops down massively; for now,
1441 * TODO v14.0 that is, we pay a lot & heavily depend on the allocator */
1447 in
.s
= UNCONST(ptr
);
1452 dflags
|= _TD_BUFCOPY
;
1453 if ((sz
= size
) == 0) {
1454 if (rest
!= NULL
&& rest
->l
!= 0)
1460 if ((dflags
& TD_ICONV
) && iconvd
!= (iconv_t
)-1 &&
1461 (convert
== CONV_TOQP
|| convert
== CONV_8BIT
||
1462 convert
== CONV_TOB64
|| convert
== CONV_TOHDR
)) {
1463 if (n_iconv_str(iconvd
, &out
, &in
, NULL
, FAL0
) != 0) {
1464 /* XXX report conversion error? */;
1470 dflags
&= ~_TD_BUFCOPY
;
1477 state
= qp_decode(&out
, &in
, rest
);
1480 qp_encode(&out
, &in
, QP_NONE
);
1483 sz
= quoteflt_push(qf
, in
.s
, in
.l
);
1488 case CONV_FROMB64_T
:
1489 state
= b64_decode(&out
, &in
, rest
);
1491 if ((sz
= out
.l
) != 0) {
1492 ui32_t opl
= qf
->qf_pfix_len
;
1494 qf
->qf_pfix_len
= 0;
1495 sz
= _fwrite_td(&out
, (dflags
& ~_TD_BUFCOPY
), rest
, qf
);
1496 qf
->qf_pfix_len
= opl
;
1502 b64_encode(&out
, &in
, B64_LF
| B64_MULTILINE
);
1504 sz
= fwrite(out
.s
, sizeof *out
.s
, out
.l
, f
);
1505 if (sz
!= (ssize_t
)out
.l
)
1509 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
| (dflags
& TD_DELCTRL
));
1510 sz
= quoteflt_push(qf
, out
.s
, out
.l
);
1513 sz
= mime_write_tohdr(&in
, f
);
1516 sz
= mime_write_tohdr_a(&in
, f
);
1519 sz
= _fwrite_td(&in
, dflags
, NULL
, qf
);
1531 /* vim:set fenc=utf-8:s-it-mode */