cpgpexchmessageevents.cpp

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

CPP
2,164 行
字号
			
			pNewText = (char *) calloc(1, (dwTextSize+1)*4);
			if(NULL == pNewText)
			{
				UIDisplayStringID(hwnd, IDS_E_NOMEMORY);
				nError = kPGPError_OutOfMemory;
				goto cleanup;
			}

			pgpLocalStringToUTF8(0, pCPID[0].Value.l, pTextInput, dwTextSize, 
				pNewText, (dwTextSize+1)*4, &nNewSize);

			//free up the existing buffer
			free(pTextInput);
			pTextInput = NULL;

			//remember the new size and the buffer that has the UTF8 buffer 
			//instead of the original buffer
			pgpAssert(NULL == pTextInput);
			pTextInput = pNewText;
			dwTextSize = nNewSize;

			pCPID[0].Value.l = CP_UTF8;
			hrRet = pmsg->SetProps(1, pCPID, NULL);
			pgpAssert(SUCCEEDED(hrRet));
		}
	}*/

	//get the message attachment table
	pgpAssert(NULL == ptableAttach);
	hrRet = pmsg->GetAttachmentTable(0, &ptableAttach);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != ptableAttach));
	if(SUCCEEDED(hrRet) && (NULL != ptableAttach))
	{
		//get the attachment rows
		pgpAssert(NULL == prAttach);
		hrRet = HrQueryAllRows(ptableAttach, (SPropTagArray *)&tagaTable, 
						NULL, NULL, 0, &prAttach);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != prAttach));
	}

	if ((prds->dwOptions & kPGPclSecureViewer) == kPGPclSecureViewer)
		bConvert = TRUE;

	if (_bConvert == TRUE)
		bConvert = TRUE;

	//get the HTML text in the mail 
	pgpAssert(NULL == pstrmBody);
	hrRet = pmsg->OpenProperty(PR_BODY_HTML, &IID_IStream, STGM_READ, 
			0, (IUnknown**)&pstrmBody);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
	if (FAILED(hrRet) || (NULL == pstrmBody))
		goto NoBodyHTML;
	
	hrRet = pstrmBody->Stat(&StreamStats, STATFLAG_NONAME);
	pgpAssert(SUCCEEDED(hrRet));

	dwInSize = StreamStats.cbSize.LowPart;
	if (dwInSize < 3)
		goto NoBodyHTML;
	
	if (bConvert)
	{
		bConvertHTML = TRUE;
		goto NoBodyHTML;
	}

	pgpAssert(NULL == pInput);
	pInput = (char *) calloc(dwInSize+1, sizeof(char));
	if (NULL == pInput)
	{
		UIDisplayStringID(hwnd, IDS_E_NOMEMORY);
		nError = kPGPError_OutOfMemory;
		goto cleanup;
	}

	hrRet = pstrmBody->Read(pInput, dwInSize, &dwInSize);
	pgpAssert(SUCCEEDED(hrRet));

	pInput[dwInSize] = NULL;

	//office versions prior to office XP
	pConverted = strstr(pInput, "<!-- Converted from text/rtf format -->");

	if(NULL == pConverted)//if not found search for office XP's little hint in there
		pConverted = strstr(pInput, "<!-- Converted from text/plain format -->");

	if (NULL != pConverted)
	{
		SPropTagArray taga={0};
		
		taga.cValues = 1;
		taga.aulPropTag[0] = PR_BODY_HTML;
		hrRet = pmsg->DeleteProps(&taga, NULL);
		pgpAssert(SUCCEEDED(hrRet));

		hrRet = pmsg->SaveChanges(KEEP_OPEN_READWRITE);
		pgpAssert(SUCCEEDED(hrRet));

		pgpAssert(NULL != pInput);
		free(pInput);
		pInput = NULL;

		dwInSize = 0;
		goto NoBodyHTML;
	}

	if (!UIWarnYesNo(hwnd, IDS_Q_HTMLENCRYPT, kPGPPrefWin32OutlookHTMLEncrypt, TRUE))
	{
		nError = kPGPError_UserAbort;//user chose not to go further with html manipulations
		goto cleanup;
	}

	pHeader = strstr(pInput, "<BODY");
	pgpAssert(NULL != pHeader);
	if(NULL == pHeader)
	{
		UIDisplayStringID(hwnd, IDS_E_MALFORMEDHTMLFOUND);
		nError = kPGPError_UnknownError;
		goto cleanup;
	}

	pgpAssert(NULL == pOutHeader);
	pOutHeader = (char *) calloc(sizeof(char), pHeader - pInput + 20);
	if(NULL == pOutHeader)
	{
		UIDisplayStringID(hwnd, IDS_E_NOMEMORY);
		nError = kPGPError_OutOfMemory;
		goto cleanup;
	}

	strncpy(pOutHeader, pInput, pHeader - pInput);
	strcat(pOutHeader, "<BODY><PRE>\n");

	bGotHTML = TRUE;
	dwOrigOptions = prds->dwOptions;
	prds->dwOptions &= ~kPGPclASCIIArmor;
	nError = EncryptSignBuffer(UIGetInstance(), hwnd, _pgpContext, 
				_tlsContext, szName, szFile, pInput, 
				dwInSize, prds, NULL, pSignOptions, (VOID ** )&pFormatOutput, 
				&nFormatOutSize, _bEncrypt, _bSign, FALSE, TRUE, 
				m_pExtMdlessCallBack, m_pemOurMdlessCallBack);
	prds->dwOptions = dwOrigOptions;

	free(pInput);
	pInput = NULL;

	if (IsPGPError(nError))
		goto cleanup;
	else
		goto NoBodyRTF;

