示例#1
0
文件: DmxMessage.C 项目: juddy/edcde
char *
DmxMsg::getHeaders (DtMailBoolean abbreviated_only)
{
    DtMailEnv		error;
    DtMail::Session	*m_session = theRoamApp.session()->session(); 
    DtMail::MailRc	*mail_rc = m_session->mailRc(error);
    DtMail::Envelope	*env = message->getEnvelope(error);

    DtMailHeaderHandle	hdr_hnd;
    char		*name;
    DtMailValueSeq	value;

    // Code from MsgScrollingList - display_message().
    // We're trying to reduce heap size by not allocating and 
    // deleting space in every loop iteration.  So just have a 
    // fixed size buffer initially.
    // 

    // Initial line size.  When not enough, allocate more.
    int			buffer_size = 2048;   
    char		*buffer = new char [buffer_size];
    int			count = 0;
    int			hdr_num = 0;
    char		*newline = "\n";
    char		*separator = ": ";
    int			val = 0;

    //
    // Iterate through each header in the message and add it
    // to the buffer.
    //
    for (hdr_hnd = env->getFirstHeader(error, &name, value), *buffer = '\0';
	 hdr_hnd && !error.isSet();
	 hdr_hnd = env->getNextHeader(error, hdr_hnd, &name, value), hdr_num++)
    {
        if (abbreviated_only == DTM_TRUE &&
	    (hdr_num != 0 || strcmp(name, "From") != 0))
	{
	    DtMailEnv ierror;
	    if (mail_rc->ignore(ierror, name))
	    {
		free(name);
		value.clear();
		continue;
	    }
	}
	    
	for (val=0;  val<value.length();  val++)
	  count += strlen(name) +
		   strlen(*(value[val])) +
		   strlen(separator) +
		   strlen(newline) + 1;

	if (count > buffer_size)
	{
	    // Need to increase buffer size.
	    char	*new_buffer;
	    
	    buffer_size *= 2;
	    new_buffer = new char [buffer_size];
	    memset(new_buffer, 0, buffer_size);

	    strcpy(new_buffer, buffer);
	    delete [] buffer;
	    buffer = new_buffer;
	}

	for (val=0;  val<value.length();  val++)
	{
	    strcat(buffer, name);
		
	    if (hdr_num != 0 || strcmp(name, "From") != 0)
	      strcat(buffer, separator);
	    else
	      strcat(buffer, " ");
		
	    strcat(buffer, *(value[val]));
	    strcat(buffer, newline);
	}
	value.clear();
	free(name);
    }

    //
    // Need to free this after using;
    //
    return buffer;
}
示例#2
0
文件: RFCFormat.C 项目: juddy/edcde
void
RFCFormat::writeHeaders(DtMailEnv & error,
			DtMail::Message & msg,
			DtMailBoolean include_unix_from,
			const char * extra_headers,
			const char ** suppress_headers,
			Buffer & buf)
{
    error.clear();

    // First we copy each header from the message to the
    // buffer. The headers may need encoding to put them away, so
    // we will apply RFC1522 if necessary.
    //
    DtMailHeaderHandle hnd;
    DtMail::Envelope * env = msg.getEnvelope(error);
    if (error.isSet()) {
	return;
    }

    char * name = NULL;
    DtMailValueSeq value;

    hnd = env->getFirstHeader(error, &name, value);
    if (!hnd || error.isSet()) {
	return;
    }

    if (include_unix_from &&
	(error.isSet() || strcmp(name, "From") != 0)) {
	// We require a Unix from line, and we don't have one.
	// we will make one up using the sender, and the current
	// date.
	//
	char *unix_from = new char[100];
	strcpy(unix_from, "From ");
	
	DtMailValueSeq sender;
	env->getHeader(error, DtMailMessageSender, DTM_TRUE, sender);
	if (error.isSet()) {
	    // We no longer know who this is really from.
	    //
	    strcat(unix_from, "nobody@nowhere");
	}
	else {
	    DtMailAddressSeq * addrSeq = sender[0]->toAddress();
	    
	    strcat(unix_from, (*addrSeq)[0]->dtm_address);
	    delete addrSeq;
	}
	error.clear();

	time_t now = time(NULL);
	char time_format[30];
	
	SafeCtime(&now, time_format, sizeof(time_format));
	
	strcat(unix_from, " ");
	strcat(unix_from, time_format);
	buf.appendData(unix_from, strlen(unix_from));
	delete [] unix_from;
    }
    else {
	// Put out any header, except Unix From line
	//
	if (strcmp(name, "From") == 0) {
	    value.clear();
	    free(name);
	    hnd = env->getNextHeader(error, hnd, &name, value);
	}
    }

    for (; // First time is determined above.
	 hnd && !error.isSet();
	 value.clear(), hnd = env->getNextHeader(error, hnd, &name, value)) {

	const char **hdr;
	for (hdr = suppress_headers; *hdr; hdr++) {
	    if (strcasecmp(name, *hdr) == 0)
	      break;
	}

        //add _is_write_bcc for fixing aix defect 177096
	if (*hdr || strcasecmp(name, "bcc") == 0 && !_is_write_bcc ) {
	    free(name);
	    continue; // We will generate these headers.
	}

	int name_len = strlen(name);

	for (int val = 0; val < value.length(); val++) {
	    //
	    // If the value is null or empty do not emit this field
	    const char *valPtr;
	    for (valPtr = *(value[val]);
		 *valPtr && (isspace((unsigned char)*valPtr));
		 valPtr++)
	    {}
	    if (!*valPtr)
	      continue;
	    
	    buf.appendData(name, name_len);
	    buf.appendData(": ", 2);
	    rfc1522cpy(buf, *(value[val]));
	}

	free(name);
    }
    error.clear();

    buf.appendData(extra_headers, strlen(extra_headers));

    // Add new line that terminates the headers.
    //
    crlf(buf);
}