cpgpexchmessageevents.cpp

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

CPP
2,164
字号
/*____________________________________________________________________________
	Copyright (C) 2002 PGP Corporation
	All rights reserved.

	$Id: CPGPexchMessageEvents.cpp,v 1.62 2002/11/22 23:35:13 sdas Exp $
____________________________________________________________________________*/
#include "CPGPexch.h"
#include <mapiutil.h>
#include "Globals.h"
#include "Recipients.h"
#include "Utils.h"
#include "resource.h"

#include "pgpUnicodeWin32.h"
#include "EncryptSign.h"
#include "DecryptVerify.h"
#include "PGPsc.h"
#include "Prefs.h"
#include "pgpClientPrefs.h"
#include "pgpDebug.h"

STDMETHODIMP CPGPexch::OnRead(IExchExtCallback* peecb)
{
	HWND hwnd;

	peecb->GetWindow(&hwnd);
	if (!hwnd)
		hwnd = GetTopWindow(NULL);

	switch (_context)
	{
	case EECONTEXT_SENDNOTEMESSAGE:
		{
			BOOL fAddButtons = TRUE;
			int nNumButtons;
			int nIndex;
			TBBUTTON tbbNew[5];
			TBBUTTON tbbCheck;
			
			// Check to make sure the buttons aren't already on the toolbar

			nNumButtons = SendMessage(_hwndSendToolbar, TB_BUTTONCOUNT, 0, 0);
			for (nIndex=0; nIndex<nNumButtons; nIndex++)
			{
				SendMessage(_hwndSendToolbar, TB_GETBUTTON, (WPARAM) nIndex, 
							(LPARAM) &tbbCheck);

				// If a button exists with the same bitmap ID and command ID
				// as the Encrypt button, odds are good that the button is
				// already on the toolbar
				if ((tbbCheck.iBitmap == (int)_itbmEncrypt) && 
					(tbbCheck.idCommand == (int)_cmdidEncrypt))
				{
					fAddButtons = FALSE;
					nIndex = nNumButtons;
				}
			}

			if (fAddButtons)
			{
				tbbNew[0].iBitmap = 0;
				tbbNew[0].idCommand = 0;
				tbbNew[0].fsState = 0;
				tbbNew[0].fsStyle = TBSTYLE_SEP;
				tbbNew[0].dwData = 0;
				tbbNew[0].iString = -1;
			
				tbbNew[1].iBitmap = _itbmEncrypt;
				tbbNew[1].idCommand = _cmdidEncrypt;
				tbbNew[1].fsState = TBSTATE_ENABLED;
				tbbNew[1].fsStyle = TBSTYLE_CHECK;
				tbbNew[1].dwData = 0;
				tbbNew[1].iString = -1;
			
				tbbNew[2].iBitmap = _itbmSign;
				tbbNew[2].idCommand = _cmdidSign;
				tbbNew[2].fsState = TBSTATE_ENABLED;
				tbbNew[2].fsStyle = TBSTYLE_CHECK;
				tbbNew[2].dwData = 0;
				tbbNew[2].iString = -1;

				tbbNew[3].iBitmap = 0;
				tbbNew[3].idCommand = 0;
				tbbNew[3].fsState = 0;
				tbbNew[3].fsStyle = TBSTYLE_SEP;
				tbbNew[3].dwData = 0;
				tbbNew[3].iString = -1;
			
				tbbNew[4].iBitmap = _itbmPgpKeys;
				tbbNew[4].idCommand = _cmdidPgpKeys;
				tbbNew[4].fsState = TBSTATE_ENABLED;
				tbbNew[4].fsStyle = TBSTYLE_BUTTON;
				tbbNew[4].dwData = 0;
				tbbNew[4].iString = -1;
			
				SendMessage(_hwndSendToolbar, TB_ADDBUTTONS, 5, 
					(LPARAM) tbbNew);
			}
		}
		break;

    case EECONTEXT_SENDPOSTMESSAGE:
		break;
    
	case EECONTEXT_READNOTEMESSAGE:
		{
			BOOL fAddButtons = TRUE;
			UINT nNumButtons;
			UINT nIndex;
			TBBUTTON tbbNew[5];
			TBBUTTON tbbCheck;

			// Check to make sure the buttons aren't already on the toolbar

			nNumButtons = SendMessage(_hwndReadToolbar, TB_BUTTONCOUNT, 0, 0);
			for (nIndex=0; nIndex<nNumButtons; nIndex++)
			{
				SendMessage(_hwndReadToolbar, TB_GETBUTTON, 
							(WPARAM) nIndex, (LPARAM) &tbbCheck);

				// If a button exists with the same bitmap ID and command ID
				// as the Decrypt button, odds are good that the button is
				// already on the toolbar
				if ((tbbCheck.iBitmap == (int)_itbmDecrypt) && 
					(tbbCheck.idCommand == (int)_cmdidDecrypt))
				{
					fAddButtons = FALSE;
					nIndex = nNumButtons;
				}
			}

			if (fAddButtons)
			{
				tbbNew[0].iBitmap = 0;
				tbbNew[0].idCommand = 0;
				tbbNew[0].fsState = 0;
				tbbNew[0].fsStyle = TBSTYLE_SEP;
				tbbNew[0].dwData = 0;
				tbbNew[0].iString = -1;
			
				tbbNew[1].iBitmap = _itbmDecrypt;
				tbbNew[1].idCommand = _cmdidDecrypt;
				tbbNew[1].fsState = TBSTATE_ENABLED;
				tbbNew[1].fsStyle = TBSTYLE_BUTTON;
				tbbNew[1].dwData = 0;
				tbbNew[1].iString = -1;
			
				tbbNew[2].iBitmap = 0;
				tbbNew[2].idCommand = 0;
				tbbNew[2].fsState = 0;
				tbbNew[2].fsStyle = TBSTYLE_SEP;
				tbbNew[2].dwData = 0;
				tbbNew[2].iString = -1;
			
				tbbNew[3].iBitmap = _itbmPgpKeys;
				tbbNew[3].idCommand = _cmdidPgpKeys;
				tbbNew[3].fsState = TBSTATE_ENABLED;
				tbbNew[3].fsStyle = TBSTYLE_BUTTON;
				tbbNew[3].dwData = 0;
				tbbNew[3].iString = -1;
			
				SendMessage(_hwndReadToolbar, TB_ADDBUTTONS, 4, 
					(LPARAM) tbbNew);
			}

			IMAPISession* psess;

			HRESULT hr = peecb->GetSession(&psess, NULL);
			if (FAILED(hr))
			{
				UIDisplayStringID(hwnd, IDS_E_NOSESSION);
				return S_FALSE;
			}
			
			IMsgStore *pMsgStore = 0;
			IMessage *pmsg = 0;
			int nReturn;

			hr = peecb->GetObject(&pMsgStore, (IMAPIProp**)&pmsg);
			if (FAILED(hr))
			{
				psess->Release();
				return S_FALSE;
			}
			
			nReturn = DecryptVerifyMessage(hwnd, pmsg, pMsgStore);

			pmsg->Release();
			if (pMsgStore != NULL)
				pMsgStore->Release();
			psess->Release();
			return nReturn;
		}
		break;

	case EECONTEXT_READPOSTMESSAGE:
		break;

	default:
		return S_FALSE;
	}

	return S_FALSE;
}


