From 436e303e24e5046d6cc52ac3da51a0b51adbef36 Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Mon, 15 Jun 2020 18:35:21 +0300
Subject: simpler way of applying server ids to messages

---
 plugins/Actman/iac_inout.pas                |  1 +
 plugins/Boltun/src/actionQueue.cpp          |  2 +-
 plugins/BuddyPounce/src/main.cpp            |  3 +-
 plugins/CmdLine/src/mimcmd_handlers.cpp     | 21 +++-----
 plugins/Db3x_mmap/src/dbevents.cpp          |  5 --
 plugins/Db3x_mmap/src/dbintf.h              |  1 -
 plugins/Dbx_mdbx/src/dbevents.cpp           | 75 +++++++++++++++++++----------
 plugins/Dbx_mdbx/src/dbintf.h               |  3 +-
 plugins/Dbx_sqlite/src/dbevents.cpp         | 36 ++++++--------
 plugins/Dbx_sqlite/src/dbintf.h             |  1 -
 plugins/Import/src/dbrw/dbintf.cpp          |  5 --
 plugins/Import/src/dbrw/dbintf.h            |  1 -
 plugins/Import/src/textjson.cpp             |  2 +-
 plugins/MirLua/src/Modules/m_database.cpp   |  4 +-
 plugins/NewAwaySysMod/src/MsgEventAdded.cpp | 19 ++++----
 plugins/NewXstatusNotify/src/main.cpp       |  6 +--
 plugins/NewXstatusNotify/src/xstatus.cpp    |  2 +-
 plugins/Scriver/src/globals.cpp             |  5 +-
 plugins/Spamotron/src/spamotron.cpp         | 32 ++++++------
 plugins/Spamotron/src/utils.cpp             | 23 +++++----
 plugins/TabSRMM/src/sendqueue.cpp           |  3 +-
 21 files changed, 120 insertions(+), 130 deletions(-)

(limited to 'plugins')

diff --git a/plugins/Actman/iac_inout.pas b/plugins/Actman/iac_inout.pas
index d6abe6085b..713ff00bcb 100644
--- a/plugins/Actman/iac_inout.pas
+++ b/plugins/Actman/iac_inout.pas
@@ -266,6 +266,7 @@ begin
     if DBReadByte(hContact,p,'ChatRoom',0)<>1 then
     begin
       WidetoUTF8(last,blob);
+      FillChar(dbei,SizeOf(dbeo),0);
 
       ccs.hContact       := hContact;
       ccs.szProtoService := PSS_MESSAGE;
diff --git a/plugins/Boltun/src/actionQueue.cpp b/plugins/Boltun/src/actionQueue.cpp
index f31d1c6f75..52873c80ac 100644
--- a/plugins/Boltun/src/actionQueue.cpp
+++ b/plugins/Boltun/src/actionQueue.cpp
@@ -93,8 +93,8 @@ static void TimerAnswer(MCONTACT hContact, const TalkBot::MessageInfo* info)
 	dbei.flags = DBEF_SENT | DBEF_UTF;
 	dbei.szModule = BOLTUN_NAME;
 	dbei.timestamp = (DWORD)time(0);
-
 	db_event_add(hContact, &dbei);
+
 	bot->AnswerGiven(hContact, *info);
 	delete info;
 
