static char rcsid[] = "@(#)$Id: header_alloc.c,v 1.22 2006/05/30 16:33:21 hurtta Exp $";


/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.22 $   $State: Exp $
 *
 *  Author: Kari Hurtta <hurtta+elm@posti.FMI.FI> (was hurtta+elm@ozone.FMI.FI)
 *****************************************************************************
 * Some code copied from src/newmbox.c. That file is following
 * copyright:
 *
 *  The Elm Mail System 
 *
 *			Copyright (c) 1988-1992 USENET Community Trust
 *			Copyright (c) 1986,1987 Dave Taylor
 *****************************************************************************/

#include "def_messages.h"

DEBUG_VAR(Debug,__FILE__,"messages");


void realloc_headers(storage,current_count)
     struct current_storage *storage;
     int current_count;
{

    /* Code (indirectly) copied from src/newmbox.c 
       function read_headers()
    */

    if (current_count >= storage->max_headers) {
	struct header_rec **new_headers;
	int new_max;

	new_max = storage->max_headers + KLICK;
	if (storage->max_headers == 0) {
	    new_headers = (struct header_rec **)
		safe_malloc(new_max * sizeof(struct header_rec *));
	} else {
	    new_headers = (struct header_rec **)
		safe_realloc((char *) storage->headers,
			     new_max * sizeof(struct header_rec *));
	}
	storage->headers = new_headers;
	while (storage->max_headers < new_max)
	    storage->headers[storage->max_headers++] = NULL;
    }
}

void alloc_header(storage,current_count)
     struct current_storage *storage;
     int current_count;
{
    if (storage->headers[current_count] == NULL) {
	struct header_rec *h = safe_malloc(sizeof(*h));
		
	header_zero(h);
	storage->headers[current_count] = h;
    }
}


static time_t now = 0;

void header_clear(h)      /* Free's fields */
     struct header_rec *h;
{
    if (0 == now) now = time(NULL);
    
    h->lines            = 0;
    h->index_number_X   = 0; 
    h->status           = 0;
    h->status1          = 0;
    h->encrypted        = 0;
    h->exit_disposition = 0;
    h->status_chgd      = 0;
    h->content_length   = -1;
    h->offset           = -1;
    h->received_time    = now;
    h->env_from[0]      = '\0';
    if (h->from)
	free_addr_items(h->from);
    h->from             = NULL;
    if (h->to)
	free_addr_items(h->to);
    h->to               = NULL;
    if (h->cc)
	free_addr_items(h->cc);
    h->cc               = NULL;
    h->messageid[0]     = '\0';
    h->time_zone[0]     = '\0';
    h->time_sent        = 0;
    h->time_menu[0]     = '\0';
    h->tz_offset        = 0;
    if (h->subject)
	free_string(&(h->subject));
    h->subject          = NULL;
    h->mailx_status[0]  = '\0';
    mime_t_clear (&(h->mime_rec));
    free_rec_mbx_info(h);
    if (h->header_error)
	free_header_errors(& (h->header_error));

    free_partial_vector(h);

    if (h->list_info)
	free_list_info(& (h->list_info));
    
#ifdef USE_PGP
    h->pgp              = 0;
#endif
    h->binary           = 0;
    h->have_from        = 0;
    h->body_parsed      = 0;
    h->mime_parsed      = 0;
    h->header_charset   = NULL;
    h->override_charset = NULL;

}

void header_free(h)
     struct header_rec **h;
{
    header_clear(*h);
    free(*h);
    *h = NULL;
}

void header_zero(h)
     struct header_rec *h;
{
    if (0 == now) now = time(NULL);
    
    h->lines            = 0;
    h->index_number_X   = 0; 
    h->status           = 0;
    h->status1          = 0;
    h->encrypted        = 0;
    h->exit_disposition = 0;
    h->status_chgd      = 0;
    h->content_length   = -1;
    h->offset           = -1;
    h->received_time    = now;
    h->time_sent        = 0;
    h->env_from[0]      = '\0';
    h->from             = NULL;
    h->to               = NULL;
    h->cc               = NULL;
    h->messageid[0]     = '\0';
    h->time_zone[0]     = '\0';
    h->time_sent        = 0;
    h->time_menu[0]     = '\0';
    h->tz_offset        = 0;
    h->subject          = NULL;
    h->mailx_status[0]  = '\0';
    mime_t_zero (&(h->mime_rec));

    h->header_error     = NULL;
    h->mbx_info         = NULL;
    h->partial_vector   = NULL;
    h->partial_len      = 0;
    h->list_info        = NULL;


#ifdef USE_PGP
    h->pgp              = 0;
#endif
    h->binary           = 0;
    h->have_from        = 0;
    h->body_parsed      = 0;
    h->mime_parsed      = 0;
    h->header_charset   = NULL;
    h->override_charset = NULL;
}

