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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
|
/*
Import plugin for Miranda NG
Copyright (C) 2012-14 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"
#define SetProgress(n) SendMessage(hdlgProgress,PROGM_SETPROGRESS,n,0)
struct AccountMap
{
AccountMap(const char *_src, const char *_dst) :
szSrcAcc(mir_strdup(_src)),
szDstAcc(mir_strdup(_dst))
{}
~AccountMap() {}
ptrA szSrcAcc, szDstAcc;
};
static int CompareAccs(const AccountMap *p1, const AccountMap *p2)
{ return stricmp(p1->szSrcAcc, p2->szSrcAcc);
}
/////////////////////////////////////////////////////////////////////////////////////////
struct ContactMap
{
ContactMap(MCONTACT _src, MCONTACT _dst) :
srcID(_src),
dstID(_dst)
{}
MCONTACT srcID, dstID;
};
/////////////////////////////////////////////////////////////////////////////////////////
static OBJLIST<AccountMap> arAccountMap(5, CompareAccs);
static OBJLIST<ContactMap> arContactMap(50, NumericKeySortT);
static LIST<DBCachedContact> arMetas(10);
/////////////////////////////////////////////////////////////////////////////////////////
// local data
static HWND hdlgProgress;
static DWORD nDupes, nContactsCount, nMessagesCount, nGroupsCount, nSkippedEvents, nSkippedContacts;
static MIDatabase *srcDb, *dstDb;
/////////////////////////////////////////////////////////////////////////////////////////
void AddMessage(const TCHAR* fmt, ...)
{
va_list args;
TCHAR msgBuf[4096];
va_start(args, fmt);
mir_vsntprintf(msgBuf, SIZEOF(msgBuf), TranslateTS(fmt), args);
SendMessage(hdlgProgress, PROGM_ADDMESSAGE, 0, (LPARAM)msgBuf);
}
/////////////////////////////////////////////////////////////////////////////////////////
static bool CompareDb(DBVARIANT &dbv1, DBVARIANT &dbv2)
{
if (dbv1.type == dbv2.type) {
switch (dbv1.type) {
case DBVT_DWORD:
return dbv1.dVal == dbv2.dVal;
case DBVT_ASCIIZ:
case DBVT_UTF8:
return 0 == strcmp(dbv1.pszVal, dbv2.pszVal);
}
}
return false;
}
static int myGet(MCONTACT hContact, const char *szModule, const char *szSetting, DBVARIANT *dbv)
{
dbv->type = 0;
return srcDb->GetContactSetting(hContact, szModule, szSetting, dbv);
}
static int myGetD(MCONTACT hContact, const char *szModule, const char *szSetting, int iDefault)
{
DBVARIANT dbv = { DBVT_DWORD };
return srcDb->GetContactSetting(hContact, szModule, szSetting, &dbv) ? iDefault : dbv.dVal;
}
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 MCONTACT HContactFromNumericID(char *pszProtoName, char *pszSetting, DWORD dwID)
{
for (MCONTACT hContact = dstDb->FindFirstContact(); hContact; hContact = dstDb->FindNextContact(hContact)) {
if (db_get_dw(hContact, pszProtoName, pszSetting, 0) == dwID) {
char* szProto = GetContactProto(hContact);
if (szProto != NULL && !lstrcmpA(szProto, pszProtoName))
return hContact;
}
}
return INVALID_CONTACT_ID;
}
static MCONTACT HContactFromID(char *pszProtoName, char *pszSetting, TCHAR *pwszID)
{
for (MCONTACT hContact = dstDb->FindFirstContact(); hContact; hContact = dstDb->FindNextContact(hContact)) {
char *szProto = GetContactProto(hContact);
if (!lstrcmpA(szProto, pszProtoName)) {
ptrW id(db_get_tsa(hContact, pszProtoName, pszSetting));
if (!lstrcmp(pwszID, id))
return 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 = CallService(MS_DB_CONTACT_ADD, 0, 0);
CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)szModuleName);
db_set_dw(hContact, szModuleName, "UIN", uin);
AddMessage(LPGENT("Added contact %u (found in history)"), uin);
return hContact;
}
/////////////////////////////////////////////////////////////////////////////////////////
static int CopySettingsEnum(const char *szSetting, LPARAM lParam)
{
LIST<char> *pSettings = (LIST<char>*)lParam;
pSettings->insert(mir_strdup(szSetting));
return 0;
}
void CopySettings(MCONTACT srcID, const char *szSrcModule, MCONTACT dstID, const char *szDstModule)
{
LIST<char> arSettings(50);
DBCONTACTENUMSETTINGS dbces = { 0 };
dbces.szModule = szSrcModule;
dbces.pfnEnumProc = CopySettingsEnum;
dbces.lParam = (LPARAM)&arSettings;
srcDb->EnumContactSettings(srcID, &dbces);
for (int i = arSettings.getCount() - 1; i >= 0; i--) {
DBVARIANT dbv = { 0 };
if (!srcDb->GetContactSetting(srcID, szSrcModule, arSettings[i], &dbv))
db_set(dstID, szDstModule, arSettings[i], &dbv);
mir_free(arSettings[i]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
static PROTOACCOUNT* FindMyAccount(const char *szProto, const char *szBaseProto)
{
int destProtoCount;
PROTOACCOUNT **destAccs;
CallService(MS_PROTO_ENUMACCOUNTS, (WPARAM)&destProtoCount, (LPARAM)&destAccs);
for (int i = 0; i < destProtoCount; i++) {
PROTOACCOUNT *pa = destAccs[i];
if (lstrcmpA(pa->szProtoName, szBaseProto))
continue;
// these protocols have no accounts, and their name match -> success
if (pa->bOldProto || pa->bIsVirtual || pa->bDynDisabled)
return pa;
char *pszUniqueSetting = (char*)CallProtoService(pa->szModuleName, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0);
if (!pszUniqueSetting || INT_PTR(pszUniqueSetting) == CALLSERVICE_NOTFOUND)
continue;
DBVARIANT dbSrc, dbDst;
if (dstDb->GetContactSetting(NULL, pa->szModuleName, pszUniqueSetting, &dbDst))
continue;
bool bEqual = false;
if (!myGet(NULL, szProto, pszUniqueSetting, &dbSrc)) {
bEqual = CompareDb(dbSrc, dbDst);
srcDb->FreeVariant(&dbSrc);
}
dstDb->FreeVariant(&dbDst);
if (bEqual)
return pa;
}
return NULL;
}
void ImportAccounts()
{
int protoCount = myGetD(NULL, "Protocols", "ProtoCount", 0);
for (int i = 0; i < protoCount; i++) {
char szSetting[100], szProto[100];
itoa(i, szSetting, 10);
if (myGetS(NULL, "Protocols", szSetting, szProto))
continue;
// check if it's an account-based proto or an old style proto
char szBaseProto[100];
if (myGetS(NULL, szProto, "AM_BaseProto", szBaseProto)) {
arAccountMap.insert(new AccountMap(szProto, NULL));
continue;
}
PROTOACCOUNT *pa = FindMyAccount(szProto, szBaseProto);
if (pa) {
arAccountMap.insert(new AccountMap(szProto, pa->szModuleName));
continue;
}
itoa(800 + i, szSetting, 10);
ptrT tszAccountName(myGetWs(NULL, "Protocols", szSetting));
if (tszAccountName == NULL)
tszAccountName = mir_a2t(szProto);
ACC_CREATE newacc;
newacc.pszBaseProto = szBaseProto;
newacc.pszInternal = NULL;
newacc.ptszAccountName = tszAccountName;
pa = ProtoCreateAccount(&newacc);
if (pa == NULL) {
arAccountMap.insert(new AccountMap(szProto, NULL));
continue;
}
arAccountMap.insert(new AccountMap(szProto, pa->szModuleName));
itoa(400 + i, szSetting, 10);
int iVal = myGetD(NULL, "Protocols", szSetting, 1);
itoa(400 + pa->iOrder, szSetting, 10);
db_set_dw(NULL, "Protocols", szSetting, iVal);
pa->bIsVisible = iVal != 0;
itoa(600 + i, szSetting, 10);
iVal = myGetD(NULL, "Protocols", szSetting, 1);
itoa(600 + pa->iOrder, szSetting, 10);
db_set_dw(NULL, "Protocols", szSetting, iVal);
pa->bIsEnabled = iVal != 0;
CopySettings(NULL, szProto, NULL, pa->szModuleName);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
static MCONTACT MapContact(MCONTACT hSrc)
{
ContactMap *pDestContact = arContactMap.find((ContactMap*)&hSrc);
return (pDestContact == NULL) ? INVALID_CONTACT_ID : pDestContact->dstID;
}
static MCONTACT AddContact(HWND hdlgProgress, char* szProto, char* pszUniqueSetting, DBVARIANT* id, const TCHAR* pszUserID, TCHAR *nick, TCHAR *group)
{
MCONTACT hContact = 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;
}
db_set(hContact, szProto, pszUniqueSetting, id);
CreateGroup(group, hContact);
if (nick && *nick) {
db_set_ws(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;
}
void ImportContactSettings(AccountMap *pda, MCONTACT hSrc, MCONTACT hDst)
{
// Hidden?
DBVARIANT dbv;
if (!myGet(hSrc, "CList", "Hidden", &dbv)) {
db_set(hDst, "CList", "Hidden", &dbv);
srcDb->FreeVariant(&dbv);
}
// Ignore settings
if (!myGet(hSrc, "Ignore", "Mask1", &dbv)) {
db_set(hDst, "Ignore", "Mask1", &dbv);
srcDb->FreeVariant(&dbv);
}
// Apparent mode
if (!myGet(hSrc, pda->szSrcAcc, "ApparentMode", &dbv)) {
db_set(hDst, pda->szDstAcc, "ApparentMode", &dbv);
srcDb->FreeVariant(&dbv);
}
// Nick
if (!myGet(hSrc, pda->szSrcAcc, "Nick", &dbv)) {
db_set(hDst, pda->szDstAcc, "Nick", &dbv);
srcDb->FreeVariant(&dbv);
}
// Myhandle
if (!myGet(hSrc, pda->szSrcAcc, "MyHandle", &dbv)) {
db_set(hDst, pda->szDstAcc, "MyHandle", &dbv);
srcDb->FreeVariant(&dbv);
}
// First name
if (!myGet(hSrc, pda->szSrcAcc, "FirstName", &dbv)) {
db_set(hDst, pda->szDstAcc, "FirstName", &dbv);
srcDb->FreeVariant(&dbv);
}
// Last name
if (!myGet(hSrc, pda->szSrcAcc, "LastName", &dbv)) {
db_set(hDst, pda->szDstAcc, "LastName", &dbv);
srcDb->FreeVariant(&dbv);
}
// About
if (!myGet(hSrc, pda->szSrcAcc, "About", &dbv)) {
db_set(hDst, pda->szDstAcc, "About", &dbv);
srcDb->FreeVariant(&dbv);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
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;
}
/////////////////////////////////////////////////////////////////////////////////////////
DBCachedContact* FindDestMeta(DBCachedContact *ccSrc)
{
for (MCONTACT hMeta = dstDb->FindFirstContact(META_PROTO); hMeta != 0; hMeta = dstDb->FindNextContact(hMeta, META_PROTO)) {
DBCachedContact *cc = dstDb->m_cache->GetCachedContact(hMeta);
if (cc->nSubs != ccSrc->nSubs)
continue;
int i;
for (i = 0; i < ccSrc->nSubs; i++) {
MCONTACT hDest = MapContact(ccSrc->pSubs[i]);
if (hDest == INVALID_CONTACT_ID || ccSrc->pSubs[i] != hDest)
break;
}
if (i == ccSrc->nSubs)
return cc;
}
return NULL;
}
void ImportMeta(DBCachedContact *cc)
{
MCONTACT hDest;
DBCachedContact *ccDst = FindDestMeta(cc);
if (ccDst == NULL) {
hDest = CallService(MS_DB_CONTACT_ADD, 0, 0);
CallService(MS_PROTO_ADDTOCONTACT, hDest, (LPARAM)META_PROTO);
CopySettings(cc->contactID, META_PROTO, hDest, META_PROTO);
ccDst = dstDb->m_cache->GetCachedContact(hDest);
if (ccDst) {
ccDst->nDefault = cc->nDefault;
if (ccDst->nSubs = cc->nSubs) {
ccDst->pSubs = (MCONTACT*)mir_alloc(sizeof(MCONTACT)*cc->nSubs);
for (int i = 0; i < cc->nSubs; i++) {
ccDst->pSubs[i] = MapContact(cc->pSubs[i]);
char szSettingName[100];
mir_snprintf(szSettingName, SIZEOF(szSettingName), "Handle%d", i);
db_set_dw(hDest, META_PROTO, szSettingName, ccDst->pSubs[i]);
db_set_b(ccDst->pSubs[i], META_PROTO, "IsSubcontact", 1);
db_set_dw(ccDst->pSubs[i], META_PROTO, "ParentMeta", hDest);
DBCachedContact *ccSub = dstDb->m_cache->GetCachedContact(ccDst->pSubs[i]);
if (ccSub)
ccSub->parentID = hDest;
}
}
}
ptrT tszGroup(myGetWs(cc->contactID, "CList", "Group")), tszNick(myGetWs(cc->contactID, "CList", "MyHandle"));
if (tszNick == NULL)
tszNick = myGetWs(cc->contactID, cc->szProto, "Nick");
CreateGroup(tszGroup, hDest);
if (tszNick && *tszNick) {
db_set_ws(hDest, "CList", "MyHandle", tszNick);
AddMessage(LPGENT("Added metacontact '%s'"), tszNick);
}
else AddMessage(LPGENT("Added metacontact"));
}
else hDest = ccDst->contactID;
AccountMap pda(META_PROTO, META_PROTO);
ImportContactSettings(&pda, cc->contactID, hDest);
}
/////////////////////////////////////////////////////////////////////////////////////////
static MCONTACT ImportContact(MCONTACT hSrc)
{
TCHAR id[40], *pszUniqueID;
// Check what protocol this contact belongs to
DBCachedContact *cc = srcDb->m_cache->GetCachedContact(hSrc);
if (cc == NULL || cc->szProto == NULL) {
AddMessage(LPGENT("Skipping contact with no protocol"));
return NULL;
}
if (cc->IsMeta()) {
arMetas.insert(cc);
return NULL;
}
AccountMap *pda = arAccountMap.find((AccountMap*)&cc->szProto);
if (pda == NULL) {
AddMessage(LPGENT("Skipping contact, account %S cannot be mapped."), cc->szProto);
return NULL;
}
if (!IsProtocolLoaded(pda->szDstAcc)) {
AddMessage(LPGENT("Skipping contact, %S not installed."), cc->szProto);
return NULL;
}
// Skip protocols with no unique id setting (some non IM protocols return NULL)
char *pszUniqueSetting = (char*)CallProtoService(pda->szDstAcc, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0);
if (!pszUniqueSetting || (INT_PTR)pszUniqueSetting == CALLSERVICE_NOTFOUND) {
AddMessage(LPGENT("Skipping non-IM contact (%S)"), cc->szProto);
return NULL;
}
DBVARIANT dbv;
if (myGet(hSrc, cc->szProto, pszUniqueSetting, &dbv)) {
AddMessage(LPGENT("Skipping %S contact, ID not found"), cc->szProto);
return NULL;
}
// Does the contact already exist?
MCONTACT hDst;
switch (dbv.type) {
case DBVT_DWORD:
pszUniqueID = _ltot(dbv.dVal, id, 10);
hDst = HContactFromNumericID(cc->szProto, pszUniqueSetting, dbv.dVal);
break;
case DBVT_ASCIIZ:
case DBVT_UTF8:
pszUniqueID = NEWTSTR_ALLOCA(_A2T(dbv.pszVal));
hDst = HContactFromID(cc->szProto, pszUniqueSetting, pszUniqueID);
break;
}
if (hDst != INVALID_CONTACT_ID) {
AddMessage(LPGENT("Skipping duplicate %S contact %s"), cc->szProto, pszUniqueID);
srcDb->FreeVariant(&dbv);
return NULL;
}
ptrT tszGroup(myGetWs(hSrc, "CList", "Group")), tszNick(myGetWs(hSrc, "CList", "MyHandle"));
if (tszNick == NULL)
tszNick = myGetWs(hSrc, cc->szProto, "Nick");
hDst = AddContact(hdlgProgress, pda->szDstAcc, pszUniqueSetting, &dbv, pszUniqueID, tszNick, tszGroup);
if (hDst == INVALID_CONTACT_ID) {
AddMessage(LPGENT("Unknown error while adding %S contact %s"), pda->szDstAcc, pszUniqueID);
return INVALID_CONTACT_ID;
}
arContactMap.insert(new ContactMap(hSrc, hDst));
ImportContactSettings(pda, hSrc, hDst);
return hDst;
}
/////////////////////////////////////////////////////////////////////////////////////////
// contact's history import
static void ImportHistory(MCONTACT hContact, PROTOACCOUNT **protocol, int protoCount)
{
// Is it contats history import?
MCONTACT hDst = (protoCount == 0) ? MapContact(hContact) : NULL; //system history import
// OK to import this chain?
if (hDst == INVALID_CONTACT_ID) {
nSkippedContacts++;
return;
}
bool bSkipAll = false;
DWORD cbAlloc = 4096;
BYTE* eventBuf = (PBYTE)mir_alloc(cbAlloc);
// Get the start of the event chain
HANDLE hEvent = srcDb->FindFirstEvent(hContact);
for (int i = 0; hEvent; i++, hEvent = srcDb->FindNextEvent(hContact, hEvent)) {
// 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;
bool bSkipThis = false;
if (!srcDb->GetEvent(hEvent, &dbei)) {
// check protocols during system history import
if (hDst == NULL) {
bSkipAll = true;
for (int i = 0; i < protoCount; i++) {
if (!strcmp(dbei.szModule, protocol[i]->szModuleName)) {
bSkipAll = false;
break;
}
}
bSkipThis = bSkipAll;
}
// custom filtering
if (!bSkipThis && nImportOption == IMPORT_CUSTOM) {
BOOL sent = (dbei.flags & DBEF_SENT);
if (dbei.timestamp < (DWORD)dwSinceDate)
bSkipThis = 1;
if (!bSkipThis) {
if (hDst) {
bSkipThis = 1;
switch (dbei.eventType) {
case EVENTTYPE_MESSAGE:
if ((sent ? IOPT_MSGSENT : IOPT_MSGRECV) & nCustomOptions)
bSkipThis = 0;
break;
case EVENTTYPE_FILE:
if ((sent ? IOPT_FILESENT : IOPT_FILERECV) & nCustomOptions)
bSkipThis = 0;
break;
case EVENTTYPE_URL:
if ((sent ? IOPT_URLSENT : IOPT_URLRECV) & nCustomOptions)
bSkipThis = 0;
break;
default:
if ((sent ? IOPT_OTHERSENT : IOPT_OTHERRECV) & nCustomOptions)
bSkipThis = 0;
break;
}
}
else if (!(nCustomOptions & IOPT_SYSTEM))
bSkipThis = 1;
}
if (bSkipThis)
nSkippedEvents++;
}
if (!bSkipThis) {
// Check for duplicate entries
if (!IsDuplicateEvent(hDst, dbei)) {
// no need to display all these dialogs again
if (dbei.eventType == EVENTTYPE_AUTHREQUEST || dbei.eventType == EVENTTYPE_ADDED)
dbei.flags |= DBEF_READ;
// Add dbevent
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);
}
}
// bSkipThis this chain if needed
if (bSkipAll)
break;
}
mir_free(eventBuf);
}
/////////////////////////////////////////////////////////////////////////////////////////
void MirandaImport(HWND hdlg)
{
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, TRUE)) == 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
DWORD dwTimer = time(NULL);
ImportAccounts();
// 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);
}
for (i = 0; i < arMetas.getCount(); i++)
ImportMeta(arMetas[i]);
}
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);
}
arMetas.destroy();
arAccountMap.destroy();
arContactMap.destroy();
}
|