From 8975af6cee9cfed40a075a16841cb0e6428ebaa6 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 6 Feb 2022 18:11:23 +0300 Subject: duplicate event checking code moved to the core --- include/m_database.h | 1 + libs/win32/mir_app.lib | Bin 223922 -> 224272 bytes libs/win64/mir_app.lib | Bin 219646 -> 220002 bytes plugins/Import/src/import.cpp | 2 +- plugins/Import/src/stdafx.h | 2 - plugins/Import/src/utils.cpp | 131 ------------------------------ protocols/JabberG/src/jabber_archive.cpp | 134 +------------------------------ src/mir_app/src/db_events.cpp | 133 ++++++++++++++++++++++++++++++ src/mir_app/src/mir_app.def | 1 + src/mir_app/src/mir_app64.def | 1 + 10 files changed, 138 insertions(+), 267 deletions(-) diff --git a/include/m_database.h b/include/m_database.h index 5ec7c13149..dbf8455e56 100644 --- a/include/m_database.h +++ b/include/m_database.h @@ -669,6 +669,7 @@ __inline uint32_t DBGetContactSettingRangedDword(MCONTACT hContact, const char * namespace DB { + MIR_APP_DLL(bool) IsDuplicateEvent(MCONTACT hContact, DBEVENTINFO &dbei); ///////////////////////////////////////////////////////////////////////////////////////// // Helper to free event contents automatically diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib index 5953a2d9d8..07f3067d3c 100644 Binary files a/libs/win32/mir_app.lib and b/libs/win32/mir_app.lib differ diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib index 18fc2ae0a0..e1c0110e62 100644 Binary files a/libs/win64/mir_app.lib and b/libs/win64/mir_app.lib differ diff --git a/plugins/Import/src/import.cpp b/plugins/Import/src/import.cpp index 9ea7f723df..a90a993b80 100644 --- a/plugins/Import/src/import.cpp +++ b/plugins/Import/src/import.cpp @@ -951,7 +951,7 @@ void CImportBatch::ImportHistory(MCONTACT hContact, PROTOACCOUNT **protocol, int continue; // check for duplicate entries - if ((m_iOptions & IOPT_CHECKDUPS) != 0 && IsDuplicateEvent(hDst, dbei)) { + if ((m_iOptions & IOPT_CHECKDUPS) != 0 && DB::IsDuplicateEvent(hDst, dbei)) { nDupes++; continue; } diff --git a/plugins/Import/src/stdafx.h b/plugins/Import/src/stdafx.h index df21b67c16..ed7e5e0e23 100644 --- a/plugins/Import/src/stdafx.h +++ b/plugins/Import/src/stdafx.h @@ -308,8 +308,6 @@ public: CImportPattern *m_pPattern; }; -bool IsDuplicateEvent(MCONTACT hContact, DBEVENTINFO dbei); - int CreateGroup(const wchar_t *name, MCONTACT hContact); uint32_t RLInteger(const uint8_t *p); diff --git a/plugins/Import/src/utils.cpp b/plugins/Import/src/utils.cpp index d6f2ba929f..3c83bf11a9 100644 --- a/plugins/Import/src/utils.cpp +++ b/plugins/Import/src/utils.cpp @@ -48,137 +48,6 @@ int CreateGroup(const wchar_t *group, MCONTACT hContact) return 1; } -// Returns TRUE if the event already exist in the database -bool IsDuplicateEvent(MCONTACT hContact, DBEVENTINFO dbei) -{ - static uint32_t dwPreviousTimeStamp = -1; - static MCONTACT hPreviousContact = INVALID_CONTACT_ID; - static MEVENT hPreviousDbEvent = NULL; - - // get last event - MEVENT hExistingDbEvent = db_event_last(hContact); - if (hExistingDbEvent == NULL) - return FALSE; - - DBEVENTINFO dbeiExisting = {}; - db_event_get(hExistingDbEvent, &dbeiExisting); - uint32_t dwEventTimeStamp = dbeiExisting.timestamp; - - // compare with last timestamp - if (dbei.timestamp > dwEventTimeStamp) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dwEventTimeStamp; - return FALSE; - } - - if (hContact != hPreviousContact) { - hPreviousContact = hContact; - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dwEventTimeStamp; - - // get first event - if (!(hExistingDbEvent = db_event_first(hContact))) - return FALSE; - - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - dwEventTimeStamp = dbeiExisting.timestamp; - - // compare with first timestamp - if (dbei.timestamp <= dwEventTimeStamp) { - // remember event - dwPreviousTimeStamp = dwEventTimeStamp; - hPreviousDbEvent = hExistingDbEvent; - if (dbei.timestamp != dwEventTimeStamp) - return FALSE; - } - } - - // check for equal timestamps - if (dbei.timestamp == dwPreviousTimeStamp) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hPreviousDbEvent, &dbeiExisting); - - if (dbei == dbeiExisting) - return TRUE; - - // find event with another timestamp - hExistingDbEvent = db_event_next(hContact, hPreviousDbEvent); - while (hExistingDbEvent != NULL) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - - if (dbeiExisting.timestamp != dwPreviousTimeStamp) { - // use found event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - break; - } - - hPreviousDbEvent = hExistingDbEvent; - hExistingDbEvent = db_event_next(hContact, hExistingDbEvent); - } - } - - hExistingDbEvent = hPreviousDbEvent; - - if (dbei.timestamp <= dwPreviousTimeStamp) { - // look back - while (hExistingDbEvent != NULL) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - - if (dbei.timestamp > dbeiExisting.timestamp) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return FALSE; - } - - // Compare event with import candidate - if (dbei == dbeiExisting) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return TRUE; - } - - // Get previous event in chain - hExistingDbEvent = db_event_prev(hContact, hExistingDbEvent); - } - } - else { - // look forward - while (hExistingDbEvent != NULL) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - - if (dbei.timestamp < dbeiExisting.timestamp) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return FALSE; - } - - // Compare event with import candidate - if (dbei == dbeiExisting) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return TRUE; - } - - // Get next event in chain - hExistingDbEvent = db_event_next(hContact, hExistingDbEvent); - } - } - // reset last event - hPreviousContact = INVALID_CONTACT_ID; - return FALSE; -} - ///////////////////////////////////////////////////////////////////////////////////////// // rtl integers diff --git a/protocols/JabberG/src/jabber_archive.cpp b/protocols/JabberG/src/jabber_archive.cpp index 8fb02e3430..97c76993ba 100644 --- a/protocols/JabberG/src/jabber_archive.cpp +++ b/protocols/JabberG/src/jabber_archive.cpp @@ -78,138 +78,6 @@ void CJabberProto::OnIqResultGetCollectionList(const TiXmlElement *iqNode, CJabb ///////////////////////////////////////////////////////////////////////////////////////// -static uint32_t dwPreviousTimeStamp = -1; -static MCONTACT hPreviousContact = INVALID_CONTACT_ID; -static MEVENT hPreviousDbEvent = 0; - -// Returns TRUE if the event already exist in the database -bool IsDuplicateEvent(MCONTACT hContact, DBEVENTINFO& dbei) -{ - // get last event - MEVENT hExistingDbEvent = db_event_last(hContact); - if (!hExistingDbEvent) - return false; - - DBEVENTINFO dbeiExisting = {}; - db_event_get(hExistingDbEvent, &dbeiExisting); - uint32_t dwEventTimeStamp = dbeiExisting.timestamp; - - // compare with last timestamp - if (dbei.timestamp > dwEventTimeStamp) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dwEventTimeStamp; - return false; - } - - if (hContact != hPreviousContact) { - hPreviousContact = hContact; - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dwEventTimeStamp; - - // get first event - if (!(hExistingDbEvent = db_event_first(hContact))) - return false; - - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - dwEventTimeStamp = dbeiExisting.timestamp; - - // compare with first timestamp - if (dbei.timestamp <= dwEventTimeStamp) { - // remember event - dwPreviousTimeStamp = dwEventTimeStamp; - hPreviousDbEvent = hExistingDbEvent; - - if (dbei.timestamp != dwEventTimeStamp) - return false; - } - } - - // check for equal timestamps - if (dbei.timestamp == dwPreviousTimeStamp) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hPreviousDbEvent, &dbeiExisting); - - if (dbei == dbeiExisting) - return true; - - // find event with another timestamp - hExistingDbEvent = db_event_next(hContact, hPreviousDbEvent); - while (hExistingDbEvent != 0) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - - if (dbeiExisting.timestamp != dwPreviousTimeStamp) { - // use found event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - break; - } - - hPreviousDbEvent = hExistingDbEvent; - hExistingDbEvent = db_event_next(hContact, hExistingDbEvent); - } - } - - hExistingDbEvent = hPreviousDbEvent; - - if (dbei.timestamp <= dwPreviousTimeStamp) { - // look back - while (hExistingDbEvent != 0) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - - if (dbei.timestamp > dbeiExisting.timestamp) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return false; - } - - // Compare event with import candidate - if (dbei == dbeiExisting) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return true; - } - - // Get previous event in chain - hExistingDbEvent = db_event_prev(hContact, hExistingDbEvent); - } - } - else { - // look forward - while (hExistingDbEvent != 0) { - memset(&dbeiExisting, 0, sizeof(dbeiExisting)); - db_event_get(hExistingDbEvent, &dbeiExisting); - - if (dbei.timestamp < dbeiExisting.timestamp) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return false; - } - - // Compare event with import candidate - if (dbei == dbeiExisting) { - // remember event - hPreviousDbEvent = hExistingDbEvent; - dwPreviousTimeStamp = dbeiExisting.timestamp; - return true; - } - - // Get next event in chain - hExistingDbEvent = db_event_next(hContact, hExistingDbEvent); - } - } - // reset last event - hPreviousContact = INVALID_CONTACT_ID; - return false; -} - void CJabberProto::OnIqResultGetCollection(const TiXmlElement *iqNode, CJabberIqInfo*) { if (mir_strcmp(XmlGetAttr(iqNode, "type"), "result")) @@ -259,7 +127,7 @@ void CJabberProto::OnIqResultGetCollection(const TiXmlElement *iqNode, CJabberIq dbei.flags = DBEF_READ + DBEF_UTF + from; dbei.pBlob = (uint8_t*)tszBody; dbei.timestamp = tmStart + atol(tszSecs); - if (!IsDuplicateEvent(hContact, dbei)) + if (!DB::IsDuplicateEvent(hContact, dbei)) db_event_add(hContact, &dbei); tmStart = dbei.timestamp; diff --git a/src/mir_app/src/db_events.cpp b/src/mir_app/src/db_events.cpp index c42fc6ba67..f30b0af087 100644 --- a/src/mir_app/src/db_events.cpp +++ b/src/mir_app/src/db_events.cpp @@ -314,3 +314,136 @@ uint8_t* DB::AUTH_BLOB::makeBlob() return pBlob; } +///////////////////////////////////////////////////////////////////////////////////////// + +static uint32_t dwPreviousTimeStamp = -1; +static MCONTACT hPreviousContact = INVALID_CONTACT_ID; +static MEVENT hPreviousDbEvent = 0; + +// Returns TRUE if the event already exist in the database +MIR_APP_DLL(bool) DB::IsDuplicateEvent(MCONTACT hContact, DBEVENTINFO &dbei) +{ + // get last event + MEVENT hExistingDbEvent = db_event_last(hContact); + if (!hExistingDbEvent) + return false; + + DBEVENTINFO dbeiExisting = {}; + db_event_get(hExistingDbEvent, &dbeiExisting); + uint32_t dwEventTimeStamp = dbeiExisting.timestamp; + + // compare with last timestamp + if (dbei.timestamp > dwEventTimeStamp) { + // remember event + hPreviousDbEvent = hExistingDbEvent; + dwPreviousTimeStamp = dwEventTimeStamp; + return false; + } + + if (hContact != hPreviousContact) { + hPreviousContact = hContact; + // remember event + hPreviousDbEvent = hExistingDbEvent; + dwPreviousTimeStamp = dwEventTimeStamp; + + // get first event + if (!(hExistingDbEvent = db_event_first(hContact))) + return false; + + memset(&dbeiExisting, 0, sizeof(dbeiExisting)); + db_event_get(hExistingDbEvent, &dbeiExisting); + dwEventTimeStamp = dbeiExisting.timestamp; + + // compare with first timestamp + if (dbei.timestamp <= dwEventTimeStamp) { + // remember event + dwPreviousTimeStamp = dwEventTimeStamp; + hPreviousDbEvent = hExistingDbEvent; + + if (dbei.timestamp != dwEventTimeStamp) + return false; + } + } + + // check for equal timestamps + if (dbei.timestamp == dwPreviousTimeStamp) { + memset(&dbeiExisting, 0, sizeof(dbeiExisting)); + db_event_get(hPreviousDbEvent, &dbeiExisting); + + if (dbei == dbeiExisting) + return true; + + // find event with another timestamp + hExistingDbEvent = db_event_next(hContact, hPreviousDbEvent); + while (hExistingDbEvent != 0) { + memset(&dbeiExisting, 0, sizeof(dbeiExisting)); + db_event_get(hExistingDbEvent, &dbeiExisting); + + if (dbeiExisting.timestamp != dwPreviousTimeStamp) { + // use found event + hPreviousDbEvent = hExistingDbEvent; + dwPreviousTimeStamp = dbeiExisting.timestamp; + break; + } + + hPreviousDbEvent = hExistingDbEvent; + hExistingDbEvent = db_event_next(hContact, hExistingDbEvent); + } + } + + hExistingDbEvent = hPreviousDbEvent; + + if (dbei.timestamp <= dwPreviousTimeStamp) { + // look back + while (hExistingDbEvent != 0) { + memset(&dbeiExisting, 0, sizeof(dbeiExisting)); + db_event_get(hExistingDbEvent, &dbeiExisting); + + if (dbei.timestamp > dbeiExisting.timestamp) { + // remember event + hPreviousDbEvent = hExistingDbEvent; + dwPreviousTimeStamp = dbeiExisting.timestamp; + return false; + } + + // Compare event with import candidate + if (dbei == dbeiExisting) { + // remember event + hPreviousDbEvent = hExistingDbEvent; + dwPreviousTimeStamp = dbeiExisting.timestamp; + return true; + } + + // Get previous event in chain + hExistingDbEvent = db_event_prev(hContact, hExistingDbEvent); + } + } + else { + // look forward + while (hExistingDbEvent != 0) { + memset(&dbeiExisting, 0, sizeof(dbeiExisting)); + db_event_get(hExistingDbEvent, &dbeiExisting); + + if (dbei.timestamp < dbeiExisting.timestamp) { + // remember event + hPreviousDbEvent = hExistingDbEvent; + dwPreviousTimeStamp = dbeiExisting.timestamp; + return false; + } + + // Compare event with import candidate + if (dbei == dbeiExisting) { + // remember event + hPreviousDbEvent = hExistingDbEvent; + dwPreviousTimeStamp = dbeiExisting.timestamp; + return true; + } + + // Get next event in chain + hExistingDbEvent = db_event_next(hContact, hExistingDbEvent); + } + } + // reset last event + hPreviousContact = INVALID_CONTACT_ID; + return false; +} diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index be81fe60de..20befc32d3 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -791,3 +791,4 @@ _Netlib_SslWrite@12 @877 NONAME ?setTyping@CSrmmBaseDialog@@QAEXHPBUUSERINFO@@@Z @879 NONAME ?SafeRecycleBin@PU@@YGHPB_W@Z @880 NONAME ?IsMirandaFolderWritable@PU@@YG_NXZ @881 NONAME +?IsDuplicateEvent@DB@@YG_NIAAUDBEVENTINFO@@@Z @882 NONAME diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index 52a1e3b701..0330b8708a 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -791,3 +791,4 @@ Netlib_SslWrite @877 NONAME ?setTyping@CSrmmBaseDialog@@QEAAXHPEBUUSERINFO@@@Z @879 NONAME ?SafeRecycleBin@PU@@YAHPEB_W@Z @880 NONAME ?IsMirandaFolderWritable@PU@@YA_NXZ @881 NONAME +?IsDuplicateEvent@DB@@YA_NIAEAUDBEVENTINFO@@@Z @882 NONAME -- cgit v1.2.3