From d62697046552b48d674df7f7439383d2e01f9b6a Mon Sep 17 00:00:00 2001 From: "bienvenu%nventure.com" Date: Tue, 13 Apr 2004 18:16:35 +0000 Subject: [PATCH] 224318 backend support for automatic server spam filter filters sr=mscott --- mailnews/base/public/nsIMsgIncomingServer.idl | 8 +- mailnews/base/public/nsISpamSettings.idl | 8 ++ .../base/search/resources/content/Makefile.in | 24 ++++ mailnews/base/search/src/nsMsgFilterList.cpp | 8 +- mailnews/base/src/nsSpamSettings.cpp | 26 +++- mailnews/base/src/nsSpamSettings.h | 6 +- mailnews/base/util/nsMsgIdentity.cpp | 2 +- mailnews/base/util/nsMsgIncomingServer.cpp | 130 +++++++++++++++++- mailnews/base/util/nsMsgIncomingServer.h | 3 + mailnews/imap/src/nsImapMailFolder.cpp | 2 +- mailnews/local/src/nsParseMailbox.cpp | 2 +- mailnews/makefiles | 1 + 12 files changed, 199 insertions(+), 21 deletions(-) diff --git a/mailnews/base/public/nsIMsgIncomingServer.idl b/mailnews/base/public/nsIMsgIncomingServer.idl index 7c125383c7b..8b9ae044915 100644 --- a/mailnews/base/public/nsIMsgIncomingServer.idl +++ b/mailnews/base/public/nsIMsgIncomingServer.idl @@ -359,16 +359,16 @@ interface nsIMsgIncomingServer : nsISupports { readonly attribute boolean passwordPromptRequired; /** + * This configures both the MDN filter, and the server-side + * spam filter filters, if needed. + * * If we have set up to filter return receipts into * our Sent folder, this utility method creates * a filter to do that, and adds it to our filterList * if it doesn't exist. If it does, it will enable it. * - * If the user changes their prefs, to leave the - * return receipt in the inbox, this will disable - * the existing return receipts filter, if it exists. */ - void configureTemporaryReturnReceiptsFilter(in nsIMsgFilterList filterList); + void configureTemporaryFilters(in nsIMsgFilterList filterList); /** * If Sent folder pref is changed we need to clear the temporary diff --git a/mailnews/base/public/nsISpamSettings.idl b/mailnews/base/public/nsISpamSettings.idl index 14a0e8199dc..d1b8513e820 100644 --- a/mailnews/base/public/nsISpamSettings.idl +++ b/mailnews/base/public/nsISpamSettings.idl @@ -89,6 +89,14 @@ interface nsISpamSettings: nsISupports { const long MANUAL_MARK_MODE_MOVE = 0; const long MANUAL_MARK_MODE_DELETE = 1; + /** + * integrate with server-side spam detection programs + */ + attribute ACString serverFilterName; + const long TRUST_POSITIVES = 1; + const long TRUST_NEGATIVES = 2; + attribute long serverFilterTrustFlags; + // for logging attribute boolean loggingEnabled; attribute nsIOutputStream logStream; diff --git a/mailnews/base/search/resources/content/Makefile.in b/mailnews/base/search/resources/content/Makefile.in index a70700653a4..0329ca78856 100644 --- a/mailnews/base/search/resources/content/Makefile.in +++ b/mailnews/base/search/resources/content/Makefile.in @@ -26,5 +26,29 @@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk +PACKAGE_FILE = searchContent.pkg + +EXPORT_DIR = $(DIST)/bin/defaults/messenger +EXPORT_L10N_DIR = $(EXPORT_DIR)/US + +EXPORT_RESOURCE_FILES = \ + Habeas.sfd \ + SpamAssassin.sfd \ + SpamCatcher.sfd \ + SpamPal.sfd \ + $(NULL) + include $(topsrcdir)/config/rules.mk +GARBAGE += $(addprefix $(EXPORT_DIR)/, $(EXPORT_RESOURCE_FILES)) \ + $(addprefix $(EXPORT_L10N_DIR)/, $(EXPORT_RESOURCE_FILES)) + +libs:: $(EXPORT_RESOURCE_FILES) + $(INSTALL) $^ $(EXPORT_DIR) + $(INSTALL) $^ $(EXPORT_L10N_DIR) + + + +install:: $(EXPORT_RESOURCE_FILES) + $(SYSINSTALL) $(IFLAGS1) $^ $(DESTDIR)$(mozappdir)/defaults/messenger + $(SYSINSTALL) $(IFLAGS1) $^ $(DESTDIR)$(mozappdir)/defaults/messenger/US diff --git a/mailnews/base/search/src/nsMsgFilterList.cpp b/mailnews/base/search/src/nsMsgFilterList.cpp index f937b75b2e9..1dceaa915d9 100644 --- a/mailnews/base/search/src/nsMsgFilterList.cpp +++ b/mailnews/base/search/src/nsMsgFilterList.cpp @@ -952,10 +952,12 @@ nsMsgFilterList::RemoveFilter(nsIMsgFilter *aFilter) return m_filters->RemoveElement(NS_STATIC_CAST(nsISupports*, aFilter)); } -nsresult nsMsgFilterList::InsertFilterAt(PRUint32 filterIndex, nsIMsgFilter *filter) +nsresult nsMsgFilterList::InsertFilterAt(PRUint32 filterIndex, nsIMsgFilter *aFilter) { - m_filters->InsertElementAt(filter, filterIndex); - return NS_OK; + nsMsgFilter *filter = NS_STATIC_CAST(nsMsgFilter *, aFilter); + filter->SetFilterList(this); + m_filters->InsertElementAt(aFilter, filterIndex); + return NS_OK; } // Attempt to move the filter at index filterIndex in the specified direction. diff --git a/mailnews/base/src/nsSpamSettings.cpp b/mailnews/base/src/nsSpamSettings.cpp index 7481b436e55..0b3c445ea19 100644 --- a/mailnews/base/src/nsSpamSettings.cpp +++ b/mailnews/base/src/nsSpamSettings.cpp @@ -22,6 +22,7 @@ * Contributor(s): * Seth Spitzer * Dan Mosedale + * David Bienvenu * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -60,6 +61,9 @@ nsSpamSettings::nsSpamSettings() mMoveTargetMode = nsISpamSettings::MOVE_TARGET_MODE_ACCOUNT; mPurge = PR_FALSE; mPurgeInterval = 14; // 14 days + + mServerFilterTrustFlags = 0; + mUseWhiteList = PR_FALSE; mLoggingEnabled = PR_FALSE; mManualMark = PR_FALSE; @@ -373,10 +377,11 @@ NS_IMETHODIMP nsSpamSettings::Clone(nsISpamSettings *aSpamSettings) NS_ENSURE_SUCCESS(rv,rv); mWhiteListAbURI = whiteListAbURI; - PRBool loggingEnabled; - rv = aSpamSettings->GetLoggingEnabled(&loggingEnabled); - NS_ENSURE_SUCCESS(rv,rv); - mLoggingEnabled = loggingEnabled; + rv = aSpamSettings->GetLoggingEnabled(&mLoggingEnabled); + + aSpamSettings->GetServerFilterName(mServerFilterName); + aSpamSettings->GetServerFilterTrustFlags(&mServerFilterTrustFlags); + return rv; } @@ -438,6 +443,19 @@ NS_IMETHODIMP nsSpamSettings::GetSpamFolderURI(char **aSpamFolderURI) return rv; } +NS_IMETHODIMP nsSpamSettings::GetServerFilterName(nsACString &aFilterName) +{ + aFilterName = mServerFilterName; + return NS_OK; +} + +NS_IMETHODIMP nsSpamSettings::SetServerFilterName(const nsACString &aFilterName) +{ + mServerFilterName = aFilterName; + return NS_OK; +} + +NS_IMPL_GETSET(nsSpamSettings, ServerFilterTrustFlags, PRBool, mServerFilterTrustFlags); #define LOG_ENTRY_START_TAG "

\n" #define LOG_ENTRY_START_TAG_LEN (strlen(LOG_ENTRY_START_TAG)) diff --git a/mailnews/base/src/nsSpamSettings.h b/mailnews/base/src/nsSpamSettings.h index 8d7dd677ed6..618d809873f 100644 --- a/mailnews/base/src/nsSpamSettings.h +++ b/mailnews/base/src/nsSpamSettings.h @@ -22,6 +22,7 @@ * Contributor(s): * Seth Spitzer * Dan Mosedale + * David Bienvenu * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -76,7 +77,10 @@ private: nsCString mActionTargetFolder; nsCString mWhiteListAbURI; nsCString mLogURL; - + + nsCString mServerFilterName; + PRInt32 mServerFilterTrustFlags; + nsresult GetLogFileSpec(nsIFileSpec **aFileSpec); nsresult TruncateLog(); }; diff --git a/mailnews/base/util/nsMsgIdentity.cpp b/mailnews/base/util/nsMsgIdentity.cpp index ae8781f8ef9..ced69c17da4 100644 --- a/mailnews/base/util/nsMsgIdentity.cpp +++ b/mailnews/base/util/nsMsgIdentity.cpp @@ -636,7 +636,7 @@ nsMsgIdentity::setFolderPref(const char *prefname, const char *value) if (nsCRT::strcmp(prefname, "fcc_folder") == 0) { // Clear the temporary return receipt filter so that the new filter - // rule can be recreated (by ConfigureTemporaryReturnReceiptsFilter()). + // rule can be recreated (by ConfigureTemporaryFilters()). nsCOMPtr accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv,rv); diff --git a/mailnews/base/util/nsMsgIncomingServer.cpp b/mailnews/base/util/nsMsgIncomingServer.cpp index 34845d39ff1..82d7f589a5d 100644 --- a/mailnews/base/util/nsMsgIncomingServer.cpp +++ b/mailnews/base/util/nsMsgIncomingServer.cpp @@ -21,6 +21,7 @@ * * Contributor(s): * Pierre Phaneuf + * David Bienvenu * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -58,7 +59,7 @@ #include "nsIMsgWindow.h" #include "nsIMsgFilterService.h" #include "nsIMsgProtocolInfo.h" - +#include "nsIMsgMailSession.h" #include "nsIPrefService.h" #include "nsIDocShell.h" #include "nsIWebShell.h" @@ -79,6 +80,7 @@ #include "nsIMsgMdnGenerator.h" #include "nsMsgFolderFlags.h" #include "nsMsgUtils.h" +#include "nsAppDirectoryServiceDefs.h" #define PORT_NOT_SET -1 @@ -1815,10 +1817,111 @@ nsMsgIncomingServer::GetPasswordPromptRequired(PRBool *aPasswordIsRequired) return rv; } -NS_IMETHODIMP +NS_IMETHODIMP nsMsgIncomingServer::ConfigureTemporaryFilters(nsIMsgFilterList *aFilterList) +{ + nsresult rv = ConfigureTemporaryReturnReceiptsFilter(aFilterList); + NS_ENSURE_SUCCESS(rv, rv); + return ConfigureTemporaryServerSpamFilters(aFilterList); +} + +nsresult +nsMsgIncomingServer::ConfigureTemporaryServerSpamFilters(nsIMsgFilterList *filterList) +{ + nsCOMPtr spamSettings; + nsresult rv = GetSpamSettings(getter_AddRefs(spamSettings)); + NS_ENSURE_SUCCESS(rv, rv); + // For performance reasons, we'll handle clearing of filters if the user turns + // off the server-side filters from the junk mail controls, in the junk mail controls. + nsCAutoString serverFilterName; + spamSettings->GetServerFilterName(serverFilterName); + if (serverFilterName.IsEmpty()) + return NS_OK; + PRInt32 serverFilterTrustFlags = 0; + (void) spamSettings->GetServerFilterTrustFlags(&serverFilterTrustFlags); + if (!serverFilterTrustFlags) + return NS_OK; + // check if filters have been setup already. + nsAutoString yesFilterName, noFilterName; + yesFilterName.AppendWithConversion(serverFilterName); + yesFilterName.AppendWithConversion("Yes"); + + noFilterName.AppendWithConversion(serverFilterName); + noFilterName.AppendWithConversion("No"); + + nsCOMPtr newFilter; + (void) filterList->GetFilterNamed(yesFilterName.get(), + getter_AddRefs(newFilter)); + + if (!newFilter) + (void) filterList->GetFilterNamed(noFilterName.get(), + getter_AddRefs(newFilter)); + if (newFilter) + return NS_OK; + + nsCAutoString serverFilterFileName(serverFilterName); + serverFilterFileName.Append(".sfd"); + nsCOMPtr file; + rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = file->AppendNative(serverFilterFileName); + + // if the file doesn't exist, we should try to get it from the defaults directory and copy it over + PRBool exists = PR_FALSE; + file->Exists(&exists); + if (!exists) + { + nsCOMPtr mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr defaultServerFilterFile; + rv = mailSession->GetDataFilesDir("messenger", getter_AddRefs(defaultServerFilterFile)); + rv = defaultServerFilterFile->AppendNative(serverFilterFileName); + + nsCOMPtr defaultServerFilterSpec; + rv = NS_NewFileSpecFromIFile(defaultServerFilterFile, getter_AddRefs(defaultServerFilterSpec)); + + // get the profile directory + rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(defaultServerFilterFile)); + + // convert to spec + nsCOMPtr profileDirSpec; + rv = NS_NewFileSpecFromIFile(defaultServerFilterFile, getter_AddRefs(profileDirSpec)); + // now copy the file over to the profile directory + defaultServerFilterSpec->CopyToDir(profileDirSpec); + } + + nsCOMPtr serverFilterSpec; + rv = NS_NewFileSpecFromIFile(file, getter_AddRefs(serverFilterSpec)); + if (NS_FAILED(rv)) return rv; + + nsCOMPtr filterService = do_GetService(NS_MSGFILTERSERVICE_CONTRACTID, &rv); + nsCOMPtr serverFilterList; + + rv = filterService->OpenFilterList(serverFilterSpec, NULL, NULL, getter_AddRefs(serverFilterList)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = serverFilterList->GetFilterNamed(yesFilterName.get(), + getter_AddRefs(newFilter)); + if (newFilter && serverFilterTrustFlags & nsISpamSettings::TRUST_POSITIVES) + { + newFilter->SetTemporary(PR_TRUE); + filterList->InsertFilterAt(0, newFilter); + } + + rv = serverFilterList->GetFilterNamed(noFilterName.get(), + getter_AddRefs(newFilter)); + if (newFilter && serverFilterTrustFlags & nsISpamSettings::TRUST_NEGATIVES) + { + newFilter->SetTemporary(PR_TRUE); + filterList->InsertFilterAt(0, newFilter); + } + + return rv; +} + +nsresult nsMsgIncomingServer::ConfigureTemporaryReturnReceiptsFilter(nsIMsgFilterList *filterList) { - NS_ENSURE_ARG_POINTER(filterList); nsresult rv; nsCOMPtr accountMgr = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); @@ -2062,6 +2165,14 @@ nsMsgIncomingServer::SetSpamSettings(nsISpamSettings *aSpamSettings) rv = SetIntValue("purgeSpamInterval", purgeSpamInterval); NS_ENSURE_SUCCESS(rv,rv); + nsCAutoString serverFilterName; + mSpamSettings->GetServerFilterName(serverFilterName); + SetCharValue("serverFilterName", serverFilterName.get()); + + PRInt32 serverFilterTrustFlags; + mSpamSettings->GetServerFilterTrustFlags(&serverFilterTrustFlags); + SetIntValue("serverFilterTrustFlags", serverFilterTrustFlags); + PRInt32 loggingEnabled; rv = mSpamSettings->GetLoggingEnabled(&loggingEnabled); NS_ENSURE_SUCCESS(rv,rv); @@ -2151,7 +2262,15 @@ nsMsgIncomingServer::GetSpamSettings(nsISpamSettings **aSpamSettings) NS_ENSURE_SUCCESS(rv,rv); rv = mSpamSettings->SetPurgeInterval(purgeSpamInterval); NS_ENSURE_SUCCESS(rv,rv); - + + nsXPIDLCString serverFilterName; + rv = GetCharValue("serverFilterName", getter_Copies(serverFilterName)); + if (NS_SUCCEEDED(rv)) + mSpamSettings->SetServerFilterName(serverFilterName); + PRInt32 serverFilterTrustFlags = 0; + rv = GetIntValue("serverFilterTrustFlags", &serverFilterTrustFlags); + mSpamSettings->SetServerFilterTrustFlags(serverFilterTrustFlags); + PRInt32 loggingEnabled; rv = GetBoolValue("spamLoggingEnabled", &loggingEnabled); NS_ENSURE_SUCCESS(rv,rv); @@ -2174,9 +2293,8 @@ nsMsgIncomingServer::GetSpamFilterPlugin(nsIMsgFilterPlugin **aFilterPlugin) // get the plugin service mFilterPlugin = do_GetService("@mozilla.org/messenger/filter-plugin;1?name=bayesianfilter", &rv); - if (NS_FAILED(rv)) { + if (NS_FAILED(rv)) return rv; - } } NS_IF_ADDREF(*aFilterPlugin = mFilterPlugin); diff --git a/mailnews/base/util/nsMsgIncomingServer.h b/mailnews/base/util/nsMsgIncomingServer.h index 928276ba492..314da1dbddb 100644 --- a/mailnews/base/util/nsMsgIncomingServer.h +++ b/mailnews/base/util/nsMsgIncomingServer.h @@ -79,6 +79,9 @@ protected: void getPrefName(const char *serverKey, const char *pref, nsCString& fullPrefName); void getDefaultPrefName(const char *pref, nsCString& fullPrefName); + nsresult ConfigureTemporaryReturnReceiptsFilter(nsIMsgFilterList *filterList); + nsresult ConfigureTemporaryServerSpamFilters(nsIMsgFilterList *filterList); + // these are private pref getters and setters for the password // field. Callers should be using Get/Set Password NS_IMETHOD GetPrefPassword(char * *aPassword); diff --git a/mailnews/imap/src/nsImapMailFolder.cpp b/mailnews/imap/src/nsImapMailFolder.cpp index e68a3fd8e8f..c8376c9e1be 100644 --- a/mailnews/imap/src/nsImapMailFolder.cpp +++ b/mailnews/imap/src/nsImapMailFolder.cpp @@ -648,7 +648,7 @@ nsImapMailFolder::UpdateFolder(nsIMsgWindow *msgWindow) // can't file to the sent folder, so we don't add the filter for those servers if (canFileMessagesOnServer) { - rv = server->ConfigureTemporaryReturnReceiptsFilter(m_filterList); + rv = server->ConfigureTemporaryFilters(m_filterList); NS_ASSERTION(NS_SUCCEEDED(rv), "failed to add MDN filter"); } } diff --git a/mailnews/local/src/nsParseMailbox.cpp b/mailnews/local/src/nsParseMailbox.cpp index 65a8585f71a..35b074bf9ac 100644 --- a/mailnews/local/src/nsParseMailbox.cpp +++ b/mailnews/local/src/nsParseMailbox.cpp @@ -1461,7 +1461,7 @@ nsParseNewMailState::Init(nsIMsgFolder *rootFolder, nsIMsgFolder *downloadFolder rv = server->GetFilterList(aMsgWindow, getter_AddRefs(m_filterList)); if (m_filterList) - rv = server->ConfigureTemporaryReturnReceiptsFilter(m_filterList); + rv = server->ConfigureTemporaryFilters(m_filterList); m_disableFilters = PR_FALSE; return NS_OK; diff --git a/mailnews/makefiles b/mailnews/makefiles index 94161d4e059..1f1a639b7df 100644 --- a/mailnews/makefiles +++ b/mailnews/makefiles @@ -11,6 +11,7 @@ mailnews/base/build/Makefile mailnews/base/search/Makefile mailnews/base/search/public/Makefile mailnews/base/search/src/Makefile +mailnews/base/search/resources/content/Makefile mailnews/base/resources/Makefile mailnews/base/resources/content/Makefile mailnews/base/resources/content/unix/Makefile