summaryrefslogtreecommitdiff
path: root/plugins/MirFox/src/MirfoxMiranda.cpp
blob: bbe1819862bdfab22f0370d53054c679fc3c2192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "common.h"
#include "MirfoxMiranda.h"




CMirfoxMiranda::CMirfoxMiranda()
				: sharedMemoryUtils(SharedMemoryUtils::getInstance())
				, mirandaUtils(MirandaUtils::getInstance())
				, logger(MFLogger::getInstance())
{
}



CMirfoxMiranda::~CMirfoxMiranda()
{
}

MirfoxData& CMirfoxMiranda::getMirfoxData(){
	return mirfoxData;
}

int CMirfoxMiranda::onMirandaInterfaceLoad()
{

	mirandaUtils->netlibRegister(); //for Miranda logger init
	logger->initLogger(&MirandaUtils::netlibLog);
	logger->set6CharsPrefix(L"MNG   ");

	mirandaUtils->translateOldDBNames();
	
	mirfoxData.initializeMirfoxData();
	logger->log(L"dll init, MirfoxData initialized");

	initializeSharedMemory(mirfoxData);
	if (mirfoxData.getPluginState() == MFENUM_PLUGIN_STATE_ERROR){
		return 0;
	}

	initializeMessageQueue(mirfoxData);
	if (mirfoxData.getPluginState() == MFENUM_PLUGIN_STATE_ERROR){
		return 0;
	}

	mirfoxData.hhook_EventOpenMW = CreateHookableEvent("MirFox/OpenMW");
	mirfoxData.hhook_OpenMW = HookEvent("MirFox/OpenMW", MirandaUtils::on_hook_OpenMW);

	commitSharedMemory();


	return 0;
}


int CMirfoxMiranda::onMirandaInterfaceUnload()
{

	UnhookEvent(mirfoxData.hhook_OpenMW);
	DestroyHookableEvent(mirfoxData.hhook_EventOpenMW);

	unloadMessageQueue(mirfoxData.processCsmId);
	unloadSharedMemory();

	mirfoxData.releaseMirfoxData();

	logger->releaseLogger();
	mirandaUtils->netlibUnregister();

	return 0;
}

void CMirfoxMiranda::initializeSharedMemory(MirfoxData& mirfoxData)
{

	//initialize CSM record and MSMs with data from mirfoxData
	initializeSharedMemoryData(mirfoxData, sharedMemoryUtils);
	
	if (mirfoxData.getPluginState() == MFENUM_PLUGIN_STATE_ERROR){
		return;
	}

	//Create new thread to maintain shared memory data
	mir_forkthread(CMirfoxMiranda::csmThread, this);

}

void CMirfoxMiranda::commitSharedMemory()
{
	//commitSM();
	sharedMemoryUtils->commitSM();
}

