lineWidth = PR_smprintf("; linewidth=%d", ((nsMsgCompose*)fields->GetOwner())->GetLineWidth());
mime_generate_headers (nsMsgCompFields *fields,									     const char *charset,									     nsMsgDeliverMode deliver_mode){  nsresult rv;  NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv); 	int size = 0;	char *buffer = 0, *buffer_tail = 0;	PRBool isDraft = deliver_mode == nsMsgSaveAsDraft ||					deliver_mode == nsMsgSaveAsTemplate ||					deliver_mode == nsMsgQueueForLater;	const char* pFrom;	const char* pTo;	const char* pCc;	const char* pMessageID;	const char* pReplyTo;	const char* pOrg;	const char* pNewsGrp;	const char* pFollow;	const char* pSubject;	const char* pPriority;	const char* pReference;	const char* pOtherHdr;	NS_ASSERTION (fields, "null fields");	if (!fields)		return NULL;	/* Multiply by 3 here to make enough room for MimePartII conversion */	pFrom = fields->GetFrom(); if (pFrom)						size += 3 * PL_strlen (pFrom);	pReplyTo =fields->GetReplyTo(); if (pReplyTo)				size += 3 * PL_strlen (pReplyTo);	pTo = fields->GetTo(); if (pTo)								size += 3 * PL_strlen (pTo);	pCc = fields->GetCc(); if (pCc)								size += 3 * PL_strlen (pCc);	pNewsGrp = fields->GetNewsgroups(); if (pNewsGrp)			size += 3 * PL_strlen (pNewsGrp);	pFollow= fields->GetFollowupTo(); if (pFollow)				size += 3 * PL_strlen (pFollow);	pSubject = fields->GetSubject(); if (pSubject)				size += 3 * PL_strlen (pSubject);	pReference = fields->GetReferences(); if (pReference)		size += 3 * PL_strlen (pReference);	pOrg= fields->GetOrganization(); if (pOrg)					size += 3 * PL_strlen (pOrg);	pOtherHdr= fields->GetOtherRandomHeaders(); if (pOtherHdr)	size += 3 * PL_strlen (pOtherHdr);	pPriority = fields->GetPriority();  if (pPriority)			size += 3 * PL_strlen (pPriority);#ifdef GENERATE_MESSAGE_ID	pMessageID = fields->GetMessageId(); if (pMessageID)		size += PL_strlen (pMessageID);#endif /* GENERATE_MESSAGE_ID */	/* Add a bunch of slop for the static parts of the headers. */	/* size += 2048; */	size += 2560;	buffer = (char *) PR_Malloc (size);	if (!buffer)		return 0; /* MK_OUT_OF_MEMORY */		buffer_tail = buffer;#ifdef GENERATE_MESSAGE_ID	if (pMessageID && *pMessageID) {		char *convbuf = NULL;		PUSH_STRING ("Message-ID: ");		PUSH_STRING (pMessageID);		PUSH_NEWLINE ();		/* MDN request header requires to have MessageID header presented		* in the message in order to		* coorelate the MDN reports to the original message. Here will be		* the right place		*/		if (fields->GetReturnReceipt() && 			(fields->GetReturnReceiptType() == 2 ||			fields->GetReturnReceiptType() == 3) && 			(deliver_mode != nsMsgSaveAsDraft &&			deliver_mode != nsMsgSaveAsTemplate))		{			PRInt32 receipt_header_type = 0;      if (prefs)     	  prefs->GetIntPref("mail.receipt.request_header_type", &receipt_header_type);			// 0 = MDN Disposition-Notification-To: ; 1 = Return-Receipt-To: ; 2 =			// both MDN DNT & RRT headers			if (receipt_header_type == 1) {				RRT_HEADER:				PUSH_STRING ("Return-Receipt-To: ");				convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pFrom, charset, nsMsgMIMEGetConformToStandard());				if (convbuf) {    /* MIME-PartII conversion */					PUSH_STRING (convbuf);					PR_FREEIF(convbuf);				}				else					PUSH_STRING (pFrom);				PUSH_NEWLINE ();			}			else  {				PUSH_STRING ("Disposition-Notification-To: ");				convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pFrom, charset,												nsMsgMIMEGetConformToStandard());				if (convbuf) {     /* MIME-PartII conversion */					PUSH_STRING (convbuf);					PR_FREEIF(convbuf);				}				else					PUSH_STRING (pFrom);				PUSH_NEWLINE ();				if (receipt_header_type == 2)					goto RRT_HEADER;			}		}#ifdef SUPPORT_X_TEMPLATE_NAME		if (deliver_mode == MSG_SaveAsTemplate) {			PUSH_STRING ("X-Template: ");			char * pStr = fields->GetTemplateName();			if (pStr) {				convbuf = nsMsgI18NEncodeMimePartIIStr((char *)									 pStr,									 charset,									 nsMsgMIMEGetConformToStandard());				if (convbuf) {				  PUSH_STRING (convbuf);				  PR_FREEIF(convbuf);				}				else				  PUSH_STRING(pStr);			}			PUSH_NEWLINE ();		}#endif /* SUPPORT_X_TEMPLATE_NAME */	}#endif /* GENERATE_MESSAGE_ID */	PRExplodedTime now;    PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &now);	int gmtoffset = now.tm_params.tp_gmt_offset / 60; /*We need the offset in minute and not in second! */	/* Use PR_FormatTimeUSEnglish() to format the date in US English format,	   then figure out what our local GMT offset is, and append it (since	   PR_FormatTimeUSEnglish() can't do that.) Generate four digit years as	   per RFC 1123 (superceding RFC 822.)	 */	PR_FormatTimeUSEnglish(buffer_tail, 100,						   "Date: %a, %d %b %Y %H:%M:%S ",						   &now);	buffer_tail += PL_strlen (buffer_tail);	PR_snprintf(buffer_tail, buffer + size - buffer_tail,				"%c%02d%02d" CRLF,				(gmtoffset >= 0 ? '+' : '-'),				((gmtoffset >= 0 ? gmtoffset : -gmtoffset) / 60),				((gmtoffset >= 0 ? gmtoffset : -gmtoffset) % 60));	buffer_tail += PL_strlen (buffer_tail);	if (pFrom && *pFrom) {		char *convbuf;		PUSH_STRING ("From: ");		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pFrom, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf) {    /* MIME-PartII conversion */			PUSH_STRING (convbuf);			PR_Free(convbuf);		}		else			PUSH_STRING (pFrom);		PUSH_NEWLINE ();	}	if (pReplyTo && *pReplyTo)	{		char *convbuf;		PUSH_STRING ("Reply-To: ");		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pReplyTo, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf) {     /* MIME-PartII conversion */			PUSH_STRING (convbuf);			PR_Free(convbuf);		}		else			PUSH_STRING (pReplyTo);		PUSH_NEWLINE ();	}	if (pOrg && *pOrg)	{		char *convbuf;		PUSH_STRING ("Organization: ");		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pOrg, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf) {     /* MIME-PartII conversion */			PUSH_STRING (convbuf);			PR_Free(convbuf);		}		else			PUSH_STRING (pOrg);		PUSH_NEWLINE ();	}	// X-Sender tag	if (fields->GetOwner())	{		PRBool bUseXSender = PR_FALSE;    if (prefs)   	  prefs->GetBoolPref("mail.use_x_sender", &bUseXSender);		if (bUseXSender) {			char *convbuf;			char *tmpBuffer=nsnull;			PUSH_STRING ("X-Sender: ");			PUSH_STRING("\"");      if (prefs)   	    prefs->CopyCharPref("mail.identity.username", &tmpBuffer);			convbuf = nsMsgI18NEncodeMimePartIIStr(tmpBuffer, charset,										nsMsgMIMEGetConformToStandard());			if (convbuf) {     /* MIME-PartII conversion */				PUSH_STRING (convbuf);				PR_Free(convbuf);			}			else				PUSH_STRING (tmpBuffer);            if (tmpBuffer) PL_strfree(tmpBuffer);            tmpBuffer=nsnull;            			PUSH_STRING("\" <");      if (NS_SUCCEEDED(rv) && prefs)   	    prefs->CopyCharPref("mail.smtp_name", &tmpBuffer);			convbuf = nsMsgI18NEncodeMimePartIIStr(tmpBuffer, charset,											nsMsgMIMEGetConformToStandard());			if (convbuf) {     /* MIME-PartII conversion */				PUSH_STRING (convbuf);				PR_Free(convbuf);			}			else				PUSH_STRING (tmpBuffer);            if (tmpBuffer) PL_strfree(tmpBuffer);            tmpBuffer=nsnull;			PUSH_STRING ("@");      if (NS_SUCCEEDED(rv) && prefs)   	    prefs->CopyCharPref("network.hosts.smtp_server", &tmpBuffer);			convbuf = nsMsgI18NEncodeMimePartIIStr(tmpBuffer, charset,											nsMsgMIMEGetConformToStandard());			if (convbuf) {     /* MIME-PartII conversion */				PUSH_STRING (convbuf);				PR_Free(convbuf);			}			else				PUSH_STRING (tmpBuffer);			PUSH_STRING(">");			convbuf = nsMsgI18NEncodeMimePartIIStr(tmpBuffer, charset,											nsMsgMIMEGetConformToStandard());            if (tmpBuffer) PL_strfree(tmpBuffer);            tmpBuffer=nsnull;			if (!fields->GetOwner()->GetMaster()->IsUserAuthenticated())				PUSH_STRING (" (Unverified)");			PUSH_NEWLINE ();		}	}	// X-Mozilla-Draft-Info	if (isDraft) {		char *htmlAction = 0;		char *lineWidth = 0; // force plain text hard line break info		PUSH_STRING(HEADER_X_MOZILLA_DRAFT_INFO);		PUSH_STRING(": internal/draft; ");		if (fields->GetAttachVCard())			PUSH_STRING("vcard=1");		else			PUSH_STRING("vcard=0");		PUSH_STRING("; ");		if (fields->GetReturnReceipt()) {			char *type = PR_smprintf("%d", (int) fields->GetReturnReceiptType());			if (type)			{				PUSH_STRING("receipt=");				PUSH_STRING(type);				PR_FREEIF(type);			}		}		else			PUSH_STRING("receipt=0");		PUSH_STRING("; ");		if (fields->GetBoolHeader(MSG_UUENCODE_BINARY_BOOL_HEADER_MASK))		  PUSH_STRING("uuencode=1");		else		  PUSH_STRING("uuencode=0");		htmlAction = PR_smprintf("html=%d", 		  ((nsMsgCompose*)fields->GetOwner())->GetHTMLAction());		if (htmlAction) {			PUSH_STRING("; ");			PUSH_STRING(htmlAction);			PR_FREEIF(htmlAction);		}		lineWidth = PR_smprintf("; linewidth=%d",		  ((nsMsgCompose*)fields->GetOwner())->GetLineWidth());		if (lineWidth) {			PUSH_STRING(lineWidth);			PR_FREEIF(lineWidth);		}		PUSH_NEWLINE ();	}  NS_WITH_SERVICE(nsINetService, pNetService, kNetServiceCID, &rv); 	if (NS_SUCCEEDED(rv) && pNetService)	{		nsString aNSStr;		char* sCStr;		pNetService->GetAppCodeName(aNSStr);		sCStr = aNSStr.ToNewCString();		if (sCStr) {			PUSH_STRING ("X-Mailer: ");			PUSH_STRING(sCStr);			delete [] sCStr;			pNetService->GetAppVersion(aNSStr);			sCStr = aNSStr.ToNewCString();			if (sCStr) {				PUSH_STRING (" ");				PUSH_STRING(sCStr);				delete [] sCStr;			}			PUSH_NEWLINE ();		}	}	/* for Netscape Server, Accept-Language data sent in Mail header */	char *acceptlang = nsMsgI18NGetAcceptLanguage();	if( (acceptlang != NULL) && ( *acceptlang != '\0') ){		PUSH_STRING( "X-Accept-Language: " );		PUSH_STRING( acceptlang );		PUSH_NEWLINE();	}	PUSH_STRING ("MIME-Version: 1.0" CRLF);	if (pNewsGrp && *pNewsGrp) {		/* turn whitespace into a comma list		*/		char *ptr, *ptr2;		char *n2;		char *convbuf;		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pNewsGrp, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf)	  		n2 = nsMsgStripLine (convbuf);		else {			ptr = PL_strdup(pNewsGrp);			if (!ptr) {				PR_FREEIF(buffer);				return 0; /* MK_OUT_OF_MEMORY */			}  	  		n2 = nsMsgStripLine(ptr);			NS_ASSERTION(n2 == ptr, "n2 != ptr");	/* Otherwise, the PR_Free below is													   gonna choke badly. */		}		for(ptr=n2; *ptr != '\0'; ptr++) {			/* find first non white space */			while(!IS_SPACE(*ptr) && *ptr != ',' && *ptr != '\0')				ptr++;			if(*ptr == '\0')				break;			if(*ptr != ',')				*ptr = ',';			/* find next non white space */			ptr2 = ptr+1;			while(IS_SPACE(*ptr2))				ptr2++;			if(ptr2 != ptr+1)				PL_strcpy(ptr+1, ptr2);		}		PUSH_STRING ("Newsgroups: ");		PUSH_STRING (n2);		PR_Free (n2);		PUSH_NEWLINE ();	}	/* #### shamelessly duplicated from above */	if (pFollow && *pFollow) {		/* turn whitespace into a comma list		*/		char *ptr, *ptr2;		char *n2;		char *convbuf;		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pFollow, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf)			n2 = nsMsgStripLine (convbuf);		else {			ptr = PL_strdup(pFollow);			if (!ptr) {				PR_FREEIF(buffer);				return 0; /* MK_OUT_OF_MEMORY */			}			n2 = nsMsgStripLine (ptr);			NS_ASSERTION(n2 == ptr, "n2 != ptr");	/* Otherwise, the PR_Free below is													   gonna choke badly. */		}		for (ptr=n2; *ptr != '\0'; ptr++) {			/* find first non white space */			while(!IS_SPACE(*ptr) && *ptr != ',' && *ptr != '\0')				ptr++;			if(*ptr == '\0')				break;			if(*ptr != ',')				*ptr = ',';			/* find next non white space */			ptr2 = ptr+1;			while(IS_SPACE(*ptr2))				ptr2++;			if(ptr2 != ptr+1)				PL_strcpy(ptr+1, ptr2);		}		PUSH_STRING ("Followup-To: ");		PUSH_STRING (n2);		PR_Free (n2);		PUSH_NEWLINE ();	}	if (pTo && *pTo) {		char *convbuf;		PUSH_STRING ("To: ");		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pTo, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf) {     /* MIME-PartII conversion */			PUSH_STRING (convbuf);			PR_Free(convbuf);		}		else			PUSH_STRING (pTo);		PUSH_NEWLINE ();	} 	if (pCc && *pCc) {		char *convbuf;		PUSH_STRING ("CC: ");		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pCc, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf) {   /* MIME-PartII conversion */			PUSH_STRING (convbuf);			PR_Free(convbuf);		}		else			PUSH_STRING (pCc);		PUSH_NEWLINE ();	}	if (pSubject && *pSubject) {		char *convbuf;		PUSH_STRING ("Subject: ");		convbuf = nsMsgI18NEncodeMimePartIIStr((char *)pSubject, charset,										nsMsgMIMEGetConformToStandard());		if (convbuf) {  /* MIME-PartII conversion */			PUSH_STRING (convbuf);			PR_Free(convbuf);		}		else			PUSH_STRING (pSubject);		PUSH_NEWLINE ();	}		if (pPriority && *pPriority)		if (!PL_strcasestr(pPriority, "normal")) {			PUSH_STRING ("X-Priority: ");			/* Important: do not change the order of the 			* following if statements			*/			if (PL_strcasestr (pPriority, "highest"))				PUSH_STRING("1 (");			else if (PL_strcasestr(pPriority, "high"))				PUSH_STRING("2 (");			else if (PL_strcasestr(pPriority, "lowest"))				PUSH_STRING("5 (");			else if (PL_strcasestr(pPriority, "low"))				PUSH_STRING("4 (");			PUSH_STRING (pPriority);			PUSH_STRING(")");			PUSH_NEWLINE ();		}	if (pReference && *pReference) {		PUSH_STRING ("References: ");		if (PL_strlen(pReference) >= 986) {			char *references = PL_strdup(pReference);			char *trimAt = PL_strchr(references+1, '<');			char *ptr;			// per sfraser, RFC 1036 - a message header line should not exceed			// 998 characters including the header identifier			// retiring the earliest reference one after the other			// but keep the first one for proper threading			while (references && PL_strlen(references) >= 986 && trimAt) {				ptr = PL_strchr(trimAt+1, '<');				if (ptr)				  XP_MEMMOVE(trimAt, ptr, PL_strlen(ptr)+1); // including the				else				  break;			}			NS_ASSERTION(references, "null references");			if (references) {			  PUSH_STRING (references);			  PR_Free(references);			}		}		else		  PUSH_STRING (pReference);		PUSH_NEWLINE ();	}	if (pOtherHdr && *pOtherHdr) {		/* Assume they already have the right newlines and continuations		 and so on. */		PUSH_STRING (pOtherHdr);	}	if (buffer_tail > buffer + size - 1)		abort ();	/* realloc it smaller... */	buffer = (char*)PR_REALLOC (buffer, buffer_tail - buffer + 1);	return buffer;}