From a407a883e37bb4cc93b01952ca3fd2ef463fd4fd Mon Sep 17 00:00:00 2001 From: "bienvenu%nventure.com" Date: Tue, 12 Sep 2006 20:22:04 +0000 Subject: [PATCH] fix 352184 mark messages from trusted domains as non-junk, sr=mscott git-svn-id: svn://10.0.0.236/trunk@209894 18797224-902f-48f8-a5cc-f745e15eee43 --- .../mailnews/base/src/nsMsgContentPolicy.cpp | 42 +----------------- mozilla/mailnews/base/util/nsMsgDBFolder.cpp | 42 ++++++++++++++---- mozilla/mailnews/base/util/nsMsgUtils.cpp | 44 +++++++++++++++++++ mozilla/mailnews/base/util/nsMsgUtils.h | 2 + 4 files changed, 81 insertions(+), 49 deletions(-) diff --git a/mozilla/mailnews/base/src/nsMsgContentPolicy.cpp b/mozilla/mailnews/base/src/nsMsgContentPolicy.cpp index f4003b7870e..f6c84d3c51e 100644 --- a/mozilla/mailnews/base/src/nsMsgContentPolicy.cpp +++ b/mozilla/mailnews/base/src/nsMsgContentPolicy.cpp @@ -177,47 +177,7 @@ nsresult nsMsgContentPolicy::IsTrustedDomain(nsIURI * aContentLocation, PRBool * aContentLocation->GetHost(host); if (!mTrustedMailDomains.IsEmpty()) - { - const char *domain, *domainEnd, *end; - PRUint32 hostLen, domainLen; - - domain = mTrustedMailDomains.BeginReading(); - domainEnd = mTrustedMailDomains.EndReading(); - nsACString::const_iterator hostStart; - - host.BeginReading(hostStart); - hostLen = host.Length(); - - do { - // skip any whitespace - while (*domain == ' ' || *domain == '\t') - ++domain; - - // find end of this domain in the string - end = strchr(domain, ','); - if (!end) - end = domainEnd; - - // to see if the hostname is in the domain, check if the domain - // matches the end of the hostname. - domainLen = end - domain; - if (domainLen && hostLen >= domainLen) { - const char *hostTail = hostStart.get() + hostLen - domainLen; - if (PL_strncasecmp(domain, hostTail, domainLen) == 0) - { - // now, make sure either that the hostname is a direct match or - // that the hostname begins with a dot. - if (hostLen == domainLen || *hostTail == '.' || *(hostTail - 1) == '.') - { - *aTrustedDomain = PR_TRUE; - break; - } - } - } - - domain = end + 1; - } while (*end); - } + *aTrustedDomain = MsgHostDomainIsTrusted(host, mTrustedMailDomains); return rv; } diff --git a/mozilla/mailnews/base/util/nsMsgDBFolder.cpp b/mozilla/mailnews/base/util/nsMsgDBFolder.cpp index 0999589080a..73b0d5e15b3 100644 --- a/mozilla/mailnews/base/util/nsMsgDBFolder.cpp +++ b/mozilla/mailnews/base/util/nsMsgDBFolder.cpp @@ -1957,12 +1957,21 @@ nsMsgDBFolder::CallFilterPlugins(nsIMsgWindow *aMsgWindow, PRBool *aFiltersRun) whiteListDirectory = do_QueryInterface(resource, &rv); NS_ENSURE_SUCCESS(rv, rv); - headerParser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); } // if we can't get the db, we probably want to continue firing spam filters. } + nsXPIDLCString trustedMailDomains; + nsCOMPtr prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); + if (prefBranch) + prefBranch->GetCharPref("mail.trusteddomains", getter_Copies(trustedMailDomains)); + + if (whiteListDirectory || !trustedMailDomains.IsEmpty()) + { + headerParser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + } + // build up list of keys to classify // nsXPIDLCString uri; @@ -1977,7 +1986,28 @@ nsMsgDBFolder::CallFilterPlugins(nsIMsgWindow *aMsgWindow, PRBool *aFiltersRun) rv = mDatabase->GetMsgHdrForKey(msgKey, getter_AddRefs(msgHdr)); if (!NS_SUCCEEDED(rv)) continue; - + nsXPIDLCString author; + nsXPIDLCString authorEmailAddress; + if (whiteListDirectory || !trustedMailDomains.IsEmpty()) + { + msgHdr->GetAuthor(getter_Copies(author)); + rv = headerParser->ExtractHeaderAddressMailboxes(nsnull, author.get(), getter_Copies(authorEmailAddress)); + } + + if (!trustedMailDomains.IsEmpty()) + { + nsCAutoString domain; + PRInt32 atPos = authorEmailAddress.FindChar('@'); + if (atPos >= 0) + authorEmailAddress.Right(domain, authorEmailAddress.Length() - atPos - 1); + if (!domain.IsEmpty() && MsgHostDomainIsTrusted(domain, trustedMailDomains)) + { + // mark this msg as non-junk, because we whitelisted it. + mDatabase->SetStringProperty(msgKey, "junkscore", "0"); + mDatabase->SetStringProperty(msgKey, "junkscoreorigin", "plugin"); + continue; // skip this msg since it's in the white list + } + } msgHdr->GetStringProperty("junkscore", getter_Copies(junkScore)); if (!junkScore.IsEmpty()) // ignore already scored messages. continue; @@ -1987,12 +2017,8 @@ nsMsgDBFolder::CallFilterPlugins(nsIMsgWindow *aMsgWindow, PRBool *aFiltersRun) if (NS_SUCCEEDED(rv)) { PRBool cardExists = PR_FALSE; - nsXPIDLCString author; - nsXPIDLCString authorEmailAddress; - msgHdr->GetAuthor(getter_Copies(author)); - rv = headerParser->ExtractHeaderAddressMailboxes(nsnull, author.get(), getter_Copies(authorEmailAddress)); // don't want to abort the rest of the scoring. - if (NS_SUCCEEDED(rv)) + if (!authorEmailAddress.IsEmpty()) rv = whiteListDirectory->HasCardForEmailAddress(authorEmailAddress, &cardExists); if (NS_SUCCEEDED(rv) && cardExists) { diff --git a/mozilla/mailnews/base/util/nsMsgUtils.cpp b/mozilla/mailnews/base/util/nsMsgUtils.cpp index 60feee794f6..105e2000d32 100644 --- a/mozilla/mailnews/base/util/nsMsgUtils.cpp +++ b/mozilla/mailnews/base/util/nsMsgUtils.cpp @@ -1256,3 +1256,47 @@ PRBool MsgFindKeyword(const nsACString &keyword, nsACString &keywords, nsACStrin return PR_FALSE; } +PRBool MsgHostDomainIsTrusted(nsCString &host, nsCString &trustedMailDomains) +{ + const char *domain, *domainEnd, *end; + PRUint32 hostLen, domainLen; + PRBool domainIsTrusted = PR_FALSE; + + domain = trustedMailDomains.BeginReading(); + domainEnd = trustedMailDomains.EndReading(); + nsACString::const_iterator hostStart; + + host.BeginReading(hostStart); + hostLen = host.Length(); + + do { + // skip any whitespace + while (*domain == ' ' || *domain == '\t') + ++domain; + + // find end of this domain in the string + end = strchr(domain, ','); + if (!end) + end = domainEnd; + + // to see if the hostname is in the domain, check if the domain + // matches the end of the hostname. + domainLen = end - domain; + if (domainLen && hostLen >= domainLen) { + const char *hostTail = hostStart.get() + hostLen - domainLen; + if (PL_strncasecmp(domain, hostTail, domainLen) == 0) + { + // now, make sure either that the hostname is a direct match or + // that the hostname begins with a dot. + if (hostLen == domainLen || *hostTail == '.' || *(hostTail - 1) == '.') + { + domainIsTrusted = PR_TRUE; + break; + } + } + } + + domain = end + 1; + } while (*end); + return domainIsTrusted; +} diff --git a/mozilla/mailnews/base/util/nsMsgUtils.h b/mozilla/mailnews/base/util/nsMsgUtils.h index ea85b645635..1488d8ee636 100644 --- a/mozilla/mailnews/base/util/nsMsgUtils.h +++ b/mozilla/mailnews/base/util/nsMsgUtils.h @@ -164,5 +164,7 @@ NS_MSG_BASE PRBool MsgFindKeyword(const nsACString &keyword, nsACString &keyword nsACString::const_iterator &start, nsACString::const_iterator &end); +NS_MSG_BASE PRBool MsgHostDomainIsTrusted(nsCString &host, nsCString &trustedMailDomains); + #endif