cpgpexchmessageevents.cpp

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C++ 代码 · 共 2,164 行 · 第 1/4 页

CPP
2,164 行
字号
	{
		PGPFreeData(pOutput);
		pOutput = NULL;
	}

	if(NULL != prAttach)
	{
		FreeProws(prAttach);
		prAttach = NULL;
	}
	
	if(NULL != ptableAttach)
	{
		ptableAttach->Release();
		ptableAttach = NULL;
	}

	return S_FALSE;
}

HRESULT CPGPexch::AreAnyOleAttachmentsPresent(IMessage *pmsg, 
			SRowSet *prsAttachTabRowSet, BOOL &bPresent)
{
	SizedSPropTagArray(1, tagaAttach) = {1, {PR_ATTACH_METHOD}};
	SPropValue *ppvValArray = NULL;
	LPATTACH pAttach = NULL;
	ULONG ulNumVals = 0;
	ULONG ulRow = 0;
	ULONG ulNum = 0;
	HRESULT hrRet = S_OK;
	
	//check parameters
	pgpAssert(NULL != pmsg);
	pgpAssert(NULL != prsAttachTabRowSet);
	if((NULL == pmsg) || (NULL == prsAttachTabRowSet))
		return E_INVALIDARG;

	for (ulRow=0; ulRow < prsAttachTabRowSet->cRows; ulRow++)
	{
		ulNum = prsAttachTabRowSet->aRow[ulRow].lpProps[0].Value.l;

		if(NULL != pAttach)
		{
			pAttach->Release();
			pAttach = NULL;
		}

		pgpAssert(NULL == pAttach);
		hrRet = pmsg->OpenAttach(ulNum, NULL, 0, &pAttach);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pAttach));
		if(FAILED(hrRet) || (NULL == pAttach))
			goto cleanup;

		if(NULL != ppvValArray)
		{
			hrRet = MAPIFreeBuffer(ppvValArray);
			pgpAssert(S_OK == hrRet);
			ppvValArray = NULL;
		}
		
		pgpAssert(NULL == ppvValArray);
		hrRet = pAttach->GetProps((SPropTagArray *)&tagaAttach, 
					0, &ulNumVals, &ppvValArray);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != ppvValArray) && (1 == ulNumVals));
		if(FAILED(hrRet) || (NULL == ppvValArray))
			goto cleanup;

		if (ATTACH_OLE == ppvValArray[0].Value.l)
		{
			
			bPresent = TRUE;
			goto cleanup;
		}
	}

	//control comes here only if there was no error in any of the calls
	pgpAssert(SUCCEEDED(hrRet));
	hrRet = S_OK;

	cleanup:

	if(NULL != pAttach)
	{
		pAttach->Release();
		pAttach = NULL;
	}

	if(NULL != ppvValArray)
	{
		HRESULT hrDiagRet = MAPIFreeBuffer(ppvValArray);
		pgpAssert(S_OK == hrDiagRet);
		ppvValArray = NULL;
	}
	
	return hrRet;
}


HRESULT CPGPexch::NullifyStream(IStream *pStream)
{
	LARGE_INTEGER li = {0,0};
	ULARGE_INTEGER uli = {1,0};

	//check parameters
	pgpAssert(NULL != pStream);
	if(NULL == pStream)
		return E_INVALIDARG;

	HRESULT hrRet = pStream->Seek(li, STREAM_SEEK_SET, NULL);
	pgpAssert(SUCCEEDED(hrRet));

	hrRet = pStream->Write("\0", 1, NULL);
	pgpAssert(SUCCEEDED(hrRet));

	hrRet = pStream->SetSize(uli);
	pgpAssert(SUCCEEDED(hrRet));

	hrRet = pStream->Commit(STGC_DEFAULT);
	pgpAssert(SUCCEEDED(hrRet));

	return hrRet;
}