NoBodyHTML:
	//we reuse the pointer here
	if(NULL != pstrmBody)
	{
		pstrmBody->Release();
		pstrmBody = NULL;
	}

	pgpAssert(NULL == pstrmBody);
	hrRet = pmsg->OpenProperty(PR_RTF_COMPRESSED, &IID_IStream, STGM_READ, 
			0, (IUnknown**)&pstrmBody);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
	if (FAILED(hrRet) || (NULL == pstrmBody))
		goto NoBodyRTF;
		
	pgpAssert(NULL == pstrmRTF);
	hrRet = WrapCompressedRTFStream(pstrmBody, 0, &pstrmRTF);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmRTF));
	if(FAILED(hrRet) || (NULL == pstrmRTF))
	{
		UIDisplayStringID(hwnd, IDS_E_GENERICRTFTXTFAILURE);
		nError = kPGPError_UnknownError;
		goto cleanup;
	}

	pgpAssert(NULL == hRTFInput);
	hRTFInput = GlobalAlloc(GHND, 0);
	pgpAssert(NULL != hRTFInput);
	if(NULL == hRTFInput)
	{
		UIDisplayStringID(hwnd, IDS_E_NOMEMORY);
		nError = kPGPError_OutOfMemory;
		goto cleanup;
	}

	pgpAssert(NULL == pstrmRTFInput);
	hrRet = CreateStreamOnHGlobal(hRTFInput, TRUE, &pstrmRTFInput);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmRTFInput));
	if(FAILED(hrRet) || (NULL == pstrmRTFInput))
	{
		UIDisplayStringID(hwnd, IDS_E_GENERICFAILURE);
		nError = kPGPError_UnknownError;
		goto cleanup;
	}

	hrRet = pstrmRTF->CopyTo(pstrmRTFInput, ulMaxSize, NULL, &ulSize);
	pgpAssert(SUCCEEDED(hrRet));
	if(FAILED(hrRet))
	{
		UIDisplayStringID(hwnd, IDS_E_GENERICFAILURE);
		nError = kPGPError_UnknownError;
		goto cleanup;
	}

	hrRet = pstrmRTFInput->Commit(STGC_DEFAULT);
	pgpAssert(SUCCEEDED(hrRet));
	if(FAILED(hrRet))
	{
		UIDisplayStringID(hwnd, IDS_E_GENERICFAILURE);
		nError = kPGPError_UnknownError;
		goto cleanup;
	}

	dwRTFSize = ulSize.LowPart;

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

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

	pRTFInput = (char *) GlobalLock(hRTFInput);
	pgpAssert(NULL != pRTFInput);
	if(NULL == pRTFInput)
	{
		UIDisplayStringID(hwnd, IDS_E_GENERICFAILURE);
		nError = kPGPError_UnknownError;
		goto cleanup;
	}
	
	pHeader = strstr(pRTFInput, "\\fromtext");
	if (pHeader || (dwRTFSize < 3))
		goto NoBodyRTF;

	if (bConvert)
	{
		BOOL bGotOLE = FALSE;
		bConvertRTF = TRUE;

		//check if there are any OLE attachments in this message
		hrRet = AreAnyOleAttachmentsPresent(pmsg, prAttach, bGotOLE);
		pgpAssert(SUCCEEDED(hrRet));
		if(SUCCEEDED(hrRet) && (TRUE == bGotOLE))
		{
			UIDisplayStringID(hwnd, IDS_E_CANTCONVERTOLE);
			nError = kPGPError_UserAbort;
			goto cleanup;		
		}

		goto NoBodyRTF;
	}

	if (!UIWarnYesNo(hwnd, IDS_Q_HTMLENCRYPT, kPGPPrefWin32OutlookHTMLEncrypt, TRUE))
	{
		nError = kPGPError_UserAbort;
		goto cleanup;
	}

	bGotRTF = TRUE;
	dwOrigOptions = prds->dwOptions;
	prds->dwOptions &= ~kPGPclASCIIArmor;
	nError = EncryptSignBuffer(UIGetInstance(), hwnd, _pgpContext, 
				_tlsContext, szName, szFile, pRTFInput, 
				dwRTFSize-1, prds, NULL, pSignOptions, (VOID ** )&pFormatOutput, 
				&nFormatOutSize, _bEncrypt, _bSign, FALSE, TRUE, 
				m_pExtMdlessCallBack, m_pemOurMdlessCallBack);
	prds->dwOptions = dwOrigOptions;
	if (IsPGPError(nError))
		goto cleanup;

	pHeader = strstr(pRTFInput, "\\fromhtml");
	if (NULL != pHeader)
	{
		pOutHeader = _strdup("{\\fromhtml1\n");
		pOutFooter = "\n}\0";
	}
	else
	{
		pOutHeader = _strdup("{\\fromtext\n");
		pOutFooter = "\n}\0";
	}


