Bug 842632, part 3g: Use the C++ API elsewhere, r=Neil.

SeaMonkey is still a CLOSED TREE though.

--HG--
extra : amend_source : c3eda6ca3f52b831446c7e8804296a92265d5d63
This commit is contained in:
Joshua Cranmer 2013-12-11 13:21:02 -06:00
Родитель 0a02fb2eff
Коммит 4319a0b8d0
5 изменённых файлов: 79 добавлений и 178 удалений

Просмотреть файл

@ -8,7 +8,6 @@
#include "msgCore.h"
#include "nsIMsgCompFields.h"
#include "nsIMsgHeaderParser.h"
#include "nsIMsgIdentity.h"
#include "nsISMimeCert.h"
#include "nsIX509CertDB.h"
@ -21,10 +20,11 @@
#include "nsAlgorithm.h"
#include "mozilla/Services.h"
#include "mozilla/mailnews/MimeEncoder.h"
#include "mozilla/mailnews/MimeHeaderParser.h"
#include "nsIMimeConverter.h"
#include <algorithm>
using mozilla::mailnews::MimeEncoder;
using namespace mozilla::mailnews;
#define MK_MIME_ERROR_WRITING_FILE -1
@ -586,15 +586,6 @@ nsresult nsMsgComposeSecure::MimeFinishMultipartSigned (bool aOuter, nsIMsgSendR
NS_ConvertUTF16toUTF8 sig_content_desc_utf8(mime_smime_sig_content_desc);
nsCOMPtr<nsIMimeConverter> mimeConverter =
do_GetService(NS_MIME_CONVERTER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCString encodedContentDescription;
mimeConverter->EncodeMimePartIIStr_UTF8(sig_content_desc_utf8, false, "UTF-8",
sizeof("Content-Description: "),
nsIMimeConverter::MIME_ENCODED_WORD_SIZE,
getter_Copies(encodedContentDescription));
/* Compute the hash...
*/
@ -620,8 +611,8 @@ nsresult nsMsgComposeSecure::MimeFinishMultipartSigned (bool aOuter, nsIMsgSendR
"Content-Description: %s" CRLF
CRLF,
mMultipartSignedBoundary,
encodedContentDescription.get());
sig_content_desc_utf8.get());
if (!header) {
rv = NS_ERROR_OUT_OF_MEMORY;
goto FAIL;
@ -782,14 +773,8 @@ nsresult nsMsgComposeSecure::MimeCryptoHackCerts(const char *aRecipients,
bool aEncrypt,
bool aSign)
{
char *mailbox_list = 0;
nsCString all_mailboxes, mailboxes;
const char *mailbox = 0;
uint32_t count = 0;
nsCOMPtr<nsIX509CertDB> certdb = do_GetService(NS_X509CERTDB_CONTRACTID);
nsresult res;
nsCOMPtr<nsIMsgHeaderParser> pHeader = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID, &res);
NS_ENSURE_SUCCESS(res,res);
mCerts = do_CreateInstance(NS_ARRAY_CONTRACTID, &res);
if (NS_FAILED(res)) {
@ -803,24 +788,14 @@ nsresult nsMsgComposeSecure::MimeCryptoHackCerts(const char *aRecipients,
// must have both the signing and encryption certs to sign
if ((mSelfSigningCert == nullptr) && aSign) {
SetError(sendReport, NS_LITERAL_STRING("NoSenderSigningCert").get());
res = NS_ERROR_FAILURE;
goto FAIL;
return NS_ERROR_FAILURE;
}
if ((mSelfEncryptionCert == nullptr) && aEncrypt) {
SetError(sendReport, NS_LITERAL_STRING("NoSenderEncryptionCert").get());
res = NS_ERROR_FAILURE;
goto FAIL;
return NS_ERROR_FAILURE;
}
pHeader->ExtractHeaderAddressMailboxes(nsDependentCString(aRecipients),
all_mailboxes);
pHeader->RemoveDuplicateAddresses(all_mailboxes, EmptyCString(), mailboxes);
pHeader->ParseHeaderAddresses(mailboxes.get(), 0, &mailbox_list, &count);
// XXX This is not a valid use of nsresult
if (count < 0) return static_cast<nsresult>(count);
if (aEncrypt && mSelfEncryptionCert) {
// Make sure self's configured cert is prepared for being used
@ -834,13 +809,16 @@ nsresult nsMsgComposeSecure::MimeCryptoHackCerts(const char *aRecipients,
/* If the message is to be encrypted, then get the recipient certs */
if (aEncrypt) {
mailbox = mailbox_list;
nsTArray<nsCString> mailboxes;
ExtractEmails(EncodedHeader(nsDependentCString(aRecipients)),
UTF16ArrayAdapter<>(mailboxes));
uint32_t count = mailboxes.Length();
bool already_added_self_cert = false;
for (; count > 0; count--) {
for (uint32_t i = 0; i < count; i++) {
nsCString mailbox_lowercase;
ToLowerCase(nsDependentCString(mailbox), mailbox_lowercase);
ToLowerCase(mailboxes[i], mailbox_lowercase);
nsCOMPtr<nsIX509Cert> cert;
res = certdb->FindCertByEmailAddress(nullptr, mailbox_lowercase.get(),
getter_AddRefs(cert));
@ -849,9 +827,9 @@ nsresult nsMsgComposeSecure::MimeCryptoHackCerts(const char *aRecipients,
// Here I assume that mailbox is ascii rather than utf8.
SetErrorWithParam(sendReport,
NS_LITERAL_STRING("MissingRecipientEncryptionCert").get(),
mailbox);
mailboxes[i].get());
goto FAIL;
return res;
}
/* #### see if recipient requests `signedData'.
@ -868,19 +846,12 @@ nsresult nsMsgComposeSecure::MimeCryptoHackCerts(const char *aRecipients,
}
mCerts->AppendElement(cert, false);
// To understand this loop, especially the "+= strlen +1", look at the documentation
// of ParseHeaderAddresses. Basically, it returns a list of zero terminated strings.
mailbox += strlen(mailbox) + 1;
}
if (!already_added_self_cert) {
mCerts->AppendElement(mSelfEncryptionCert, false);
}
}
FAIL:
if (mailbox_list) {
nsMemory::Free(mailbox_list);
}
return res;
}

Просмотреть файл

@ -3,18 +3,20 @@
* 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/. */
#include "mozilla/mailnews/MimeHeaderParser.h"
#include "nspr.h"
#include "nsSMimeJSHelper.h"
#include "nsCOMPtr.h"
#include "nsMemory.h"
#include "nsStringGlue.h"
#include "nsIMsgHeaderParser.h"
#include "nsIX509CertDB.h"
#include "nsIX509CertValidity.h"
#include "nsIServiceManager.h"
#include "nsServiceManagerUtils.h"
#include "nsCRTGlue.h"
using namespace mozilla::mailnews;
NS_IMPL_ISUPPORTS1(nsSMimeJSHelper, nsISMimeJSHelper)
nsSMimeJSHelper::nsSMimeJSHelper()
@ -47,15 +49,11 @@ NS_IMETHODIMP nsSMimeJSHelper::GetRecipientCertsInfo(
NS_ENSURE_ARG_POINTER(compFields);
uint32_t mailbox_count;
char *mailbox_list;
nsTArray<nsCString> mailboxes;
nsresult rv = getMailboxList(compFields, mailboxes);
NS_ENSURE_SUCCESS(rv, rv);
nsresult rv = getMailboxList(compFields, &mailbox_count, &mailbox_list);
if (NS_FAILED(rv))
return rv;
if (!mailbox_list)
return NS_ERROR_FAILURE;
uint32_t mailbox_count = mailboxes.Length();
nsCOMPtr<nsIX509CertDB> certdb = do_GetService(NS_X509CERTDB_CONTRACTID);
@ -91,13 +89,9 @@ NS_IMETHODIMP nsSMimeJSHelper::GetRecipientCertsInfo(
bool found_blocker = false;
bool memory_failure = false;
const char *walk = mailbox_list;
// To understand this loop, especially the "+= strlen +1", look at the documentation
// of ParseHeaderAddresses. Basically, it returns a list of zero terminated strings.
for (uint32_t i = 0;
i < mailbox_count;
++i, ++iEA, ++iCV, ++iCII, ++iCEI, ++iCert, walk += strlen(walk) + 1)
++i, ++iEA, ++iCV, ++iCII, ++iCEI, ++iCert)
{
*iCert = nullptr;
*iCV = 0;
@ -109,8 +103,8 @@ NS_IMETHODIMP nsSMimeJSHelper::GetRecipientCertsInfo(
continue;
}
nsDependentCString email(walk);
*iEA = ToNewUnicode(NS_ConvertUTF8toUTF16(walk));
nsCString &email = mailboxes[i];
*iEA = ToNewUnicode(NS_ConvertUTF8toUTF16(email));
if (!*iEA) {
memory_failure = true;
continue;
@ -179,10 +173,6 @@ NS_IMETHODIMP nsSMimeJSHelper::GetRecipientCertsInfo(
}
}
}
if (mailbox_list) {
nsMemory::Free(mailbox_list);
}
return rv;
}
@ -198,23 +188,16 @@ NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(
NS_ENSURE_ARG_POINTER(compFields);
uint32_t mailbox_count;
char *mailbox_list;
nsTArray<nsCString> mailboxes;
nsresult rv = getMailboxList(compFields, mailboxes);
NS_ENSURE_SUCCESS(rv, rv);
nsresult rv = getMailboxList(compFields, &mailbox_count, &mailbox_list);
if (NS_FAILED(rv))
return rv;
if (!mailbox_list)
return NS_ERROR_FAILURE;
uint32_t mailbox_count = mailboxes.Length();
if (!mailbox_count)
{
*count = 0;
*emailAddresses = nullptr;
if (mailbox_list) {
nsMemory::Free(mailbox_list);
}
return NS_OK;
}
@ -224,9 +207,6 @@ NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(
bool *haveCert = new bool[mailbox_count];
if (!haveCert)
{
if (mailbox_list) {
nsMemory::Free(mailbox_list);
}
return NS_ERROR_OUT_OF_MEMORY;
}
@ -234,19 +214,12 @@ NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(
if (mailbox_count)
{
const char *walk = mailbox_list;
// To understand this loop, especially the "+= strlen +1", look at the documentation
// of ParseHeaderAddresses. Basically, it returns a list of zero terminated strings.
for (uint32_t i = 0;
i < mailbox_count;
++i, walk += strlen(walk) + 1)
for (uint32_t i = 0; i < mailbox_count; ++i)
{
haveCert[i] = false;
nsDependentCString email(walk);
nsCString email_lowercase;
ToLowerCase(email, email_lowercase);
ToLowerCase(mailboxes[i], email_lowercase);
nsCOMPtr<nsIX509Cert> cert;
if (NS_SUCCEEDED(certdb->FindCertByEmailAddress(nullptr,
@ -270,15 +243,10 @@ NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(
else
{
PRUnichar **iEA = outEA;
const char *walk = mailbox_list;
bool memory_failure = false;
// To understand this loop, especially the "+= strlen +1", look at the documentation
// of ParseHeaderAddresses. Basically, it returns a list of zero terminated strings.
for (uint32_t i = 0;
i < mailbox_count;
++i, walk += strlen(walk) + 1)
for (uint32_t i = 0; i < mailbox_count; ++i)
{
if (!haveCert[i])
{
@ -286,7 +254,7 @@ NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(
*iEA = nullptr;
}
else {
*iEA = ToNewUnicode(NS_ConvertUTF8toUTF16(walk));
*iEA = ToNewUnicode(NS_ConvertUTF8toUTF16(mailboxes[i]));
if (!*iEA) {
memory_failure = true;
}
@ -310,25 +278,16 @@ NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(
}
delete [] haveCert;
if (mailbox_list) {
nsMemory::Free(mailbox_list);
}
return rv;
}
nsresult nsSMimeJSHelper::getMailboxList(nsIMsgCompFields *compFields, uint32_t *mailbox_count, char **mailbox_list)
nsresult nsSMimeJSHelper::getMailboxList(nsIMsgCompFields *compFields,
nsTArray<nsCString> &mailboxes)
{
NS_ENSURE_ARG(mailbox_count);
NS_ENSURE_ARG(mailbox_list);
if (!compFields)
return NS_ERROR_INVALID_ARG;
nsresult res;
nsCOMPtr<nsIMsgHeaderParser> parser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID, &res);
if (NS_FAILED(res))
return res;
nsString to, cc, bcc, ng;
res = compFields->GetTo(to);
@ -347,9 +306,6 @@ nsresult nsSMimeJSHelper::getMailboxList(nsIMsgCompFields *compFields, uint32_t
if (NS_FAILED(res))
return res;
*mailbox_list = nullptr;
*mailbox_count = 0;
{
nsCString all_recipients;
@ -371,13 +327,8 @@ nsresult nsSMimeJSHelper::getMailboxList(nsIMsgCompFields *compFields, uint32_t
if (!ng.IsEmpty())
all_recipients.Append(NS_ConvertUTF16toUTF8(ng));
nsCString unique_mailboxes;
nsCString all_mailboxes;
parser->ExtractHeaderAddressMailboxes(all_recipients, all_mailboxes);
parser->RemoveDuplicateAddresses(all_mailboxes, EmptyCString(),
unique_mailboxes);
parser->ParseHeaderAddresses(unique_mailboxes.get(), 0, mailbox_list,
mailbox_count);
ExtractEmails(EncodedHeader(all_recipients),
UTF16ArrayAdapter<>(mailboxes));
}
return NS_OK;

Просмотреть файл

@ -19,7 +19,8 @@ public:
virtual ~nsSMimeJSHelper();
private:
nsresult getMailboxList(nsIMsgCompFields *compFields, uint32_t *mailbox_count, char **mailbox_list);
nsresult getMailboxList(nsIMsgCompFields *compFields,
nsTArray<nsCString> &mailboxes);
};
#endif

Просмотреть файл

@ -29,13 +29,15 @@
#include "nsIMsgImapMailFolder.h"
#include <time.h>
#include "nsIInputStream.h"
#include "nsIMsgHeaderParser.h"
#include "nsILineInputStream.h"
#include "nsISeekableStream.h"
#include "nsIFile.h"
#include "nsIFileStreams.h"
#include "nsNetCID.h"
#include "nsMsgMessageFlags.h"
#include "mozilla/mailnews/MimeHeaderParser.h"
using namespace mozilla::mailnews;
PRLogModuleInfo *MAPI;
@ -321,9 +323,9 @@ protected:
char *ConvertDateToMapiFormat (time_t);
char *ConvertBodyToMapiFormat (nsIMsgDBHdr *hdr);
void ConvertRecipientsToMapiFormat (nsIMsgHeaderParser *parser,
const char *ourRecips, lpnsMapiRecipDesc mapiRecips,
int mapiRecipClass);
void ConvertRecipientsToMapiFormat(const ParsedHeader &ourRecips,
lpnsMapiRecipDesc mapiRecips,
int mapiRecipClass);
nsCOMPtr <nsIMsgFolder> m_folder;
nsCOMPtr <nsIMsgDatabase> m_db;
@ -595,34 +597,33 @@ lpnsMapiMessage MsgMapiListContext::GetMessage (nsMsgKey key, unsigned long flFl
if (ourFlags & (nsMsgMessageFlags::MDNReportNeeded | nsMsgMessageFlags::MDNReportSent))
message->flFlags |= MAPI_RECEIPT_REQUESTED;
nsCOMPtr<nsIMsgHeaderParser> parser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID);
if (!parser)
return nullptr;
// Pull out the author/originator info
message->lpOriginator = (lpnsMapiRecipDesc) CoTaskMemAlloc (sizeof(nsMapiRecipDesc));
memset(message->lpOriginator, 0, sizeof(nsMapiRecipDesc));
if (message->lpOriginator)
{
msgHdr->GetAuthor (getter_Copies(author));
ConvertRecipientsToMapiFormat (parser, author.get(), message->lpOriginator, MAPI_ORIG);
ConvertRecipientsToMapiFormat(EncodedHeader(author),
message->lpOriginator, MAPI_ORIG);
}
// Pull out the To/CC info
nsCString recipients, ccList;
msgHdr->GetRecipients(getter_Copies(recipients));
msgHdr->GetCcList(getter_Copies(ccList));
uint32_t numToRecips;
uint32_t numCCRecips;
parser->ParseHeaderAddresses(recipients.get(), nullptr, nullptr,
&numToRecips);
parser->ParseHeaderAddresses(ccList.get(), nullptr, nullptr, &numCCRecips);
ParsedHeader parsedToRecips = EncodedHeader(recipients);
ParsedHeader parsedCCRecips = EncodedHeader(ccList);
uint32_t numToRecips = parsedToRecips.mCount;
uint32_t numCCRecips = parsedCCRecips.mCount;
message->lpRecips = (lpnsMapiRecipDesc) CoTaskMemAlloc ((numToRecips + numCCRecips) * sizeof(MapiRecipDesc));
memset(message->lpRecips, 0, (numToRecips + numCCRecips) * sizeof(MapiRecipDesc));
if (message->lpRecips)
{
ConvertRecipientsToMapiFormat (parser, recipients.get(), message->lpRecips, MAPI_TO);
ConvertRecipientsToMapiFormat (parser, ccList.get(), &message->lpRecips[numToRecips], MAPI_CC);
ConvertRecipientsToMapiFormat(parsedToRecips, message->lpRecips,
MAPI_TO);
ConvertRecipientsToMapiFormat(parsedCCRecips,
&message->lpRecips[numToRecips], MAPI_CC);
}
PR_LOG(MAPI, PR_LOG_DEBUG, ("MsgMapiListContext::GetMessage flags=%x subject %s date %s sender %s\n",
@ -656,51 +657,31 @@ char *MsgMapiListContext::ConvertDateToMapiFormat (time_t ourTime)
}
void MsgMapiListContext::ConvertRecipientsToMapiFormat (nsIMsgHeaderParser *parser, const char *recipients, lpnsMapiRecipDesc mapiRecips,
int mapiRecipClass)
void MsgMapiListContext::ConvertRecipientsToMapiFormat(
const ParsedHeader &recipients, lpnsMapiRecipDesc mapiRecips,
int mapiRecipClass)
{
char *names = nullptr;
char *addresses = nullptr;
nsTArray<nsCString> names, addresses;
ExtractAllAddresses(recipients, UTF16ArrayAdapter<>(names),
UTF16ArrayAdapter<>(addresses));
if (!parser)
return ;
uint32_t numAddresses = 0;
parser->ParseHeaderAddresses(recipients, &names, &addresses, &numAddresses);
if (numAddresses > 0)
uint32_t numAddresses = names.Length();
for (int i = 0; i < numAddresses; i++)
{
char *walkNames = names;
char *walkAddresses = addresses;
for (int i = 0; i < numAddresses; i++)
if (!names[i].IsEmpty())
{
if (walkNames)
{
if (*walkNames)
{
mapiRecips[i].lpszName = (char *) CoTaskMemAlloc(strlen(walkNames) + 1);
if (mapiRecips[i].lpszName )
strcpy((char *) mapiRecips[i].lpszName, walkNames);
}
walkNames += strlen (walkNames) + 1;
}
if (walkAddresses)
{
if (*walkAddresses)
{
mapiRecips[i].lpszAddress = (char *) CoTaskMemAlloc(strlen(walkAddresses) + 1);
if (mapiRecips[i].lpszAddress)
strcpy((char *) mapiRecips[i].lpszAddress, walkAddresses);
}
walkAddresses += strlen (walkAddresses) + 1;
}
mapiRecips[i].ulRecipClass = mapiRecipClass;
mapiRecips[i].lpszName = (char *) CoTaskMemAlloc(names[i].Length() + 1);
if (mapiRecips[i].lpszName)
strcpy((char *)mapiRecips[i].lpszName, names[i].get());
}
if (!addresses[i].IsEmpty())
{
mapiRecips[i].lpszName = (char *) CoTaskMemAlloc(addresses[i].Length() + 1);
if (mapiRecips[i].lpszName)
strcpy((char *)mapiRecips[i].lpszName, addresses[i].get());
}
mapiRecips[i].ulRecipClass = mapiRecipClass;
}
PR_Free(names);
PR_Free(addresses);
}

Просмотреть файл

@ -31,13 +31,13 @@
#include "prerror.h"
#include "nsStringGlue.h"
#include "mozilla/Services.h"
#include "mozilla/mailnews/MimeHeaderParser.h"
#include "prprf.h"
#include <algorithm>
/* include event sink interfaces for news */
#include "nsIMsgHeaderParser.h"
#include "nsIMsgSearchSession.h"
#include "nsIMsgSearchAdapter.h"
#include "nsIMsgStatusFeedback.h"
@ -92,6 +92,8 @@
#define RATE_STR_BUF_LEN 32
#define UPDATE_THRESHHOLD 25600 /* only update every 25 KB */
using namespace mozilla::mailnews;
// NNTP extensions are supported yet
// until the extension code is ported,
// we'll skip right to the first nntp command
@ -3540,19 +3542,14 @@ void nsNNTPProtocol::CheckIfAuthor(nsIMsgIdentity *aIdentity, const nsCString &a
return;
PR_LOG(NNTP,PR_LOG_ALWAYS,("from = %s", from.get()));
nsCOMPtr<nsIMsgHeaderParser> parser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID, &rv);
if (NS_FAILED(rv))
return;
nsCString us;
nsCString them;
nsresult rv1 = parser->ExtractHeaderAddressMailboxes(from, us);
nsresult rv2 = parser->ExtractHeaderAddressMailboxes(aOldFrom, them);
ExtractEmail(EncodedHeader(from), us);
ExtractEmail(EncodedHeader(aOldFrom), them);
PR_LOG(NNTP,PR_LOG_ALWAYS,("us = %s, them = %s", us.get(), them.get()));
if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2) &&
us.Equals(them, nsCaseInsensitiveCStringComparator()))
if (us.Equals(them, nsCaseInsensitiveCStringComparator()))
aFrom = from;
}