diff --git a/mozilla/mailnews/local/src/nsMovemailIncomingServer.cpp b/mozilla/mailnews/local/src/nsMovemailIncomingServer.cpp index 4d4159cc7cf..5b97b3e4cfd 100644 --- a/mozilla/mailnews/local/src/nsMovemailIncomingServer.cpp +++ b/mozilla/mailnews/local/src/nsMovemailIncomingServer.cpp @@ -36,24 +36,11 @@ * * ***** END LICENSE BLOCK ***** */ -#include "prmem.h" -#include "plstr.h" -#include "prprf.h" - -//#include "nsCOMPtr.h" -//#include "nsIPref.h" - -#include "nsXPIDLString.h" - #include "nsMsgLocalCID.h" #include "nsMsgFolderFlags.h" #include "nsIFileSpec.h" -#include "nsIPref.h" - #include "nsIMsgLocalMailFolder.h" - #include "nsIMovemailService.h" - #include "msgCore.h" // pre-compiled headers #include "nsMovemailIncomingServer.h" @@ -70,17 +57,13 @@ NS_IMPL_ISUPPORTS_INHERITED2(nsMovemailIncomingServer, nsMovemailIncomingServer::nsMovemailIncomingServer() { - m_canHaveFilters = PR_TRUE; + m_canHaveFilters = PR_TRUE; } nsMovemailIncomingServer::~nsMovemailIncomingServer() { } -/*NS_IMPL_SERVERPREF_BOOL(nsMovemailIncomingServer, - AdamIsASillyhead, - "adam_is_a_sillyhead")*/ - NS_IMETHODIMP nsMovemailIncomingServer::GetIsSecureServer(PRBool *aIsSecureServer) { @@ -123,62 +106,64 @@ nsMovemailIncomingServer::SetFlagsOnDefaultMailboxes() NS_IMETHODIMP nsMovemailIncomingServer::CreateDefaultMailboxes(nsIFileSpec *path) { - nsresult rv; - PRBool exists; - if (!path) return NS_ERROR_NULL_POINTER; + nsresult rv; + PRBool exists; + if (!path) return NS_ERROR_NULL_POINTER; - rv =path->AppendRelativeUnixPath("Inbox"); + rv = path->AppendRelativeUnixPath("Inbox"); + if (NS_FAILED(rv)) return rv; + rv = path->Exists(&exists); + if (NS_FAILED(rv)) return rv; + if (!exists) { + rv = path->Touch(); if (NS_FAILED(rv)) return rv; - rv = path->Exists(&exists); - if (!exists) { - rv = path->Touch(); - if (NS_FAILED(rv)) return rv; - } + } - rv =path->SetLeafName("Trash"); + rv = path->SetLeafName("Trash"); + if (NS_FAILED(rv)) return rv; + rv = path->Exists(&exists); + if (NS_FAILED(rv)) return rv; + if (!exists) { + rv = path->Touch(); if (NS_FAILED(rv)) return rv; - rv = path->Exists(&exists); - if (!exists) { - rv = path->Touch(); - if (NS_FAILED(rv)) return rv; - } + } - rv = path->SetLeafName("Sent"); + rv = path->SetLeafName("Sent"); + if (NS_FAILED(rv)) return rv; + rv = path->Exists(&exists); + if (NS_FAILED(rv)) return rv; + if (!exists) { + rv = path->Touch(); if (NS_FAILED(rv)) return rv; - rv = path->Exists(&exists); - if (NS_FAILED(rv)) return rv; - if (!exists) { - rv = path->Touch(); - if (NS_FAILED(rv)) return rv; - } + } - rv = path->SetLeafName("Drafts"); + rv = path->SetLeafName("Drafts"); + if (NS_FAILED(rv)) return rv; + rv = path->Exists(&exists); + if (NS_FAILED(rv)) return rv; + if (!exists) { + rv = path->Touch(); if (NS_FAILED(rv)) return rv; - rv = path->Exists(&exists); - if (NS_FAILED(rv)) return rv; - if (!exists) { - rv = path->Touch(); - if (NS_FAILED(rv)) return rv; - } + } - rv = path->SetLeafName("Templates"); + rv = path->SetLeafName("Templates"); + if (NS_FAILED(rv)) return rv; + rv = path->Exists(&exists); + if (NS_FAILED(rv)) return rv; + if (!exists) { + rv = path->Touch(); if (NS_FAILED(rv)) return rv; - rv = path->Exists(&exists); - if (NS_FAILED(rv)) return rv; - if (!exists) { - rv = path->Touch(); - if (NS_FAILED(rv)) return rv; - } + } - rv = path->SetLeafName("Unsent Messages"); + rv = path->SetLeafName("Unsent Messages"); + if (NS_FAILED(rv)) return rv; + rv = path->Exists(&exists); + if (NS_FAILED(rv)) return rv; + if (!exists) { + rv = path->Touch(); if (NS_FAILED(rv)) return rv; - rv = path->Exists(&exists); - if (NS_FAILED(rv)) return rv; - if (!exists) { - rv = path->Touch(); - if (NS_FAILED(rv)) return rv; - } - return rv; + } + return rv; } diff --git a/mozilla/mailnews/local/src/nsMovemailService.cpp b/mozilla/mailnews/local/src/nsMovemailService.cpp index b720bcc1cc6..e648a05345f 100644 --- a/mozilla/mailnews/local/src/nsMovemailService.cpp +++ b/mozilla/mailnews/local/src/nsMovemailService.cpp @@ -73,6 +73,14 @@ #define PREF_MAIL_ROOT_MOVEMAIL "mail.root.movemail" +const char * gDefaultSpoolPaths[] = { + "/var/spool/mail/", + "/usr/spool/mail/", + "/var/mail/", + "/usr/mail/" +}; +#define NUM_DEFAULT_SPOOL_PATHS (sizeof(gDefaultSpoolPaths)/sizeof(gDefaultSpoolPaths[0])) + static NS_DEFINE_CID(kPrefCID, NS_PREF_CID); nsMovemailService::nsMovemailService() @@ -261,11 +269,19 @@ PRBool YieldSpoolLock(const char *spoolnameStr) if (NS_FAILED(rv)) return PR_FALSE; - // Delete the file - rv = locklocfile->Remove(PR_FALSE /* non-recursive */); + // Check if the lock file exists + PRBool exists; + rv = locklocfile->Exists(&exists); if (NS_FAILED(rv)) return PR_FALSE; + // Delete the file if it exists + if (exists) { + rv = locklocfile->Remove(PR_FALSE /* non-recursive */); + if (NS_FAILED(rv)) + return PR_FALSE; + } + #ifdef MOVEMAIL_DEBUG fprintf(stderr, " LOCK YIELDING WAS SUCCESSFUL.\n"); #endif @@ -275,49 +291,6 @@ PRBool YieldSpoolLock(const char *spoolnameStr) } -nsInputFileStream * Probe_SpoolFilePath(const char *pathStr, - PRBool *found_but_locked) -{ - *found_but_locked = PR_FALSE; - if (!pathStr) return nsnull; - nsInputFileStream * rtnStream = nsnull; - nsFileSpec * filespec = new nsFileSpec(pathStr); - - if (!filespec) - return nsnull; - - if ((!filespec->Failed()) && - filespec->IsFile()) { -#ifdef MOVEMAIL_DEBUG - fprintf(stderr, "##%s@(%s)##", filespec->GetLeafName(), - pathStr); -#endif - - // before opening (or truncating) the spool file - // we need to successfully obtain its lock to ensure - // exclusive access (conforming spool-molestors allowing). - if (!ObtainSpoolLock(pathStr, 5 /* seconds of retrying */ )) { - *found_but_locked = PR_TRUE; - } - else { // got lock okay - // open the file for reading - rtnStream = new nsInputFileStream (*filespec); - - if (rtnStream) { - if (rtnStream->failed() || - !rtnStream->is_open()) { - delete rtnStream; - rtnStream = nsnull; - } - } - } - } - - delete filespec; - return rtnStream; -} - - nsresult nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, nsIUrlListener *aUrlListener, @@ -339,82 +312,63 @@ nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, // FUN STUFF starts here { // we now quest for the mail spool file... - nsInputFileStream * spoolfile = nsnull; - PRBool found_spool_but_it_is_locked = PR_FALSE; + nsFileSpec spoolFileSpec; + PRBool foundSpoolFile = PR_FALSE; // If $(MAIL) is set then we have things easy. - char * wholeboxname_from_env = PR_GetEnv("MAIL"); - if (wholeboxname_from_env) { - wholeboxname.Assign(wholeboxname_from_env); - spoolfile = Probe_SpoolFilePath(wholeboxname_from_env, - &found_spool_but_it_is_locked); - } else { + char * mailEnv = PR_GetEnv("MAIL"); + char * userEnv = PR_GetEnv("USER"); + if (!userEnv) + userEnv = PR_GetEnv("USERNAME"); + + if (mailEnv) { + wholeboxname = mailEnv; + spoolFileSpec = wholeboxname.get(); + foundSpoolFile = (spoolFileSpec.Valid() && spoolFileSpec.IsFile()); + } else if (userEnv) { // Otherwise try to build the mailbox path from the // username and a number of guessed spool directory // paths. - char * boxfilename; - boxfilename = PR_GetEnv("USER"); // UNIXy - if (!boxfilename) { - boxfilename = PR_GetEnv("USERNAME"); // WIN32 (!) + int i; + for (i = 0; i < NUM_DEFAULT_SPOOL_PATHS && !foundSpoolFile; i++) { + wholeboxname = gDefaultSpoolPaths[i]; + wholeboxname += userEnv; + spoolFileSpec = wholeboxname.get(); + foundSpoolFile = (spoolFileSpec.Valid() && spoolFileSpec.IsFile()); } + } - if (boxfilename) { - // We have the username which is probably also the - // mailbox file name, so now try to find the mailbox - // in a number of likely places. - - wholeboxname = "/var/spool/mail/"; - wholeboxname += boxfilename; - spoolfile = Probe_SpoolFilePath(wholeboxname.get(), - &found_spool_but_it_is_locked); - - if ((!spoolfile) && (!found_spool_but_it_is_locked)) { - wholeboxname = "/usr/spool/mail/"; - wholeboxname += boxfilename; - spoolfile = Probe_SpoolFilePath(wholeboxname.get(), - &found_spool_but_it_is_locked); - } - - if ((!spoolfile) && (!found_spool_but_it_is_locked)) { - wholeboxname = "/var/mail/"; - wholeboxname += boxfilename; - spoolfile = Probe_SpoolFilePath(wholeboxname.get(), - &found_spool_but_it_is_locked); - } - - if ((!spoolfile) && (!found_spool_but_it_is_locked)) { - wholeboxname = "/usr/mail/"; - wholeboxname += boxfilename; - spoolfile = Probe_SpoolFilePath(wholeboxname.get(), - &found_spool_but_it_is_locked); - } - } - } - - if (!spoolfile) { -#ifdef MOVEMAIL_DEBUG - fprintf(stderr, - "Nope, no luck getting spool file name at all.\n"); -#endif + if (!foundSpoolFile) { + Error(MOVEMAIL_SPOOL_FILE_NOT_FOUND, nsnull, 0); return NS_ERROR_FAILURE; } - - if (spoolfile && found_spool_but_it_is_locked) { -#ifdef MOVEMAIL_DEBUG - fprintf(stderr, - "Well, got the spool file but not the lock.\n"); -#endif + + nsInputFileStream spoolfile(spoolFileSpec); + if (!spoolfile.is_open() || spoolfile.failed()) { + const PRUnichar *params[] = { + NS_ConvertUTF8toUCS2(wholeboxname).get() + }; + Error(MOVEMAIL_CANT_OPEN_SPOOL_FILE, params, 1); + return NS_ERROR_FAILURE; + } + + // Try and obtain the lock for the spool file + if (!ObtainSpoolLock(wholeboxname.get(), 5)) { + nsAutoString lockFile = NS_ConvertUTF8toUCS2(wholeboxname); + lockFile += NS_LITERAL_STRING(".lock"); + const PRUnichar *params[] = { + lockFile.get() + }; + Error(MOVEMAIL_CANT_CREATE_LOCK, params, 1); return NS_ERROR_FAILURE; } #define READBUFSIZE 4096 char * buffer = (char*)PR_CALLOC(READBUFSIZE); + nsParseNewMailState *newMailParser = nsnull; if (!buffer) rv = NS_ERROR_OUT_OF_MEMORY; else { - nsIOFileStream* outFileStream; - nsParseNewMailState *newMailParser; - nsCOMPtr mailDirectory; rv = in_server->GetLocalPath(getter_AddRefs(mailDirectory)); if (NS_FAILED(rv)) @@ -423,21 +377,17 @@ nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, nsFileSpec fileSpec; mailDirectory->GetFileSpec(&fileSpec); fileSpec += "Inbox"; - outFileStream = new nsIOFileStream(fileSpec); - if (!outFileStream) { - rv = NS_ERROR_OUT_OF_MEMORY; - goto freebuff_and_unlock; - } - outFileStream->seek(fileSpec.GetFileSize()); + nsIOFileStream outFileStream(fileSpec); + outFileStream.seek(fileSpec.GetFileSize()); // create a new mail parser newMailParser = new nsParseNewMailState; + NS_IF_ADDREF(newMailParser); if (newMailParser == nsnull) { rv = NS_ERROR_OUT_OF_MEMORY; goto freebuff_and_unlock; } - nsCOMPtr iParseMailMsgState = NS_STATIC_CAST(nsIMsgParseMailMsgState*, newMailParser); nsCOMPtr serverFolder; rv = in_server->GetRootFolder(getter_AddRefs(serverFolder)); @@ -455,7 +405,7 @@ nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, if (NS_FAILED(rv)) goto freebuff_and_unlock; rv = newMailParser->Init(serverFolder, inbox, - fileSpec, outFileStream, nsnull); + fileSpec, &outFileStream, nsnull); if (NS_FAILED(rv)) goto freebuff_and_unlock; @@ -465,12 +415,12 @@ nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, int numlines = 0; // main parsing loop - while (!spoolfile->eof() && - !spoolfile->failed() && - spoolfile->is_open()) + while (!spoolfile.eof() && + !spoolfile.failed() && + spoolfile.is_open()) { - spoolfile->readline(buffer, - READBUFSIZE-(1+PL_strlen(MSG_LINEBREAK))); + spoolfile.readline(buffer, + READBUFSIZE-(1+PL_strlen(MSG_LINEBREAK))); // 'From' lines delimit messages if ((numlines>0) && @@ -482,7 +432,7 @@ nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, // (or, alternatively, we got a 'From' line and // then the file mysteriously ended) then abort // parsing. - if (numlines == 0 && !*buffer && spoolfile->eof()) { + if (numlines == 0 && !*buffer && spoolfile.eof()) { #ifdef MOVEMAIL_DEBUG fprintf(stderr, "*** Utterly empty spool file\n"); #endif @@ -492,21 +442,21 @@ nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, PL_strcpy(&buffer[PL_strlen(buffer)], MSG_LINEBREAK); newMailParser->HandleLine(buffer, PL_strlen(buffer)); - *outFileStream << buffer; + outFileStream << buffer; numlines++; - if (numlines == 1 && !spoolfile->eof()) { + if (numlines == 1 && !spoolfile.eof()) { PL_strcpy(buffer, "X-Mozilla-Status: 8000" MSG_LINEBREAK); newMailParser->HandleLine(buffer, PL_strlen(buffer)); - *outFileStream << buffer; + outFileStream << buffer; PL_strcpy(buffer, "X-Mozilla-Status2: 00000000" MSG_LINEBREAK); newMailParser->HandleLine(buffer, PL_strlen(buffer)); - *outFileStream << buffer; + outFileStream << buffer; } } @@ -514,24 +464,37 @@ nsMovemailService::GetNewMail(nsIMsgWindow *aMsgWindow, // END - outFileStream->flush(); // try this. + outFileStream.flush(); // try this. newMailParser->OnStopRequest(nsnull, nsnull, NS_OK); - if (outFileStream->is_open()) - outFileStream->close(); - delete outFileStream; - outFileStream = 0; + newMailParser->SetDBFolderStream(nsnull); // stream is going away + if (outFileStream.is_open()) + outFileStream.close(); // truncate the spool file here. nsFileSpec filespecForTrunc(wholeboxname.get()); - filespecForTrunc.Truncate(0); - if (spoolfile->is_open()) - spoolfile->close(); - delete spoolfile; + rv = filespecForTrunc.Truncate(0); + if (NS_FAILED(rv)) { + const PRUnichar *params[] = { + NS_ConvertUTF8toUCS2(wholeboxname).get() + }; + Error(MOVEMAIL_CANT_TRUNCATE_SPOOL_FILE, params, 1); + } + + if (spoolfile.is_open()) + spoolfile.close(); } freebuff_and_unlock: PR_Free(buffer); - YieldSpoolLock(wholeboxname.get()); + NS_IF_RELEASE(newMailParser); + if (!YieldSpoolLock(wholeboxname.get())) { + nsAutoString spoolLock = NS_ConvertUTF8toUCS2(wholeboxname); + spoolLock += NS_LITERAL_STRING(".lock"); + const PRUnichar *params[] = { + spoolLock.get() + }; + Error(MOVEMAIL_CANT_DELETE_LOCK, params, 1); + } } in_server->SetServerBusy(PR_FALSE);