static char rcsid[] = "@(#)$Id: mimewalk.c,v 1.7 2006/04/09 07:37:30 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.7 $   $State: Exp $
 *
 *  Author: Kari Hurtta <hurtta+elm@posti.FMI.FI> (was hurtta+elm@ozone.FMI.FI)
 *****************************************************************************/

#include "def_melib.h"
#include "s_me.h"

DEBUG_VAR(Debug,__FILE__,"mime");

struct walk_handler * malloc_walk_handler(t)
     struct handler_type *t;
{

    struct walk_handler *ret;

    if (t->magic != WALKTYPE_magic) 
	panic("MALLOC PANIC",__FILE__,__LINE__,"malloc_walk_handler",
	      "Bad type magic number",0);
    
    ret = safe_malloc(sizeof(*ret));

    /* bzero is defined hdrs/defs.h */
    bzero((void *)ret,sizeof (*ret));

    ret->magic        = WALKHANDLER_magic;
    ret->handler_type = t;
    ret->u.dummy  = NULL;

    ret->handler_type->mw_init(ret);

    return ret;
}

void free_walk_handler(ptr)
     struct walk_handler **ptr;
{
    if ((*ptr)->magic != WALKHANDLER_magic) 
	panic("MIME PANIC",__FILE__,__LINE__,"free_walk_handler",
	      "Bad magic number",0);
	
    if ((*ptr)->handler_type->magic != WALKTYPE_magic) 
	panic("MIME PANIC",__FILE__,__LINE__,"free_walk_handler",
	      "Bad type magic number",0);

    
    (*ptr)->handler_type->mw_free(*ptr);

    if ((*ptr)->u.dummy) 
	panic("MALLOC PANIC",__FILE__,__LINE__,"free_walk_handler",
	      "Data not free'ed",0);

    free(*ptr);
    *ptr = NULL;
}

void simple_mime_walk(ptr,handler,state_in,state_out)
     mime_t *ptr; 
     struct walk_handler *handler;
     in_state_t *state_in;
     out_state_t *state_out;   /* May be NULL */
{
    int count;

    if (handler->magic != WALKHANDLER_magic) 
	panic("MIME PANIC",__FILE__,__LINE__,"simple_mime_walk",
	      "Bad magic number",0);
	
    if (handler->handler_type->magic != WALKTYPE_magic) 
	panic("MIME PANIC",__FILE__,__LINE__,"simple_mime_walk",
	      "Bad type magic number",0);

    if (ptr->magic != MIME_magic)
	mime_panic(__FILE__,__LINE__,"simple_mime_walk",
		   "Bad mime magic number");
    
    if (!ptr->parser_data) 
	return;                     /* DATA not parsed */


    count = mime_parser_subparts(ptr->parser_data);
    
    /* Non leaf data is handled recursively */
    
    if (count > 0) {
	int i;

	for (i = 0; i < count; i++) {
	    mime_t *att        = mime_parser_index(ptr->parser_data,i);

	    simple_mime_walk(att,handler,state_in,state_out);
	}

	return;
    }

    DPRINT(Debug,11,(&Debug, 
		     "simple_mime_walk -> state: offset=%ld, length=%ld -- \n",
		     (long) ptr -> offset, (long) ptr -> length));


    if (state_in) {
	/* DECODE Content first */

	in_state_t newstate2;
	FILE *fp;
	
	in_state_clear(&newstate2,STATE_in_file);

	if (!in_state_seekable(state_in)) {
	    mime_panic(__FILE__,__LINE__,"simple_mime_walk",
		       "unsupported input state");
	}


	if (in_state_fseek(state_in,ptr->offset) != 0) {
	    /* state_nlputs or state_printf is needed for EOLN_is_CRLF
	       conversions */

	    if (state_out) {
		state_puts("[ ",state_out);
		state_printf(state_out,
			     CATGETS(elm_msg_cat, MeSet, MeXSeekFailed,
				     "%s: seek failed"),
			     "simple_mime_walk");
		state_nlputs(" ]\n",state_out);
	    }
	    lib_error(CATGETS(elm_msg_cat, MeSet, MeXSeekFailed,
			      "%s: seek failed"),
		      "simple_mime_walk");
	    return;
	}




	/* If state_out is NULL, or
	   state_out->filter == NULL then charset is not converted

	   state_out->filter tells input charset (ie. charset of 
	   mime type ....   messy??? )
	*/

	if (state_out)
	    state_out->filter = NULL;   /* FORCE no conversion */

	if (NULL != (fp=arrange_decoded(ptr,state_in,state_out,
					&newstate2,NULL))) {

	    handler->handler_type->mw_action(handler,ptr,
					     &newstate2,fp,state_out);
	    
	    fclose(fp);   /* in_state_destroy do not close file */
	}
	
	in_state_destroy(&newstate2);
  		
    } else
	handler->handler_type->mw_action(handler,ptr,NULL,NULL,state_out);
	    
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */


syntax highlighted by Code2HTML, v. 0.9.1