// static bool FetchUtil::ExtractHeader(nsACString::const_iterator& aStart, nsACString::const_iterator& aEnd, nsCString& aHeaderName, nsCString& aHeaderValue, bool* aWasEmptyHeader) { MOZ_ASSERT(aWasEmptyHeader); // Set it to a valid value here so we don't forget later. *aWasEmptyHeader = false; const char* beginning = aStart.get(); nsACString::const_iterator end(aEnd); if (!FindCRLF(aStart, end)) { return false; } if (aStart.get() == beginning) { *aWasEmptyHeader = true; return true; } nsAutoCString header(beginning, aStart.get() - beginning); nsACString::const_iterator headerStart, iter, headerEnd; header.BeginReading(headerStart); header.EndReading(headerEnd); iter = headerStart; if (!FindCharInReadable(':', iter, headerEnd)) { return false; } aHeaderName.Assign(StringHead(header, iter - headerStart)); aHeaderName.CompressWhitespace(); if (!NS_IsValidHTTPToken(aHeaderName)) { return false; } aHeaderValue.Assign(Substring(++iter, headerEnd)); if (!NS_IsReasonableHTTPHeaderValue(aHeaderValue)) { return false; } aHeaderValue.CompressWhitespace(); return PushOverLine(aStart, aEnd); }
void nsMediaFragmentURIParser::Parse() { nsCCharSeparatedTokenizer tokenizer(mHash, '&'); while (tokenizer.hasMoreTokens()) { const nsCSubstring& nv = tokenizer.nextToken(); PRInt32 index = nv.FindChar('='); if (index >= 0) { nsCAutoString name; nsCAutoString value; NS_UnescapeURL(StringHead(nv, index), esc_Ref | esc_AlwaysCopy, name); NS_UnescapeURL(Substring(nv, index + 1, nv.Length()), esc_Ref | esc_AlwaysCopy, value); nsAutoString a = NS_ConvertUTF8toUTF16(name); nsAutoString b = NS_ConvertUTF8toUTF16(value); mFragments.AppendElement(Pair(a, b)); } } }
nsresult nsAbMDBDirectory::GetAbDatabase() { if (mURI.IsEmpty()) return NS_ERROR_NOT_INITIALIZED; if (mDatabase) return NS_OK; nsresult rv; if (m_IsMailList) { // Get the database of the parent directory. nsCString parentURI(mURINoQuery); PRInt32 pos = parentURI.RFindChar('/'); // If we didn't find a / something really bad has happened if (pos == -1) return NS_ERROR_FAILURE; parentURI = StringHead(parentURI, pos); nsCOMPtr<nsIRDFService> rdfService = do_GetService("@mozilla.org/rdf/rdf-service;1", &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIRDFResource> resource; rv = rdfService->GetResource(parentURI, getter_AddRefs(resource)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIAbMDBDirectory> mdbDir(do_QueryInterface(resource, &rv)); NS_ENSURE_SUCCESS(rv, rv); rv = mdbDir->GetDatabase(getter_AddRefs(mDatabase)); } else rv = GetDatabase(getter_AddRefs(mDatabase)); if (NS_SUCCEEDED(rv)) rv = mDatabase->AddListener(this); return rv; }
nsresult nsAbMDBDirectory::GetAbDatabase() { if (mURI.IsEmpty()) return NS_ERROR_NOT_INITIALIZED; if (mDatabase) return NS_OK; nsresult rv; if (m_IsMailList) { // Get the database of the parent directory. nsAutoCString parentURI(mURINoQuery); int32_t pos = parentURI.RFindChar('/'); // If we didn't find a / something really bad has happened if (pos == -1) return NS_ERROR_FAILURE; parentURI = StringHead(parentURI, pos); nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIAbDirectory> directory; rv = abManager->GetDirectory(parentURI, getter_AddRefs(directory)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIAbMDBDirectory> mdbDir(do_QueryInterface(directory, &rv)); NS_ENSURE_SUCCESS(rv, rv); rv = mdbDir->GetDatabase(getter_AddRefs(mDatabase)); } else rv = GetDatabase(getter_AddRefs(mDatabase)); if (NS_SUCCEEDED(rv)) rv = mDatabase->AddListener(this); return rv; }
void nsEudoraWin32::ConvertPath( nsCString& str) { nsCString temp; nsCString path; PRInt32 idx = 0; PRInt32 start = 0; nsCString search; idx = str.FindChar( '\\', idx); if ((idx == 2) && (str.CharAt( 1) == ':')) { path = StringHead(str, 3); idx++; idx = str.FindChar( '\\', idx); start = 3; if ((idx == -1) && (str.Length() > 3)) path.Append(Substring(str, start)); } WIN32_FIND_DATA findFileData; while (idx != -1) { search = path; search.Append(Substring(str, start, idx - start)); HANDLE h = FindFirstFile( search.get(), &findFileData); if (h == INVALID_HANDLE_VALUE) return; path.Append( findFileData.cFileName); idx++; start = idx; idx = str.FindChar( '\\', idx); FindClose( h); if (idx != -1) path.Append( '\\'); else { path.Append( '\\'); path.Append(Substring(str, start)); } } str = path; }
nsresult nsSAXXMLReader::SplitExpatName(const PRUnichar *aExpatName, nsString &aURI, nsString &aLocalName, nsString &aQName) { /** * Adapted from RDFContentSinkImpl * * Expat can send the following: * localName * namespaceURI<separator>localName * namespaceURI<separator>localName<separator>prefix * * and we use 0xFFFF for the <separator>. * */ NS_ASSERTION(aExpatName, "null passed to handler"); nsDependentString expatStr(aExpatName); int32_t break1, break2 = kNotFound; break1 = expatStr.FindChar(PRUnichar(0xFFFF)); if (break1 == kNotFound) { aLocalName = expatStr; // no namespace aURI.Truncate(); aQName = expatStr; } else { aURI = StringHead(expatStr, break1); break2 = expatStr.FindChar(PRUnichar(0xFFFF), break1 + 1); if (break2 == kNotFound) { // namespace, but no prefix aLocalName = Substring(expatStr, break1 + 1); aQName = aLocalName; } else { // namespace with prefix aLocalName = Substring(expatStr, break1 + 1, break2 - break1 - 1); aQName = Substring(expatStr, break2 + 1) + NS_LITERAL_STRING(":") + aLocalName; } } return NS_OK; }
nsresult nsRDFXMLSerializer::RegisterQName(nsIRDFResource* aResource) { nsAutoCString uri, qname; aResource->GetValueUTF8(uri); nsNameSpaceMap::const_iterator iter = mNameSpaces.GetNameSpaceOf(uri); if (iter != mNameSpaces.last()) { NS_ENSURE_TRUE(iter->mPrefix, NS_ERROR_UNEXPECTED); iter->mPrefix->ToUTF8String(qname); qname.Append(':'); qname += StringTail(uri, uri.Length() - iter->mURI.Length()); mQNames.Put(aResource, qname); return NS_OK; } // Okay, so we don't have it in our map. Try to make one up. This // is very bogus. int32_t i = uri.RFindChar('#'); // first try a '#' if (i == -1) { i = uri.RFindChar('/'); if (i == -1) { // Okay, just punt and assume there is _no_ namespace on // this thing... mQNames.Put(aResource, uri); return NS_OK; } } // Take whatever is to the right of the '#' or '/' and call it the // local name, make up a prefix. nsCOMPtr<nsIAtom> prefix = EnsureNewPrefix(); mNameSpaces.Put(StringHead(uri, i+1), prefix); prefix->ToUTF8String(qname); qname.Append(':'); qname += StringTail(uri, uri.Length() - (i + 1)); mQNames.Put(aResource, qname); return NS_OK; }
nsresult nsEudoraWin32::FindTOCFile( nsIFile *pMailFile, nsIFile **ppTOCFile, bool *pDeleteToc) { nsresult rv; nsCAutoString leaf; *pDeleteToc = false; *ppTOCFile = nsnull; rv = pMailFile->GetNativeLeafName(leaf); if (NS_FAILED( rv)) return( rv); rv = pMailFile->GetParent( ppTOCFile); if (NS_FAILED( rv)) return( rv); nsCString name; if ((leaf.Length() > 4) && (leaf.CharAt( leaf.Length() - 4) == '.')) name = StringHead(leaf, leaf.Length() - 4); else name = leaf; name.Append( ".toc"); rv = (*ppTOCFile)->AppendNative(name); if (NS_FAILED( rv)) return( rv); bool exists = false; rv = (*ppTOCFile)->Exists( &exists); if (NS_FAILED( rv)) return( rv); bool isFile = false; rv = (*ppTOCFile)->IsFile( &isFile); if (NS_FAILED( rv)) return( rv); if (exists && isFile) return( NS_OK); return( NS_ERROR_FAILURE); }
void nsMediaFragmentURIParser::Parse(nsACString& aRef) { // Create an array of possibly-invalid media fragments. nsTArray< std::pair<nsCString, nsCString> > fragments; nsCCharSeparatedTokenizer tokenizer(aRef, '&'); while (tokenizer.hasMoreTokens()) { const nsCSubstring& nv = tokenizer.nextToken(); int32_t index = nv.FindChar('='); if (index >= 0) { nsAutoCString name; nsAutoCString value; NS_UnescapeURL(StringHead(nv, index), esc_Ref | esc_AlwaysCopy, name); NS_UnescapeURL(Substring(nv, index + 1, nv.Length()), esc_Ref | esc_AlwaysCopy, value); fragments.AppendElement(make_pair(name, value)); } } // Parse the media fragment values. bool gotTemporal = false, gotSpatial = false, gotResolution = false; for (int i = fragments.Length() - 1 ; i >= 0 ; --i) { if (gotTemporal && gotSpatial && gotResolution) { // We've got one of each possible type. No need to look at the rest. break; } else if (!gotTemporal && fragments[i].first.EqualsLiteral("t")) { nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second); gotTemporal = ParseNPT(nsDependentSubstring(value, 0)); } else if (!gotSpatial && fragments[i].first.EqualsLiteral("xywh")) { nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second); gotSpatial = ParseXYWH(nsDependentSubstring(value, 0)); } else if (!gotResolution && fragments[i].first.EqualsLiteral("-moz-resolution")) { nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second); gotResolution = ParseMozResolution(nsDependentSubstring(value, 0)); } } }
nsresult nsBeckyFilters::GetResendTarget(const nsCString &aLine, nsCString &aTemplate, nsCString &aTargetAddress) { nsresult rv; nsAutoCString target; rv = GetActionTarget(aLine, target); NS_ENSURE_SUCCESS(rv, rv); int32_t asteriskPosition = target.FindChar('*'); if (asteriskPosition < 0) { aTemplate.Assign(target); return NS_OK; } if (target.Length() == static_cast<uint32_t>(asteriskPosition)) return NS_ERROR_FAILURE; aTemplate.Assign(StringHead(target, asteriskPosition - 1)); aTargetAddress.Assign(Substring(target, asteriskPosition + 1)); return NS_OK; }
/* parses NewsMessageURI */ nsresult nsParseNewsMessageURI(const char* uri, nsCString& group, nsMsgKey *key) { NS_ENSURE_ARG_POINTER(uri); NS_ENSURE_ARG_POINTER(key); nsAutoCString uriStr(uri); int32_t keySeparator = uriStr.FindChar('#'); if(keySeparator != -1) { int32_t keyEndSeparator = MsgFindCharInSet(uriStr, "?&", keySeparator); // Grab between the last '/' and the '#' for the key group = StringHead(uriStr, keySeparator); int32_t groupSeparator = group.RFind("/"); if (groupSeparator == -1) return NS_ERROR_FAILURE; // Our string APIs don't let us unescape into the same buffer from earlier, // so escape into a temporary nsAutoCString unescapedGroup; MsgUnescapeString(Substring(group, groupSeparator + 1), 0, unescapedGroup); group = unescapedGroup; nsAutoCString keyStr; if (keyEndSeparator != -1) keyStr = Substring(uriStr, keySeparator + 1, keyEndSeparator - (keySeparator + 1)); else keyStr = Substring(uriStr, keySeparator + 1); nsresult errorCode; *key = keyStr.ToInteger(&errorCode); return errorCode; } return NS_ERROR_FAILURE; }
nsresult txExprParser::resolveQName(const nsAString& aQName, nsIAtom** aPrefix, txIParseContext* aContext, nsIAtom** aLocalName, int32_t& aNamespace, bool aIsNameTest) { aNamespace = kNameSpaceID_None; int32_t idx = aQName.FindChar(':'); if (idx > 0) { *aPrefix = NS_NewAtom(StringHead(aQName, (uint32_t)idx)).get(); if (!*aPrefix) { return NS_ERROR_OUT_OF_MEMORY; } *aLocalName = NS_NewAtom(Substring(aQName, (uint32_t)idx + 1, aQName.Length() - (idx + 1))).get(); if (!*aLocalName) { NS_RELEASE(*aPrefix); return NS_ERROR_OUT_OF_MEMORY; } return aContext->resolveNamespacePrefix(*aPrefix, aNamespace); } // the lexer dealt with idx == 0 *aPrefix = 0; if (aIsNameTest && aContext->caseInsensitiveNameTests()) { nsAutoString lcname; nsContentUtils::ASCIIToLower(aQName, lcname); *aLocalName = NS_NewAtom(lcname).get(); } else { *aLocalName = NS_NewAtom(aQName).get(); } if (!*aLocalName) { return NS_ERROR_OUT_OF_MEMORY; } return NS_OK; }
NS_IMETHODIMP nsScriptErrorBase::ToString(nsACString& /*UTF8*/ aResult) { static const char format0[] = "[%s: \"%s\" {file: \"%s\" line: %d column: %d source: \"%s\"}]"; static const char format1[] = "[%s: \"%s\" {file: \"%s\" line: %d}]"; static const char format2[] = "[%s: \"%s\"]"; static const char error[] = "JavaScript Error"; static const char warning[] = "JavaScript Warning"; const char* severity = !(mFlags & JSREPORT_WARNING) ? error : warning; char* temp; char* tempMessage = nullptr; char* tempSourceName = nullptr; char* tempSourceLine = nullptr; if (!mMessage.IsEmpty()) tempMessage = ToNewUTF8String(mMessage); if (!mSourceName.IsEmpty()) // Use at most 512 characters from mSourceName. tempSourceName = ToNewUTF8String(StringHead(mSourceName, 512)); if (!mSourceLine.IsEmpty()) // Use at most 512 characters from mSourceLine. tempSourceLine = ToNewUTF8String(StringHead(mSourceLine, 512)); if (nullptr != tempSourceName && nullptr != tempSourceLine) temp = JS_smprintf(format0, severity, tempMessage, tempSourceName, mLineNumber, mColumnNumber, tempSourceLine); else if (!mSourceName.IsEmpty()) temp = JS_smprintf(format1, severity, tempMessage, tempSourceName, mLineNumber); else temp = JS_smprintf(format2, severity, tempMessage); if (nullptr != tempMessage) free(tempMessage); if (nullptr != tempSourceName) free(tempSourceName); if (nullptr != tempSourceLine) free(tempSourceLine); if (!temp) return NS_ERROR_OUT_OF_MEMORY; aResult.Assign(temp); JS_smprintf_free(temp); return NS_OK; }
bool ParseHeader(nsACString::const_iterator& aStart, nsACString::const_iterator& aEnd, bool* aWasEmptyHeader) { MOZ_ASSERT(aWasEmptyHeader); // Set it to a valid value here so we don't forget later. *aWasEmptyHeader = false; const char* beginning = aStart.get(); nsACString::const_iterator end(aEnd); if (!FindCRLF(aStart, end)) { return false; } if (aStart.get() == beginning) { *aWasEmptyHeader = true; return true; } nsAutoCString header(beginning, aStart.get() - beginning); nsACString::const_iterator headerStart, headerEnd; header.BeginReading(headerStart); header.EndReading(headerEnd); if (!FindCharInReadable(':', headerStart, headerEnd)) { return false; } nsAutoCString headerName(StringHead(header, headerStart.size_backward())); headerName.CompressWhitespace(); if (!NS_IsValidHTTPToken(headerName)) { return false; } nsAutoCString headerValue(Substring(++headerStart, headerEnd)); if (!NS_IsReasonableHTTPHeaderValue(headerValue)) { return false; } headerValue.CompressWhitespace(); if (headerName.LowerCaseEqualsLiteral("content-disposition")) { nsCCharSeparatedTokenizer tokenizer(headerValue, ';'); bool seenFormData = false; while (tokenizer.hasMoreTokens()) { const nsDependentCSubstring& token = tokenizer.nextToken(); if (token.IsEmpty()) { continue; } if (token.EqualsLiteral("form-data")) { seenFormData = true; continue; } if (seenFormData && StringBeginsWith(token, NS_LITERAL_CSTRING("name="))) { mName = StringTail(token, token.Length() - 5); mName.Trim(" \""); continue; } if (seenFormData && StringBeginsWith(token, NS_LITERAL_CSTRING("filename="))) { mFilename = StringTail(token, token.Length() - 9); mFilename.Trim(" \""); continue; } } if (mName.IsVoid()) { // Could not parse a valid entry name. return false; } } else if (headerName.LowerCaseEqualsLiteral("content-type")) { mContentType = headerValue; } return true; }
/* parses MessageURI */ static nsresult ParseMessageURI(const char* uri, nsCString& folderURI, uint32_t *key, char **part) { if(!key) return NS_ERROR_NULL_POINTER; nsAutoCString uriStr(uri); int32_t folderEnd = -1; // if (StringBeginsWith(uriStr, NS_LITERAL_CSTRING("ews:-message"))) { uriStr.Replace(0, strlen("ews:-message"), nsCString("ews-message:")); } // imap-message uri's can have imap:// url strings tacked on the end, // e.g., when opening/saving attachments. We don't want to look for '#' // in that part of the uri, if the attachment name contains '#', // so check for that here. if (StringBeginsWith(uriStr, NS_LITERAL_CSTRING("ews-message"))) folderEnd = uriStr.Find("ews://"); int32_t keySeparator = MsgRFindChar(uriStr, '#', folderEnd); if(keySeparator != -1) { int32_t keyEndSeparator = mailews::MsgFindCharInSet(uriStr, "/?&", keySeparator); nsAutoString folderPath; folderURI = StringHead(uriStr, keySeparator); folderURI.Cut(3 /*strlen("ews")*/, 8 /*strlen("_message") */); // cut out the _message part of ews-message: // folder uri's don't have fully escaped usernames. int32_t atPos = folderURI.FindChar('@'); if (atPos != -1) { nsCString unescapedName, escapedName; int32_t userNamePos = folderURI.Find("//") + 2; uint32_t origUserNameLen = atPos - userNamePos; if (NS_SUCCEEDED(mailews::MsgUnescapeString(Substring(folderURI, userNamePos, origUserNameLen), 0, unescapedName))) { // Re-escape the username, matching the way we do it in uris, not the // way necko escapes urls. See nsMsgIncomingServer::GetServerURI. mailews::MsgEscapeString(unescapedName, nsINetUtil::ESCAPE_XALPHAS, escapedName); folderURI.Replace(userNamePos, origUserNameLen, escapedName); } } nsAutoCString keyStr; if (keyEndSeparator != -1) keyStr = Substring(uriStr, keySeparator + 1, keyEndSeparator - (keySeparator + 1)); else keyStr = Substring(uriStr, keySeparator + 1); *key = strtoul(keyStr.get(), nullptr, 10); if (part && keyEndSeparator != -1) { int32_t partPos = mailews::MsgFind(uriStr, "part=", false, keyEndSeparator); if (partPos != -1) { *part = ToNewCString(Substring(uriStr, keyEndSeparator)); } } } return NS_OK; }
static bool StringBeginsWithLowercaseLiteral(nsAString& aString, const char (&aSubstring)[N]) { return StringHead(aString, N).LowerCaseEqualsLiteral(aSubstring); }
/** Parses e.g. "a(href,title)" (but not several tags at once). */ nsresult mozSanitizingHTMLSerializer::ParseTagPref(const nsCAutoString& tagpref) { nsIParserService* parserService = nsContentUtils::GetParserService(); if (!parserService) return NS_ERROR_OUT_OF_MEMORY; // Parsing tag PRInt32 bracket = tagpref.FindChar('('); if (bracket == 0) { printf(" malformed pref: %s\n", tagpref.get()); return NS_ERROR_CANNOT_CONVERT_DATA; } nsAutoString tag; CopyUTF8toUTF16(StringHead(tagpref, bracket), tag); // Create key PRInt32 tag_id = parserService->HTMLStringTagToId(tag); if (tag_id == eHTMLTag_userdefined) { printf(" unknown tag <%s>, won't add.\n", NS_ConvertUTF16toUTF8(tag).get()); return NS_ERROR_CANNOT_CONVERT_DATA; } nsPRUint32Key tag_key(tag_id); if (mAllowedTags.Exists(&tag_key)) { printf(" duplicate tag: %s\n", NS_ConvertUTF16toUTF8(tag).get()); return NS_ERROR_CANNOT_CONVERT_DATA; } if (bracket == kNotFound) /* There are no attributes in the pref. So, allow none; only the tag itself */ { mAllowedTags.Put(&tag_key, 0); } else { // Attributes // where is the macro for non-fatal errors in opt builds? if(tagpref[tagpref.Length() - 1] != ')' || tagpref.Length() < PRUint32(bracket) + 3) { printf(" malformed pref: %s\n", tagpref.get()); return NS_ERROR_CANNOT_CONVERT_DATA; } nsCOMPtr<nsIProperties> attr_bag = do_CreateInstance(NS_PROPERTIES_CONTRACTID); NS_ENSURE_TRUE(attr_bag, NS_ERROR_INVALID_POINTER); nsCAutoString attrList; attrList.Append(Substring(tagpref, bracket + 1, tagpref.Length() - 2 - bracket)); char* attrs_lasts; for (char* iAttr = PL_strtok_r(attrList.BeginWriting(), ",", &attrs_lasts); iAttr; iAttr = PL_strtok_r(NULL, ",", &attrs_lasts)) { attr_bag->Set(iAttr, 0); } nsIProperties* attr_bag_raw = attr_bag; NS_ADDREF(attr_bag_raw); mAllowedTags.Put(&tag_key, attr_bag_raw); } return NS_OK; }
static int MimeInlineTextPlain_parse_line (const char *line, int32_t length, MimeObject *obj) { int status; bool quoting = ( obj->options && ( obj->options->format_out == nsMimeOutput::nsMimeMessageQuoting || obj->options->format_out == nsMimeOutput::nsMimeMessageBodyQuoting ) ); // see above bool plainHTML = quoting || (obj->options && obj->options->format_out == nsMimeOutput::nsMimeMessageSaveAs); // see above bool rawPlainText = obj->options && (obj->options->format_out == nsMimeOutput::nsMimeMessageFilterSniffer || obj->options->format_out == nsMimeOutput::nsMimeMessageAttach); // this routine gets called for every line of data that comes through the // mime converter. It's important to make sure we are efficient with // how we allocate memory in this routine. be careful if you go to add // more to this routine. NS_ASSERTION(length > 0, "zero length"); if (length <= 0) return 0; mozITXTToHTMLConv *conv = GetTextConverter(obj->options); MimeInlineTextPlain *text = (MimeInlineTextPlain *) obj; bool skipConversion = !conv || rawPlainText || (obj->options && obj->options->force_user_charset); char *mailCharset = NULL; nsresult rv; if (!skipConversion) { nsDependentCString inputStr(line, length); nsAutoString lineSourceStr; // For 'SaveAs', |line| is in |mailCharset|. // convert |line| to UTF-16 before 'html'izing (calling ScanTXT()) if (obj->options->format_out == nsMimeOutput::nsMimeMessageSaveAs) { // Get the mail charset of this message. MimeInlineText *inlinetext = (MimeInlineText *) obj; if (!inlinetext->initializeCharset) ((MimeInlineTextClass*)&mimeInlineTextClass)->initialize_charset(obj); mailCharset = inlinetext->charset; if (mailCharset && *mailCharset) { rv = nsMsgI18NConvertToUnicode(mailCharset, inputStr, lineSourceStr); NS_ENSURE_SUCCESS(rv, -1); } else // this probably never happens ... CopyUTF8toUTF16(inputStr, lineSourceStr); } else // line is in UTF-8 CopyUTF8toUTF16(inputStr, lineSourceStr); nsAutoCString prefaceResultStr; // Quoting stuff before the real text // Recognize quotes uint32_t oldCiteLevel = text->mCiteLevel; uint32_t logicalLineStart = 0; rv = conv->CiteLevelTXT(lineSourceStr.get(), &logicalLineStart, &(text->mCiteLevel)); NS_ENSURE_SUCCESS(rv, -1); // Find out, which recognitions to do uint32_t whattodo = obj->options->whattodo; if (plainHTML) { if (quoting) whattodo = 0; // This is done on Send. Don't do it twice. else whattodo = whattodo & ~mozITXTToHTMLConv::kGlyphSubstitution; /* Do recognition for the case, the result is viewed in Mozilla, but not GlyphSubstitution, because other UAs might not be able to display the glyphs. */ if (!text->mBlockquoting) text->mCiteLevel = 0; } // Write blockquote if (text->mCiteLevel > oldCiteLevel) { prefaceResultStr += "</pre>"; for (uint32_t i = 0; i < text->mCiteLevel - oldCiteLevel; i++) { nsAutoCString style; MimeTextBuildPrefixCSS(text->mQuotedSizeSetting, text->mQuotedStyleSetting, text->mCitationColor, style); if (!plainHTML && !style.IsEmpty()) { prefaceResultStr += "<blockquote type=cite style=\""; prefaceResultStr += style; prefaceResultStr += "\">"; } else prefaceResultStr += "<blockquote type=cite>"; } prefaceResultStr += "<pre wrap>\n"; } else if (text->mCiteLevel < oldCiteLevel) { prefaceResultStr += "</pre>"; for (uint32_t i = 0; i < oldCiteLevel - text->mCiteLevel; i++) prefaceResultStr += "</blockquote>"; prefaceResultStr += "<pre wrap>\n"; } // Write plain text quoting tags if (logicalLineStart != 0 && !(plainHTML && text->mBlockquoting)) { if (!plainHTML) prefaceResultStr += "<span class=\"moz-txt-citetags\">"; nsString citeTagsSource(StringHead(lineSourceStr, logicalLineStart)); // Convert to HTML nsString citeTagsResultUnichar; rv = conv->ScanTXT(citeTagsSource.get(), 0 /* no recognition */, getter_Copies(citeTagsResultUnichar)); if (NS_FAILED(rv)) return -1; prefaceResultStr.Append(NS_ConvertUTF16toUTF8(citeTagsResultUnichar)); if (!plainHTML) prefaceResultStr += "</span>"; } // recognize signature if ((lineSourceStr.Length() >= 4) && lineSourceStr.First() == '-' && Substring(lineSourceStr, 0, 3).EqualsLiteral("-- ") && (lineSourceStr[3] == '\r' || lineSourceStr[3] == '\n') ) { text->mIsSig = true; if (!quoting) prefaceResultStr += "<div class=\"moz-txt-sig\">"; } /* This is the main TXT to HTML conversion: escaping (very important), eventually recognizing etc. */ nsString lineResultUnichar; rv = conv->ScanTXT(lineSourceStr.get() + logicalLineStart, whattodo, getter_Copies(lineResultUnichar)); NS_ENSURE_SUCCESS(rv, -1); if (!(text->mIsSig && quoting && text->mStripSig)) { status = MimeObject_write(obj, prefaceResultStr.get(), prefaceResultStr.Length(), true); if (status < 0) return status; nsAutoCString outString; if (obj->options->format_out != nsMimeOutput::nsMimeMessageSaveAs || !mailCharset || !*mailCharset) CopyUTF16toUTF8(lineResultUnichar, outString); else { // convert back to mailCharset before writing. rv = nsMsgI18NConvertFromUnicode(mailCharset, lineResultUnichar, outString); NS_ENSURE_SUCCESS(rv, -1); } status = MimeObject_write(obj, outString.get(), outString.Length(), true); } else { status = 0; } } else { status = MimeObject_write(obj, line, length, true); } return status; }
/* * Evaluates this Expr based on the given context node and processor state * @param context the context node for evaluation of this Expr * @param ps the ContextState containing the stack information needed * for evaluation * @return the result of the evaluation */ nsresult txCoreFunctionCall::evaluate(txIEvalContext* aContext, txAExprResult** aResult) { *aResult = nsnull; if (!requireParams(descriptTable[mType].mMinParams, descriptTable[mType].mMaxParams, aContext)) { return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT; } nsresult rv = NS_OK; switch (mType) { case COUNT: { nsRefPtr<txNodeSet> nodes; rv = evaluateToNodeSet(mParams[0], aContext, getter_AddRefs(nodes)); NS_ENSURE_SUCCESS(rv, rv); return aContext->recycler()->getNumberResult(nodes->size(), aResult); } case ID: { nsRefPtr<txAExprResult> exprResult; rv = mParams[0]->evaluate(aContext, getter_AddRefs(exprResult)); NS_ENSURE_SUCCESS(rv, rv); nsRefPtr<txNodeSet> resultSet; rv = aContext->recycler()->getNodeSet(getter_AddRefs(resultSet)); NS_ENSURE_SUCCESS(rv, rv); txXPathTreeWalker walker(aContext->getContextNode()); if (exprResult->getResultType() == txAExprResult::NODESET) { txNodeSet* nodes = static_cast<txNodeSet*> (static_cast<txAExprResult*> (exprResult)); PRInt32 i; for (i = 0; i < nodes->size(); ++i) { nsAutoString idList; txXPathNodeUtils::appendNodeValue(nodes->get(i), idList); nsWhitespaceTokenizer tokenizer(idList); while (tokenizer.hasMoreTokens()) { if (walker.moveToElementById(tokenizer.nextToken())) { resultSet->add(walker.getCurrentPosition()); } } } } else { nsAutoString idList; exprResult->stringValue(idList); nsWhitespaceTokenizer tokenizer(idList); while (tokenizer.hasMoreTokens()) { if (walker.moveToElementById(tokenizer.nextToken())) { resultSet->add(walker.getCurrentPosition()); } } } *aResult = resultSet; NS_ADDREF(*aResult); return NS_OK; } case LAST: { return aContext->recycler()->getNumberResult(aContext->size(), aResult); } case LOCAL_NAME: case NAME: case NAMESPACE_URI: { // Check for optional arg nsRefPtr<txNodeSet> nodes; if (!mParams.IsEmpty()) { rv = evaluateToNodeSet(mParams[0], aContext, getter_AddRefs(nodes)); NS_ENSURE_SUCCESS(rv, rv); if (nodes->isEmpty()) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } } const txXPathNode& node = nodes ? nodes->get(0) : aContext->getContextNode(); switch (mType) { case LOCAL_NAME: { StringResult* strRes = nsnull; rv = aContext->recycler()->getStringResult(&strRes); NS_ENSURE_SUCCESS(rv, rv); *aResult = strRes; txXPathNodeUtils::getLocalName(node, strRes->mValue); return NS_OK; } case NAMESPACE_URI: { StringResult* strRes = nsnull; rv = aContext->recycler()->getStringResult(&strRes); NS_ENSURE_SUCCESS(rv, rv); *aResult = strRes; txXPathNodeUtils::getNamespaceURI(node, strRes->mValue); return NS_OK; } case NAME: { // XXX Namespace: namespaces have a name if (txXPathNodeUtils::isAttribute(node) || txXPathNodeUtils::isElement(node) || txXPathNodeUtils::isProcessingInstruction(node)) { StringResult* strRes = nsnull; rv = aContext->recycler()->getStringResult(&strRes); NS_ENSURE_SUCCESS(rv, rv); *aResult = strRes; txXPathNodeUtils::getNodeName(node, strRes->mValue); } else { aContext->recycler()->getEmptyStringResult(aResult); } return NS_OK; } default: { break; } } } case POSITION: { return aContext->recycler()->getNumberResult(aContext->position(), aResult); } // String functions case CONCAT: { nsRefPtr<StringResult> strRes; rv = aContext->recycler()->getStringResult(getter_AddRefs(strRes)); NS_ENSURE_SUCCESS(rv, rv); PRUint32 i, len = mParams.Length(); for (i = 0; i < len; ++i) { rv = mParams[i]->evaluateToString(aContext, strRes->mValue); NS_ENSURE_SUCCESS(rv, rv); } NS_ADDREF(*aResult = strRes); return NS_OK; } case CONTAINS: { nsAutoString arg2; rv = mParams[1]->evaluateToString(aContext, arg2); NS_ENSURE_SUCCESS(rv, rv); if (arg2.IsEmpty()) { aContext->recycler()->getBoolResult(PR_TRUE, aResult); } else { nsAutoString arg1; rv = mParams[0]->evaluateToString(aContext, arg1); NS_ENSURE_SUCCESS(rv, rv); aContext->recycler()->getBoolResult(FindInReadable(arg2, arg1), aResult); } return NS_OK; } case NORMALIZE_SPACE: { nsAutoString resultStr; if (!mParams.IsEmpty()) { rv = mParams[0]->evaluateToString(aContext, resultStr); NS_ENSURE_SUCCESS(rv, rv); } else { txXPathNodeUtils::appendNodeValue(aContext->getContextNode(), resultStr); } nsRefPtr<StringResult> strRes; rv = aContext->recycler()->getStringResult(getter_AddRefs(strRes)); NS_ENSURE_SUCCESS(rv, rv); MBool addSpace = MB_FALSE; MBool first = MB_TRUE; strRes->mValue.SetCapacity(resultStr.Length()); PRUnichar c; PRUint32 src; for (src = 0; src < resultStr.Length(); src++) { c = resultStr.CharAt(src); if (XMLUtils::isWhitespace(c)) { addSpace = MB_TRUE; } else { if (addSpace && !first) strRes->mValue.Append(PRUnichar(' ')); strRes->mValue.Append(c); addSpace = MB_FALSE; first = MB_FALSE; } } *aResult = strRes; NS_ADDREF(*aResult); return NS_OK; } case STARTS_WITH: { nsAutoString arg2; rv = mParams[1]->evaluateToString(aContext, arg2); NS_ENSURE_SUCCESS(rv, rv); PRBool result = PR_FALSE; if (arg2.IsEmpty()) { result = PR_TRUE; } else { nsAutoString arg1; rv = mParams[0]->evaluateToString(aContext, arg1); NS_ENSURE_SUCCESS(rv, rv); result = StringBeginsWith(arg1, arg2); } aContext->recycler()->getBoolResult(result, aResult); return NS_OK; } case STRING: { nsRefPtr<StringResult> strRes; rv = aContext->recycler()->getStringResult(getter_AddRefs(strRes)); NS_ENSURE_SUCCESS(rv, rv); if (!mParams.IsEmpty()) { rv = mParams[0]->evaluateToString(aContext, strRes->mValue); NS_ENSURE_SUCCESS(rv, rv); } else { txXPathNodeUtils::appendNodeValue(aContext->getContextNode(), strRes->mValue); } NS_ADDREF(*aResult = strRes); return NS_OK; } case STRING_LENGTH: { nsAutoString resultStr; if (!mParams.IsEmpty()) { rv = mParams[0]->evaluateToString(aContext, resultStr); NS_ENSURE_SUCCESS(rv, rv); } else { txXPathNodeUtils::appendNodeValue(aContext->getContextNode(), resultStr); } rv = aContext->recycler()->getNumberResult(resultStr.Length(), aResult); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; } case SUBSTRING: { nsAutoString src; rv = mParams[0]->evaluateToString(aContext, src); NS_ENSURE_SUCCESS(rv, rv); double start; rv = evaluateToNumber(mParams[1], aContext, &start); NS_ENSURE_SUCCESS(rv, rv); // check for NaN or +/-Inf if (Double::isNaN(start) || Double::isInfinite(start) || start >= src.Length() + 0.5) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } start = floor(start + 0.5) - 1; double end; if (mParams.Length() == 3) { rv = evaluateToNumber(mParams[2], aContext, &end); NS_ENSURE_SUCCESS(rv, rv); end += start; if (Double::isNaN(end) || end < 0) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } if (end > src.Length()) end = src.Length(); else end = floor(end + 0.5); } else { end = src.Length(); } if (start < 0) start = 0; if (start > end) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } return aContext->recycler()->getStringResult( Substring(src, (PRUint32)start, (PRUint32)(end - start)), aResult); } case SUBSTRING_AFTER: { nsAutoString arg1; rv = mParams[0]->evaluateToString(aContext, arg1); NS_ENSURE_SUCCESS(rv, rv); nsAutoString arg2; rv = mParams[1]->evaluateToString(aContext, arg2); NS_ENSURE_SUCCESS(rv, rv); if (arg2.IsEmpty()) { return aContext->recycler()->getStringResult(arg1, aResult); } PRInt32 idx = arg1.Find(arg2); if (idx == kNotFound) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } const nsSubstring& result = Substring(arg1, idx + arg2.Length()); return aContext->recycler()->getStringResult(result, aResult); } case SUBSTRING_BEFORE: { nsAutoString arg2; rv = mParams[1]->evaluateToString(aContext, arg2); NS_ENSURE_SUCCESS(rv, rv); if (arg2.IsEmpty()) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } nsAutoString arg1; rv = mParams[0]->evaluateToString(aContext, arg1); NS_ENSURE_SUCCESS(rv, rv); PRInt32 idx = arg1.Find(arg2); if (idx == kNotFound) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } return aContext->recycler()->getStringResult(StringHead(arg1, idx), aResult); } case TRANSLATE: { nsAutoString src; rv = mParams[0]->evaluateToString(aContext, src); NS_ENSURE_SUCCESS(rv, rv); if (src.IsEmpty()) { aContext->recycler()->getEmptyStringResult(aResult); return NS_OK; } nsRefPtr<StringResult> strRes; rv = aContext->recycler()->getStringResult(getter_AddRefs(strRes)); NS_ENSURE_SUCCESS(rv, rv); strRes->mValue.SetCapacity(src.Length()); nsAutoString oldChars, newChars; rv = mParams[1]->evaluateToString(aContext, oldChars); NS_ENSURE_SUCCESS(rv, rv); rv = mParams[2]->evaluateToString(aContext, newChars); NS_ENSURE_SUCCESS(rv, rv); PRUint32 i; PRInt32 newCharsLength = (PRInt32)newChars.Length(); for (i = 0; i < src.Length(); i++) { PRInt32 idx = oldChars.FindChar(src.CharAt(i)); if (idx != kNotFound) { if (idx < newCharsLength) strRes->mValue.Append(newChars.CharAt((PRUint32)idx)); } else { strRes->mValue.Append(src.CharAt(i)); } } NS_ADDREF(*aResult = strRes); return NS_OK; } // Number functions case NUMBER: { double res; if (!mParams.IsEmpty()) { rv = evaluateToNumber(mParams[0], aContext, &res); NS_ENSURE_SUCCESS(rv, rv); } else { nsAutoString resultStr; txXPathNodeUtils::appendNodeValue(aContext->getContextNode(), resultStr); res = Double::toDouble(resultStr); } return aContext->recycler()->getNumberResult(res, aResult); } case ROUND: { double dbl; rv = evaluateToNumber(mParams[0], aContext, &dbl); NS_ENSURE_SUCCESS(rv, rv); if (!Double::isNaN(dbl) && !Double::isInfinite(dbl)) { if (Double::isNeg(dbl) && dbl >= -0.5) { dbl *= 0; } else { dbl = floor(dbl + 0.5); } } return aContext->recycler()->getNumberResult(dbl, aResult); } case FLOOR: { double dbl; rv = evaluateToNumber(mParams[0], aContext, &dbl); NS_ENSURE_SUCCESS(rv, rv); if (!Double::isNaN(dbl) && !Double::isInfinite(dbl) && !(dbl == 0 && Double::isNeg(dbl))) { dbl = floor(dbl); } return aContext->recycler()->getNumberResult(dbl, aResult); } case CEILING: { double dbl; rv = evaluateToNumber(mParams[0], aContext, &dbl); NS_ENSURE_SUCCESS(rv, rv); if (!Double::isNaN(dbl) && !Double::isInfinite(dbl)) { if (Double::isNeg(dbl) && dbl > -1) { dbl *= 0; } else { dbl = ceil(dbl); } } return aContext->recycler()->getNumberResult(dbl, aResult); } case SUM: { nsRefPtr<txNodeSet> nodes; nsresult rv = evaluateToNodeSet(mParams[0], aContext, getter_AddRefs(nodes)); NS_ENSURE_SUCCESS(rv, rv); double res = 0; PRInt32 i; for (i = 0; i < nodes->size(); ++i) { nsAutoString resultStr; txXPathNodeUtils::appendNodeValue(nodes->get(i), resultStr); res += Double::toDouble(resultStr); } return aContext->recycler()->getNumberResult(res, aResult); } // Boolean functions case BOOLEAN: { PRBool result; nsresult rv = mParams[0]->evaluateToBool(aContext, result); NS_ENSURE_SUCCESS(rv, rv); aContext->recycler()->getBoolResult(result, aResult); return NS_OK; } case _FALSE: { aContext->recycler()->getBoolResult(PR_FALSE, aResult); return NS_OK; } case LANG: { txXPathTreeWalker walker(aContext->getContextNode()); nsAutoString lang; PRBool found; do { found = walker.getAttr(txXMLAtoms::lang, kNameSpaceID_XML, lang); } while (!found && walker.moveToParent()); if (!found) { aContext->recycler()->getBoolResult(PR_FALSE, aResult); return NS_OK; } nsAutoString arg; rv = mParams[0]->evaluateToString(aContext, arg); NS_ENSURE_SUCCESS(rv, rv); PRBool result = StringBeginsWith(lang, arg, txCaseInsensitiveStringComparator()) && (lang.Length() == arg.Length() || lang.CharAt(arg.Length()) == '-'); aContext->recycler()->getBoolResult(result, aResult); return NS_OK; } case _NOT: { PRBool result; rv = mParams[0]->evaluateToBool(aContext, result); NS_ENSURE_SUCCESS(rv, rv); aContext->recycler()->getBoolResult(!result, aResult); return NS_OK; } case _TRUE: { aContext->recycler()->getBoolResult(PR_TRUE, aResult); return NS_OK; } } aContext->receiveError(NS_LITERAL_STRING("Internal error"), NS_ERROR_UNEXPECTED); return NS_ERROR_UNEXPECTED; }
NS_IMETHODIMP nsAbMDBDirectory::Init(const char *aUri) { // We need to ensure that the m_DirPrefId is initialized properly nsDependentCString uri(aUri); if (uri.Find("MailList") != -1) m_IsMailList = true; // Mailing lists don't have their own prefs. if (m_DirPrefId.IsEmpty() && !m_IsMailList) { // Find the first ? (of the search params) if there is one. // We know we can start at the end of the moz-abmdbdirectory:// because // that's the URI we should have been passed. int32_t searchCharLocation = uri.FindChar('?', kMDBDirectoryRootLen); nsAutoCString filename; // extract the filename from the uri. if (searchCharLocation == -1) filename = Substring(uri, kMDBDirectoryRootLen); else filename = Substring(uri, kMDBDirectoryRootLen, searchCharLocation - kMDBDirectoryRootLen); // Get the pref servers and the address book directory branch nsresult rv; nsCOMPtr<nsIPrefService> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIPrefBranch> prefBranch; rv = prefService->GetBranch(NS_LITERAL_CSTRING(PREF_LDAP_SERVER_TREE_NAME ".").get(), getter_AddRefs(prefBranch)); NS_ENSURE_SUCCESS(rv, rv); char** childArray; uint32_t childCount, i; int32_t dotOffset; nsCString childValue; nsDependentCString child; rv = prefBranch->GetChildList("", &childCount, &childArray); NS_ENSURE_SUCCESS(rv, rv); for (i = 0; i < childCount; ++i) { child.Assign(childArray[i]); if (StringEndsWith(child, NS_LITERAL_CSTRING(".filename"))) { if (NS_SUCCEEDED(prefBranch->GetCharPref(child.get(), getter_Copies(childValue)))) { if (childValue == filename) { dotOffset = child.RFindChar('.'); if (dotOffset != -1) { nsAutoCString prefName(StringHead(child, dotOffset)); m_DirPrefId.AssignLiteral(PREF_LDAP_SERVER_TREE_NAME "."); m_DirPrefId.Append(prefName); } } } } } NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(childCount, childArray); NS_ASSERTION(!m_DirPrefId.IsEmpty(), "Error, Could not set m_DirPrefId in nsAbMDBDirectory::Init"); } return nsAbDirProperty::Init(aUri); }
bool nsMessengerUnixIntegration::BuildNotificationBody(nsIMsgDBHdr *aHdr, nsIStringBundle *aBundle, nsString &aBody) { nsAutoString alertBody; bool showPreview = true; bool showSubject = true; bool showSender = true; int32_t previewLength = SHOW_ALERT_PREVIEW_LENGTH_DEFAULT; nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID)); if (!prefBranch) return false; prefBranch->GetBoolPref(SHOW_ALERT_PREVIEW, &showPreview); prefBranch->GetBoolPref(SHOW_ALERT_SENDER, &showSender); prefBranch->GetBoolPref(SHOW_ALERT_SUBJECT, &showSubject); prefBranch->GetIntPref(SHOW_ALERT_PREVIEW_LENGTH, &previewLength); nsCOMPtr<nsIMsgHeaderParser> parser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID); if (!parser) return false; nsCOMPtr<nsIMsgFolder> folder; aHdr->GetFolder(getter_AddRefs(folder)); if (!folder) return false; nsCString msgURI; folder->GetUriForMsg(aHdr, msgURI); bool localOnly; uint32_t msgURIIndex = mFetchingURIs.IndexOf(msgURI); if (msgURIIndex == -1) { localOnly = false; mFetchingURIs.AppendElement(msgURI); } else localOnly = true; uint32_t messageKey; if (NS_FAILED(aHdr->GetMessageKey(&messageKey))) return false; bool asyncResult = false; nsresult rv = folder->FetchMsgPreviewText(&messageKey, 1, localOnly, this, &asyncResult); // If we're still waiting on getting the message previews, // bail early. We'll come back later when the async operation // finishes. if (NS_FAILED(rv) || asyncResult) return false; // If we got here, that means that we've retrieved the message preview, // so we can stop tracking it with our mFetchingURIs array. if (msgURIIndex != -1) mFetchingURIs.RemoveElementAt(msgURIIndex); nsCString utf8previewString; if (showPreview && NS_FAILED(aHdr->GetStringProperty("preview", getter_Copies(utf8previewString)))) return false; // need listener that mailbox is remote such as IMAP // to generate preview message nsString previewString; CopyUTF8toUTF16(utf8previewString, previewString); nsString subject; if (showSubject && NS_FAILED(aHdr->GetMime2DecodedSubject(subject))) return false; nsString author; if (showSender) { if (NS_FAILED(aHdr->GetMime2DecodedAuthor(author))) return false; PRUnichar **emails; PRUnichar **names; PRUnichar **fullnames; uint32_t num; if (NS_FAILED(parser->ParseHeadersWithArray(author.get(), &emails, &names, &fullnames, &num))) return false; if (num > 0) { author.Assign(names[0] ? names[0] : emails[0]); NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(num, emails); NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(num, names); NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(num, fullnames); } } if (showSubject && showSender) { nsString msgTitle; const PRUnichar *formatStrings[] = { subject.get(), author.get() }; aBundle->FormatStringFromName(NS_LITERAL_STRING("newMailNotification_messagetitle").get(), formatStrings, 2, getter_Copies(msgTitle)); alertBody.Append(msgTitle); } else if (showSubject) alertBody.Append(subject); else if (showSender) alertBody.Append(author); if (showPreview && (showSubject || showSender)) { alertBody.AppendLiteral("\n"); } if (showPreview) alertBody.Append(StringHead(previewString, previewLength)); if (alertBody.IsEmpty()) return false; aBody.Assign(alertBody); return true; }
static void ProxiedAuthCallback(gconstpointer in, gsize in_size, gpointer out, gsize out_size, gpointer callback_data) { GnomeVFSModuleCallbackAuthenticationIn *authIn = (GnomeVFSModuleCallbackAuthenticationIn *) in; GnomeVFSModuleCallbackAuthenticationOut *authOut = (GnomeVFSModuleCallbackAuthenticationOut *) out; LOG(("gnomevfs: ProxiedAuthCallback [uri=%s]\n", authIn->uri)); // Without a channel, we have no way of getting a prompter. nsIChannel *channel = (nsIChannel *) callback_data; if (!channel) return; nsCOMPtr<nsIAuthPrompt> prompt; NS_QueryNotificationCallbacks(channel, prompt); // If no auth prompt, then give up. We could failover to using the // WindowWatcher service, but that might defeat a consumer's purposeful // attempt to disable authentication (for whatever reason). if (!prompt) return; // Parse out the host and port... nsCOMPtr<nsIURI> uri; channel->GetURI(getter_AddRefs(uri)); if (!uri) return; #ifdef DEBUG { // // Make sure authIn->uri is consistent with the channel's URI. // // XXX This check is probably not IDN safe, and it might incorrectly // fire as a result of escaping differences. It's unclear what // kind of transforms GnomeVFS might have applied to the URI spec // that we originally gave to it. In spite of the likelihood of // false hits, this check is probably still valuable. // nsAutoCString spec; uri->GetSpec(spec); int uriLen = strlen(authIn->uri); if (!StringHead(spec, uriLen).Equals(nsDependentCString(authIn->uri, uriLen))) { LOG(("gnomevfs: [spec=%s authIn->uri=%s]\n", spec.get(), authIn->uri)); NS_ERROR("URI mismatch"); } } #endif nsAutoCString scheme, hostPort; uri->GetScheme(scheme); uri->GetHostPort(hostPort); // It doesn't make sense for either of these strings to be empty. What kind // of funky URI is this? if (scheme.IsEmpty() || hostPort.IsEmpty()) return; // Construct the single signon key. Altering the value of this key will // cause people's remembered passwords to be forgotten. Think carefully // before changing the way this key is constructed. nsAutoString key, realm; NS_ConvertUTF8toUTF16 dispHost(scheme); dispHost.AppendLiteral("://"); dispHost.Append(NS_ConvertUTF8toUTF16(hostPort)); key = dispHost; if (authIn->realm) { // We assume the realm string is ASCII. That might be a bogus assumption, // but we have no idea what encoding GnomeVFS is using, so for now we'll // limit ourselves to ISO-Latin-1. XXX What is a better solution? realm.Append('"'); realm.Append(NS_ConvertASCIItoUTF16(authIn->realm)); realm.Append('"'); key.Append(' '); key.Append(realm); } // Construct the message string... // // We use Necko's string bundle here. This code really should be encapsulated // behind some Necko API, after all this code is based closely on the code in // nsHttpChannel.cpp. nsCOMPtr<nsIStringBundleService> bundleSvc = do_GetService(NS_STRINGBUNDLE_CONTRACTID); if (!bundleSvc) return; nsCOMPtr<nsIStringBundle> bundle; bundleSvc->CreateBundle("chrome://global/locale/commonDialogs.properties", getter_AddRefs(bundle)); if (!bundle) return; nsString message; if (!realm.IsEmpty()) { const char16_t *strings[] = { realm.get(), dispHost.get() }; bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordForRealm"), strings, 2, getter_Copies(message)); } else { const char16_t *strings[] = { dispHost.get() }; bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordFor"), strings, 1, getter_Copies(message)); } if (message.IsEmpty()) return; // Prompt the user... nsresult rv; bool retval = false; char16_t *user = nullptr, *pass = nullptr; rv = prompt->PromptUsernameAndPassword(nullptr, message.get(), key.get(), nsIAuthPrompt::SAVE_PASSWORD_PERMANENTLY, &user, &pass, &retval); if (NS_FAILED(rv)) return; if (!retval || !user || !pass) return; // XXX We need to convert the UTF-16 username and password from our dialog to // strings that GnomeVFS can understand. It's unclear what encoding GnomeVFS // expects, so for now we assume 7-bit ASCII. Hopefully, we can get a better // solution at some point. // One copy is never enough... authOut->username = g_strdup(NS_LossyConvertUTF16toASCII(user).get()); authOut->password = g_strdup(NS_LossyConvertUTF16toASCII(pass).get()); nsMemory::Free(user); nsMemory::Free(pass); }
NS_IMETHODIMP nsAbManager::DeleteAddressBook(const nsACString &aURI) { // Find the address book nsresult rv; nsCOMPtr<nsIAbDirectory> directory; rv = GetDirectory(aURI, getter_AddRefs(directory)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIAbDirectory> rootDirectory; rv = GetRootDirectory(getter_AddRefs(rootDirectory)); NS_ENSURE_SUCCESS(rv, rv); // Go through each of the children of the address book // (so, the mailing lists) and remove their entries from // the look up table. nsCOMPtr<nsISimpleEnumerator> enumerator; rv = directory->GetChildNodes(getter_AddRefs(enumerator)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsISupports> item; nsCOMPtr<nsIAbDirectory> childDirectory; bool hasMore = false; while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) { rv = enumerator->GetNext(getter_AddRefs(item)); NS_ENSURE_SUCCESS(rv, rv); childDirectory = do_QueryInterface(item, &rv); if (NS_SUCCEEDED(rv)) { nsCString childURI; rv = childDirectory->GetURI(childURI); NS_ENSURE_SUCCESS(rv, rv); mAbStore.Remove(childURI); } } mAbStore.Remove(aURI); bool isMailList; rv = directory->GetIsMailList(&isMailList); NS_ENSURE_SUCCESS(rv, rv); if (!isMailList) // If we're not a mailing list, then our parent // must be the root address book directory. return rootDirectory->DeleteDirectory(directory); nsCString parentUri; parentUri.Append(aURI); PRInt32 pos = parentUri.RFindChar('/'); // If we didn't find a /, we're in trouble. if (pos == -1) return NS_ERROR_FAILURE; parentUri = StringHead(parentUri, pos); nsCOMPtr<nsIAbDirectory> parentDirectory; rv = GetDirectory(parentUri, getter_AddRefs(parentDirectory)); NS_ENSURE_SUCCESS(rv, rv); return parentDirectory->DeleteDirectory(directory); }
bool CAliasData::Process(const char *pLine, int32_t len) { // Extract any comments first! nsCString str; const char *pStart = pLine; int32_t tCnt = 0; int32_t cnt = 0; int32_t max = len; bool endCollect = false; // Keep track of the full entry without any processing for potential alias // nickname resolution. Previously alias resolution was done with m_email, // but unfortunately that doesn't work for nicknames with spaces. // For example for the nickname "Joe Smith", "Smith" was being interpreted // as the potential email address and placed in m_email, but routines like // ResolveAlias were failing because "Smith" is not the full nickname. // Now we just stash the full entry for nickname resolution before processing // the line as a potential entry in its own right. m_fullEntry.Append(pLine, len); while (max) { if (*pLine == '"') { if (tCnt && !endCollect) { str.Trim(kWhitespace); if (!str.IsEmpty()) str.Append(" ", 1); str.Append(pStart, tCnt); } cnt = nsEudoraAddress::CountQuote(pLine, max); if ((cnt > 2) && m_realName.IsEmpty()) { m_realName.Append(pLine + 1, cnt - 2); } pLine += cnt; max -= cnt; pStart = pLine; tCnt = 0; } else if (*pLine == '<') { if (tCnt && !endCollect) { str.Trim(kWhitespace); if (!str.IsEmpty()) str.Append(" ", 1); str.Append(pStart, tCnt); } cnt = nsEudoraAddress::CountAngle(pLine, max); if ((cnt > 2) && m_email.IsEmpty()) { m_email.Append(pLine + 1, cnt - 2); } pLine += cnt; max -= cnt; pStart = pLine; tCnt = 0; endCollect = true; } else if (*pLine == '(') { if (tCnt && !endCollect) { str.Trim(kWhitespace); if (!str.IsEmpty()) str.Append(" ", 1); str.Append(pStart, tCnt); } cnt = nsEudoraAddress::CountComment(pLine, max); if (cnt > 2) { if (!m_realName.IsEmpty() && m_nickName.IsEmpty()) m_nickName = m_realName; m_realName.Truncate(); m_realName.Append(pLine + 1, cnt - 2); } pLine += cnt; max -= cnt; pStart = pLine; tCnt = 0; } else { tCnt++; pLine++; max--; } } if (tCnt) { str.Trim(kWhitespace); if (!str.IsEmpty()) str.Append(" ", 1); str.Append(pStart, tCnt); } str.Trim(kWhitespace); if (!m_realName.IsEmpty() && !m_email.IsEmpty()) return true; // now we should have a string with any remaining non-delimitted text // we assume that the last token is the email // anything before that is realName if (!m_email.IsEmpty()) { m_realName = str; return true; } tCnt = str.RFindChar(' '); if (tCnt == -1) { if (!str.IsEmpty()) { m_email = str; return true; } return false; } m_email = Substring(str, tCnt + 1); m_realName = StringHead(str, tCnt); m_realName.Trim(kWhitespace); m_email.Trim(kWhitespace); return !m_email.IsEmpty(); }
bool nsEudoraWin32::FindMimeIniFile( nsIFile *pFile) { bool hasMore; nsCOMPtr<nsISimpleEnumerator> directoryEnumerator; nsresult rv = pFile->GetDirectoryEntries(getter_AddRefs(directoryEnumerator)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr <nsILocalFile> pLocalFile = do_QueryInterface(pFile, &rv); NS_ENSURE_SUCCESS(rv, rv); directoryEnumerator->HasMoreElements(&hasMore); bool isFile; nsCOMPtr<nsIFile> entry; nsCString fName; nsCString ext; nsCString name; bool found = false; while (hasMore && NS_SUCCEEDED(rv)) { nsCOMPtr<nsISupports> aSupport; rv = directoryEnumerator->GetNext(getter_AddRefs(aSupport)); nsCOMPtr<nsILocalFile> entry(do_QueryInterface(aSupport, &rv)); directoryEnumerator->HasMoreElements(&hasMore); if (NS_SUCCEEDED( rv)) { rv = entry->GetNativeLeafName(fName); if (NS_SUCCEEDED( rv) && !fName.IsEmpty()) { if (fName.Length() > 4) { ext = StringTail(fName, 4); name = StringHead(fName, fName.Length() - 4); } else { ext.Truncate(); name = fName; } ToLowerCase(ext); if (ext.EqualsLiteral(".ini")) { isFile = false; entry->IsFile( &isFile); if (isFile) { if (found) { // which one of these files is newer? PRInt64 modDate1, modDate2; entry->GetLastModifiedTime( &modDate2); pFile->GetLastModifiedTime( &modDate1); if (modDate2 > modDate1) pLocalFile->InitWithFile( entry); } else { pLocalFile->InitWithFile( entry); found = true; } } } } } } return found; }
nsresult nsEudoraWin32::FindAddressBooks( nsIFile *pRoot, nsISupportsArray **ppArray) { nsresult rv; nsCOMPtr<nsILocalFile> file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsILocalFile> localRoot = do_QueryInterface(pRoot); rv = file->InitWithFile( localRoot); NS_ENSURE_SUCCESS(rv, rv); rv = NS_NewISupportsArray( ppArray); if (NS_FAILED( rv)) { IMPORT_LOG0( "FAILED to allocate the nsISupportsArray\n"); return( rv); } nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv)); if (NS_FAILED( rv)) return( rv); m_addressImportFolder = pRoot; nsString displayName; nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_NICKNAMES_NAME, displayName); // First off, get the default nndbase.txt, then scan the default nicknames subdir, // then look in the .ini file for additional directories to scan for address books! bool exists = false; bool isFile = false; rv = file->AppendNative(NS_LITERAL_CSTRING("nndbase.txt")); bool checkedBoth = false; do { if (NS_SUCCEEDED(rv)) rv = file->Exists(&exists); if (NS_SUCCEEDED(rv) && exists) rv = file->IsFile(&isFile); // Stop if we already checked the second default name possibility or // if we found the default address book name. if (checkedBoth || (exists && isFile)) break; // Check for alternate file extension ".nnt" which Windows Eudora uses as an option // to hide from simple minded viruses that scan ".txt" files for addresses. rv = file->SetNativeLeafName(NS_LITERAL_CSTRING("nndbase.nnt")); checkedBoth = true; } while (NS_SUCCEEDED(rv)); if (exists && isFile) { if (NS_FAILED( rv = FoundAddressBook( file, displayName.get(), *ppArray, impSvc))) return( rv); } // Try the default directory rv = rv = file->InitWithFile( localRoot); if (NS_FAILED( rv)) return( rv); rv = file->AppendNative(NS_LITERAL_CSTRING("Nickname")); bool isDir = false; exists = false; if (NS_SUCCEEDED( rv)) rv = file->Exists( &exists); if (NS_SUCCEEDED( rv) && exists) rv = file->IsDirectory( &isDir); if (exists && isDir) { if (NS_FAILED( rv = ScanAddressDir(file, *ppArray, impSvc))) return( rv); } // Try the ini file to find other directories! rv = rv = file->InitWithFile( localRoot); if (NS_FAILED( rv)) return( rv); rv = file->AppendNative(NS_LITERAL_CSTRING("eudora.ini")); exists = false; isFile = false; if (NS_SUCCEEDED( rv)) rv = file->Exists( &exists); if (NS_SUCCEEDED( rv) && exists) rv = file->IsFile( &isFile); if (!isFile || !exists) { rv = file->InitWithFile( localRoot); if (NS_FAILED( rv)) return( NS_OK); if (!FindMimeIniFile(file)) return( NS_OK); } nsCString fileName; file->GetNativePath(fileName); // This is the supposed ini file name! // Get the extra directories for nicknames and parse it for valid nickname directories // to look into... char *pBuffer = new char[2048]; DWORD len = ::GetPrivateProfileString( "Settings", "ExtraNicknameDirs", "", pBuffer, 2048, fileName.get()); if (len == 2047) { // If the value is really that large then don't bother! delete [] pBuffer; return( NS_OK); } nsCString dirs(pBuffer); delete [] pBuffer; dirs.Trim( kWhitespace); PRInt32 idx = 0; nsCString currentDir; while ((idx = dirs.FindChar( ';')) != -1) { currentDir = StringHead(dirs, idx); currentDir.Trim( kWhitespace); if (!currentDir.IsEmpty()) { rv = file->InitWithNativePath(currentDir); exists = false; isDir = false; if (NS_SUCCEEDED( rv)) rv = file->Exists( &exists); if (NS_SUCCEEDED( rv) && exists) rv = file->IsDirectory( &isDir); if (exists && isDir) { if (NS_FAILED( rv = ScanAddressDir(file, *ppArray, impSvc))) return( rv); } } dirs = Substring(dirs, idx + 1); dirs.Trim( kWhitespace); } if (!dirs.IsEmpty()) { rv = file->InitWithNativePath(dirs); exists = false; isDir = false; if (NS_SUCCEEDED( rv)) rv = file->Exists( &exists); if (NS_SUCCEEDED( rv) && exists) rv = file->IsDirectory( &isDir); if (exists && isDir) { if (NS_FAILED( rv = ScanAddressDir(file, *ppArray, impSvc))) return( rv); } } return( NS_OK); }
nsresult nsEudoraWin32::IterateMailDir( nsIFile *pFolder, nsISupportsArray *pArray, nsIImportService *pImport) { bool hasMore; nsCOMPtr<nsISimpleEnumerator> directoryEnumerator; nsresult rv = pFolder->GetDirectoryEntries(getter_AddRefs(directoryEnumerator)); NS_ENSURE_SUCCESS(rv, rv); directoryEnumerator->HasMoreElements(&hasMore); bool isFolder; bool isFile; nsCOMPtr<nsIFile> entry; nsCString fName; nsCString ext; nsCString name; while (hasMore && NS_SUCCEEDED(rv)) { nsCOMPtr<nsISupports> aSupport; rv = directoryEnumerator->GetNext(getter_AddRefs(aSupport)); nsCOMPtr<nsILocalFile> entry(do_QueryInterface(aSupport, &rv)); directoryEnumerator->HasMoreElements(&hasMore); if (NS_SUCCEEDED( rv)) { rv = entry->GetNativeLeafName(fName); if (NS_SUCCEEDED( rv) && !fName.IsEmpty()) { if (fName.Length() > 4) { ext = StringTail(fName, 4); name = StringHead(fName, fName.Length() - 4); } else { ext.Truncate(); name = fName; } ToLowerCase(ext); if (ext.EqualsLiteral(".fol")) { isFolder = false; entry->IsDirectory( &isFolder); if (isFolder) { // add the folder rv = FoundMailFolder( entry, name.get(), pArray, pImport); if (NS_SUCCEEDED( rv)) { rv = ScanMailDir( entry, pArray, pImport); if (NS_FAILED( rv)) IMPORT_LOG0( "*** Error scanning mail directory\n"); } } } else if (ext.EqualsLiteral(".mbx")) { isFile = false; entry->IsFile( &isFile); if (isFile) rv = FoundMailbox( entry, name.get(), pArray, pImport); } } } } return( rv); }