Skip to content

Commit

Permalink
Bug 998191, part 10: Simplify mime_generate_headers by using JSMime f…
Browse files Browse the repository at this point in the history
…or newsgroup headers, r=irving

It is hoped that other, non-NNTP headers will get added to extraMimeParsers.jsm,
but there is only a need for the Newsgroup header right now.
  • Loading branch information
jcranmer committed Jan 7, 2015
1 parent 4ada1a9 commit b7fac64
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 109 deletions.
121 changes: 13 additions & 108 deletions mailnews/compose/src/nsMsgCompUtils.cpp
Expand Up @@ -224,30 +224,6 @@ nsresult mime_sanity_check_fields (
return mime_sanity_check_fields_recipients(to, cc, bcc, newsgroups);
}

static char *
nsMsgStripLine (char * string)
{
char * ptr;

/* remove leading blanks */
while(*string=='\t' || *string==' ' || *string=='\r' || *string=='\n')
string++;

for(ptr=string; *ptr; ptr++)
; /* NULL BODY; Find end of string */

/* remove trailing blanks */
for(ptr--; ptr >= string; ptr--)
{
if(*ptr=='\t' || *ptr==' ' || *ptr=='\r' || *ptr=='\n')
*ptr = '\0';
else
break;
}

return string;
}