diff --git a/plugins/BuddyPounce/src/main.cpp b/plugins/BuddyPounce/src/main.cpp
index bf7de24bb2..6cb099bfc7 100644
--- a/plugins/BuddyPounce/src/main.cpp
+++ b/plugins/BuddyPounce/src/main.cpp
@@ -36,11 +36,12 @@ int MsgAck(WPARAM, LPARAM lParam)
 				CreateMessageAcknowlegedWindow(ack->hContact, ack->result == ACKRESULT_SUCCESS);
 			if (ack->result == ACKRESULT_SUCCESS) {
 				// wrtie it to the DB
-				DBEVENTINFO dbei = {};
 				DBVARIANT dbv;
 				int reuse = g_plugin.getByte(ack->hContact, "Reuse");
 				if (!g_plugin.getWString(ack->hContact, "PounceMsg", &dbv) && (dbv.pwszVal[0] != '\0')) {
 					T2Utf pszUtf(dbv.pwszVal);
+
+					DBEVENTINFO dbei = {};
 					dbei.eventType = EVENTTYPE_MESSAGE;
 					dbei.flags = DBEF_UTF | DBEF_SENT;
 					dbei.szModule = (char*)ack->szModule;
diff --git a/plugins/CmdLine/src/mimcmd_handlers.cpp b/plugins/CmdLine/src/mimcmd_handlers.cpp
index 7427dc3257..d4393eae43 100644
--- a/plugins/CmdLine/src/mimcmd_handlers.cpp
+++ b/plugins/CmdLine/src/mimcmd_handlers.cpp
@@ -702,19 +702,14 @@ void HandleMessageCommand(PCommand command, TArgument *argv, int argc, PReply re
 						if (ack->szModule) {
 							szReply.AppendFormat(TranslateT("Message sent to '%s'."), contact);
 
-							DBEVENTINFO e = {};
-							char module[128];
-							e.eventType = EVENTTYPE_MESSAGE;
-							e.flags = DBEF_SENT | DBEF_UTF;
-
-							e.pBlob = (PBYTE)szMessage.get();
-							e.cbBlob = (DWORD)mir_strlen(szMessage) + 1;
-
-							strncpy_s(module, ack->szModule, _countof(module));
-							e.szModule = module;
-							e.timestamp = (DWORD)time(0);
-
-							db_event_add(ack->hContact, &e);
+							DBEVENTINFO dbei = {};
+							dbei.eventType = EVENTTYPE_MESSAGE;
+							dbei.flags = DBEF_SENT | DBEF_UTF;
+							dbei.pBlob = (PBYTE)szMessage.get();
+							dbei.cbBlob = (DWORD)mir_strlen(szMessage) + 1;
+							dbei.szModule = ack->szModule;
+							dbei.timestamp = (DWORD)time(0);
+							db_event_add(ack->hContact, &dbei);
 						}
 						else szReply.AppendFormat(TranslateT("Message to '%s' was marked as sent but the account seems to be offline"), contact);
 					}
diff --git a/plugins/Db3x_mmap/src/dbevents.cpp b/plugins/Db3x_mmap/src/dbevents.cpp
index a49e9ebbfb..e2e37f15a8 100644
--- a/plugins/Db3x_mmap/src/dbevents.cpp
+++ b/plugins/Db3x_mmap/src/dbevents.cpp
@@ -542,8 +542,3 @@ MEVENT CDb3Mmap::GetEventById(LPCSTR, LPCSTR)
 {
 	return 0;
 }
-
-BOOL CDb3Mmap::SetEventId(LPCSTR, MEVENT, LPCSTR)
-{
-	return FALSE;
-}
diff --git a/plugins/Db3x_mmap/src/dbintf.h b/plugins/Db3x_mmap/src/dbintf.h
index 97c262f8ce..17f634e051 100644
--- a/plugins/Db3x_mmap/src/dbintf.h
+++ b/plugins/Db3x_mmap/src/dbintf.h
@@ -242,7 +242,6 @@ public:
 	STDMETHODIMP_(BOOL)     MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) override;
 
 	STDMETHODIMP_(MEVENT)   GetEventById(LPCSTR szModule, LPCSTR szId) override;
-	STDMETHODIMP_(BOOL)     SetEventId(LPCSTR szModule, MEVENT, LPCSTR szId) override;
 
 protected:
 	DWORD GetSettingsGroupOfsByModuleNameOfs(DBContact *dbc, DWORD ofsModuleName);
diff --git a/plugins/Dbx_mdbx/src/dbevents.cpp b/plugins/Dbx_mdbx/src/dbevents.cpp
index f065989221..724c5e4542 100644
--- a/plugins/Dbx_mdbx/src/dbevents.cpp
+++ b/plugins/Dbx_mdbx/src/dbevents.cpp
@@ -52,6 +52,7 @@ BOOL CDbxMDBX::DeleteEvent(MEVENT hDbEvent)
 {
 	DBCachedContact *cc, *cc2;
 	DBEvent dbe;
+	char *szId = nullptr;
 	{
 		txn_ptr_ro txn(m_txn_ro);
 		MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data;
@@ -59,6 +60,11 @@ BOOL CDbxMDBX::DeleteEvent(MEVENT hDbEvent)
 			return 1;
 
 		dbe = *(DBEvent*)data.iov_base;
+		if (dbe.flags & DBEF_HAS_ID) {
+			char *src = (char *)data.iov_base + sizeof(dbe) + dbe.cbBlob + 1;
+			szId = NEWSTR_ALLOCA(src);
+		}
+
 		cc = (dbe.dwContactID != 0) ? m_cache->GetCachedContact(dbe.dwContactID) : &m_ccDummy;
 		if (cc == nullptr || cc->dbc.dwEventCount == 0)
 			return 1;
@@ -111,6 +117,15 @@ BOOL CDbxMDBX::DeleteEvent(MEVENT hDbEvent)
 				return 1;
 		}
 
+		if (szId) {
+			DBEventIdKey keyId;
+			keyId.iModuleId = dbe.iModuleId;
+			strncpy_s(keyId.szEventId, szId, _TRUNCATE);
+
+			MDBX_val keyid = { &keyId, sizeof(MEVENT) + strlen(keyId.szEventId) + 1 };
+			mdbx_del(trnlck, m_dbEventIds, &keyid, nullptr);
+		}
+
 		// remove an event
 		key.iov_len = sizeof(MEVENT); key.iov_base = &hDbEvent;
 		if (mdbx_del(trnlck, m_dbEvents, &key, nullptr) != MDBX_SUCCESS)
@@ -194,14 +209,25 @@ bool CDbxMDBX::EditEvent(MCONTACT contactID, MEVENT hDbEvent, const DBEVENTINFO
 		}
 	}
 
-	{
-		BYTE *recBuf = (BYTE*)_alloca(sizeof(DBEvent) + dbe.cbBlob);
-		DBEvent *pNewEvent = (DBEvent*)recBuf;
-		*pNewEvent = dbe;
-		memcpy(pNewEvent + 1, pBlob, dbe.cbBlob);
+	size_t cbSrvId = mir_strlen(dbei->szId);
+	if (cbSrvId > 0) {
+		cbSrvId++;
+		dbe.flags |= DBEF_HAS_ID;
+	}
 
+	BYTE *recBuf = (BYTE*)_alloca(sizeof(DBEvent) + dbe.cbBlob + cbSrvId + 1), *p = recBuf;
+	memcpy(p, &dbe, sizeof(dbe)); p += sizeof(dbe);
+	memcpy(p, pBlob, dbe.cbBlob); p += dbe.cbBlob;
+	if (*p != 0)
+		*p++ = 0;
+	if (cbSrvId) {
+		memcpy(p, dbei->szId, cbSrvId);
+		p += cbSrvId;
+	}
+
+	{
 		txn_ptr trnlck(StartTran());
-		MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data = { recBuf, sizeof(DBEvent) + dbe.cbBlob };
+		MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data = { recBuf, int(p - recBuf) };
 		if (mdbx_put(trnlck, m_dbEvents, &key, &data, 0) != MDBX_SUCCESS)
 			return false;
 
@@ -238,6 +264,16 @@ bool CDbxMDBX::EditEvent(MCONTACT contactID, MEVENT hDbEvent, const DBEVENTINFO
 				return false;
 		}
 
+		if (dbei->szId) {
+			DBEventIdKey keyId;
+			keyId.iModuleId = dbe.iModuleId;
+			strncpy_s(keyId.szEventId, dbei->szId, _TRUNCATE);
+
+			MDBX_val keyid = { &keyId, sizeof(MEVENT) + strlen(keyId.szEventId) + 1 }, dataid = { &hDbEvent, sizeof(hDbEvent) };
+			if (mdbx_put(trnlck, m_dbEventIds, &keyid, &dataid, 0) != MDBX_SUCCESS)
+				return false;
+		}
+
 		if (trnlck.commit() != MDBX_SUCCESS)
 			return false;
 	}
@@ -273,6 +309,7 @@ BOOL CDbxMDBX::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
 		return 1;
 	}
 
+	size_t cbBlob;
 	const DBEvent *dbe;
 	{
 		txn_ptr_ro txn(m_txn_ro);
@@ -282,13 +319,14 @@ BOOL CDbxMDBX::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
 			return 1;
 
 		dbe = (const DBEvent*)data.iov_base;
+		cbBlob = data.iov_len - sizeof(DBEvent);
 	}
 
 	dbei->szModule = GetModuleName(dbe->iModuleId);
 	dbei->timestamp = dbe->timestamp;
 	dbei->flags = dbe->flags;
 	dbei->eventType = dbe->wEventType;
-	size_t bytesToCopy = min(dbei->cbBlob, dbe->cbBlob);
+	size_t bytesToCopy = min(dbei->cbBlob, cbBlob);
 	dbei->cbBlob = dbe->cbBlob;
 	if (bytesToCopy && dbei->pBlob) {
 		BYTE *pSrc = (BYTE*)dbe + sizeof(DBEvent);
@@ -302,9 +340,13 @@ BOOL CDbxMDBX::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
 			memcpy(dbei->pBlob, pBlob, bytesToCopy);
 			if (bytesToCopy > len)
 				memset(dbei->pBlob + len, 0, bytesToCopy - len);
+
 			mir_free(pBlob);
 		}
 		else memcpy(dbei->pBlob, pSrc, bytesToCopy);
+
+		if (dbei->flags & DBEF_HAS_ID)
+			dbei->szId = (char *)pSrc + dbei->cbBlob + 1;
 	}
 	return 0;
 }
