From cb8ff5c1221d3c820142cf2333face1ab8f03c93 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 7 Dec 2023 17:34:17 +0300 Subject: fixes #3679 (StopSpam/StopSpamMod: Jabber message query overflow) --- plugins/StopSpamPlus/src/events.cpp | 83 ++++++++++++--------------- plugins/StopSpamPlus/src/options.cpp | 12 ++-- plugins/StopSpamPlus/src/services.cpp | 7 +-- plugins/StopSpamPlus/src/stdafx.h | 16 +++--- plugins/StopSpamPlus/src/stopspam.cpp | 4 +- plugins/StopSpamPlus/src/utils.cpp | 34 +---------- plugins/StopSpamPlus/stopspam.vcxproj | 1 - plugins/StopSpamPlus/stopspam.vcxproj.filters | 3 - 8 files changed, 55 insertions(+), 105 deletions(-) (limited to 'plugins/StopSpamPlus') diff --git a/plugins/StopSpamPlus/src/events.cpp b/plugins/StopSpamPlus/src/events.cpp index fcea573c55..de5a5cfad9 100644 --- a/plugins/StopSpamPlus/src/events.cpp +++ b/plugins/StopSpamPlus/src/events.cpp @@ -14,22 +14,20 @@ int OnDbEventAdded(WPARAM, LPARAM lParam) // event is an auth request if (!(dbei.flags & DBEF_SENT) && !(dbei.flags & DBEF_READ) && dbei.eventType == EVENTTYPE_AUTHREQUEST) { - MCONTACT hcntct = DbGetAuthEventContact(&dbei); + MCONTACT hContact = DbGetAuthEventContact(&dbei); // if request is from unknown or not marked Answered contact //and if I don't sent message to this contact - if (!Contact::OnList(hcntct) && !g_plugin.getByte(hcntct, DB_KEY_ANSWERED) && !IsExistMyMessage(hcntct)) { + if (!Contact::OnList(hContact) && !g_plugin.getByte(hContact, DB_KEY_ANSWERED) && !g_plugin.getByte(hContact, DB_KEY_HASSENT)) { if (!g_plugin.HandleAuthReq) { - char *buf = mir_utf8encodeW(variables_parse(g_plugin.getReply(), hcntct).c_str()); - ProtoChainSend(hcntct, PSS_MESSAGE, 0, (LPARAM)buf); + char *buf = mir_utf8encodeW(variables_parse(g_plugin.getReply(), hContact).c_str()); + ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)buf); mir_free(buf); } - // ...send message - CallProtoService(dbei.szModule, PS_AUTHDENY, hDbEvent, (LPARAM)_T2A(variables_parse(g_plugin.getReply(), hcntct).c_str())); - - Contact::RemoveFromList(hcntct); - Contact::Hide(hcntct); + g_plugin.setDword(hContact, DB_KEY_HASAUTH, hDbEvent); + Contact::RemoveFromList(hContact); + Contact::Hide(hContact); if (!g_plugin.HistLog) db_event_delete(hDbEvent); return 1; @@ -41,32 +39,29 @@ int OnDbEventAdded(WPARAM, LPARAM lParam) int OnDbEventFilterAdd(WPARAM w, LPARAM l) { MCONTACT hContact = (MCONTACT)w; - DBEVENTINFO *dbei = (DBEVENTINFO*)l; + DBEVENTINFO *dbei = (DBEVENTINFO *)l; if (dbei == nullptr || dbei->szModule == nullptr) //fix potential DEP crash return 0; // if event is in protocol that is not despammed if (g_plugin.ProtoDisabled(dbei->szModule)) - // ...let the event go its way - return 0; + return 0; // ...let the event go its way // if event is not a message, or if the message has been read or sent... - if (dbei->flags & DBEF_SENT || dbei->flags & DBEF_READ || dbei->eventType != EVENTTYPE_MESSAGE) - // ...let the event go its way + if (dbei->eventType != EVENTTYPE_MESSAGE || (dbei->flags & DBEF_READ) != 0) return 0; - // if message is from known or marked Answered contact - if (g_plugin.getByte(hContact, DB_KEY_ANSWERED)) - // ...let the event go its way + if (dbei->flags & DBEF_SENT) { + g_plugin.setByte(hContact, DB_KEY_HASSENT, 1); return 0; + } - // checking if message from self-added contact - //Contact in Not in list icq group - if (Contact::OnList(hContact) && db_get_w(hContact, dbei->szModule, "SrvGroupId", -1) != 1) + // if message is from known or marked Answered contact + if (g_plugin.getByte(hContact, DB_KEY_ANSWERED) || g_plugin.getByte(hContact, DB_KEY_HASSENT)) return 0; - //if I sent message to this contact - if (IsExistMyMessage(hContact)) + // checking if message from self-added contact + if (Contact::OnList(hContact)) return 0; // if message is corrupted or empty it cannot be an answer. @@ -77,12 +72,12 @@ int OnDbEventFilterAdd(WPARAM w, LPARAM l) tstring message; if (dbei->flags & DBEF_UTF) { - wchar_t* msg_u = mir_utf8decodeW((char*)dbei->pBlob); + wchar_t *msg_u = mir_utf8decodeW((char *)dbei->pBlob); message = msg_u; mir_free(msg_u); } else { - wchar_t* msg_u = mir_a2u((char*)(dbei->pBlob)); + wchar_t *msg_u = mir_a2u((char *)(dbei->pBlob)); message = msg_u; mir_free(msg_u); } @@ -102,16 +97,15 @@ int OnDbEventFilterAdd(WPARAM w, LPARAM l) // unhide contact Contact::Hide(hContact, false); - // mark contact as Answered - g_plugin.setByte(hContact, DB_KEY_ANSWERED, 1); - - //add contact permanently - if (g_plugin.AddPermanent) + // add contact permanently and delete our temporary variables + if (g_plugin.AddPermanent) { Contact::PutOnList(hContact); + db_delete_module(hContact, MODULENAME); + } + else g_plugin.setByte(hContact, DB_KEY_ANSWERED, 1); // send congratulation - - char * buf = mir_utf8encodeW(variables_parse(g_plugin.getCongrats(), hContact).c_str()); + char *buf = mir_utf8encodeW(variables_parse(g_plugin.getCongrats(), hContact).c_str()); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)buf); mir_free(buf); @@ -129,12 +123,10 @@ int OnDbEventFilterAdd(WPARAM w, LPARAM l) // send question tstring q = pwszPrefix + variables_parse(g_plugin.getQuestion(), hContact); - - char * buf = mir_utf8encodeW(q.c_str()); + char *buf = mir_utf8encodeW(q.c_str()); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)buf); mir_free(buf); - // increment question count uint32_t questCount = g_plugin.getDword(hContact, DB_KEY_QUESTCOUNT); g_plugin.setDword(hContact, DB_KEY_QUESTCOUNT, questCount + 1); @@ -149,20 +141,17 @@ int OnDbEventFilterAdd(WPARAM w, LPARAM l) return 0; } -int OnDbContactSettingchanged(WPARAM hContact, LPARAM l) +int OnShutdown(WPARAM, LPARAM) { - DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING*)l; + for (auto &cc : Contacts()) { + if (Contact::OnList(cc)) + continue; - // if CList/NotOnList is being deleted then remove DB_KEY_ANSWERED - if (strcmp(cws->szModule, "CList")) - return 0; - if (strcmp(cws->szSetting, "NotOnList")) - return 0; - - if (!cws->value.type) { - g_plugin.delSetting(hContact, DB_KEY_ANSWERED); - g_plugin.delSetting(hContact, DB_KEY_QUESTCOUNT); + if (MEVENT hDbEvent = g_plugin.getByte(cc, DB_KEY_HASAUTH)) { + char *szProto = Proto_GetBaseAccountName(cc); + CallProtoService(szProto, PS_AUTHDENY, hDbEvent, (LPARAM)_T2A(variables_parse(g_plugin.getReply(), cc).c_str())); + } } - + return 0; -} +} \ No newline at end of file diff --git a/plugins/StopSpamPlus/src/options.cpp b/plugins/StopSpamPlus/src/options.cpp index 2d55ef9309..99981028c7 100644 --- a/plugins/StopSpamPlus/src/options.cpp +++ b/plugins/StopSpamPlus/src/options.cpp @@ -73,12 +73,12 @@ public: return true; } - void onHelp(CCtrlButton*) + void onHelp(CCtrlButton *) { variables_showhelp(m_hwnd, WM_COMMAND, VHF_FULLDLG | VHF_SETLASTSUBJECT, nullptr, nullptr); } - void onRestore(CCtrlButton*) + void onRestore(CCtrlButton *) { g_plugin.delSetting(g_plugin.AuthRepl.GetDBSettingName()); g_plugin.delSetting(g_plugin.Question.GetDBSettingName()); @@ -89,7 +89,7 @@ public: edtCongrat.SetText(g_plugin.getCongrats()); edtReply.SetText(g_plugin.getReply()); edtDivider.SetText(g_plugin.AnswSplitString.Default()); - + NotifyChange(); } }; @@ -148,14 +148,14 @@ public: continue; if (m_accounts.GetCheckState(i) == 0) - out << (char*)item.lParam << " "; + out << (char *)item.lParam << " "; } - g_plugin.DisabledProtoList = (char*)out.str().c_str(); + g_plugin.DisabledProtoList = (char *)out.str().c_str(); return true; } - void list_OnItemChanged(CCtrlListView::TEventInfo*) + void list_OnItemChanged(CCtrlListView::TEventInfo *) { if (m_bInitialized) NotifyChange(); diff --git a/plugins/StopSpamPlus/src/services.cpp b/plugins/StopSpamPlus/src/services.cpp index ed57888afb..1c3435988b 100644 --- a/plugins/StopSpamPlus/src/services.cpp +++ b/plugins/StopSpamPlus/src/services.cpp @@ -9,13 +9,10 @@ INT_PTR IsContactPassed(WPARAM hContact, LPARAM /*lParam*/) if (!g_plugin.ProtoDisabled(szProto)) return CS_PASSED; - if (g_plugin.getByte(hContact, DB_KEY_ANSWERED)) + if (g_plugin.getByte(hContact, DB_KEY_ANSWERED) || g_plugin.getByte(hContact, DB_KEY_HASSENT)) return CS_PASSED; - if (Contact::OnList(hContact) && db_get_w(hContact, szProto, "SrvGroupId", -1) != 1) - return CS_PASSED; - - if (IsExistMyMessage(hContact)) + if (Contact::OnList(hContact)) return CS_PASSED; return CS_NOTPASSED; diff --git a/plugins/StopSpamPlus/src/stdafx.h b/plugins/StopSpamPlus/src/stdafx.h index 84916a972f..75840d2c03 100644 --- a/plugins/StopSpamPlus/src/stdafx.h +++ b/plugins/StopSpamPlus/src/stdafx.h @@ -20,7 +20,6 @@ #include #include -#include #include typedef std::wstring tstring; @@ -32,6 +31,8 @@ typedef std::wstring tstring; #define MODULENAME LPGEN("StopSpam") #define DB_KEY_ANSWERED "Answered" +#define DB_KEY_HASAUTH "HasAuth" +#define DB_KEY_HASSENT "HasSent" #define DB_KEY_QUESTCOUNT "QuestionCount" struct CMPlugin : public PLUGIN @@ -57,14 +58,13 @@ struct CMPlugin : public PLUGIN }; // utils -tstring &GetDlgItemString(HWND hwnd, int id); -bool IsExistMyMessage(MCONTACT hContact); tstring variables_parse(const wchar_t *tstrFormat, MCONTACT hContact); tstring trim(tstring const &tstr, tstring const &trimChars = L" \f\n\r\t\v"); -INT_PTR IsContactPassed(WPARAM wParam, LPARAM /*lParam*/); -int OnDbEventAdded(WPARAM wParam, LPARAM lParam); -int OnDbEventFilterAdd(WPARAM w, LPARAM l); -int OnOptInit(WPARAM w, LPARAM l); -int OnDbContactSettingchanged(WPARAM hContact, LPARAM l); +INT_PTR IsContactPassed(WPARAM, LPARAM); +int OnDbEventAdded(WPARAM, LPARAM); +int OnDbEventFilterAdd(WPARAM, LPARAM); +int OnOptInit(WPARAM, LPARAM); +int OnShutdown(WPARAM, LPARAM); + #endif diff --git a/plugins/StopSpamPlus/src/stopspam.cpp b/plugins/StopSpamPlus/src/stopspam.cpp index c042e4fd4c..81de4f5c53 100644 --- a/plugins/StopSpamPlus/src/stopspam.cpp +++ b/plugins/StopSpamPlus/src/stopspam.cpp @@ -38,11 +38,9 @@ CMPlugin::CMPlugin() : int CMPlugin::Load() { - CreateServiceFunction(MS_STOPSPAM_CONTACTPASSED, IsContactPassed); - HookEvent(ME_DB_EVENT_ADDED, OnDbEventAdded); HookEvent(ME_DB_EVENT_FILTER_ADD, OnDbEventFilterAdd); HookEvent(ME_OPT_INITIALISE, OnOptInit); - HookEvent(ME_DB_CONTACT_SETTINGCHANGED, OnDbContactSettingchanged); + HookEvent(ME_SYSTEM_SHUTDOWN, OnShutdown); return 0; } diff --git a/plugins/StopSpamPlus/src/utils.cpp b/plugins/StopSpamPlus/src/utils.cpp index 427495026c..91378b7181 100644 --- a/plugins/StopSpamPlus/src/utils.cpp +++ b/plugins/StopSpamPlus/src/utils.cpp @@ -1,35 +1,5 @@ #include "stdafx.h" -tstring& GetDlgItemString(HWND hwnd, int id) -{ - HWND h = GetDlgItem(hwnd, id); - int len = GetWindowTextLength(h); - wchar_t * buf = new wchar_t[len + 1]; - GetWindowText(h, buf, len + 1); - static tstring s; - s = buf; - delete[]buf; - return s; -} - -bool IsExistMyMessage(MCONTACT hContact) -{ - DB::ECPTR pCursor(DB::Events(hContact)); - while (MEVENT hDbEvent = pCursor.FetchNext()) { - DBEVENTINFO dbei = {}; - if (db_event_get(hDbEvent, &dbei)) - break; - - if (dbei.flags & DBEF_SENT){ - // mark contact as Answered - g_plugin.setByte(hContact, DB_KEY_ANSWERED, 1); - // ...let the event go its way - return true; - } - } - return false; -} - tstring variables_parse(const wchar_t *tstrFormat, MCONTACT hContact) { if (ServiceExists(MS_VARS_FORMATSTRING)) { @@ -38,7 +8,7 @@ tstring variables_parse(const wchar_t *tstrFormat, MCONTACT hContact) fi.szFormat.w = wcsdup(tstrFormat); fi.hContact = hContact; fi.flags = FIF_UNICODE; - wchar_t *tszParsed = (wchar_t*)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0); + wchar_t *tszParsed = (wchar_t *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0); free(fi.szFormat.w); if (tszParsed) { tstring tstrResult = tszParsed; @@ -49,7 +19,7 @@ tstring variables_parse(const wchar_t *tstrFormat, MCONTACT hContact) return tstrFormat; } -tstring trim(const tstring &tstr, const tstring& trimChars) +tstring trim(const tstring &tstr, const tstring &trimChars) { size_t s = tstr.find_first_not_of(trimChars); size_t e = tstr.find_last_not_of(trimChars); diff --git a/plugins/StopSpamPlus/stopspam.vcxproj b/plugins/StopSpamPlus/stopspam.vcxproj index ca3ec31db4..e09b1892e4 100644 --- a/plugins/StopSpamPlus/stopspam.vcxproj +++ b/plugins/StopSpamPlus/stopspam.vcxproj @@ -28,7 +28,6 @@ - Create diff --git a/plugins/StopSpamPlus/stopspam.vcxproj.filters b/plugins/StopSpamPlus/stopspam.vcxproj.filters index 3227d52b41..b50f611162 100644 --- a/plugins/StopSpamPlus/stopspam.vcxproj.filters +++ b/plugins/StopSpamPlus/stopspam.vcxproj.filters @@ -8,9 +8,6 @@ Source Files - - Source Files - Source Files -- cgit v1.2.3