From 7fb93c40e30b6a88b1e8de5dfa09284aca50a891 Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Thu, 11 Jan 2018 17:20:13 +0300
Subject: fix for importing unknown / broken subs

---
 plugins/Import/src/import.cpp | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

(limited to 'plugins')

diff --git a/plugins/Import/src/import.cpp b/plugins/Import/src/import.cpp
index bbb39911be..2d4e756943 100644
--- a/plugins/Import/src/import.cpp
+++ b/plugins/Import/src/import.cpp
@@ -671,16 +671,27 @@ void ImportMeta(DBCachedContact *ccSrc)
 			if (ccDst->nSubs = ccSrc->nSubs) {
 				ccDst->pSubs = (MCONTACT*)mir_alloc(sizeof(MCONTACT)*ccSrc->nSubs);
 				for (int i = 0; i < ccSrc->nSubs; i++) {
-					ccDst->pSubs[i] = MapContact(ccSrc->pSubs[i]);
+					MCONTACT hSub = MapContact(ccSrc->pSubs[i]);
+					if (hSub == INVALID_CONTACT_ID) {
+						hSub = db_add_contact();
+
+						DBCachedContact *ccSub = srcDb->m_cache->GetCachedContact(ccSrc->pSubs[i]);
+						if (ccSub && ccSub->szProto) {
+							Proto_AddToContact(hDest, ccSub->szProto);
+							CopySettings(ccSrc->contactID, ccSub->szProto, hSub, ccSub->szProto);
+						}
+					}
+
+					ccDst->pSubs[i] = hSub;
 
 					char szSettingName[100];
 					mir_snprintf(szSettingName, "Handle%d", i);
-					db_set_dw(hDest, META_PROTO, szSettingName, ccDst->pSubs[i]);
+					db_set_dw(hDest, META_PROTO, szSettingName, hSub);
 
-					db_set_b(ccDst->pSubs[i], META_PROTO, "IsSubcontact", 1);
-					db_set_dw(ccDst->pSubs[i], META_PROTO, "ParentMeta", hDest);
+					db_set_b(hSub, META_PROTO, "IsSubcontact", 1);
+					db_set_dw(hSub, META_PROTO, "ParentMeta", hDest);
 
-					DBCachedContact *ccSub = dstDb->m_cache->GetCachedContact(ccDst->pSubs[i]);
+					DBCachedContact *ccSub = dstDb->m_cache->GetCachedContact(hSub);
 					if (ccSub)
 						ccSub->parentID = hDest;
 				}
-- 
cgit v1.2.3