@@ -439,25 +481,6 @@ MEVENT CDbxMDBX::GetEventById(LPCSTR szModule, LPCSTR szId)
 	return hDbEvent;
 }
 
-BOOL CDbxMDBX::SetEventId(LPCSTR szModule, MEVENT hDbEvent, LPCSTR szId)
-{
-	if (szModule == nullptr || szId == nullptr || !hDbEvent)
-		return 1;
-
-	DBEventIdKey keyId;
-	keyId.iModuleId = GetModuleID(szModule);
-	strncpy_s(keyId.szEventId, szId, _TRUNCATE);
-
-	txn_ptr trnlck(StartTran());
-	MDBX_val key = { &keyId, sizeof(MEVENT) + strlen(keyId.szEventId) + 1 }, data = { &hDbEvent, sizeof(hDbEvent) };
-	if (mdbx_put(trnlck, m_dbEventIds, &key, &data, 0) != MDBX_SUCCESS)
-		return 1;
-	if (trnlck.commit() != MDBX_SUCCESS)
-		return 1;
-
-	return 0;
-}
-
 /////////////////////////////////////////////////////////////////////////////////////////
 
 MCONTACT CDbxMDBX::GetEventContact(MEVENT hDbEvent)
diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h
index 06602c8632..0a29c30a78 100644
--- a/plugins/Dbx_mdbx/src/dbintf.h
+++ b/plugins/Dbx_mdbx/src/dbintf.h
@@ -296,8 +296,7 @@ public:
 	STDMETHODIMP_(BOOL)     Backup(const wchar_t*);
 
 	STDMETHODIMP_(MEVENT)   GetEventById(LPCSTR szModule, LPCSTR szId) override;
