summaryrefslogtreecommitdiff
path: root/src/modules/utils/openurl.cpp
blob: fb10c5f43cd749f2eee828e567b3531266084af8 (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2009 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

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; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "..\..\core\commonheaders.h"
#include <ctype.h>

#define DDEMESSAGETIMEOUT  1000
#define WNDCLASS_DDEMSGWINDOW  _T("MirandaDdeMsgWindow")

struct DdeMsgWindowData {
	int fAcked,fData;
	HWND hwndDde;
};

static LRESULT CALLBACK DdeMessageWindow(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	struct DdeMsgWindowData *dat;
	ATOM hSzItem;
	HGLOBAL hDdeData;

	dat=(struct DdeMsgWindowData*)GetWindowLongPtr(hwnd,0);
	switch(msg) {
		case WM_DDE_ACK:
			dat->fAcked=1;
			dat->hwndDde=(HWND)wParam;
			return 0;
		case WM_DDE_DATA:
			UnpackDDElParam(msg,lParam,(PUINT_PTR)&hDdeData,(PUINT_PTR)&hSzItem);
			dat->fData=1;
			if (hDdeData) {
				DDEDATA *data;
				int release;
				data=(DDEDATA*)GlobalLock(hDdeData);
				if (data->fAckReq) {
					DDEACK ack={0};
					PostMessage((HWND)wParam,WM_DDE_ACK,(WPARAM)hwnd,PackDDElParam(WM_DDE_ACK,*(PUINT)&ack,(UINT)hSzItem));
				}
				else GlobalDeleteAtom(hSzItem);
				release=data->fRelease;
				GlobalUnlock(hDdeData);
				if (release) GlobalFree(hDdeData);
			}
			else GlobalDeleteAtom(hSzItem);
			return 0;
	}
	return DefWindowProc(hwnd,msg,wParam,lParam);
}

static int DoDdeRequest(const char *szItemName,HWND hwndDdeMsg)
{
	ATOM hSzItemName;
	DWORD timeoutTick,thisTick;
	MSG msg;
	struct DdeMsgWindowData *dat=(struct DdeMsgWindowData*)GetWindowLongPtr(hwndDdeMsg,0);

	hSzItemName=GlobalAddAtomA(szItemName);
	if (!PostMessage(dat->hwndDde,WM_DDE_REQUEST,(WPARAM)hwndDdeMsg,MAKELPARAM(CF_TEXT,hSzItemName))) {
		GlobalDeleteAtom(hSzItemName);
		return 1;
	}
	timeoutTick=GetTickCount()+5000;
	dat->fData=0; dat->fAcked=0;
	do {
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		if (dat->fData || dat->fAcked) break;
		thisTick=GetTickCount();
		if (thisTick>timeoutTick) break;
	} while (MsgWaitForMultipleObjects(0,NULL,FALSE,timeoutTick-thisTick,QS_ALLINPUT) == WAIT_OBJECT_0);

	if (!dat->fData) {
		GlobalDeleteAtom(hSzItemName);
		return 1;
	}
	return 0;
}

//see Q160957 and http://developer.netscape.com/docs/manuals/communicator/DDE/index.htm
static int DdeOpenUrl(const char *szBrowser,char *szUrl,int newWindow,HWND hwndDdeMsg)
{
	ATOM hSzBrowser,hSzTopic;
	DWORD_PTR dwResult;
	char *szItemName;
	struct DdeMsgWindowData *dat=(struct DdeMsgWindowData*)GetWindowLongPtr(hwndDdeMsg,0);

	hSzBrowser=GlobalAddAtomA(szBrowser);
	hSzTopic=GlobalAddAtomA("WWW_OpenURL");
	dat->fAcked=0;
	if (!SendMessageTimeout(HWND_BROADCAST,WM_DDE_INITIATE,(WPARAM)hwndDdeMsg,MAKELPARAM(hSzBrowser,hSzTopic),SMTO_ABORTIFHUNG|SMTO_NORMAL,DDEMESSAGETIMEOUT,&dwResult)
	   || !dat->fAcked) {
		GlobalDeleteAtom(hSzTopic);
		GlobalDeleteAtom(hSzBrowser);
		return 1;
	}
	szItemName=(char*)mir_alloc(lstrlenA(szUrl)+7);
	wsprintfA(szItemName,"\"%s\",,%d",szUrl,newWindow?0:-1);
	if (DoDdeRequest(szItemName,hwndDdeMsg)) {
		mir_free(szItemName);
		GlobalDeleteAtom(hSzTopic);
		GlobalDeleteAtom(hSzBrowser);
		return 1;
	}
	PostMessage(dat->hwndDde,WM_DDE_TERMINATE,(WPARAM)hwndDdeMsg,0);
	GlobalDeleteAtom(hSzTopic);
	GlobalDeleteAtom(hSzBrowser);
	mir_free(szItemName);
	return 0;
}

typedef struct {
    char *szUrl;
    int newWindow;
} TOpenUrlInfo;

static void OpenURLThread(void *arg)
{
   TOpenUrlInfo *hUrlInfo = (TOpenUrlInfo*)arg;
	char *szResult;
	HWND hwndDdeMsg;
	struct DdeMsgWindowData msgWndData={0};
	char *pszProtocol;
	HKEY hKey;
	char szSubkey[80];
	char szCommandName[MAX_PATH];
	DWORD dataLength;
	int success=0;
    
   if (!hUrlInfo->szUrl) return;
	hwndDdeMsg=CreateWindow(WNDCLASS_DDEMSGWINDOW,_T(""),0,0,0,0,0,NULL,NULL,hMirandaInst,NULL);
	SetWindowLongPtr(hwndDdeMsg,0,(LONG_PTR)&msgWndData);

	if (!_strnicmp(hUrlInfo->szUrl,"ftp:",4) || !_strnicmp(hUrlInfo->szUrl,"ftp.",4)) pszProtocol="ftp";
	if (!_strnicmp(hUrlInfo->szUrl,"mailto:",7)) pszProtocol="mailto";
	if (!_strnicmp(hUrlInfo->szUrl,"news:",5)) pszProtocol="news";
	else pszProtocol="http";
	wsprintfA(szSubkey,"%s\\shell\\open\\command",pszProtocol);
	if (RegOpenKeyExA(HKEY_CURRENT_USER,szSubkey,0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS
	   || RegOpenKeyExA(HKEY_CLASSES_ROOT,szSubkey,0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS) {
		dataLength=SIZEOF(szCommandName);
		if (RegQueryValueEx(hKey,NULL,NULL,NULL,(PBYTE)szCommandName,&dataLength) == ERROR_SUCCESS) {
			_strlwr(szCommandName);
			if (strstr(szCommandName,"mozilla") || strstr(szCommandName,"netscape"))
				success=(DdeOpenUrl("mozilla",hUrlInfo->szUrl,hUrlInfo->newWindow,hwndDdeMsg) == 0 || DdeOpenUrl("netscape",hUrlInfo->szUrl,hUrlInfo->newWindow,hwndDdeMsg) == 0);
			else if (strstr(szCommandName,"iexplore") || strstr(szCommandName,"msimn"))
				success=0 == DdeOpenUrl("iexplore",hUrlInfo->szUrl,hUrlInfo->newWindow,hwndDdeMsg);
			else if (strstr(szCommandName,"opera"))
				success=0 == DdeOpenUrl("opera",hUrlInfo->szUrl,hUrlInfo->newWindow,hwndDdeMsg);
			//opera's the default anyway
		}
		RegCloseKey(hKey);
	}

	DestroyWindow(hwndDdeMsg);
	if (success) return;

	//wack a protocol on it
	if ((isalpha(hUrlInfo->szUrl[0]) && hUrlInfo->szUrl[1] == ':') || hUrlInfo->szUrl[0] == '\\') {
		szResult=(char*)mir_alloc(lstrlenA(hUrlInfo->szUrl)+9);
		wsprintfA(szResult,"file:///%s",hUrlInfo->szUrl);
	}
	else {
		int i;
		for (i=0;isalpha(hUrlInfo->szUrl[i]);i++);
		if (hUrlInfo->szUrl[i] == ':') szResult=mir_strdup(hUrlInfo->szUrl);
		else {
			if (!_strnicmp(hUrlInfo->szUrl,"ftp.",4)) {
				szResult=(char*)mir_alloc(lstrlenA(hUrlInfo->szUrl)+7);
				wsprintfA(szResult,"ftp://%s",hUrlInfo->szUrl);
			}
			else {
				szResult=(char*)mir_alloc(lstrlenA(hUrlInfo->szUrl)+8);
				wsprintfA(szResult,"http://%s",hUrlInfo->szUrl);
			}
		}
	}
	ShellExecuteA(NULL, "open", szResult, NULL, NULL, SW_SHOWDEFAULT);
	mir_free(szResult);
	mir_free(hUrlInfo->szUrl);
	mir_free(hUrlInfo);
	return;
}

static INT_PTR OpenURL(WPARAM wParam,LPARAM lParam) {
	TOpenUrlInfo *hUrlInfo = (TOpenUrlInfo*)mir_alloc(sizeof(TOpenUrlInfo));
	hUrlInfo->szUrl = (char*)lParam?mir_strdup((char*)lParam):NULL;
	hUrlInfo->newWindow = (int)wParam;
	forkthread(OpenURLThread, 0, (void*)hUrlInfo);
	return 0;
}

int InitOpenUrl(void)
{
	WNDCLASS wcl;
	wcl.lpfnWndProc=DdeMessageWindow;
	wcl.cbClsExtra=0;
	wcl.cbWndExtra=sizeof(void*);
	wcl.hInstance=hMirandaInst;
	wcl.hCursor=NULL;
	wcl.lpszClassName=WNDCLASS_DDEMSGWINDOW;
	wcl.hbrBackground=NULL;
	wcl.hIcon=NULL;
	wcl.lpszMenuName=NULL;
	wcl.style=0;
	RegisterClass(&wcl);
	CreateServiceFunction(MS_UTILS_OPENURL,OpenURL);
	return 0;
}