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

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

#include "def_mailer.h"
#include "hdr_imp.h"
#include "s_me.h"

#include <errno.h>
#ifndef ANSI_C
extern int errno;
#endif         

DEBUG_VAR(Debug,__FILE__,"header");


static unsigned char *s2us P_((char *str));
static unsigned char *s2us(str) 
     char *str;
{
    return (unsigned char *)str;
}

char * from_addr_literal(mailer_info)
     struct mailer_info *mailer_info;
{
    char * Q = NULL;

    /* If on username is strange characters (specially if username
       is from some other user database backend than from /etc/passwd)
       then quote it...
    */
    
    if (NULL != strpbrk(username,
			":\"\\!#%&()=?',;.:<>")) {
	Q = elm_message(FRM("%Q"),username);

	DPRINT(Debug,6,
	       (&Debug, "from_addr_literal: Quoting username: %s\n",Q));

    } else 
	Q = safe_strdup(username);


    if (mailer_info &&
	query_mailer_info(mailer_info,MI_USE_DOMAIN)) {
	Q = strmcat(Q,"@");
	Q = strmcat(Q, hostfullname);
    }

    return Q;
}

void make_from_addr(expanded,mailer_info)
     struct expanded_address *expanded;
     struct mailer_info *mailer_info;
{
    int pos;

    /* FIXME: Posible wrong charset */
    struct string * comment = new_string(display_charset);
    struct string * name    = new_string2(display_charset,s2us(full_username));

    char *s = safe_strdup(username);     /* Use username as surface addr */
    struct string *s1 = NULL;
    char *x;

    char * Q = from_addr_literal(mailer_info);

    free_expanded_address(expanded);

    pos = add_expanded_addr_(expanded,Q,name,comment);
    free(Q); Q = NULL;

    /* No fancy quotation stuff on textual form for editing */
    for (x = s; *x; x++) {
	switch(*x) {
	case '"': case '\\': case '(': case ')': case ':':
	case '<': case '>':
	    *x = '_';
	    break;
	}
    }
    
    s1 = new_string2(display_charset,s2us(s));

    add_textual_addr_(expanded,s1,pos,1);
    free(s); s = NULL;

    free_string(&s1);

    free_string(&name);
    free_string(&comment);
}



void zero_mailing_headers (hdrs)
     struct mailing_headers *hdrs;
{
    /* bzero is defined hdrs/defs.h */
    bzero((void *)hdrs,sizeof (*hdrs));
 
    hdrs->magic       = MAIL_HDR_magic;
    
    hdrs->subject     = NULL;
    zero_expanded_address(&(hdrs->from));
    zero_id_phrase(&(hdrs->in_reply_to));
    hdrs->expires     = NULL;
    hdrs->action      = NULL;
    hdrs->priority    = NULL;
    hdrs->sender    = NULL;
    zero_expanded_address(&(hdrs->reply_to));
    zero_expanded_address(&(hdrs->to));
    zero_expanded_address(&(hdrs->cc));
    hdrs->user_header       = NULL;
    hdrs->user_header_count = 0;
    zero_expanded_address(&(hdrs->bcc));
    hdrs->precedence   = NULL;
    hdrs->expires_days = NULL;
    hdrs->env_from     = NULL;
}

void free_mailing_headers (hdrs)
     struct mailing_headers *hdrs;
{
    if (hdrs->magic   != MAIL_HDR_magic) 
    	panic("HEADERS PANIC",__FILE__,__LINE__,"free_mailing_headers",
	      "Bad magic number",0);

    if (hdrs->subject)
	free_string(&(hdrs->subject));
    hdrs->subject     = NULL;
    free_expanded_address(&(hdrs->from));
    free_id_phrase(&(hdrs->in_reply_to));
    if (hdrs->expires)
	free(hdrs->expires);
    hdrs->expires     = NULL;
    if (hdrs->action)
	free(hdrs->action);
    hdrs->action      = NULL;
    if (hdrs->priority)
	free(hdrs->priority);
    hdrs->priority    = NULL;
    if (hdrs->sender)
	free(hdrs->sender);
    hdrs->sender     = NULL;
    free_expanded_address(&(hdrs->reply_to));
    free_expanded_address(&(hdrs->to));
    free_expanded_address(&(hdrs->cc));

    if (hdrs->user_header) {
	int i;

	for (i = 0; i < hdrs->user_header_count; i++) {
	    if (hdrs->user_header[i].value)
		free_string(&(hdrs->user_header[i].value));
	    hdrs->user_header[i].name = NULL;
	}
	free(hdrs->user_header);
	hdrs->user_header       = NULL;
    }
    hdrs->user_header_count = 0;

    free_expanded_address(&(hdrs->bcc));
    if (hdrs->precedence)
	free(hdrs->precedence);
    hdrs->precedence   = NULL;
    if (hdrs->expires_days)
	free(hdrs->expires_days);
    hdrs->expires_days = NULL;

    if (hdrs->env_from)
	mailer_free_env_from( &(hdrs->env_from) );

    /* Make sure that dangling pointters are catched */
    /* bzero is defined hdrs/defs.h */
    bzero((void *)hdrs,sizeof (*hdrs));
    
}


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


syntax highlighted by Code2HTML, v. 0.9.1