-	STDMETHODIMP_(BOOL)     SetEventId(LPCSTR szModule, MEVENT, LPCSTR szId) override;
-
+	
 	STDMETHODIMP_(DB::EventCursor *) EventCursor(MCONTACT hContact, MEVENT hDbEvent) override;
 	STDMETHODIMP_(DB::EventCursor *) EventCursorRev(MCONTACT hContact, MEVENT hDbEvent) override;
 
diff --git a/plugins/Dbx_sqlite/src/dbevents.cpp b/plugins/Dbx_sqlite/src/dbevents.cpp
index 9c24ddb3f0..4b5c3e80de 100755
--- a/plugins/Dbx_sqlite/src/dbevents.cpp
+++ b/plugins/Dbx_sqlite/src/dbevents.cpp
@@ -14,7 +14,6 @@ enum {
 	SQL_EVT_STMT_FINDFIRSTUNREAD,
 	SQL_EVT_STMT_FINDLAST,
 	SQL_EVT_STMT_GETIDBYSRVID,
-	SQL_EVT_STMT_SETSRVID,
 	SQL_EVT_STMT_ADDEVENT_SRT,
 	SQL_EVT_STMT_DELETE_SRT,
 	SQL_EVT_STMT_META_SPLIT,
@@ -37,7 +36,7 @@ static const char* reverse_order_pos_query =
 
 static const char *evt_stmts[SQL_EVT_STMT_NUM] = {
 	"select count(1) from events where contact_id = ? limit 1;",
-	"insert into events(contact_id, module, timestamp, type, flags, data) values (?, ?, ?, ?, ?, ?);",
+	"insert into events(contact_id, module, timestamp, type, flags, data, server_id) values (?, ?, ?, ?, ?, ?, ?);",
 	"delete from events where id = ?;",
 	"update events set module = ?, timestamp = ?, type = ?, flags = ?, blob = ? where id = ?;",
 	"select length(data) from events where id = ? limit 1;",
@@ -49,7 +48,6 @@ static const char *evt_stmts[SQL_EVT_STMT_NUM] = {
 	"select id, timestamp from events where contact_id = ? and (flags & ?) = 0 order by timestamp, id limit 1;",
 	reverse_order_query,
 	"select id, timestamp from events where module = ? and server_id = ? limit 1;",
-	"update events set server_id = ? where id = ?;",
 	"insert into events_srt(id, contact_id, timestamp) values (?, ?, ?);",
 	"delete from events_srt where id = ?;",
 	"delete from events_srt where contact_id = ?;",
@@ -173,14 +171,23 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei)
 
 	MEVENT hDbEvent = 0;
 	{
+		const char *szEventId;
+		DWORD dwFlags = dbei->flags;
+		if (dbei->szId != nullptr) {
+			dwFlags |= DBEF_HAS_ID;
+			szEventId = dbei->szId;
+		}
+		else szEventId = "";
+
 		mir_cslock lock(m_csDbAccess);
 		sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_ADDEVENT];
 		sqlite3_bind_int64(stmt, 1, hContact);
 		sqlite3_bind_text(stmt, 2, dbei->szModule, (int)mir_strlen(dbei->szModule), nullptr);
 		sqlite3_bind_int64(stmt, 3, dbei->timestamp);
 		sqlite3_bind_int(stmt, 4, dbei->eventType);
-		sqlite3_bind_int64(stmt, 5, dbei->flags);
+		sqlite3_bind_int64(stmt, 5, dwFlags);
 		sqlite3_bind_blob(stmt, 6, dbei->pBlob, dbei->cbBlob, nullptr);
+		sqlite3_bind_text(stmt, 7, szEventId, (int)mir_strlen(szEventId), nullptr);
 		int rc = sqlite3_step(stmt);
 		assert(rc == SQLITE_DONE);
 		sqlite3_reset(stmt);
@@ -209,7 +216,7 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei)
 			ccSub->AddEvent(hDbEvent, dbei->timestamp, !dbei->markedRead());
 		}
 