STDMETHODIMP CPGPexch::OnReadComplete(IExchExtCallback* peecb, ULONG ulFlags)
{
	return S_FALSE;
}


STDMETHODIMP CPGPexch::OnWrite(IExchExtCallback* pecb)
{
	return S_FALSE;
}


STDMETHODIMP CPGPexch::OnWriteComplete(IExchExtCallback* pecb, ULONG ulFlags)
{
	if (_context != EECONTEXT_SENDNOTEMESSAGE)
		return S_FALSE;

	if (ulFlags == EEME_COMPLETE_FAILED) // Nothing to unwind
		return S_FALSE;

	if (!_fInSubmitState)	// This is not a submission.
		return S_FALSE;

	if (_bEncrypt || _bSign)
	{
		IMessage *pmsg = NULL;
		LPMAPITABLE ptableAttach = NULL;
		SizedSPropTagArray(1, tagaTable) = {1, {PR_ATTACH_NUM}};
		SRowSet *prAttach = NULL;
		PGPclRecipientDialogStruct *prds = NULL;
		PGPOptionListRef signOptions = NULL;
		HWND hwnd = NULL;
		BOOL bHasAttachments = FALSE;
		PGPError nError = kPGPError_NoErr;

		HRESULT hrRet = pecb->GetWindow(&hwnd);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != hwnd));
		if (NULL == hwnd)
		{
			hwnd = GetTopWindow(NULL);
			pgpAssert(NULL != hwnd);
		}
		
		hrRet = pecb->GetObject(NULL, (IMAPIProp**)&pmsg);
		if (FAILED(hrRet) || (NULL == pmsg))
		{
			UIDisplayStringID(hwnd, IDS_E_NOMESSAGE);
			hrRet = E_ABORT;
			goto cleanup;
		}

		prds = (PGPclRecipientDialogStruct *) 
				calloc(sizeof(PGPclRecipientDialogStruct), 1);
		pgpAssert(NULL != prds);
		if(NULL == prds)
		{
			hrRet = E_ABORT;//out of memory
			goto cleanup;
		}

		nError = PGPclOpenDefaultKeyrings(_pgpContext, 
					kPGPOpenKeyDBFileOptions_Mutable, &(prds->keydbOriginal));

		if (IsPGPError(nError))
			nError = PGPclOpenDefaultKeyrings(_pgpContext, 
						kPGPOpenKeyDBFileOptions_None, &(prds->keydbOriginal));

		if (IsPGPError(nError))
		{
			UIDisplayErrorCode(__FILE__, __LINE__, NULL, nError, TRUE);
			hrRet = E_ABORT;
			goto cleanup;
		}

		//try to see if there are any attachments in this message
		hrRet = pmsg->GetAttachmentTable(0, &ptableAttach);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != ptableAttach));
		if(FAILED(hrRet) || (NULL == ptableAttach))
		{
			hrRet = E_ABORT;
			goto cleanup;
		}

		hrRet = HrQueryAllRows(ptableAttach, (SPropTagArray *)&tagaTable, 
			NULL, NULL, 0, &prAttach);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != prAttach));
		if(FAILED(hrRet) || (NULL == prAttach))
		{
			hrRet = E_ABORT;
			goto cleanup;
		}
		if (prAttach->cRows)
			bHasAttachments = TRUE;


		if (_bEncrypt)
		{
			nError = GetRecipients(pecb, _pgpContext, _tlsContext, prds,
						bHasAttachments);
			pgpAssert(IsntPGPError(nError));
			if (IsPGPError(nError))
			{
				if (nError != kPGPError_UserAbort)
					UIDisplayErrorCode(__FILE__, __LINE__, NULL, 
						nError, FALSE);
				hrRet = E_ABORT;
				goto cleanup;
			}
		}


		nError = EncryptSignMessage(hwnd, pmsg, prds, &signOptions);
		if (IsPGPError(nError))
		{
			hrRet = E_ABORT;
			goto cleanup;
		}
		
		hrRet = pmsg->SaveChanges(KEEP_OPEN_READWRITE);
		pgpAssert(SUCCEEDED(hrRet));
		if(FAILED(hrRet))
		{
			hrRet = E_ABORT;
			goto cleanup;
		}

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

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

		if (signOptions != NULL)
		{
			PGPFreeOptionList(signOptions);
			signOptions = NULL;
		}

		if(NULL != prds)
		{
			FreeRecipients(prds);

			if (NULL != prds->keydbOriginal)
			{
				PGPFreeKeyDB(prds->keydbOriginal);
				prds->keydbOriginal = NULL;
			}

			free(prds);
			prds = NULL;
		}

		if(NULL != pmsg)
		{
			pmsg->Release();
			pmsg = NULL;
		}
		
		return SUCCEEDED(hrRet)?S_FALSE:hrRet;//return S_FALSE in case of success
											  //the error hresult otherwise
	}

	return S_FALSE;
}


