summaryrefslogtreecommitdiff
path: root/plugins/AssocMgr/src/dde.cpp
blob: c080d76c472e67ea6850a4fb8757933cf6fc4b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*

'File Association Manager'-Plugin for Miranda IM

Copyright (C) 2005-2007 H. Herkenrath

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program (AssocMgr-License.txt); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

/* Conversation */
extern HINSTANCE hInst;
static HWND hwndDdeMsg;
/* Misc */
static HANDLE hHookModulesLoaded, hHookPreShutdown;

/************************* Open Handler ***************************/

// pszFilePath needs to be allocated using mir_alloc()
static void __stdcall FileActionAsync(void *param)
{
	wchar_t *pszFilePath = (wchar_t*)param;
	/* invoke main handler */
	switch (InvokeFileHandler(pszFilePath)) { /* pszFilePath is always a long path name */
	case 0: /* success */ break;
	case CALLSERVICE_NOTFOUND:
		ShowInfoMessage(NIIF_ERROR, Translate("Miranda NG could not open file"), Translate("Miranda NG was not able to open \"%S\".\n\nThere is no registered handler for this file type."), pszFilePath);
		break;
	default:
		ShowInfoMessage(NIIF_ERROR, Translate("Miranda NG could not open file"), Translate("Miranda NG was not able to open \"%S\".\n\nThe file could not be processed."), pszFilePath);
	}
	mir_free(pszFilePath); /* async param */
}

// pszUrl needs to be allocated using mir_alloc()
static void __stdcall UrlActionAsync(void *param)
{
	wchar_t *pszUrl = (wchar_t*)param;
	/* invoke main handler */
	switch (InvokeUrlHandler(pszUrl)) {
	case 0: /* success */ break;
	case CALLSERVICE_NOTFOUND:
		ShowInfoMessage(NIIF_ERROR, Translate("Miranda NG could not open URL"), Translate("Miranda NG was not able to open \"%S\".\n\nThere is no registered handler for this URL type."), pszUrl);
		break;
	default:
		ShowInfoMessage(NIIF_ERROR, Translate("Miranda NG could not open URL"), Translate("Miranda NG was not able to open \"%S\".\n\nThe given URL is invalid and cannot be parsed."), pszUrl);
	}
	mir_free(pszUrl); /* async param */
}

/************************* Conversation ***************************/

#define DDEMESSAGETIMEOUT      30000
#define WNDCLASS_DDEMSGWINDOW  L"MirandaDdeMsgWindow"

// returned pointer points into a substring of ppszString
// returns an empty string if the string does not have enough arguments
static wchar_t* GetExecuteParam(wchar_t **ppszString)
{
	bool fQuoted = (**ppszString == '"');
	wchar_t *pszParam = *ppszString;
	if (fQuoted)
		pszParam++;
	wchar_t *p = wcschr(pszParam, (wchar_t)(fQuoted ? '"' : ','));
	if (p != nullptr) {
		*(p++) = 0;
		if (fQuoted && *p == ',') p++;
	}
	else p = &pszParam[mir_wstrlen(pszParam)];
	*ppszString = p;
	return pszParam;
}

static LRESULT CALLBACK DdeMessageWindow(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_DDE_INITIATE:
		{
			ATOM hSzApp = LOWORD(lParam); /* no UnpackDDElParam() here */
			ATOM hSzTopic = HIWORD(lParam);
			if ((hSzApp == GlobalFindAtom(DDEAPP) && hSzTopic == GlobalFindAtom(DDETOPIC)) || !hSzApp) {
				hSzApp = GlobalAddAtom(DDEAPP);
				hSzTopic = GlobalAddAtom(DDETOPIC);
				if (hSzApp && hSzTopic)
					/* PackDDElParam() only for posted msgs */
					SendMessage((HWND)wParam, WM_DDE_ACK, (WPARAM)hwnd, MAKELPARAM(hSzApp, hSzTopic));
				if (hSzApp) GlobalDeleteAtom(hSzApp);
				if (hSzTopic) GlobalDeleteAtom(hSzTopic);
			}
		}
		return 0;

	case WM_DDE_EXECUTE: /* posted message */
		HGLOBAL hCommand;
		if (UnpackDDElParam(msg, lParam, nullptr, (PUINT_PTR)&hCommand)) {
			/* ANSI execute command can't happen for shell */
			if (IsWindowUnicode((HWND)wParam)) {
				wchar_t *pszCommand = (wchar_t*)GlobalLock(hCommand);
				if (pszCommand != nullptr) {
					wchar_t *pszAction = GetExecuteParam(&pszCommand);
					wchar_t *pszArg = GetExecuteParam(&pszCommand);
					if (pszArg != nullptr) {
						/* we are inside miranda here, we make it async so the shell does
							* not timeout regardless what the plugins try to do. */
						if (!mir_wstrcmpi(pszAction, L"file"))
							CallFunctionAsync(FileActionAsync, mir_wstrdup(pszArg));
						else if (!mir_wstrcmpi(pszAction, L"url"))
							CallFunctionAsync(UrlActionAsync, mir_wstrdup(pszArg));
					}
					GlobalUnlock(hCommand);
				}
			}

			DDEACK ack;
			memset(&ack, 0, sizeof(ack));
			lParam = ReuseDDElParam(lParam, msg, WM_DDE_ACK, *(PUINT)&ack, (UINT_PTR)hCommand);
			if (!PostMessage((HWND)wParam, WM_DDE_ACK, (WPARAM)hwnd, lParam)) {
				GlobalFree(hCommand);
				FreeDDElParam(WM_DDE_ACK, lParam);
			}
		}
		return 0;

	case WM_DDE_TERMINATE:
		PostMessage((HWND)wParam, msg, (WPARAM)hwnd, 0); /* ack reply */
		return 0;

	case WM_DDE_REQUEST:
	case WM_DDE_ADVISE:
	case WM_DDE_UNADVISE:
	case WM_DDE_POKE:
		/* fail safely for those to avoid memory leak in lParam */
		{
			ATOM hSzItem;
			DDEACK ack;
			memset(&ack, 0, sizeof(ack));
			if (UnpackDDElParam(msg, lParam, nullptr, (PUINT_PTR)&hSzItem)) {
				lParam = ReuseDDElParam(lParam, msg, WM_DDE_ACK, *(PUINT)&ack, (UINT)hSzItem);
				if (!PostMessage((HWND)wParam, WM_DDE_ACK, (WPARAM)hwnd, lParam)) {
					if (hSzItem) GlobalDeleteAtom(hSzItem);
					FreeDDElParam(WM_DDE_ACK, lParam);
				}
			}
			return 0;
		}
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}