//find the plugin format information if any present as an attachment in this message
HRESULT CPGPexch::SearchForPluginAttachment(IMessage *pCurMsg, SRowSet *prAttach, 
											ULONG &ulNum, DWORD &dwResult, char *szFileName,
											DWORD dwBufSize1, char *szLongFileName,
											DWORD dwBufSize2, char *szDisplayName,
											DWORD dwBufSize3)
{
	ULONG ulNumVals = 0;
	ULONG ulRow = 0, ulAttachNum = 0;
	HRESULT hrRet = S_OK;
	BOOL bOurAttFound = FALSE;
	BOOL bRet = TRUE;
	SPropValue *pval = NULL;
	SizedSPropTagArray(4, tagaAttach) = {4, {PR_ATTACH_METHOD, PR_ATTACH_FILENAME, 
											 PR_ATTACH_LONG_FILENAME, PR_DISPLAY_NAME}};
	enum {ivalAttachMethod=0, ivalFilename, ivalLongFilename, ivalDisplayName};
	LPATTACH pAttach = NULL;


	//check parameters
	pgpAssert(NULL != pCurMsg);
	pgpAssert(NULL != prAttach);
	if((NULL == pCurMsg) || (NULL == prAttach))
		return E_INVALIDARG;

	pgpAssert(!IsBadWritePtr(szFileName, dwBufSize1));
	pgpAssert(!IsBadWritePtr(szLongFileName, dwBufSize2));
	pgpAssert(!IsBadWritePtr(szDisplayName, dwBufSize3));

	//for each attachment in the message being operated on as long as we have not
	//found html or rtf body of the message...
	for (ulRow=0; (ulRow < prAttach->cRows) && !bOurAttFound; ulRow++)
	{
		ulAttachNum = prAttach->aRow[ulRow].lpProps[0].Value.l;

		//open the attachment
		pgpAssert(NULL == pAttach);
		hrRet = pCurMsg->OpenAttach(ulAttachNum, NULL, 0, &pAttach);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pAttach));
		if(FAILED(hrRet) || (NULL == pAttach))
		{
			bRet = FALSE;//some attachments could not be opened !!
			goto attach_cleanup;
		}

		//get the interesting properties of the attachment
		pgpAssert(NULL == pval);
		hrRet = pAttach->GetProps((SPropTagArray *)&tagaAttach, 0, &ulNumVals, &pval);
		pgpAssert(SUCCEEDED(hrRet) && (4 == ulNumVals));
		pgpAssert(MAPI_W_ERRORS_RETURNED != hrRet);
		if(FAILED(hrRet))
		{
			bRet = FALSE;//some attachment properties could not be gotten
			goto attach_cleanup;
		}
		
		//if this attachment is not attached by value
		if (pval[ivalAttachMethod].Value.l != ATTACH_BY_VALUE)
		{
			//skip it - since we know the body is not attached that way
			goto attach_cleanup;
		}
		
		//if we successfully got the long file name
		if(PT_ERROR != PROP_TYPE(pval[ivalLongFilename].ulPropTag))
		{
			if (!strcmp(pval[ivalLongFilename].Value.lpszA, "PGPexch.htm.asc") ||
				!strcmp(pval[ivalLongFilename].Value.lpszA, "PGPexch.htm.pgp"))
			{
				dwResult |= SFPA_FOUND_HTML_DATA;
			}
			else if (!strcmp(pval[ivalLongFilename].Value.lpszA, "PGPexch.rtf.asc") ||
					!strcmp(pval[ivalLongFilename].Value.lpszA, "PGPexch.rtf.pgp"))
			{
				dwResult |= SFPA_FOUND_RTF_DATA;
			}
			else
				goto attach_cleanup;//skip this attachment since we know that
									//the formatted body is named as one of the above
		}
		
		//the control comes here only if we found the formatted body as an
		//attachment in this message

		//copy the attributes of the attachment
		pgpAssert(PROP_TYPE(pval[ivalFilename].ulPropTag) == PROP_TYPE(PR_ATTACH_FILENAME));
		if(PT_ERROR != PROP_TYPE(pval[ivalFilename].ulPropTag))
			strncpy(szFileName, pval[ivalFilename].Value.lpszA, dwBufSize1);

		pgpAssert(PROP_TYPE(pval[ivalLongFilename].ulPropTag) == PROP_TYPE(PR_ATTACH_LONG_FILENAME));
		if(PT_ERROR != PROP_TYPE(pval[ivalLongFilename].ulPropTag))
			strncpy(szLongFileName, pval[ivalLongFilename].Value.lpszA, dwBufSize2);

		pgpAssert(PROP_TYPE(pval[ivalDisplayName].ulPropTag) == PROP_TYPE(PR_DISPLAY_NAME));
		if(PT_ERROR != PROP_TYPE(pval[ivalDisplayName].ulPropTag))
			strncpy(szDisplayName, pval[ivalDisplayName].Value.lpszA, dwBufSize3);

		hrRet = S_OK;
		ulNum = ulAttachNum;
		bOurAttFound = TRUE;
		pgpAssert(0 != dwResult);
				
		attach_cleanup:
		if(NULL != pval)
		{
			hrRet = MAPIFreeBuffer(pval);
			pgpAssert(S_OK == hrRet);
			pval = NULL;
		}

		if(NULL != pAttach)
		{
			pAttach->Release();
			pAttach = NULL;
		}
	}

	return (TRUE == bRet)?S_OK:E_FAIL;
}

