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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
/*
* Smart Auto Replier (SAR) - auto replier plugin for Miranda IM
*
* Copyright (C) 2004 - 2012 by Volodymyr M. Shcherbyna <volodymyr@shcherbyna.com>
*
* This file is part of SAR.
*
* SAR is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SAR is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SAR. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "rulesstorage.h"
#include "crc32static.h"
extern HINSTANCE hInst;
extern CCrushLog CRUSHLOGOBJ;
extern CMessagesHandler * g_pMessHandler;
/// ctor...
CRulesStorage::CRulesStorage(void) : m_hFile(NULL)
{
m_comItem.Message = NULL;
BEGIN_PROTECT_AND_LOG_CODE
MakeFullPath(m_szSettFileName, sizeof(m_szSettFileName), STORAGE_NAME);
InitializeCriticalSection(&m_critSect);
END_PROTECT_AND_LOG_CODE
}
/// dtor...
CRulesStorage::~CRulesStorage(void)
{
BEGIN_PROTECT_AND_LOG_CODE
DeleteCriticalSection(&m_critSect);
DeInit();
END_PROTECT_AND_LOG_CODE
}
bool CRulesStorage::Serialize(ISettingsStream *pSettings, DWORD & ObjectSize)
{
BEGIN_PROTECT_AND_LOG_CODE
Flush();
END_PROTECT_AND_LOG_CODE
return true;
}
bool CRulesStorage::Deserialize(DWORD & ObjectSize)
{
BEGIN_PROTECT_AND_LOG_CODE
Init();
END_PROTECT_AND_LOG_CODE
return false;
}
/// deiniting...
void CRulesStorage::DeInit(void)
{
BEGIN_PROTECT_AND_LOG_CODE
if (m_hFile && m_hFile != INVALID_HANDLE_VALUE)
CloseHandle(m_hFile);
m_hFile = NULL;
/// now release memory...
for (RulesHash::iterator it = m_hashTable.begin(); it != m_hashTable.end(); it++)
{
RULE_ITEM & i = it->second;
delete i.ContactName;
delete i.ReplyText;
delete i.RuleName;
}
m_hashTable.clear();
if (m_comItem.Message != NULL)
{
delete m_comItem.Message;
m_comItem.Message = NULL;
}
END_PROTECT_AND_LOG_CODE
}
/// writing a string into a file..
void CRulesStorage::RawWriteDataBufByChunk(LPTSTR str)
{
BEGIN_PROTECT_AND_LOG_CODE
int nLength = _tcslen(str) * sizeof(TCHAR);
DWORD dwWritten = 0;
BOOL bVal = FALSE;
bVal = WriteFile(m_hFile, &nLength, sizeof(nLength), &dwWritten, NULL);
if (!bVal || dwWritten != sizeof(nLength))
throw bVal;
bVal = WriteFile(m_hFile, str, nLength, &dwWritten, NULL);
if (!bVal || nLength != dwWritten)
{
throw bVal;
}
END_PROTECT_AND_LOG_CODE
}
/// flushing all data from memory into a file...
void CRulesStorage::Flush(void)
{
BEGIN_PROTECT_AND_LOG_CODE
RulesHash::iterator it;
SetFilePointer(m_hFile, NULL, NULL, FILE_BEGIN);
SetEndOfFile(m_hFile); /// clear old data
SetCommonMessages();
for (it = m_hashTable.begin(); it != m_hashTable.end(); it++)
{
FlushItem(it->second);
}
END_PROTECT_AND_LOG_CODE
}
/// n.c.
bool CRulesStorage::IsRuleMatch(RULE_METAINFO & info, LPTSTR & strMess, LPTSTR & lpIncomingMsg)
{
BEGIN_PROTECT_AND_LOG_CODE
EnterCriticalSection(&m_critSect);
RulesHash::iterator iter;
bool bExists = false;
DWORD dwCRC32 = 0;
CCrc32Static::StringCrc32(info.ContactName, dwCRC32);
iter = m_hashTable.find(dwCRC32);
CScriptsReader & screader = g_pMessHandler->getSettings().getScriptsReader();
if (iter != m_hashTable.end())
{
/// I have to make copy of ReplyText,
/// cause GetReturnedMessage will release ptr
/// that is stored rule
/// found this bug after 20 min. of debugging ;(
strMess = new TCHAR[_tcslen(iter->second.ReplyText) + (1 * sizeof(TCHAR))];
if (strMess == NULL) /// ooops...
{
//screader.GetReturnMessage(info.ContactName, strMess, lpIncomingMsg);
LeaveCriticalSection(&m_critSect);
return false;
}
memset(strMess, 0, _tcslen(iter->second.ReplyText) * sizeof(TCHAR) + (1 * sizeof(TCHAR)));
_tcscpy(strMess, iter->second.ReplyText);
bExists = true;
//screader.GetReturnMessage(iter->second.ContactName, strMess, lpIncomingMsg);
}
else
screader.GetReturnMessage(info.ContactName, strMess, lpIncomingMsg);
LeaveCriticalSection(&m_critSect);
return bExists;
END_PROTECT_AND_LOG_CODE
return false;
}
/// adding rule into memory...
DWORD CRulesStorage::AddRuleItem(RULE_ITEM item, bool & bExists)
{
BEGIN_PROTECT_AND_LOG_CODE
DWORD dwCRC32 = NULL;
CCrc32Static::StringCrc32(item.ContactName, dwCRC32);
RulesHash::iterator iter;
iter = m_hashTable.find(dwCRC32);
if (iter != m_hashTable.end())
{
bExists = true;
return 0;
}
bExists = false;
m_hashTable.insert(RulesHash::value_type(dwCRC32, item));
return dwCRC32;
END_PROTECT_AND_LOG_CODE
return 0;
}
/// flushing item...
void CRulesStorage::FlushItem(RULE_ITEM & item)
{
BEGIN_PROTECT_AND_LOG_CODE
RawWriteDataBufByChunk(item.RuleName);
RawWriteDataBufByChunk(item.ContactName);
RawWriteDataBufByChunk(item.ReplyText);
//RawWriteDataBufByChunk(item.ReplyAction);
END_PROTECT_AND_LOG_CODE
}
/// initing rules manager...
void CRulesStorage::Init(void)
{
#ifdef _DEBUG
//MessageBox (NULL, __FUNCTION__, __FILE__, MB_OK);
#endif
BEGIN_PROTECT_AND_LOG_CODE
if (m_hFile)
CloseHandle (m_hFile);
bool bFileExists = true;
if (!PathFileExists(m_szSettFileName))
{/// file does not exists...
/// need to fill it with common rules...
bFileExists = false;
}
m_hFile = CreateFile (m_szSettFileName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, (bFileExists ? OPEN_ALWAYS : CREATE_ALWAYS), FILE_ATTRIBUTE_NORMAL, NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
{/// show notification msg to user..
throw INVALID_HANDLE_VALUE;
}
if (bFileExists == false)
{
ClearCommonMessages();
m_comItem.Message = new TCHAR[sizeof(SETTINGS_DEF_MESSAGE) / sizeof(TCHAR) + sizeof(TCHAR)];
if (m_comItem.Message != NULL)
{
memset(m_comItem.Message, 0, sizeof(SETTINGS_DEF_MESSAGE) / sizeof(TCHAR) + sizeof(TCHAR));
_tcscpy(m_comItem.Message, SETTINGS_DEF_MESSAGE);
SetCommonMessages();
}
}
DWORD dwSize = 0;
dwSize = GetFileSize(m_hFile, &dwSize);
DWORD dwReaded = 0,
dwCrc32 = 0;
int nLength = 0;
/// suppose that func is failed
BOOL bReaded = FALSE;
bool bFailed = false;
if (bFileExists)
{
dwSize -= GetCommonMessages();
}
else
{
dwSize -= _tcslen(SETTINGS_DEF_MESSAGE) * sizeof(TCHAR);
dwSize -= 1 * sizeof(int);
}
while (dwSize)
{
RULE_ITEM it;
bReaded = ReadFile(m_hFile, &nLength, sizeof(nLength), &dwReaded, NULL);
if (bReaded == FALSE || dwReaded != sizeof(nLength))
{
bFailed = true;
break;
}
dwSize -= sizeof(nLength);
it.RuleName = new TCHAR[nLength / sizeof(TCHAR) + (1 * sizeof(TCHAR))];
if (it.RuleName == NULL)
{
bFailed = true;
break;
}
memset(it.RuleName, 0, (nLength + (1 * sizeof(TCHAR))));
bReaded = ReadFile(m_hFile, it.RuleName, nLength, &dwReaded, NULL);
if (bReaded == FALSE || dwReaded != (nLength))
{
delete it.RuleName;
bFailed = true;
break;
}
dwSize -= dwReaded;
bReaded = ReadFile(m_hFile, &nLength, sizeof(nLength), &dwReaded, NULL);
if (bReaded == FALSE || dwReaded != sizeof(nLength))
{
delete it.RuleName;
bFailed = true;
break;
}
dwSize -= sizeof(nLength);
it.ContactName = new TCHAR[nLength / sizeof(TCHAR) + (1 * sizeof(TCHAR))];
if (it.ContactName == NULL)
{
delete it.RuleName;
bFailed = true;
break;
}
memset(it.ContactName, 0, (nLength + (1 * sizeof(TCHAR))));
bReaded = ReadFile(m_hFile, it.ContactName, nLength, &dwReaded, NULL);
if (bReaded == FALSE || dwReaded != (nLength))
{
delete it.RuleName;
delete it.ContactName;
bFailed = true;
break;
}
dwSize -= dwReaded;
bReaded = ReadFile (m_hFile, &nLength, sizeof(nLength), &dwReaded, NULL);
if (bReaded == FALSE || dwReaded != sizeof(nLength))
{
delete it.RuleName;
delete it.ContactName;
bFailed = true;
break;
}
dwSize -= sizeof(nLength);
it.ReplyText = new TCHAR[nLength / sizeof(TCHAR) + (1 * sizeof(TCHAR))];
if (it.ReplyText == NULL)
{
delete it.RuleName;
delete it.ContactName;
bFailed = true;
break;
}
memset(it.ReplyText, 0, (nLength + (1 * sizeof(TCHAR))));
bReaded = ReadFile (m_hFile, it.ReplyText, nLength, &dwReaded, NULL);
if (bReaded == FALSE || dwReaded != (nLength))
{
delete it.RuleName;
delete it.ContactName;
delete it.ReplyText;
bFailed = true;
break;
}
dwSize -= dwReaded;
dwCrc32 = NULL;
CCrc32Static::StringCrc32(it.ContactName, dwCrc32);
m_hashTable.insert(RulesHash::value_type(dwCrc32, it) ); /// filling list
}
if (bFailed)
{ /// upps...
bool bdelete = NotifyAboutWrongSettings(m_szSettFileName);
if (bdelete)
{
CloseHandle (m_hFile);
m_hFile = NULL;
DeleteFile (m_szSettFileName);
DeInit();
Init();
}
}
END_PROTECT_AND_LOG_CODE
}
/// reading an data from file storage...
UINT CRulesStorage::RawReadDataBufByChunk(LPTSTR & szData)
{
BEGIN_PROTECT_AND_LOG_CODE
UINT nRetVal = 0;
if (m_hFile == NULL || m_hFile == INVALID_HANDLE_VALUE)
{
return nRetVal;
}
int nSizeOf = 0;
DWORD dwReaded = 0;
const int nsizeof = sizeof(nSizeOf);
BOOL bReaded = ReadFile(m_hFile, &nSizeOf, nsizeof, &dwReaded, NULL);
nRetVal += dwReaded;
if (bReaded == FALSE || dwReaded != nsizeof)
{
return nRetVal;
}
szData = new TCHAR[(nSizeOf / sizeof(TCHAR)) + (1 * sizeof(TCHAR))];
if (szData == NULL)
{
return nRetVal + dwReaded;
}
memset(szData, 0, nSizeOf + (1 * sizeof(TCHAR)));
bReaded = ReadFile(m_hFile, szData, nSizeOf, &dwReaded, NULL);
nRetVal += dwReaded;
if (bReaded == FALSE || dwReaded != nSizeOf)
{
delete szData;
szData = NULL;
return nRetVal;
}
return nRetVal;
END_PROTECT_AND_LOG_CODE
}
///
void CRulesStorage::SetCommonMessages(void)
{
BEGIN_PROTECT_AND_LOG_CODE
RawWriteDataBufByChunk(m_comItem.Message);
END_PROTECT_AND_LOG_CODE
}
void CRulesStorage::ClearCommonMessages(void)
{
if (m_comItem.Message)
{
delete m_comItem.Message;
m_comItem.Message = NULL;
}
}
/// reads from storage common rules - header and
/// message that is replyed to all users...
UINT CRulesStorage::GetCommonMessages(void)
{
BEGIN_PROTECT_AND_LOG_CODE
ClearCommonMessages();
UINT nRetVal = 0;
nRetVal += RawReadDataBufByChunk(m_comItem.Message);
return nRetVal;
END_PROTECT_AND_LOG_CODE
#undef RAW_DATACLEAR
return 0;
}
void CRulesStorage::setCommonRule(COMMON_RULE_ITEM r) /// n.c.
{
BEGIN_PROTECT_AND_LOG_CODE
memcpy (&m_comItem, &r, sizeof(COMMON_RULE_ITEM));
END_PROTECT_AND_LOG_CODE
}
COMMON_RULE_ITEM & CRulesStorage::getCommonRule(void) /// n.c.
{
BEGIN_PROTECT_AND_LOG_CODE
return m_comItem;
END_PROTECT_AND_LOG_CODE
}
/// getting hash table
RulesHash & CRulesStorage::getHashTable(void)
{
BEGIN_PROTECT_AND_LOG_CODE
return m_hashTable;
END_PROTECT_AND_LOG_CODE
}
/// remove rule by its id
bool CRulesStorage::RemReplyAction(DWORD dwCrc32)
{
bool bRetVal = false;
BEGIN_PROTECT_AND_LOG_CODE
EnterCriticalSection(&m_critSect);
RulesHash::iterator it;
it = m_hashTable.find(dwCrc32);
if (it != m_hashTable.end())
{
delete it->second.ContactName;
delete it->second.ReplyText;
delete it->second.RuleName;
m_hashTable.erase(it);
bRetVal = true;
}
LeaveCriticalSection(&m_critSect);
END_PROTECT_AND_LOG_CODE
return bRetVal;
}
bool CRulesStorage::RuleIsRegistered(LPTSTR lpContactName)
{
BEGIN_PROTECT_AND_LOG_CODE
bool bRetVal = false;
if (lpContactName == NULL)
return bRetVal;
DWORD dwCRC32 = NULL;
CCrc32Static::StringCrc32(lpContactName, dwCRC32);
RulesHash::iterator iter;
EnterCriticalSection(&m_critSect);
iter = m_hashTable.find(dwCRC32);
if (iter != m_hashTable.end())
{
bRetVal = true;
}
LeaveCriticalSection(&m_critSect);
return bRetVal;
END_PROTECT_AND_LOG_CODE
return false;
}
|