// CloseHandle() the return value
static HANDLE StartupMainProcess(wchar_t *pszDatabasePath)
{
	wchar_t *p, szPath[MAX_PATH];

	/* we are inside RunDll32 here */
	if (!GetModuleFileName(hInst, szPath, _countof(szPath))) return nullptr;
	p = wcsrchr(szPath, '\\');
	if (p != nullptr) { *p = 0; p = wcsrchr(szPath, '\\'); }
	if (p == nullptr) return nullptr;
	mir_wstrcpy(++p, L"miranda32.exe");

	/* inherit startup data from RunDll32 process */
	STARTUPINFO si;
	GetStartupInfo(&si);
	PROCESS_INFORMATION pi;
	if (!CreateProcess(szPath, pszDatabasePath, nullptr, nullptr, TRUE, GetPriorityClass(GetCurrentProcess()), nullptr, nullptr, &si, &pi))
		return nullptr;
	CloseHandle(pi.hThread);
	return pi.hProcess;
}

// entry point for RunDll32, this is also WaitForDDEW
EXTERN_C __declspec(dllexport) void CALLBACK WaitForDDE(HWND, HINSTANCE, wchar_t *pszCmdLine, int)
{
	// executed to initialize path for delay-loaded libraries
	DynamicLoadInit();

	/* wait for dde window to be available (avoiding race condition) */
	HANDLE pHandles[2];
	pHandles[0] = CreateEvent(nullptr, TRUE, FALSE, WNDCLASS_DDEMSGWINDOW);
	if (pHandles[0] != nullptr) {
		pHandles[1] = StartupMainProcess(pszCmdLine); /* obeys nCmdShow using GetStartupInfo() */
		if (pHandles[1] != nullptr) {
			DWORD dwTick = GetTickCount();
			/* either process terminated or dde window created */
			if (WaitForMultipleObjects(_countof(pHandles), pHandles, FALSE, DDEMESSAGETIMEOUT) == WAIT_OBJECT_0) {
				dwTick = GetTickCount() - dwTick;
				if (dwTick < DDEMESSAGETIMEOUT)
					WaitForInputIdle(pHandles[1], DDEMESSAGETIMEOUT - dwTick);
			}
			CloseHandle(pHandles[1]);
		}
		CloseHandle(pHandles[0]);
	}
	/* shell called WaitForInputIdle() on us to detect when dde is ready,
		* we are ready now: exit helper process */
}

/************************* Misc ***********************************/

static int DdePreShutdown(WPARAM, LPARAM)
{
	/* dde needs to be stopped before any plugins are unloaded */
	if (hwndDdeMsg != nullptr) DestroyWindow(hwndDdeMsg);
	UnregisterClass(WNDCLASS_DDEMSGWINDOW, hInst);
	return 0;
}

static int DdeModulesLoaded2(WPARAM, LPARAM)
{
	/* create message window */
	WNDCLASS wcl;
	wcl.lpfnWndProc = DdeMessageWindow;
	wcl.cbClsExtra = 0;
	wcl.cbWndExtra = 0;
	wcl.hInstance = hInst;
	wcl.hCursor = nullptr;
	wcl.lpszClassName = WNDCLASS_DDEMSGWINDOW;
	wcl.hbrBackground = nullptr;
	wcl.hIcon = nullptr;
	wcl.lpszMenuName = nullptr;
	wcl.style = 0;
	RegisterClass(&wcl);
	/* Note: use of HWND_MESSAGE does not fit for DDE as the window must be a top-level one */
	hwndDdeMsg = CreateWindow(WNDCLASS_DDEMSGWINDOW, nullptr, 0, 0, 0, 0, 0, nullptr, nullptr, hInst, nullptr);

	/* make known dde startup code is passed */
	HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, WNDCLASS_DDEMSGWINDOW);
	if (hEvent != nullptr) {
		SetEvent(hEvent);
		CloseHandle(hEvent);
	}

	CleanupRegTreeBackupSettings();
	CleanupMimeTypeAddedSettings();
	CleanupAssocEnabledSettings();
	return 0;
}

static int DdeModulesLoaded(WPARAM, LPARAM)
{
	/* dde needs to be loaded after all the other plugins got loaded,
	 * hook again to get the latest position in chain */
	UnhookEvent(hHookModulesLoaded);
	hHookModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, DdeModulesLoaded2);
	return 0;
}

void InitDde(void)
{
	hHookModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, DdeModulesLoaded);
	hHookPreShutdown = HookEvent(ME_SYSTEM_PRESHUTDOWN, DdePreShutdown);
}

void UninitDde(void)
{
	UnhookEvent(hHookModulesLoaded);
	UnhookEvent(hHookPreShutdown);
}