NoBodyRTF:	

	nError = EncryptSignBuffer(UIGetInstance(), hwnd, _pgpContext, 
				_tlsContext, szName, szFile, pTextInput, 
				dwTextSize, prds, NULL, pSignOptions, (VOID ** )&pOutput, 
				&nOutSize, _bEncrypt, _bSign, FALSE, FALSE, 
				m_pExtMdlessCallBack, m_pemOurMdlessCallBack);

	if (IsPGPError(nError))
		goto cleanup;

	if (bGotHTML || bConvertHTML)
	{

/*
		LARGE_INTEGER li = {0,0};
		ULARGE_INTEGER uli = {nOutSize + strlen(pOutHeader) + 
								strlen(pOutFooter),0};

		pstrmBody = NULL;
		pmsg->OpenProperty(PR_BODY_HTML, &IID_IStream, STGM_READWRITE, 
			MAPI_MODIFY, (IUnknown**)&pstrmBody);

		pstrmBody->Write(pOutHeader, strlen(pOutHeader), NULL);
		pstrmBody->Write(pOutput, nOutSize, NULL);
		pstrmBody->Write(pOutFooter, strlen(pOutFooter), NULL);
		pstrmBody->SetSize(uli);
		pstrmBody->Commit(STGC_DEFAULT);
		pstrmBody->Release();
*/

		SPropTagArray taga;
		
		taga.cValues = 1;
		taga.aulPropTag[0] = PR_BODY_HTML;
		pmsg->DeleteProps(&taga, NULL);
		pmsg->SaveChanges(KEEP_OPEN_READWRITE);
	}

	if (bGotRTF || bConvertRTF)
	{
		BOOL fPartied;

		//we reuse the pointer here again !!
		if(NULL != pstrmBody)
		{
			pstrmBody->Release();
			pstrmBody = NULL;
		}

		pgpAssert(NULL == pstrmBody);
		hrRet = pmsg->OpenProperty(PR_RTF_COMPRESSED, &IID_IStream, STGM_READWRITE, 
					MAPI_MODIFY, (IUnknown**)&pstrmBody);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));

		hrRet = WrapCompressedRTFStream(pstrmBody, MAPI_MODIFY, &pstrmRTF);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmRTF));

		if(NULL != pstrmRTF)
		{
			hrRet = NullifyStream(pstrmRTF);
			pgpAssert(SUCCEEDED(hrRet));

			pstrmRTF->Release();
			pstrmRTF = NULL;
		}

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

		hrRet = RTFSync(pmsg, RTF_SYNC_RTF_CHANGED, &fPartied);
		pgpAssert(SUCCEEDED(hrRet));
	}

	if (dwTextSize > 0)
	{
		BOOL fPartied;

		if(NULL != pstrmBody)
		{
			pstrmBody->Release();
			pstrmBody = NULL;
		}
		
		//get PR_BODY, create if it does not exist. On Office XP we seem
		//to be losing the mail body !!
		pgpAssert(NULL == pstrmBody);
		hrRet = pmsg->OpenProperty(PR_BODY, &IID_IStream, STGM_READWRITE, 
					MAPI_CREATE|MAPI_MODIFY, (IUnknown**)&pstrmBody);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
		if(SUCCEEDED(hrRet) && (NULL != pstrmBody))
		{
			hrRet = NullifyStream(pstrmBody);
			pgpAssert(SUCCEEDED(hrRet));

			pstrmBody->Release();
			pstrmBody = NULL;

			hrRet = RTFSync(pmsg, RTF_SYNC_BODY_CHANGED, &fPartied);
			pgpAssert(S_OK == hrRet);

			hrRet = pmsg->OpenProperty(PR_BODY, &IID_IStream, STGM_READWRITE, 
				MAPI_MODIFY, (IUnknown**)&pstrmBody);
			pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
		}

	}

	if (dwTextSize > 0)
	{
		LARGE_INTEGER li = {0,0};
		ULARGE_INTEGER uli = {nOutSize + prAttach->cRows + 3, 0};
		BOOL fPartied;
		UINT nLoop;

		pgpAssert(NULL != pstrmBody);
		if(NULL != pstrmBody)
		{
			hrRet = pstrmBody->Seek(li, STREAM_SEEK_SET, NULL);
			pgpAssert(SUCCEEDED(hrRet));

			hrRet = pstrmBody->Write(" \r\n", 3, NULL);
			pgpAssert(SUCCEEDED(hrRet));

			hrRet = pstrmBody->Write(pOutput, nOutSize, NULL);
			pgpAssert(SUCCEEDED(hrRet));

			for (nLoop=0; nLoop<(prAttach->cRows+3); nLoop++)
			{
				hrRet = pstrmBody->Write(" ", 1, NULL);
				pgpAssert(SUCCEEDED(hrRet));
			}

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

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

			pstrmBody->Release();
			pstrmBody = NULL;

			hrRet = RTFSync(pmsg, RTF_SYNC_BODY_CHANGED, &fPartied);
			pgpAssert(SUCCEEDED(hrRet));

			nOutSize += 3;
		}
	}

	if (prAttach->cRows && IsntPGPError(nError))
		nError = EncryptSignAttachment(hwnd, pmsg, nOutSize,
					prAttach, prds, pSignOptions);

	if (IsPGPError(nError))
		goto cleanup;

	if (bGotHTML)
	{
		pgpAssert(FALSE == bGotRTF);//we should not have got RTF then

		ULONG ulAttachNum = 0;
		LPATTACH pAttach = NULL;
		IStream *pstrmAttach = NULL;
		SPropValue pVal[5] = {0};
		LARGE_INTEGER li = {0,0};
		ULARGE_INTEGER uli = {nFormatOutSize, 0};

		pVal[0].ulPropTag = PR_ATTACH_METHOD;
		pVal[0].Value.l = ATTACH_BY_VALUE;

		pVal[1].ulPropTag = PR_RENDERING_POSITION;
		pVal[1].Value.l = -1;
		
		pVal[2].ulPropTag = PR_ATTACH_FILENAME;
		pVal[2].Value.lpszA = "PGPexch.pgp";
		
		pVal[3].ulPropTag = PR_ATTACH_LONG_FILENAME;
		pVal[3].Value.lpszA = "PGPexch.htm.pgp";

		pVal[4].ulPropTag = PR_DISPLAY_NAME;
		pVal[4].Value.lpszA = "PGPexch.htm.pgp";

		pgpAssert(NULL == pAttach);
		hrRet = pmsg->CreateAttach(NULL, 0, &ulAttachNum, &pAttach);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pAttach));

		if(SUCCEEDED(hrRet) && (NULL != pAttach))
		{
			hrRet = pAttach->SetProps(5, pVal, NULL);
			pgpAssert(SUCCEEDED(hrRet));

			pstrmAttach = NULL;
			hrRet = pAttach->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, 0, 
				MAPI_CREATE | MAPI_MODIFY, (LPUNKNOWN *) &pstrmAttach);
			pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmAttach));
			if(SUCCEEDED(hrRet) && (NULL != pstrmAttach))
			{
				hrRet = pstrmAttach->Seek(li, STREAM_SEEK_SET, NULL);
				pgpAssert(SUCCEEDED(hrRet));

				hrRet = pstrmAttach->Write(pFormatOutput, nFormatOutSize, NULL);
				pgpAssert(SUCCEEDED(hrRet));

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

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

				pstrmAttach->Release();
				pstrmAttach = NULL;
			}

			hrRet = pAttach->SaveChanges(0);
			pgpAssert(SUCCEEDED(hrRet));
		}
	}

	if (bGotRTF)
	{
		pgpAssert(FALSE == bGotHTML);//we should not have got HTML then

		ULONG ulAttachNum = 0;
		LPATTACH pAttach = NULL;
		IStream *pstrmAttach = NULL;
		SPropValue pVal[5] = {0};
		LARGE_INTEGER li = {0,0};
		ULARGE_INTEGER uli = {nFormatOutSize, 0};

		pVal[0].ulPropTag = PR_ATTACH_METHOD;
		pVal[0].Value.l = ATTACH_BY_VALUE;

		pVal[1].ulPropTag = PR_RENDERING_POSITION;
		pVal[1].Value.l = -1;
		
		pVal[2].ulPropTag = PR_ATTACH_FILENAME;
		pVal[2].Value.lpszA = "PGPexch.pgp";

		pVal[3].ulPropTag = PR_ATTACH_LONG_FILENAME;
		pVal[3].Value.lpszA = "PGPexch.rtf.pgp";
		
		pVal[4].ulPropTag = PR_DISPLAY_NAME;
		pVal[4].Value.lpszA = "PGPexch.rtf.pgp";

		pgpAssert(NULL == pAttach);
		hrRet = pmsg->CreateAttach(NULL, 0, &ulAttachNum, &pAttach);

⌨️ 快捷键说明

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