void CMirfoxMiranda::initializeSharedMemoryData(MirfoxData& mirfoxData, SharedMemoryUtils* sharedMemoryUtils)
{

	std::wstring visableTo;
	if (mirfoxData.getClientsProfilesFilterCheckbox()){
		visableTo = *mirfoxData.getClientsProfilesFilterStringPtr();
	}else{
		visableTo = TEXT("");
	}
	OpenOrCreateSMReturnStruct result = sharedMemoryUtils->openOrCreateSM('M', mirandaUtils->getDisplayName(), visableTo, false);
	logger->log_p(L"openOrCreateCSM('M')   result = [%u]", result);
	
	if(result.errorCode != 0){
		//error
		if (result.errorCode == -3){  //existing csm version is too high -> i'm too old
			MessageBox(NULL, TranslateT("This MirFox (Miranda) plugin is too old. Please update it."), TranslateT("MirFox (Miranda) - Error"), MB_OK | MB_ICONWARNING );
		}
		if (result.errorCode == -4){  //existing csm version is too low -> sb is too old
			MessageBox(NULL, TranslateT("This MirFox (Miranda) plugin cannot start because some other MirFox component is too old. Please, check and update your MirFox components."), TranslateT("MirFox (Miranda) - Error"), MB_OK | MB_ICONWARNING );
		}
		mirfoxData.setPluginState(MFENUM_PLUGIN_STATE_ERROR);
		return;
	} else {
		mirfoxData.processCsmId = result.processCsmId;
		wchar_t prefix[7];
		mir_sntprintf(prefix, SIZEOF(prefix), L"MNG%d", result.processCsmId);
		logger->set6CharsPrefix(prefix);
	}
	

	sharedMemoryUtils->resetMsmParameters();

	//addOptionToSM();
	//	no options to add yet

	//addTranslationToSM();
	//	no translations to add yet

	//addAccountToSM();
	boost::ptr_list<MirandaAccount>* mirandaAccountsPtr = mirfoxData.getMirandaAccounts();
	boost::ptr_list<MirandaAccount>::iterator mirandaAccountsIter;
	for (mirandaAccountsIter = mirandaAccountsPtr->begin(); mirandaAccountsIter != mirandaAccountsPtr->end(); mirandaAccountsIter++){
		if (mirandaAccountsIter->accountState == MFENUM_MIRANDAACCOUNT_STATE_ON){
			std::wstring tszAccountNameW = mirandaAccountsIter->tszAccountName;
			sharedMemoryUtils->addAccountToSM(mirandaAccountsIter->id, tszAccountNameW);
		}
	}
	
	//addGroupToSM();
	//TODO groups support
	std::wstring groupName = L"ROOT GROUP";
	sharedMemoryUtils->addGroupToSM((uint64_t)1, (uint64_t)NULL, 'R', groupName);

	//addContactToSM();
	boost::ptr_list<MirandaContact>* mirandaContactsPtr = mirfoxData.getMirandaContacts();
	boost::ptr_list<MirandaContact>::iterator mirandaContactsIter;
	for (mirandaContactsIter = mirandaContactsPtr->begin(); mirandaContactsIter != mirandaContactsPtr->end(); mirandaContactsIter++){
		if (mirandaContactsIter->contactState == MFENUM_MIRANDACONTACT_STATE_ON){
			//TODO mirandaAccountHandle support
			sharedMemoryUtils->addContactToSM((uint64_t)mirandaContactsIter->contactHandle, (uint64_t)NULL, (uint64_t)1, mirandaContactsIter->contactNameW);
		}
	}
}



void CMirfoxMiranda::unloadSharedMemory()
{
	sharedMemoryUtils->unloadSharedMemory(mirfoxData.processCsmId);
	//all msm's will be deleted  when miranda process returns
}

void CMirfoxMiranda::csmThread(void* threadArg)
{
	Thread_Push(0);

	CMirfoxMiranda* mirfoxMirandaPtr = (CMirfoxMiranda*)threadArg;
	MirfoxData* mirfoxDataPtr = &(mirfoxMirandaPtr->getMirfoxData());
	SharedMemoryUtils* sharedMemoryUtils = SharedMemoryUtils::getInstance();
	MirandaUtils* mirandaUtils = MirandaUtils::getInstance();
	MFLogger* logger = MFLogger::getInstance();

	mirfoxDataPtr->workerThreadsCount++;

	logger->log_p(L"MirfoxMiranda::csmThread (processCsmId = [%u]) - started", mirfoxDataPtr->processCsmId);

	int i = CSMTHREAD_FIRSTRUN_TIME;		//first run after 10s	(100 * 100ms)
	for (;;)
	{
		if (i <= 0){
			i = CSMTHREAD_NEXTRUN_TIME;		//next run after 10s

			logger->log_p(L"MirfoxMiranda::csmThread (processCsmId = [%u]) - checking", mirfoxDataPtr->processCsmId);

			boost::interprocess::windows_shared_memory* checkedCsm =
					sharedMemoryUtils->getSmById(sharedMemoryUtils->getCsmName(), sharedMemoryUtils->getCsmTotalSize());

			// - check if our csm record is correct
			int result = sharedMemoryUtils->checkCSM(checkedCsm, mirfoxDataPtr->processCsmId, mirandaUtils->getDisplayName());

			if (result == 1){
				//wrong record in CSM - try to recreate own record
				logger->log_p(L"MirfoxMiranda::csmThread (old processCsmId = [%u]) - checkCSM returned 1, try to recreate record in CSM", mirfoxDataPtr->processCsmId);

				mirfoxDataPtr->setPluginState(MFENUM_PLUGIN_STATE_INIT);
				
				uint16_t unloadedMQProcessId = mirfoxDataPtr->processCsmId;
				//refresh msm
				mirfoxMirandaPtr->initializeSharedMemoryData(*mirfoxDataPtr, sharedMemoryUtils);
				if (unloadedMQProcessId != mirfoxDataPtr->processCsmId){
					//refresh miranda message queue if id changed
					mirfoxMirandaPtr->unloadMessageQueue(unloadedMQProcessId);
					mirfoxMirandaPtr->initializeMessageQueue(*mirfoxDataPtr);
				}
				
				if (mirfoxDataPtr->getPluginState() != MFENUM_PLUGIN_STATE_ERROR){
					
					sharedMemoryUtils->commitSM();
					
					mirfoxDataPtr->setPluginState(MFENUM_PLUGIN_STATE_WORK);
					result = 0; //ok
					logger->log_p(L"MirfoxMiranda::csmThread - Success of recreating own record in CSM and own MSMs.  new processCsmId = [%u]", mirfoxDataPtr->processCsmId);
					
				}

			}

			if (result != 0){ //can't recreate own shared memory record
				logger->log_p(L"ERROR. CSM record data is still corrupted. goto plugin error state   result: %i", result);
				mirfoxDataPtr->setPluginState(MFENUM_PLUGIN_STATE_ERROR);
				break; //exit thread
			}

			// - update our timestamp
			sharedMemoryUtils->updateCsmTimestamp(*checkedCsm, mirfoxDataPtr->processCsmId);

			// - delete another records with too old timestamps
			sharedMemoryUtils->checkAnotherCsm(*checkedCsm, mirfoxDataPtr->processCsmId);

			//delete checkedCsm object, clear handle
			delete checkedCsm;

		}

		i--;
		SleepEx(CSMTHREAD_TICK_TIME, TRUE);	//check exit every 0,1s

		//if miranda is exiting - exit this thread
		if (Miranda_Terminated() || mirfoxDataPtr->Plugin_Terminated){
			logger->log_p(L"MirfoxMiranda::csmThread break by Plugin_Terminated (=%d) or Miranda_Terminated()", mirfoxDataPtr->Plugin_Terminated);
			break;
		}

	}

	mirfoxDataPtr->workerThreadsCount--;
	Thread_Pop();
	return;

}

