nsCOMPtr<nsIURL> url;
nsImapURI2Path(const char* rootURI, const char* uriStr, nsFileSpec& pathResult){  nsresult rv;    nsAutoString sbdSep;    rv = nsGetMailFolderSeparator(sbdSep);  if (NS_FAILED(rv))     return rv;    nsCAutoString uri(uriStr);  if (uri.Find(rootURI) != 0)     // if doesn't start with rootURI    return NS_ERROR_FAILURE;    if ((PL_strcmp(rootURI, kImapRootURI) != 0) &&    (PL_strcmp(rootURI, kImapMessageRootURI) != 0))   {    pathResult = nsnull;    rv = NS_ERROR_FAILURE;   }    // the server name is the first component of the path, so extract it out  PRInt32 hostStart;    hostStart = uri.FindChar('/');  if (hostStart <= 0) return NS_ERROR_FAILURE;    // skip past all //  while (uri.CharAt(hostStart) =='/') hostStart++;    // cut imap://[userid@]hostname/folder -> [userid@]hostname/folder  nsCAutoString hostname;  uri.Right(hostname, uri.Length() - hostStart);    nsCAutoString username;    PRInt32 atPos = hostname.FindChar('@');  if (atPos != -1) {    hostname.Left(username, atPos);    hostname.Cut(0, atPos+1);  }    nsCAutoString folder;  // folder comes after the hostname, after the '/'  // cut off first '/' and everything following it  // hostname/folder -> hostname  PRInt32 hostEnd = hostname.FindChar('/');  if (hostEnd > 0)   {    hostname.Right(folder, hostname.Length() - hostEnd - 1);    hostname.Truncate(hostEnd);  }    nsCOMPtr<nsIMsgIncomingServer> server;  nsCOMPtr<nsIMsgAccountManager> accountManager =     do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);  if(NS_FAILED(rv)) return rv;    char *unescapedUserName = ToNewCString(username);  if (unescapedUserName)  {    nsUnescape(unescapedUserName);    rv = accountManager->FindServer(unescapedUserName,      hostname.get(),      "imap",      getter_AddRefs(server));    PR_Free(unescapedUserName);  }  else    rv = NS_ERROR_OUT_OF_MEMORY;    if (NS_FAILED(rv)) return rv;    if (server)   {    nsCOMPtr<nsIFileSpec> localPath;    rv = server->GetLocalPath(getter_AddRefs(localPath));    if (NS_FAILED(rv)) return rv;        rv = localPath->GetFileSpec(&pathResult);    if (NS_FAILED(rv)) return rv;        // This forces the creation of the parent server directory    // so that we don't get imapservername.sbd instead    // when the host directory has been deleted. See bug 210683    nsFileSpec tempPath(pathResult.GetNativePathCString(), PR_TRUE);     pathResult.CreateDirectory();  }    if (NS_FAILED(rv))   {    pathResult = nsnull;    return rv;  }    if (!folder.IsEmpty())  {    nsCAutoString parentName = folder;    nsCAutoString leafName = folder;    PRInt32 dirEnd = parentName.FindChar('/');        while(dirEnd > 0)    {      parentName.Right(leafName, parentName.Length() - dirEnd -1);      parentName.Truncate(dirEnd);      NS_MsgHashIfNecessary(parentName);      parentName.AppendWithConversion(sbdSep);      pathResult += parentName.get();      // this fixes a strange purify warning.      parentName = leafName.get();      dirEnd = parentName.FindChar('/');    }    if (!leafName.IsEmpty())     {      NS_MsgHashIfNecessary(leafName);      pathResult += leafName.get();    }  }    return NS_OK;}