//get the attachment contents in a buffer allocated by callee
HRESULT CPGPexch::GetAttachBuf(IMessage *pCurMsg, ULONG ulNum, 
						char *&pAttachBuf, DWORD &dwBufSize)
{
	STATSTG AttStreamStats = {0};
	HRESULT hrRet = S_OK;
	BOOL bRet = FALSE;
	LPATTACH pAttach = NULL;
	LPSTREAM pstrmAttach = NULL; 
	DWORD dwAttSize = 0, dwSize = 0;

	//check parameters
	pgpAssert(NULL != pCurMsg);
	pgpAssert(NULL == pAttachBuf);//should not have anything in there
	if(NULL == pCurMsg)
		return E_INVALIDARG;
	
	//init the out parameters
	pAttachBuf = NULL;
	dwBufSize = 0;

	//open the attachment
	pgpAssert(NULL == pAttach);
	hrRet = pCurMsg->OpenAttach(ulNum, NULL, 0, &pAttach);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pAttach));
	if(FAILED(hrRet) || (NULL == pAttach))
		goto cleanup;
	
	//open attachment
	pgpAssert(NULL == pstrmAttach);
	hrRet = pAttach->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, 0, 0,
				(LPUNKNOWN *) &pstrmAttach);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmAttach));
	if(FAILED(hrRet) || (NULL == pstrmAttach))
		goto cleanup;

	//get the size
	hrRet = pstrmAttach->Stat(&AttStreamStats, STATFLAG_NONAME);
	pgpAssert(SUCCEEDED(hrRet));
	if(FAILED(hrRet))
		goto cleanup;

	dwAttSize = AttStreamStats.cbSize.LowPart;
	
	//allocate as necessary
	pAttachBuf = (char *) calloc(1, dwAttSize+1);
	pgpAssert(NULL != pAttachBuf);
	if(NULL == pAttachBuf)
		goto cleanup;

	//read the attachment bytes
	hrRet = pstrmAttach->Read(pAttachBuf, dwAttSize, &dwSize);
	pgpAssert(SUCCEEDED(hrRet) && (dwSize == dwAttSize));
	if(FAILED(hrRet))
		goto cleanup;
	
	//null terminate
	pAttachBuf[dwAttSize] = NULL;

	dwBufSize = dwAttSize;
	bRet = TRUE;

	cleanup:

	if(NULL != pstrmAttach)
	{
		pstrmAttach->Release();
		pstrmAttach = NULL;
	}

	if(NULL != pAttach)
	{
		pAttach->Release();
		pAttach = NULL;
	}

	if(FALSE == bRet)//if we have a failure
	{
		//free the buffer if we allocated
		if(NULL != pAttachBuf)
		{
			free(pAttachBuf);
			pAttachBuf = NULL;
			dwBufSize = 0;
		}
	}

	return bRet?S_OK:E_FAIL;
}