void header_parse_helper(current_header,parsed_headers)
     struct header_rec *current_header;
     header_list_ptr parsed_headers;
{
    int mh = give_dt_enumerate_as_int(&message_hide_hack);

    header_list_ptr tmphdr;

    if (NULL != (tmphdr = locate_header_by_name(parsed_headers,
						"Sensitivity"))) {
	if (0 == strcmp(tmphdr->body,"2"))
	    current_header->status |= PRIVATE_MAIL;
	else if (0 == strcmp(tmphdr->body,"3"))
	    current_header->status |= CONFIDENTIAL;
    }
    
    if (NULL != (tmphdr = locate_header_by_name(parsed_headers,
						"Importance"))) {
	if (0 == strcmp(tmphdr->body,"2"))
	    current_header->status |= URGENT;
    }
    
    if (NULL != (tmphdr = locate_header_by_name(parsed_headers,
						"Priority"))) {
	if (0 != istrcmp(tmphdr->body,"normal") &&
	    0 != istrcmp(tmphdr->body,"non-urgent"))
	    current_header->status |= URGENT;
    }

    if (NULL != (tmphdr = locate_header_by_name(parsed_headers,
						"Action"))) {
	current_header->status |= ACTION;

	
    }

    mime_parse_helper(current_header,parsed_headers);


    if (NULL != (tmphdr = locate_header_by_name(parsed_headers,
						"Status"))) {
	mailbox_2_status(tmphdr->body,current_header);
    }
    
    if (NULL != (tmphdr = locate_header_by_name(parsed_headers,
						"Subject"))) {
	char * Subject = safe_strdup(tmphdr->body);
	
	if (current_header->subject)
	    free_string(&(current_header->subject));
	
	remove_possible_trailing_spaces(Subject);
	
	current_header->subject = 
	    hdr_to_string(HDR_TEXT,
			  Subject,
			  current_header->header_charset,
			  !(current_header -> status &
			    NOHDRENCODING) &&
			  is_rfc1522(Subject));		
	
	/* This does not work correctly with unknown character 
	   sets... */
	if (current_header->subject)
	    remove_control(current_header->subject);
	
	free(Subject);
    } else if (!current_header->subject)
	current_header->subject = new_string(display_charset);
    
    
    
    if (current_header->from)
	free_addr_items(current_header->from);
    current_header->from = NULL;

    for (tmphdr = locate_header_by_name(parsed_headers,
					"From");
	 tmphdr;
	 tmphdr = tmphdr -> next_this_header) {
	
	struct addr_item * addrs = 
	    break_down_address(tmphdr->body, 
			       !(current_header -> status & 
				 NOHDRENCODING) &&
			       is_rfc1522(tmphdr->body),
			       current_header->header_charset);
	
	if (addrs) {	    

	    if (current_header->from) {
		append_addr_items(& (current_header->from), addrs);
		free_addr_items(addrs);
	    
	    } else
		current_header->from = addrs;

	}	       
    }

    if (current_header->list_info)
	free_list_info(& (current_header->list_info));
    current_header->list_info = 
	parse_list_headers(parsed_headers,
			   !(current_header -> status & NOHDRENCODING),
			   current_header-> header_charset,
			   & (current_header-> header_error) );

    if (current_header->to)
	free_addr_items(current_header->to);
    current_header->to = NULL;

    for (tmphdr = locate_header_by_name(parsed_headers,
					"To");
	 tmphdr;
	 tmphdr = tmphdr -> next_this_header) {

	struct addr_item * addrs = 
	    break_down_address(tmphdr->body,
			       !(current_header -> status & 
				 NOHDRENCODING) &&
			       is_rfc1522(tmphdr->body),
			       current_header->header_charset);
	
	if (addrs) {

	    if (current_header->to) {
		append_addr_items(& (current_header->to), addrs);
		free_addr_items(addrs);
	    } else
		current_header->to = addrs;
	}	       
    }

    if (current_header->cc)
	free_addr_items(current_header->cc);
    current_header->cc = NULL;

    for (tmphdr = locate_header_by_name(parsed_headers,
						"Newsgroup");
	 tmphdr;
	 tmphdr = tmphdr -> next_this_header) {
	struct addr_item * addrs =
	    break_down_address(tmphdr->body,
			       !(current_header -> status &
				 NOHDRENCODING) &&
			       is_rfc1522(tmphdr->body),
			       current_header->header_charset);

	if (addrs) {
	    if (current_header->to)
		free_addr_items(current_header->to);
	    current_header->to = addrs;

#if DEBUG
	    if (Debug.active > 10) {
		struct addr_item * p;
		for (p =  current_header->to; p->addr; p++) {
		    DPRINT(Debug,15,(&Debug,
				     "--   newsgroup[%d] addr='%s' comment='%S' fullname='%S'\n",
				     p - current_header->to,
				     p->addr,
				     p->comment,
				     p->fullname));
		}
	    }
#endif
	}
    }

    for (tmphdr = locate_header_by_name(parsed_headers,
					"Cc");
	 tmphdr;
	 tmphdr = tmphdr -> next_this_header) {

	struct addr_item * addrs = 
	    break_down_address(tmphdr->body,
				       !(current_header -> status & 
					 NOHDRENCODING) &&
			       is_rfc1522(tmphdr->body),
			       current_header->header_charset);
	
	if (addrs) {
	    if (current_header->cc) {
		append_addr_items(& (current_header->cc), addrs);
		free_addr_items(addrs);
	    } else 
		current_header->cc = addrs;
	}
    }

    /* message_hide_hack values:   0 == none                          */
                                /* 1 = FOLDER INTERNAL DATA           */

    switch (mh) {
	header_list_ptr tmphdr2;

    case 1:
	tmphdr  = locate_header_by_name(parsed_headers, "Subject");
	tmphdr2 = locate_header_by_name(parsed_headers, "X-IMAP");

	if (tmphdr && tmphdr2 &&
	    NULL != strstr(tmphdr->body,
			   "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA")) {
	    current_header->exit_disposition = HIDE;
	    DPRINT(Debug,10,(&Debug, "message_hide_hack=%d -- hiding message: %s\n",
			     mh,tmphdr->body));
	}
	break;
    }
}			      


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


syntax highlighted by Code2HTML, v. 0.9.1