-		char *module = m_modules.find(dbei->szModule);
+		char *module = m_modules.find((char*)dbei->szModule);
 		if (module == nullptr)
 			m_modules.insert(mir_strdup(dbei->szModule));
 	}
@@ -289,7 +296,7 @@ BOOL CDbxSQLite::EditEvent(MCONTACT hContact, MEVENT hDbEvent, const DBEVENTINFO
 		if (cc->IsSub() && (cc = m_cache->GetCachedContact(cc->parentID)))
 			cc->EditEvent(hDbEvent, dbei->timestamp, !dbei->markedRead());
 
-		char *module = m_modules.find(dbei->szModule);
+		char *module = m_modules.find((char*)dbei->szModule);
 		if (module == nullptr)
 			m_modules.insert(mir_strdup(dbei->szModule));
 	}
@@ -619,7 +626,7 @@ MEVENT CDbxSQLite::FindPrevEvent(MCONTACT hContact, MEVENT hDbEvent)
 		return 0;
 	}
 
-	while (hDbEvent !=  sqlite3_column_int64(evt_cur_backwd, 0))
+	while (hDbEvent != sqlite3_column_int64(evt_cur_backwd, 0))
 	{
 		int rc = sqlite3_step(evt_cur_backwd);
 		assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
@@ -669,21 +676,6 @@ MEVENT CDbxSQLite::GetEventById(LPCSTR szModule, LPCSTR szId)
 	return hDbEvent;
 }
 
-BOOL CDbxSQLite::SetEventId(LPCSTR, MEVENT hDbEvent, LPCSTR szId)
-{
-	if (hDbEvent == 0 || szId == nullptr)
-		return 1;
-
-	mir_cslock lock(m_csDbAccess);
-	sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_SETSRVID];
-	sqlite3_bind_text(stmt, 1, szId, (int)mir_strlen(szId), nullptr);
-	sqlite3_bind_int64(stmt, 2, hDbEvent);
-	int rc = sqlite3_step(stmt);
-	assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
-	sqlite3_reset(stmt);
-	return (rc != SQLITE_DONE);
-}
-
 BOOL CDbxSQLite::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
 {
 	//TODO: test this
diff --git a/plugins/Dbx_sqlite/src/dbintf.h b/plugins/Dbx_sqlite/src/dbintf.h
index 70d021f202..62582a21c1 100755
--- a/plugins/Dbx_sqlite/src/dbintf.h
+++ b/plugins/Dbx_sqlite/src/dbintf.h
@@ -87,7 +87,6 @@ public:
 	STDMETHODIMP_(MEVENT)   FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent) override;
 
 	STDMETHODIMP_(MEVENT)   GetEventById(LPCSTR szModule, LPCSTR szId) override;
-	STDMETHODIMP_(BOOL)     SetEventId(LPCSTR szModule, MEVENT, LPCSTR szId) override;
 
 	STDMETHODIMP_(BOOL)     EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam) override;
 
diff --git a/plugins/Import/src/dbrw/dbintf.cpp b/plugins/Import/src/dbrw/dbintf.cpp
index aae77e66ab..4369c03eea 100644
--- a/plugins/Import/src/dbrw/dbintf.cpp
+++ b/plugins/Import/src/dbrw/dbintf.cpp
@@ -85,8 +85,3 @@ STDMETHODIMP_(MEVENT) CDbxSQLite::GetEventById(LPCSTR, LPCSTR)
 {
 	return 0;
 }
-
-STDMETHODIMP_(BOOL) CDbxSQLite::SetEventId(LPCSTR, MEVENT, LPCSTR)
-{
-	return FALSE;
-}
diff --git a/plugins/Import/src/dbrw/dbintf.h b/plugins/Import/src/dbrw/dbintf.h
index 09d7514f2f..e6a171f87d 100644
--- a/plugins/Import/src/dbrw/dbintf.h
+++ b/plugins/Import/src/dbrw/dbintf.h
@@ -156,5 +156,4 @@ public:
 	STDMETHODIMP_(BOOL)     MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) override;
 
 	STDMETHODIMP_(MEVENT)   GetEventById(LPCSTR szModule, LPCSTR szId) override;