BOOL CPGPexch::DoesBufHavePGPHeaders(char *pInput)
{
	pgpAssert(NULL != pInput);
	char *szStart = pInput;
	char *szEnd = strstr(szStart, "\r\n");
	BOOL bGotPGP = FALSE;
	char szPGPbegin[] = "-----BEGIN PGP";
	char szPGPendMsg[] = "-----END PGP MESSAGE-----";
	char szPGPendSig[] = "-----END PGP SIGNATURE-----";
	UINT nPGPbegin = strlen(szPGPbegin);
	UINT nPGPendMsg = strlen(szPGPendMsg);
	UINT nPGPendSig = strlen(szPGPendSig);
	
	while (NULL != szEnd)
	{
		if (!strncmp(szStart, szPGPbegin, nPGPbegin))
		{
			bGotPGP = TRUE;
			break;
		}
		
		szStart = szEnd+2;
		szEnd = strstr(szStart, "\r\n");
	}
	
	if (bGotPGP)
	{
		bGotPGP = FALSE;
		do
		{
			szStart = szEnd+2;
			szEnd = strstr(szStart, "\r\n");
			
			if (!strncmp(szStart, szPGPendMsg, nPGPendMsg) ||
				!strncmp(szStart, szPGPendSig, nPGPendSig))
			{
				bGotPGP = TRUE;
				break;
			}
		}
		while (szEnd != NULL);
	}

	return bGotPGP;
}

HRESULT CPGPexch::ReadMsgBodyToBuf(IMessage *pCurMsg, void * &pvBuf, DWORD &dwSize)
{
	STATSTG StreamStats = {0};
	IStream *pstrmBody = NULL;
	DWORD dwStreamSize = 0, dwReadSize = 0;
	BOOL bRet = FALSE;
	HRESULT hrRet = S_OK;

	//check parameters
	pgpAssert(NULL != pCurMsg);
	if(NULL == pCurMsg)
		goto cleanup;

	//reset contents of output parameters
	pgpAssert(NULL == pvBuf);//leak ?
	pvBuf = NULL;
	dwSize = 0;

	//open up PR_BODY property
	pgpAssert(NULL == pstrmBody);
	hrRet = pCurMsg->OpenProperty(PR_BODY, &IID_IStream, STGM_READ, 0, 
				(IUnknown**)&pstrmBody);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
	if (FAILED(hrRet) || (NULL == pstrmBody))
		goto cleanup;
	
	//get the size
	hrRet = pstrmBody->Stat(&StreamStats, STATFLAG_NONAME);
	pgpAssert(SUCCEEDED(hrRet));
	if(FAILED(hrRet))
		goto cleanup;

	dwStreamSize = StreamStats.cbSize.LowPart;
	pgpAssert(0 < dwStreamSize);

	//allocate as necessary
	pvBuf = calloc(1, dwStreamSize+1);
	pgpAssert(NULL != pvBuf);
	if (NULL == pvBuf)
		goto cleanup;
	
	//read the stream to buffer allocated
	hrRet = pstrmBody->Read(pvBuf, dwStreamSize, &dwReadSize);
	pgpAssert(SUCCEEDED(hrRet) && (dwStreamSize == dwReadSize));
	if(FAILED(hrRet) || (dwReadSize != dwStreamSize))
		goto cleanup;

	((BYTE *)pvBuf)[dwStreamSize] = NULL;//null terminate
	dwSize = dwStreamSize;
	bRet = TRUE;//all success

	cleanup:
	
	if(NULL != pstrmBody)
	{
		pstrmBody->Release();
		pstrmBody = NULL;
	}

	if(FALSE == bRet)//if we failed anywhere
	{
		//free up the buffer if allocated
		if(NULL != pvBuf)
		{
			free(pvBuf);
			pvBuf = NULL;
		}
	}

	return bRet?S_OK:E_FAIL;
}

