From 385c6ad2482ae2b5dfbc0c8fa2046080db713995 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 3 Jan 2025 18:57:43 +0300 Subject: fixes #3679 (StopSpam: Jabber message query overflow) --- plugins/StopSpamPlus/src/events.cpp | 44 +++++++++++++++++++++++++++-------- plugins/StopSpamPlus/src/options.cpp | 21 ++++++++++------- plugins/StopSpamPlus/src/resource.h | 9 ++++--- plugins/StopSpamPlus/src/stdafx.h | 29 +++++++++++++++++------ plugins/StopSpamPlus/src/stopspam.cpp | 21 ++++++++++++----- 5 files changed, 89 insertions(+), 35 deletions(-) (limited to 'plugins/StopSpamPlus/src') diff --git a/plugins/StopSpamPlus/src/events.cpp b/plugins/StopSpamPlus/src/events.cpp index de5a5cfad9..9b7cf5cbb7 100644 --- a/plugins/StopSpamPlus/src/events.cpp +++ b/plugins/StopSpamPlus/src/events.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" +static std::map g_times; + int OnDbEventAdded(WPARAM, LPARAM lParam) { MEVENT hDbEvent = (MEVENT)lParam; @@ -19,16 +21,16 @@ int OnDbEventAdded(WPARAM, LPARAM lParam) // if request is from unknown or not marked Answered contact //and if I don't sent message to this contact 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(), hContact).c_str()); - ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)buf); - mir_free(buf); - } + if (!g_plugin.bHandleAuthReq) + ProtoChainSend(hContact, PSS_MESSAGE, 0, ptrA(mir_utf8encodeW(variables_parse(g_plugin.getReply(), hContact).c_str()))); + + if (g_plugin.iAnswerTimeout) + g_times[hContact] = time(0); g_plugin.setDword(hContact, DB_KEY_HASAUTH, hDbEvent); Contact::RemoveFromList(hContact); Contact::Hide(hContact); - if (!g_plugin.HistLog) + if (!g_plugin.bHistLog) db_event_delete(hDbEvent); return 1; } @@ -93,12 +95,12 @@ int OnDbEventFilterAdd(WPARAM w, LPARAM l) // if answer not empty if (answer.length() > 0) { // if message equal right answer... - if (g_plugin.AnswNotCaseSens ? !mir_wstrcmpi(message.c_str(), answer.c_str()) : !mir_wstrcmp(message.c_str(), answer.c_str())) { + if (g_plugin.bAnswNotCaseSens ? !mir_wstrcmpi(message.c_str(), answer.c_str()) : !mir_wstrcmp(message.c_str(), answer.c_str())) { // unhide contact Contact::Hide(hContact, false); // add contact permanently and delete our temporary variables - if (g_plugin.AddPermanent) { + if (g_plugin.bAddPermanent) { Contact::PutOnList(hContact); db_delete_module(hContact, MODULENAME); } @@ -119,7 +121,7 @@ int OnDbEventFilterAdd(WPARAM w, LPARAM l) // if message message does not contain infintite talk protection prefix // and question count for this contact is less then maximum const wchar_t *pwszPrefix = TranslateT("StopSpam automatic message:\r\n"); - if ((!g_plugin.InfTalkProtection || tstring::npos == message.find(pwszPrefix)) && (!g_plugin.MaxQuestCount || g_plugin.getDword(hContact, DB_KEY_QUESTCOUNT) < g_plugin.MaxQuestCount)) { + if ((!g_plugin.bInfTalkProtection || tstring::npos == message.find(pwszPrefix)) && (!g_plugin.iMaxQuestCount || g_plugin.getDword(hContact, DB_KEY_QUESTCOUNT) < g_plugin.iMaxQuestCount)) { // send question tstring q = pwszPrefix + variables_parse(g_plugin.getQuestion(), hContact); @@ -141,13 +143,35 @@ int OnDbEventFilterAdd(WPARAM w, LPARAM l) return 0; } +void CMPlugin::Impl::OnTimer(CTimer *) +{ + int iTimeout = g_plugin.iAnswerTimeout; + if (!iTimeout) + return; + + time_t now = time(0); + + for (auto &it : g_times) { + if (now - it.second < iTimeout) + continue; + + if (MEVENT hDbEvent = g_plugin.getDword(it.first, DB_KEY_HASAUTH)) { + char *szProto = Proto_GetBaseAccountName(it.first); + CallProtoService(szProto, PS_AUTHDENY, hDbEvent, (LPARAM)_T2A(variables_parse(g_plugin.getReply(), it.first).c_str())); + + Netlib_Logf(0, "StopSpam: removing temporary contact %d", it.first); + db_delete_contact(it.first); + } + } +} + int OnShutdown(WPARAM, LPARAM) { for (auto &cc : Contacts()) { if (Contact::OnList(cc)) continue; - if (MEVENT hDbEvent = g_plugin.getByte(cc, DB_KEY_HASAUTH)) { + if (MEVENT hDbEvent = g_plugin.getDword(cc, DB_KEY_HASAUTH)) { char *szProto = Proto_GetBaseAccountName(cc); CallProtoService(szProto, PS_AUTHDENY, hDbEvent, (LPARAM)_T2A(variables_parse(g_plugin.getReply(), cc).c_str())); } diff --git a/plugins/StopSpamPlus/src/options.cpp b/plugins/StopSpamPlus/src/options.cpp index 99981028c7..0eb3114c39 100644 --- a/plugins/StopSpamPlus/src/options.cpp +++ b/plugins/StopSpamPlus/src/options.cpp @@ -4,27 +4,30 @@ const wchar_t pluginDescription[] = LPGENW("No more spam! Robots can't go! Only class COptMainDlg : public CDlgBase { - CCtrlEdit edtCount, edtDescr; + CCtrlEdit edtDescr; + CCtrlSpin spinCount, spinTimeout; CCtrlCheck chk1, chk2, chk3, chk4, chk6; public: COptMainDlg() : CDlgBase(g_plugin, IDD_MAIN), - edtCount(this, ID_MAXQUESTCOUNT), edtDescr(this, ID_DESCRIPTION), + spinCount(this, IDC_SPIN1, 10), + spinTimeout(this, IDC_SPIN2, 60), chk1(this, ID_INFTALKPROT), chk2(this, ID_ADDPERMANENT), chk3(this, ID_HANDLEAUTHREQ), chk4(this, ID_NOTCASESENS), chk6(this, ID_HISTORY_LOG) { - CreateLink(edtCount, g_plugin.MaxQuestCount); - - CreateLink(chk1, g_plugin.InfTalkProtection); - CreateLink(chk2, g_plugin.AddPermanent); - CreateLink(chk3, g_plugin.HandleAuthReq); - CreateLink(chk4, g_plugin.AnswNotCaseSens); - CreateLink(chk6, g_plugin.HistLog); + CreateLink(spinCount, g_plugin.iMaxQuestCount); + CreateLink(spinTimeout, g_plugin.iAnswerTimeout); + + CreateLink(chk1, g_plugin.bInfTalkProtection); + CreateLink(chk2, g_plugin.bAddPermanent); + CreateLink(chk3, g_plugin.bHandleAuthReq); + CreateLink(chk4, g_plugin.bAnswNotCaseSens); + CreateLink(chk6, g_plugin.bHistLog); } bool OnInitDialog() override diff --git a/plugins/StopSpamPlus/src/resource.h b/plugins/StopSpamPlus/src/resource.h index a4e7c05b61..82c2cd8694 100644 --- a/plugins/StopSpamPlus/src/resource.h +++ b/plugins/StopSpamPlus/src/resource.h @@ -1,6 +1,6 @@ //{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. -// Used by C:\Users\georg\DiskW\miranda-ng\plugins\StopSpamPlus\res\stopspam.rc +// Used by W:\miranda-ng\plugins\StopSpamPlus\res\stopspam.rc // #define IDD_MESSAGES 101 #define IDD_MAIN 102 @@ -13,6 +13,7 @@ #define ID_AUTHREPL 1007 #define ID_MAXQUESTCOUNT 1008 #define IDC_PROTO 1009 +#define ID_MAXQUESTCOUNT2 1009 #define ID_INFTALKPROT 1010 #define ID_ADDPERMANENT 1011 #define ID_HANDLEAUTHREQ 1012 @@ -20,14 +21,16 @@ #define ID_DIVIDER 1014 #define IDC_VARS 1015 #define ID_HISTORY_LOG 1017 +#define IDC_SPIN1 1018 +#define IDC_SPIN2 1019 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 105 +#define _APS_NEXT_RESOURCE_VALUE 106 #define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1018 +#define _APS_NEXT_CONTROL_VALUE 1019 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif diff --git a/plugins/StopSpamPlus/src/stdafx.h b/plugins/StopSpamPlus/src/stdafx.h index 75840d2c03..e4e3992f5b 100644 --- a/plugins/StopSpamPlus/src/stdafx.h +++ b/plugins/StopSpamPlus/src/stdafx.h @@ -8,17 +8,19 @@ #include #include #include +#include #include +#include #include #include -#include -#include -#include +#include #include +#include +#include +#include +#include #include -#include -#include #include @@ -39,10 +41,22 @@ struct CMPlugin : public PLUGIN { CMPlugin(); - CMOption InfTalkProtection, AddPermanent, HandleAuthReq, AnswNotCaseSens, HistLog; + struct Impl + { + Impl() : + timerAnswer(Miranda_GetSystemWindow(), UINT_PTR(this)) + { + timerAnswer.OnEvent = Callback(this, &Impl::OnTimer); + } + + CTimer timerAnswer; + void OnTimer(CTimer *); + } m_impl; + + CMOption bInfTalkProtection, bAddPermanent, bHandleAuthReq, bAnswNotCaseSens, bHistLog; CMOption Question, AuthRepl, Answer, Congratulation, AnswSplitString; CMOption DisabledProtoList; - CMOption MaxQuestCount; + CMOption iMaxQuestCount, iAnswerTimeout; const wchar_t* getQuestion(); const wchar_t* getReply(); @@ -55,6 +69,7 @@ struct CMPlugin : public PLUGIN } int Load() override; + int Unload() override; }; // utils diff --git a/plugins/StopSpamPlus/src/stopspam.cpp b/plugins/StopSpamPlus/src/stopspam.cpp index 81de4f5c53..fd6e3e8ebc 100644 --- a/plugins/StopSpamPlus/src/stopspam.cpp +++ b/plugins/StopSpamPlus/src/stopspam.cpp @@ -25,22 +25,31 @@ CMPlugin::CMPlugin() : Answer(MODULENAME, "Answer", L"nospam"), Congratulation(MODULENAME, "Congratulation"), DisabledProtoList(MODULENAME, "DisabledProtoList", "MetaContacts RSSNews"), - InfTalkProtection(MODULENAME, "InfTalkProtection", 1), - AddPermanent(MODULENAME, "AddPermanent", 0), - HandleAuthReq(MODULENAME, "HandleAuthReq", 0), - MaxQuestCount(MODULENAME, "MaxQuestCount", 2), - AnswNotCaseSens(MODULENAME, "AnswNotCaseSens", 1), + bInfTalkProtection(MODULENAME, "InfTalkProtection", 1), + bAddPermanent(MODULENAME, "AddPermanent", 0), + bHandleAuthReq(MODULENAME, "HandleAuthReq", 0), + iMaxQuestCount(MODULENAME, "MaxQuestCount", 2), + iAnswerTimeout(MODULENAME, "AnswerTimeout", 5), + bAnswNotCaseSens(MODULENAME, "AnswNotCaseSens", 1), AnswSplitString(MODULENAME, "AnswSplitString", L"|"), - HistLog(MODULENAME, "HistLog", 0) + bHistLog(MODULENAME, "HistLog", 0) {} ///////////////////////////////////////////////////////////////////////////////////////// int CMPlugin::Load() { + m_impl.timerAnswer.Start(60000); + HookEvent(ME_DB_EVENT_ADDED, OnDbEventAdded); HookEvent(ME_DB_EVENT_FILTER_ADD, OnDbEventFilterAdd); HookEvent(ME_OPT_INITIALISE, OnOptInit); HookEvent(ME_SYSTEM_SHUTDOWN, OnShutdown); return 0; } + +int CMPlugin::Unload() +{ + m_impl.timerAnswer.Stop(); + return 0; +} -- cgit v1.2.3