Skip to content

Commit

Permalink
* Generalize message box hook function
Browse files Browse the repository at this point in the history
  • Loading branch information
iProgramMC committed Jun 4, 2024
1 parent d6eb97f commit 588b905
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
41 changes: 3 additions & 38 deletions src/windows/MessageList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1777,54 +1777,19 @@ void MessageList::DetermineMessageData(
}
}

static HHOOK g_MsgBoxHookTemp = NULL;

LRESULT MessageList::OpenLinkMsgBoxHook(int code, WPARAM wParam, LPARAM lParam)
{
if (code == HC_ACTION)
{
CWPRETSTRUCT* cwp = (CWPRETSTRUCT*)lParam;

if (cwp->message == WM_INITDIALOG) {
HWND hWnd = cwp->hwnd;

HWND item = GetDlgItem(hWnd, IDOK);
if (item)
SetWindowText(item, TmGetTString(IDS_EXCITED_YES));
}

LRESULT res = CallNextHookEx(g_MsgBoxHookTemp, code, wParam, lParam);

if (cwp->message == WM_NCDESTROY) {
UnhookWindowsHookEx(g_MsgBoxHookTemp);
g_MsgBoxHookTemp = NULL;
}

return res;
}

return CallNextHookEx(g_MsgBoxHookTemp, code, wParam, lParam);
}

void MessageList::ConfirmOpenLink(const std::string& link)
{
bool trust = GetLocalSettings()->CheckTrustedDomain(link);

if (!trust)
{
if (g_MsgBoxHookTemp) {
UnhookWindowsHookEx(g_MsgBoxHookTemp);
g_MsgBoxHookTemp = NULL;
}

g_MsgBoxHookTemp = SetWindowsHookEx(WH_CALLWNDPROCRET, MessageList::OpenLinkMsgBoxHook, NULL, GetCurrentThreadId());

TCHAR buffer[8192];
static TCHAR buffer[8192];
LPTSTR tstr = ConvertCppStringToTString(link);
WAsnprintf(buffer, _countof(buffer), TmGetTString(IDS_LINK_CONFIRM), tstr);
buffer[_countof(buffer) - 1] = 0;
free(tstr);

if (MessageBox(g_Hwnd, buffer, TmGetTString(IDS_HOLD_UP_CONFIRM), MB_ICONWARNING | MB_OKCANCEL) != IDOK)
if (MessageBoxHooked(g_Hwnd, buffer, TmGetTString(IDS_HOLD_UP_CONFIRM), MB_ICONWARNING | MB_OKCANCEL, IDOK, TmGetTString(IDS_EXCITED_YES)) != IDOK)
return;
}

Expand Down
2 changes: 0 additions & 2 deletions src/windows/MessageList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,6 @@ class MessageList

static COLORREF DrawMentionBackground(HDC hdc, RECT& rc, COLORREF chosenBkColor);

static LRESULT CALLBACK OpenLinkMsgBoxHook(int code, WPARAM wParam, LPARAM lParam);

// called by OnUpdateEmoji
static void InvalidateEmote(void* context, const Rect& rc);
};
70 changes: 70 additions & 0 deletions src/windows/WinUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,73 @@ void CenterWindow(HWND hWnd, HWND hRelativeTo)

MoveWindow(hWnd, rect1.left, rect1.top, rect1.right - rect1.left, rect1.bottom - rect1.top, TRUE);
}

#ifndef MINGW_SPECIFIC_HACKS
#include <mutex>
using Wmutex = std::mutex;
#else
#include <iprog/mutex.hpp>
using Wmutex = iprog::mutex;
#endif

static const LPCTSTR* s_msgBoxText;
static const int* s_msgBoxIDReplaced;

static Wmutex s_msgBoxMutex;
static int s_msgBoxCount;
static HHOOK s_msgBoxHook;

LRESULT CALLBACK MessageBoxHookFunc(int code, WPARAM wParam, LPARAM lParam)
{
if (code == HC_ACTION)
{
CWPRETSTRUCT* cwp = (CWPRETSTRUCT*)lParam;

if (cwp->message == WM_INITDIALOG) {
HWND hWnd = cwp->hwnd;

for (int i = 0; i < s_msgBoxCount; i++) {
SetDlgItemText(hWnd, s_msgBoxIDReplaced[i], s_msgBoxText[i]);
}
s_msgBoxCount = 0;
}

LRESULT res = CallNextHookEx(s_msgBoxHook, code, wParam, lParam);

if (cwp->message == WM_INITDIALOG) {
// just unhook the hook, we don't care about overriding any other behavior
UnhookWindowsHookEx(s_msgBoxHook);
s_msgBoxHook = NULL;

// mutex was locked before the MessageBox call!
#pragma warning(push)
#pragma warning(disable : 26110)
s_msgBoxMutex.unlock();
#pragma warning(pop)
}

return res;
}

return CallNextHookEx(s_msgBoxHook, code, wParam, lParam);
}

// This calls MessageBox() with the important difference that one of the buttons (specified by overID)
// will have its text changed via a hook.
int MessageBoxHooked(HWND hWnd, LPCTSTR title, LPCTSTR caption, int flags, int overCount, const int* overID, const LPCTSTR* overText)
{
s_msgBoxMutex.lock(); // will be unlocked in MessageBox's hook

s_msgBoxCount = overCount;
s_msgBoxText = overText;
s_msgBoxIDReplaced = overID;

s_msgBoxHook = SetWindowsHookEx(WH_CALLWNDPROCRET, MessageBoxHookFunc, NULL, GetCurrentThreadId());

return MessageBox(hWnd, title, caption, flags);
}

int MessageBoxHooked(HWND hWnd, LPCTSTR title, LPCTSTR caption, int flags, int overID, LPCTSTR overText)
{
return MessageBoxHooked(hWnd, title, caption, flags, 1, &overID, &overText);
}
2 changes: 2 additions & 0 deletions src/windows/WinUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ void DrawErrorBox(HDC hdc, RECT rect);
bool GetDataFromBitmap(HDC hdc, HBITMAP hbm, BYTE*& pBytes, int& width, int& height, int& bpp);
int HandleGestureMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, float mulDeltas = 1.0f);
std::string CutOutURLPath(const std::string& url);
int MessageBoxHooked(HWND hWnd, LPCTSTR title, LPCTSTR caption, int flags, int overID, LPCTSTR overText);
int MessageBoxHooked(HWND hWnd, LPCTSTR title, LPCTSTR caption, int flags, int overCount, const int* overID, const LPCTSTR* overText);

#ifdef USE_DEBUG_PRINTS
void DbgPrintW(const char* fmt, ...);
Expand Down

0 comments on commit 588b905

Please sign in to comment.