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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
/*
Import plugin for Miranda NG
Copyright (C) 2012 George Hazan
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "import.h"
time_t dwSinceDate = 0;
HWND hdlgProgress;
static DWORD nDupes, nContactsCount, nMessagesCount, nGroupsCount, nSkippedEvents, nSkippedContacts;
static MIDatabase *srcDb, *dstDb;
/////////////////////////////////////////////////////////////////////////////////////////
static int myGet(MCONTACT hContact, const char *szModule, const char *szSetting, DBVARIANT *dbv)
{
dbv->type = 0;
return srcDb->GetContactSetting(hContact, szModule, szSetting, dbv);
}
static TCHAR* myGetWs(MCONTACT hContact, const char *szModule, const char *szSetting)
{
DBVARIANT dbv = { DBVT_TCHAR };
return srcDb->GetContactSettingStr(hContact, szModule, szSetting, &dbv) ? NULL : dbv.ptszVal;
}
static BOOL myGetS(MCONTACT hContact, const char *szModule, const char *szSetting, char* dest)
{
DBVARIANT dbv = { DBVT_ASCIIZ };
dbv.pszVal = dest; dbv.cchVal = 100;
return srcDb->GetContactSettingStatic(hContact, szModule, szSetting, &dbv);
}
static void mySet(MCONTACT hContact, const char *module, const char *var, DBVARIANT *dbv)
{
DBCONTACTWRITESETTING dbw;
dbw.szModule = module;
dbw.szSetting = var;
dbw.value = *dbv;
dstDb->WriteContactSetting(hContact, &dbw);
}
/////////////////////////////////////////////////////////////////////////////////////////
static MCONTACT HContactFromNumericID(char *pszProtoName, char *pszSetting, DWORD dwID)
{
MCONTACT hContact = dstDb->FindFirstContact();
while (hContact != NULL) {
if ( db_get_dw((HCONTACT)hContact, pszProtoName, pszSetting, 0) == dwID) {
char* szProto = GetContactProto((HCONTACT)hContact);
if (szProto != NULL && !lstrcmpA(szProto, pszProtoName))
return hContact;
}
hContact = dstDb->FindNextContact((MCONTACT)hContact);
}
return INVALID_CONTACT_ID;
}
static MCONTACT HContactFromID(char *pszProtoName, char *pszSetting, TCHAR *pwszID)
{
MCONTACT hContact = dstDb->FindFirstContact();
while (hContact != NULL) {
char* szProto = GetContactProto((HCONTACT)hContact);
if ( !lstrcmpA(szProto, pszProtoName)) {
ptrW id(db_get_tsa((HCONTACT)hContact, pszProtoName, pszSetting));
if ( !lstrcmp(pwszID, id))
return hContact;
}
hContact = (MCONTACT)dstDb->FindNextContact(hContact);
}
return INVALID_CONTACT_ID;
}
static MCONTACT HistoryImportFindContact(HWND hdlgProgress, char* szModuleName, DWORD uin, int addUnknown)
{
MCONTACT hContact = HContactFromNumericID(szModuleName, "UIN", uin);
if (hContact == NULL) {
AddMessage( LPGENT("Ignored event from/to self"));
return INVALID_CONTACT_ID;
}
if (hContact != INVALID_CONTACT_ID)
return hContact;
if (!addUnknown)
return INVALID_CONTACT_ID;
hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)szModuleName);
db_set_dw((HCONTACT)hContact, szModuleName, "UIN", uin);
AddMessage( LPGENT("Added contact %u (found in history)"), uin );
return hContact;
}
/////////////////////////////////////////////////////////////////////////////////////////
static MCONTACT AddContact(HWND hdlgProgress, char* szProto, char* pszUniqueSetting, DBVARIANT* id, const TCHAR* pszUserID, TCHAR *nick, TCHAR *group)
{
MCONTACT hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
if (CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)szProto) != 0) {
CallService(MS_DB_CONTACT_DELETE, hContact, 0);
AddMessage(LPGENT("Failed to add %S contact %s"), szProto, pszUserID);
return INVALID_CONTACT_ID;
}
mySet(hContact, szProto, pszUniqueSetting, id);
CreateGroup(group, (HCONTACT)hContact);
if (nick && *nick) {
db_set_ws((HCONTACT)hContact, "CList", "MyHandle", nick);
AddMessage(LPGENT("Added %S contact %s, '%s'"), szProto, pszUserID, nick);
}
else AddMessage(LPGENT("Added %S contact %s"), szProto, pszUserID);
srcDb->FreeVariant(id);
return hContact;
}
static int ImportGroup(const char* szSettingName, LPARAM lParam)
{
int* pnGroups = (int*)lParam;
TCHAR* tszGroup = myGetWs(NULL, "CListGroups", szSettingName);
if (tszGroup != NULL) {
if ( CreateGroup( tszGroup+1, NULL ))
pnGroups[0]++;
mir_free(tszGroup);
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
static int ImportGroups()
{
int nGroups = 0;
DBCONTACTENUMSETTINGS param = { 0 };
param.szModule = "CListGroups";
param.pfnEnumProc = ImportGroup;
param.lParam = (LPARAM)&nGroups;
srcDb->EnumContactSettings(NULL, ¶m);
return nGroups;
}
static MCONTACT ImportContact(MCONTACT hSrc)
{
MCONTACT hDst;
TCHAR id[ 40 ], *pszUniqueID;
char szProto[100];
// Check what protocol this contact belongs to
if (myGetS(hSrc, "Protocol", "p", szProto)) {
AddMessage(LPGENT("Skipping contact with no protocol"));
return NULL;
}
if (!IsProtocolLoaded(szProto)) {
AddMessage(LPGENT("Skipping contact, %S not installed."), szProto);
return NULL;
}
// Skip protocols with no unique id setting (some non IM protocols return NULL)
char* pszUniqueSetting = (char*)CallProtoService(szProto, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0);
if (!pszUniqueSetting || (INT_PTR)pszUniqueSetting == CALLSERVICE_NOTFOUND) {
AddMessage(LPGENT("Skipping non-IM contact (%S)"), szProto);
return NULL;
}
DBVARIANT dbv;
if (myGet(hSrc, szProto, pszUniqueSetting, &dbv)) {
AddMessage(LPGENT("Skipping %S contact, ID not found"), szProto);
return NULL;
}
// Does the contact already exist?
switch (dbv.type) {
case DBVT_DWORD:
pszUniqueID = _ltot(dbv.dVal, id, 10);
hDst = HContactFromNumericID(szProto, pszUniqueSetting, dbv.dVal);
break;
case DBVT_ASCIIZ:
pszUniqueID = NEWTSTR_ALLOCA(_A2T(dbv.pszVal));
hDst = HContactFromID(szProto, pszUniqueSetting, pszUniqueID);
break;
case DBVT_WCHAR:
pszUniqueID = NEWTSTR_ALLOCA(dbv.ptszVal);
hDst = HContactFromID(szProto, pszUniqueSetting, pszUniqueID);
break;
}
if (hDst != INVALID_CONTACT_ID) {
AddMessage(LPGENT("Skipping duplicate %S contact %s"), szProto, pszUniqueID);
srcDb->FreeVariant(&dbv);
return NULL;
}
TCHAR *tszGroup = myGetWs(hSrc, "CList", "Group"), *tszNick = myGetWs(hSrc, "CList", "MyHandle");
if (tszNick == NULL)
tszNick = myGetWs(hSrc, szProto, "Nick");
hDst = AddContact(hdlgProgress, szProto, pszUniqueSetting, &dbv, pszUniqueID, tszNick, tszGroup);
mir_free(tszGroup), mir_free(tszNick);
if (hDst != INVALID_CONTACT_ID) {
// Hidden?
if (!myGet(hSrc, "CList", "Hidden", &dbv)) {
mySet(hDst, "CList", "Hidden", &dbv);
srcDb->FreeVariant(&dbv);
}
// Ignore settings
if (!myGet(hSrc, "Ignore", "Mask1", &dbv)) {
mySet(hDst, "Ignore", "Mask1", &dbv);
srcDb->FreeVariant(&dbv);
}
// Apparent mode
if (!myGet(hSrc, szProto, "ApparentMode", &dbv)) {
mySet(hDst, szProto, "ApparentMode", &dbv);
srcDb->FreeVariant(&dbv);
}
// Nick
if (!myGet(hSrc, szProto, "Nick", &dbv)) {
mySet(hDst, szProto, "Nick", &dbv);
srcDb->FreeVariant(&dbv);
}
// Myhandle
if (!myGet(hSrc, szProto, "MyHandle", &dbv)) {
mySet(hDst, szProto, "MyHandle", &dbv);
srcDb->FreeVariant(&dbv);
}
// First name
if (!myGet(hSrc, szProto, "FirstName", &dbv)) {
mySet(hDst, szProto, "FirstName", &dbv);
srcDb->FreeVariant(&dbv);
}
// Last name
if (!myGet(hSrc, szProto, "LastName", &dbv)) {
mySet(hDst, szProto, "LastName", &dbv);
srcDb->FreeVariant(&dbv);
}
// About
if (!myGet(hSrc, szProto, "About", &dbv)) {
mySet(hDst, szProto, "About", &dbv);
srcDb->FreeVariant(&dbv);
}
}
else AddMessage(LPGENT("Unknown error while adding %S contact %s"), szProto, pszUniqueID);
return hDst;
}
// This function should always be called after contact import. That is
// why there are no messages for errors related to contacts. Those
// would only be a repetition of the messages printed during contact
// import.
static MCONTACT convertContact(MCONTACT hContact)
{
// Check what protocol this contact belongs to
char szProto[100];
if (myGetS(hContact, "Protocol", "p", szProto))
return INVALID_CONTACT_ID;
// Protocol installed?
if (!IsProtocolLoaded(szProto))
return INVALID_CONTACT_ID;
// Is contact in database?
char* pszUniqueSetting = (char*)CallProtoService(szProto, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0);
// Skip protocols with no unique id setting (some non IM protocols return NULL)
MCONTACT hDst = INVALID_CONTACT_ID;
if (pszUniqueSetting && (INT_PTR)pszUniqueSetting != CALLSERVICE_NOTFOUND) {
DBVARIANT dbv;
if (!myGet(hContact, szProto, pszUniqueSetting, &dbv)) {
switch (dbv.type) {
case DBVT_DWORD:
hDst = HContactFromNumericID(szProto, pszUniqueSetting, dbv.dVal);
break;
case DBVT_ASCIIZ:
hDst = HContactFromID(szProto, pszUniqueSetting, _A2T(dbv.pszVal));
break;
case DBVT_WCHAR:
hDst = HContactFromID(szProto, pszUniqueSetting, dbv.ptszVal);
break;
}
srcDb->FreeVariant(&dbv);
}
}
return hDst;
}
static void ImportHistory(MCONTACT hContact, PROTOACCOUNT **protocol, int protoCount)
{
// Is it contats history import?
MCONTACT hDst = (protoCount == 0) ? convertContact(hContact) : NULL; //system history import
// OK to import this chain?
if (hDst == INVALID_CONTACT_ID) {
nSkippedContacts++;
return;
}
int i = 0, skipAll = 0;
DWORD cbAlloc = 4096;
BYTE* eventBuf = (PBYTE)mir_alloc(cbAlloc);
bool bIsVoidContact = dstDb->GetEventCount(hDst) == 0;
// Get the start of the event chain
HANDLE hEvent = srcDb->FindFirstEvent(hContact);
while (hEvent) {
int skip = 0;
// Copy the event and import it
DBEVENTINFO dbei = { sizeof(DBEVENTINFO) };
dbei.cbBlob = srcDb->GetBlobSize(hEvent);
if (dbei.cbBlob > cbAlloc) {
cbAlloc = dbei.cbBlob + 4096 - dbei.cbBlob % 4096;
eventBuf = (PBYTE)mir_realloc(eventBuf, cbAlloc);
}
dbei.pBlob = eventBuf;
if (!srcDb->GetEvent(hEvent, &dbei)) {
// check protocols during system history import
if (hDst == NULL) {
skipAll = 1;
for (int i = 0; i < protoCount; i++)
if (!strcmp(dbei.szModule, protocol[i]->szModuleName)) {
skipAll = 0;
break;
}
skip = skipAll;
}
// custom filtering
if (!skip && nImportOption == IMPORT_CUSTOM) {
BOOL sent = (dbei.flags & DBEF_SENT);
if (dbei.timestamp < (DWORD)dwSinceDate)
skip = 1;
if (!skip) {
if (hDst) {
skip = 1;
switch (dbei.eventType) {
case EVENTTYPE_MESSAGE:
if ((sent ? IOPT_MSGSENT : IOPT_MSGRECV) & nCustomOptions)
skip = 0;
break;
case EVENTTYPE_FILE:
if ((sent ? IOPT_FILESENT : IOPT_FILERECV) & nCustomOptions)
skip = 0;
break;
case EVENTTYPE_URL:
if ((sent ? IOPT_URLSENT : IOPT_URLRECV) & nCustomOptions)
skip = 0;
break;
default:
if ((sent ? IOPT_OTHERSENT : IOPT_OTHERRECV) & nCustomOptions)
skip = 0;
break;
}
}
else if (!(nCustomOptions & IOPT_SYSTEM))
skip = 1;
}
if (skip)
nSkippedEvents++;
}
if (!skip) {
// Check for duplicate entries
if (!IsDuplicateEvent((HCONTACT)hDst, dbei)) {
// Add dbevent
if (!bIsVoidContact)
dbei.flags &= ~DBEF_FIRST;
if (dstDb->AddEvent(hDst, &dbei) != NULL)
nMessagesCount++;
else
AddMessage(LPGENT("Failed to add message"));
}
else
nDupes++;
}
}
if (!(i % 10)) {
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// skip this chain if needed
if (skipAll)
break;
// Get next event
hEvent = srcDb->FindNextEvent(hEvent);
i++;
}
mir_free(eventBuf);
}
/////////////////////////////////////////////////////////////////////////////////////////
void MirandaImport(HWND hdlg)
{
DWORD dwTimer;
char* pszModuleName = NULL;
// Just to keep the macros happy
hdlgProgress = hdlg;
if ((dstDb = GetCurrentDatabase()) == NULL) {
AddMessage(LPGENT("Error retrieving current profile, exiting."));
return;
}
DATABASELINK* dblink = FindDatabasePlugin(importFile);
if (dblink == NULL) {
AddMessage(LPGENT("There's no database driver to open the input file, exiting."));
return;
}
if ((srcDb = dblink->Load(importFile)) == NULL) {
AddMessage(LPGENT("Error loading source file, exiting."));
return;
}
// Reset statistics
nSkippedEvents = 0;
nDupes = 0;
nContactsCount = 0;
nMessagesCount = 0;
nGroupsCount = 0;
nSkippedContacts = 0;
SetProgress(0);
// Get number of contacts
int nNumberOfContacts = srcDb->GetContactCount();
AddMessage(LPGENT("Number of contacts in database: %d"), nNumberOfContacts);
AddMessage(_T(""));
// Configure database for fast writing
dstDb->SetCacheSafetyMode(FALSE);
// Start benchmark timer
dwTimer = time(NULL);
// Import Groups
if (nImportOption == IMPORT_ALL || (nCustomOptions & IOPT_GROUPS)) {
AddMessage(LPGENT("Importing groups."));
nGroupsCount = ImportGroups();
if (nGroupsCount == -1)
AddMessage(LPGENT("Group import failed."));
AddMessage(_T(""));
}
// End of Import Groups
// Import Contacts
if (nImportOption != IMPORT_CUSTOM || (nCustomOptions & IOPT_CONTACTS)) {
AddMessage(LPGENT("Importing contacts."));
int i = 1;
MCONTACT hContact = srcDb->FindFirstContact();
while (hContact != NULL) {
if (ImportContact(hContact))
nContactsCount++;
// Update progress bar
SetProgress(100 * i / nNumberOfContacts);
i++;
// Process queued messages
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Get next contact in chain
hContact = srcDb->FindNextContact(hContact);
}
}
else AddMessage(LPGENT("Skipping new contacts import."));
AddMessage(_T(""));
// End of Import Contacts
// Import history
if (nImportOption != IMPORT_CONTACTS) {
// Import NULL contact message chain
if (nImportOption == IMPORT_ALL || (nCustomOptions & IOPT_SYSTEM)) {
AddMessage(LPGENT("Importing system history."));
int protoCount;
PROTOACCOUNT **accs;
CallService(MS_PROTO_ENUMACCOUNTS, (WPARAM)&protoCount, (LPARAM)&accs);
if (protoCount > 0)
ImportHistory(NULL, accs, protoCount);
}
else AddMessage(LPGENT("Skipping system history import."));
AddMessage(_T(""));
// Import other contact messages
if (nImportOption == IMPORT_ALL || (nCustomOptions & 2046)) { // 2 - 1024 types
AddMessage(LPGENT("Importing history."));
MCONTACT hContact = srcDb->FindFirstContact();
for (int i = 1; hContact != NULL; i++) {
ImportHistory(hContact, NULL, NULL);
SetProgress(100 * i / nNumberOfContacts);
hContact = srcDb->FindNextContact(hContact);
}
}
else AddMessage(LPGENT("Skipping history import."));
AddMessage(_T(""));
}
// End of Import History
// Restore database writing mode
dstDb->SetCacheSafetyMode(TRUE);
// Clean up before exit
dblink->Unload(srcDb);
// Stop timer
dwTimer = time(NULL) - dwTimer;
// Print statistics
AddMessage(LPGENT("Import completed in %d seconds."), dwTimer);
SetProgress(100);
AddMessage((nImportOption == IMPORT_CONTACTS) ?
LPGENT("Added %d contacts and %d groups.") : LPGENT("Added %d contacts, %d groups and %d events."),
nContactsCount, nGroupsCount, nMessagesCount);
if (nImportOption != IMPORT_CONTACTS) {
if (nSkippedContacts)
AddMessage(LPGENT("Skipped %d contacts."), nSkippedContacts);
AddMessage((nImportOption == IMPORT_CUSTOM) ?
LPGENT("Skipped %d duplicates and %d filtered events.") : LPGENT("Skipped %d duplicates."),
nDupes, nSkippedEvents);
}
}
|