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
;
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
) == 0 || (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
) == 0 && *line
!= '\0')
165 tlen
= PTR2SIZE(line
- typ
);
167 while (blankchar(*line
) != 0 && *line
!= '\0')
173 if (line
[elen
- 1] == '\n' && line
[--elen
] == '\0')
176 mtn
= smalloc(sizeof(struct mtnode
) -
177 VFIELD_SIZEOF(struct mtnode
, mt_line
) + tlen
+ 1 + elen
+1);
179 (*tail
)->mt_next
= mtn
;
184 mtn
->mt_mtlen
= tlen
;
185 memcpy(mtn
->mt_line
, typ
, tlen
);
186 mtn
->mt_line
[tlen
] = '\0';
188 memcpy(mtn
->mt_line
+ tlen
, line
, elen
);
190 mtn
->mt_line
[tlen
] = '\0';
197 _has_highbit(char const *s
)
206 while (*s
++ != '\0');
215 _name_highbit(struct name
*np
)
221 if (_has_highbit(np
->n_name
) || _has_highbit(np
->n_fullname
))
232 static enum conversion
233 _conversion_by_encoding(void)
239 if ((cp
= ok_vlook(encoding
)) == NULL
)
240 ret
= MIME_DEFAULT_ENCODING
;
241 else if (!strcmp(cp
, "quoted-printable"))
243 else if (!strcmp(cp
, "8bit"))
245 else if (!strcmp(cp
, "base64"))
248 fprintf(stderr
, tr(177, "Warning: invalid encoding %s, using base64\n"),
257 _fwrite_td(struct str
const *input
, enum tdflags flags
, struct str
*rest
,
260 /* TODO note: after send/MIME layer rewrite we will have a string pool
261 * TODO so that memory allocation count drops down massively; for now,
262 * TODO v14.* that is, we pay a lot & heavily depend on the allocator */
263 /* TODO well if we get a broken pipe here, and it happens to
264 * TODO happen pretty easy when sleeping in a full pipe buffer,
265 * TODO then the current codebase performs longjump away;
266 * TODO this leaves memory leaks behind ('think up to 3 per,
267 * TODO dep. upon alloca availability). For this to be fixed
268 * TODO we either need to get rid of the longjmp()s (tm) or
269 * TODO the storage must come from the outside or be tracked
270 * TODO in a carrier struct. Best both. But storage reuse
271 * TODO would be a bigbig win besides */
272 /* *input* _may_ point to non-modifyable buffer; but even then it only
273 * needs to be dup'ed away if we have to transform the content */
284 if ((flags
& TD_ICONV
) && iconvd
!= (iconv_t
)-1) {
287 if (rest
!= NULL
&& rest
->l
> 0) {
288 in
.l
= rest
->l
+ input
->l
;
289 in
.s
= buf
= smalloc(in
.l
+ 1);
290 memcpy(in
.s
, rest
->s
, rest
->l
);
291 memcpy(in
.s
+ rest
->l
, input
->s
, input
->l
);
295 if (n_iconv_str(iconvd
, &out
, &in
, &in
, TRU1
) != 0 && rest
!= NULL
&&
297 /* Incomplete multibyte at EOF is special */
298 if (flags
& _TD_EOF
) {
299 out
.s
= srealloc(out
.s
, out
.l
+ 4);
300 /* TODO 0xFFFD out.s[out.l++] = '[';*/
301 out
.s
[out
.l
++] = '?'; /* TODO 0xFFFD !!! */
302 /* TODO 0xFFFD out.s[out.l++] = ']';*/
304 n_str_add(rest
, &in
);
308 flags
&= ~_TD_BUFCOPY
;
316 makeprint(&in
, &out
);
317 else if (flags
& _TD_BUFCOPY
)
318 n_str_dup(&out
, &in
);
321 if (flags
& TD_DELCTRL
)
322 out
.l
= delctrl(out
.s
, out
.l
);
324 rv
= quoteflt_push(qf
, out
.s
, out
.l
);
328 if (in
.s
!= input
->s
)
335 delctrl(char *cp
, size_t sz
)
341 if (!cntrlchar(cp
[x
]))
350 is_this_enc(char const *line
, char const *encoding
)
352 int rv
, c
, quoted
= 0;
358 while (*line
&& *encoding
)
359 if ((c
= *line
++, lowerconv(c
) != *encoding
++))
362 if (quoted
&& *line
== '"')
364 if (*line
== '\0' || whitechar(*line
))
373 mime_write_tohdr(struct str
*in
, FILE *fo
) /* TODO rewrite - FAST! */
375 struct str cin
, cout
;
376 char buf
[B64_LINESIZE
+1]; /* (No CR/LF used) */
377 char const *charset7
, *charset
, *upper
, *wbeg
, *wend
, *lastspc
,
379 size_t sz
= 0, col
= 0, quoteany
, wr
, charsetlen
,
380 maxcol
= 65 /* there is the header field's name, too */;
381 bool_t highbit
, mustquote
, broken
;
384 charset7
= charset_get_7bit();
385 charset
= _CHARSET();
386 wr
= strlen(charset7
);
387 charsetlen
= strlen(charset
);
388 charsetlen
= MAX(charsetlen
, wr
);
389 upper
= in
->s
+ in
->l
;
391 /* xxx note this results in too much hits since =/? force quoting even
392 * xxx if they don't form =? etc. */
393 quoteany
= mime_cte_mustquote(in
->s
, in
->l
, TRU1
);
397 for (wbeg
= in
->s
; wbeg
< upper
; ++wbeg
)
398 if ((ui8_t
)*wbeg
& 0x80)
401 if (quoteany
<< 1 > in
->l
) {
402 /* Print the entire field in base64 */
403 for (wbeg
= in
->s
; wbeg
< upper
; wbeg
= wend
) {
405 cin
.s
= UNCONST(wbeg
);
408 if (cin
.l
* 4/3 + 7 + charsetlen
< maxcol
- col
) {
411 wr
= fprintf(fo
, "=?%s?B?%s?=", (highbit
? charset
: charset7
),
412 b64_encode(&cout
, &cin
, B64_BUF
)->s
);
416 fwrite("\n ", sizeof(char), 2, fo
);
434 /* Print the field word-wise in quoted-printable */
436 for (wbeg
= in
->s
; wbeg
< upper
; wbeg
= wend
) {
438 while (wbeg
< upper
&& whitechar(*wbeg
)) {
439 lastspc
= lastspc
? lastspc
: wbeg
;
446 while (lastspc
< wbeg
) {
447 putc(*lastspc
&0377, fo
);
457 for (wend
= wbeg
; wend
< upper
&& !whitechar(*wend
); ++wend
)
458 if ((ui8_t
)*wend
& 0x80)
460 mustquote
= (mime_cte_mustquote(wbeg
, PTR2SIZE(wend
- wbeg
), TRU1
)
463 if (mustquote
|| broken
|| ((wend
- wbeg
) >= 76-5 && quoteany
)) {
464 for (cout
.s
= NULL
;;) {
465 cin
.s
= UNCONST(lastwordend
? lastwordend
: wbeg
);
466 cin
.l
= wend
- cin
.s
;
467 qp_encode(&cout
, &cin
, QP_ISHEAD
);
468 wr
= cout
.l
+ charsetlen
+ 7;
470 if (col
<= maxcol
&& wr
<= maxcol
- col
) {
472 /* TODO because we included the WS in the encoded str,
474 * TODO RFC: "any 'linear-white-space' that separates
475 * TODO a pair of adjacent 'encoded-word's is ignored" */
480 fprintf(fo
, "=?%s?Q?%.*s?=",
481 (highbit
? charset
: charset7
), (int)cout
.l
, cout
.s
);
485 } else if (col
> 1) {
486 /* TODO assuming SP separator, ignore *lastspc* !?? */
488 if (lastspc
!= NULL
) {
513 if (col
&& PTR2SIZE(wend
- wbeg
) > maxcol
- col
) {
518 if (lastspc
== NULL
) {
523 maxcol
-= wbeg
- lastspc
;
526 while (lastspc
< wbeg
) {
527 putc(*lastspc
&0377, fo
);
531 wr
= fwrite(wbeg
, sizeof *wbeg
, PTR2SIZE(wend
- wbeg
), fo
);
543 convhdra(char const *str
, size_t len
, FILE *fp
)
552 cin
.s
= UNCONST(str
);
556 if (iconvd
!= (iconv_t
)-1) {
558 if (n_iconv_str(iconvd
, &ciconv
, &cin
, NULL
, FAL0
) != 0)
563 ret
= mime_write_tohdr(&cin
, fp
);
566 if (ciconv
.s
!= NULL
)
574 mime_write_tohdr_a(struct str
*in
, FILE *f
)
576 char const *cp
, *lastcp
;
582 if ((cp
= routeaddr(in
->s
)) != NULL
&& cp
> lastcp
) {
583 sz
+= convhdra(lastcp
, cp
- lastcp
, f
);
587 for ( ; *cp
!= '\0'; ++cp
) {
590 sz
+= fwrite(lastcp
, 1, PTR2SIZE(cp
- lastcp
+ 1), f
);
592 cp
= skip_comment(cp
);
594 sz
+= convhdra(lastcp
, PTR2SIZE(cp
- lastcp
), f
);
601 if (*cp
== '\\' && cp
[1] != '\0')
608 sz
+= fwrite(lastcp
, 1, PTR2SIZE(cp
- lastcp
), f
);
614 addstr(char **buf
, size_t *sz
, size_t *pos
, char const *str
, size_t len
)
617 *buf
= srealloc(*buf
, *sz
+= len
);
618 memcpy(&(*buf
)[*pos
], str
, len
);
624 addconv(char **buf
, size_t *sz
, size_t *pos
, char const *str
, size_t len
)
631 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
632 addstr(buf
, sz
, pos
, out
.s
, out
.l
);
638 charset_get_7bit(void)
643 if ((t
= ok_vlook(charset_7bit
)) == NULL
)
650 charset_get_8bit(void)
655 if ((t
= ok_vlook(CHARSET_8BIT_OKEY
)) == NULL
)
667 if ((t
= ok_vlook(ttycharset
)) == NULL
)
674 charset_iter_reset(char const *a_charset_to_try_first
)
677 size_t sarrl
[3], len
;
681 sarr
[0] = a_charset_to_try_first
;
683 if ((sarr
[1] = ok_vlook(sendcharsets
)) == NULL
&&
684 ok_blook(sendcharsets_else_ttycharset
))
685 sarr
[1] = charset_get_lc();
687 sarr
[2] = charset_get_8bit();
689 sarrl
[2] = len
= strlen(sarr
[2]);
691 if ((cp
= UNCONST(sarr
[1])) != NULL
)
692 len
+= (sarrl
[1] = strlen(cp
));
695 if ((cp
= UNCONST(sarr
[0])) != NULL
)
696 len
+= (sarrl
[0] = strlen(cp
));
701 _cs_iter_base
= cp
= salloc(len
+ 1 + 1 +1);
704 if ((len
= sarrl
[0]) != 0) {
705 memcpy(cp
, sarr
[0], len
);
709 if ((len
= sarrl
[1]) != 0) {
710 memcpy(cp
, sarr
[1], len
);
716 memcpy(cp
, sarr
[2], len
);
723 charset_iter_next(void)
728 rv
= _cs_iter
= n_strsep(&_cs_iter_base
, ',', TRU1
);
734 charset_iter_current(void)
745 charset_iter_recurse(char *outer_storage
[2]) /* TODO LEGACY FUN, REMOVE */
748 outer_storage
[0] = _cs_iter_base
;
749 outer_storage
[1] = _cs_iter
;
754 charset_iter_restore(char *outer_storage
[2]) /* TODO LEGACY FUN, REMOVE */
757 _cs_iter_base
= outer_storage
[0];
758 _cs_iter
= outer_storage
[1];
764 need_hdrconv(struct header
*hp
, enum gfield w
)
766 char const *ret
= NULL
;
770 if (hp
->h_from
!= NULL
) {
771 if (_name_highbit(hp
->h_from
))
773 } else if (_has_highbit(myaddrs(NULL
)))
775 if (hp
->h_organization
) {
776 if (_has_highbit(hp
->h_organization
))
778 } else if (_has_highbit(ok_vlook(ORGANIZATION
)))
781 if (_name_highbit(hp
->h_replyto
))
783 } else if (_has_highbit(ok_vlook(replyto
)))
786 if (_name_highbit(hp
->h_sender
))
788 } else if (_has_highbit(ok_vlook(sender
)))
791 if (w
& GTO
&& _name_highbit(hp
->h_to
))
793 if (w
& GCC
&& _name_highbit(hp
->h_cc
))
795 if (w
& GBCC
&& _name_highbit(hp
->h_bcc
))
797 if (w
& GSUBJECT
&& _has_highbit(hp
->h_subject
))
803 #endif /* HAVE_ICONV */
811 if (is_this_enc(p
, "7bit"))
813 else if (is_this_enc(p
, "8bit"))
815 else if (is_this_enc(p
, "base64"))
817 else if (is_this_enc(p
, "binary"))
819 else if (is_this_enc(p
, "quoted-printable"))
828 mime_getparam(char const *param
, char *h
)
830 char *p
= h
, *q
, *rv
= NULL
;
836 if (!whitechar(*p
)) {
838 while (*p
&& (*p
!= ';' || c
== '\\')) {
839 c
= (c
== '\\') ? '\0' : *p
;
847 while (whitechar(*p
))
849 if (!ascncasecmp(p
, param
, sz
)) {
851 while (whitechar(*p
))
857 while (*p
&& (*p
!= ';' || c
== '\\')) {
858 if (*p
== '"' && c
!= '\\') {
860 while (*p
&& (*p
!= '"' || c
== '\\')) {
861 c
= (c
== '\\') ? '\0' : *p
;
866 c
= (c
== '\\') ? '\0' : *p
;
873 while (whitechar(*p
))
879 if ((q
= strchr(p
, '"')) == NULL
)
882 while (*q
&& !whitechar(*q
) && *q
!= ';')
886 rv
= salloc(q
- p
+1);
895 mime_get_boundary(char *h
, size_t *len
)
901 if ((p
= mime_getparam("boundary", h
)) != NULL
) {
905 q
= salloc(sz
+ 2 +1);
907 memcpy(q
+ 2, p
, sz
);
908 *(q
+ sz
+ 2) = '\0';
915 mime_create_boundary(void)
921 snprintf(bp
, 48, "=_%011lu=-%s=_", (ul_it
)time_current
.tc_time
,
922 getrandstring(47 - (11 + 6)));
928 mime_classify_file(FILE *fp
, char const **contenttype
, char const **charset
,
931 /* TODO classify once only PLEASE PLEASE PLEASE */
932 /* TODO BTW., after the MIME/send layer rewrite we could use a MIME
933 * TODO boundary of "=-=-=" if we would add a B_ in EQ spirit to F_,
934 * TODO and report that state to the outer world */
936 #define F_SIZEOF (sizeof(F_) - 1)
938 char f_buf
[F_SIZEOF
], *f_p
= f_buf
;
940 _CLEAN
= 0, /* Plain RFC 2822 message */
941 _NCTT
= 1<<0, /* *contenttype == NULL */
942 _ISTXT
= 1<<1, /* *contenttype =~ text/ */
943 _ISTXTCOK
= 1<<2, /* _ISTXT+*mime-allow-text-controls* */
944 _HIGHBIT
= 1<<3, /* Not 7bit clean */
945 _LONGLINES
= 1<<4, /* MIME_LINELEN_LIMIT exceed. */
946 _CTRLCHAR
= 1<<5, /* Control characters seen */
947 _HASNUL
= 1<<6, /* Contains \0 characters */
948 _NOTERMNL
= 1<<7, /* Lacks a final newline */
949 _TRAILWS
= 1<<8, /* Blanks before NL */
950 _FROM_
= 1<<9 /* ^From_ seen */
952 enum conversion convert
;
957 assert(ftell(fp
) == 0x0l
);
961 if (*contenttype
== NULL
)
963 else if (!ascncasecmp(*contenttype
, "text/", 5))
964 ctt
= ok_blook(mime_allow_text_controls
) ? _ISTXT
| _ISTXTCOK
: _ISTXT
;
965 convert
= _conversion_by_encoding();
970 /* We have to inspect the file content */
971 for (curlen
= 0, c
= EOF
;; ++curlen
) {
977 if ((ctt
& _ISTXTCOK
) == 0)
981 if (c
== '\n' || c
== EOF
) {
982 if (curlen
>= MIME_LINELEN_LIMIT
)
986 if (blankchar(lastc
))
992 /* A bit hairy is handling of \r=\x0D=CR.
994 * Control characters other than TAB, or CR and LF as parts of CRLF
995 * pairs, must not appear. \r alone does not force _CTRLCHAR below since
996 * we cannot peek the next character. Thus right here, inspect the last
997 * seen character for if its \r and set _CTRLCHAR in a delayed fashion */
998 /*else*/ if (lastc
== '\r')
1001 /* Control character? */
1002 if (c
< 0x20 || c
== 0x7F) {
1003 /* RFC 2045, 6.7, as above ... */
1004 if (c
!= '\t' && c
!= '\r')
1006 /* If there is a escape sequence in backslash notation defined for
1007 * this in ANSI X3.159-1989 (ANSI C89), don't treat it as a control
1008 * for real. I.e., \a=\x07=BEL, \b=\x08=BS, \t=\x09=HT. Don't follow
1009 * libmagic(1) in respect to \v=\x0B=VT. \f=\x0C=NP; do ignore
1011 if ((c
>= '\x07' && c
<= '\x0D') || c
== '\x1B')
1013 ctt
|= _HASNUL
; /* Force base64 */
1014 if ((ctt
& _ISTXTCOK
) == 0)
1016 } else if (c
& 0x80) {
1018 /* TODO count chars with HIGHBIT? libmagic?
1019 * TODO try encode part - base64 if bails? */
1020 if ((ctt
& (_NCTT
|_ISTXT
)) == 0) { /* TODO _NCTT?? */
1021 ctt
|= _HASNUL
; /* Force base64 */
1024 } else if ((ctt
& _FROM_
) == 0 && curlen
< (sl_it
)F_SIZEOF
) {
1026 if (curlen
== (sl_it
)(F_SIZEOF
- 1) &&
1027 PTR2SIZE(f_p
- f_buf
) == F_SIZEOF
&&
1028 !memcmp(f_buf
, F_
, F_SIZEOF
))
1036 if (ctt
& _HASNUL
) {
1037 convert
= CONV_TOB64
;
1038 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1039 * on request; else enforce what file(1)/libmagic(3) would suggest */
1040 if (ctt
& _ISTXTCOK
)
1042 if (ctt
& (_NCTT
| _ISTXT
))
1043 *contenttype
= "application/octet-stream";
1044 if (*charset
== NULL
)
1045 *charset
= "binary";
1049 if (ctt
& (_LONGLINES
| _CTRLCHAR
| _NOTERMNL
| _TRAILWS
| _FROM_
)) {
1050 convert
= CONV_TOQP
;
1053 if (ctt
& _HIGHBIT
) {
1055 if (ctt
& (_NCTT
|_ISTXT
))
1056 *do_iconv
= (ctt
& _HIGHBIT
) != 0;
1059 convert
= CONV_7BIT
;
1061 *contenttype
= "text/plain";
1063 /* Not an attachment with specified charset? */
1065 if (*charset
== NULL
)
1066 *charset
= (ctt
& _HIGHBIT
) ? _CHARSET() : charset_get_7bit();
1075 mime_classify_content_of_part(struct mimepart
const *mip
)
1077 enum mimecontent mc
;
1082 ct
= mip
->m_ct_type_plain
;
1084 if (!asccasecmp(ct
, "application/octet-stream") &&
1085 mip
->m_filename
!= NULL
&& ok_blook(mime_counter_evidence
)) {
1086 ct
= mime_classify_content_type_by_fileext(mip
->m_filename
);
1088 /* TODO how about let *mime-counter-evidence* have
1089 * TODO a value, and if set, saving the attachment in
1090 * TODO a temporary file that mime_classify_file() can
1091 * TODO examine, and using MIME_TEXT if that gives us
1092 * TODO something that seems to be human readable?! */
1095 if (strchr(ct
, '/') == NULL
) /* For compatibility with non-MIME */
1097 else if (!asccasecmp(ct
, "text/plain"))
1098 mc
= MIME_TEXT_PLAIN
;
1099 else if (!asccasecmp(ct
, "text/html"))
1100 mc
= MIME_TEXT_HTML
;
1101 else if (!ascncasecmp(ct
, "text/", 5))
1103 else if (!asccasecmp(ct
, "message/rfc822"))
1105 else if (!ascncasecmp(ct
, "message/", 8))
1107 else if (!asccasecmp(ct
, "multipart/alternative"))
1108 mc
= MIME_ALTERNATIVE
;
1109 else if (!asccasecmp(ct
, "multipart/digest"))
1111 else if (!ascncasecmp(ct
, "multipart/", 10))
1113 else if (!asccasecmp(ct
, "application/x-pkcs7-mime") ||
1114 !asccasecmp(ct
, "application/pkcs7-mime"))
1122 mime_classify_content_type_by_fileext(char const *name
)
1124 char *content
= NULL
;
1129 if ((name
= strrchr(name
, '.')) == NULL
|| *++name
== '\0')
1132 if (_mt_list
== NULL
)
1135 nlen
= strlen(name
);
1136 for (mtn
= _mt_list
; mtn
!= NULL
; mtn
= mtn
->mt_next
) {
1137 char const *ext
= mtn
->mt_line
+ mtn
->mt_mtlen
+ 1,
1140 while (!whitechar(*cp
) && *cp
!= '\0')
1142 /* Better to do case-insensitive comparison on extension, since the
1143 * RFC doesn't specify case of attribute values? */
1144 if (nlen
== PTR2SIZE(cp
- ext
) && !ascncasecmp(name
, ext
, nlen
)) {
1145 content
= savestrbuf(mtn
->mt_line
, mtn
->mt_mtlen
);
1148 while (whitechar(*cp
) && *cp
!= '\0')
1151 } while (*ext
!= '\0');
1159 c_mimetypes(void *v
)
1167 if (argv
[1] != NULL
)
1169 if (!asccasecmp(*argv
, "show"))
1171 if (!asccasecmp(*argv
, "clear"))
1174 fprintf(stderr
, "Synopsis: mimetypes: %s\n",
1175 tr(418, "Either <show> (default) or <clear> the mime.types cache"));
1179 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1185 if (_mt_list
== NULL
)
1188 if ((fp
= Ftmp(NULL
, "mimelist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1195 for (l
= 0, mtn
= _mt_list
; mtn
!= NULL
; ++l
, mtn
= mtn
->mt_next
)
1196 fprintf(fp
, "%s\t%s\n", mtn
->mt_line
, mtn
->mt_line
+ mtn
->mt_mtlen
+ 1);
1198 page_or_print(fp
, l
);
1204 while ((mtn
= _mt_list
) != NULL
) {
1205 _mt_list
= mtn
->mt_next
;
1212 mime_fromhdr(struct str
const *in
, struct str
*out
, enum tdflags flags
)
1214 /* TODO mime_fromhdr(): is called with strings that contain newlines;
1215 * TODO this is the usual newline problem all around the codebase;
1216 * TODO i.e., if we strip it, then the display misses it ;>
1217 * TODO this is why it is so messy and why S-nail v14.2 plus additional
1218 * TODO patch for v14.5.2 (and maybe even v14.5.3 subminor) occurred, and
1219 * TODO why our display reflects what is contained in the message: the 1:1
1220 * TODO relationship of message content and display!
1221 * TODO instead a header line should be decoded to what it is (a single
1222 * TODO line that is) and it should be objective to the backend wether
1223 * TODO it'll be folded to fit onto the display or not, e.g., for search
1224 * TODO purposes etc. then the only condition we have to honour in here
1225 * TODO is that whitespace in between multiple adjacent MIME encoded words
1226 * TODO á la RFC 2047 is discarded; i.e.: this function should deal with
1227 * TODO RFC 2047 and be renamed: mime_fromhdr() -> mime_rfc2047_decode() */
1228 struct str cin
, cout
;
1229 char *p
, *op
, *upper
, *cs
, *cbeg
;
1230 ui32_t convert
, lastenc
, lastoutl
;
1233 iconv_t fhicd
= (iconv_t
)-1;
1239 *(out
->s
= smalloc(1)) = '\0';
1245 tcs
= charset_get_lc();
1249 lastenc
= lastoutl
= 0;
1253 if (*p
== '=' && *(p
+ 1) == '?') {
1256 while (p
< upper
&& *p
!= '?')
1257 ++p
; /* strip charset */
1260 cs
= salloc(PTR2SIZE(++p
- cbeg
));
1261 memcpy(cs
, cbeg
, PTR2SIZE(p
- cbeg
- 1));
1262 cs
[p
- cbeg
- 1] = '\0';
1264 if (fhicd
!= (iconv_t
)-1)
1265 n_iconv_close(fhicd
);
1266 fhicd
= asccasecmp(cs
, tcs
) ? n_iconv_open(tcs
, cs
) : (iconv_t
)-1;
1270 convert
= CONV_FROMB64
;
1273 convert
= CONV_FROMQP
;
1275 default: /* invalid, ignore */
1283 if (PTRCMP(p
+ 1, >=, upper
))
1285 if (*p
++ == '?' && *p
== '=')
1294 if (convert
== CONV_FROMB64
) {
1295 /* XXX Take care for, and strip LF from
1296 * XXX [Invalid Base64 encoding ignored] */
1297 if (b64_decode(&cout
, &cin
, NULL
) == STOP
&&
1298 cout
.s
[cout
.l
- 1] == '\n')
1301 qp_decode(&cout
, &cin
, NULL
);
1305 if ((flags
& TD_ICONV
) && fhicd
!= (iconv_t
)-1) {
1306 cin
.s
= NULL
, cin
.l
= 0; /* XXX string pool ! */
1307 convert
= n_iconv_str(fhicd
, &cin
, &cout
, NULL
, TRU1
);
1308 out
= n_str_add(out
, &cin
);
1309 if (convert
) /* EINVAL at EOS */
1310 out
= n_str_add_buf(out
, "?", 1);
1314 out
= n_str_add(out
, &cout
);
1315 lastenc
= lastoutl
= out
->l
;
1322 onlyws
= (lastenc
> 0);
1326 if (op
[0] == '=' && (PTRCMP(op
+ 1, ==, upper
) || op
[1] == '?'))
1328 if (onlyws
&& !blankchar(*op
))
1332 out
= n_str_add_buf(out
, p
, PTR2SIZE(op
- p
));
1334 if (!onlyws
|| lastoutl
!= lastenc
)
1339 out
->s
[out
->l
] = '\0';
1341 if (flags
& TD_ISPR
) {
1342 makeprint(out
, &cout
);
1346 if (flags
& TD_DELCTRL
)
1347 out
->l
= delctrl(out
->s
, out
->l
);
1349 if (fhicd
!= (iconv_t
)-1)
1350 n_iconv_close(fhicd
);
1358 mime_fromaddr(char const *name
)
1360 char const *cp
, *lastcp
;
1362 size_t ressz
= 1, rescur
= 0;
1367 if (*name
== '\0') {
1368 res
= savestr(name
);
1372 if ((cp
= routeaddr(name
)) != NULL
&& cp
> name
) {
1373 addconv(&res
, &ressz
, &rescur
, name
, PTR2SIZE(cp
- name
));
1378 for ( ; *cp
; ++cp
) {
1381 addstr(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
+ 1));
1383 cp
= skip_comment(cp
);
1385 addconv(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
));
1392 if (*cp
== '\\' && cp
[1] != '\0')
1399 addstr(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
));
1400 /* TODO rescur==0: inserted to silence Coverity ...; check that */
1415 xmime_write(char const *ptr
, size_t size
, FILE *f
, enum conversion convert
,
1416 enum tdflags dflags
, struct str
*rest
)
1419 struct quoteflt
*qf
;
1422 quoteflt_reset(qf
= quoteflt_dummy(), f
);
1423 rv
= mime_write(ptr
, size
, f
, convert
, dflags
, qf
, rest
);
1424 assert(quoteflt_flush(qf
) == 0);
1430 mime_write(char const *ptr
, size_t size
, FILE *f
,
1431 enum conversion convert
, enum tdflags dflags
,
1432 struct quoteflt
*qf
, struct str
*rest
)
1434 /* TODO note: after send/MIME layer rewrite we will have a string pool
1435 * TODO so that memory allocation count drops down massively; for now,
1436 * TODO v14.0 that is, we pay a lot & heavily depend on the allocator */
1442 in
.s
= UNCONST(ptr
);
1447 dflags
|= _TD_BUFCOPY
;
1448 if ((sz
= size
) == 0) {
1449 if (rest
!= NULL
&& rest
->l
!= 0)
1455 if ((dflags
& TD_ICONV
) && iconvd
!= (iconv_t
)-1 &&
1456 (convert
== CONV_TOQP
|| convert
== CONV_8BIT
||
1457 convert
== CONV_TOB64
|| convert
== CONV_TOHDR
)) {
1458 if (n_iconv_str(iconvd
, &out
, &in
, NULL
, FAL0
) != 0) {
1459 /* XXX report conversion error? */;
1465 dflags
&= ~_TD_BUFCOPY
;
1472 state
= qp_decode(&out
, &in
, rest
);
1475 qp_encode(&out
, &in
, QP_NONE
);
1478 sz
= quoteflt_push(qf
, in
.s
, in
.l
);
1483 case CONV_FROMB64_T
:
1484 state
= b64_decode(&out
, &in
, rest
);
1486 if ((sz
= out
.l
) != 0) {
1487 ui32_t opl
= qf
->qf_pfix_len
;
1489 qf
->qf_pfix_len
= 0;
1490 sz
= _fwrite_td(&out
, (dflags
& ~_TD_BUFCOPY
), rest
,qf
);
1491 qf
->qf_pfix_len
= opl
;
1497 b64_encode(&out
, &in
, B64_LF
| B64_MULTILINE
);
1499 sz
= fwrite(out
.s
, sizeof *out
.s
, out
.l
, f
);
1500 if (sz
!= (ssize_t
)out
.l
)
1504 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
| (dflags
& TD_DELCTRL
));
1505 sz
= quoteflt_push(qf
, out
.s
, out
.l
);
1508 sz
= mime_write_tohdr(&in
, f
);
1511 sz
= mime_write_tohdr_a(&in
, f
);
1514 sz
= _fwrite_td(&in
, dflags
, NULL
, qf
);
1526 /* vim:set fenc=utf-8:s-it-mode */