static char rcsid[] = "@(#)$Id: write_hdr.c,v 1.6 2006/05/22 19:17:14 hurtta Exp $";

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

#include "headers.h"

DEBUG_VAR(Debug,__FILE__,"header");

void print_EOLN(mailer,top_encoding) 
     out_state_t *mailer;
     int top_encoding;
{  
    /* Write canoniocal end of line of we are sending binary mail 
     * Also SMTP requires \r\n
     */
    if (top_encoding == ENCODING_BINARY || mailer->EOLN_is_CRLF)
	state_putc('\r',mailer);
    state_putc('\n',mailer);
}

void write_addr_header(mailer,hdr_name,addr,top_encoding,enmime,hdr_charset)
     out_state_t *mailer;
     char *hdr_name;
     struct addr_item * addr;
     int top_encoding;
     int enmime;
     charset_t hdr_charset;
{
    struct addr_item *p;
    long POS = out_state_ftell(mailer);

#define LEN (out_state_ftell(mailer)-POS)
#define WRAP print_EOLN(mailer,top_encoding), \
 (POS = out_state_ftell(mailer)), state_putc(' ',mailer)

    state_puts(hdr_name,mailer);
    state_puts(": ",mailer);

    DPRINT(Debug,9,
	   (&Debug, "write_addr_header: (enmime=%d) %s:",
	    enmime,hdr_name));
    
    for (p = addr; p && p->fullname && p->addr && p->comment; p++) {
	CONST int al = strlen(p->addr);
	CONST int fn = string_len(p->fullname);
	CONST int cm = string_len(p->comment);

	DPRINT(Debug,9,
	       (&Debug, "\n          [%d] ",p-addr));
	
	if (p != addr) {
	    state_puts(", ",mailer);
	    DPRINT(Debug,9,
		   (&Debug, ", "));
	}
	if (LEN > 70 || al + fn + cm + LEN > 76) {
	    WRAP;
	    DPRINT(Debug,9,
		   (&Debug,"[WRAP]\n          [%d] ",p-addr));
	}
	
	if (fn || ! p->addr[0] || '@' == p->addr[0]) {
	    if (fn) {
		char * buffer = string_to_hdr(HDR_PHRASE, p->fullname,
					      hdr_charset, enmime, NULL);
		char * c;

		DPRINT(Debug,9,
		       (&Debug,"{fullname quoted=%s}\n",
			buffer));
		DPRINT(Debug,9,
		       (&Debug, "\n          [%d] ",p-addr));
	       		
		for (c = buffer; *c != '\0'; c++) {
		    if (*c == '\n' || 
			(whitespace(*c) && (LEN > 120))) {
			WRAP;
			DPRINT(Debug,9,
			       (&Debug, "[WRAP]\n          [%d] ",
				p-addr));
		    } else {
			state_putc(*c,mailer);
			DPRINT(Debug,9,
			       (&Debug, "%c",*c));
		    }
		}
		free(buffer);
	    }

	    if (LEN > 120 || (LEN > 70 && fn > 70)) {
		WRAP;
		DPRINT(Debug,9,
		       (&Debug, "[WRAP]\n          [%d] ",p-addr));
	    } else {
		state_putc(' ',mailer);
		DPRINT(Debug,9,
		       (&Debug ," "));
	    }

	    state_putc('<',mailer);
	    state_puts(p->addr,mailer);
	    state_putc('>',mailer);
	    DPRINT(Debug,9,
		   (&Debug, "<%s>",p->addr));
	} else {
	    state_puts(p->addr,mailer);
	    DPRINT(Debug,9,
		   (&Debug ,"%s",p->addr));
	}
	if (cm) {
	    char * buffer = string_to_hdr(HDR_COMMENT, p->comment,
					  hdr_charset, enmime,NULL);
	    char * c;
	    
	    DPRINT(Debug,9,
		   (&Debug,"\n          [%d] ",p-addr));

	    if (LEN > 85) {
		DPRINT(Debug,9,
		       (&Debug, "[WRAP]\n          [%d] ",p-addr));
		WRAP;
	    } else {
		state_putc(' ',mailer);
		DPRINT(Debug,9,
		       (&Debug ," "));
	    }
	    
	    DPRINT(Debug,9,
		   (&Debug, "\n          {comment encoded=%s}",buffer));
	    DPRINT(Debug,9,
		   (&Debug, "\n          [%d] ",p-addr));

	    state_putc('(',mailer);			
	    DPRINT(Debug,9,
		   (&Debug, "("));
	    for (c = buffer; *c != '\0'; c++) {
		if (*c == '\n' || 
		    (whitespace(*c) && (LEN > 100))) {
		    WRAP;
		    DPRINT(Debug,9,
			   (&Debug, "[WRAP]\n          [%d] ",
			    p-addr));
		} else {
		    state_putc(*c,mailer);
		    DPRINT(Debug,9,
			   (&Debug, "%c",*c));
		}
	    }
	    state_putc(')',mailer);			
	    DPRINT(Debug,9,
		   (&Debug, ")"));
	    free(buffer);
	}
    }
    print_EOLN(mailer,top_encoding);
    DPRINT(Debug,9,
	   (&Debug, "[EOLN]\n"));
}


void write_text_header (mailer,hdr_name,text, top_encoding)
     out_state_t *mailer;
     CONST char *hdr_name; 
     CONST char * text;
     int top_encoding;
{
    long POS = out_state_ftell(mailer);
    CONST char *c;

    state_puts(hdr_name,mailer);
    state_puts(": ",mailer);

    DPRINT(Debug,9,
	   (&Debug,"write_text_header: %s:",hdr_name));
    
    for (c = text; *c != '\0'; c++) {
	if (*c == '\n' || 
	    (whitespace(*c) && (LEN > 75))) {
	    WRAP;
	    DPRINT(Debug,9,
		   (&Debug,"[WRAP]\n          "));
	} else {
	    state_putc(*c,mailer);
	    DPRINT(Debug,9,
		   (&Debug,"%c",*c));
	}
    }
    print_EOLN(mailer,top_encoding);    
    DPRINT(Debug,9,
	   (&Debug,"[EOLN]\n"));
}

#undef LEN
#undef WRAP

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



syntax highlighted by Code2HTML, v. 0.9.1