void CMapiApi::ReportLongProp( const char *pTag, LPSPropValue pVal) { if ( pVal && (PROP_TYPE( pVal->ulPropTag) == PT_LONG)) { nsCString num; nsCString num2; num.AppendInt( (PRInt32) pVal->Value.l); num2.AppendInt( (PRInt32) pVal->Value.l, 16); MAPI_TRACE3( "%s %s, 0x%s\n", pTag, num, num2); } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_NULL)) { MAPI_TRACE1( "%s {NULL}\n", pTag); } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_ERROR)) { MAPI_TRACE1( "%s {Error retrieving property}\n", pTag); } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_ERROR)) { MAPI_TRACE1( "%s {Error retrieving property}\n", pTag); } else { MAPI_TRACE1( "%s invalid value, expecting long\n", pTag); } if (pVal) MAPIFreeBuffer( pVal); }
void CMapiApi::ReportUIDProp( const char *pTag, LPSPropValue pVal) { if ( pVal && (PROP_TYPE( pVal->ulPropTag) == PT_BINARY)) { if (pVal->Value.bin.cb != 16) { MAPI_TRACE1( "%s - INVALID, expecting 16 bytes of binary data for UID\n", pTag); } else { nsIID uid; memcpy( &uid, pVal->Value.bin.lpb, 16); char * pStr = uid.ToString(); if (pStr) { MAPI_TRACE2( "%s %s\n", pTag, (const char *)pStr); NS_Free( pStr); } } } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_NULL)) { MAPI_TRACE1( "%s {NULL}\n", pTag); } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_ERROR)) { MAPI_TRACE1( "%s {Error retrieving property}\n", pTag); } else { MAPI_TRACE1( "%s invalid value, expecting binary\n", pTag); } if (pVal) MAPIFreeBuffer( pVal); }
BOOL CMapiApi::HandleContentsItem( ULONG oType, ULONG cb, LPENTRYID pEntry) { if (oType == MAPI_MESSAGE) { LPMESSAGE pMsg; if (OpenEntry( cb, pEntry, (LPUNKNOWN *) &pMsg)) { LPSPropValue pVal; pVal = GetMapiProperty( pMsg, PR_SUBJECT); ReportStringProp( "PR_SUBJECT:", pVal); pVal = GetMapiProperty( pMsg, PR_DISPLAY_BCC); ReportStringProp( "PR_DISPLAY_BCC:", pVal); pVal = GetMapiProperty( pMsg, PR_DISPLAY_CC); ReportStringProp( "PR_DISPLAY_CC:", pVal); pVal = GetMapiProperty( pMsg, PR_DISPLAY_TO); ReportStringProp( "PR_DISPLAY_TO:", pVal); pVal = GetMapiProperty( pMsg, PR_MESSAGE_CLASS); ReportStringProp( "PR_MESSAGE_CLASS:", pVal); ListProperties( pMsg); pMsg->Release(); } else { MAPI_TRACE0( " Folder type - error opening\n"); } } else MAPI_TRACE1( " ObjectType: %ld\n", oType); return( TRUE); }
BOOL CMapiApi::GetStringFromProp( LPSPropValue pVal, nsString& val, BOOL delVal) { BOOL bResult = TRUE; if ( pVal && (PROP_TYPE( pVal->ulPropTag) == PT_STRING8)) { CStrToUnicode( (const char *)pVal->Value.lpszA, val); } else if ( pVal && (PROP_TYPE( pVal->ulPropTag) == PT_UNICODE)) { val = (PRUnichar *) pVal->Value.lpszW; } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_NULL)) { val.Truncate(); } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_ERROR)) { val.Truncate(); bResult = FALSE; } else { if (pVal) { MAPI_TRACE1( "GetStringFromProp: invalid value, expecting string - %d\n", (int) PROP_TYPE( pVal->ulPropTag)); } else { MAPI_TRACE0( "GetStringFromProp: invalid value, expecting string, got null pointer\n"); } val.Truncate(); bResult = FALSE; } if (pVal && delVal) MAPIFreeBuffer( pVal); return( bResult); }
bool CMapiMessage::CopyBinAttachToFile(LPATTACH lpAttach, nsIFile **tmp_file) { nsCOMPtr<nsIFile> _tmp_file; nsresult rv = GetSpecialDirectoryWithFileName(NS_OS_TEMP_DIR, "mapiattach.tmp", getter_AddRefs(_tmp_file)); NS_ENSURE_SUCCESS(rv, false); rv = _tmp_file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 00600); NS_ENSURE_SUCCESS(rv, false); nsCString tmpPath; _tmp_file->GetNativePath(tmpPath); LPSTREAM lpStreamFile; HRESULT hr = CMapiApi::OpenStreamOnFile(gpMapiAllocateBuffer, gpMapiFreeBuffer, STGM_READWRITE | STGM_CREATE, const_cast<char*>(tmpPath.get()), NULL, &lpStreamFile); if (HR_FAILED(hr)) { MAPI_TRACE1("~~ERROR~~ OpenStreamOnFile failed - temp path: %s\r\n", tmpPath.get()); return false; } bool bResult = true; LPSTREAM lpAttachStream; hr = lpAttach->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, 0, 0, (LPUNKNOWN *)&lpAttachStream); if (HR_FAILED(hr)) { MAPI_TRACE0("~~ERROR~~ OpenProperty failed for PR_ATTACH_DATA_BIN.\r\n"); lpAttachStream = NULL; bResult = false; } else { STATSTG st; hr = lpAttachStream->Stat(&st, STATFLAG_NONAME); if (HR_FAILED(hr)) { MAPI_TRACE0("~~ERROR~~ Stat failed for attachment stream\r\n"); bResult = false; } else { hr = lpAttachStream->CopyTo(lpStreamFile, st.cbSize, NULL, NULL); if (HR_FAILED(hr)) { MAPI_TRACE0("~~ERROR~~ Attach Stream CopyTo temp file failed.\r\n"); bResult = false; } } } if (lpAttachStream) lpAttachStream->Release(); lpStreamFile->Release(); if (!bResult) _tmp_file->Remove(false); else CallQueryInterface(_tmp_file, tmp_file); return bResult; }
void CMapiApi::ReportStringProp( const char *pTag, LPSPropValue pVal) { if ( pVal && (PROP_TYPE( pVal->ulPropTag) == PT_TSTRING)) { nsCString val((LPCTSTR) (pVal->Value.LPSZ)); MAPI_TRACE2( "%s %s\n", pTag, (const char *)val); } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_NULL)) { MAPI_TRACE1( "%s {NULL}\n", pTag); } else if (pVal && (PROP_TYPE( pVal->ulPropTag) == PT_ERROR)) { MAPI_TRACE1( "%s {Error retrieving property}\n", pTag); } else { MAPI_TRACE1( "%s invalid value, expecting string\n", pTag); } if (pVal) MAPIFreeBuffer( pVal); }
void CMapiMessage::ProcessHeaders( void) { m_bHasSubject = FALSE; m_bHasFrom = FALSE; m_bHasDate = FALSE; PC_S8 pChar = (PC_S8) m_headers.get(); int start = 0; int len = 0; int hdrLen = strlen(pChar); nsCString line; nsCString mid; while (*pChar) { if ((*pChar == 0x0D) && (*(pChar + 1) == 0x0A)) { if ((*(pChar + 2) != ' ') && (*(pChar + 2) != 9)) { m_headers.Mid( mid, start, len); line += mid; ProcessHeaderLine( line); line.Truncate(); pChar++; // subsequent increment will move pChar to the next line start += len; start += 2; len = -1; } } pChar++; len++; } // See if we still have data to be processed. if (start < hdrLen) { line.Assign(m_headers.get()+start); ProcessHeaderLine(line); } if (!m_mimeContentType.IsEmpty() || !m_mimeBoundary.IsEmpty() || !m_mimeCharset.IsEmpty()) { MAPI_TRACE1("\tDecoded mime content type: %s\r\n", m_mimeContentType.get()); MAPI_TRACE1("\tDecoded mime boundary: %s\r\n", m_mimeBoundary.get()); MAPI_TRACE1("\tDecoded mime charset: %s\r\n", m_mimeCharset.get()); } }
BOOL CMapiMessage::IterateAttachTable( void) { LPMAPITABLE lpTable = m_pAttachTable; ULONG rowCount; HRESULT hr = lpTable->GetRowCount( 0, &rowCount); if (!rowCount) { return( TRUE); } hr = lpTable->SetColumns( (LPSPropTagArray)&ptaEid, 0); if (FAILED(hr)) { MAPI_TRACE2( "SetColumns for attachment table failed: 0x%lx, %d\r\n", (long)hr, (int)hr); return( FALSE); } hr = lpTable->SeekRow( BOOKMARK_BEGINNING, 0, NULL); if (FAILED(hr)) { MAPI_TRACE2( "SeekRow for attachment table failed: 0x%lx, %d\r\n", (long)hr, (int)hr); return( FALSE); } int cNumRows = 0; LPSRowSet lpRow; BOOL bResult = TRUE; do { lpRow = NULL; hr = lpTable->QueryRows( 1, 0, &lpRow); if(HR_FAILED(hr)) { MAPI_TRACE2( "QueryRows for attachment table failed: 0x%lx, %d\n", (long)hr, (int)hr); bResult = FALSE; break; } if (lpRow) { cNumRows = lpRow->cRows; if (cNumRows) { DWORD aNum = lpRow->aRow[0].lpProps[ieidPR_ATTACH_NUM].Value.ul; m_attachNums.AppendElement(aNum); MAPI_TRACE1( "\t\t****Attachment found - #%d\r\n", (int)aNum); } CMapiApi::FreeProws( lpRow); } } while ( SUCCEEDED(hr) && cNumRows && lpRow); return( bResult); }
BOOL CMapiApi::HandleHierarchyItem( ULONG oType, ULONG cb, LPENTRYID pEntry) { if (oType == MAPI_FOLDER) { LPMAPIFOLDER pFolder; if (OpenEntry( cb, pEntry, (LPUNKNOWN *) &pFolder)) { LPSPropValue pVal; pVal = GetMapiProperty( pFolder, PR_DISPLAY_NAME); ReportStringProp( "Folder name:", pVal); IterateContents( NULL, pFolder); IterateHierarchy( NULL, pFolder); pFolder->Release(); } else { MAPI_TRACE0( " Folder type - error opening\n"); } } else MAPI_TRACE1( " ObjectType: %ld\n", oType); return( TRUE); }
BOOL CGetStoreFoldersIter::HandleHierarchyItem( ULONG oType, ULONG cb, LPENTRYID pEntry) { if (oType == MAPI_FOLDER) { LPMAPIFOLDER pFolder; if (m_pApi->OpenEntry( cb, pEntry, (LPUNKNOWN *) &pFolder)) { LPSPropValue pVal; nsString name; pVal = m_pApi->GetMapiProperty( pFolder, PR_CONTAINER_CLASS); if (pVal) m_pApi->GetStringFromProp( pVal, name); else name.Truncate(); if ((name.IsEmpty() && m_isMail) || (!ExcludeFolderClass(name.get()))) { pVal = m_pApi->GetMapiProperty( pFolder, PR_DISPLAY_NAME); m_pApi->GetStringFromProp( pVal, name); CMapiFolder *pNewFolder = new CMapiFolder(name.get(), cb, pEntry, m_depth); m_pList->AddItem( pNewFolder); pVal = m_pApi->GetMapiProperty( pFolder, PR_FOLDER_TYPE); MAPI_TRACE2( "Type: %d, name: %s\n", m_pApi->GetLongFromProp( pVal), (const char *)name); // m_pApi->ListProperties( pFolder); CGetStoreFoldersIter nextIter( m_pApi, *m_pList, m_depth + 1, m_isMail); m_pApi->IterateHierarchy( &nextIter, pFolder); } pFolder->Release(); } else { MAPI_TRACE0( "GetStoreFolders - HandleHierarchyItem: Error opening folder entry.\n"); return( FALSE); } } else MAPI_TRACE1( "GetStoreFolders - HandleHierarchyItem: Unhandled ObjectType: %ld\n", oType); return( TRUE); }
BOOL CMapiMessage::GetAttachmentInfo( int idx) { ClearTempAttachFile(); BOOL bResult = TRUE; if ((idx < 0) || (idx >= (int)m_attachNums.Length())) { return( FALSE); } DWORD aNum = m_attachNums[idx]; LPATTACH lpAttach = NULL; HRESULT hr = m_lpMsg->OpenAttach( aNum, NULL, 0, &lpAttach); if (HR_FAILED( hr)) { MAPI_TRACE2( "\t\t****Attachment error, unable to open attachment: %d, 0x%lx\r\n", idx, hr); return( FALSE); } LPSPropValue pVal; pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_MIME_TAG); if (pVal) CMapiApi::GetStringFromProp( pVal, m_attachMimeType); else m_attachMimeType.Truncate(); pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_METHOD); if (pVal) { LONG aMethod = CMapiApi::GetLongFromProp( pVal); if ((aMethod == ATTACH_BY_REF_ONLY) || (aMethod == ATTACH_BY_REFERENCE) || (aMethod == ATTACH_BY_REF_RESOLVE)) { m_attachPath.Truncate(); pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_PATHNAME); if (pVal) CMapiApi::GetStringFromProp( pVal, m_attachPath); MAPI_TRACE2("\t\t** Attachment #%d by ref: %s\r\n", idx, m_attachPath.get()); m_ownsAttachFile = FALSE; } else if (aMethod == ATTACH_BY_VALUE) { MAPI_TRACE1( "\t\t** Attachment #%d by value.\r\n", idx); bResult = CopyBinAttachToFile( lpAttach); } else if (aMethod == ATTACH_OLE) { MAPI_TRACE1( "\t\t** Attachment #%d by OLE - yuck!!!\r\n", idx); } else if (aMethod == ATTACH_EMBEDDED_MSG) { MAPI_TRACE1( "\t\t** Attachment #%d by Embedded Message??\r\n", idx); } else { MAPI_TRACE2( "\t\t** Attachment #%d unknown attachment method - 0x%lx\r\n", idx, aMethod); bResult = FALSE; } } else bResult = FALSE; nsCString fName, fExt; pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_LONG_FILENAME); if (pVal) CMapiApi::GetStringFromProp( pVal, fName); pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_EXTENSION); if (pVal) CMapiApi::GetStringFromProp( pVal, fExt); pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_SIZE); long sz = 0; if (pVal) sz = CMapiApi::GetLongFromProp( pVal); /* // I have no idea how this tag is used, how to interpret it's value, etc. // Fortunately, the Microsoft documentation is ABSOLUTELY NO HELP AT ALL. In fact, // if one goes by the docs and sample code, this tag is completely 100% useless. I'm // sure it has some important meaning which will one day be obvious, but for now, // it is ignored. pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_TAG); if (pVal) { ::MAPIFreeBuffer( pVal); } */ MAPI_TRACE1("\t\t\t--- Mime type: %s\r\n", m_attachMimeType.get()); MAPI_TRACE2("\t\t\t--- File name: %s, extension: %s\r\n", fName.get(), fExt.get()); MAPI_TRACE1("\t\t\t--- Size: %ld\r\n", sz); if (fExt.IsEmpty()) { int idx = fName.RFindChar( '.'); if (idx != -1) fName.Right( fExt, fName.Length() - idx); } if ((fName.RFindChar( '.') == -1) && !fExt.IsEmpty()) { fName += "."; fName += fExt; } m_attachFileName = fName; if (m_attachMimeType.IsEmpty()) { PRUint8 *pType = NULL; if (!fExt.IsEmpty()) { pType = CMimeTypes::GetMimeType( fExt); } if (pType) m_attachMimeType = (PC_S8)pType; else m_attachMimeType = "application/octet-stream"; } pVal = CMapiApi::GetMapiProperty( lpAttach, PR_ATTACH_TRANSPORT_NAME); if (pVal) { CMapiApi::GetStringFromProp( pVal, fName); MAPI_TRACE1("\t\t\t--- Transport name: %s\r\n", fName.get()); } lpAttach->Release(); return( bResult); }
BOOL CMapiMessage::CopyBinAttachToFile( LPATTACH lpAttach) { LPSTREAM lpStreamFile; m_ownsAttachFile = FALSE; m_attachPath.Truncate(); nsCOMPtr<nsIFile> tmpFile; nsresult rv = GetSpecialDirectoryWithFileName(NS_OS_TEMP_DIR, "mapiattach.tmp", getter_AddRefs(tmpFile)); NS_ENSURE_SUCCESS(rv, rv); rv = tmpFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 00600); NS_ENSURE_SUCCESS(rv, rv); nsCString tmpPath; tmpFile->GetNativePath(tmpPath); HRESULT hr = CMapiApi::OpenStreamOnFile( gpMapiAllocateBuffer, gpMapiFreeBuffer, STGM_READWRITE | STGM_CREATE, (char *) tmpPath.get(), NULL, &lpStreamFile); if (HR_FAILED(hr)) { MAPI_TRACE1("~~ERROR~~ OpenStreamOnFile failed - temp path: %s\r\n", tmpPath.get()); return( FALSE); } MAPI_TRACE1("\t\t** Attachment extracted to temp file: %s\r\n", m_attachPath.get()); BOOL bResult = TRUE; LPSTREAM lpAttachStream; hr = lpAttach->OpenProperty( PR_ATTACH_DATA_BIN, &IID_IStream, 0, 0, (LPUNKNOWN *)&lpAttachStream); if (HR_FAILED( hr)) { MAPI_TRACE0( "~~ERROR~~ OpenProperty failed for PR_ATTACH_DATA_BIN.\r\n"); lpAttachStream = NULL; bResult = FALSE; } else { STATSTG st; hr = lpAttachStream->Stat( &st, STATFLAG_NONAME); if (HR_FAILED( hr)) { MAPI_TRACE0( "~~ERROR~~ Stat failed for attachment stream\r\n"); bResult = FALSE; } else { hr = lpAttachStream->CopyTo( lpStreamFile, st.cbSize, NULL, NULL); if (HR_FAILED( hr)) { MAPI_TRACE0( "~~ERROR~~ Attach Stream CopyTo temp file failed.\r\n"); bResult = FALSE; } } } m_attachPath = tmpPath; if (lpAttachStream) lpAttachStream->Release(); lpStreamFile->Release(); if (!bResult) tmpFile->Remove(PR_FALSE); else m_ownsAttachFile = TRUE; return( bResult); }
BOOL CMapiApi::IterateStores( CMapiFolderList& stores) { stores.ClearAll(); if (!m_lpSession) { MAPI_TRACE0( "IterateStores called before session is open\n"); m_lastError = -1; return( FALSE); } HRESULT hr; /* -- Some Microsoft sample code just to see if things are working --- *//* ULONG cbEIDStore; LPENTRYID lpEIDStore; hr = HrMAPIFindDefaultMsgStore( m_lpSession, &cbEIDStore, &lpEIDStore); if (HR_FAILED(hr)) { MAPI_TRACE0( "Default message store not found\n"); // MessageBox(NULL,"Message Store Not Found",NULL,MB_OK); } else { LPMDB lpStore; MAPI_TRACE0( "Default Message store FOUND\n"); hr = m_lpSession->OpenMsgStore( NULL, cbEIDStore, lpEIDStore, NULL, MDB_NO_MAIL | MDB_NO_DIALOG, &lpStore); if (HR_FAILED(hr)) { MAPI_TRACE1( "Unable to open default message store: 0x%lx\n", hr); } else { MAPI_TRACE0( "Default message store OPENED\n"); lpStore->Release(); } } */ LPMAPITABLE lpTable; hr = m_lpSession->GetMsgStoresTable( 0, &lpTable); if (FAILED(hr)) { MAPI_TRACE0( "GetMsgStoresTable failed\n"); m_lastError = hr; return( FALSE); } ULONG rowCount; hr = lpTable->GetRowCount( 0, &rowCount); MAPI_TRACE1( "MsgStores Table rowCount: %ld\n", rowCount); hr = lpTable->SetColumns( (LPSPropTagArray)&ptaTbl, 0); if (FAILED(hr)) { lpTable->Release(); MAPI_TRACE2( "SetColumns failed: 0x%lx, %d\n", (long)hr, (int)hr); m_lastError = hr; return( FALSE); } hr = lpTable->SeekRow( BOOKMARK_BEGINNING, 0, NULL); if (FAILED(hr)) { lpTable->Release(); MAPI_TRACE2( "SeekRow failed: 0x%lx, %d\n", (long)hr, (int)hr); m_lastError = hr; return( FALSE); } int cNumRows = 0; LPSRowSet lpRow; BOOL keepGoing = TRUE; BOOL bResult = TRUE; do { lpRow = NULL; hr = lpTable->QueryRows( 1, 0, &lpRow); if(HR_FAILED(hr)) { MAPI_TRACE2( "QueryRows failed: 0x%lx, %d\n", (long)hr, (int)hr); bResult = FALSE; m_lastError = hr; break; } if(lpRow) { cNumRows = lpRow->cRows; if (cNumRows) { LPCTSTR lpStr = (LPCTSTR) lpRow->aRow[0].lpProps[itblPR_DISPLAY_NAME].Value.LPSZ; LPENTRYID lpEID = (LPENTRYID) lpRow->aRow[0].lpProps[itblPR_ENTRYID].Value.bin.lpb; ULONG cbEID = lpRow->aRow[0].lpProps[itblPR_ENTRYID].Value.bin.cb; // In the future, GetStoreInfo needs to somehow return // whether or not the store is from an IMAP server. // Currently, GetStoreInfo opens the store and attempts // to get the hierarchy tree. If the tree is empty or // does not exist, then szContents will be zero. We'll // assume that any store that doesn't have anything in // it's hierarchy tree is not a store we want to import - // there would be nothing to import from anyway! // Currently, this does exclude IMAP server accounts // which is the desired behaviour. int strLen = strlen(lpStr); PRUnichar * pwszStr = (PRUnichar *) nsMemory::Alloc((strLen + 1) * sizeof(WCHAR)); if (!pwszStr) { // out of memory FreeProws( lpRow); lpTable->Release(); return FALSE; } ::MultiByteToWideChar(CP_ACP, 0, lpStr, strlen(lpStr) + 1, pwszStr, (strLen + 1) * sizeof(WCHAR)); CMapiFolder *pFolder = new CMapiFolder( pwszStr, cbEID, lpEID, 0, MAPI_STORE); nsMemory::Free(pwszStr); long szContents = 1; GetStoreInfo( pFolder, &szContents); MAPI_TRACE1( " DisplayName: %s\n", lpStr); if (szContents) { stores.AddItem( pFolder); } else { delete pFolder; MAPI_TRACE0( " ^^^^^ Not added to store list\n"); } keepGoing = TRUE; } FreeProws( lpRow); } } while ( SUCCEEDED(hr) && cNumRows && lpRow && keepGoing); lpTable->Release(); return( bResult); }
BOOL CMapiApi::IterateContents( CMapiContentIter *pIter, LPMAPIFOLDER pFolder, ULONG flags) { // flags can be 0 or MAPI_ASSOCIATED // MAPI_ASSOCIATED is usually used for forms and views HRESULT hr; LPMAPITABLE lpTable; hr = pFolder->GetContentsTable( flags, &lpTable); if (FAILED(hr)) { MAPI_TRACE2( "GetContentsTable failed: 0x%lx, %d\n", (long)hr, (int)hr); return( FALSE); } ULONG rowCount; hr = lpTable->GetRowCount( 0, &rowCount); if (!rowCount) { MAPI_TRACE0( " Empty Table\n"); } hr = lpTable->SetColumns( (LPSPropTagArray)&ptaEid, 0); if (FAILED(hr)) { lpTable->Release(); MAPI_TRACE2( "SetColumns failed: 0x%lx, %d\n", (long)hr, (int)hr); return( FALSE); } hr = lpTable->SeekRow( BOOKMARK_BEGINNING, 0, NULL); if (FAILED(hr)) { lpTable->Release(); MAPI_TRACE2( "SeekRow failed: 0x%lx, %d\n", (long)hr, (int)hr); return( FALSE); } int cNumRows = 0; LPSRowSet lpRow; BOOL keepGoing = TRUE; BOOL bResult = TRUE; do { lpRow = NULL; hr = lpTable->QueryRows( 1, 0, &lpRow); if(HR_FAILED(hr)) { MAPI_TRACE2( "QueryRows failed: 0x%lx, %d\n", (long)hr, (int)hr); bResult = FALSE; break; } if(lpRow) { cNumRows = lpRow->cRows; if (cNumRows) { LPENTRYID lpEID = (LPENTRYID) lpRow->aRow[0].lpProps[ieidPR_ENTRYID].Value.bin.lpb; ULONG cbEID = lpRow->aRow[0].lpProps[ieidPR_ENTRYID].Value.bin.cb; ULONG oType = lpRow->aRow[0].lpProps[ieidPR_OBJECT_TYPE].Value.ul; keepGoing = HandleContentsItem( oType, cbEID, lpEID); MAPI_TRACE1( " ObjectType: %ld\n", oType); } FreeProws( lpRow); } } while ( SUCCEEDED(hr) && cNumRows && lpRow && keepGoing); lpTable->Release(); return( bResult); }
bool CMapiMessage::AddAttachment(DWORD aNum) { LPATTACH lpAttach = NULL; HRESULT hr = m_lpMsg->OpenAttach(aNum, NULL, 0, &lpAttach); if (HR_FAILED(hr)) { MAPI_TRACE2("\t\t****Attachment error, unable to open attachment: %d, 0x%lx\r\n", idx, hr); return false; } bool bResult = false; attach_data *data = new attach_data; ULONG aMethod; if (data) { bResult = true; // 1. Get the file that contains the attachment data LPSPropValue pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_METHOD); if (pVal) { aMethod = CMapiApi::GetLongFromProp(pVal); switch (aMethod) { case ATTACH_BY_VALUE: MAPI_TRACE1("\t\t** Attachment #%d by value.\r\n", aNum); bResult = CopyBinAttachToFile(lpAttach, getter_AddRefs(data->tmp_file)); data->delete_file = true; break; case ATTACH_BY_REFERENCE: case ATTACH_BY_REF_RESOLVE: case ATTACH_BY_REF_ONLY: pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_PATHNAME_W); if (pVal) { nsCString path; CMapiApi::GetStringFromProp(pVal, path); nsresult rv; data->tmp_file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); if (NS_FAILED(rv) || !data->tmp_file) { MAPI_TRACE0("*** Error creating file spec for attachment\n"); bResult = false; } else data->tmp_file->InitWithNativePath(path); } MAPI_TRACE2("\t\t** Attachment #%d by ref: %s\r\n", aNum, m_attachPath.get()); break; case ATTACH_EMBEDDED_MSG: MAPI_TRACE1("\t\t** Attachment #%d by Embedded Message??\r\n", aNum); // Convert the embedded IMessage from PR_ATTACH_DATA_OBJ to rfc822 attachment // (see http://msdn.microsoft.com/en-us/library/cc842329.aspx) // This is a recursive call. bResult = CopyMsgAttachToFile(lpAttach, getter_AddRefs(data->tmp_file)); data->delete_file = true; break; case ATTACH_OLE: MAPI_TRACE1("\t\t** Attachment #%d by OLE - yuck!!!\r\n", aNum); break; default: MAPI_TRACE2("\t\t** Attachment #%d unknown attachment method - 0x%lx\r\n", aNum, aMethod); bResult = false; } } else bResult = false; if (bResult) bResult = data->tmp_file; if (bResult) { bool isFile = false; bool exists = false; data->tmp_file->Exists(&exists); data->tmp_file->IsFile(&isFile); if (!exists || !isFile) { bResult = false; MAPI_TRACE0("Attachment file does not exist\n"); } } if (bResult) bResult = GetURL(data->tmp_file, getter_AddRefs(data->orig_url)); if (bResult) { // Now we have the file; proceed to the other properties data->encoding = NS_strdup(ENCODING_BINARY); nsString fname, fext; pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_LONG_FILENAME_W); if (!pVal) pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_FILENAME_W); CMapiApi::GetStringFromProp(pVal, fname); pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_EXTENSION_W); CMapiApi::GetStringFromProp(pVal, fext); MAPI_TRACE2("\t\t\t--- File name: %s, extension: %s\r\n", fname.get(), fext.get()); if (fext.IsEmpty()) { int idx = fname.RFindChar(L'.'); if (idx != -1) fext = Substring(fname, idx); } else if (fname.RFindChar(L'.') == -1) { fname += L"."; fname += fext; } if (fname.IsEmpty()) { // If no description use "Attachment i" format. fname = L"Attachment "; fname.AppendInt(static_cast<uint32_t>(aNum)); } data->real_name = ToNewUTF8String(fname); nsCString tmp; // We have converted it to the rfc822 document if (aMethod == ATTACH_EMBEDDED_MSG) { data->type = NS_strdup(MESSAGE_RFC822); } else { pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_MIME_TAG_A); CMapiApi::GetStringFromProp(pVal, tmp); MAPI_TRACE1("\t\t\t--- Mime type: %s\r\n", tmp.get()); if (tmp.IsEmpty()) { uint8_t *pType = NULL; if (!fext.IsEmpty()) { pType = CMimeTypes::GetMimeType(fext); } if (pType) data->type = NS_strdup((PC_S8)pType); else data->type = NS_strdup(APPLICATION_OCTET_STREAM); } else data->type = ToNewCString(tmp); } pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_CONTENT_ID_A); CMapiApi::GetStringFromProp(pVal, tmp); if (!tmp.IsEmpty()) data->cid = ToNewCString(tmp); } if (bResult) { // Now we need to decide if this attachment is embedded or not. // At first, I tried to simply check for the presence of the Content-Id. // But it turned out that this method is unreliable, since there exist cases // when an attachment has a Content-Id while isn't embedded (even in a message // with a plain-text body!). So next I tried to look for <img> tags that contain // the found Content-Id. But this is unreliable, too, because there exist cases // where other places of HTML reference the embedded messages (e.g. it may be // a background of a table cell, or some CSS; further, it is possible that the // reference to an embedded object is not in the main body, but in another // embedded object - like body references a CSS attachment that in turn references // a picture as a background of its element). From the other hand, it's unreliable // to relax the search criteria to any occurence of the Content-Id string in the body - // partly because the string may be simply in a text or other non-referencing part, // partly because of the abovementioned possibility that the reference is outside // the body at all. // There exist the PR_ATTACH_FLAGS property of the attachment. The MS documentation // tells about two possible flags in it: ATT_INVISIBLE_IN_HTML and ATT_INVISIBLE_IN_RTF. // There is at least one more undocumented flag: ATT_MHTML_REF. Some sources in Internet // suggest simply check for the latter flag to distinguish between the embedded // and ordinary attachments. But my observations indicate that even if the flags // don't include ATT_MHTML_REF, the attachment is still may be embedded. // However, my observations always show that the message is embedded if the flags // is not 0. // So now I will simply test for the non-zero flags to decide whether the attachment // is embedded or not. Possible advantage is reliability (I hope). // Another advantage is that it's much faster than search the body for Content-Id. DWORD flags = 0; pVal = CMapiApi::GetMapiProperty(lpAttach, PR_ATTACH_FLAGS); if (pVal) flags = CMapiApi::GetLongFromProp(pVal); if (m_bodyIsHtml && data->cid && (flags != 0)) // this is the embedded attachment m_embattachments.push_back(data); else // this is ordinary attachment m_stdattachments.push_back(data); } else { delete data; } } lpAttach->Release(); return bResult; }