From 8f9292d483fcf3a10d9284512359c4562f5311eb Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 11 Apr 2023 17:07:04 +0300 Subject: DB::FILE_BLOB - file events' access unification --- src/core/stdfile/src/file.cpp | 50 +++++++++++++---------------------- src/core/stdfile/src/filexferdlg.cpp | 32 +++++++--------------- src/core/stdmsg/src/msglog.cpp | 10 +++---- src/mir_app/src/MDatabaseReadonly.cpp | 2 +- src/mir_app/src/db_events.cpp | 49 +++++++++++++++++++++++++++------- src/mir_app/src/mir_app.def | 8 +++++- src/mir_app/src/mir_app64.def | 8 +++++- src/mir_app/src/proto_interface.cpp | 2 +- src/mir_core/src/db.cpp | 4 +-- 9 files changed, 90 insertions(+), 75 deletions(-) (limited to 'src') diff --git a/src/core/stdfile/src/file.cpp b/src/core/stdfile/src/file.cpp index abe3152cdd..126b428c99 100644 --- a/src/core/stdfile/src/file.cpp +++ b/src/core/stdfile/src/file.cpp @@ -356,7 +356,7 @@ static INT_PTR Proto_RecvFileT(WPARAM, LPARAM lParam) if (pre->fileCount == 0) return 0; - DBEVENTINFO dbei = {}; + DB::EventInfo dbei; dbei.szModule = Proto_GetBaseAccountName(ccs->hContact); dbei.timestamp = pre->timestamp; dbei.eventType = EVENTTYPE_FILE; @@ -364,47 +364,33 @@ static INT_PTR Proto_RecvFileT(WPARAM, LPARAM lParam) if (pre->dwFlags & PREF_CREATEREAD) dbei.flags |= DBEF_READ; - bool bUnicode = (pre->dwFlags & PRFF_UNICODE) == PRFF_UNICODE; + if ((pre->dwFlags & PRFF_UNICODE) == PRFF_UNICODE) { + CMStringW wszFiles; - const char *szDescr, **pszFiles; - if (bUnicode) { - pszFiles = (const char**)alloca(pre->fileCount * sizeof(char*)); - for (int i = 0; i < pre->fileCount; i++) - pszFiles[i] = mir_utf8encodeW(pre->files.w[i]); + for (int i = 0; i < pre->fileCount; i++) { + if (i != 0) + wszFiles.AppendChar(','); + wszFiles.Append(pre->files.w[i]); + } - szDescr = mir_utf8encodeW(pre->descr.w); + DB::FILE_BLOB blob(wszFiles, pre->descr.w); + blob.write(dbei); } else { - pszFiles = pre->files.a; - szDescr = pre->descr.a; - } - - dbei.cbBlob = sizeof(uint32_t); + CMStringW wszFiles; - for (int i = 0; i < pre->fileCount; i++) - dbei.cbBlob += (int)mir_strlen(pszFiles[i]) + 1; - - dbei.cbBlob += (int)mir_strlen(szDescr) + 1; - - if ((dbei.pBlob = (uint8_t*)mir_alloc(dbei.cbBlob)) == nullptr) - return 0; + for (int i = 0; i < pre->fileCount; i++) { + if (i != 0) + wszFiles.AppendChar(','); + wszFiles.Append(_A2T(pre->files.a[i])); + } - *(uint32_t*)dbei.pBlob = 0; - uint8_t* p = dbei.pBlob + sizeof(uint32_t); - for (int i = 0; i < pre->fileCount; i++) { - mir_strcpy((char*)p, pszFiles[i]); - p += mir_strlen(pszFiles[i]) + 1; - if (bUnicode) - mir_free((void*)pszFiles[i]); + DB::FILE_BLOB blob(wszFiles, _A2T(pre->descr.a)); + blob.write(dbei); } - mir_strcpy((char*)p, (szDescr == nullptr) ? "" : szDescr); - if (bUnicode) - mir_free((void*)szDescr); - MEVENT hdbe = db_event_add(ccs->hContact, &dbei); PushFileEvent(ccs->hContact, hdbe, pre->lParam); - mir_free(dbei.pBlob); return 0; } diff --git a/src/core/stdfile/src/filexferdlg.cpp b/src/core/stdfile/src/filexferdlg.cpp index e545649b5b..5ab3a0679b 100644 --- a/src/core/stdfile/src/filexferdlg.cpp +++ b/src/core/stdfile/src/filexferdlg.cpp @@ -78,24 +78,6 @@ static void SetOpenFileButtonStyle(HWND hwndButton, int enabled) EnableWindow(hwndButton, enabled); } -void FillSendData(FileDlgData *dat, DBEVENTINFO &dbei) -{ - dbei.szModule = Proto_GetBaseAccountName(dat->hContact); - dbei.eventType = EVENTTYPE_FILE; - dbei.flags = DBEF_SENT; - dbei.timestamp = time(0); - char *szFileNames = mir_utf8encodeW(dat->szFilenames), *szMsg = mir_utf8encodeW(dat->szMsg); - dbei.flags |= DBEF_UTF; - - dbei.cbBlob = int(sizeof(uint32_t) + mir_strlen(szFileNames) + mir_strlen(szMsg) + 2); - dbei.pBlob = (uint8_t*)mir_alloc(dbei.cbBlob); - *(PDWORD)dbei.pBlob = 0; - mir_strcpy((char*)dbei.pBlob + sizeof(uint32_t), szFileNames); - mir_strcpy((char*)dbei.pBlob + sizeof(uint32_t) + mir_strlen(szFileNames) + 1, szMsg); - - mir_free(szFileNames), mir_free(szMsg); -} - static void __cdecl RunVirusScannerThread(virusscanthreadstartinfo *info) { DBVARIANT dbv; @@ -650,11 +632,17 @@ INT_PTR CALLBACK DlgProcFileTransfer(HWND hwndDlg, UINT msg, WPARAM wParam, LPAR dat->fs = nullptr; /* protocol will free structure */ SetFtStatus(hwndDlg, LPGENW("Transfer completed."), FTS_TEXT); - DBEVENTINFO dbei = {}; - FillSendData(dat, dbei); + DB::EventInfo dbei; + dbei.szModule = Proto_GetBaseAccountName(dat->hContact); + dbei.eventType = EVENTTYPE_FILE; + dbei.flags = DBEF_SENT; + dbei.timestamp = time(0); + dbei.flags |= DBEF_UTF; + + DB::FILE_BLOB blob(dat->szFilenames, dat->szMsg); + blob.write(dbei); db_event_add(dat->hContact, &dbei); - if (dbei.pBlob) - mir_free(dbei.pBlob); + dat->files = nullptr; //protocol library frees this } else { diff --git a/src/core/stdmsg/src/msglog.cpp b/src/core/stdmsg/src/msglog.cpp index c20b597beb..290c8ad77e 100644 --- a/src/core/stdmsg/src/msglog.cpp +++ b/src/core/stdmsg/src/msglog.cpp @@ -304,18 +304,16 @@ static bool CreateRTFFromDbEvent(LogStreamData *dat) case EVENTTYPE_FILE: { - char *filename = (char*)dbei.pBlob + sizeof(uint32_t); - char *descr = filename + mir_strlen(filename) + 1; + DB::FILE_BLOB blob(dbei); SetToStyle(MSGFONTID_NOTICE, buf); AppendToBufferWithRTF(buf, (dbei.flags & DBEF_SENT) ? TranslateT("File sent") : TranslateT("File received")); buf.Append(": "); - AppendToBufferWithRTF(buf, ptrW(DbEvent_GetString(&dbei, filename))); + AppendToBufferWithRTF(buf, blob.getName()); - if (*descr != 0) { - ptrW ptszDescr(DbEvent_GetString(&dbei, descr)); + if (*blob.getDescr() != 0) { buf.Append(" ("); - AppendToBufferWithRTF(buf, ptszDescr); + AppendToBufferWithRTF(buf, blob.getDescr()); buf.Append(")"); } } diff --git a/src/mir_app/src/MDatabaseReadonly.cpp b/src/mir_app/src/MDatabaseReadonly.cpp index c386c4d6f4..9a0194373a 100644 --- a/src/mir_app/src/MDatabaseReadonly.cpp +++ b/src/mir_app/src/MDatabaseReadonly.cpp @@ -108,7 +108,7 @@ BOOL MDatabaseReadonly::DeleteEvent(MEVENT) return 1; } -BOOL MDatabaseReadonly::EditEvent(MCONTACT, MEVENT, const DBEVENTINFO*) +BOOL MDatabaseReadonly::EditEvent(MEVENT, const DBEVENTINFO*) { return 1; } diff --git a/src/mir_app/src/db_events.cpp b/src/mir_app/src/db_events.cpp index f4153d6850..9e413aac76 100644 --- a/src/mir_app/src/db_events.cpp +++ b/src/mir_app/src/db_events.cpp @@ -98,7 +98,7 @@ static wchar_t* getEventString(DBEVENTINFO *dbei, LPSTR &buf) return (dbei->flags & DBEF_UTF) ? mir_utf8decodeW(in) : mir_a2u(in); } -static INT_PTR DbEventGetTextWorker(DBEVENTINFO *dbei, int codepage, int datatype) +static INT_PTR DbEventGetTextWorker(DB::EventInfo *dbei, int codepage, int datatype) { if (dbei == nullptr || dbei->szModule == nullptr) return 0; @@ -162,13 +162,12 @@ static INT_PTR DbEventGetTextWorker(DBEVENTINFO *dbei, int codepage, int datatyp } if (dbei->eventType == EVENTTYPE_FILE) { - char *buf = LPSTR(dbei->pBlob) + sizeof(uint32_t); - ptrW tszFileName(getEventString(dbei, buf)); - ptrW tszDescription(getEventString(dbei, buf)); - CMStringW wszText(tszFileName.get()); - if (mir_wstrlen(tszDescription) > 0) { + DB::FILE_BLOB blob(*dbei); + + CMStringW wszText(blob.getName()); + if (mir_wstrlen(blob.getDescr()) > 0) { wszText.Append(L": "); - wszText.Append(tszDescription); + wszText.Append(blob.getDescr()); } switch (datatype) { @@ -208,12 +207,12 @@ static INT_PTR DbEventGetTextWorker(DBEVENTINFO *dbei, int codepage, int datatyp MIR_APP_DLL(char*) DbEvent_GetTextA(DBEVENTINFO *dbei, int codepage) { - return (char*)DbEventGetTextWorker(dbei, codepage, DBVT_ASCIIZ); + return (char*)DbEventGetTextWorker((DB::EventInfo *)dbei, codepage, DBVT_ASCIIZ); } MIR_APP_DLL(wchar_t*) DbEvent_GetTextW(DBEVENTINFO *dbei, int codepage) { - return (wchar_t*)DbEventGetTextWorker(dbei, codepage, DBVT_WCHAR); + return (wchar_t*)DbEventGetTextWorker((DB::EventInfo *)dbei, codepage, DBVT_WCHAR); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -285,6 +284,38 @@ DB::EventInfo::~EventInfo() } ///////////////////////////////////////////////////////////////////////////////////////// +// File blob helper + +DB::FILE_BLOB::FILE_BLOB(const wchar_t *pwszName, const wchar_t *pwszDescr) : + m_wszFileName(mir_wstrdup(pwszName)), + m_wszDescription(mir_wstrdup(pwszDescr)) +{} + +DB::FILE_BLOB::FILE_BLOB(const DB::EventInfo &dbei) +{ + JSONNode root = JSONNode::parse((const char *)dbei.pBlob); + if (root) { + m_wszFileName = root["f"].as_mstring().Detach(); + m_wszDescription = root["d"].as_mstring().Detach(); + } +} + +DB::FILE_BLOB::~FILE_BLOB() +{} + +void DB::FILE_BLOB::write(DB::EventInfo &dbei) +{ + JSONNode root; + root << WCHAR_PARAM("f", m_wszFileName) << WCHAR_PARAM("d", m_wszDescription ? m_wszDescription : L""); + + std::string text = root.write(); + dbei.cbBlob = (int)text.size() + 1; + dbei.pBlob = (uint8_t*)mir_realloc(dbei.pBlob, dbei.cbBlob); + memcpy(dbei.pBlob, text.c_str(), dbei.cbBlob); +} + +///////////////////////////////////////////////////////////////////////////////////////// +// Auth blob helper DB::AUTH_BLOB::AUTH_BLOB(MCONTACT hContact, LPCSTR nick, LPCSTR fname, LPCSTR lname, LPCSTR email, LPCSTR reason) : m_dwUin(0), diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index 68c051c394..92990599e4 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -572,7 +572,7 @@ Menu_SetVisible @662 ??0MDatabaseReadonly@@QAE@XZ @684 NONAME ??_7MDatabaseReadonly@@6B@ @685 NONAME ?GetEventById@MDatabaseReadonly@@UAGIPBD0@Z @686 NONAME -?EditEvent@MDatabaseReadonly@@UAGHIIPBUDBEVENTINFO@@@Z @688 NONAME +?EditEvent@MDatabaseReadonly@@UAGHIPBUDBEVENTINFO@@@Z @688 NONAME g_hevContactAdded @689 NONAME g_hevContactDeleted @690 NONAME g_hevEventAdded @691 NONAME @@ -837,3 +837,9 @@ Chat_IsMuted @941 NONAME ??0EventInfo@DB@@QAE@XZ @952 NONAME ??1EventInfo@DB@@QAE@XZ @953 NONAME ??BEventInfo@DB@@QBE_NXZ @954 NONAME +??0FILE_BLOB@DB@@QAE@ABVEventInfo@1@@Z @955 NONAME +??0FILE_BLOB@DB@@QAE@PB_W0@Z @956 NONAME +??1FILE_BLOB@DB@@QAE@XZ @957 NONAME +?getDescr@FILE_BLOB@DB@@QBEPB_WXZ @958 NONAME +?getName@FILE_BLOB@DB@@QBEPB_WXZ @959 NONAME +?write@FILE_BLOB@DB@@QAEXAAVEventInfo@2@@Z @960 NONAME diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index 6f977c0379..41bbf55dc6 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -572,7 +572,7 @@ Menu_SetVisible @662 ??0MDatabaseReadonly@@QEAA@XZ @684 NONAME ??_7MDatabaseReadonly@@6B@ @685 NONAME ?GetEventById@MDatabaseReadonly@@UEAAIPEBD0@Z @686 NONAME -?EditEvent@MDatabaseReadonly@@UEAAHIIPEBUDBEVENTINFO@@@Z @688 NONAME +?EditEvent@MDatabaseReadonly@@UEAAHIPEBUDBEVENTINFO@@@Z @688 NONAME g_hevContactAdded @689 NONAME g_hevContactDeleted @690 NONAME g_hevEventAdded @691 NONAME @@ -837,3 +837,9 @@ Chat_IsMuted @941 NONAME ??0EventInfo@DB@@QEAA@XZ @952 NONAME ??1EventInfo@DB@@QEAA@XZ @953 NONAME ??BEventInfo@DB@@QEBA_NXZ @954 NONAME +??0FILE_BLOB@DB@@QEAA@AEBVEventInfo@1@@Z @955 NONAME +??0FILE_BLOB@DB@@QEAA@PEB_W0@Z @956 NONAME +??1FILE_BLOB@DB@@QEAA@XZ @957 NONAME +?getDescr@FILE_BLOB@DB@@QEBAPEB_WXZ @958 NONAME +?getName@FILE_BLOB@DB@@QEBAPEB_WXZ @959 NONAME +?write@FILE_BLOB@DB@@QEAAXAEAVEventInfo@2@@Z @960 NONAME diff --git a/src/mir_app/src/proto_interface.cpp b/src/mir_app/src/proto_interface.cpp index a1b005d16b..07a276d20a 100644 --- a/src/mir_app/src/proto_interface.cpp +++ b/src/mir_app/src/proto_interface.cpp @@ -223,7 +223,7 @@ MEVENT PROTO_INTERFACE::RecvMsg(MCONTACT hContact, PROTORECVEVENT *pre) // if it's possible to find an existing event by its id, do that if ((GetCaps(PFLAGNUM_4) & PF4_SERVERMSGID) && pre->szMsgId != nullptr) { MEVENT hDbEvent = db_event_getById(m_szModuleName, pre->szMsgId); - if (hDbEvent == 0 || db_event_edit(hContact, hDbEvent, &dbei)) { + if (hDbEvent == 0 || db_event_edit(hDbEvent, &dbei)) { dbei.szId = pre->szMsgId; hDbEvent = db_event_add(hContact, &dbei); } diff --git a/src/mir_core/src/db.cpp b/src/mir_core/src/db.cpp index 3c92e965c2..e5d8d508ae 100644 --- a/src/mir_core/src/db.cpp +++ b/src/mir_core/src/db.cpp @@ -426,9 +426,9 @@ MIR_CORE_DLL(int) db_event_delete(MEVENT hDbEvent, bool bFromServer) return g_pCurrDb->DeleteEvent(hDbEvent); } -MIR_CORE_DLL(int) db_event_edit(MCONTACT hContact, MEVENT hDbEvent, const DBEVENTINFO *dbei) +MIR_CORE_DLL(int) db_event_edit(MEVENT hDbEvent, const DBEVENTINFO *dbei) { - return (g_pCurrDb == nullptr) ? 0 : g_pCurrDb->EditEvent(hContact, hDbEvent, dbei); + return (g_pCurrDb == nullptr) ? 0 : g_pCurrDb->EditEvent(hDbEvent, dbei); } MIR_CORE_DLL(MEVENT) db_event_first(MCONTACT hContact) -- cgit v1.2.3