Back to index

lightning-sunbird  0.9+nobinonly
nsSpamSettings.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001-2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Seth Spitzer <sspitzer@netscape.com>
00024  *   Dan Mosedale <dmose@netscape.com>
00025  *   David Bienvenu <bienvenu@mozilla.org>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsSpamSettings.h"
00042 #include "nsISupportsObsolete.h"
00043 #include "nsILocalFile.h"
00044 #include "plstr.h"
00045 #include "prmem.h"
00046 #include "nsIMsgHdr.h"
00047 #include "nsEscape.h"
00048 #include "nsNetUtil.h"
00049 #include "nsIMsgFolder.h"
00050 #include "nsMsgUtils.h"
00051 #include "nsMsgFolderFlags.h"
00052 #include "nsImapCore.h"
00053 #include "nsIImapIncomingServer.h"
00054 #include "nsIRDFService.h"
00055 #include "nsIRDFResource.h"
00056 #include "nsAppDirectoryServiceDefs.h"
00057 #include "nsIPrefService.h"
00058 #include "nsIPrefBranch.h"
00059 #include "nsIStringBundle.h"
00060 #include "nsDateTimeFormatCID.h"
00061 
00062 static NS_DEFINE_CID(kDateTimeFormatCID,    NS_DATETIMEFORMAT_CID);
00063 
00064 #include "nsMailDirServiceDefs.h"
00065 #include "nsDirectoryServiceUtils.h"
00066 #include "nsDirectoryServiceDefs.h"
00067 #include "nsISimpleEnumerator.h"
00068 #include "nsIDirectoryEnumerator.h"
00069 
00070 nsSpamSettings::nsSpamSettings()
00071 {
00072   mLevel = 0;
00073   mMoveOnSpam = PR_FALSE;
00074   mMoveTargetMode = nsISpamSettings::MOVE_TARGET_MODE_ACCOUNT;
00075   mPurge = PR_FALSE;
00076   mPurgeInterval = 14; // 14 days
00077 
00078   mServerFilterTrustFlags = 0;
00079 
00080   mUseWhiteList = PR_FALSE;
00081   mUseServerFilter = PR_FALSE;
00082 
00083   nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mLogFile));
00084   if (NS_SUCCEEDED(rv))
00085     mLogFile->Append(NS_LITERAL_STRING("junklog.html"));
00086 }
00087 
00088 nsSpamSettings::~nsSpamSettings()
00089 {
00090 }
00091 
00092 NS_IMPL_ISUPPORTS2(nsSpamSettings, nsISpamSettings, nsIUrlListener)
00093 
00094 NS_IMETHODIMP 
00095 nsSpamSettings::GetLevel(PRInt32 *aLevel)
00096 {
00097   NS_ENSURE_ARG_POINTER(aLevel);
00098   *aLevel = mLevel;
00099   return NS_OK;
00100 }
00101 
00102 NS_IMETHODIMP nsSpamSettings::SetLevel(PRInt32 aLevel)
00103 {
00104   NS_ASSERTION((aLevel >= 0 && aLevel <= 100), "bad level");
00105   mLevel = aLevel;
00106   return NS_OK;
00107 }
00108 
00109 NS_IMETHODIMP 
00110 nsSpamSettings::GetMoveTargetMode(PRInt32 *aMoveTargetMode)
00111 {
00112   NS_ENSURE_ARG_POINTER(aMoveTargetMode);
00113   *aMoveTargetMode = mMoveTargetMode;
00114   return NS_OK;
00115 }
00116 
00117 NS_IMETHODIMP nsSpamSettings::SetMoveTargetMode(PRInt32 aMoveTargetMode)
00118 {
00119   NS_ASSERTION((aMoveTargetMode == nsISpamSettings::MOVE_TARGET_MODE_FOLDER || aMoveTargetMode == nsISpamSettings::MOVE_TARGET_MODE_ACCOUNT), "bad move target mode");
00120   mMoveTargetMode = aMoveTargetMode;
00121   return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP nsSpamSettings::GetManualMark(PRBool *aManualMark)
00125 {
00126   NS_ENSURE_ARG_POINTER(aManualMark);
00127   nsresult rv;
00128   nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00129   NS_ENSURE_SUCCESS(rv, rv);
00130   return prefBranch->GetBoolPref("mail.spam.manualMark", aManualMark);
00131 }
00132 
00133 NS_IMETHODIMP nsSpamSettings::GetManualMarkMode(PRInt32 *aManualMarkMode)
00134 {
00135   NS_ENSURE_ARG_POINTER(aManualMarkMode);
00136   nsresult rv;
00137   nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00138   NS_ENSURE_SUCCESS(rv, rv);
00139   return prefBranch->GetIntPref("mail.spam.manualMarkMode", aManualMarkMode);
00140 }
00141 
00142 NS_IMETHODIMP nsSpamSettings::GetLoggingEnabled(PRBool *aLoggingEnabled)
00143 { 
00144   NS_ENSURE_ARG_POINTER(aLoggingEnabled);
00145   nsresult rv;
00146   nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00147   NS_ENSURE_SUCCESS(rv, rv);
00148   return prefBranch->GetBoolPref("mail.spam.logging.enabled", aLoggingEnabled);
00149 }
00150 
00151 NS_IMETHODIMP nsSpamSettings::GetMarkAsReadOnSpam(PRBool *aMarkAsReadOnSpam)
00152 {
00153   NS_ENSURE_ARG_POINTER(aMarkAsReadOnSpam);
00154   nsresult rv;
00155   nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00156   NS_ENSURE_SUCCESS(rv, rv);
00157   return prefBranch->GetBoolPref("mail.spam.markAsReadOnSpam", aMarkAsReadOnSpam);
00158 }
00159 
00160 NS_IMPL_GETSET(nsSpamSettings, MoveOnSpam, PRBool, mMoveOnSpam)
00161 NS_IMPL_GETSET(nsSpamSettings, Purge, PRBool, mPurge)
00162 NS_IMPL_GETSET(nsSpamSettings, UseWhiteList, PRBool, mUseWhiteList)
00163 NS_IMPL_GETSET(nsSpamSettings, UseServerFilter, PRBool, mUseServerFilter)
00164 
00165 NS_IMETHODIMP nsSpamSettings::GetWhiteListAbURI(char * *aWhiteListAbURI)
00166 {
00167   NS_ENSURE_ARG_POINTER(aWhiteListAbURI);
00168   *aWhiteListAbURI = ToNewCString(mWhiteListAbURI);
00169   return NS_OK;
00170 }
00171 NS_IMETHODIMP nsSpamSettings::SetWhiteListAbURI(const char * aWhiteListAbURI)
00172 {
00173   mWhiteListAbURI = aWhiteListAbURI;
00174   return NS_OK;
00175 }
00176 
00177 NS_IMETHODIMP nsSpamSettings::GetActionTargetAccount(char * *aActionTargetAccount)
00178 {
00179   NS_ENSURE_ARG_POINTER(aActionTargetAccount);
00180   *aActionTargetAccount = ToNewCString(mActionTargetAccount);
00181   return NS_OK;
00182 }
00183 
00184 NS_IMETHODIMP nsSpamSettings::SetActionTargetAccount(const char * aActionTargetAccount)
00185 {
00186   mActionTargetAccount = aActionTargetAccount;
00187   return NS_OK;
00188 }
00189 
00190 NS_IMETHODIMP nsSpamSettings::GetActionTargetFolder(char * *aActionTargetFolder)
00191 {
00192   NS_ENSURE_ARG_POINTER(aActionTargetFolder);
00193   *aActionTargetFolder = ToNewCString(mActionTargetFolder);
00194   return NS_OK;
00195 }
00196 
00197 NS_IMETHODIMP nsSpamSettings::SetActionTargetFolder(const char * aActionTargetFolder)
00198 {
00199   mActionTargetFolder = aActionTargetFolder;
00200   return NS_OK;
00201 }
00202 
00203 NS_IMETHODIMP nsSpamSettings::GetPurgeInterval(PRInt32 *aPurgeInterval)
00204 {
00205   NS_ENSURE_ARG_POINTER(aPurgeInterval);
00206   *aPurgeInterval = mPurgeInterval;
00207   return NS_OK;
00208 }
00209 
00210 NS_IMETHODIMP nsSpamSettings::SetPurgeInterval(PRInt32 aPurgeInterval)
00211 {
00212   NS_ASSERTION(aPurgeInterval >= 0, "bad purge interval");
00213   mPurgeInterval = aPurgeInterval;
00214   return NS_OK;
00215 }
00216 
00217 NS_IMETHODIMP
00218 nsSpamSettings::SetLogStream(nsIOutputStream *aLogStream)
00219 {
00220   // if there is a log stream already, close it
00221   if (mLogStream) {
00222     // will flush
00223     nsresult rv = mLogStream->Close();
00224     NS_ENSURE_SUCCESS(rv,rv);
00225   }
00226 
00227   mLogStream = aLogStream;
00228   return NS_OK;
00229 }
00230 
00231  #define LOG_HEADER "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head>"
00232  #define LOG_HEADER_LEN (strlen(LOG_HEADER))
00233  
00234 NS_IMETHODIMP
00235 nsSpamSettings::GetLogStream(nsIOutputStream **aLogStream)
00236 {
00237   NS_ENSURE_ARG_POINTER(aLogStream);
00238 
00239   nsresult rv;
00240 
00241   if (!mLogStream) {
00242     nsCOMPtr <nsILocalFile> logFile = do_QueryInterface(mLogFile, &rv);
00243     NS_ENSURE_SUCCESS(rv,rv);
00244 
00245     // append to the end of the log file
00246     rv = NS_NewLocalFileOutputStream(getter_AddRefs(mLogStream),
00247                                    logFile,
00248                                    PR_CREATE_FILE | PR_WRONLY | PR_APPEND,
00249                                    0600);
00250     NS_ENSURE_SUCCESS(rv,rv);
00251   }
00252  
00253   NS_ADDREF(*aLogStream = mLogStream);
00254   return NS_OK;
00255 }
00256 
00257 NS_IMETHODIMP nsSpamSettings::Initialize(nsIMsgIncomingServer *aServer)
00258 {
00259   NS_ENSURE_ARG_POINTER(aServer);
00260   nsresult rv;
00261   PRInt32 spamLevel;
00262   rv = aServer->GetIntValue("spamLevel", &spamLevel);
00263   NS_ENSURE_SUCCESS(rv, rv);
00264   rv = SetLevel(spamLevel);
00265   NS_ENSURE_SUCCESS(rv, rv);
00266 
00267   PRBool moveOnSpam;
00268   rv = aServer->GetBoolValue("moveOnSpam", &moveOnSpam);
00269   NS_ENSURE_SUCCESS(rv, rv);
00270   rv = SetMoveOnSpam(moveOnSpam);
00271   NS_ENSURE_SUCCESS(rv, rv);
00272 
00273   PRInt32 moveTargetMode;
00274   rv = aServer->GetIntValue("moveTargetMode", &moveTargetMode);
00275   NS_ENSURE_SUCCESS(rv, rv);
00276   rv = SetMoveTargetMode(moveTargetMode);
00277   NS_ENSURE_SUCCESS(rv, rv);
00278 
00279   nsXPIDLCString spamActionTargetAccount;
00280   rv = aServer->GetCharValue("spamActionTargetAccount", getter_Copies(spamActionTargetAccount));
00281   NS_ENSURE_SUCCESS(rv, rv);
00282   rv = SetActionTargetAccount(spamActionTargetAccount);    
00283   NS_ENSURE_SUCCESS(rv,rv);
00284 
00285   nsXPIDLCString spamActionTargetFolder;
00286   rv = aServer->GetCharValue("spamActionTargetFolder", getter_Copies(spamActionTargetFolder));
00287   NS_ENSURE_SUCCESS(rv, rv);
00288   rv = SetActionTargetFolder(spamActionTargetFolder);
00289   NS_ENSURE_SUCCESS(rv,rv);
00290 
00291   PRBool useWhiteList;
00292   rv = aServer->GetBoolValue("useWhiteList", &useWhiteList);
00293   NS_ENSURE_SUCCESS(rv, rv);
00294   rv = SetUseWhiteList(useWhiteList);
00295   NS_ENSURE_SUCCESS(rv, rv);
00296 
00297   nsXPIDLCString whiteListAbURI;
00298   rv = aServer->GetCharValue("whiteListAbURI", getter_Copies(whiteListAbURI));
00299   NS_ENSURE_SUCCESS(rv, rv);
00300   rv = SetWhiteListAbURI(whiteListAbURI);
00301   NS_ENSURE_SUCCESS(rv,rv);
00302   
00303   PRBool purgeSpam;
00304   rv = aServer->GetBoolValue("purgeSpam", &purgeSpam);
00305   NS_ENSURE_SUCCESS(rv, rv);
00306   rv = SetPurge(purgeSpam);
00307   NS_ENSURE_SUCCESS(rv,rv);
00308 
00309   PRInt32 purgeSpamInterval;
00310   rv = aServer->GetIntValue("purgeSpamInterval", &purgeSpamInterval);
00311   NS_ENSURE_SUCCESS(rv, rv);
00312   rv = SetPurgeInterval(purgeSpamInterval);
00313   NS_ENSURE_SUCCESS(rv,rv);
00314 
00315   PRBool useServerFilter;
00316   rv = aServer->GetBoolValue("useServerFilter", &useServerFilter);
00317   NS_ENSURE_SUCCESS(rv, rv);
00318   rv = SetUseServerFilter(useServerFilter);
00319   NS_ENSURE_SUCCESS(rv,rv);
00320 
00321   nsXPIDLCString serverFilterName;
00322   rv = aServer->GetCharValue("serverFilterName", getter_Copies(serverFilterName));
00323   if (NS_SUCCEEDED(rv))
00324     SetServerFilterName(serverFilterName);
00325   PRInt32 serverFilterTrustFlags = 0;
00326   rv = aServer->GetIntValue("serverFilterTrustFlags", &serverFilterTrustFlags);
00327   NS_ENSURE_SUCCESS(rv,rv);
00328   rv = SetServerFilterTrustFlags(serverFilterTrustFlags);
00329   NS_ENSURE_SUCCESS(rv, rv);
00330 
00331   return UpdateJunkFolderState();
00332 }
00333 
00334 nsresult nsSpamSettings::UpdateJunkFolderState()
00335 {
00336   nsresult rv;
00337 
00338   // if the spam folder uri changed on us, we need to unset the junk flag
00339   // on the old spam folder
00340   nsXPIDLCString newJunkFolderURI;
00341   rv = GetSpamFolderURI(getter_Copies(newJunkFolderURI));
00342   NS_ENSURE_SUCCESS(rv,rv);
00343 
00344   if (!mCurrentJunkFolderURI.IsEmpty() && !mCurrentJunkFolderURI.Equals(newJunkFolderURI))
00345   {
00346     nsCOMPtr<nsIMsgFolder> oldJunkFolder;
00347     rv = GetExistingFolder(mCurrentJunkFolderURI.get(), getter_AddRefs(oldJunkFolder));
00348     if (NS_SUCCEEDED(rv) && oldJunkFolder) 
00349     {
00350       // remove the MSG_FOLDER_FLAG_JUNK on the old junk folder
00351       // XXX TODO
00352       // JUNK MAIL RELATED
00353       // (in ClearFlag?) we need to make sure that this folder
00354       // is not a the junk folder for another account
00355       // the same goes for set flag.  have fun with all that.
00356       oldJunkFolder->ClearFlag(MSG_FOLDER_FLAG_JUNK);
00357   }
00358 }
00359 
00360   mCurrentJunkFolderURI = newJunkFolderURI;
00361 
00362   // only try to create the junk folder if we are moving junk
00363   // and we have a non-empty uri
00364   if (mMoveOnSpam && !mCurrentJunkFolderURI.IsEmpty()) {
00365     // as the url listener, the spam settings will set the MSG_FOLDER_FLAG_JUNK folder flag
00366     // on the junk mail folder, after it is created
00367     rv = GetOrCreateFolder(mCurrentJunkFolderURI, this);
00368   }
00369 
00370   return rv;
00371 }
00372 
00373 NS_IMETHODIMP nsSpamSettings::Clone(nsISpamSettings *aSpamSettings)
00374 {
00375   NS_ENSURE_ARG_POINTER(aSpamSettings);
00376 
00377   nsresult rv = aSpamSettings->GetUseWhiteList(&mUseWhiteList); 
00378   NS_ENSURE_SUCCESS(rv,rv);
00379 
00380   (void)aSpamSettings->GetMoveOnSpam(&mMoveOnSpam);
00381   (void)aSpamSettings->GetPurge(&mPurge); 
00382   (void)aSpamSettings->GetUseServerFilter(&mUseServerFilter);
00383 
00384   rv = aSpamSettings->GetPurgeInterval(&mPurgeInterval); 
00385   NS_ENSURE_SUCCESS(rv,rv);
00386 
00387   rv = aSpamSettings->GetLevel(&mLevel); 
00388   NS_ENSURE_SUCCESS(rv,rv);
00389 
00390   rv = aSpamSettings->GetMoveTargetMode(&mMoveTargetMode); 
00391   NS_ENSURE_SUCCESS(rv,rv);
00392 
00393   nsXPIDLCString actionTargetAccount;
00394   rv = aSpamSettings->GetActionTargetAccount(getter_Copies(actionTargetAccount)); 
00395   NS_ENSURE_SUCCESS(rv,rv);
00396   mActionTargetAccount = actionTargetAccount;
00397 
00398   nsXPIDLCString actionTargetFolder;
00399   rv = aSpamSettings->GetActionTargetFolder(getter_Copies(actionTargetFolder)); 
00400   NS_ENSURE_SUCCESS(rv,rv);
00401   mActionTargetFolder = actionTargetFolder;
00402 
00403   nsXPIDLCString whiteListAbURI;
00404   rv = aSpamSettings->GetWhiteListAbURI(getter_Copies(whiteListAbURI)); 
00405   NS_ENSURE_SUCCESS(rv,rv);
00406   mWhiteListAbURI = whiteListAbURI;
00407   
00408   aSpamSettings->GetServerFilterName(mServerFilterName);
00409   aSpamSettings->GetServerFilterTrustFlags(&mServerFilterTrustFlags);
00410 
00411   return rv;
00412 }
00413 
00414 NS_IMETHODIMP nsSpamSettings::GetSpamFolderURI(char **aSpamFolderURI)
00415 {
00416   NS_ENSURE_ARG_POINTER(aSpamFolderURI);
00417 
00418   if (mMoveTargetMode == nsISpamSettings::MOVE_TARGET_MODE_FOLDER)
00419     return GetActionTargetFolder(aSpamFolderURI);
00420 
00421   // if the mode is nsISpamSettings::MOVE_TARGET_MODE_ACCOUNT
00422   // the spam folder URI = account uri + "/Junk"
00423   nsXPIDLCString folderURI;
00424   nsresult rv = GetActionTargetAccount(getter_Copies(folderURI));
00425   NS_ENSURE_SUCCESS(rv,rv);
00426 
00427   // we might be trying to get the old spam folder uri
00428   // in order to clear the flag
00429   // if we didn't have one, bail out.
00430   if (folderURI.IsEmpty())
00431     return NS_OK;
00432 
00433   nsCOMPtr<nsIRDFService> rdf(do_GetService("@mozilla.org/rdf/rdf-service;1", &rv));
00434   NS_ENSURE_SUCCESS(rv, rv);
00435   
00436   nsCOMPtr<nsIRDFResource> folderResource;
00437   rv = rdf->GetResource(folderURI, getter_AddRefs(folderResource));
00438   NS_ENSURE_SUCCESS(rv, rv);
00439 
00440   nsCOMPtr <nsIMsgFolder> folder = do_QueryInterface(folderResource);
00441   if (!folder)
00442     return NS_ERROR_UNEXPECTED;
00443 
00444   nsCOMPtr <nsIMsgIncomingServer> server;
00445   rv = folder->GetServer(getter_AddRefs(server));
00446   NS_ENSURE_SUCCESS(rv,rv);
00447 
00448   // see nsMsgFolder::SetPrettyName() for where the pretty name is set.
00449   folderURI.Append("/Junk");
00450   
00451   // XXX todo
00452   // better not to make base depend in imap
00453   // but doing it here, like in nsMsgCopy.cpp
00454   // one day, we'll fix this (and nsMsgCopy.cpp) to use GetMsgFolderFromURI()
00455   nsCOMPtr<nsIImapIncomingServer> imapServer = do_QueryInterface(server);
00456   if (imapServer) {
00457     // Make sure an specific IMAP folder has correct personal namespace
00458     // see bug #197043
00459     nsXPIDLCString folderUriWithNamespace;
00460     (void)imapServer->GetUriWithNamespacePrefixIfNecessary(kPersonalNamespace, folderURI.get(), getter_Copies(folderUriWithNamespace));
00461     if (!folderUriWithNamespace.IsEmpty())
00462       folderURI = folderUriWithNamespace;
00463   }
00464 
00465   *aSpamFolderURI = ToNewCString(folderURI);
00466   if (!*aSpamFolderURI)
00467     return NS_ERROR_OUT_OF_MEMORY;
00468   else 
00469     return rv;
00470 }
00471 
00472 NS_IMETHODIMP nsSpamSettings::GetServerFilterName(nsACString &aFilterName)
00473 {
00474   aFilterName = mServerFilterName;
00475   return NS_OK;
00476 }
00477 
00478 NS_IMETHODIMP nsSpamSettings::SetServerFilterName(const nsACString &aFilterName)
00479 {
00480   mServerFilterName = aFilterName;
00481   mServerFilterFile = nsnull; // clear out our stored location value
00482   return NS_OK;
00483 }
00484 
00485 NS_IMETHODIMP nsSpamSettings::GetServerFilterFile(nsIFile ** aFile)
00486 {
00487   NS_ENSURE_ARG_POINTER(aFile);
00488   if (!mServerFilterFile)
00489   {
00490     nsresult rv;
00491     nsCAutoString serverFilterFileName;
00492     GetServerFilterName(serverFilterFileName);
00493     serverFilterFileName.Append(".sfd");
00494 
00495     nsCOMPtr<nsIProperties> dirSvc = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
00496     NS_ENSURE_SUCCESS(rv, rv);
00497 
00498     // Walk through the list of isp directories
00499     nsCOMPtr<nsISimpleEnumerator> ispDirectories;
00500     rv = dirSvc->Get(ISP_DIRECTORY_LIST, NS_GET_IID(nsISimpleEnumerator), getter_AddRefs(ispDirectories));
00501     NS_ENSURE_SUCCESS(rv, rv);
00502 
00503     PRBool hasMore;
00504     nsCOMPtr<nsIFile> file;
00505     while (NS_SUCCEEDED(ispDirectories->HasMoreElements(&hasMore)) && hasMore) 
00506     {
00507       nsCOMPtr<nsISupports> elem;
00508       ispDirectories->GetNext(getter_AddRefs(elem));
00509       file = do_QueryInterface(elem);
00510 
00511       if (file)
00512       {
00513         // append our desired leaf name then test to see if the file exists. If it does, we've found
00514         // mServerFilterFile.
00515         file->AppendNative(serverFilterFileName);
00516         PRBool exists;
00517         if (NS_SUCCEEDED(file->Exists(&exists)) && exists)
00518         {
00519           file.swap(mServerFilterFile);
00520           break;
00521         }
00522       } // if file
00523     } // until we find the location of mServerFilterName
00524   } // if we haven't already stored mServerFilterFile
00525 
00526   NS_IF_ADDREF(*aFile = mServerFilterFile);
00527   return NS_OK;
00528 }
00529 
00530 
00531 NS_IMPL_GETSET(nsSpamSettings, ServerFilterTrustFlags, PRBool, mServerFilterTrustFlags)
00532 
00533 #define LOG_ENTRY_START_TAG "<p>\n"
00534 #define LOG_ENTRY_START_TAG_LEN (strlen(LOG_ENTRY_START_TAG))
00535 #define LOG_ENTRY_END_TAG "</p>\n"
00536 #define LOG_ENTRY_END_TAG_LEN (strlen(LOG_ENTRY_END_TAG))
00537 
00538 NS_IMETHODIMP nsSpamSettings::LogJunkHit(nsIMsgDBHdr *aMsgHdr, PRBool aMoveMessage)
00539 {
00540   PRBool loggingEnabled;
00541   nsresult rv = GetLoggingEnabled(&loggingEnabled);
00542   NS_ENSURE_SUCCESS(rv,rv);
00543 
00544   if (!loggingEnabled)
00545     return NS_OK;
00546 
00547   PRTime date;
00548   
00549   nsXPIDLString authorValue;
00550   nsXPIDLString subjectValue;
00551   nsXPIDLString dateValue;
00552   
00553   (void)aMsgHdr->GetDate(&date);
00554   PRExplodedTime exploded;
00555   PR_ExplodeTime(date, PR_LocalTimeParameters, &exploded);
00556 
00557   if (!mDateFormatter)
00558   {
00559     mDateFormatter = do_CreateInstance(kDateTimeFormatCID, &rv);
00560     NS_ENSURE_SUCCESS(rv, rv);
00561     if (!mDateFormatter)
00562     {
00563       return NS_ERROR_FAILURE;
00564     }
00565   }
00566   mDateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort,
00567                                       kTimeFormatSeconds, &exploded, 
00568                                       dateValue);
00569   
00570   (void)aMsgHdr->GetMime2DecodedAuthor(getter_Copies(authorValue));
00571   (void)aMsgHdr->GetMime2DecodedSubject(getter_Copies(subjectValue));
00572   
00573   nsCString buffer;
00574   // this is big enough to hold a log entry.  
00575   // do this so we avoid growing and copying as we append to the log.
00576   buffer.SetCapacity(512);  
00577   
00578   nsCOMPtr<nsIStringBundleService> bundleService =
00579     do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
00580   NS_ENSURE_SUCCESS(rv, rv);
00581   
00582   nsCOMPtr<nsIStringBundle> bundle;
00583   rv = bundleService->CreateBundle("chrome://messenger/locale/filter.properties",
00584     getter_AddRefs(bundle));
00585   NS_ENSURE_SUCCESS(rv, rv);
00586   
00587   const PRUnichar *junkLogDetectFormatStrings[3] = { authorValue.get(), subjectValue.get(), dateValue.get() };
00588   nsXPIDLString junkLogDetectStr;
00589   rv = bundle->FormatStringFromName(
00590     NS_LITERAL_STRING("junkLogDetectStr").get(),
00591     junkLogDetectFormatStrings, 3,
00592     getter_Copies(junkLogDetectStr));
00593   NS_ENSURE_SUCCESS(rv, rv);
00594 
00595   buffer += NS_ConvertUTF16toUTF8(junkLogDetectStr);
00596   buffer +=  "\n";
00597   
00598   if (aMoveMessage) {
00599     nsXPIDLCString msgId;
00600     aMsgHdr->GetMessageId(getter_Copies(msgId));
00601     
00602     nsXPIDLCString junkFolderURI;
00603     rv = GetSpamFolderURI(getter_Copies(junkFolderURI));
00604     NS_ENSURE_SUCCESS(rv, rv);
00605 
00606     NS_ConvertASCIItoUTF16 msgIdValue(msgId);
00607     NS_ConvertASCIItoUTF16 junkFolderURIValue(junkFolderURI);
00608     
00609     const PRUnichar *logMoveFormatStrings[2] = { msgIdValue.get(), junkFolderURIValue.get() };
00610     nsXPIDLString logMoveStr;
00611     rv = bundle->FormatStringFromName(
00612       NS_LITERAL_STRING("logMoveStr").get(),
00613       logMoveFormatStrings, 2,
00614       getter_Copies(logMoveStr));
00615     NS_ENSURE_SUCCESS(rv, rv);
00616     
00617     buffer += NS_ConvertUTF16toUTF8(logMoveStr);
00618     buffer += "\n";
00619   }
00620 
00621   return LogJunkString(buffer.get());
00622 }
00623 
00624 NS_IMETHODIMP nsSpamSettings::LogJunkString(const char *string)
00625 {
00626   PRBool loggingEnabled;
00627   nsresult rv = GetLoggingEnabled(&loggingEnabled);
00628   NS_ENSURE_SUCCESS(rv,rv);
00629 
00630   if (!loggingEnabled)
00631     return NS_OK;
00632 
00633   nsCOMPtr <nsIOutputStream> logStream;
00634   rv = GetLogStream(getter_AddRefs(logStream));
00635   NS_ENSURE_SUCCESS(rv,rv);
00636   
00637   PRUint32 writeCount;
00638   
00639   rv = logStream->Write(LOG_ENTRY_START_TAG, LOG_ENTRY_START_TAG_LEN, &writeCount);
00640   NS_ENSURE_SUCCESS(rv,rv);
00641   NS_ASSERTION(writeCount == LOG_ENTRY_START_TAG_LEN, "failed to write out start log tag");
00642   
00643   // html escape the log for security reasons.
00644   // we don't want some to send us a message with a subject with
00645   // html tags, especially <script>
00646   char *escapedBuffer = nsEscapeHTML(string);
00647   if (!escapedBuffer)
00648     return NS_ERROR_OUT_OF_MEMORY;
00649   
00650   PRUint32 escapedBufferLen = strlen(escapedBuffer);
00651   rv = logStream->Write(escapedBuffer, escapedBufferLen, &writeCount);
00652   PR_Free(escapedBuffer);
00653   NS_ENSURE_SUCCESS(rv,rv);
00654   NS_ASSERTION(writeCount == escapedBufferLen, "failed to write out log hit");
00655   
00656   rv = logStream->Write(LOG_ENTRY_END_TAG, LOG_ENTRY_END_TAG_LEN, &writeCount);
00657   NS_ENSURE_SUCCESS(rv,rv);
00658   NS_ASSERTION(writeCount == LOG_ENTRY_END_TAG_LEN, "failed to write out end log tag");
00659   return NS_OK;
00660 }
00661 
00662 NS_IMETHODIMP nsSpamSettings::OnStartRunningUrl(nsIURI* aURL)
00663 {
00664   // do nothing
00665   // all the action happens in OnStopRunningUrl()
00666   return NS_OK;
00667 }
00668 
00669 NS_IMETHODIMP nsSpamSettings::OnStopRunningUrl(nsIURI* aURL, nsresult exitCode)
00670 {
00671   nsXPIDLCString junkFolderURI;
00672   nsresult rv = GetSpamFolderURI(getter_Copies(junkFolderURI));
00673   NS_ENSURE_SUCCESS(rv,rv);
00674 
00675   if (junkFolderURI.IsEmpty())
00676     return NS_ERROR_UNEXPECTED;
00677 
00678   // when we get here, the folder should exist.
00679   nsCOMPtr <nsIMsgFolder> junkFolder;
00680   rv = GetExistingFolder(junkFolderURI.get(), getter_AddRefs(junkFolder));
00681   NS_ENSURE_SUCCESS(rv,rv);
00682   if (!junkFolder)
00683     return NS_ERROR_UNEXPECTED;
00684 
00685   rv = junkFolder->SetFlag(MSG_FOLDER_FLAG_JUNK);
00686   NS_ENSURE_SUCCESS(rv,rv);
00687   return rv;
00688 }