void CMirfoxMiranda::initializeMessageQueue(MirfoxData& mirfoxData)
{

	MessageQueueUtils* messageQueueUtils = MessageQueueUtils::getInstance();

	//get name of message queue for this client
	std::string mqName = messageQueueUtils->getMqName(mirfoxData.processCsmId);
	//create own mq
	int result = messageQueueUtils->createMessageQueue(mqName);
	if (result > 0){
		mirfoxData.setPluginState(MFENUM_PLUGIN_STATE_ERROR);
		//sm will not be commited
		return;
	}

	//Create new thread to maintain actions from message queue
	mir_forkthread(CMirfoxMiranda::msgQueueThread, this);

}

void CMirfoxMiranda::unloadMessageQueue(uint16_t unloadedMQProcessId)
{

	MessageQueueUtils* messageQueueUtils = MessageQueueUtils::getInstance();
	messageQueueUtils->unloadMessageQueue(unloadedMQProcessId);

}

void CMirfoxMiranda::msgQueueThread(void* threadArg)
{
	Thread_Push(0);

	CMirfoxMiranda* mirfoxMirandaPtr = (CMirfoxMiranda*)threadArg;
	MirfoxData* mirfoxDataPtr = &(mirfoxMirandaPtr->getMirfoxData());
	uint16_t myProcessCsmId = mirfoxDataPtr->processCsmId;
	MFLogger* logger = MFLogger::getInstance();

	mirfoxDataPtr->workerThreadsCount++;

	logger->log_p(L"MirfoxMiranda::msgQueueThread - started for processCsmId = [%u]", myProcessCsmId);

	MessageQueueUtils* messageQueueUtils = MessageQueueUtils::getInstance();
	MirandaUtils* mirandaUtils = MirandaUtils::getInstance();

	char menuItemType;
	char userActionType;
	char userButton;
	uint64_t targetHandle;
	wchar_t* userActionSelection = new wchar_t[MQCONST_MQSM_TEXT_SIZEC + 1];


	//infinite loop for listening to messages from queue
	int i = MQTHREAD_FIRSTRUN_TIME;		//first run after 0s	(0 * 100ms)
	for (;;)
	{
		if (i<=0){
			i = MQTHREAD_NEXTRUN_TIME;		//next run after 0,10s

			if (messageQueueUtils->tryReceiveMessage(menuItemType, userActionType, userButton, targetHandle, userActionSelection, MQCONST_MQSM_TEXT_SIZEC + 1) == true){
				//message received, variables are available

				logger->log_p(L"mqThread: message received   menuItemType = [%c]  userActionType = [%c]  userButton = [%c]  targetHandle = [%I64u]",
						menuItemType, userActionType, userButton, targetHandle);

				if (menuItemType == 'B'){

					//for B - one action thread per one account needed

					//for all enabled accounts;
					boost::ptr_list<MirandaAccount>* mirandaAccountsPtr = mirfoxDataPtr->getMirandaAccounts();
					boost::ptr_list<MirandaAccount>::iterator mirandaAccountsIter;
					for (mirandaAccountsIter = mirandaAccountsPtr->begin(); mirandaAccountsIter != mirandaAccountsPtr->end(); mirandaAccountsIter++){
						if (mirandaAccountsIter->accountState == MFENUM_MIRANDAACCOUNT_STATE_ON){

							ActionThreadArgStruct* actionThreadArgPtr = new(ActionThreadArgStruct);

							actionThreadArgPtr->userActionType = userActionType;
							actionThreadArgPtr->menuItemType = 'A';
							actionThreadArgPtr->userButton = userButton;

							actionThreadArgPtr->targetHandle = NULL;

							std::size_t mnSize = strlen(mirandaAccountsIter->szModuleName) + 1;
							char* accountSzModuleName = new char[mnSize];
							memset(accountSzModuleName, 0, mnSize * sizeof(char));
							strcpy_s(accountSzModuleName, mnSize, mirandaAccountsIter->szModuleName);
							actionThreadArgPtr->accountSzModuleName = accountSzModuleName;

							std::size_t uasSize = mir_wstrlen(userActionSelection) + 1;
							actionThreadArgPtr->userActionSelection = new wchar_t[uasSize];
							memset(actionThreadArgPtr->userActionSelection, 0, uasSize * sizeof(wchar_t));
							wcsncpy_s(actionThreadArgPtr->userActionSelection, uasSize, userActionSelection, uasSize - 1);

							actionThreadArgPtr->instancePtr = mirandaUtils;
							actionThreadArgPtr->mirfoxDataPtr = mirfoxDataPtr;

							mir_forkthread(MirandaUtils::userActionThread, actionThreadArgPtr);

						}
					}

				} else {

					//for A and C - one action thread needed

					ActionThreadArgStruct* actionThreadArgPtr = new(ActionThreadArgStruct);

					actionThreadArgPtr->userActionType = userActionType;
					actionThreadArgPtr->menuItemType = menuItemType;
					actionThreadArgPtr->userButton = userButton;

					actionThreadArgPtr->targetHandle = (HANDLE)targetHandle;
					if (menuItemType == 'A'){ //action on account
						actionThreadArgPtr->accountSzModuleName = mirfoxDataPtr->getAccountSzModuleNameById(targetHandle);
					}

					std::size_t uasSize = mir_wstrlen(userActionSelection) + 1;
					actionThreadArgPtr->userActionSelection = new wchar_t[uasSize];
					memset(actionThreadArgPtr->userActionSelection, 0, uasSize * sizeof(wchar_t));
					wcsncpy_s(actionThreadArgPtr->userActionSelection, uasSize, userActionSelection, uasSize - 1);

					actionThreadArgPtr->instancePtr = mirandaUtils;
					actionThreadArgPtr->mirfoxDataPtr = mirfoxDataPtr;

					mir_forkthread(MirandaUtils::userActionThread, actionThreadArgPtr);

				}
			}
		}

		i--;
		SleepEx(MQTHREAD_TICK_TIME, TRUE);	//check exit every 0,1s
	
		//if miranda is exiting - exit this thread
		if (Miranda_Terminated() || mirfoxDataPtr->Plugin_Terminated){
			logger->log_p(L"mqThread break by Plugin_Terminated (=%d) or Miranda_Terminated()", mirfoxDataPtr->Plugin_Terminated);
			delete [] userActionSelection;
			break;
		}
		if(messageQueueUtils->unloadedMQProcessId == myProcessCsmId){
			messageQueueUtils->unloadedMQProcessId = -1;
			logger->log_p(L"mqThread [%u]: returning. unloadedMQProcessId == myProcessCsmId", myProcessCsmId);
			delete [] userActionSelection;
			break;
		}

	}

	mirfoxDataPtr->workerThreadsCount--;
	Thread_Pop();
	return;
}