HRESULT CPGPexch::BackupFormatInfoIfAny(IMessage *pCurMsg, void * &pvBuf, 
					DWORD &dwSize, DWORD &dwBufType, ULONG &ulAttIndex)
{
	HRESULT hrRet = S_OK;
	LPMAPITABLE ptableAttach = 0;
	SRowSet *prAttach = NULL;
	ULONG ulNum = 0;
	SizedSPropTagArray(1, tagaTable) = {1, {PR_ATTACH_NUM}};
	DWORD dwFormatFlags = 0;
	char szFileName[50] = {0};
	char szLongFileName[256] = {0};
	char szDisplayName[256] = {0};

	pgpAssert(NULL == ptableAttach);
	hrRet = pCurMsg->GetAttachmentTable(0, &ptableAttach);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != ptableAttach));
	if(FAILED(hrRet) || (NULL == ptableAttach))
		goto cleanup;

	pgpAssert(NULL == prAttach);
	hrRet = HrQueryAllRows(ptableAttach, (SPropTagArray *)&tagaTable, NULL, 
				NULL, 0, &prAttach);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != prAttach));
	if(FAILED(hrRet) || (NULL == prAttach))
		goto cleanup;
	
	//find if there is any formatting attachment in this mail
	hrRet = SearchForPluginAttachment(pCurMsg, prAttach, ulNum, dwFormatFlags,
				szFileName, sizeof(szFileName), szLongFileName, 
				sizeof(szLongFileName),	szDisplayName, sizeof(szDisplayName));
	pgpAssert(SUCCEEDED(hrRet));

	//if we did find the formatting attachment
	if(SUCCEEDED(hrRet) && (0 != dwFormatFlags))
	{
		pgpAssert(NULL != szFileName[0]);
		pgpAssert(NULL != szLongFileName[0]);
		pgpAssert(NULL != szDisplayName[0]);

		//the control comes here only if we found the formatted body as an
		//attachment in this message

		//remember the attachment number
		ulAttIndex = ulNum;

		//set the format on the backup object
		pgpAssert(NULL != m_pBackupMsg);
		m_pBackupMsg->SetFormat(dwFormatFlags&SFPA_FOUND_HTML_DATA, 
				dwFormatFlags&SFPA_FOUND_RTF_DATA);
		dwBufType = (dwFormatFlags&SFPA_FOUND_HTML_DATA)?
			SFPA_FOUND_HTML_DATA:SFPA_FOUND_RTF_DATA;
		
		//get the attachment text
		hrRet = GetAttachBuf(pCurMsg, ulNum, (char *&)pvBuf, dwSize);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pvBuf) && (0 < dwSize));
		if(SUCCEEDED(hrRet) && (NULL != pvBuf) && (0 < dwSize))
		{
			//update backup
			m_pBackupMsg->AddAttachment(ulNum, TRUE, szFileName,
					szLongFileName, szDisplayName, (char *)pvBuf, dwSize);
			
		}
	}
	
	cleanup:

	if(NULL != prAttach)
	{
		FreeProws(prAttach);
		prAttach = NULL;
	}

	if(NULL != ptableAttach)
	{
		ptableAttach->Release();
		ptableAttach = NULL;
	}

	return hrRet;
}

/*__Editor_settings____

	Local Variables:
	tab-width: 4
	End:
	vi: ts=4 sw=4
	vim: si
_____________________*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?