NS_IMETHODIMP nsPop3Service::NewURI(const nsACString &aSpec, const char *aOriginCharset, // ignored nsIURI *aBaseURI, nsIURI **_retval) { nsresult rv = NS_ERROR_FAILURE; if (!_retval) return rv; nsCAutoString folderUri(aSpec); nsCOMPtr<nsIRDFResource> resource; PRInt32 offset = folderUri.Find("?"); if (offset != -1) folderUri.Truncate(offset); const nsCString &flatSpec = PromiseFlatCString(aSpec); const char *uidl = PL_strstr(flatSpec.get(), "uidl="); if (!uidl) return NS_ERROR_FAILURE; nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &rv)); if (NS_FAILED(rv)) return rv; rv = rdfService->GetResource(folderUri, getter_AddRefs(resource)); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsIMsgFolder> folder = do_QueryInterface(resource, &rv); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsIMsgIncomingServer> server; nsLocalFolderScanState folderScanState; nsCOMPtr<nsIMsgLocalMailFolder> localFolder = do_QueryInterface(folder); nsCOMPtr <nsIMailboxUrl> mailboxUrl = do_QueryInterface(aBaseURI); nsCOMPtr<nsIFileSpec> path; rv = folder->GetPath(getter_AddRefs(path)); if (NS_FAILED(rv)) return rv; nsFileSpec fileSpec; path->GetFileSpec(&fileSpec); folderScanState.m_fileSpec = &fileSpec; if (mailboxUrl && localFolder) { rv = localFolder->GetFolderScanState(&folderScanState); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr <nsIMsgDBHdr> msgHdr; nsCOMPtr<nsIMsgMessageUrl> msgUrl = do_QueryInterface(mailboxUrl, &rv); NS_ENSURE_SUCCESS(rv,rv); msgUrl->GetMessageHeader(getter_AddRefs(msgHdr)); // we do this to get the account key if (msgHdr) localFolder->GetUidlFromFolder(&folderScanState, msgHdr); if (!folderScanState.m_accountKey.IsEmpty()) { nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); if (accountManager) { nsCOMPtr <nsIMsgAccount> account; accountManager->GetAccount(folderScanState.m_accountKey.get(), getter_AddRefs(account)); if (account) account->GetIncomingServer(getter_AddRefs(server)); } } } if (!server) rv = folder->GetServer(getter_AddRefs(server)); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsIPop3IncomingServer> popServer = do_QueryInterface(server,&rv); if (NS_FAILED(rv)) return rv; nsXPIDLCString hostname; nsXPIDLCString username; server->GetHostName(getter_Copies(hostname)); server->GetUsername(getter_Copies(username)); PRInt32 port; server->GetPort(&port); if (port == -1) port = POP3_PORT; // we need to escape the username because it may contain // characters like / % or @ nsXPIDLCString escapedUsername; *((char **)getter_Copies(escapedUsername)) = nsEscape(username, url_XAlphas); nsCAutoString popSpec("pop://"); popSpec += escapedUsername; popSpec += "@"; popSpec += hostname; popSpec += ":"; popSpec.AppendInt(port); popSpec += "?"; popSpec += uidl; nsCOMPtr<nsIUrlListener> urlListener = do_QueryInterface(folder, &rv); if (NS_FAILED(rv)) return rv; rv = BuildPop3Url(popSpec.get(), folder, popServer, urlListener, _retval, nsnull); if (NS_SUCCEEDED(rv)) { nsCOMPtr<nsIMsgMailNewsUrl> mailnewsurl = do_QueryInterface(*_retval, &rv); if (NS_SUCCEEDED(rv)) { // escape the username before we call SetUsername(). we do this because GetUsername() // will unescape the username mailnewsurl->SetUsername(escapedUsername); } nsCOMPtr<nsIPop3URL> popurl = do_QueryInterface(mailnewsurl, &rv); if (NS_SUCCEEDED(rv)) { nsCAutoString messageUri (aSpec); messageUri.ReplaceSubstring("mailbox:", "mailbox-message:"); messageUri.ReplaceSubstring("?number=", "#"); offset = messageUri.Find("&"); if (offset != -1) messageUri.Truncate(offset); popurl->SetMessageUri(messageUri.get()); nsCOMPtr<nsIPop3Sink> pop3Sink; rv = popurl->GetPop3Sink(getter_AddRefs(pop3Sink)); if (NS_SUCCEEDED(rv)) pop3Sink->SetBuildMessageUri(PR_TRUE); } } return rv; }
NS_IMETHODIMP nsPop3Service::NewURI(const nsACString &aSpec, const char *aOriginCharset, // ignored nsIURI *aBaseURI, nsIURI **_retval) { NS_ENSURE_ARG_POINTER(_retval); nsCAutoString folderUri(aSpec); nsCOMPtr<nsIRDFResource> resource; PRInt32 offset = folderUri.FindChar('?'); if (offset != kNotFound) folderUri.SetLength(offset); const char *uidl = PL_strstr(nsCString(aSpec).get(), "uidl="); NS_ENSURE_TRUE(uidl, NS_ERROR_FAILURE); nsresult rv; nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &rv)); NS_ENSURE_SUCCESS(rv, rv); rv = rdfService->GetResource(folderUri, getter_AddRefs(resource)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIMsgFolder> folder = do_QueryInterface(resource, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIMsgIncomingServer> server; nsLocalFolderScanState folderScanState; nsCOMPtr<nsIMsgLocalMailFolder> localFolder = do_QueryInterface(folder); nsCOMPtr<nsIMailboxUrl> mailboxUrl = do_QueryInterface(aBaseURI); if (mailboxUrl && localFolder) { rv = localFolder->GetFolderScanState(&folderScanState); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIMsgDBHdr> msgHdr; nsMsgKey msgKey; mailboxUrl->GetMessageKey(&msgKey); folder->GetMessageHeader(msgKey, getter_AddRefs(msgHdr)); // we do this to get the account key if (msgHdr) localFolder->GetUidlFromFolder(&folderScanState, msgHdr); if (!folderScanState.m_accountKey.IsEmpty()) { nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); if (accountManager) { nsCOMPtr<nsIMsgAccount> account; accountManager->GetAccount(folderScanState.m_accountKey, getter_AddRefs(account)); if (account) account->GetIncomingServer(getter_AddRefs(server)); } } } if (!server) rv = folder->GetServer(getter_AddRefs(server)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIPop3IncomingServer> popServer = do_QueryInterface(server,&rv); NS_ENSURE_SUCCESS(rv, rv); nsCString hostname; nsCString username; server->GetHostName(hostname); server->GetUsername(username); PRInt32 port; server->GetPort(&port); if (port == -1) port = nsIPop3URL::DEFAULT_POP3_PORT; // we need to escape the username because it may contain // characters like / % or @ nsCString escapedUsername; MsgEscapeString(username, nsINetUtil::ESCAPE_XALPHAS, escapedUsername); nsCAutoString popSpec("pop://"); popSpec += escapedUsername; popSpec += "@"; popSpec += hostname; popSpec += ":"; popSpec.AppendInt(port); popSpec += "?"; popSpec += uidl; nsCOMPtr<nsIUrlListener> urlListener = do_QueryInterface(folder, &rv); NS_ENSURE_SUCCESS(rv, rv); rv = BuildPop3Url(popSpec.get(), folder, popServer, urlListener, _retval, nsnull); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIMsgMailNewsUrl> mailnewsurl = do_QueryInterface(*_retval, &rv); if (NS_SUCCEEDED(rv)) { // escape the username before we call SetUsername(). we do this because GetUsername() // will unescape the username mailnewsurl->SetUsername(escapedUsername); } nsCOMPtr<nsIPop3URL> popurl = do_QueryInterface(mailnewsurl, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCAutoString messageUri (aSpec); if (!strncmp(messageUri.get(), "mailbox:", 8)) messageUri.Replace(0, 8, "mailbox-message:"); offset = messageUri.Find("?number="); if (offset != kNotFound) messageUri.Replace(offset, 8, "#"); offset = messageUri.FindChar('&'); if (offset != kNotFound) messageUri.SetLength(offset); popurl->SetMessageUri(messageUri.get()); nsCOMPtr<nsIPop3Sink> pop3Sink; rv = popurl->GetPop3Sink(getter_AddRefs(pop3Sink)); NS_ENSURE_SUCCESS(rv, rv); pop3Sink->SetBuildMessageUri(true); return NS_OK; }