STDMETHODIMP CPGPexch::OnCheckNames(IExchExtCallback*)
{
	return S_FALSE;
}


STDMETHODIMP CPGPexch::OnCheckNamesComplete(IExchExtCallback*, ULONG)
{
	return S_FALSE;
}


STDMETHODIMP CPGPexch::OnSubmit(IExchExtCallback*)
{
	_fInSubmitState = TRUE;
	return S_FALSE;
}


STDMETHODIMP_(VOID) CPGPexch::OnSubmitComplete(IExchExtCallback*, ULONG)
{
	_fInSubmitState = FALSE;
	return;
}


int CPGPexch::EncryptSignMessage(HWND hwnd,
								 IMessage *pmsg,
								 PGPclRecipientDialogStruct *prds,
								 PGPOptionListRef *pSignOptions)
{
	IStream *pstrmBody = NULL;
	IStream *pstrmRTF = NULL;
	IStream *pstrmRTFInput = NULL;
	STATSTG StreamStats = {0};
	DWORD dwInSize = 0;
	DWORD dwRTFSize = 0;
	DWORD dwTextSize = 0;
	UINT nOutSize = 0;
	UINT nFormatOutSize = 0;
	char *pInput = NULL;
	char *pTextInput = NULL;
	char *pRTFInput = NULL;
	char *pOutput = NULL;
	char *pFormatOutput = NULL;
	LPMAPITABLE ptableAttach = NULL;
	SizedSPropTagArray(1, tagaTable) = {1, {PR_ATTACH_NUM}};
	SRowSet *prAttach = NULL;
	SizedSPropTagArray(1, tagaCodePage) = {1, {PR_INTERNET_CPID}};
	SPropValue *pCPID = NULL;
	ULONG ulNumVals = 0;
	PGPError nError = kPGPError_NoErr;
	char szName[256] = {0};
	char szFile[256] = {0};
	char *pHeader = NULL;
	char *pOutHeader = NULL;
	char *pConverted = NULL;
	char *pOutFooter = "\n</PRE></BODY></HTML>";
	HGLOBAL hRTFInput = NULL;
	ULARGE_INTEGER ulSize = {0};
	ULARGE_INTEGER ulMaxSize = {INT_MAX, 0};
	BOOL bGotHTML = FALSE;
	BOOL bGotRTF = FALSE;
	BOOL bConvert = FALSE;
	BOOL bConvertHTML = FALSE;
	BOOL bConvertRTF = FALSE;
	//BOOL bNeedUTF8 = FALSE;
	HRESULT hrRet = S_OK;
	DWORD dwOrigOptions = 0;
	UINT nIndex = 0;

	UIGetString(szName, sizeof(szName), IDS_LOGNAME);
	UIGetString(szFile, sizeof(szFile), IDS_DLL);

	pgpAssert(NULL == pstrmBody);
	hrRet = pmsg->OpenProperty(PR_BODY, &IID_IStream, STGM_READ, 0, 
		(IUnknown**)&pstrmBody);
	pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
	if (FAILED(hrRet))
	{
		pgpAssert(NULL == pstrmBody);
		hrRet = pmsg->OpenProperty(PR_BODY, &IID_IStream, STGM_READ, 
				MAPI_CREATE | MAPI_MODIFY, (IUnknown**)&pstrmBody);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
	}

	if (FAILED(hrRet) || (NULL == pstrmBody))
	{
		UIDisplayStringID(hwnd, IDS_E_NOBODY);
		nError = kPGPError_ItemNotFound;
		goto cleanup;
	}
	
	hrRet = pstrmBody->Stat(&StreamStats, STATFLAG_NONAME);
	pgpAssert(SUCCEEDED(hrRet));
	if (StreamStats.cbSize.QuadPart == 0)
	{
		pstrmBody->Release();
		pstrmBody = NULL;

		pTextInput = (char *) calloc(3, sizeof(char));
		if(NULL == pTextInput)
		{
			UIDisplayStringID(hwnd, IDS_E_NOMEMORY);
			nError = kPGPError_OutOfMemory;
			goto cleanup;
		}

		strcpy(pTextInput, "\r\n");
		dwTextSize = strlen(pTextInput);
	}
	else
	{
		dwTextSize = StreamStats.cbSize.LowPart;
		pTextInput = (char *) calloc(dwTextSize+1, sizeof(char));
		if (NULL == pTextInput)
		{
			UIDisplayStringID(hwnd, IDS_E_NOMEMORY);
			nError = kPGPError_OutOfMemory;
			goto cleanup;
			//pstrmBody->Release();
			//return kPGPError_OutOfMemory;
		}
		
		//read the email body in plain text
		hrRet = pstrmBody->Read(pTextInput, dwTextSize, &dwTextSize);
		pgpAssert(SUCCEEDED(hrRet));
		if(FAILED(hrRet))
		{
			UIDisplayStringID(hwnd, IDS_E_NOBODY);
			nError = kPGPError_ItemNotFound;
			goto cleanup;
		}

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

	pTextInput[dwTextSize] = NULL;

	/*if (_bEncrypt)
	{
		for (nIndex=0; nIndex < dwTextSize; nIndex++)
		{
			if (pTextInput[nIndex] & 0x80)
			{
				bNeedUTF8 = TRUE;
				break;
			}
		}
	}

	if (bNeedUTF8)
	{
		hrRet = pmsg->GetProps((SPropTagArray *)&tagaCodePage, 0, &ulNumVals, 
				&pCPID);

		pgpAssert(SUCCEEDED(hrRet) && (NULL != pCPID));
		if (SUCCEEDED(hrRet) && (NULL != pCPID))
		{
			char *pNewText = NULL;
			PGPUInt32 nNewSize = 0;

⌨️ 快捷键说明

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