-	STDMETHODIMP_(BOOL)     SetEventId(LPCSTR szModule, MEVENT, LPCSTR szId) override;
 };
diff --git a/plugins/Import/src/textjson.cpp b/plugins/Import/src/textjson.cpp
index 242574cc3a..b2fc92dae1 100644
--- a/plugins/Import/src/textjson.cpp
+++ b/plugins/Import/src/textjson.cpp
@@ -151,7 +151,7 @@ public:
 			dbei->szModule = m_modules.find((char*)szModule.c_str());
 			if (dbei->szModule == nullptr) {
 				dbei->szModule = mir_strdup(szModule.c_str());
-				m_modules.insert(dbei->szModule);
+				m_modules.insert((char*)dbei->szModule);
 			}
 		}
 
diff --git a/plugins/MirLua/src/Modules/m_database.cpp b/plugins/MirLua/src/Modules/m_database.cpp
index d4f5a051be..f00887a7da 100644
--- a/plugins/MirLua/src/Modules/m_database.cpp
+++ b/plugins/MirLua/src/Modules/m_database.cpp
@@ -327,10 +327,10 @@ static int db_AddEvent(lua_State *L)
 {
 	MCONTACT hContact = luaL_optinteger(L, 1, 0);
 
-	DBEVENTINFO dbei;
+	DBEVENTINFO dbei = {};
 	MakeDbEvent(L, dbei);
-	MEVENT hDbEvent = db_event_add(hContact, &dbei);
 
+	MEVENT hDbEvent = db_event_add(hContact, &dbei);
 	if (hDbEvent)
 		lua_pushnumber(L, hDbEvent);
 	else
diff --git a/plugins/NewAwaySysMod/src/MsgEventAdded.cpp b/plugins/NewAwaySysMod/src/MsgEventAdded.cpp
index ff4d1c35d4..56749a1d7f 100644
--- a/plugins/NewAwaySysMod/src/MsgEventAdded.cpp
+++ b/plugins/NewAwaySysMod/src/MsgEventAdded.cpp
@@ -64,17 +64,16 @@ void __cdecl AutoreplyDelayThread(CAutoreplyData *ad)
 	ProtoChainSend(ad->hContact, PSS_MESSAGE, 0, (LPARAM)pszReply);
 
 	if (g_AutoreplyOptPage.GetDBValueCopy(IDC_REPLYDLG_LOGREPLY)) { // store in the history
-		DBEVENTINFO dbeo = {};
-		dbeo.eventType = EVENTTYPE_MESSAGE;
-		dbeo.flags = DBEF_SENT | DBEF_UTF;
-		dbeo.szModule = szProto;
-		dbeo.timestamp = time(0);
-
-		dbeo.cbBlob = ReplyLen;
-		dbeo.pBlob = (PBYTE)(char*)pszReply;
-
 		SleepEx(1000, true); // delay before sending the reply, as we need it to be later than the message we're replying to (without this delay, srmm puts the messages in a wrong order)
-		db_event_add(ad->hContact, &dbeo);
+
+		DBEVENTINFO dbei = {};
+		dbei.eventType = EVENTTYPE_MESSAGE;
+		dbei.flags = DBEF_SENT | DBEF_UTF;
+		dbei.szModule = szProto;
+		dbei.timestamp = time(0);
+		dbei.cbBlob = ReplyLen;
+		dbei.pBlob = (PBYTE)(char*)pszReply;
+		db_event_add(ad->hContact, &dbei);
 	}
 
 	delete ad;
diff --git a/plugins/NewXstatusNotify/src/main.cpp b/plugins/NewXstatusNotify/src/main.cpp
index 0da90707e8..2992e620d2 100644
--- a/plugins/NewXstatusNotify/src/main.cpp
+++ b/plugins/NewXstatusNotify/src/main.cpp
@@ -275,11 +275,10 @@ void LogSMsgToDB(STATUSMSGINFO *smi, const wchar_t *tmplt)
 	dbei.pBlob = (PBYTE)blob;
 	dbei.eventType = EVENTTYPE_STATUSCHANGE;
 	dbei.flags = DBEF_READ | DBEF_UTF;
-
 	dbei.timestamp = (DWORD)time(0);
 	dbei.szModule = MODULENAME;
-	MEVENT hDBEvent = db_event_add(smi->hContact, &dbei);
 
+	MEVENT hDBEvent = db_event_add(smi->hContact, &dbei);
 	if (opt.SMsgLogToDB_WinOpen && opt.SMsgLogToDB_Remove) {
 		DBEVENT *dbevent = (DBEVENT *)mir_alloc(sizeof(DBEVENT));
 		dbevent->hContact = smi->hContact;
@@ -360,11 +359,10 @@ int ContactStatusChanged(MCONTACT hContact, WORD oldStatus, WORD newStatus)
 		dbei.pBlob = (PBYTE)blob;
 		dbei.eventType = EVENTTYPE_STATUSCHANGE;
 		dbei.flags = DBEF_READ | DBEF_UTF;
-
 		dbei.timestamp = (DWORD)time(0);
 		dbei.szModule = MODULENAME;
-		MEVENT hDBEvent = db_event_add(hContact, &dbei);
 
+		MEVENT hDBEvent = db_event_add(hContact, &dbei);
 		if (opt.LogToDB_WinOpen && opt.LogToDB_Remove) {
 			DBEVENT *dbevent = (DBEVENT *)mir_alloc(sizeof(DBEVENT));
 			dbevent->hContact = hContact;
diff --git a/plugins/NewXstatusNotify/src/xstatus.cpp b/plugins/NewXstatusNotify/src/xstatus.cpp
index b703204fd5..3e72eed316 100644
--- a/plugins/NewXstatusNotify/src/xstatus.cpp
+++ b/plugins/NewXstatusNotify/src/xstatus.cpp
@@ -294,8 +294,8 @@ void LogChangeToDB(XSTATUSCHANGE *xsc)
 		dbei.flags = DBEF_READ | DBEF_UTF;
 		dbei.timestamp = (DWORD)time(0);
 		dbei.szModule = MODULENAME;
-		MEVENT hDBEvent = db_event_add(xsc->hContact, &dbei);
 
+		MEVENT hDBEvent = db_event_add(xsc->hContact, &dbei);
 		if (opt.XLogToDB_WinOpen && opt.XLogToDB_Remove) {
 			DBEVENT *dbevent = (DBEVENT *)mir_alloc(sizeof(DBEVENT));
 			dbevent->hContact = xsc->hContact;
diff --git a/plugins/Scriver/src/globals.cpp b/plugins/Scriver/src/globals.cpp
index ac1d9be163..6c017b7266 100644
--- a/plugins/Scriver/src/globals.cpp
+++ b/plugins/Scriver/src/globals.cpp
@@ -149,14 +149,13 @@ static int ackevent(WPARAM, LPARAM lParam)
 	dbei.timestamp = time(0);
 	dbei.cbBlob = (int)mir_strlen(item->sendBuffer) + 1;
 	dbei.pBlob = (PBYTE)item->sendBuffer;
+	dbei.szId = (char *)pAck->lParam;
 
 	MessageWindowEvent evt = { item->hSendId, hContact, &dbei };
 	NotifyEventHooks(g_chatApi.hevPreCreate, 0, (LPARAM)&evt);
 
 	item->sendBuffer = (char *)dbei.pBlob;
-	MEVENT hNewEvent = db_event_add(hContact, &dbei);
-	if (hNewEvent && pAck->lParam)
-		db_event_setId(dbei.szModule, hNewEvent, (char *)pAck->lParam);
+	db_event_add(hContact, &dbei);
 
 	if (item->hwndErrorDlg != nullptr)
 		DestroyWindow(item->hwndErrorDlg);
diff --git a/plugins/Spamotron/src/spamotron.cpp b/plugins/Spamotron/src/spamotron.cpp
index 445261ee14..f61f1c43f5 100644
--- a/plugins/Spamotron/src/spamotron.cpp
+++ b/plugins/Spamotron/src/spamotron.cpp
@@ -227,23 +227,21 @@ int OnDBEventFilterAdd(WPARAM wParam, LPARAM lParam)
 			wchar_t AuthEventModule[100];
 			char* szAuthEventModule;
 			if (db_get(hContact, MODULENAME, "AuthEvent", &_dbv) == 0) {
-				DBEVENTINFO *_dbei = (DBEVENTINFO *)malloc(sizeof(DBEVENTINFO));
-				if (_dbei != nullptr) {
-					memcpy(&_dbei->cbBlob, _dbv.pbVal, sizeof(DWORD));
-					_dbei->eventType = EVENTTYPE_AUTHREQUEST;
-					_getCOptS(AuthEventModule, 100, hContact, "AuthEventModule", L"ICQ");
-					szAuthEventModule = mir_u2a(AuthEventModule);
-					_dbei->szModule = szAuthEventModule;
-					_dbei->timestamp = dbei->timestamp;
-					_dbei->flags = 0;
-					_dbei->pBlob = _dbv.pbVal + sizeof(DWORD);
-					db_event_add(hContact,_dbei);
-					g_plugin.delSetting(hContact, "AuthEvent");
-					g_plugin.delSetting(hContact, "AuthEventPending");
-					g_plugin.delSetting(hContact, "AuthEventModule");
-					mir_free(szAuthEventModule);
-					free(_dbei);
-				}
+				DBEVENTINFO dbei2 = {};
+				dbei2.cbBlob = *(DWORD *)_dbv.pbVal;
+				dbei2.eventType = EVENTTYPE_AUTHREQUEST;
+				_getCOptS(AuthEventModule, 100, hContact, "AuthEventModule", L"ICQ");
+				szAuthEventModule = mir_u2a(AuthEventModule);
+				dbei2.szModule = szAuthEventModule;
+				dbei2.timestamp = dbei->timestamp;
+				dbei2.pBlob = _dbv.pbVal + sizeof(DWORD);
+				db_event_add(hContact, &dbei2);
+
+				g_plugin.delSetting(hContact, "AuthEvent");
+				g_plugin.delSetting(hContact, "AuthEventPending");
+				g_plugin.delSetting(hContact, "AuthEventModule");
+				mir_free(szAuthEventModule);
+
 				db_free(&_dbv);
 			}
 		}
diff --git a/plugins/Spamotron/src/utils.cpp b/plugins/Spamotron/src/utils.cpp
index bdbbdacd09..52ba7ef5c4 100644
--- a/plugins/Spamotron/src/utils.cpp
+++ b/plugins/Spamotron/src/utils.cpp
@@ -488,21 +488,20 @@ void MarkUnread(MCONTACT hContact)
 	if (db_get(hContact, MODULENAME, "LastMsgEvents", &_dbv) == 0) {
 		pos = _dbv.pbVal;
 		while (pos - _dbv.pbVal < _dbv.cpbVal) {
-			DBEVENTINFO _dbei = {};
-			memcpy(&_dbei.eventType, pos, sizeof(WORD)); pos += sizeof(WORD);
-			memcpy(&_dbei.flags, pos, sizeof(DWORD)); pos += sizeof(DWORD);
-			memcpy(&_dbei.timestamp, pos, sizeof(DWORD)); pos += sizeof(DWORD);
+			DBEVENTINFO dbei = {};
+			memcpy(&dbei.eventType, pos, sizeof(WORD)); pos += sizeof(WORD);
+			memcpy(&dbei.flags, pos, sizeof(DWORD)); pos += sizeof(DWORD);
+			memcpy(&dbei.timestamp, pos, sizeof(DWORD)); pos += sizeof(DWORD);
 
-			_dbei.szModule = (char*)malloc(mir_strlen((const char*)pos)+1);
-			mir_strcpy(_dbei.szModule, (const char*)pos);
+			dbei.szModule = (char*)malloc(mir_strlen((const char*)pos)+1);
+			mir_strcpy((char*)dbei.szModule, (const char*)pos);
 			pos += mir_strlen((const char*)pos)+1;
 
-			memcpy(&_dbei.cbBlob, pos, sizeof(DWORD)); pos += sizeof(DWORD);
-			_dbei.pBlob = (PBYTE)malloc(_dbei.cbBlob);
-			memcpy(_dbei.pBlob, pos, _dbei.cbBlob);
-			pos += _dbei.cbBlob;
-
-			db_event_add(hContact,&_dbei);
+			memcpy(&dbei.cbBlob, pos, sizeof(DWORD)); pos += sizeof(DWORD);
+			dbei.pBlob = (PBYTE)malloc(dbei.cbBlob);
+			memcpy(dbei.pBlob, pos, dbei.cbBlob);
+			pos += dbei.cbBlob;
+			db_event_add(hContact,&dbei);
 		}
 		db_free(&_dbv);
 		g_plugin.delSetting(hContact, "LastMsgEvents");
diff --git a/plugins/TabSRMM/src/sendqueue.cpp b/plugins/TabSRMM/src/sendqueue.cpp
index a31bbac0fd..bf444489fd 100644
--- a/plugins/TabSRMM/src/sendqueue.cpp
+++ b/plugins/TabSRMM/src/sendqueue.cpp
@@ -472,14 +472,13 @@ int SendQueue::ackMessage(CMsgDialog *dat, WPARAM wParam, LPARAM lParam)
 	if (job.dwFlags & PREF_RTL)
 		dbei.flags |= DBEF_RTL;
 	dbei.pBlob = (PBYTE)job.szSendBuffer;
+	dbei.szId = (char *)ack->lParam;
 
 	MessageWindowEvent evt = { job.iSendId, job.hContact, &dbei };
 	NotifyEventHooks(g_chatApi.hevPreCreate, 0, (LPARAM)&evt);
 
 	job.szSendBuffer = (char*)dbei.pBlob;
 	MEVENT hNewEvent = db_event_add(job.hContact, &dbei);
-	if (hNewEvent && ack->lParam)
-		db_event_setId(dbei.szModule, hNewEvent, (char*)ack->lParam);
 
 	if (m_pContainer)
 		if (!nen_options.iNoSounds && !m_pContainer->m_flags.m_bNoSound)
-- 
cgit v1.2.3