//
// Generate the message headers for the new RFC822 message
//
Expand Down Expand Up @@ -275,8 +251,6 @@ mime_generate_headers (nsMsgCompFields *fields,
deliver_mode == nsIMsgSend::nsMsgQueueForLater ||
deliver_mode == nsIMsgSend::nsMsgDeliverBackground;

const char* pNewsGrp;
const char* pFollow;
const char* pReference;

bool hasDisclosedRecipient = false;
Expand All @@ -286,8 +260,6 @@ mime_generate_headers (nsMsgCompFields *fields,
*status = NS_ERROR_NULL_POINTER;
return nullptr;
}
pNewsGrp = fields->GetNewsgroups(); if (pNewsGrp) size += 3 * PL_strlen (pNewsGrp);
pFollow= fields->GetFollowupTo(); if (pFollow) size += 3 * PL_strlen (pFollow);
pReference = fields->GetReferences(); if (pReference) size += 3 * PL_strlen (pReference);

/* Add a bunch of slop for the static parts of the headers. */
Expand Down Expand Up @@ -417,63 +389,31 @@ mime_generate_headers (nsMsgCompFields *fields,

finalHeaders->SetUnstructuredHeader("MIME-Version", NS_LITERAL_STRING("1.0"));

if (pNewsGrp && *pNewsGrp) {
/* turn whitespace into a comma list
*/
char *duppedNewsGrp = PL_strdup(pNewsGrp);
if (!duppedNewsGrp) {
PR_FREEIF(buffer);
*status = NS_ERROR_OUT_OF_MEMORY;
return nullptr; /* NS_ERROR_OUT_OF_MEMORY */
}
char *n2 = nsMsgStripLine(duppedNewsGrp);

for(char *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 */
char *ptr2 = ptr+1;
while(IS_SPACE(*ptr2))
ptr2++;

if(ptr2 != ptr+1)
PL_strcpy(ptr+1, ptr2);
}

// we need to decide the Newsgroup related headers
// to write to the outgoing message. In ANY case, we need to write the
// "Newsgroup" header which is the "proper" header as opposed to the
// HEADER_X_MOZILLA_NEWSHOST which can contain the "news:" URL's.
//
// Since n2 can contain data in the form of:
nsAutoCString newsgroups;
finalHeaders->GetRawHeader("Newsgroups", newsgroups);
if (!newsgroups.IsEmpty())
{
// Since the newsgroup header can contain data in the form of:
// "news://news.mozilla.org/netscape.test,news://news.mozilla.org/netscape.junk"
// we need to turn that into: "netscape.test,netscape.junk"
//
nsCOMPtr<nsINntpService> nntpService = do_GetService("@mozilla.org/messenger/nntpservice;1", &rv);
if (NS_FAILED(rv) || !nntpService) {
// (XXX: can it really?)
nsCOMPtr<nsINntpService> nntpService =
do_GetService("@mozilla.org/messenger/nntpservice;1", &rv);
if (NS_FAILED(rv) || !nntpService)
{
*status = NS_ERROR_FAILURE;
return nullptr;
}

nsCString newsgroupsHeaderVal;
nsCString newshostHeaderVal;
rv = nntpService->GenerateNewsHeaderValsForPosting(nsDependentCString(n2), getter_Copies(newsgroupsHeaderVal), getter_Copies(newshostHeaderVal));
rv = nntpService->GenerateNewsHeaderValsForPosting(newsgroups,
getter_Copies(newsgroupsHeaderVal), getter_Copies(newshostHeaderVal));
if (NS_FAILED(rv))
{
*status = rv;
return nullptr;
}

// fixme:the newsgroups header had better be encoded as the server-side
// character encoding, but this |charset| might be different from it.
finalHeaders->SetRawHeader("Newsgroups", newsgroupsHeaderVal, charset);

// If we are here, we are NOT going to send this now. (i.e. it is a Draft,
Expand All @@ -490,45 +430,10 @@ mime_generate_headers (nsMsgCompFields *fields,
charset);
}

PR_FREEIF(duppedNewsGrp);
// Newsgroups are a recipient...
hasDisclosedRecipient = true;
}

/* #### shamelessly duplicated from above */
if (pFollow && *pFollow) {
/* turn whitespace into a comma list
*/
char *duppedFollowup = PL_strdup(pFollow);
if (!duppedFollowup) {
PR_FREEIF(buffer);
return nullptr; /* NS_ERROR_OUT_OF_MEMORY */
}
char *n2 = nsMsgStripLine (duppedFollowup);

for (char *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 */
char *ptr2 = ptr+1;
while(IS_SPACE(*ptr2))
ptr2++;

if(ptr2 != ptr+1)
PL_strcpy(ptr+1, ptr2);
}

finalHeaders->SetRawHeader("Followup-To", nsDependentCString(n2), charset);
PR_Free (duppedFollowup);
}

nsCOMArray<msgIAddressObject> recipients;
finalHeaders->GetAddressingHeader("To", recipients);
hasDisclosedRecipient |= !recipients.IsEmpty();
Expand Down
2 changes: 1 addition & 1 deletion mailnews/compose/test/unit/test_messageHeaders.js
Expand Up @@ -268,7 +268,7 @@ function* testNewsgroups() {
yield richCreateMessage(fields, [], identity);
checkDraftHeaders({
// The identity should override the compose fields here.
"Newsgroups": "mozilla.test,mozilla.test.multimedia",
"Newsgroups": "mozilla.test, mozilla.test.multimedia",
"Followup-To": "mozilla.test",
"X-Mozilla-News-Host": "localhost",
});
Expand Down
29 changes: 29 additions & 0 deletions mailnews/mime/src/extraMimeParsers.jsm
@@ -0,0 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

function parseNewsgroups(headers) {
let ng = [];
for (let header of headers) {
ng = ng.concat(header.split(/\s*,\s*/));
}
return ng;
}

function emitNewsgroups(groups) {
// Don't encode the newsgroups names in RFC 2047...
if (groups.length == 1)
this.addText(groups[0], false);
else {
this.addText(groups[0], false);
for (let i = 1; i < groups.length; i++) {
this.addText(", ", true);
this.addText(groups[i], false);
}
}
}

jsmime.headerparser.addStructuredDecoder("Newsgroups", parseNewsgroups);
jsmime.headerparser.addStructuredDecoder("Followup-To", parseNewsgroups);
jsmime.headeremitter.addStructuredEncoder("Newsgroups", emitNewsgroups);
jsmime.headeremitter.addStructuredEncoder("Followup-To", emitNewsgroups);
1 change: 1 addition & 0 deletions mailnews/mime/src/moz.build
Expand Up @@ -81,6 +81,7 @@ EXTRA_COMPONENTS += [
]

EXTRA_JS_MODULES += [
'extraMimeParsers.jsm',
'jsmime.jsm',
'mimeParser.jsm'
]
Expand Down
1 change: 1 addition & 0 deletions mailnews/mime/src/msgMime.manifest
Expand Up @@ -6,3 +6,4 @@ contract @mozilla.org/messenger/mimeheaders;1 {d1258011-f391-44fd-992e-c6f4b461a
contract @mozilla.org/messenger/mimeconverter;1 {93f8c049-80ed-4dda-9000-94ad8daba44c}
contract @mozilla.org/messenger/headerparser;1 {96bd8769-2d0e-4440-963d-22b97fb3ba77}
contract @mozilla.org/messenger/structuredheaders;1 {c560806a-425f-4f0f-bf69-397c58c599a7}
category custom-mime-encoder A-extra resource:///modules/extraMimeParsers.jsm

0 comments on commit b7fac64

Please sign in to comment.