Home | History | Annotate | Line # | Download | only in mail
mime_decode.c revision 1.1
      1  1.1  christos /*	$NetBSD: mime_decode.c,v 1.1 2006/10/21 21:37:21 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Anon Ymous.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19  1.1  christos  *    must display the following acknowledgement:
     20  1.1  christos  *        This product includes software developed by the NetBSD
     21  1.1  christos  *        Foundation, Inc. and its contributors.
     22  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  christos  *    contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  christos  */
     38  1.1  christos 
     39  1.1  christos 
     40  1.1  christos #ifdef MIME_SUPPORT
     41  1.1  christos 
     42  1.1  christos #include <sys/cdefs.h>
     43  1.1  christos #ifndef __lint__
     44  1.1  christos __RCSID("$NetBSD: mime_decode.c,v 1.1 2006/10/21 21:37:21 christos Exp $");
     45  1.1  christos #endif /* not __lint__ */
     46  1.1  christos 
     47  1.1  christos #include <assert.h>
     48  1.1  christos #include <err.h>
     49  1.1  christos #include <fcntl.h>
     50  1.1  christos #include <libgen.h>
     51  1.1  christos #include <setjmp.h>
     52  1.1  christos #include <signal.h>
     53  1.1  christos #include <stdio.h>
     54  1.1  christos #include <stdlib.h>
     55  1.1  christos #include <string.h>
     56  1.1  christos #include <unistd.h>
     57  1.1  christos #include <iconv.h>
     58  1.1  christos 
     59  1.1  christos #include "def.h"
     60  1.1  christos #include "extern.h"
     61  1.1  christos #ifdef USE_EDITLINE
     62  1.1  christos #include "complete.h"
     63  1.1  christos #endif
     64  1.1  christos #ifdef MIME_SUPPORT
     65  1.1  christos #include "mime.h"
     66  1.1  christos #include "mime_child.h"
     67  1.1  christos #include "mime_codecs.h"
     68  1.1  christos #include "mime_header.h"
     69  1.1  christos #endif
     70  1.1  christos #include "glob.h"
     71  1.1  christos 
     72  1.1  christos 
     73  1.1  christos /************************************************
     74  1.1  christos  * The fundametal data structure for this module!
     75  1.1  christos  */
     76  1.1  christos struct mime_info {
     77  1.1  christos 	struct mime_info *mi_blink;
     78  1.1  christos 	struct mime_info *mi_flink;
     79  1.1  christos 
     80  1.1  christos 	/* sendmessage -> decoder -> filter -> pager */
     81  1.1  christos 
     82  1.1  christos 	FILE *mi_fo;		/* output file handle pointing to PAGER */
     83  1.1  christos 	FILE *mi_pipe_end;	/* initial end of pipe */
     84  1.1  christos 	FILE *mi_head_end;	/* close to here at start of body */
     85  1.1  christos 
     86  1.1  christos 	int mi_partnum;		/* part number displayed (if nonzero) */
     87  1.1  christos 
     88  1.1  christos 	const char *mi_version;
     89  1.1  christos 	const char *mi_type;
     90  1.1  christos 	const char *mi_subtype;
     91  1.1  christos 	const char *mi_boundary;
     92  1.1  christos 	const char *mi_charset;
     93  1.1  christos 	const char *mi_encoding;
     94  1.1  christos 
     95  1.1  christos 	struct message *mp;		/* MP for this message regarded as a part. */
     96  1.1  christos 	struct {
     97  1.1  christos 		struct mime_info *mip;	/* parent of part of multipart message */
     98  1.1  christos 		struct message *mp;
     99  1.1  christos 	} mi_parent;
    100  1.1  christos 
    101  1.1  christos 	const char *mi_command_hook;	/* alternate command used to process this message */
    102  1.1  christos };
    103  1.1  christos 
    104  1.1  christos 
    105  1.1  christos #if 0
    106  1.1  christos #ifndef __lint__
    107  1.1  christos /*
    108  1.1  christos  * XXX - This block for debugging only and eventually should go away.
    109  1.1  christos  */
    110  1.1  christos static void
    111  1.1  christos show_one_mime_info(FILE *fp, struct mime_info *mip)
    112  1.1  christos {
    113  1.1  christos #define XX(a) (a) ? (a) : "<null>"
    114  1.1  christos 
    115  1.1  christos 	(void)fprintf(fp, ">> --------\n");
    116  1.1  christos 	(void)fprintf(fp, "mip %d:\n", mip->mi_partnum);
    117  1.1  christos 	(void)fprintf(fp, "** Version: %s\n",  XX(mip->mi_version));
    118  1.1  christos 	(void)fprintf(fp, "** type: %s\n",     XX(mip->mi_type));
    119  1.1  christos 	(void)fprintf(fp, "** subtype: %s\n",  XX(mip->mi_subtype));
    120  1.1  christos 	(void)fprintf(fp, "** charset: %s\n",  XX(mip->mi_charset));
    121  1.1  christos 	(void)fprintf(fp, "** encoding: %s\n", XX(mip->mi_encoding));
    122  1.1  christos 	(void)fprintf(fp, "** boundary: %s\n", XX(mip->mi_boundary));
    123  1.1  christos 	(void)fprintf(fp, "** %p: flag: 0x%x, block: %ld, offset: %d, size: %lld, lines: %ld:%ld\n",
    124  1.1  christos 	    mip->mp,
    125  1.1  christos 	    mip->mp->m_flag,
    126  1.1  christos 	    mip->mp->m_block, mip->mp->m_offset, mip->mp->m_size,
    127  1.1  christos 	    mip->mp->m_lines, mip->mp->m_blines);
    128  1.1  christos 	(void)fprintf(fp, "** mip: %p\n", mip);
    129  1.1  christos 	(void)fprintf(fp, "** mi_flink: %p\n", mip->mi_flink);
    130  1.1  christos 	(void)fprintf(fp, "** mi_blink: %p\n", mip->mi_blink);
    131  1.1  christos 	(void)fprintf(fp, "** mip %p, mp %p,  parent_mip %p, parent_mp %p\n",
    132  1.1  christos 	    mip, mip->mp, mip->mi_parent.mip, mip->mi_parent.mp);
    133  1.1  christos 
    134  1.1  christos 	(void)fprintf(fp, "** mi_fo %p, mi_head_end %p, mi_pipe_end %p\n",
    135  1.1  christos 	    mip->mi_fo, mip->mi_head_end, mip->mi_pipe_end);
    136  1.1  christos 
    137  1.1  christos 	(void)fprintf(fp, "** mi_partnum: %d\n", mip->mi_partnum);
    138  1.1  christos 
    139  1.1  christos 	(void)fflush(fp);
    140  1.1  christos 
    141  1.1  christos #undef XX
    142  1.1  christos }
    143  1.1  christos 
    144  1.1  christos __unused
    145  1.1  christos static void
    146  1.1  christos show_mime_info(FILE *fp, struct mime_info *mip, struct mime_info *end_mip)
    147  1.1  christos {
    148  1.1  christos 	for (/* EMTPY */; mip != end_mip; mip = mip->mi_flink)
    149  1.1  christos 		show_one_mime_info(fp, mip);
    150  1.1  christos 
    151  1.1  christos 	(void)fprintf(fp, "++ =========\n");
    152  1.1  christos 	(void)fflush(fp);
    153  1.1  christos }
    154  1.1  christos #endif /* __lint__ */
    155  1.1  christos #endif /* #if */
    156  1.1  christos 
    157  1.1  christos 
    158  1.1  christos /*
    159  1.1  christos  * Our interface to the file registry in popen.c
    160  1.1  christos  */
    161  1.1  christos static FILE *
    162  1.1  christos pipe_end(struct mime_info *mip)
    163  1.1  christos {
    164  1.1  christos 	FILE *fp;
    165  1.1  christos 	fp = last_registered_file(1);	/* get last registered pipe */
    166  1.1  christos 	if (fp == NULL)
    167  1.1  christos 		fp = mip->mi_fo;
    168  1.1  christos 	return fp;
    169  1.1  christos }
    170  1.1  christos 
    171  1.1  christos /*
    172  1.1  christos  * Copy the first ';' delimited substring from 'src' (null terminated)
    173  1.1  christos  * into 'dst', expanding quotes and removing comments (as per RFC
    174  1.1  christos  * 822).  Returns a pointer in src to the next non-white character
    175  1.1  christos  * following ';'.  The caller is responsible for ensuring 'dst' is
    176  1.1  christos  * sufficiently large to hold the result.
    177  1.1  christos  */
    178  1.1  christos static char *
    179  1.1  christos get_param(char *dst, char *src)
    180  1.1  christos {
    181  1.1  christos 	char *lastq;
    182  1.1  christos 	char *cp;
    183  1.1  christos 	char *cp2;
    184  1.1  christos 	int nesting;
    185  1.1  christos 
    186  1.1  christos 	cp2 = dst;
    187  1.1  christos 	lastq = dst;
    188  1.1  christos 	for (cp = src; *cp && *cp != ';'; cp++) {
    189  1.1  christos 		switch (*cp) {
    190  1.1  christos 		case '"':	/* start of quoted string */
    191  1.1  christos 			for (cp++; *cp; cp++) {
    192  1.1  christos 				if (*cp == '"')
    193  1.1  christos 					break;
    194  1.1  christos 				if (*cp == '\\' && cp[1] != '\0')
    195  1.1  christos 					++cp;
    196  1.1  christos 				*cp2++ = *cp;
    197  1.1  christos 			}
    198  1.1  christos 			lastq = cp2-1;
    199  1.1  christos 			break;
    200  1.1  christos 		case '(':	/* start of comment */
    201  1.1  christos 			nesting = 1;
    202  1.1  christos 			while (nesting > 0 && *++cp) {
    203  1.1  christos 				if (*cp == '\\' && cp[1] != '\0')
    204  1.1  christos 					cp++;
    205  1.1  christos 				if (*cp == '(')
    206  1.1  christos 					nesting++;
    207  1.1  christos 				if (*cp == ')')
    208  1.1  christos 					nesting--;
    209  1.1  christos 			}
    210  1.1  christos 			break;
    211  1.1  christos 		default:
    212  1.1  christos 			*cp2++ = *cp;
    213  1.1  christos 			break;
    214  1.1  christos 		}
    215  1.1  christos 	}
    216  1.1  christos 	/* remove trailing white space */
    217  1.1  christos 	while (cp2 > lastq && isblank((unsigned char)cp2[-1]))
    218  1.1  christos 		cp2--;
    219  1.1  christos 	*cp2 = '\0';
    220  1.1  christos 	if (*cp == ';')
    221  1.1  christos 		cp++;
    222  1.1  christos 	cp = skip_white(cp);
    223  1.1  christos 	return cp;
    224  1.1  christos }
    225  1.1  christos 
    226  1.1  christos /*
    227  1.1  christos  * Content parameter
    228  1.1  christos  *    if field is NULL, return the content "specifier".
    229  1.1  christos  */
    230  1.1  christos static char*
    231  1.1  christos cparam(const char field[], char *src, int downcase)
    232  1.1  christos {
    233  1.1  christos 	char *cp;
    234  1.1  christos 	char *dst;
    235  1.1  christos 
    236  1.1  christos 	if (src == NULL)
    237  1.1  christos 		return NULL;
    238  1.1  christos 
    239  1.1  christos 	dst = salloc(strlen(src) + 1); /* large enough for any param in src */
    240  1.1  christos 	cp = skip_white(src);
    241  1.1  christos 	cp = get_param(dst, cp);
    242  1.1  christos 
    243  1.1  christos 	if (field == NULL)
    244  1.1  christos 		return dst;
    245  1.1  christos 
    246  1.1  christos 	while (*cp != '\0') {
    247  1.1  christos 		size_t len = strlen(field);
    248  1.1  christos 		cp = get_param(dst, cp);
    249  1.1  christos 		if (strncasecmp(dst, field, len) == 0 && dst[len] == '=') {
    250  1.1  christos 			char *cp2;
    251  1.1  christos 			cp2 = dst + len + 1;
    252  1.1  christos 			if (downcase)
    253  1.1  christos 				istrcpy(cp2, cp2);
    254  1.1  christos 			return cp2;
    255  1.1  christos 		}
    256  1.1  christos 	}
    257  1.1  christos 	return NULL;
    258  1.1  christos }
    259  1.1  christos 
    260  1.1  christos 
    261  1.1  christos static void
    262  1.1  christos get_content(struct mime_info *mip)
    263  1.1  christos {
    264  1.1  christos 	char *mime_type_field;
    265  1.1  christos 	struct message *mp;
    266  1.1  christos 	char *cp;
    267  1.1  christos 
    268  1.1  christos 	mp = mip->mp;
    269  1.1  christos 	mip->mi_version  = cparam(NULL, hfield(MIME_HDR_VERSION, mp), 0);
    270  1.1  christos 	mip->mi_encoding = cparam(NULL, hfield(MIME_HDR_ENCODING, mp), 1);
    271  1.1  christos 
    272  1.1  christos 	mime_type_field = hfield(MIME_HDR_TYPE, mp);
    273  1.1  christos 	mip->mi_type = cparam(NULL, mime_type_field, 1);
    274  1.1  christos 	if (mip->mi_type) {
    275  1.1  christos 		cp = strchr(mip->mi_type, '/');
    276  1.1  christos 		if (cp)
    277  1.1  christos 			*cp++ = '\0';
    278  1.1  christos 		mip->mi_subtype = cp;
    279  1.1  christos 	}
    280  1.1  christos 	mip->mi_charset = cparam("charset", mime_type_field, 1);
    281  1.1  christos 	mip->mi_boundary = cparam("boundary", mime_type_field, 0);
    282  1.1  christos }
    283  1.1  christos 
    284  1.1  christos 
    285  1.1  christos static struct message *
    286  1.1  christos salloc_message(int flag, long block, short offset)
    287  1.1  christos {
    288  1.1  christos 	struct message *mp;
    289  1.1  christos 	/* use csalloc in case someone adds a field someday! */
    290  1.1  christos 	mp = csalloc(1, sizeof(*mp));
    291  1.1  christos 	mp->m_flag   = flag;
    292  1.1  christos 	mp->m_block  = block;
    293  1.1  christos 	mp->m_offset = offset;
    294  1.1  christos #if 0
    295  1.1  christos 	mp->m_lines  = 0;
    296  1.1  christos 	mp->m_size   = 0;
    297  1.1  christos 	mp->m_blines = 0;
    298  1.1  christos #endif
    299  1.1  christos 	return mp;
    300  1.1  christos }
    301  1.1  christos 
    302  1.1  christos static struct mime_info *
    303  1.1  christos insert_new_mip(struct mime_info *this_mip)
    304  1.1  christos {
    305  1.1  christos 	struct mime_info *new_mip;
    306  1.1  christos 	new_mip = csalloc(1, sizeof(*new_mip));
    307  1.1  christos 	new_mip->mi_blink = this_mip;
    308  1.1  christos 	new_mip->mi_flink = this_mip->mi_flink;
    309  1.1  christos 	this_mip->mi_flink = new_mip;
    310  1.1  christos 	this_mip = new_mip;
    311  1.1  christos 	return new_mip;
    312  1.1  christos }
    313  1.1  christos 
    314  1.1  christos static void
    315  1.1  christos split_multipart(struct mime_info *top_mip)
    316  1.1  christos {
    317  1.1  christos 	FILE *fp;
    318  1.1  christos 	struct message *top_mp;
    319  1.1  christos 	struct message *this_mp;
    320  1.1  christos 	struct mime_info *this_mip;
    321  1.1  christos 	off_t beg_pos;
    322  1.1  christos 	const char *boundary;
    323  1.1  christos 	size_t boundary_len;
    324  1.1  christos 	long lines_left;	/* must be signed and same size as m_lines */
    325  1.1  christos 	int partnum;
    326  1.1  christos 	int in_header;
    327  1.1  christos 
    328  1.1  christos 	top_mp = top_mip->mp;
    329  1.1  christos 	this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
    330  1.1  christos 	this_mip = top_mip;
    331  1.1  christos 	this_mip->mp = this_mp;
    332  1.1  christos 
    333  1.1  christos 	partnum = 1;
    334  1.1  christos /*	top_mip->mi_partnum = partnum++;  */ /* Keep the number set by the caller */
    335  1.1  christos 	in_header = 1;
    336  1.1  christos 	boundary = top_mip->mi_boundary;
    337  1.1  christos 	boundary_len = boundary ? strlen(boundary) : 0;
    338  1.1  christos 
    339  1.1  christos 	fp = setinput(top_mp);
    340  1.1  christos 	beg_pos = ftello(fp);
    341  1.1  christos 
    342  1.1  christos 	for (lines_left = top_mp->m_lines - 1; lines_left >= 0; lines_left--) {
    343  1.1  christos 		char *line;
    344  1.1  christos 		size_t line_len;
    345  1.1  christos 
    346  1.1  christos 		line = fgetln(fp, &line_len);
    347  1.1  christos 
    348  1.1  christos 		this_mp->m_lines++;		/* count the message lines */
    349  1.1  christos 
    350  1.1  christos 		if (!in_header)
    351  1.1  christos 			this_mp->m_blines++;	/* count the body lines */
    352  1.1  christos 
    353  1.1  christos 		if (lines_left == 0 || (
    354  1.1  christos 			    !in_header &&
    355  1.1  christos 			    line_len >= boundary_len + 2 &&
    356  1.1  christos 			    line[0] == '-' && line[1] == '-' &&
    357  1.1  christos 			    strncmp(line + 2, boundary, boundary_len) == 0)) {
    358  1.1  christos 			off_t cur_pos;
    359  1.1  christos 			off_t end_pos;
    360  1.1  christos 
    361  1.1  christos 			cur_pos = ftello(fp);
    362  1.1  christos 
    363  1.1  christos 			/* the boundary belongs to the next part */
    364  1.1  christos 			end_pos = cur_pos - line_len;
    365  1.1  christos 			this_mp->m_lines  -= 1;
    366  1.1  christos 			this_mp->m_blines -= 1;
    367  1.1  christos 
    368  1.1  christos 			this_mp->m_size = end_pos - beg_pos;
    369  1.1  christos 
    370  1.1  christos 			if (line[boundary_len + 2] == '-' &&
    371  1.1  christos 			    line[boundary_len + 3] == '-') {/* end of multipart */
    372  1.1  christos 				/* do a sanity check on the EOM */
    373  1.1  christos 				if (lines_left) {
    374  1.1  christos 					/*
    375  1.1  christos 					 * XXX - this can happen!
    376  1.1  christos 					 * Should we display the
    377  1.1  christos 					 * trailing garbage or check
    378  1.1  christos 					 * that it is blank or just
    379  1.1  christos 					 * ignore it?
    380  1.1  christos 					 */
    381  1.1  christos /*					(void)printf("EOM: lines left: %ld\n", lines_left); */
    382  1.1  christos 				}
    383  1.1  christos 				break;	/* XXX - stop at this point or grab the rest? */
    384  1.1  christos 			}
    385  1.1  christos 
    386  1.1  christos 			this_mip = insert_new_mip(this_mip);
    387  1.1  christos 			this_mp = salloc_message(top_mp->m_flag,
    388  1.1  christos 			    (long)blockof(end_pos), offsetof(end_pos));
    389  1.1  christos 			this_mip->mp = this_mp;
    390  1.1  christos 			this_mip->mi_parent.mip = top_mip;
    391  1.1  christos 			this_mip->mi_parent.mp = top_mp;
    392  1.1  christos 			this_mip->mi_partnum = partnum++;
    393  1.1  christos 
    394  1.1  christos 			beg_pos = end_pos;
    395  1.1  christos 			in_header = 1;
    396  1.1  christos 		}
    397  1.1  christos 
    398  1.1  christos 		if (line_len == 1)
    399  1.1  christos 			in_header = 0;
    400  1.1  christos 	}
    401  1.1  christos }
    402  1.1  christos 
    403  1.1  christos static void
    404  1.1  christos split_message(struct mime_info *top_mip)
    405  1.1  christos {
    406  1.1  christos 	struct mime_info *this_mip;
    407  1.1  christos 	struct message *top_mp;
    408  1.1  christos 	struct message *this_mp;
    409  1.1  christos 	FILE *fp;
    410  1.1  christos 	off_t beg_pos;
    411  1.1  christos 	long lines_left;	/* must be same size as m_lines */
    412  1.1  christos 	int in_header;
    413  1.1  christos 
    414  1.1  christos 	top_mp = top_mip->mp;
    415  1.1  christos 	this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
    416  1.1  christos 	this_mip = top_mip;
    417  1.1  christos 	this_mip->mp = this_mp;
    418  1.1  christos 
    419  1.1  christos 	in_header = 1;
    420  1.1  christos 
    421  1.1  christos 	fp = setinput(top_mp);
    422  1.1  christos 	beg_pos = ftello(fp);
    423  1.1  christos 
    424  1.1  christos 	for (lines_left = top_mp->m_lines; lines_left > 0; lines_left--) {
    425  1.1  christos 		size_t line_len;
    426  1.1  christos 
    427  1.1  christos 		(void)fgetln(fp, &line_len);
    428  1.1  christos 
    429  1.1  christos 		this_mp->m_lines++;		/* count the message lines */
    430  1.1  christos 		if (!in_header)
    431  1.1  christos 			this_mp->m_blines++;	/* count the body lines */
    432  1.1  christos 
    433  1.1  christos 		if (in_header && line_len == 1) { /* end of header */
    434  1.1  christos 			off_t end_pos;
    435  1.1  christos 			end_pos = ftello(fp);
    436  1.1  christos 			this_mp->m_size = end_pos - beg_pos;
    437  1.1  christos 
    438  1.1  christos 			this_mip = insert_new_mip(this_mip);
    439  1.1  christos 			this_mp = salloc_message(top_mp->m_flag,
    440  1.1  christos 			    (long)blockof(end_pos), offsetof(end_pos));
    441  1.1  christos 			this_mip->mp = this_mp;
    442  1.1  christos 			this_mip->mi_parent.mip = top_mip;
    443  1.1  christos 			this_mip->mi_parent.mp = top_mp;
    444  1.1  christos 			this_mip->mi_partnum = 0;  /* no partnum displayed */
    445  1.1  christos 
    446  1.1  christos 			beg_pos = end_pos;
    447  1.1  christos 			in_header = 0;	/* never in header again */
    448  1.1  christos 		}
    449  1.1  christos 	}
    450  1.1  christos 
    451  1.1  christos 	/* close the last message */
    452  1.1  christos 	this_mp->m_size = ftello(fp) - beg_pos;
    453  1.1  christos }
    454  1.1  christos 
    455  1.1  christos 
    456  1.1  christos static const char *
    457  1.1  christos get_command_hook(struct mime_info *mip, const char *domain)
    458  1.1  christos {
    459  1.1  christos 	char *key;
    460  1.1  christos 	char *cmd;
    461  1.1  christos 
    462  1.1  christos 	if (mip->mi_type == NULL)
    463  1.1  christos 		return NULL;
    464  1.1  christos 
    465  1.1  christos 	/* XXX - should we use easprintf() here?  We are probably
    466  1.1  christos 	 * hosed elsewhere if this fails anyway. */
    467  1.1  christos 
    468  1.1  christos 	cmd = NULL;
    469  1.1  christos 	if (mip->mi_subtype) {
    470  1.1  christos 		if (asprintf(&key, "mime%s-%s-%s",
    471  1.1  christos 			domain,	mip->mi_type, mip->mi_subtype) == -1) {
    472  1.1  christos 			warn("get_command_hook: subtupe: asprintf");
    473  1.1  christos 			return NULL;
    474  1.1  christos 		}
    475  1.1  christos 		cmd = value(key);
    476  1.1  christos 		free(key);
    477  1.1  christos 	}
    478  1.1  christos 	if (cmd == NULL) {
    479  1.1  christos 		if (asprintf(&key, "mime%s-%s", domain, mip->mi_type) == -1) {
    480  1.1  christos 			warn("get_command_hook: type: asprintf");
    481  1.1  christos 			return NULL;
    482  1.1  christos 		}
    483  1.1  christos 		cmd = value(key);
    484  1.1  christos 		free(key);
    485  1.1  christos 	}
    486  1.1  christos 	return cmd;
    487  1.1  christos }
    488  1.1  christos 
    489  1.1  christos 
    490  1.1  christos static int
    491  1.1  christos is_basic_alternative(struct mime_info *mip)
    492  1.1  christos {
    493  1.1  christos 	return
    494  1.1  christos 	    strcasecmp(mip->mi_type, "text") == 0 &&
    495  1.1  christos 	    strcasecmp(mip->mi_subtype, "plain") == 0;
    496  1.1  christos }
    497  1.1  christos 
    498  1.1  christos static struct mime_info *
    499  1.1  christos select_alternative(struct mime_info *top_mip, struct mime_info *end_mip)
    500  1.1  christos {
    501  1.1  christos 	struct mime_info *the_mip;	/* the chosen alternate */
    502  1.1  christos 	struct mime_info *this_mip;
    503  1.1  christos 	/*
    504  1.1  christos 	 * The alternates are supposed to occur in order of
    505  1.1  christos 	 * increasing "complexity".  So: if there is at least
    506  1.1  christos 	 * one alternate of type "text/plain", use the last
    507  1.1  christos 	 * one, otherwise default to the first alternate.
    508  1.1  christos 	 */
    509  1.1  christos 	the_mip = top_mip->mi_flink;
    510  1.1  christos 	for (this_mip = top_mip->mi_flink;
    511  1.1  christos 	     this_mip != end_mip;
    512  1.1  christos 	     this_mip = this_mip->mi_flink) {
    513  1.1  christos 		const char *cmd;
    514  1.1  christos 
    515  1.1  christos 		if (this_mip->mi_type == NULL ||
    516  1.1  christos 		    this_mip->mi_subtype == NULL)
    517  1.1  christos 			continue;
    518  1.1  christos 
    519  1.1  christos 		if (is_basic_alternative(this_mip))
    520  1.1  christos 			the_mip = this_mip;
    521  1.1  christos 		else if (
    522  1.1  christos 			(cmd = get_command_hook(this_mip, "-hook")) ||
    523  1.1  christos 			(cmd = get_command_hook(this_mip, "-head")) ||
    524  1.1  christos 			(cmd = get_command_hook(this_mip, "-body"))) {
    525  1.1  christos 			int flags;
    526  1.1  christos 			/* just get the flags. */
    527  1.1  christos 			flags = mime_run_command(cmd, NULL);
    528  1.1  christos 			if ((flags & CMD_FLAG_ALTERNATIVE) != 0)
    529  1.1  christos 				the_mip = this_mip;
    530  1.1  christos 		}
    531  1.1  christos 	}
    532  1.1  christos 	return the_mip;
    533  1.1  christos }
    534  1.1  christos 
    535  1.1  christos 
    536  1.1  christos static inline int
    537  1.1  christos is_multipart(struct mime_info *mip)
    538  1.1  christos {
    539  1.1  christos 	return mip->mi_type &&
    540  1.1  christos 	    strcasecmp("multipart", mip->mi_type) == 0;
    541  1.1  christos }
    542  1.1  christos static inline int
    543  1.1  christos is_message(struct mime_info *mip)
    544  1.1  christos {
    545  1.1  christos 	return mip->mi_type &&
    546  1.1  christos 	    strcasecmp("message", mip->mi_type) == 0;
    547  1.1  christos }
    548  1.1  christos 
    549  1.1  christos static inline int
    550  1.1  christos is_alternative(struct mime_info *mip)
    551  1.1  christos {
    552  1.1  christos 	return mip->mi_subtype &&
    553  1.1  christos 	    strcasecmp("alternative", mip->mi_subtype) == 0;
    554  1.1  christos }
    555  1.1  christos 
    556  1.1  christos 
    557  1.1  christos /*
    558  1.1  christos  * Take a mime_info pointer and expand it recursively into all its
    559  1.1  christos  * mime parts.  Only "multipart" and "message" types recursed into;
    560  1.1  christos  * they are handled separately.
    561  1.1  christos  */
    562  1.1  christos static struct mime_info *
    563  1.1  christos expand_mip(struct mime_info *top_mip)
    564  1.1  christos {
    565  1.1  christos 	struct mime_info *this_mip;
    566  1.1  christos 	struct mime_info *next_mip;
    567  1.1  christos 
    568  1.1  christos 	next_mip = top_mip->mi_flink;
    569  1.1  christos 
    570  1.1  christos 	if (is_multipart(top_mip)) {
    571  1.1  christos 		split_multipart(top_mip);
    572  1.1  christos 
    573  1.1  christos 		for (this_mip = top_mip->mi_flink;
    574  1.1  christos 		     this_mip != next_mip;
    575  1.1  christos 		     this_mip = this_mip->mi_flink) {
    576  1.1  christos 			get_content(this_mip);
    577  1.1  christos 		}
    578  1.1  christos 		if (is_alternative(top_mip)) {
    579  1.1  christos 			this_mip = select_alternative(top_mip, next_mip);
    580  1.1  christos 			this_mip->mi_partnum = 0; /* suppress partnum display */
    581  1.1  christos 			this_mip->mi_flink = next_mip;
    582  1.1  christos 			this_mip->mi_blink = top_mip;
    583  1.1  christos 			top_mip->mi_flink  = this_mip;
    584  1.1  christos 		}
    585  1.1  christos 		/*
    586  1.1  christos 		 * Recurse into each part.
    587  1.1  christos 		 */
    588  1.1  christos 		for (this_mip = top_mip->mi_flink;
    589  1.1  christos 		     this_mip != next_mip;
    590  1.1  christos 		     this_mip = expand_mip(this_mip))
    591  1.1  christos 			continue;
    592  1.1  christos 	}
    593  1.1  christos 	else if (is_message(top_mip)) {
    594  1.1  christos 		split_message(top_mip);
    595  1.1  christos 
    596  1.1  christos 		this_mip = top_mip->mi_flink;
    597  1.1  christos 		if (this_mip) {
    598  1.1  christos 			get_content(this_mip);
    599  1.1  christos 			/*
    600  1.1  christos 			 * If the one part is MIME encoded, recurse into it.
    601  1.1  christos 			 * XXX - Should this be conditional on subtype "rcs822"?
    602  1.1  christos 			 */
    603  1.1  christos 			if (this_mip->mi_type &&
    604  1.1  christos 			    this_mip->mi_version &&
    605  1.1  christos 			    equal(this_mip->mi_version, MIME_VERSION)) {
    606  1.1  christos 				this_mip->mi_partnum = 0;
    607  1.1  christos 				(void)expand_mip(this_mip);
    608  1.1  christos 			}
    609  1.1  christos 		}
    610  1.1  christos 	}
    611  1.1  christos 
    612  1.1  christos 	return next_mip;
    613  1.1  christos }
    614  1.1  christos 
    615  1.1  christos 
    616  1.1  christos static int
    617  1.1  christos show_partnum(FILE *fp, struct mime_info *mip)
    618  1.1  christos {
    619  1.1  christos 	int need_dot;
    620  1.1  christos 	need_dot = 0;
    621  1.1  christos 	if (mip->mi_parent.mip && mip->mi_parent.mip->mi_parent.mip)
    622  1.1  christos 		need_dot = show_partnum(fp, mip->mi_parent.mip);
    623  1.1  christos 
    624  1.1  christos 	if (mip->mi_partnum) {
    625  1.1  christos 		(void)fprintf(fp, "%s%d", need_dot ? "." : "",  mip->mi_partnum);
    626  1.1  christos 		need_dot = 1;
    627  1.1  christos 	}
    628  1.1  christos 	return need_dot;
    629  1.1  christos }
    630  1.1  christos 
    631  1.1  christos 
    632  1.1  christos PUBLIC struct mime_info *
    633  1.1  christos mime_decode_open(struct message *mp)
    634  1.1  christos {
    635  1.1  christos 	struct mime_info *mip;
    636  1.1  christos 
    637  1.1  christos 	mip = csalloc(1, sizeof(*mip));
    638  1.1  christos 	mip->mp = salloc(sizeof(*mip->mp));
    639  1.1  christos 	*mip->mp = *mp;		/* copy this so we can change its m_lines */
    640  1.1  christos 
    641  1.1  christos 	get_content(mip);
    642  1.1  christos 
    643  1.1  christos 	/* RFC 2049 - sec 2 item 1 */
    644  1.1  christos 	if (mip->mi_version == NULL ||
    645  1.1  christos 	    !equal(mip->mi_version, MIME_VERSION))
    646  1.1  christos 		return NULL;
    647  1.1  christos 
    648  1.1  christos 	if (mip->mi_type)
    649  1.1  christos 		(void)expand_mip(mip);
    650  1.1  christos 
    651  1.1  christos /*	show_mime_info(stderr, mip, NULL); */
    652  1.1  christos 
    653  1.1  christos 	return mip;
    654  1.1  christos }
    655  1.1  christos 
    656  1.1  christos 
    657  1.1  christos PUBLIC void
    658  1.1  christos mime_decode_close(struct mime_info *mip)
    659  1.1  christos {
    660  1.1  christos 	if (mip)
    661  1.1  christos 		close_top_files(mip->mi_pipe_end);
    662  1.1  christos }
    663  1.1  christos 
    664  1.1  christos 
    665  1.1  christos struct prefix_line_args_s {
    666  1.1  christos 	const char *prefix;
    667  1.1  christos 	size_t prefixlen;
    668  1.1  christos };
    669  1.1  christos 
    670  1.1  christos static void
    671  1.1  christos prefix_line(FILE *fi, FILE *fo, void *cookie)
    672  1.1  christos {
    673  1.1  christos 	struct prefix_line_args_s *args;
    674  1.1  christos 	const char *line;
    675  1.1  christos 	const char *prefix;
    676  1.1  christos 	size_t prefixlen;
    677  1.1  christos 	size_t length;
    678  1.1  christos 
    679  1.1  christos 	args = cookie;
    680  1.1  christos 	prefix    = args->prefix;
    681  1.1  christos 	prefixlen = args->prefixlen;
    682  1.1  christos 
    683  1.1  christos 	while ((line = fgetln(fi, &length)) != NULL) {
    684  1.1  christos 		if (length > 1)
    685  1.1  christos 			(void)fputs(prefix, fo);
    686  1.1  christos 		else
    687  1.1  christos 			(void)fwrite(prefix, sizeof *prefix,
    688  1.1  christos 			    prefixlen, fo);
    689  1.1  christos 		(void)fwrite(line, sizeof(*line), length, fo);
    690  1.1  christos 	}
    691  1.1  christos 	(void)fflush(fo);
    692  1.1  christos }
    693  1.1  christos 
    694  1.1  christos PUBLIC int
    695  1.1  christos mime_sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
    696  1.1  christos     const char *prefix, struct mime_info *mip)
    697  1.1  christos {
    698  1.1  christos 	int error;
    699  1.1  christos 	FILE *end_of_pipe;
    700  1.1  christos 	FILE *end_of_prefix;
    701  1.1  christos 
    702  1.1  christos 	if (mip == NULL)
    703  1.1  christos 		return sendmessage(mp, obuf, doign ? ignore : 0, prefix, NULL);
    704  1.1  christos 
    705  1.1  christos 	(void)fflush(obuf);  /* Be safe and flush!  XXX - necessary? */
    706  1.1  christos 
    707  1.1  christos 	/*
    708  1.1  christos 	 * Set these early so pipe_end() and mime_decode_close() work!
    709  1.1  christos 	 */
    710  1.1  christos 	mip->mi_fo = obuf;
    711  1.1  christos 	mip->mi_pipe_end = last_registered_file(0);
    712  1.1  christos 
    713  1.1  christos 	/*
    714  1.1  christos 	 * Handle the prefix as a pipe stage so it doesn't get seen by
    715  1.1  christos 	 * any decoding or hooks.
    716  1.1  christos 	 */
    717  1.1  christos 	if (prefix != NULL) {
    718  1.1  christos 		static struct prefix_line_args_s prefix_line_args;
    719  1.1  christos 		const char *dp, *dp2 = NULL;
    720  1.1  christos 		for (dp = prefix; *dp; dp++)
    721  1.1  christos 			if (*dp != ' ' && *dp != '\t')
    722  1.1  christos 				dp2 = dp;
    723  1.1  christos 		prefix_line_args.prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
    724  1.1  christos 		prefix_line_args.prefix = prefix;
    725  1.1  christos 		mime_run_function(prefix_line, pipe_end(mip), (void*)&prefix_line_args);
    726  1.1  christos 	}
    727  1.1  christos 
    728  1.1  christos 	end_of_pipe   = mip->mi_pipe_end;
    729  1.1  christos 	end_of_prefix = last_registered_file(0);
    730  1.1  christos 	error = 0;
    731  1.1  christos 	for (/* EMPTY */; mip; mip = mip->mi_flink) {
    732  1.1  christos 		mip->mi_fo = obuf;
    733  1.1  christos 		mip->mi_pipe_end = end_of_pipe;
    734  1.1  christos 		error |= sendmessage(mip->mp, pipe_end(mip), doign ? ignore : 0, NULL, mip);
    735  1.1  christos 		close_top_files(end_of_prefix);	/* don't close the prefixer! */
    736  1.1  christos 	}
    737  1.1  christos 	return error;
    738  1.1  christos }
    739  1.1  christos 
    740  1.1  christos 
    741  1.1  christos #ifdef CHARSET_SUPPORT
    742  1.1  christos /**********************************************
    743  1.1  christos  * higher level interface to run mime_ficonv().
    744  1.1  christos  *
    745  1.1  christos  */
    746  1.1  christos static void
    747  1.1  christos run_mime_ficonv(struct mime_info *mip, const char *charset)
    748  1.1  christos {
    749  1.1  christos 	FILE *fo;
    750  1.1  christos 	iconv_t cd;
    751  1.1  christos 
    752  1.1  christos 	fo = pipe_end(mip);
    753  1.1  christos 
    754  1.1  christos 	if (charset == NULL ||
    755  1.1  christos 	    mip->mi_charset == NULL ||
    756  1.1  christos 	    strcasecmp(mip->mi_charset, charset) == 0 ||
    757  1.1  christos 	    strcasecmp(mip->mi_charset, "unknown") == 0)
    758  1.1  christos 		return;
    759  1.1  christos 
    760  1.1  christos 	cd = iconv_open(charset, mip->mi_charset);
    761  1.1  christos 	if (cd == (iconv_t)-1) {
    762  1.1  christos 		(void)fprintf(fo, "\t [ iconv_open failed: %s ]\n\n",
    763  1.1  christos 		    strerror(errno));
    764  1.1  christos 		(void)fflush(fo);	/* flush here or see double! */
    765  1.1  christos 		return;
    766  1.1  christos 	}
    767  1.1  christos 
    768  1.1  christos 	if (value(ENAME_MIME_CHARSET_VERBOSE))
    769  1.1  christos 		(void)fprintf(fo, "\t[ converting %s -> %s ]\n\n", mip->mi_charset, charset);
    770  1.1  christos 
    771  1.1  christos 	mime_run_function(mime_ficonv, fo, cd);
    772  1.1  christos 
    773  1.1  christos 	iconv_close(cd);
    774  1.1  christos }
    775  1.1  christos #endif /* CHARSET_SUPPORT */
    776  1.1  christos 
    777  1.1  christos 
    778  1.1  christos static void
    779  1.1  christos run_decoder(struct mime_info *mip, void(*fn)(FILE*, FILE*, void *))
    780  1.1  christos {
    781  1.1  christos #ifdef CHARSET_SUPPORT
    782  1.1  christos 	char *charset;
    783  1.1  christos 
    784  1.1  christos 	charset = value(ENAME_MIME_CHARSET);
    785  1.1  christos 	if (charset && mip->mi_type && strcasecmp(mip->mi_type, "text") == 0)
    786  1.1  christos 		run_mime_ficonv(mip, charset);
    787  1.1  christos #endif /* CHARSET_SUPPORT */
    788  1.1  christos 
    789  1.1  christos 	if (fn == mime_fio_copy)/* XXX - avoid an extra unnecessary pipe stage */
    790  1.1  christos 		return;
    791  1.1  christos 
    792  1.1  christos 	mime_run_function(fn, pipe_end(mip), (void*)1);
    793  1.1  christos }
    794  1.1  christos 
    795  1.1  christos 
    796  1.1  christos /*
    797  1.1  christos  * Determine how to handle the display based on the type and subtype
    798  1.1  christos  * fields.
    799  1.1  christos  */
    800  1.1  christos enum dispmode_e {
    801  1.1  christos 	DM_IGNORE	= 0x00,	/* silently ignore part - must be zero! */
    802  1.1  christos 	DM_DISPLAY,		/* decode and display the part */
    803  1.1  christos 	DM_UNKNOWN,		/* unknown display */
    804  1.1  christos 	DM_BINARY,		/* indicate binary data */
    805  1.1  christos 	DM_PGPSIGN,		/* OpenPGP signed part */
    806  1.1  christos 	DM_PGPENCR,		/* OpenPGP encrypted part */
    807  1.1  christos 	DM_PGPKEYS		/* OpenPGP keys part */
    808  1.1  christos };
    809  1.1  christos #define APPLICATION_OCTET_STREAM	DM_BINARY
    810  1.1  christos 
    811  1.1  christos static enum dispmode_e
    812  1.1  christos get_display_mode(struct mime_info *mip, mime_codec_t dec)
    813  1.1  christos {
    814  1.1  christos 	struct mime_subtype_s {
    815  1.1  christos 		const char *st_name;
    816  1.1  christos 		enum dispmode_e st_dispmode;
    817  1.1  christos 	};
    818  1.1  christos 	struct mime_type_s {
    819  1.1  christos 		const char *mt_type;
    820  1.1  christos 		const struct mime_subtype_s *mt_subtype;
    821  1.1  christos 		enum dispmode_e mt_dispmode;	/* default if NULL subtype */
    822  1.1  christos 	};
    823  1.1  christos 	static const struct mime_subtype_s text_subtype_tbl[] = {
    824  1.1  christos 		{ "plain",		DM_DISPLAY },
    825  1.1  christos 		{ "html", 		DM_DISPLAY },	/* rfc2854 */
    826  1.1  christos 		{ "rfc822-headers",	DM_DISPLAY },
    827  1.1  christos 		{ "css",		DM_DISPLAY },	/* rfc2318 */
    828  1.1  christos 		{ "enriched",		DM_DISPLAY },	/* rfc1523/rfc1563/rfc1896 */
    829  1.1  christos 		{ "graphics",		DM_DISPLAY },	/* rfc0553 */
    830  1.1  christos 		{ "nroff",		DM_DISPLAY },	/* rfc4263 */
    831  1.1  christos 		{ "red",		DM_DISPLAY },	/* rfc4102 */
    832  1.1  christos 		{ NULL,			DM_DISPLAY }	/* default */
    833  1.1  christos 	};
    834  1.1  christos 	static const struct mime_subtype_s image_subtype_tbl[] = {
    835  1.1  christos 		{ "tiff",		DM_BINARY },	/* rfc2302/rfc3302 */
    836  1.1  christos 		{ "tiff-fx",		DM_BINARY },	/* rfc3250/rfc3950 */
    837  1.1  christos 		{ "t38",		DM_BINARY },	/* rfc3362 */
    838  1.1  christos 		{ NULL,			DM_BINARY }	/* default */
    839  1.1  christos 	};
    840  1.1  christos 	static const struct mime_subtype_s audio_subtype_tbl[] = {
    841  1.1  christos 		{ "mpeg",		DM_BINARY },	/* rfc3003 */
    842  1.1  christos 		{ "t38",		DM_BINARY },	/* rfc4612 */
    843  1.1  christos 		{ NULL,			DM_BINARY }	/* default */
    844  1.1  christos 	};
    845  1.1  christos 	static const struct mime_subtype_s video_subtype_tbl[] = {
    846  1.1  christos 		{ NULL,			DM_BINARY }	/* default */
    847  1.1  christos 	};
    848  1.1  christos 	static const struct mime_subtype_s application_subtype_tbl[] = {
    849  1.1  christos 		{ "octet-stream",	APPLICATION_OCTET_STREAM },
    850  1.1  christos                 { "pgp-encrypted",      DM_PGPENCR },   /* rfc3156 */
    851  1.1  christos 		{ "pgp-keys",           DM_PGPKEYS },   /* rfc3156 */
    852  1.1  christos 		{ "pgp-signature",      DM_PGPSIGN },   /* rfc3156 */
    853  1.1  christos 		{ "pdf",		DM_BINARY },	/* rfc3778 */
    854  1.1  christos 		{ "whoispp-query",	DM_UNKNOWN },	/* rfc2957 */
    855  1.1  christos 		{ "whoispp-response",	DM_UNKNOWN },	/* rfc2958 */
    856  1.1  christos 		{ "font-tdpfr",		DM_UNKNOWN },	/* rfc3073 */
    857  1.1  christos 		{ "xhtml+xml",		DM_UNKNOWN },	/* rfc3236 */
    858  1.1  christos 		{ "ogg",		DM_UNKNOWN },	/* rfc3534 */
    859  1.1  christos 		{ "rdf+xml",		DM_UNKNOWN },	/* rfc3870 */
    860  1.1  christos 		{ "soap+xml",		DM_UNKNOWN },	/* rfc3902 */
    861  1.1  christos 		{ "mbox",		DM_UNKNOWN },	/* rfc4155 */
    862  1.1  christos 		{ "xv+xml",		DM_UNKNOWN },	/* rfc4374 */
    863  1.1  christos 		{ "smil",		DM_UNKNOWN },	/* rfc4536 */
    864  1.1  christos 		{ "smil+xml",		DM_UNKNOWN },	/* rfc4536 */
    865  1.1  christos 		{ "json",		DM_UNKNOWN },	/* rfc4627 */
    866  1.1  christos 		{ "voicexml+xml",	DM_UNKNOWN },	/* rfc4267 */
    867  1.1  christos 		{ "ssml+xml",		DM_UNKNOWN },	/* rfc4267 */
    868  1.1  christos 		{ "srgs",		DM_UNKNOWN },	/* rfc4267 */
    869  1.1  christos 		{ "srgs+xml",		DM_UNKNOWN },	/* rfc4267 */
    870  1.1  christos 		{ "ccxml+xml",		DM_UNKNOWN },	/* rfc4267 */
    871  1.1  christos 		{ "pls+xml.",		DM_UNKNOWN },	/* rfc4267 */
    872  1.1  christos 		{ NULL,			APPLICATION_OCTET_STREAM } /* default */
    873  1.1  christos 	};
    874  1.1  christos 	static const struct mime_type_s mime_type_tbl[] = {
    875  1.1  christos 		{ "text",	 text_subtype_tbl,		DM_DISPLAY },
    876  1.1  christos 		{ "image",	 image_subtype_tbl,		DM_IGNORE },
    877  1.1  christos 		{ "audio",	 audio_subtype_tbl,		DM_IGNORE },
    878  1.1  christos 		{ "video",	 video_subtype_tbl,		DM_IGNORE },
    879  1.1  christos 		{ "application", application_subtype_tbl,	APPLICATION_OCTET_STREAM },
    880  1.1  christos 		{ "NULL",	 NULL,				DM_UNKNOWN }, /* default */
    881  1.1  christos 	};
    882  1.1  christos 	const struct mime_type_s *mtp;
    883  1.1  christos 	const struct mime_subtype_s *stp;
    884  1.1  christos 	const char *mi_type;
    885  1.1  christos 	const char *mi_subtype;
    886  1.1  christos 
    887  1.1  christos 	/*
    888  1.1  christos 	 * Silently ignore all multipart bodies.
    889  1.1  christos 	 * 1) In the case of "multipart" types, this typically
    890  1.1  christos 	 *    contains a message for non-mime enabled mail readers.
    891  1.1  christos 	 * 2) In the case of "message" type, there should be no body.
    892  1.1  christos 	 */
    893  1.1  christos 	if (is_multipart(mip) || is_message(mip))
    894  1.1  christos 		return DM_IGNORE;
    895  1.1  christos 
    896  1.1  christos 	/*
    897  1.1  christos 	 * If the encoding type given but not recognized, treat block
    898  1.1  christos 	 * as "application/octet-stream".  rfc 2049 sec 2 part 2.
    899  1.1  christos 	 */
    900  1.1  christos 	if (mip->mi_encoding && dec == NULL)
    901  1.1  christos 		return APPLICATION_OCTET_STREAM;
    902  1.1  christos 
    903  1.1  christos 	mi_type    = mip->mi_type;
    904  1.1  christos 	mi_subtype = mip->mi_type ? mip->mi_subtype : NULL;
    905  1.1  christos 
    906  1.1  christos 	/*
    907  1.1  christos 	 * If there was no type specified, display anyway so we don't
    908  1.1  christos 	 * miss anything.  (The encoding type is known.)
    909  1.1  christos 	 */
    910  1.1  christos 	if (mi_type == NULL)
    911  1.1  christos 		return DM_DISPLAY;	/* XXX - default to something safe! */
    912  1.1  christos 
    913  1.1  christos 	for (mtp = mime_type_tbl; mtp->mt_type; mtp++) {
    914  1.1  christos 		if (strcasecmp(mtp->mt_type, mi_type) == 0) {
    915  1.1  christos 			if (mi_subtype == NULL)
    916  1.1  christos 				return mtp->mt_dispmode;
    917  1.1  christos 			for (stp = mtp->mt_subtype; stp->st_name; stp++) {
    918  1.1  christos 				if (strcasecmp(stp->st_name, mi_subtype) == 0)
    919  1.1  christos 					return stp->st_dispmode;
    920  1.1  christos 			}
    921  1.1  christos 			return stp->st_dispmode;
    922  1.1  christos 		}
    923  1.1  christos 	}
    924  1.1  christos 	return mtp->mt_dispmode;
    925  1.1  christos }
    926  1.1  christos 
    927  1.1  christos 
    928  1.1  christos PUBLIC FILE *
    929  1.1  christos mime_decode_body(struct mime_info *mip)
    930  1.1  christos {
    931  1.1  christos 	static enum dispmode_e dispmode;
    932  1.1  christos 	mime_codec_t dec;
    933  1.1  christos 	const char *cmd;
    934  1.1  christos 
    935  1.1  christos 	/* close anything left over from mime_decode_head() */
    936  1.1  christos 	close_top_files(mip->mi_head_end);
    937  1.1  christos 
    938  1.1  christos 	/*
    939  1.1  christos 	 * Make sure we flush everything down the pipe so children
    940  1.1  christos 	 * don't see it.
    941  1.1  christos 	 */
    942  1.1  christos 	(void)fflush(pipe_end(mip));
    943  1.1  christos 
    944  1.1  christos 	cmd = NULL;
    945  1.1  christos 	if (mip->mi_command_hook == NULL)
    946  1.1  christos 		cmd = get_command_hook(mip, "-body");
    947  1.1  christos 
    948  1.1  christos 	dec = mime_fio_decoder(mip->mi_encoding);
    949  1.1  christos 	/*
    950  1.1  christos 	 * If there is a filter running, we need to send the message
    951  1.1  christos 	 * to it.  Otherwise, get the default display mode for this body.
    952  1.1  christos 	 */
    953  1.1  christos 	dispmode = cmd || mip->mi_command_hook ? DM_DISPLAY : get_display_mode(mip, dec);
    954  1.1  christos 
    955  1.1  christos 	if (dec == NULL)	/* make sure we have a usable decoder */
    956  1.1  christos 		dec = mime_fio_decoder(MIME_TRANSFER_7BIT);
    957  1.1  christos 
    958  1.1  christos 	if (dispmode == DM_DISPLAY) {
    959  1.1  christos 		int flags;
    960  1.1  christos 		if (cmd == NULL)
    961  1.1  christos 			/* just get the flags */
    962  1.1  christos 			flags = mime_run_command(mip->mi_command_hook, NULL);
    963  1.1  christos 		else
    964  1.1  christos 			flags = mime_run_command(cmd, pipe_end(mip));
    965  1.1  christos 		if ((flags & CMD_FLAG_NO_DECODE) == 0)
    966  1.1  christos 			run_decoder(mip, dec);
    967  1.1  christos 		return pipe_end(mip);
    968  1.1  christos 	}
    969  1.1  christos 	else {
    970  1.1  christos 		static const struct msg_tbl_s {
    971  1.1  christos 			enum dispmode_e dm;
    972  1.1  christos 			const char *msg;
    973  1.1  christos 		} msg_tbl[] = {
    974  1.1  christos 			{ DM_BINARY,	"binary content"	},
    975  1.1  christos 			{ DM_PGPSIGN,	"OpenPGP signature"	},
    976  1.1  christos 			{ DM_PGPENCR,	"OpenPGP encrypted"	},
    977  1.1  christos 			{ DM_PGPKEYS,	"OpenPGP keys"		},
    978  1.1  christos 			{ DM_UNKNOWN,	"unknown data"		},
    979  1.1  christos 			{ DM_IGNORE,	NULL			},
    980  1.1  christos 			{ -1,		NULL			},
    981  1.1  christos 		};
    982  1.1  christos 		const struct msg_tbl_s *mp;
    983  1.1  christos 
    984  1.1  christos 		for (mp = msg_tbl; mp->dm != -1; mp++)
    985  1.1  christos 			if (mp->dm == dispmode)
    986  1.1  christos 				break;
    987  1.1  christos 
    988  1.1  christos 		assert(mp->dm != -1);	/* msg_tbl is short if this happens! */
    989  1.1  christos 
    990  1.1  christos 		if (mp->msg)
    991  1.1  christos 			(void)fprintf(pipe_end(mip), "  [%s]\n\n", mp->msg);
    992  1.1  christos 
    993  1.1  christos 		return NULL;
    994  1.1  christos 	}
    995  1.1  christos }
    996  1.1  christos 
    997  1.1  christos 
    998  1.1  christos 
    999  1.1  christos 
   1000  1.1  christos /************************************************************************
   1001  1.1  christos  * Higher level header decoding interface.
   1002  1.1  christos  *
   1003  1.1  christos  * The core routines are in mime_header.c.
   1004  1.1  christos  */
   1005  1.1  christos 
   1006  1.1  christos PUBLIC char *
   1007  1.1  christos mime_decode_hfield(char *linebuf, size_t bufsize, char *hdrstr)
   1008  1.1  christos {
   1009  1.1  christos 	hfield_decoder_t decode;
   1010  1.1  christos 	decode = mime_hfield_decoder(hdrstr);
   1011  1.1  christos 	if (decode) {
   1012  1.1  christos 		decode(linebuf, bufsize, hdrstr);
   1013  1.1  christos 		return linebuf;
   1014  1.1  christos 	}
   1015  1.1  christos 	return hdrstr;
   1016  1.1  christos }
   1017  1.1  christos 
   1018  1.1  christos /*
   1019  1.1  christos  * Return the next header field found in the given message.
   1020  1.1  christos  * Return >= 0 if something found, < 0 elsewise.
   1021  1.1  christos  * "colon" is set to point to the colon in the header.
   1022  1.1  christos  */
   1023  1.1  christos static int
   1024  1.1  christos get_folded_hfield(FILE *f, char *linebuf, size_t bufsize, int rem, char **colon)
   1025  1.1  christos {
   1026  1.1  christos 	char *cp, *cp2;
   1027  1.1  christos 	char *line;
   1028  1.1  christos 	size_t len;
   1029  1.1  christos 
   1030  1.1  christos 	for (;;) {
   1031  1.1  christos 		if (--rem <= 0)
   1032  1.1  christos 			return -1;
   1033  1.1  christos 		if ((cp = fgetln(f, &len)) == NULL)
   1034  1.1  christos 			return -1;
   1035  1.1  christos 		for (cp2 = cp; isprint((unsigned char)*cp2) &&
   1036  1.1  christos 			 !isblank((unsigned char)*cp2) && *cp2 != ':'; cp2++)
   1037  1.1  christos 			continue;
   1038  1.1  christos 		len = MIN(bufsize - 1, len);
   1039  1.1  christos 		bufsize -= len;
   1040  1.1  christos 		(void)memcpy(linebuf, cp, len);
   1041  1.1  christos 		*colon = *cp2 == ':' ? linebuf + (cp2 - cp) : NULL;
   1042  1.1  christos 		line = linebuf + len;
   1043  1.1  christos 		for (/*EMPTY*/; rem > 0; rem--) {
   1044  1.1  christos 			int c;
   1045  1.1  christos 			(void)ungetc(c = getc(f), f);
   1046  1.1  christos 			if (c == EOF || !isblank((unsigned char)c))
   1047  1.1  christos 				break;
   1048  1.1  christos 
   1049  1.1  christos 			if ((cp = fgetln(f, &len)) == NULL)
   1050  1.1  christos 				break;
   1051  1.1  christos 			len = MIN(bufsize - 1, len);
   1052  1.1  christos 			bufsize -= len;
   1053  1.1  christos 			if (len == 0)
   1054  1.1  christos 			    break;
   1055  1.1  christos 			(void)memcpy(line, cp, len);
   1056  1.1  christos 			line += len;
   1057  1.1  christos 		}
   1058  1.1  christos 		*line = 0;
   1059  1.1  christos 		return rem;
   1060  1.1  christos 		/* NOTREACHED */
   1061  1.1  christos 	}
   1062  1.1  christos }
   1063  1.1  christos 
   1064  1.1  christos static void
   1065  1.1  christos decode_header(FILE *fi, FILE *fo, void *cookie __unused)
   1066  1.1  christos {
   1067  1.1  christos 	char linebuf[LINESIZE];
   1068  1.1  christos 	char *colon;
   1069  1.1  christos #ifdef __lint__
   1070  1.1  christos 	cookie = cookie;
   1071  1.1  christos #endif
   1072  1.1  christos 	while(get_folded_hfield(fi, linebuf, sizeof(linebuf), INT_MAX, &colon) >= 0) {
   1073  1.1  christos 		char decbuf[LINESIZE];
   1074  1.1  christos 		char *hdrstr;
   1075  1.1  christos 		hdrstr = linebuf;
   1076  1.1  christos 		if (colon)
   1077  1.1  christos 			hdrstr = mime_decode_hfield(decbuf, sizeof(decbuf), hdrstr);
   1078  1.1  christos 		(void)fprintf(fo, hdrstr);
   1079  1.1  christos 	}
   1080  1.1  christos }
   1081  1.1  christos 
   1082  1.1  christos 
   1083  1.1  christos PUBLIC FILE *
   1084  1.1  christos mime_decode_header(struct mime_info *mip)
   1085  1.1  christos {
   1086  1.1  christos 	int flags;
   1087  1.1  christos 	const char *cmd;
   1088  1.1  christos 	FILE *fo;
   1089  1.1  christos 
   1090  1.1  christos 	fo = pipe_end(mip);
   1091  1.1  christos 	/*
   1092  1.1  christos 	 * Make sure we flush everything down the pipe so children
   1093  1.1  christos 	 * don't see it.
   1094  1.1  christos 	 */
   1095  1.1  christos 
   1096  1.1  christos 	if (mip->mi_partnum) {
   1097  1.1  christos 		(void)fprintf(fo, "----- Part ");
   1098  1.1  christos 		(void)show_partnum(fo, mip);
   1099  1.1  christos 		(void)fprintf(fo, " -----\n");
   1100  1.1  christos 	}
   1101  1.1  christos 	(void)fflush(fo);
   1102  1.1  christos 
   1103  1.1  christos 	/*
   1104  1.1  christos 	 * install the message hook before the head hook.
   1105  1.1  christos 	 */
   1106  1.1  christos 	cmd = get_command_hook(mip, "-hook");
   1107  1.1  christos 	mip->mi_command_hook = cmd;
   1108  1.1  christos 	if (cmd) {
   1109  1.1  christos 		flags = mime_run_command(cmd, pipe_end(mip));
   1110  1.1  christos 		mip->mi_head_end = last_registered_file(0);
   1111  1.1  christos 	}
   1112  1.1  christos 	else {
   1113  1.1  christos 		cmd = get_command_hook(mip, "-head");
   1114  1.1  christos 		mip->mi_head_end = last_registered_file(0);
   1115  1.1  christos 		flags = mime_run_command(cmd, pipe_end(mip));
   1116  1.1  christos 	}
   1117  1.1  christos 
   1118  1.1  christos 	if (value(ENAME_MIME_DECODE_HDR) && (flags & CMD_FLAG_NO_DECODE) == 0)
   1119  1.1  christos 		mime_run_function(decode_header, pipe_end(mip), NULL);
   1120  1.1  christos 
   1121  1.1  christos 	return pipe_end(mip);
   1122  1.1  christos }
   1123  1.1  christos 
   1124  1.1  christos #endif /* MIME_SUPPORT */
   1125